      subroutine ALightCombinatorial()
      implicit none
      include '../testarea/LeafGasHybridFit.h'
      integer i,ilastrubp1,ilastrubis1,ilastrubp2,ilastrubis2,
     &ilastrubp3,ilastrubis3,ilastrubp4,ilastrubis4,
     &ilastrubp5,ilastrubis5,ilastrubp6,ilastrubis6,
     &ilastrubp7,ilastrubis7,ilastrubp8,ilastrubis8,
     &ilastrubp9,ilastrubis9,ilastrubp10,ilastrubis10,
     &ilastrubp11,ilastrubis11,ilastrubp12,ilastrubis12,
     &ilastrubp13,ilastrubis13,ilastrubp14,ilastrubis14,
     &ilastrubp15,ilastrubis15,k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,
     &k11,k12,k13,k14,k15
!common block variables: numALightcurves,nALightPoints(numALightcurves),
!ALightiphotolimit(nALightPoints,numALightcurves),ialightmin(numALightcurves),
!ialightmax(numALightcurves),ialightrubpmin(numALightcurves),ialightrubpmax(numALightcurves),
!ialightorder(numALightcurves)

      if(numALightcurves.eq.0)then
!no conventional A/Light curves. go to free-style measurements directly and then return
        call FreeCombinatorial()
        return
      endif
!(before 17/09/2014 remarks.) Assume rubp, rubisco and tpu limitations in the order of (rubp, rubisco, tpu)
!but any limitation can be missing in any light response curves. The nALightPoints data in each light 
!response curve must be ordered from low to high PAR. When ordered in such, the three limitation states
!should occur in the order of (rubp, rubisco, tpu)
!
!17/09/2014 Wenting found (RuBP, TPU, Rubisco) is more likely for A/Light curves if Ci decreases with
!increased light. Thus the following changes are made:
!we generally assume the points of an A/Light curve are lined up in a sequence of (RuBP, TPU and Rubisco),
!which is indicated by ialightorder=0. However, if Ci increases steadily from nstartalight to the end, we 
!assume a sequence of (RuBP, Rubisco and TPU),which is indicated by ialightorder=2.
      do ilastrubp1=ialightrubpmin(1),ialightrubpmax(1)
        do i=1,ilastrubp1
          ALightiphotolimit(i,1)=2
        enddo
        k1=max0(ilastrubp1,ialightmin(1))
      do 1 ilastrubis1=k1,ialightmax(1)
        do i=ilastrubp1+1,ilastrubis1
          ALightiphotolimit(i,1)=3-ialightorder(1)
        enddo
        do i=ilastrubis1+1,nALightPoints(1)
          ALightiphotolimit(i,1)=1+ialightorder(1)
        enddo
        if(numALightcurves.eq.1)then
          call FreeCombinatorial()
          goto 1
        endif

      do ilastrubp2=ialightrubpmin(2),ialightrubpmax(2)
        do i=1,ilastrubp2
          ALightiphotolimit(i,2)=2
        enddo
        k2=max0(ilastrubp2,ialightmin(2))
      do 2 ilastrubis2=k2,ialightmax(2)
        do i=ilastrubp2+1,ilastrubis2
          ALightiphotolimit(i,2)=3-ialightorder(2)
        enddo
        do i=ilastrubis2+1,nALightPoints(2)
          ALightiphotolimit(i,2)=1+ialightorder(2)
        enddo
        if(numALightcurves.eq.2)then
          call FreeCombinatorial()
          goto 2
        endif

      do ilastrubp3=ialightrubpmin(3),ialightrubpmax(3)
        do i=1,ilastrubp3
          ALightiphotolimit(i,3)=2
        enddo
        k3=max0(ilastrubp3,ialightmin(3))
      do 3 ilastrubis3=k3,ialightmax(3)
        do i=ilastrubp3+1,ilastrubis3
          ALightiphotolimit(i,3)=3-ialightorder(3)
        enddo
        do i=ilastrubis3+1,nALightPoints(3)
          ALightiphotolimit(i,3)=1+ialightorder(3)
        enddo
        if(numALightcurves.eq.3)then
          call FreeCombinatorial()
          goto 3
        endif

      do ilastrubp4=ialightrubpmin(4),ialightrubpmax(4)
        do i=1,ilastrubp4
          ALightiphotolimit(i,4)=2
        enddo
        k4=max0(ilastrubp4,ialightmin(4))
      do 4 ilastrubis4=k4,ialightmax(4)
        do i=ilastrubp4+1,ilastrubis4
          ALightiphotolimit(i,4)=3-ialightorder(4)
        enddo
        do i=ilastrubis4+1,nALightPoints(4)
          ALightiphotolimit(i,4)=1+ialightorder(4)
        enddo
        if(numALightcurves.eq.4)then
          call FreeCombinatorial()
          goto 4
        endif

      do ilastrubp5=ialightrubpmin(5),ialightrubpmax(5)
        do i=1,ilastrubp5
          ALightiphotolimit(i,5)=2
        enddo
        k5=max0(ilastrubp5,ialightmin(5))
      do 5 ilastrubis5=k5,ialightmax(5)
        do i=ilastrubp5+1,ilastrubis5
          ALightiphotolimit(i,5)=3-ialightorder(5)
        enddo
        do i=ilastrubis5+1,nALightPoints(5)
          ALightiphotolimit(i,5)=1+ialightorder(5)
        enddo
        if(numALightcurves.eq.5)then
          call FreeCombinatorial()
          goto 5
        endif

      do ilastrubp6=ialightrubpmin(6),ialightrubpmax(6)
        do i=1,ilastrubp6
          ALightiphotolimit(i,6)=2
        enddo
        k6=max0(ilastrubp6,ialightmin(6))
      do 6 ilastrubis6=k6,ialightmax(6)
        do i=ilastrubp6+1,ilastrubis6
          ALightiphotolimit(i,6)=3-ialightorder(6)
        enddo
        do i=ilastrubis6+1,nALightPoints(6)
          ALightiphotolimit(i,6)=1+ialightorder(6)
        enddo
        if(numALightcurves.eq.6)then
          call FreeCombinatorial()
          goto 6
        endif

      do ilastrubp7=ialightrubpmin(7),ialightrubpmax(7)
        do i=1,ilastrubp7
          ALightiphotolimit(i,7)=2
        enddo
        k7=max0(ilastrubp7,ialightmin(7))
      do 7 ilastrubis7=k7,ialightmax(7)
        do i=ilastrubp7+1,ilastrubis7
          ALightiphotolimit(i,7)=3-ialightorder(7)
        enddo
        do i=ilastrubis7+1,nALightPoints(7)
          ALightiphotolimit(i,7)=1+ialightorder(7)
        enddo
        if(numALightcurves.eq.7)then
          call FreeCombinatorial()
          goto 7
        endif

      do ilastrubp8=ialightrubpmin(8),ialightrubpmax(8)
        do i=1,ilastrubp8
          ALightiphotolimit(i,8)=2
        enddo
        k8=max0(ilastrubp8,ialightmin(8))
      do 8 ilastrubis8=k8,ialightmax(8)
        do i=ilastrubp8+1,ilastrubis8
          ALightiphotolimit(i,8)=3-ialightorder(8)
        enddo
        do i=ilastrubis8+1,nALightPoints(8)
          ALightiphotolimit(i,8)=1+ialightorder(8)
        enddo
        if(numALightcurves.eq.8)then
          call FreeCombinatorial()
          goto 8
        endif

      do ilastrubp9=ialightrubpmin(9),ialightrubpmax(9)
        do i=1,ilastrubp9
          ALightiphotolimit(i,9)=2
        enddo
        k9=max0(ilastrubp9,ialightmin(9))
      do 9 ilastrubis9=k9,ialightmax(9)
        do i=ilastrubp9+1,ilastrubis9
          ALightiphotolimit(i,9)=3-ialightorder(9)
        enddo
        do i=ilastrubis9+1,nALightPoints(9)
          ALightiphotolimit(i,9)=1+ialightorder(9)
        enddo
        if(numALightcurves.eq.9)then
          call FreeCombinatorial()
          goto 9
        endif

      do ilastrubp10=ialightrubpmin(10),ialightrubpmax(10)
        do i=1,ilastrubp10
          ALightiphotolimit(i,10)=2
        enddo
        k10=max0(ilastrubp10,ialightmin(10))
      do 10 ilastrubis10=k10,ialightmax(10)
        do i=ilastrubp10+1,ilastrubis10
          ALightiphotolimit(i,10)=3-ialightorder(10)
        enddo
        do i=ilastrubis10+1,nALightPoints(10)
          ALightiphotolimit(i,10)=1+ialightorder(10)
        enddo
        if(numALightcurves.eq.10)then
          call FreeCombinatorial()
          goto 10
        endif

      do ilastrubp11=ialightrubpmin(11),ialightrubpmax(11)
        do i=1,ilastrubp11
          ALightiphotolimit(i,11)=2
        enddo
        k11=max0(ilastrubp11,ialightmin(11))
      do 11 ilastrubis11=k11,ialightmax(11)
        do i=ilastrubp11+1,ilastrubis11
          ALightiphotolimit(i,11)=3-ialightorder(11)
        enddo
        do i=ilastrubis11+1,nALightPoints(11)
          ALightiphotolimit(i,11)=1+ialightorder(11)
        enddo
        if(numALightcurves.eq.11)then
          call FreeCombinatorial()
          goto 11
        endif

      do ilastrubp12=ialightrubpmin(12),ialightrubpmax(12)
        do i=1,ilastrubp12
          ALightiphotolimit(i,12)=2
        enddo
        k12=max0(ilastrubp12,ialightmin(12))
      do 12 ilastrubis12=k12,ialightmax(12)
        do i=ilastrubp12+1,ilastrubis12
          ALightiphotolimit(i,12)=3-ialightorder(12)
        enddo
        do i=ilastrubis12+1,nALightPoints(12)
          ALightiphotolimit(i,12)=1+ialightorder(12)
        enddo
        if(numALightcurves.eq.12)then
          call FreeCombinatorial()
          goto 12
        endif

      do ilastrubp13=ialightrubpmin(13),ialightrubpmax(13)
        do i=1,ilastrubp13
          ALightiphotolimit(i,13)=2
        enddo
        k13=max0(ilastrubp13,ialightmin(13))
      do 13 ilastrubis13=k13,ialightmax(13)
        do i=ilastrubp13+1,ilastrubis13
          ALightiphotolimit(i,13)=3-ialightorder(13)
        enddo
        do i=ilastrubis13+1,nALightPoints(13)
          ALightiphotolimit(i,13)=1+ialightorder(13)
        enddo
        if(numALightcurves.eq.13)then
          call FreeCombinatorial()
          goto 13
        endif

      do ilastrubp14=ialightrubpmin(14),ialightrubpmax(14)
        do i=1,ilastrubp14
          ALightiphotolimit(i,14)=2
        enddo
        k14=max0(ilastrubp14,ialightmin(14))
      do 14 ilastrubis14=k14,ialightmax(14)
        do i=ilastrubp14+1,ilastrubis14
          ALightiphotolimit(i,14)=3-ialightorder(14)
        enddo
        do i=ilastrubis14+1,nALightPoints(14)
          ALightiphotolimit(i,14)=1+ialightorder(14)
        enddo
        if(numALightcurves.eq.14)then
          call FreeCombinatorial()
          goto 14
        endif

      do ilastrubp15=ialightrubpmin(15),ialightrubpmax(15)
        do i=1,ilastrubp15
          ALightiphotolimit(i,15)=2
        enddo
        k15=max0(ilastrubp15,ialightmin(15))
      do 15 ilastrubis15=k15,ialightmax(15)
        do i=ilastrubp15+1,ilastrubis15
          ALightiphotolimit(i,15)=3-ialightorder(15)
        enddo
        do i=ilastrubis15+1,nALightPoints(15)
          ALightiphotolimit(i,15)=1+ialightorder(15)
        enddo
        if(numALightcurves.eq.15)then
          call FreeCombinatorial()
          goto 15
        endif
15    continue
      enddo
14    continue
      enddo
13    continue
      enddo
12    continue
      enddo
11    continue
      enddo
10    continue
      enddo
9     continue
      enddo
8     continue
      enddo
7     continue
      enddo
6     continue
      enddo
5     continue
      enddo
4     continue
      enddo
3     continue
      enddo
2     continue
      enddo
1     continue
      enddo
      return
      end subroutine ALightCombinatorial
