!--------------------------------------------------------------------------------------------------
!  DOXYGEN
!> @brief SURFACE PRESSURE TENDENCY CALC
!> @details VERTICALLY INTEGRATES THE MASS FLUX DIVERGENCE TO OBTAIN THE SURFACE PRESSURE TENDENCY 
!! AND ETADOT ON THE LAYER INTERFACES.
!! THEN IT UPDATES THE HYDROSTATIC SURFACE PRESSURE, THE NONHYDROSTATIC PRESSURE, AND ADDS THE LOCAL
!! TIME DERIVATIVE AND VERTICAL ADVECTION OF NONHYDROSTATIC PRESSURE CONTRIBUTION TO THE OMEGA-ALPHA 
!! TERM OF THE THERMODYNAMIC EQUATION. 
!! ALSO, THE OMEGA-ALPHA TERM IS COMPUTED FOR DIAGNOSTICS.
!> @author ORIGINATOR - JANJIC
!> @date 87-06-?? \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 DYNAM
!! @arg @c F77KINDS
!! @arg @c LOOPS
!! @arg @c MASKS
!! @arg @c MPPSTAFF
!! @arg @c NHYDRO
!! @arg @c PARMETA
!! @arg @c SET_ZERO
!! @arg @c VRBLS
!> @details <b>Driver:</b> 
!<
!> @details <b>Calls:</b>
!! @arg @c BOCOHMPI
!--------------------------------------------------------------------------------------------------     
    SUBROUTINE PDTEDT_GEF
!--------------------------------------------------------------------------------------------------
! SUBROUTINE PDTEDT_GEF
!
! SUBROUTINE: PDTEDT_GEF - SURFACE PRESSURE TENDENCY CALC
! PROGRAMMER: JANJIC
! ORG: W/NMC2
! DATE: 87-06-??
!
! ABSTRACT:
! PDTEDT VERTICALLY INTEGRATES THE MASS FLUX DIVERGENCE TO OBTAIN THE SURFACE PRESSURE TENDENCY AND
! ETADOT ON THE LAYER INTERFACES.
! THEN IT UPDATES THE HYDROSTATIC SURFACE PRESSURE, THE NONHYDROSTATIC PRESSURE, AND ADDS THE LOCAL
! TIME DERIVATIVE AND VERTICAL ADVECTION OF NONHYDROSTATIC PRESSURE CONTRIBUTION TO THE OMEGA-ALPHA 
! TERM OF THE THERMODYNAMIC EQUATION. 
! ALSO, THE OMEGA-ALPHA TERM IS COMPUTED FOR DIAGNOSTICS.
!
! PROGRAM HISTORY LOG:
! 87-06-??  JANJIC     - ORIGINATOR
! 95-03-25  BLACK      - CONVERSION FROM 1-D TO 2-D IN HORIZONTAL
! 96-05-??  JANJIC     - ADDED NONHYDROSTATIC EFFECTS & MERGED THE PREVIOUS SUBROUTINES PDTE AND 
!                        PDNEW
! 00-01-03  BLACK      - DISTRIBUTED MEMORY AND THREADS
! 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
!
! OUTPUT FILES:
! NONE
!
! USE MODULES: CONTIN
!              CTLBLK
!              DYNAM
!              F77KINDS
!              LOOPS
!              MASKS
!              MPPSTAFF
!              NHYDRO
!              PARMETA
!              SET_ZERO
!              VRBLS   
!
! DRIVER     : -----
!
! CALLS      : BOCOHMPI
!--------------------------------------------------------------------------------------------------
    USE CONTIN
    USE CTLBLK
    USE DYNAM
    USE F77KINDS
    USE LOOPS
    USE MASKS
    USE MPPSTAFF
    USE NHYDRO
    USE PARMETA
    USE SET_ZERO
    USE VRBLS
!
    IMPLICIT NONE
!
    INCLUDE "mpif.h"
!
    INTEGER(KIND=I4)                                                                            ::&
    & I       , J       , L
!
    INTEGER(KIND=I4), PARAMETER :: ITRMX = 0
    INTEGER(KIND=I4), PARAMETER :: KSMUD = 7
    INTEGER(KIND=I4), PARAMETER :: LNSDT = 7
!
    REAL   (KIND=R4), PARAMETER :: WC   = 2. / 3.
    REAL   (KIND=R4), PARAMETER :: RWCQ = (1. - WC) * 0.25
    REAL   (KIND=R4), PARAMETER :: RWC  = 1. / WC
!
    REAL   (KIND=R4)                                                                            ::&
    & A       , B       , C       ,                                                               &
    & DWDTP   , RHS     , TPMP    , TTAL
!
    REAL   (KIND=R4)    , DIMENSION(0:IM+1, 0:JM+1)                                             ::&
    & PRET    , RPSL    , TTB     , APDT    , PPDT    , TPM  
!------------------------------------------------
! COMPUTATION OF PRESSURE TENDENCY & PREPARATIONS
!------------------------------------------------
    DO 100 L=2,LM
!
!$omp parallel do
!
        DO I=1,IM
            DO J=1,JM
                DIV(I,J,L) = DIV(I,J,L-1) + DIV(I,J,L)
            END DO
        END DO

100 CONTINUE
!
!$omp parallel do
!
    DO J=1,JM    ! +1 - SAME LIKE IN PDETE.F90 (GEF)
        DO I=1,IM  
            PSDT(I,J) = -DIV(I,J,LM)
            APDT(I,J) = PSDT(I,J)
            PPDT(I,J) = PSDT(I,J)
           PDSLO(I,J) = PDSL(I,J)
            RPSL(I,J) = 1. / PDSL(I,J)
        END DO
    END DO
!
!$omp parallel do
!
    DO J=1,JM  
        DO I=1,IM
!
            PRET(I,J) = PSDT(I,J) * RES(I,J)
            PDSL(I,J) =   PD(I,J) * RES(I,J)
!
            PINT(I,J,1) = PT
!
             TPM(I,J)   = PT + PINT(I,J,2)
             TTB(I,J)   = 0.
!
        END DO
    END DO
!---------------------
! COMPUTATION OF ETADT
!---------------------
!
!$omp parallel do
!
    DO 300 L=1,LM-1
!
        DO J=1,JM   
            DO I=1,IM  
                ETADT(I,J,L) = -(PRET(I,J) * ETA(L+1) + DIV(I,J,L)) * HTM(I,J,L+1) * RPSL(I,J)
       
            END DO
        END DO
!
300 CONTINUE
!----------------------------------------------
! KINETIC ENERGY GENERATION TERMS IN T EQUATION 
!----------------------------------------------
!
!$omp parallel do private (DWDTP, RHS, TPMP, TTAL)
!
    DO J=1,JM   
        DO I=1,IM  
!                 
            DWDTP = DWDT(I,J,1)
            TPMP  = PINT(I,J,2) + PINT(I,J,3)
!
            TTAL  = 0.
!
            RHS           =   -DIV(I,J,1) * RTOP(I,J,1) * HTM(I,J,1) * DWDTP * EF4T
            OMGALF(I,J,1) = OMGALF(I,J,1) + RHS
                 T(I,J,1) = (TTAL * RDETA(1) + RHS) + T(I,J,1)
!
              PINT(I,J,2) = PRET(I,J) * (ETA(1) + ETA(2)) * DWDTP * DT + TPM(I,J) - PINT(I,J,1)
!
            TPM(I,J) = TPMP
            TTB(I,J) = TTAL
        END DO
    END DO
!
    DO 410 L=2,LM-1
!
!$omp parallel do private (DWDTP, RHS, TPMP, TTAL)
!   
    DO J=1,JM
        DO I=1,IM 
!      
            DWDTP = DWDT(I,J,L)
!
            TPMP  = PINT(I,J,L+1) + PINT(I,J,L+2)
!
            TTAL = 0.
!
            RHS = -(DIV(I,J,L-1) + DIV(I,J,L)) * RTOP(I,J,L) * HTM(I,J,L) * DWDTP * EF4T
!           
            OMGALF(I,J,L)   = OMGALF(I,J,L) + RHS
!
                 T(I,J,L)   = ((TTAL + TTB(I,J)) * RDETA(L) + RHS) + T(I,J,L)
!
              PINT(I,J,L+1) = PRET(I,J) * (ETA(L) + ETA(L+1)) * DWDTP * DT + TPM(I,J) - PINT(I,J,L)
!
            TPM(I,J) = TPMP
            TTB(I,J) = TTAL
!
        END DO
    END DO
!
410 CONTINUE
!
!$omp parallel do private (DWDTP, RHS)
!
    DO J=1,JM   
        DO I=1,IM 
!
            DWDTP = DWDT(I,J,LM)
!
            RHS   = -(DIV(I,J,LM-1) + DIV(I,J,LM)) * RTOP(I,J,LM) * HTM(I,J,LM) * DWDTP * EF4T
!
            OMGALF(I,J,LM) = OMGALF(I,J,LM) + RHS
!
                 T(I,J,LM)   = (TTB(I,J) * RDETA(LM) + RHS) + T(I,J,LM)
              PINT(I,J,LM+1) = PRET(I,J) * ( ETA(LM) + ETA(LM+1)) * DWDTP * DT + TPM(I,J)         &
    &                        - PINT(I,J,LM)
!
        END DO
    END DO
!---------------------------------------
! REGENERATE THE UNINTEGRATED DIVERGENCE
!---------------------------------------
    DO 425 L=LM,2,-1
!
!$omp parallel do
!
        DO J=1,JM
            DO I=1,IM 
                DIV(I,J,L) = DIV(I,J,L) - DIV(I,J,L-1)
            END DO
        END DO
!
425 CONTINUE
!
!-------------------------------------------- 
!SMOOTHING VERTICAL VELOCITY ALONG BOUNDARIES 
!--------------------------------------------
    CALL BOCOHMPI(DIV,LM)
    CALL BOCOHMPI(T,LM)
    CALL BOCOHMPI(ETADT(:,:,1:LM-1),LM-1)
!
    CALL BOCOHMPI(PINT,LM+1)
!
    RETURN
!
    END SUBROUTINE PDTEDT_GEF
