!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
    SUBROUTINE VADZ
!     ******************************************************************
!$$$  SUBPROGRAM DOCUMENTATION BLOCK
!                .      .    .
! SUBPROGRAM:    VADZ        VERTICAL ADVECTION OF HEIGHT
!   PRGRMMR: JANJIC          ORG: W/NP22     DATE: 93-11-17

! ABSTRACT:
!     VADV CALCULATES THE CONTRIBUTION OF THE VERTICAL ADVECTION
!     OF HEIGHT IN ORDER TO COMPUTE W=DZ/DT DIAGNOSTICALLY

! PROGRAM HISTORY LOG:
!   96-05-??  JANJIC     - ORIGINATOR
!   00-01-04  BLACK      - DISTRIBUTED MEMORY AND THREADS

! USAGE: CALL VADZ FROM MAIN PROGRAM
!   INPUT ARGUMENT LIST:
!       NONE

!   OUTPUT ARGUMENT LIST:
!     NONE

!   OUTPUT FILES:
!     NONE

!   SUBPROGRAMS CALLED:

!     UNIQUE: NONE

!     LIBRARY: NONE

!   COMMON BLOCKS: CTLBLK
!                  MASKS
!                  LOOPS
!                  DYNAM
!                  VRBLS
!                  CONTIN
!                  NHYDRO
!                  INDX
! ATTRIBUTES:
!   LANGUAGE: FORTRAN 90
!   MACHINE : IBM SP
!$$$
!***********************************************************************
    INCLUDE "parmeta.f90"
    INCLUDE "mpp.h"
!-----------------------------------------------------------------------
    PARAMETER &
    (IMJM=IM*JM-JM/2,JAM=6+2*(JM-10) &
    , LM1=LM-1,LM2=LM-2,LP1=LM+1,KSMUD=0)
!-----------------------------------------------------------------------
    LOGICAL :: &
    RUN,FIRST,RESTRT,SIGMA
!-----------------------------------------------------------------------
    INCLUDE "COMM_CTLBLK.f90"
!-----------------------------------------------------------------------
    INCLUDE "COMM_MASKS.f90"
!-----------------------------------------------------------------------
    include "COMM_LOOPS.f90"
!-----------------------------------------------------------------------
    INCLUDE "COMM_DYNAM.f90"
!-----------------------------------------------------------------------
    INCLUDE "COMM_VRBLS.f90"
!-----------------------------------------------------------------------
    INCLUDE "COMM_CONTIN.f90"
!-----------------------------------------------------------------------
    INCLUDE "COMM_NHYDRO.f90"
!-----------------------------------------------------------------------
    INCLUDE "COMM_CLDWTR.f90"
!-----------------------------------------------------------------------
    INCLUDE "COMM_INDX.f90"
!-----------------------------------------------------------------------
    REAL :: &
    PRET  (IDIM1:IDIM2,JDIM1:JDIM2) &
    ,TTB   (IDIM1:IDIM2,JDIM1:JDIM2),ALP1  (IDIM1:IDIM2,JDIM1:JDIM2) &
    ,FNE   (IDIM1:IDIM2,JDIM1:JDIM2),FSE   (IDIM1:IDIM2,JDIM1:JDIM2) &
    ,ETADTL(IDIM1:IDIM2,JDIM1:JDIM2)
!-----------------------------------------------------------------------
    G=9.8
    RG=1./9.8
    RDT=1./DT
!-----------------------------------------------------------------------
! omp parallel do
    DO J=MYJS,MYJE
        DO I=MYIS,MYIE
            W(I,J,LM+1)=0.
            Z(I,J,LM+1)=0.
            IF(SIGMA)Z(I,J,LM+1)=FIS(I,J)*RG
            PRET(I,J)=PSDT(I,J)*RES(I,J)
        !       PRET(I,J)=PSDT(I,J)/ETA(LMH(I,J)+1)
            ALP1(I,J)=ALOG(PINT(I,J,LM+1))
        ENDDO
    ENDDO
!***
    DO 50 L=LM,1,-1
        ZETA=DFL(L)*RG
        DTLRG=DETA(L)*RG
    
    ! omp parallel do
        DO J=MYJS,MYJE
            DO I=MYIS,MYIE
                ALP1P=ALOG(PINT(I,J,L))
                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)
                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)*HBM2(I,J) &
                +W(I,J,L+1)
            
                ALP1(I,J)=ALP1P
            ENDDO
        ENDDO
    50 END DO
!----------------------------------------------------------------------
!!$omp parallel do
!     DO L=1,LM+1
!       DO J=MYJS,MYJE
!       DO I=MYJS,MYJE
!          W(I,J,L)=(W(I,J,L)-W(I,J,1))*HTM(I,J,L)*HBM2(I,J)
!       ENDDO
!       ENDDO
!      ENDDO
!-----------------------------------------------------------------------
    DO L=1,LM
    
    ! omp parallel do
        DO J=MYJS,MYJE
            DO I=MYIS,MYIE
                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)*HBM2(I,J)*0.5*RDT
            ENDDO
        ENDDO
    ENDDO
!-----------------------------------------------------------------------
!--------------SMOOTHING VERTICAL VELOCITY AT H POINTS------------------
!-----------------------------------------------------------------------
!      IF(KSMUD.GT.0)THEN
!C-----------------------------------------------------------------------
!        NSMUD=KSMUD
!        DO 90 L=1,LM1
!C
!        DO J=1,JM
!        DO I=1,IM
!          ETADT(I,J,L)=ETADT(I,J,L)*HBM2(I,J)
!        ENDDO
!        ENDDO
!C
!        DO 90 KS=1,NSMUD
!        DO J=MYJS,MYJE
!        DO I=MYIS,MYIE
!          FNE(I,J)=(ETADT(I+IHE(J),J+1,L)-ETADT(I,J,L))
!     1             *HTM(I,J,L+1)*HTM(I+IHE(J),J+1,L+1)
!        ENDDO
!        ENDDO
!C
!        DO J=MYJS1,MYJE
!        DO I=MYIS,MYIE
!          FSE(I,J)=(ETADT(I+IHE(J),J-1,L)-ETADT(I,J,L))
!     1             *HTM(I+IHE(J),J-1,L+1)*HTM(I,J,L+1)
!        ENDDO
!        ENDDO
!C
!        DO J=MYJS2,MYJE2
!        DO I=MYIS1,MYIE1
!          ETADTL(I,J)=(FNE(I,J)-FNE(I+IHW(J),J-1)
!     1                +FSE(I,J)-FSE(I+IHW(J),J+1))*HBM2(I,J)
!        ENDDO
!        ENDDO
!C
!        DO J=MYJS2,MYJE2
!        DO I=MYIS1,MYIE1
!          ETADT(I,J,L)=ETADTL(I,J)*0.125+ETADT(I,J,L)
!        ENDDO
!        ENDDO
!   90   CONTINUE
!C-----------------------------------------------------------------------
!      ENDIF
!C-----------------------------------------------------------------------
! omp parallel do
    DO J=MYJS,MYJE
        DO I=MYIS,MYIE
            TTB(I,J)=0.
        ENDDO
    ENDDO

    DO L=1,LM-1
    ! omp parallel do
        DO J=MYJS2,MYJE2
            DO I=MYIS1,MYIE1
                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
            ENDDO
        ENDDO
    ENDDO

! omp parallel do
    DO J=MYJS2,MYJE2
        DO I=MYIS1,MYIE1
            W(I,J,LM)=TTB(I,J)*RDETA(LM)+W(I,J,LM)
        ENDDO
    ENDDO
!-----------------------------------------------------------------------
    RETURN
    END SUBROUTINE VADZ
