!Photosynthetic, Internal and Stomatal Conductance Analyses of Leaves (PISCAL)
!
!Created by:            Lianhong Gu
!                       Environmental Sciences Dvision
!                       Oak Ridge National Laboratory
!                       Oak Ridge, TN 37831
!                       lianhong-gu@ornl.gov
!with support from Department of Energy Office of Science, Biological
!and Environmental Research Program
!
!PISCAL first created 10 July 2008
!Paralle PISCAL 20 Feb 2013
!Updated 24 Jan 1014
!()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()
      program main
      implicit none
      integer dataunit,spareunit,ierr(2),runvalues(8),ipos1,ipos2,
     &ntotfiles,noutputfiles,i,j,k,indexunit(20),ic3c4cam
      character rundate*8,runtime*10,runzone*5,longchar*5000
      character*100 datapath,outpath,storein,storeout,
     &ACidata(8000)
      character*50 AllACiFiles,outputfile(20)
      
! Set input / output directory      
      parameter(datapath=
     &'../input/',
!     &'/home/l2g/jimei/',
!     &'/home/l2g/ngeetropics/gamboa/curves/',
!     &'/home/l2g/ngeetropics/metropolitano/curves/',
!     &'/home/l2g/ngeetropics/fortsherman/curves/',
!     &'/home/l2g/ngeetropics/kelsey/curves/',
!     &   '/home/l2g/SingleLeafModel/ACiSimulation/hybrid/',
!     &'/home/l2g/leafres/hybriddata/nicksmith/data/',
!     &'/home/l2g/leafres/hybriddata/dwestonpoplus/data3/',
!     &'/home/l2g/leafres/hybriddata/cernusak/2014data/',
!     &'/home/l2g/leafres/hybriddata/hanjimei/',
!     &'/home/l2g/junk/',
!     &   '/home/l2g/leafres/hybriddata/Berner/',
!     &   '/home/l2g/leafres/hybriddata/huidafeng/',
!     &'/home/l2g/leafres/hybriddata/dwestonpoplus/data_Pn_tree_1_195_v2/
!     &'/home/l2g/SingleLeafModel/ACiSimulation/hybrid/',
!     &   '/home/l2g/dataassim/leaf/data/moflux/2008/inputs/',
!     &   '/home/l2g/leafres/hybriddata/sphagnum/2014data1/',
!     &   '/home/l2g/dataassim/leaf/data/moflux/2010/inputs/',
!for moflux data, 2004-2008 requires correction of Ci. Other years do not
!     &   '/home/l2g/dataassim/leaf/data/LawData/inputs/',
!     &   '/home/l2g/dataassim/leaf/data/dweston/inputs/',
!     &   '/home/l2g/dataassim/leaf/data/johnbaker/inputs/',
!     &   '/home/l2g/dataassim/leaf/data/martins/inputs/',
!     &   '/home/l2g/dataassim/leaf/data/benzi/inputs/',
!     &   '/home/l2g/dataassim/leaf/data/loos/inputs/',
!     &   '/home/l2g/dataassim/leaf/data/ellsworth/inputs/',

!     &   '/home/l2g/dataassim/leaf/data/fromleafweb/inputs/',
!     &   '/home/l2g/SingleLeafModel/ACiSimulation/testkco/',
!     &   '/home/l2g/SingleLeafModel/ACiSimulation/rwprch/',
!     &   '/home/l2g/dataassim/leaf/data/panama/sept2012/inputs/',
!     &'/home/l2g/dataassim/leaf/data/williams/inputs/',
!     &    '/home/l2g/dataassim/leaf/test/',
!     &   '/home/l2g/SingleLeafModel/ACiSimulation/Martins/',
!     &   '/home/l2g/SingleLeafModel/ACiSimulation/collantes/',
!     &   '/home/l2g/dataassim/leaf/data/dweston/inputs/',
!     &   '/home/l2g/GEMSiS/curves/',
     &   outpath=
     &'../output/fitresult/touser/',
!     &'/home/l2g/jimei/',
!     &'/home/l2g/ngeetropics/gamboa/results/',
!     &'/home/l2g/ngeetropics/metropolitano/results/',
!     &'/home/l2g/ngeetropics/fortsherman/results/',
!     &'/home/l2g/ngeetropics/kelsey/results/',
!     &'/home/l2g/leafres/hybriddata/dwestonpoplus/data3/',
!     &   '/home/l2g/SingleLeafModel/ACiSimulation/wenting/',
!     &'/home/l2g/leafres/testdata/',
!     &'/home/l2g/leafres/hybriddata/hanjimei/',
!
!     &   '/home/l2g/leafres/hybriddata/Berner/',
!     &   '/home/l2g/SingleLeafModel/ACiSimulation/hybrid/',
!     &   '/home/l2g/leafres/hybriddata/nicksmith/results/',

!     &   '/home/l2g/leafres/hybriddata/huidafeng/',
!     &'/home/l2g/leafres/hybriddata/dwestonpoplus/data_Pn_tree_1_195_v2/
!     &',
!     &   '/home/l2g/dataassim/leaf/data/moflux/2008/outputs/',    
!     &   '/home/l2g/dataassim/leaf/data/moflux/2012/outputs/',
!     &   '/home/l2g/dataassim/leaf/data/LawData/outputs/',
!     &  '/home/l2g/dataassim/leaf/data/dweston/outputs/',
!     &  '/home/l2g/dataassim/leaf/data/johnbaker/outputs/',
!     &  '/home/l2g/dataassim/leaf/data/martins/outputs/',
!     &  '/home/l2g/dataassim/leaf/data/benzi/outputs/',
!     &   '/home/l2g/dataassim/leaf/data/loos/outputs/',
!     &   '/home/l2g/dataassim/leaf/data/ellsworth/outputs/',
!     &'/home/l2g/leafres/hybriddata/sphagnum/2014results1/',
!     &'/home/l2g/junk/',
!     &   '/home/l2g/SingleLeafModel/ACiSimulation/testkco/',
!     &   '/home/l2g/SingleLeafModel/ACiSimulation/rwprch/results/',
!     &   '/home/l2g/dataassim/leaf/data/panama/sept2012/outputs/',
!     &'/home/l2g/dataassim/leaf/data/panama/sept2012/outputs/rwprch/',
!     &   '/home/l2g/mpitest/',
!     &'/home/l2g/dataassim/leaf/data/williams/outputs/',
!     &   '/home/l2g/dataassim/leaf/data/fromleafweb/outputs/withpad/',
!     &    '/home/l2g/dataassim/leaf/test/',
!     &   '/home/l2g/SingleLeafModel/ACiSimulation/collantes/',
!     &   '/home/l2g/SingleLeafModel/ACiSimulation/Martins/',
!     &   '/home/l2g/dataassim/leaf/data/dweston/outputs/',
!     &   '/home/l2g/GEMSiS/results/',
!     &storein='/home/l2g/leafweb/users/curves/',
!     &storeout='/home/l2g/leafweb/users/results/',

     &storein='../output/clninput/',
     &storeout='../output/fitresult/nottouser/',

!     &storein='/home/l2g/jimei/',
!     &storeout='/home/l2g/jimei/',
!     &storein='/home/l2g/dataassim/leaf/data/ellsworth/outputs/',
!     &storeout='/home/l2g/dataassim/leaf/data/ellsworth/outputs/',

     &AllACiFiles='../piscal.cfg')
!     &AllACiFiles='/home/l2g/jimei/piscal.cfg')
!---------------End of variable declaration----------------
      ierr(1)=-1
      ierr(2)=-1
!read A/Ci curve names stored in AllACiFiles
      dataunit=1
      spareunit=3
      open(unit=dataunit,status='scratch')
      open(unit=spareunit,file=trim(AllACiFiles))
      read(spareunit,fmt=300,err=90,end=90)longchar
      rewind(spareunit)
2     read(spareunit,fmt=300,err=90,end=5)longchar
      if(longchar.eq.''.or.longchar.eq.' ')goto 2
3     k=index(longchar,char(13))
      if(k.gt.0)then
!DOS text format, convert it to unix format
        longchar(k:k+len(char(10))-1)=char(10)
        goto 3
      endif
      write(dataunit,310)trim(longchar)
      goto 2
5     close(spareunit)
      rewind(dataunit)
      ntotfiles=1
      ic3c4cam=-9999
10    read(dataunit,fmt=300,end=20)longchar
      if(ntotfiles.eq.1.and.ic3c4cam.lt.0)then
        i=0
        if((index(longchar,'_photosynthesis_leafweb')+
     &index(longchar,'_Photosynthesis_leafweb')+
     &index(longchar,'_Photosynthesis_LeafWeb')).gt.0)then
          i=index(longchar,'c3')+index(longchar,'C3')
          if(i.gt.0)then
            ic3c4cam=1
          else
            i=index(longchar,'c4')+index(longchar,'C4')
            if(i.gt.0)then
              ic3c4cam=2
            else
              i=index(longchar,'cam')+index(longchar,'caM')+
     &index(longchar,'cAm')+index(longchar,'cAM')+
     &index(longchar,'Cam')+index(longchar,'CaM')+
     &index(longchar,'CAm')+index(longchar,'CAM')
              if(i.gt.0)ic3c4cam=3
            endif
          endif
        endif
        if(i.gt.0)goto 10
!if no indication is provided, c3 photosynthesis is assumed and the first line
!contains the name of the first data file
        ic3c4cam=1
      endif
      i=len(longchar)
      j=0
15    j=j+1
      if(ichar(longchar(j:j)).ge.33.and.ichar(longchar(j:j)).le.127)then
        ipos1=j
      else
        if(j.ge.i)goto 10
        goto 15
      endif
      j=i+1
16    j=j-1
      if(ichar(longchar(j:j)).ge.33.and.ichar(longchar(j:j)).le.127)then
        ipos2=j
      else
        if(j.le.1)goto 10
        goto 16
      endif
      ACidata(ntotfiles)=longchar(ipos1:ipos2)
      ntotfiles=ntotfiles+1
      goto 10
20    ntotfiles=ntotfiles-1
      close(dataunit)
      outputfile(1)='leafgasparameters.csv'
      outputfile(2)='leafgascomparison.csv'
      outputfile(3)='stomwuecicaparameters.csv'
      outputfile(4)='stomcomparison.csv'
      outputfile(5)='wuecicacomparison.csv'
      outputfile(6)='fluorescencefit.csv'
      outputfile(7)='fluoresparameters.csv'
      outputfile(8)='aciempfitparameters.csv'
      outputfile(9)='alightempfitparameters.csv'
      outputfile(10)='warningmessage'
      outputfile(11)='errormessage'
      noutputfiles=11
      do i=1,noutputfiles
        if(ic3c4cam.eq.1)outputfile(i)='C3_'//outputfile(i)
        if(ic3c4cam.eq.2)outputfile(i)='C4_'//outputfile(i)
        if(ic3c4cam.eq.3)outputfile(i)='CAM_'//outputfile(i)
        indexunit(i)=i+9
      enddo
      do i=1,noutputfiles
        open(unit=indexunit(i),file=trim(outpath)//trim(outputfile(i)))
      enddo
      call ToLeafGasOptimization(ic3c4cam,ntotfiles,ACidata,dataunit,
     &spareunit,datapath,indexunit,ierr)
      do i=1,noutputfiles
        close(indexunit(i))
      enddo
      do j=1,noutputfiles
        open(unit=2,file=trim(outpath)//trim(outputfile(j)))
        read(2,*,end=70)
        if(j.le.(noutputfiles-2))then
          read(2,*,end=70)
          read(2,*,end=70)
        endif
        close(2)
        goto 80
70      close(2,status='delete')
80    enddo
!----------------------------------------------------------
!intercept the data
90    if(ierr(1).ne.0)then
        if(ierr(1).eq.-1)then
          close(spareunit)
          open(unit=spareunit,file=trim(outpath)//'errormessage')
          write(spareunit,*)'No data files to analyze'
          close(spareunit)
          goto 450
        endif
        do i=1,ntotfiles
          open(unit=1,file=trim(datapath)//'clean'//trim(ACidata(i)))
          close(1,status='delete')
        enddo
        goto 450
      endif
      call date_and_time(rundate,runtime,runzone,runvalues)
      do i=1,ntotfiles
        open(unit=1,file=trim(datapath)//'clean'//trim(ACidata(i)))
        open(unit=2,file=
     &trim(storein)//rundate//runtime(1:6)//'clean'//trim(ACidata(i)))
400     read(1,fmt=300,end=410)longchar
        write(2,310)trim(longchar)
        goto 400
410     close(1,status='delete')
        close(2)
      enddo
      do i=1,noutputfiles
        k=0
        open(unit=1,file=trim(outpath)//trim(outputfile(i)))
        if(i.ge.3.and.i.le.5)then
          open(unit=2,file=
     &trim(storeout)//rundate//runtime(1:6)//trim(outputfile(i)))
        else
          open(unit=2,file=
     &trim(outpath)//rundate//runtime(1:6)//trim(outputfile(i)))
        endif
420     read(1,fmt=300,end=430)longchar
        write(2,310)trim(longchar)
        k=1
        goto 420
430     if(k.eq.1)then
          close(1,status='delete')
          close(2)
        else
          close(1,status='delete')
          close(2,status='delete')
        endif
      enddo
450   continue
!----------------------------------------------------------
300   format(a5000)
310   format(a)
      end
!@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
