      program etapost

      USE  mod_vrbls
      USE  mod_extra
      USE  mod_masks
      USE  mod_pvrbls
      USE  mod_pvrbls_ll
      USE  mod_graph

      implicit none
      include 'param_o.h'
      include 'const.h'
      integer::npes,nhstep
      parameter(npes=nsub*nsub*nm,nhstep=1) 
      integer :: nh,dd, mm, yyyy, hh, bb, ddmax, mmmax,outperiod,INDAY,IOUTNUM,IIOUT,IHRST,nday,outnum
!      integer::nh,dd, mm, yyyy, hh, bb, ddmax, mmmax,outperiod
!     ! character(len=3)::c_nh

!!!!!!!!RESTART  added 30/04/2019 
      integer :: day, mon, yr, utc, iutc, nhrs, hhzero
!!!!!!!!
  
      character(len=10)::c_nh
!      character(len=8)::date
      character(len=256)::fname1,fname2

      character(len=10):: Run_Date

      character(len=6):: c_nhrs   !RESTART
      
!character(len=4)::c_nh2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!!JNT Initilize old common variables 
      print *,'allocate  vrbls ...'
      call alloc_vrbls
      print *,'allocate  extra ...'
      call alloc_extra
      print *,'allocate  masks ...'
      call alloc_masks
      print *,'allocate  pvrbls ...'
      call alloc_pvrbls
      print *,'allocate  pvrbls_ll ...'
      call alloc_pvrbls_ll
      print *,'allocate  graph ...'
      call alloc_graph
      print *,'calling  initmap ...'

      call initmap
!!!!D july2015      fname1='../data_out/hgt500UG.dat'
!!!!      open(10,file=fname1,status='unknown',form='unformatted') 
       
!commented, Dragan, 25/06/2019, when restart option is introduced       
      NAMELIST /CNSTDATA/INDAY,IHRST,IDAT,IOUTNUM,IIOUT
      open(19,file='cnstdata',status='old')
!       open(19,file='/scratchin/grupos/grpeta/projetos/tempo/oper/GEF_v1.0.0-KF/gef_trunk/cnstdata',status='old') 
      READ(19,CNSTDATA)


       nday=INDAY
       outnum=IOUTNUM
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!       
       

!!!!!!!!RESTART  added 25/06/2019 

       open(27, file='restrt_nmlst.txt', STATUS='OLD')
       rewind 27
       read(27,*)restrt
       read(27,*)nhrs
!!!!!!!!!
       
       yyyy=IDAT(3)  ! 
       mm  =IDAT(1)  ! initial date, day, month, year    !******uncomment!!!!!!!
       dd  =IDAT(2)  !
       hh  =ihrst
     
!GSM     
      write(Run_Date,'(BZ,i4.4,3i2.2)')yyyy,mm,dd,hh
!GSM  
     
      print*,'DATE=',dd,mm,yyyy,hh
     
     
       outperiod=outnum/nday
       
       
      !!!!!!!!!!!!!!! 
!      dd=29          
!      mm=3            
!      yyyy=1996            !!!! DRAGAN, 09.02.2011.  *****comment!!!!!!
!      hh=12           
!      hh=0 
     
     
                   
      mmmax=12      
      !!!!!!!!!!!!!!!
  
  
              !===================!
              !!!!!!!!!!!!!!!!!!!!!
!_ _ _ _ _ _ _!DO LOOP BEGINS HERE!  
              !!!!!!!!!!!!!!!!!!!!!  
	      !===================!
	       
!!JNT       
!RESTART      
    if(.not.restrt)then                   

     do nh = 0,outnum  !nhmax, nhstep     !DRAGAN, 17/07/2019  !test when first output is at 00 hs, 
                                                               !added another call to out2 in gef.f90, before do loop, and nhrs=-1 in init.f90

!!!     do nh = 1,outnum  !nhmax, nhstep     !DRAGAN, 13/02/2019  !test when first output is after 6hs


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DRAGAN, 09.02.2011.
!!!!!!!!===================================================!!!!!!!
!!!!!!!! RECOGNIZING THE OUTPUT PERIOD FROM CONFIGURE PART !!!!!!!
!!!!!!!!===================================================!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!     
     
!!!============!!! 
!!! 24h output !!!
!!!============!!!
    
      if(outperiod.eq.1)then
      bb=nh*24
         if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+24
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
 	 endif 
      endif

!!!============!!! 
!!! 12h output !!!
!!!============!!!  
     
      if(outperiod.eq.2)then
      bb=nh*12
         if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+12
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
	 endif 
      endif

!!!============!!! 
!!!  8h output !!!
!!!============!!!     
    
      if(outperiod.eq.3)then
      bb=nh*8
         if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+8
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
	 endif 
      endif

!!!============!!! 
!!!  6h output !!!
!!!============!!!     
     
      if(outperiod.eq.4)then
      bb=nh*6
      if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
	  hh=hh+6
      endif  
	 
     if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
	  hh=0
	  dd=dd+1
     endif 
	 
	 
     if(hh.gt.24) then
      hh=hh-24
      dd=dd+1
     endif 

!12Z      if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
!12Z          hh=hh+6
!12Z      endif
         
!12Z      if(hh.ge.24) then
!12Z	  hh=hh-24
!12Z	  dd=dd+1
!12Z      endif

      endif

!!!============!!! 
!!!  4h output !!!
!!!============!!!     
     
      if(outperiod.eq.6)then
      bb=nh*4
         
	 if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+4
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
	 endif 
    
      endif

!!!============!!! 
!!!  3h output !!!
!!!============!!!    
    
      if(outperiod.eq.8)then
      bb=nh*3
      if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+3
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
	 endif 
      endif

!!!============!!! 
!!!  2h output !!!
!!!============!!!    
     
      if(outperiod.eq.12)then
      bb=nh*2
      if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+2
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
	 endif 
      endif

!!!============!!! 
!!!  1h output !!!
!!!============!!!     
     
      if(outperiod.eq.24)then
      bb=nh*1
      if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+1
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
	 endif 
      endif
     
     
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!    


      
      if ((mm.eq.1.).or.(mm.eq.3).or.(mm.eq.5).or.(mm.eq.7).or.(mm.eq.8).or.(mm.eq.10).or.(mm.eq.12)) then
      ddmax=31
      endif
      
      if ((mm.eq.4.).or.(mm.eq.6).or.(mm.eq.9).or.(mm.eq.11)) then
      ddmax=30
      endif
      
      if (mm.eq.2) then
      ddmax=28
      endif
      
      if ((mm.eq.2.).and.(mod(yyyy,4).eq.0)) then
      ddmax=29
      endif
      
      if (dd.gt.ddmax) then
      dd=1
      mm=mm+1
      endif
      
      if (mm.gt.mmmax) then
      mm=1
      yyyy=yyyy+1
      endif
        
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DRAGAN, 09.02.2011.

      !write(c_nh,'(i3.3)')nh   !!!
      
      write(c_nh,'(i2.2,i2.2,i4.4,i2.2)')dd,mm,yyyy,hh  !!!!!!!!!!!! DRAGAN, 09.02.2011.

!      write(date,'(i4.4,i2.2,i2.2)')yyyy,mm,dd   !DRAGAN aug 2017  
	
	
       fname2='../GLOBRUN/'//Run_Date//'/globrun'//c_nh//'.dat'
      
!!!DRAGAN 03/07/2019      fname2='/scratchout/grupos/grpeta/projetos/tempo/oper/GEF_v1.0.0-KF/GLOBRUN/'//Run_Date//'/globrun'//c_nh//'.dat'

      print *,fname2
      open(9, file=fname2, status='unknown',form = 'unformatted') 
                                              
       print *,nh 
       print *, "reading input data in initpost .... "
        call initpost(nh)
       print *, "calling etafld .... "
        call etafld
       print *, "calling slp .... "
        call slp
        if(sigma) then
       print *, "calling sig2p .... "
          call sig2p
        else 
       print *, "calling eta2p .... "
	  call eta2p
        endif
       print *, "calling surfce .... "
	call surfce
       print *, "calling qu2ll2  .... "
!        call qu2ll
        call qu2ll2
       print *, ' back to etapost enddo .. '
      close(9)
     
     
     
                   enddo


              else


         print*,'RESTRT=',restrt, 'NHRS=',nhrs 
         
	 
 
         ntsd=(nhrs*((24/outperiod)*3600))/dt
 
         print*, 'NTSD_calculated=',ntsd 
     
	 iutc=NTSD*DT/3600     
         
	 CALL getdate(yyyy,mm,dd,iutc,yr,mon,day,utc)     
      
         yyyy=yr
	 mm=mon
	 dd=day
!	 hh=utc
	 
	 outperiod=outnum/nday
	 
	 print*,'RESTART_DATE=',dd,mm,yyyy,hh, 'ntsd=',ntsd, 'outperiod=',outperiod
           
         	 
!!!!!!==================================!!!!!!!!!!!!!!!!!!!!!	 
      do nh = (nhrs+1),outnum  !RESTART
!!!!!!==================================!!!!!!!!!!!!!!!!!!!!!


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DRAGAN, 09.02.2011.
!!!!!!!!===================================================!!!!!!!
!!!!!!!! RECOGNIZING THE OUTPUT PERIOD FROM CONFIGURE PART !!!!!!!
!!!!!!!!===================================================!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!     
     
!!!============!!! 
!!! 24h output !!!
!!!============!!!
    
      if(outperiod.eq.1)then
      bb=nh*24
         if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+24
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
 	 endif 
      endif

!!!============!!! 
!!! 12h output !!!
!!!============!!!  
     
      if(outperiod.eq.2)then
      bb=nh*12
         if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+12
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
	 endif 
      endif

!!!============!!! 
!!!  8h output !!!
!!!============!!!     
    
      if(outperiod.eq.3)then
      bb=nh*8
         if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+8
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
	 endif 
      endif

!!!============!!! 
!!!  6h output !!!
!!!============!!!     
     
      if(outperiod.eq.4)then
      bb=nh*6
        
      if (    (   (mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)    ).and.   (nh.ne.0)   ) then
          hh=hh+6
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	  
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
	 endif 
      endif

!!!============!!! 
!!!  4h output !!!
!!!============!!!     
     
      if(outperiod.eq.6)then
      bb=nh*4
         
	 if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+4
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
	 endif 
    
      endif

!!!============!!! 
!!!  3h output !!!
!!!============!!!    
    
      if(outperiod.eq.8)then
      bb=nh*3
      if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+3
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
	 endif 
      endif

!!!============!!! 
!!!  2h output !!!
!!!============!!!    
     
      if(outperiod.eq.12)then
      bb=nh*2
      if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+2
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
	 endif 
      endif

!!!============!!! 
!!!  1h output !!!
!!!============!!!     
     
      if(outperiod.eq.24)then
      bb=nh*1
      if (((mod(bb,24).ne.0).or.(mod(bb,24).eq.0.and.hh.lt.24)).and.(nh.ne.0)) then
          hh=hh+1
         endif  
	 
	 if ((mod(bb,24).eq.0).and.(nh.ne.0).and.(hh.eq.24)) then
          hh=0
          dd=dd+1
         endif 
	 
	 
	 if(hh.gt.24) then
	  hh=hh-24
	  dd=dd+1
	 endif 
      endif
     
     
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!    


      
      if ((mm.eq.1.).or.(mm.eq.3).or.(mm.eq.5).or.(mm.eq.7).or.(mm.eq.8).or.(mm.eq.10).or.(mm.eq.12)) then
      ddmax=31
      endif
      
      if ((mm.eq.4.).or.(mm.eq.6).or.(mm.eq.9).or.(mm.eq.11)) then
      ddmax=30
      endif
      
      if (mm.eq.2) then
      ddmax=28
      endif
      
      if ((mm.eq.2.).and.(mod(yyyy,4).eq.0)) then
      ddmax=29
      endif
      
      if (dd.gt.ddmax) then
      dd=1
      mm=mm+1
      endif
      
      if (mm.gt.mmmax) then
      mm=1
      yyyy=yyyy+1
      endif
        
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DRAGAN, 09.02.2011.

      
      write(c_nh,'(i2.2,i2.2,i4.4,i2.2)')dd,mm,yyyy,hh  !!!!!!!!!!!! DRAGAN, 09.02.2011.


      write(c_nhrs,'(i6)')nhrs


      fname2='../GLOBRUN/RESTART_'//restartdate//'/globrun'//c_nh//'.dat'

      print *,fname2
      open(9, file=fname2, status='unknown',form = 'unformatted') 
                                              
       print *,nh 
       print *, "reading input data in initpost .... "
        call initpost(nh)
       print *, "calling etafld .... "
        call etafld
       print *, "calling slp .... "
        call slp
        if(sigma) then
       print *, "calling sig2p .... "
          call sig2p
        else 
       print *, "calling eta2p .... "
	  call eta2p
        endif
       print *, "calling surfce .... "
	call surfce
       print *, "calling qu2ll2  .... "
!        call qu2ll
        call qu2ll2
       print *, ' back to etapost enddo .. '
      close(9)
     
     
      enddo
 
 
      endif 
      
 !!!!!!!! added 25/06/2019, Dragan



      
               !=====================!
               !!!!!!!!!!!!!!!!!!!!!!!
 !_ _ _ _ _ _ _!DO LOOP FINISHES HERE!  
               !!!!!!!!!!!!!!!!!!!!!!!   
               !=====================!
	       
  
      close(10)

      !! call heat_budget
      print *,'etapost finished'

      call grid_prop

      call dealloc_vrbls
      call dealloc_extra
      call dealloc_masks
      call dealloc_pvrbls
      call dealloc_pvrbls_ll
      call dealloc_graph

      end program etapost
