cc This program is used to postprocess the TFPR optimization results. All relevant information for
cc each well will be saved into a *.csv file stored in the PostPro folder under the project directory.


c define variables 
        USE DFLIB  !调用IFPORT
        USE DFWIN  !调用IFPORT

        implicit none
        real,ALLOCATABLE:: P(:),WL(:,:),R(:,:),PTOTAL(:),PWL(:)
        real weightwl,weightp,tempwl,dummyr,interval,diff,DM,DT
        real parav,paras,parar,res1,res2,res3,res4,res5,res6,res7
	  real phi,phi_wl,phi_p,para
        integer i,j,k,m,n,q,site,gauge
        integer datanum,wellnum,periodsnum,gaugenum,MONTHCOUNT
        integer,ALLOCATABLE:: PSTART(:),PEND(:),duration(:)
        integer,ALLOCATABLE:: date1(:),month(:)
        integer,ALLOCATABLE:: active(:)
        integer GAUGEDIR,WELLDIR,ierr
        integer obsnum,adparanum,active1
        LOGICAL fexist
        Character CASENAM*58,PARATPL*18,dummy*158,DIR*100
	  character X61*61,X51*51
        Character tt,currentline*100,obsnam*10,paran*19,parag*15
        Character,ALLOCATABLE::  wellnam(:)*100
        Character,ALLOCATABLE::  RAINGAUGE(:)*100,RECESSION(:)*100
        Character,ALLOCATABLE::  GAUGENAM(:)*100,CONFIG(:)*100

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
C                                                                             C
C                                   READ Name file                            C
C                                                                             C
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
c        write (*,*) "请输入NAM文件的文件名"
c        read *, CASENAM
        Call GETARG(1,CASENAM)
        read(CASENAM,*)
        open (5,file=trim(CASENAM))
           read (5,*) dummy          
            read (5,*) gaugenum
            read (5,*) dummy          
            Allocate(GAUGENAM(gaugenum))
            Allocate(RAINGAUGE(gaugenum))
            Allocate(RECESSION(gaugenum))
            Allocate(CONFIG(gaugenum))
            read (5,*) (GAUGENAM(i),i=1,gaugenum)
          do i=1,gaugenum
            read (5,*) dummy          
            read (5,*) RAINGAUGE(i)
            read (5,*) RECESSION(i)
            read (5,*) CONFIG(i)
          end do
        close (5)
        i = index(casenam,'.')
          if(i/=0) then
              casenam = casenam(1:i-1)
        endif

        open (6,file=trim(casenam)//'.csv', status = 'replace')
        write (6,'(a64,\)') 'Gauge,Well,ObsNum,Duration,PeriodNum
     *,Phi,Phi_wl,Phi_p,Parameters'
      write (6,'(a1)') ''
C LOOP FOR GAUGE I
       DO gauge=1,gaugenum

C Read Optimization parameters with sensitivity values from *.sen file
        open (10,FILE=trim(RAINGAUGE(gauge)))
           read (10,*) datanum,wellnum,MONTHCOUNT
            Allocate(P(datanum))
            Allocate(WL(wellnum,datanum))
            Allocate(R(wellnum,datanum))
            Allocate(PTOTAL(datanum))
            Allocate(PWL(datanum))
            Allocate(PSTART(datanum))
            Allocate(PEND(datanum))
            Allocate(duration(datanum))
            Allocate(date1(datanum))
            Allocate(month(datanum))
            Allocate(active(wellnum))
            Allocate(wellnam(wellnum))

            read (10,*) tt,tt,tt,(wellnam(i),i=1,wellnum)
          do i=1,datanum
            read (10,*) date1(i),month(i),P(i),(WL(j,i),j=1,wellnum)
          end do
        close (10)
 
        open (20,FILE=trim(RECESSION(gauge)))
            read (20,*) datanum,wellnum
            read (20,*) tt,tt,(wellnam(i),i=1,wellnum)
          do i=1,datanum
            read (20,*) date1(i),month(i),(R(j,i),j=1,wellnum)
          end do
        close (20)
     
C Read well name

        open (25,FILE= trim(GAUGENAM(gauge))//'.ctr')
            read (25,*) dummy
          do i=1,wellnum
            read (25,*) tt,wellnam(i),active(i)
          end do
        close (25)


c Loop for Site I
      DO site=1,wellnum
      DIR=TRIM(GAUGENAM(gauge))//'\'//TRIM(wellnam(site))
     
      Open (1, file=TRIM(DIR)//'\Postpro.csv', status = 'replace')
      write (1,*) '**Names,,,,,,,,,,,,,'
      write (1,*) 'Name file, Gauge Name, Well Name'
      write (1,*) trim(casenam),',',TRIM(GAUGENAM(gauge)),',',
     . TRIM(wellnam(site))
      write (1,*) '**Numbers'
      write (1,*) 'No. of obs, No. of WL, No. of Periods, Phi'
C Open relevant files
      Open (2, file=TRIM(DIR)//'\periods.dat')
      Read (2,*) periodsnum
      Close (2)
      obsnum=periodsnum+datanum

      Open (2, file=TRIM(DIR)//'\case.rec')
	adparanum=MONTHCOUNT+1
      phi=0
	phi_wl=0
	phi_p=0
      Do
          read(2,'(a100)',iostat = ierr) currentline 
          if(ierr /=0) exit
	    j = index(currentline,'Number of adjustable parameters')
          if(j/=0) then
	         backspace(2)
               read(2,'(a51,i5)')X51,adparanum
c              exit
          endif
          i = index(currentline,'Objective function ----->')
          if(i/=0) then
	         read(2,*) 
               read(2,'(a61,f12.2)')X61,phi
               read(2,'(a61,f12.2)')X61,phi_wl
	         read(2,'(a61,f12.2)')X61,phi_p
              exit
          endif
      enddo
      Close (2)
      
      write (1,*) obsnum,',',datanum,',',periodsnum,','
     *,phi
      if(phi<1000000) then
          write (6,'(a20,a1,a20,a1,i5,a1,i5,a1,i5,a1,f10.3,a1,
     *f10.3,a1,f10.3,\)') 
     *GAUGENAM(gauge),',',wellnam(site)
     .,',',obsnum,',',datanum,',',periodsnum,
     *',',phi,',',phi_wl,',',phi_p
      else
          write (6,'(a20,a1,a20,a1,i5,a1,i5,a1,i5,a1,es10.3,a1,
     *es10.3,a1,es10.3,\)') 
     *GAUGENAM(gauge),',',wellnam(site)
     .,',',obsnum,',',datanum,',',periodsnum,
     *',',phi,',',phi_wl,',',phi_p
      endif

      Open (7, file=TRIM(DIR)//'\case.par')
	    read(7,*) tt
	do i=1,adparanum  
	    read(7,*) tt,para,tt,tt
	    write (6,'(a1,f10.5,\)') ',',para
      enddo
	close(7)

      write (1,*) '**Parameters'
      write (1,*) 'Parameter, Current Value, Sensitivity, Rel. Sens'
      
      Open (2, file=TRIM(DIR)//'\case.sen')
      Do
          read(2,'(a100)',iostat = ierr) currentline 
          if(ierr /=0) exit
          i = index(currentline,'COMPLETION OF OPTIMISATION PROCESS' )
          if(i/=0) then
              do j=1,3
                  read(2,*) currentline 
              enddo
              do j=1,adparanum
                  read(2,*) paran,parag,parav,paras,parar
                  write (1,*) trim(paran),',',parav,',',paras,',',parar
              enddo
              write (6,'(a1)') ''
              exit
          endif
      enddo
      Close (2)
      
      write (1,*) '**WL Values'
      write (1,'(a115,\)') 'Date, Month, Precip, Rec, DM, Measu
     *red, Modelled, Res, Weight, Weight*Measured, Weight*Modelled, Weig
     *ht*Res'
      write (1,'(a1)') ''
      
      Open  (3, file=TRIM(DIR)//'\DM.dat')   
      Open  (4, file=TRIM(DIR)//'\case.res')      
            read(4,*) currentline
           do i=1,datanum
			Read(3,*)dm
              Read(4,*) paran, parag,res1,res2,res3,res4,res5,res6,res7,
     *dummyr,dummyr
              Write(1,'(i5,a1,i2,a1,f7.5,a1,f7.5,a1,f7.5
     *,a1,f8.3,a1,f8.3,a1,f8.3,a1,f12.5,a1,f12.5,a1,f12.5,a1,f12.5,\)')
     *date1(i),',',Month
     *(i),',',P(i),',',R(site,i),
     *',',DM,',',WL(site,i),',',res2,',',res3,',',
     *res4,',',res5,',',res6,',',res7
              write (1,'(a1)') ''
           enddo
      close(3)

      write (1,*) '**Period P Values'
      write (1,'(a89,\)') 'Obs Name, Measured, Modell
     *ed, Res, Weight, Weight*Measured, Weight*Modelled, Weight*Res'
      write (1,'(a1)') ''
      do i=1,periodsnum
              Read(4,*) paran, parag,res1,res2,res3,res4,res5,res6,res7,
     *dummyr,dummyr
              Write(1,'(a6,a1,
     *f8.3,a1,f8.3,a1,f8.3,a1,f12.5,a1,f12.5,a1,f12.5,a1,f12.5,\)')
     *trim(paran),',',res1,',',res2,',',res3,',',res4,
     *',',res5,',',res6,',',res7
      write (1,'(a1)') ''              
           enddo
      close(4)
      close(1)
cc end for the well number site
	 END DO
            DEAllocate(P)
            DEAllocate(WL)
            DEAllocate(R)
            DEAllocate(PTOTAL)
            DEAllocate(PWL)
            DEAllocate(PSTART)
            DEAllocate(PEND)
            DEAllocate(duration)
            DEAllocate(date1)
            DEAllocate(month)
            DEAllocate(active)
            DEAllocate(wellnam)
cc end for the gauge numbere i
       END DO
            DEAllocate(GAUGENAM)
            DEAllocate(RAINGAUGE)
            DEAllocate(RECESSION)
            DEAllocate(CONFIG)
      close(6)
	    
       STOP
       END
