      program etapost

      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
!     ! character(len=3)::c_nh
      character(len=10)::Run_Date
      
      character(len=10)::c_nh
      character(len=256)::fname1,fname2
!character(len=4)::c_nh2!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      call initmap
!!!!D july2015      fname1='../data_out/hgt500UG.dat'
!!!!      open(10,file=fname1,status='unknown',form='unformatted') 
       

!GSM
      NAMELIST /CNSTDATA/INDAY,IHRST,IDAT,IOUTNUM,IIOUT

      open(19,file='/home/lucci/GEF/GEF-reta/GEF_tempo/gef_trunk/cnstdata',status='old')
      READ(19,CNSTDATA)

						nday=INDAY
						outnum=IOUTNUM
!GSM      
       
       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 
     
     
       outperiod=outnum/nday
       
       
      !!!!!!!!!!!!!!! 
!      dd=29          
!      mm=3            
!      yyyy=1996            !!!! DRAGAN, 09.02.2011.  *****comment!!!!!!
!      hh=12           
!      hh=0 
     
     
                   
      mmmax=12      
      !!!!!!!!!!!!!!!
  
  
              !===================!
              !!!!!!!!!!!!!!!!!!!!!
!_ _ _ _ _ _ _!DO LOOP BEGINS HERE!  
              !!!!!!!!!!!!!!!!!!!!!  
	      !===================!
	       
       
!GSM       do nh = 0,outnum  !nhmax, nhstep     !DRAGAN, 17.09.10                                   
       do nh = 0,outnum-1  !nhmax, nhstep     !GUSTAVO, 31.01.17


!      do nh = 0,10  !nhmax, nhstep    !DRAGAN, 17.09.10 
!      do nh = 0,0  !nhmax, nhstep
!      do nh = nhmax,nhmax, nhstep


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 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
     
     
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!    







      !bb=nh*12
      !bb=nh*1
!!!!!!!!!!!!      bb=nh*6!!!!!!!!!!!
      
  !    if (nh.eq.0) then
  !    hh=0
  !    endif
     
!     ! if (mod(bb,24).eq.12) then
!     ! hh=12
!     ! endif
      
!!!!!!!      if (mod(bb,24).ne.0) then
!!!!!!!      hh=hh+6
      
!!!!!!!      endif   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      
      
!!!!!!!!!     if ((mod(bb,24).eq.0).and.(nh.ne.0)) then
!!!!!!!!!      hh=0
!!!!!!!!!      dd=dd+1
!!!!!!!!!      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.
        
!      fname2='../data_out/globrun'//c_nh//'.dat'
      fname2='/home/lucci/GEF/GEF-reta/GEF_tempo/scratchout/GLOBRUN/'//Run_Date//'/globrun'//c_nh//'.dat'
      print *,fname2
      open(9, file=fname2, status='unknown',form = 'unformatted') 
                                              
       print *,nh 
        call initpost(nh)
        call etafld
	call slp
        if(sigma) then
          call sig2p
        else 
	  call eta2p
        endif
	call surfce
!        call qu2ll
        call qu2ll2
      close(9)
     
     
     
                   enddo
      
               !=====================!
               !!!!!!!!!!!!!!!!!!!!!!!
 !_ _ _ _ _ _ _!DO LOOP FINISHES HERE!  
               !!!!!!!!!!!!!!!!!!!!!!!   
               !=====================!
	       
  
      close(10)

      call heat_budget
      print *,'etapost finished'

      call grid_prop

      end program etapost
