      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
      
      character(len=10)::c_nh
!      character(len=8)::date
      character(len=256)::fname1,fname2

      character(len=10)::Run_Date

!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') 
       
       
      NAMELIST /CNSTDATA/INDAY,IHRST,IDAT,IOUTNUM,IIOUT

      open(19,file='cnstdata',status='old')
      READ(19,CNSTDATA)

       nday=INDAY
       outnum=IOUTNUM
       
       
       
       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!  
              !!!!!!!!!!!!!!!!!!!!!  
	      !===================!
	       
!!JNT       
       do nh = 0,outnum  !nhmax, nhstep     !DRAGAN, 17.09.10                                   
!!!!       do nh = 0,40  !nhmax, nhstep     !DRAGAN, 17.09.10                                   


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

!      write(date,'(i4.4,i2.2,i2.2)')yyyy,mm,dd   !DRAGAN aug 2017  
	
	
       fname2='../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
      
               !=====================!
               !!!!!!!!!!!!!!!!!!!!!!!
 !_ _ _ _ _ _ _!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
