      subroutine pam_parameters(nsamples,fo,fm,fs,measlight,anet,
     &actiniclight,tempK,yield_ps2,yield_npq,qlake,qpuddle,kps2_norm,
     &knpq_norm,fo_dark,fm_dark,resp_dark,tempK_dark)
      implicit none
!Calculate fluorescence parameters
!The dark-adapted measurements must be put in the beginning of the data section and the
!corresponding par must be set to zero. If the dark-adapted measurements are sampled
!multiple times, these multiple samples must be put immediately one after the other.
      integer nsamples
      double precision fo(nsamples),fm(nsamples),fs(nsamples),
     &measlight(nsamples),anet(nsamples),actiniclight(nsamples),
     &tempK(nsamples),yield_ps2(nsamples),yield_npq(nsamples),
     &qlake(nsamples),qpuddle(nsamples),kps2_norm(nsamples),
     &knpq_norm(nsamples),fo_dark,fm_dark,resp_dark,tempK_dark
! 
!==============Inputs==========================================
!nsamples:     The total number of samples
!fo:           The fluorescence yield in the dark (zero PARi). It is measured on either
!                 a dark-adapted leaf or a previously illuminated leaf with the actinic light
!                 turned off and a far-red light applied to energize PSI to drain electrons from
!                 PSII and reoxidize QA. In other words, here fo can be either fo or fo'(to be indicated by PAR value)
!fm:           The fluorescence yield with the all PSII reaction centers closed (all QAs fully
!                 reduced) by a saturating pulse of light. It is either fm or fm' (to be indicated by PAR value)
!fs:           The steady-state fluorescence yield of an illuminated leaf.
!measlight     The measuring light (umol photons m-2s-1), not used as of 3/19/2015
!anet:         The net photosynthetical rate (umol/m2/s)
!actiniclight: The total incident actinic photosynthetically active radiation (umol photons /m2/s)
!tempK:        The temperature of each measurement (K)
!
!==============Outputs=========================================  
!yield_ps2:    The photochemical yield of PSII
!yield_npq:    The yield of regulated nonphotochemical quenching
!qlake:        The fraction of open PSII reaction centers based on the lake model
!qpuddle:      The fraction of open PSII reaction centers based on the puddle model
!kps2_norm:    The rate constant of photochemical quenching, normalized by the sum of fluorescence rate constant kf and
!                 intrinsic thermal dissipation rate constant kd. That is, kps2_norm = kp/(kf+kd)
!knpq_norm:    The rate constant of regulated nonphotochemical quenching, normalized by the sum of fluorescence rate constant kf and
!                 intrinsic thermal dissipation rate constant kd. That is, knpq_norm = knqp/(kf+kd).
!                 knpq_norm is simply the NPQ parameter commonly used in the literature.
!fo_dark:      The dark-adapted fo
!fm_dark:      The dark-adapted fm
!resp_dark:    The dark respiration rate (umol/m2/s)
!tempK_dark:    The temperature of the dark measurement (K)
!
!We don't calculate qn because it has no clear physical / biological meaning)
!
!
      integer i,j,k,n
      double precision fs_dark,threshold
!
      do i=1,nsamples
        if(measlight(i).gt.0.0d0)then
          if(fo(i).gt.0.0d0)fo(i)=fo(i)/dabs(measlight(i))
          if(fm(i).gt.0.0d0)fm(i)=fm(i)/dabs(measlight(i))
          if(fs(i).gt.0.0d0)fs(i)=fs(i)/dabs(measlight(i))
        endif
      enddo
!Find the dark-adapted fo and fm
!We assume the following:
!- The first measurement that has a zero actiniclight is a dark-adapted measurement.
!- Any measurements that immediately follow the first dark-adapated measurement and have zero actiniclight are 
!- repeated samples of dark-adated measurements.
      threshold=0.001d0
      i=1
10    if(dabs(actiniclight(i)).lt.threshold)then
        j=i
        goto 20
      endif
      if(i.lt.nsamples)then
        i=i+1
        goto 10
      endif
!no dark-adapted measurements
      fo_dark=-9999.0d0
      fm_dark=-9999.0d0
      fs_dark=-9999.0d0
      resp_dark=-9999.0d0
      tempK_dark=-9999.0d0
      goto 40
20    j=j+1
      if(j.gt.nsamples)goto 30
      if(dabs(actiniclight(j)).gt.threshold)goto 30
      if(j.lt.nsamples)goto 20
      j=j+1
30    j=j-1
!
      fo_dark=0.0d0
      n=0
      do k=i,j
        if(fo(k).gt.0.0d0)then
          n=n+1
          fo_dark=fo_dark+fo(k)
        endif
      enddo
      if(n.eq.0)then
        fo_dark=-9999.0d0
      else
        fo_dark=fo_dark/dble(n)
      endif
!
      fm_dark=0.0d0
      n=0
      do k=i,j
        if(fm(k).gt.0.0d0)then
          n=n+1
          fm_dark=fm_dark+fm(k)
        endif
      enddo
      if(n.eq.0)then
        fm_dark=-9999.0d0
      else
        fm_dark=fm_dark/dble(n)
      endif
!
      fs_dark=0.0d0
      n=0
      do k=i,j
        if(fs(k).gt.0.0d0)then
          n=n+1
          fs_dark=fs_dark+fs(k)
        endif
      enddo
      if(n.eq.0)then
        fs_dark=-9999.0d0
      else
        fs_dark=fs_dark/dble(n)
      endif
!
      resp_dark=0.0d0
      n=0
      do k=i,j
        if(anet(k).lt.0.0d0.and.dabs(anet(k)+9999.0d0).gt.0.01d0)then
          n=n+1
          resp_dark=resp_dark+anet(k)
        endif
      enddo
      if(n.eq.0)then
        resp_dark=-9999.0d0
      else
        resp_dark=dabs(resp_dark/dble(n))
      endif
!
      tempK_dark=0.0d0
      n=0
      do k=i,j
        if(tempK(k).gt.0.0d0)then
          n=n+1
          tempK_dark=tempK_dark+tempK(k)
        endif
      enddo
      if(n.eq.0)then
        tempK_dark=-9999.0d0
      else
        tempK_dark=tempK_dark/dble(n)
      endif
!
!for dark-adapted measurements, fo_dark and fs_dark are the same
      if(fo_dark.lt.0.0d0)fo_dark=fs_dark
!
40    do i=1,nsamples
        if(fo(i).lt.0.0d0.and.fo_dark.gt.0.0d0)then
!We use Oxborough and Baker (1997) Photosynthesis Research 54: 135-142 in case when Fo' in the light
!is not measured.
          if(fm_dark.gt.0.0d0.and.fm(i).gt.0.0d0)fo(i)=
     &fo_dark/(1.0d0-fo_dark/fm_dark+fo_dark/fm(i))
        endif
!
        yield_ps2(i)=-9999.0d0
        yield_npq(i)=-9999.0d0
        qpuddle(i)=-9999.0d0
        qlake(i)=-9999.0d0
        if(fm(i).gt.0.0d0.and.fs(i).gt.0.0d0)then
          yield_ps2(i)=(fm(i)-fs(i))/fm(i)
          if(fm_dark.gt.0.0d0)yield_npq(i)=fs(i)/fm(i)-fs(i)/fm_dark
          if(fo(i).gt.0.0d0)then
            qpuddle(i)=(fm(i)-fs(i))/(fm(i)-fo(i))
            qlake(i)=qpuddle(i)*fo(i)/fs(i)
          endif 
        endif
        knpq_norm(i)=-9999.0d0
        kps2_norm(i)=-9999.0d0
        if(fm_dark.gt.0.0d0.and.fm(i).gt.0.0d0)then
          knpq_norm(i)=fm_dark/fm(i)-1.0d0
          if(fs(i).gt.0.0d0)
     &kps2_norm(i)=fm_dark*(1.0d0/fs(i)-1.0d0/fm(i))
        endif
      enddo
      return
      end subroutine pam_parameters
