!--------------------------------------------------------------------------------------------------
!  DOXYGEN
!> @brief MAIN PROGRAM 
!> @details MAIN PROGRAM OF GLOBAL ETA FRAMEWORK
!> @author ORIGINATOR - ????? 
!> @date ??-??-?? \n
!> @author LUCCI 
!> @date 18-03-20 \n
!> @version V1.1.0
!> @details MODERNIZATION OF THE CODE, INCLUDING:
!!                      * F77 TO F90/F95
!!                      * INDENTATION & UNIFORMIZATION CODE
!!                      * REPLACEMENT OF COMMONS BLOCK FOR MODULES
!!                      * DOCUMENTATION WITH DOXYGEN
!!                      * OPENMP FUNCTIONALITY 
!<
!> @details <b>Use Module:</b>
!! @arg @c CONTIN
!! @arg @c CTLBLK
!! @arg @c DGNSOUT
!! @arg @c F77KINDS
!! @arg @c LOOPS 
!! @arg @c MASKS
!! @arg @c MPPSTAFF
!! @arg @c NHYDRO
!! @arg @c PARMETA
!! @arg @c PHYS
!! @arg @c PVRBLS
!! @arg @c SOIL
!! @arg @c VRBLS
!> @details <b>Driver:</b> 
!<
!> @details <b>Calls:</b>
!! @arg @c ADJUST
!! @arg @c ALLOC
!! @arg @c CHECKMXMN
!! @arg @c CLTEND
!! @arg @c DDAMP
!! @arg @c FINISHMPI
!! @arg @c GSMDRIVE
!! @arg @c HDIFF
!! @arg @c HZADV
!! @arg @c HZADVQ
!! @arg @c INIT
!! @arg @c INITDOM
!! @arg @c INITMPI
!! @arg @c INITTOP1
!! @arg @c INITTOP2
!! @arg @c KFDRIVE
!! @arg @c KFTEND
!! @arg @c OUT2
!! @arg @c OUT_HEAT
!! @arg @c PDETE
!! @arg @c PDNEW
!! @arg @c RADTN
!! @arg @c RDTEMP
!! @arg @c SSTSTP
!! @arg @c TURBL
!! @arg @c VTADV
!--------------------------------------------------------------------------------------------------
    PROGRAM GEF
!--------------------------------------------------------------------------------------------------
! MAIN PROGRAM GEF
! 
! MAIN PROGRAM: GEF - GLOBAL VERSION OF ETA FRAMEWORK
! PROGRAMMER: ?????          
! ORG: W/NP22
! DATE: ??-??-??
!
! ABSTRACT:
! GLOBAL VERSION OF ETA MODEL ON CUBIC GRID
!
! PROGRAM HISTORY LOG:
! ??-??-??  ?????      - ORIGINATOR
! 18-01-15  LUCCI      - MODERNIZATION OF THE CODE, INCLUDING:
!                        * F77 TO F90/F95
!                        * INDENTATION & UNIFORMIZATION CODE
!                        * REPLACEMENT OF COMMONS BLOCK FOR MODULES
!                        * DOCUMENTATION WITH DOXYGEN
!                        * OPENMP FUNCTIONALITY
!
! INPUT ARGUMENT LIST:
! NONE
!
! OUTPUT ARGUMENT LIST:
! NONE
!
! INPUT/OUTPUT ARGUMENT LIST:
! NONE
!
! USE MODULES: CONTIN
!              CTLBLK
!              DGNSOUT
!              F77KINDS
!              LOOPS
!              MASKS
!              MPPSTAFF
!              NHYDRO
!              PARMETA
!              PHYS
!              PVRBLS
!              SOIL
!              VRBLS 
! 
! DRIVER     : -----
!
! CALLS      : ADJUST
!              ALLOC
!              CHECKMXMN
!              CLTEND
!              DDAMP
!              FINISHMPI
!              GSMDRIVE
!              HDIFF
!              HZADV
!              HZADVQ
!              INIT
!              INITDOM
!              INITMPI
!              INITTOP1
!              INITTOP2
!              KFDRIVE
!              KFTEND
!              OUT2
!              OUT_HEAT
!              PDETE
!              PDNEW
!              RADTN
!              RDTEMP
!              SSTSTP
!              TURBL
!              VTADV		   
!--------------------------------------------------------------------------------------------------
    USE CONTIN
    USE CTLBLK
    USE DGNSOUT
    USE F77KINDS
    USE LOOPS 
    USE MASKS   
    USE MPPSTAFF
    USE NHYDRO
    USE PARMETA
    USE PHYS
    USE PVRBLS
    USE SOIL    
    USE VRBLS
!    
    IMPLICIT NONE
!
    INCLUDE "mpif.h"
!
    LOGICAL(KIND=L4)                                                                            ::&
    & LOUT    , LADV    , LADH    , LCKMM   , LPHYS   , LCHSST  , LVSST   , LDASS   , LVEG 
!
    INTEGER(KIND=I4)                                                                            ::&
    & I       , J       , L       , IERR    , ICLTEND , NTSDDAY , ND      , M       , K
!
    LVSST = .FALSE.
    LVEG  = .FALSE.  
    HYDRO = .TRUE.
!---------------
! INITIALIZE MPI
!---------------
    CALL INITMPI
!
    IF (MYPE < (NPES)) THEN
!
        CALL ALLOC
!
        CALL INIT_ZERO
!
!        CALL ZERO
!
        CALL INITDOM
!
        IF (NM == 6) THEN
	    CALL INITTOP1
        ELSE IF (NM == 14) THEN
            CALL INITTOP2
        END IF
!---------------
! INITIALIZATION
!---------------
        CALL INIT
!
        NTSDDAY = 3600 * 24 / DT
!
!-------------------------------------------------------- 
! PECIAL CONSIDERATION WHEN NTSD = 0 AT START OF FORECAST
!-------------------------------------------------------- 
        IF (NTSD == 0) NTSD = 1
!-------------------------
! ENTRY INTO THE TIME LOOP
!-------------------------   
        DO NTSD = 1,NTSTM       
!
            IF (MYPE == 0) THEN
                WRITE(0,*) ' NTSD= ', NTSD
            END IF
!
            LOUT   = MOD(NTSD - OUTSTR  , IOUT   ) == 0 .AND. NTSD >= OUTSTR
            LADV   = MOD(NTSD           , IDTAD  ) == 0 !LUCCI NTSD sem -1
            LADH   = MOD(NTSD           , IDTAD  ) == 0 !LUCCI NTSD sem -1
            LCKMM  = MOD(NTSD           , 2      ) == 0
            LPHYS  = MOD(NTSD           , NPHS   ) == 0
            LCHSST = MOD(NTSD           , NTSDDAY) == 0
!
            LDASS  = NTSD <= NDASSIMM
!-----------------
! SST-DAILY UPDATE
!-----------------
            IF (LVSST .AND. LCHSST) THEN   
                ND = NTSD / NTSDDAY
                PRINT *, 'ND=', ND, MYPE
!             
                CALL SSTSTP()
            END IF
!-------------------------- 
! VEG GREENESS-DAILY UPDATE 
!--------------------------
            IF (LVEG .AND. LCHSST) THEN
                ND = NTSD / NTSDDAY    
!        
                CALL VEGUPDT
!
            END IF
!
            CALL ADJUST 
! 
            CALL  PDETE  
!-------------------------------------------------------
! DO VERTICAL ADVECTION WITHIN THE FIRST ADJUSTMENT STEP
!-------------------------------------------------------	    
	    IF (LADV) CALL VTADV
!-----------------------------
! UPDATING PRESSURE DIFFERENCE
!-----------------------------
            CALL PDNEW
!-------------------
! DIVERGENCE DAMPING
!-------------------
            CALL DDAMP
!------------------
! LATERAL DIFFUSION
!------------------
            CALL HDIFF
!----------------------------------------
! HORIZONTAL ADVECTION OF WATER SUBSTANCE 
!----------------------------------------
            IF (LADH) THEN
!
                CALL HZADVQ
!---------------------
! HORIZONTAL ADVECTION
!---------------------	    
	        CALL HZADV
!
            END IF
!
            IF (MOD(NTSD, NRADS) == 1 .OR. MOD(NTSD, NRADL) == 1) THEN 
                CALL RADTN
            END IF
!
            CALL RDTEMP
!--------------------------------------------------------------------------------------------------
! IF THE TIME IS RIGHT, NOW DO VARIOUS PHYSICS CALLS (WARNING: TO AVOID ENDING THE INTEGRATION WITH
! PHYSICS CALLS WHICH HAVE NOT BEEN FOLLOWED BY ADJUSTMENT STEPS, PHYSICS CALLS ARE OFFSET BY 
! HALVES OF VARIOUS CALLING INTERVALS.
! IT IS ASSUMED THAT THE CALLING INTERVALS, NPHS AND NCNVC, ARE DIVISIBLE BY IDTAD.
! IF NOT, INTEGRATION WILL END WITH AN INCORRECT NUMBER OF CALLS HAVING BEEN MADE.
!--------------------------------------------------------------------------------------------------
!
!--------------------------------------
! TURBULENT PROCESSES AND PRECIPITATION 
!--------------------------------------
            IF (LPHYS) THEN
!
                CALL TURBL
!---------------------------------
! STORE ORIGINAL TEMPERATURE ARRAY
!---------------------------------
                ICLTEND = -1
!
                CALL CLTEND(ICLTEND)

!JBF: Instalando BMJ (..CALL CUCNVC)
                CALL CUCNVC
!														
                CALL GSMDRIVE
!-----------------------------------------------------
! CALCULATE TEMP TENDENCIES AND RESTORE ORIGINAL TEMPS
!-----------------------------------------------------
                ICLTEND = 0
!
                CALL CLTEND(ICLTEND)
!
!JBF: instalando BMJ                CALL KFDRIVE
!
!JBF: instalando BMJ                CALL KFTEND
!             
            END IF
!------------------------------------------------------------
! UPDATE TEMP TENDENCIES FROM CLOUD PROCESSES EVERY TIME STEP
!------------------------------------------------------------
            ICLTEND = 1
!
            CALL CLTEND(ICLTEND)
!
!JBF: modificações feitas junto com o Dragan
!            IF (LOUT) THEN
             IF (NTSD == 1 .OR. LOUT) THEN
                IF (MYPE == 0) WRITE(0,*) ' OUTPUT AT ', (NTSD * DT) / 3600.0
                CALL OUT2
            END IF
!
            IF (LCKMM) CALL CHECKMXMN
!---------------------------------------
! CLOSE THE TIME LOOP: DO NTSD = 1,NTSTM
!---------------------------------------
        END DO                
!----------------------------------
! CLOSE IF: IF (MYPE < (NPES)) THEN
!----------------------------------
    END IF
!
    CALL OUT_HEAT
!
    CALL FINISHMPI
!--------------------
! SUCESSFULLY MESSAGE
!-------------------- 
    WRITE(0,*) 'GEF FINISHED SUCCESSFULLY'
!
    END PROGRAM GEF
