!--------------------------------------------------------------------------------------------------
!  DOXYGEN
!> @brief Inserir Brief de VADZ_GEF
!> @details Inserir Details de VADZ_GEF
!> @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 CLDWTR
!! @arg @c CONTIN
!! @arg @c CTLBLK
!! @arg @c DYNAM
!! @arg @c F77KINDS
!! @arg @c LOOPS
!! @arg @c MASKS
!! @arg @c MPPSTAFF
!! @arg @c NHYDRO
!! @arg @c PARMETA
!! @arg @c VRBLS
!> @details <b>Driver:</b> 
!<
!> @details <b>Calls:</b>
!! @arg @c BOCOHMPI
!--------------------------------------------------------------------------------------------------    
    SUBROUTINE VADZ_GEF
!--------------------------------------------------------------------------------------------------
! SUBROUTINE VADZ_GEF
!
! SUBROUTINE: VADZ_GEF - ????? 
! PROGRAMMER: ?????
! ORG: ?????
! DATE: ??-??-??
!
! ABSTRACT:
! ?????
!
! 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: CLDWTR
!              CONTIN
!              CTLBLK
!              DYNAM
!              F77KINDS
!              LOOPS
!              MASKS
!              MPPSTAFF
!              NHYDRO
!              PARMETA
!              VRBLS     
!
! DRIVER     : -----
!
! CALLS      : BOCOHMPI
!-------------------------------------------------------------------------------------------------- 
    USE CLDWTR
    USE CONTIN
    USE CTLBLK
    USE DYNAM
    USE F77KINDS
    USE LOOPS
    USE MASKS
    USE MPPSTAFF
    USE NHYDRO
    USE PARMETA
    USE VRBLS
!
    IMPLICIT NONE
!
    INCLUDE "mpif.h"  
!     
    INTEGER(KIND=I4)                                                                            ::&
    & I       , J       , L
!
    INTEGER(KIND=I4)    , PARAMETER :: KSMUD = 0
!
    REAL   (KIND=R4)    , PARAMETER :: G  = 9.8
    REAL   (KIND=R4)    , PARAMETER :: RG = 1.  / 9.8
!
    REAL   (KIND=R4)                                                                            ::&
    & RDT     , ZETA    , DTLRG   , ALP1P   , DZ      , TTAL    , ALP1PL
!  
    REAL   (KIND=R4)                                                                            ::&
    & A       , B       , C  
!
    REAL   (KIND=R4)    , DIMENSION(0:IM+1, 0:JM+1)                                             ::&
    & PRET    , TTB     , ALP1    , FNE     , FSE
!
    REAL   (KIND=R4)    , DIMENSION(0:IM+1, 0:JM+1, LM+1)                                       ::&
    & PINTLG
!
    IF (NTSD == 1) THEN
        DO L=LM+1,1,-1
            DO J=0,JM+1
                DO I=0,IM+1
                    W(I,J,L) = 0.
                END DO
            END DO
        END DO
    END IF
!
    RDT = 1. / DT
!
!$omp parallel do
!
    DO J=0,JM+1
        DO I=0,IM+1
!
            W(I,J,LM+1) = 0.
            Z(I,J,LM+1) = 0.
!
            PRET(I,J) = PSDT(I,J) * RES(I,J)
!
            ALP1(I,J) = ALOG(PINT(I,J,LM+1)) 
!
        END DO
    END DO
!
    DO 50 L=LM,1,-1
!    
        ZETA  =  DFL(L) * RG
        DTLRG = DETA(L) * RG
!
!$omp parallel do
!
        DO J=1,JM
            DO I=1,IM 
!
                ALP1P = ALOG(PINT(I,J,L))  
!
                IF (DWDT(I,J,L) /= 0) THEN  
                    DZ = (Q(I,J,L) * 0.608 - CWM(I,J,L) + 1.) * T(I,J,L) * (ALP1(I,J) - ALP1P)    &
    &                  * R / (DWDT(I,J,L) * G)
                ELSE
                    DZ = (Q(I,J,L) * 0.608 - CWM(I,J,L) + 1.) * T(I,J,L) * (ALP1(I,J) - ALP1P)    &
    &                  * R / G
                END IF
!
                    Z(I,J,L) = (  Z(I,J,L+1) + DZ - ZETA) * HTM(I,J,L) + ZETA
!           
                PDWDT(I,J,L) = DWDT(I,J,L  )
!
                 DWDT(I,J,L) =    W(I,J,L)              
!
                    W(I,J,L) = (DZ - RTOP(I,J,L) * PDSLO(I,J) * DTLRG) * HTM(I,J,L) + W(I,J,L+1)  
!            
                 ALP1(I,J)   = ALP1P
!   
            END DO
        END DO
!
 50 CONTINUE
!
    DO L=1,LM
!
!$omp parallel do
!
        DO J=1,JM
            DO I=1,IM            
                Z(I,J,L) = (Z(I,J,L) + Z(I,J,L+1)) * 0.5
! 
                W(I,J,L) = (W(I,J,L) + W(I,J,L+1)) * HTM(I,J,L) * 0.5 * RDT
!     
            END DO
        END DO
!
    END DO
!
!$omp parallel do
!
    DO J=1,JM
        DO I=1,IM     
            TTB(I,J) = 0.
        END DO
    END DO
!
    DO L=1,LM-1
!       
!$omp parallel do
!
        DO J=1,JM
            DO I=1,IM  
!       
                TTAL     = (Z(I,J,L+1) - Z(I,J,L)) * ETADT(I,J,L) * 0.5
!
                W(I,J,L) = (TTAL + TTB(I,J)) * RDETA(L) + W(I,J,L)
!
                TTB(I,J) = TTAL
!
            END DO
        END DO
!      
    END DO
!
!$omp parallel do
!
    DO J=1,JM
        DO I=1,IM 
            W(I,J,LM) = TTB(I,J) * RDETA(LM) + W(I,J,LM)
         END DO
    END DO
!    
    CALL BOCOHMPI(W    , LM+1)  
    CALL BOCOHMPI(PDWDT, LM  )
!
    RETURN
!
    END SUBROUTINE VADZ_GEF
