!-----------------------------------------------------------------------
SUBROUTINE VDIFLCZ (KIDIA, KFDIA, KLON, KLEV,&
 & PTSTEP,&
 & PLSM,&
 ! TIME T VARIABLES:
 & PAPHI,&
 & PAPHIF, PCP, PR, PRDELP,&
 & PU0, PV0, PT0, PAPH0,&
 ! TIME T-1 VARIABLES:
 & PU9, PV9, PT9,&
 ! OUTPUT UPDATED VARIABLES:
 & PUT1,PVT1,PTT1)  
!-----------------------------------------------------------------------

!**** *VDIFLCZ* - SURFACE DRAG AND VERTICAL DIFFUSION SCHEME

!     PURPOSE.
!     --------
!         TO COMPUTE THE FLUXES OF U, V, S=CP*T+PHI AND Q DUE TO THE VERTICAL
!     DIFFUSION AND THE SURFACE DRAG.

!     NB: Either vertical diffusion or surface drag can be applied to
!         U,V only, or to U,V and dse, but not to dse only.

!     INTERFACE.
!     ----------
!        *CALL* *VDIFLCZ(......)

!     PARAMETER DESCRIPTION                                   UNITS
!     --------- -----------                                   -----

!     INPUT ARGUMENTS
!     ---------------

!     DIMENSIONS:

!     KIDIA, KFDIA : START/END OF HORIZONTAL LOOP  (1, IPROF IN *CPG*).
!     KLON         : HORIZONTAL DIMENSION (NPROMA IN *CPG*).
!     KLEV         : END OF VERTICAL LOOP AND VERTICAL DIMENSION
!                    (NFLEVG IN *CPG*).

!     INPUT PARAMETERS AT T-1 (REAL) OR CONSTANT IN TIME:

!     PTSTEP       TIME STEP
!     PU0          X-VELOCITY COMPONENT                          M/S
!     PV0          Y-VELOCITY COMPONENT                          M/S
!     PT0          TEMPERATURE                                   K
!     PAPH0        PRESSURE ON HALF LEVELS                       PA
!        NB: THE INDEX GOES 1:KLEV+1 INSTEAD OF 0:KLEV FOR EASY OF COMPUTATION
!     PAPHI         GEOPOTENTIAL HEIGHT ON HALF-LEVELS
!     PAPHIF        GEOPOTENTIAL HEIGHT ON FULL LEVELS
!     PRDELP        INVERSE OF PDELP (LAYER THICKNESS IN PRESSURE UNITS)
!     PCP           SPECIFIC HEAT AT CONSTANT PRESSURE FOR AIR (Cpd(1-q)+Cpv*q)
!     PR            GAS CONSTANT FOR AIR (Rd(1-q)+Rv*q)
!     PLSM          LAND/SEA MASK

!     LOCAL VARIABLES (REAL):

!     ZCPTGZ        DRY STATIC ENERGY
!     ZGEO0        GEOPOTENTIAL                                  M2/S2
!     ZRHO          DENSITY
!     ZRDPHIF       INVERSE OF THE THICKNESS BETWEEN FULL LEVELS
!                   (IN GEOPOTENTIAL HEIGHT UNITS)
!     ZSOL          VARIABLES AT TIME T+1 AFTER VERTICAL DIFFUSION
!     ZM            TRIDIAGONAL MATRIX FOR THE VERTICAL DIFFUSION SCHEME
!     ZRHS          RIGHT HAND SIDE OF THE TRIDIAGONAL PROBLEM

!     OUTPUT PARAMETERS (REAL):

!     PUT1          UPDATED U-COMPONENT
!     PVT1          UPDATED V-COMPONENT
!     PTT1          UPDATED TEMPERATURE

!     VARIABLES IN COMMON:

!     YOEVDF       CONSTANTS FOR VERT DIFF (RLAM, RKAP)
!     YOMCST       CONSTANTS FOR PHYSICS (RD, RV, RCPD, RCPV)
!     YOMLUN       OUTPUT UNITS
!     YOPHLC       LOGICAL SWITCHES AND PARAMETERS FOR THE SIMPLE PHYSICS
!     YOMGEM       VERTICAL COORDINATE DEFINITION (VALH, VBH, VP00)

!     METHOD.
!     -------
!         MIXING LENGHT THEORY IS APPLIED TO COMPUTE THE EXCHANGE
!     COEFFICIENT FOR VERT DIFFUSION. VERT DIFF IS APPLIED TO
!     U, V AND S=CP*T+PHI.
!     TEMPORARY: NO VERT DIFF ON S.

!     EXTERNALS.
!     ----------
!     NONE

!     REFERENCE.
!     ----------
!     SEE LITERATURE (HOLTON OR HALTINER-WILLIAMS).

!     AUTHOR
!     ------
!     FROM VDIF (IFS_CY6) WRITTEN BY OLIVIER TALAGRAND - 22 SEPTEMBER 1988
!     NEW VERSION FOR IFS_CY10 BY:

!     ROBERTO BUIZZA                     *ECMWF*

!     MODIFICATIONS
!     -------------
!     ORIGINAL:      2 JUNE      1992
!     MODIFICATION: 25 SEPTEMBER 1992
!                   28/02/94 R Buizza - Constant Cp in dse definition
!                                     - additional switch for drag on dse (LSDRDS)
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        K.Yessad (Nov 2008): replace TRIDIALCZ by TRIDIA + cleanings
!     -----------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE YOEVDF   , ONLY : RLAM     ,RKAP
USE YOMCST   , ONLY : RG       ,RD       ,RCPD
USE YOPHLC   , ONLY : AH0      ,USTARL   ,USTARS   ,ALANDZ0  ,&
 & ASEAZ0   ,LVDFLC   ,LSDRLC   ,LZMCON   ,LKEXP    ,&
 & LVDFDS   ,LSDRDS  
USE YOMGEM   , ONLY : VALH     ,VBH      ,VP00

!     -----------------------------------------------------------------

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSTEP 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLSM(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHI(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHIF(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PR(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDELP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PU0(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PV0(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT0(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPH0(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PU9(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PV9(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT9(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUT1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVT1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTT1(KLON,KLEV) 

!     -----------------------------------------------------------------

INTEGER(KIND=JPIM) :: JPDIMZM

PARAMETER (JPDIMZM=3)
REAL(KIND=JPRB) :: ZRHO(KLON,0:KLEV)
REAL(KIND=JPRB) :: ZGEO0(KLON,KLEV),ZCPTGZ9(KLON,KLEV)
REAL(KIND=JPRB) :: ZRDPHIF(KLON,KLEV-1)
REAL(KIND=JPRB) :: ZM(JPDIMZM*KLON,KLEV,-1:1)
REAL(KIND=JPRB) :: ZSOL(JPDIMZM*KLON,KLEV),ZRHS(JPDIMZM*KLON,KLEV)

REAL(KIND=JPRB) :: ZPS(KLON)
REAL(KIND=JPRB) :: ZPLEV(KLON,0:KLEV), ZPLEVF(KLON,KLEV)
REAL(KIND=JPRB) :: ZZLEV(KLON,0:KLEV), ZZLEVF(KLON,KLEV)
REAL(KIND=JPRB) :: ZRDELPF(KLON,KLEV)

INTEGER(KIND=JPIM) :: IDIM, IEND, IFIRST, IS, IU, IV, JK, JLEV, JLON

REAL(KIND=JPRB) :: ZCD, ZCD2, ZCON1, ZCON2, ZDELTA, ZDSDPH,&
 & ZDT, ZFC, ZFLU, ZGAMMAD, ZH0, ZH1, ZK, ZKL, &
 & ZKU, ZL, ZLAM, ZP0, ZRDELTA, ZRDELZ, ZRDELZF, &
 & ZRGLAM, ZRGZ0, ZRI, ZRI2, ZRICPT, ZRIU, ZRLAM, &
 & ZRZ0, ZT0, ZUSTAR, ZZ0  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

!     -----------------------------------------------------------------

#include "tridia.h"

!     -----------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('VDIFLCZ',0,ZHOOK_HANDLE)
!     -----------------------------------------------------------------

!*    1. INITIALIZE CONSTANTS.
!     ------------------------

!     1.1 PHYSICAL CONSTANTS

!     ASYMPTOTYC MIXING LENGHT

ZLAM=RLAM/2.0_JPRB
ZRGLAM=1.0_JPRB/(RG*ZLAM)

!     CONSTANTS TO COMPUTE THE VERT DIFF IF(LZMCON)

ZRLAM=1.0_JPRB/ZLAM
ZT0=288.15_JPRB
ZGAMMAD=RG/RCPD
ZDELTA=RCPD/RD
ZRDELTA=1.0_JPRB/ZDELTA
ZP0=VP00
ZCON1=ZGAMMAD/(RG*ZT0)
ZCON2=ZT0/ZGAMMAD
ZH0=AH0
ZH1=8.000_JPRB

!     1.2 ARGUMENTS FOR CALLING SUBROUTINE TRIDIA

IFIRST=JPDIMZM*(KIDIA-1)+1
IEND=JPDIMZM*KFDIA
IDIM=JPDIMZM*KLON

!     1.3 TIME STEP

ZDT=PTSTEP
!     -----------------------------------------------------------------

!     2. DEFINE VARIABLES.
!     --------------------

!     2.1 DEFINE S=CP(Q)*T+GZ

DO JLEV=1,KLEV
  DO JLON=KIDIA,KFDIA
    ZGEO0(JLON,JLEV)=PAPHIF(JLON,JLEV)-PAPHI(JLON,KLEV)
    ZCPTGZ9(JLON,JLEV)=ZGEO0(JLON,JLEV)+PT9(JLON,JLEV)*RCPD
  ENDDO
ENDDO

!     2.2 INVERSE OF THE FULL LEVEL GEOPOTENTIAL DIFFERENCES

DO JLEV=1,KLEV-1
  DO JLON=KIDIA,KFDIA
    ZRDPHIF(JLON,JLEV)=1.0_JPRB/(PAPHIF(JLON,JLEV+1) - PAPHIF(JLON,JLEV))
  ENDDO
ENDDO

!      2.3 DENSITY
!          RHO = P/(R*T)
!            R = (RD(1-Q)+RV*Q)

DO JLEV=1,KLEV-1
  DO JLON=KIDIA,KFDIA
    ZRHO(JLON,JLEV)=PAPH0(JLON,JLEV+1) /&
     & ( 0.5_JPRB * (PT0(JLON,JLEV)   * PR(JLON,JLEV)  +&
     & PT0(JLON,JLEV+1) * PR(JLON,JLEV+1)) )  
  ENDDO
ENDDO

DO JLON=KIDIA,KFDIA
  ZRHO(JLON,0)=PAPH0(JLON,1)/( PT0(JLON,1)*PR(JLON,1) )
  ZRHO(JLON,KLEV)=PAPH0(JLON,KLEV+1)/( PT0(JLON,KLEV)*PR(JLON,KLEV) )
ENDDO

!     2.4 INITIALIZATION.

DO JLEV=1,KLEV
  DO JLON=KIDIA,KFDIA
    IU=JPDIMZM*(JLON-1)+1
    IV=IU+1
    IS=IU+2
    ZM(IU,JLEV,-1)=0.0_JPRB
    ZM(IU,JLEV, 0)=1.0_JPRB
    ZM(IU,JLEV, 1)=0.0_JPRB

    ZRHS(IU,JLEV)=0.0_JPRB
    ZRHS(IV,JLEV)=0.0_JPRB
    ZRHS(IS,JLEV)=0.0_JPRB
  ENDDO
ENDDO

!     2.5 VARIABLES IF(LZMCON)

IF(LZMCON) THEN
  DO JLON=KIDIA,KFDIA
    ZPS(JLON)=ZP0*( (1.0_JPRB-ZCON1*PAPHI(JLON,KLEV))**(ZDELTA) )
  ENDDO

!     PRESSURE ON THE HALF LEVELS

  DO JLEV=0,KLEV
    DO JLON=KIDIA,KFDIA
      ZPLEV(JLON,JLEV)=VALH(JLEV)+VBH(JLEV)*ZPS(JLON)
    ENDDO
  ENDDO

!    PRESSURE ON THE FULL LEVELS

  DO JLEV=1,KLEV
    DO JLON=KIDIA,KFDIA
      ZPLEVF(JLON,JLEV)=0.5_JPRB*(ZPLEV(JLON,JLEV-1)+ZPLEV(JLON,JLEV))
    ENDDO
  ENDDO

!    HEIGHT OF THE HALF LEVELS

  DO JLEV=0,KLEV
    DO JLON=KIDIA,KFDIA
      ZZLEV(JLON,JLEV) = ZCON2*( 1.0_JPRB-(ZPLEV(JLON,JLEV)/ZP0)**(ZRDELTA) )
    ENDDO
  ENDDO

!    HEIGHT OF THE FULL LEVELS ABOVE THE SURFACE

  DO JLEV=1,KLEV
    DO JLON=KIDIA,KFDIA
      ZZLEVF(JLON,JLEV) = ZCON2*&
       & ( 1.0_JPRB-(ZPLEVF(JLON,JLEV)/ZP0)**(ZRDELTA) )&
       & - ZZLEV(JLON,KLEV)  
    ENDDO
  ENDDO

!     HEIGHT OF THE HALF LEVELS ABOVE THE SURFACE

  DO JLEV=0,KLEV
    DO JLON=KIDIA,KFDIA
      ZZLEV(JLON,JLEV) = ZZLEV(JLON,JLEV)-ZZLEV(JLON,KLEV)
    ENDDO
  ENDDO

!     INVERSE OF THE THICKNESS AT FULL LEVELS (IN PRESSURE)

  DO JLEV=1,KLEV
    DO JLON=KIDIA,KFDIA
      ZRDELPF(JLON,JLEV) =1.0_JPRB/(ZPLEV(JLON,JLEV)-ZPLEV(JLON,JLEV-1))
    ENDDO
  ENDDO
ENDIF
!     -----------------------------------------------------------------

!*    3. VERTICAL DIFFUSION.
!     ----------------------
!     -----------------------------------------------------------------
!     -----------------------------------------------------------------
IF(LVDFLC) THEN
!     -----------------------------------------------------------------
!     -----------------------------------------------------------------

  DO JLEV=1,KLEV-1

    DO JLON=KIDIA,KFDIA
      IU=JPDIMZM*(JLON-1)+1

!     COMPUTATION OF THE DIFFUSION COEFFICIENT

!     -----------------------------------------------------------------
      IF(.NOT.LZMCON) THEN
!     -----------------------------------------------------------------

!         - MIXING LENGHT: L = K*PHI / (1+ K*PHI/(LAMBDA*G) )
!           WHERE PHI IS REFERRED TO THE SURFACE

        ZL = RKAP*ZGEO0(JLON,JLEV)/( 1.0_JPRB + ZRGLAM*RKAP*ZGEO0(JLON,JLEV) )

!         - DIFFUSION COEFFICIENT: K = L**2 * |DU/DZ| * (1+RI)**1/2
!           WHERE: RI = - (G * D(THETA)/dZ) / (THETA * |DU/DZ|**2)
!               THETA = POTENTIAL TEMPERATURE

        ZRIU = ( (PU0(JLON,JLEV+1) - PU0(JLON,JLEV))&
         & *(PU0(JLON,JLEV+1) - PU0(JLON,JLEV))&
         & +(PV0(JLON,JLEV+1) - PV0(JLON,JLEV))&
         & *(PV0(JLON,JLEV+1) - PV0(JLON,JLEV)) ) *&
         & ZRDPHIF(JLON,JLEV)*ZRDPHIF(JLON,JLEV)  

        ZDSDPH = ( PCP(JLON,JLEV+1)*PT0(JLON,JLEV+1) -&
         & PCP(JLON,JLEV)  *PT0(JLON,JLEV)   ) *&
         & ZRDPHIF(JLON,JLEV) + 1.0_JPRB  

        ZRICPT = - 2.0_JPRB*ZDSDPH/&
         & ( PCP(JLON,JLEV+1)*PT0(JLON,JLEV+1) +&
         & PCP(JLON,JLEV)  *PT0(JLON,JLEV)   )  

        ZRI2 = ZRIU + ZRICPT

        IF(ZRI2 > 0) THEN
          ZRI = RG * SQRT(ZRI2)
        ELSE
          ZRI=0.0_JPRB
        ENDIF

        ZK = RG*RG*ZDT*ZL*ZL*ZRI*ZRHO(JLON,JLEV)*ZRDPHIF(JLON,JLEV)

        ZKU  = ZK*PRDELP(JLON,JLEV)
        ZKL  = ZK*PRDELP(JLON,JLEV+1)
!     -----------------------------------------------------------------
      ELSE
!     -----------------------------------------------------------------

!     VERT DIFF WITH CONSTANT COEFFICIENT

!     COMPUTE THE REFERENCE VALUE FOR USTAR

        ZUSTAR=PLSM(JLON)*USTARL+(1.0_JPRB-PLSM(JLON))*USTARS

        IF(.NOT.LKEXP) THEN

!     Vert diff coefficient: RHO*K=R0*EXP(-Z/ZH1)*USTAR*ZL*(1-Z/H0)
!                            WITH R0=1, H1=8.000

          IF(ZZLEV(JLON,JLEV) <= ZH0) THEN
            ZL = RKAP*ZZLEV(JLON,JLEV)/&
             & ( 1.0_JPRB + ZRLAM*RKAP*ZZLEV(JLON,JLEV) )  
            ZRDELZ   = 1.0_JPRB/(ZZLEVF(JLON,JLEV+1)-ZZLEVF(JLON,JLEV))
            ZFC =  (1.0_JPRB-(ZZLEV(JLON,JLEV)/ZH0))*EXP(-ZZLEV(JLON,JLEV)/ZH1)
            ZK  = RG*ZDT*ZL*ZFC*ZUSTAR*ZRDELZ
          ELSE
            ZK  = 0.0_JPRB
          ENDIF
        ELSE

!     Vert diff coefficient: RHO*K=R0*EXP(-Z/H0)*USTAR*ZL

          ZL = RKAP*ZZLEV(JLON,JLEV)/( 1.0_JPRB + ZRLAM*RKAP*ZZLEV(JLON,JLEV) )
          ZRDELZ   = 1.0_JPRB/(ZZLEVF(JLON,JLEV+1)-ZZLEVF(JLON,JLEV))
          ZFC =  EXP(-ZZLEV(JLON,JLEV)/ZH0)
          ZK  = RG*ZDT*ZL*ZFC*ZUSTAR*ZRDELZ
        ENDIF

        ZKU  = ZK*ZRDELPF(JLON,JLEV)
        ZKL  = ZK*ZRDELPF(JLON,JLEV+1)
!     -----------------------------------------------------------------
      ENDIF
!     -----------------------------------------------------------------
      ZM(IU,JLEV,0) = ZM(IU,JLEV,0) - ZKU
      ZM(IU,JLEV,1) = ZM(IU,JLEV,1) + ZKU
      ZM(IU,JLEV+1,-1) = ZM(IU,JLEV+1,-1) + ZKL
      ZM(IU,JLEV+1,0) = ZM(IU,JLEV+1,0) - ZKL

    ENDDO
  ENDDO
!     -----------------------------------------------------------------
!     -----------------------------------------------------------------
ENDIF
!     -----------------------------------------------------------------
!     -----------------------------------------------------------------

!*    4. AS IN 3.: FILL IN DRY STATIC ZM.
!     -----------------------------------

!     4.1 DRY STATIC ENERGY

IF(.NOT.LSDRDS) THEN

!     NO surface drag on dse

  DO JK=-1,1
    DO JLEV=1,KLEV
      DO JLON=KIDIA,KFDIA
        IU=JPDIMZM*(JLON-1)+1
        IS=IU+2
        ZM(IS,JLEV,JK) = ZM(IU,JLEV,JK)
      ENDDO
    ENDDO
  ENDDO
ENDIF

!*    5. MOMENTUM FLUX THROUGH SURFACE.
!     ---------------------------------

!     -----------------------------------------------------------------
!     -----------------------------------------------------------------
IF(LSDRLC) THEN
!     -----------------------------------------------------------------
!     -----------------------------------------------------------------

  DO JLON=KIDIA,KFDIA
    IU=JPDIMZM*(JLON-1)+1

!     COMPUTATION OF THE DRAG COEFFICIENT

!     -----------------------------------------------------------------
    IF(.NOT.LZMCON) THEN
!     -----------------------------------------------------------------

!         - SURFACE STRESS: TAU = CD * |DU/DZ|
!           WHERE: CD = RHO * L**2 * |DU/DZ| = RHO * USTAR**2
!           WITH:  USTAR = (CD * DU/DT) / RHO = CONSTANT
!                      L = K * (Z-Z0)
!           THAT IS: USTAR = (K/(LN(1+Z/Z0)) * U

      ZZ0=PLSM(JLON)*ALANDZ0+(1.0_JPRB-PLSM(JLON))*ASEAZ0
      ZRGZ0 = 1.0_JPRB/(RG*ZZ0)
      ZCD=RKAP/LOG(1.0_JPRB+ZGEO0(JLON,KLEV)*ZRGZ0)
      ZCD2 = ZCD*ZCD

      ZFLU = RG*ZDT * PRDELP(JLON,KLEV) *&
       & ZCD2*ZRHO(JLON,KLEV) *&
       & SQRT( PU0(JLON,KLEV)*PU0(JLON,KLEV) +&
       & PV0(JLON,KLEV)*PV0(JLON,KLEV) )  
!     -----------------------------------------------------------------
    ELSE
!     -----------------------------------------------------------------

!     SURFACE DRAG WITH CONSTANT COEFFICIENT

      ZUSTAR=PLSM(JLON)*USTARL+(1.0_JPRB-PLSM(JLON))*USTARS
      ZZ0=PLSM(JLON)*ALANDZ0+(1.0_JPRB-PLSM(JLON))*ASEAZ0
      ZRZ0 = 1.0_JPRB/ZZ0
      ZL=RKAP*ZZLEVF(JLON,KLEV)/LOG(1.0_JPRB+ZZLEVF(JLON,KLEV)*ZRZ0)
      ZRDELZF  = 1.0_JPRB/ZZLEVF(JLON,KLEV)
      ZFLU = RG*ZDT*ZL*ZUSTAR*ZRDELZF*ZRDELPF(JLON,KLEV)
!     -----------------------------------------------------------------
    ENDIF
!     -----------------------------------------------------------------

!     ADD SURFACE DRAG TO THE ZM MATRIX AT LEVEL KLEV FOR U-COMPONENT

    ZM(IU,KLEV,0) = ZM(IU,KLEV,0) + ZFLU
  ENDDO
!     -----------------------------------------------------------------
!     -----------------------------------------------------------------
ENDIF
!     -----------------------------------------------------------------
!     -----------------------------------------------------------------

!*    6. FILL IN V-DIFF AND DEFINE THE RHS FOR THE TRIDIAGONAL PROBLEM.
!     -----------------------------------------------------------------

DO JLEV=1,KLEV

  DO JLON=KIDIA,KFDIA
    IU = JPDIMZM*(JLON -1) + 1
    IV = IU + 1
    IS = IU + 2

!     V-MATRICES

    ZM(IV,JLEV,-1) = ZM(IU,JLEV,-1)
    ZM(IV,JLEV, 0) = ZM(IU,JLEV, 0)
    ZM(IV,JLEV, 1) = ZM(IU,JLEV, 1)

    IF(LSDRDS) THEN

!     Surface drag on dse

      ZM(IS,JLEV,-1) = ZM(IU,JLEV,-1)
      ZM(IS,JLEV, 0) = ZM(IU,JLEV, 0)
      ZM(IS,JLEV, 1) = ZM(IU,JLEV, 1)
    ENDIF

!     RIGHT-HAND SIDES

    ZRHS(IU,JLEV) = PU9(JLON,JLEV)
    ZRHS(IV,JLEV) = PV9(JLON,JLEV)
    ZRHS(IS,JLEV) = ZCPTGZ9(JLON,JLEV)
  ENDDO

ENDDO
!     -----------------------------------------------------------------

!*    7. SOLVE LINEAR SYSTEMS AND COMPUTE TENDENCIES.
!     -----------------------------------------------

!     -----------------------------------------------------------------

!     7.1 CONTROL

! 710 CONTINUE
!     WRITE (NULOUT,FMT='('' >> VDIFLCZ: LABEL 710'')')

!     DO 711 JLEV=1,KLEV

!     DO 712 JLON=KIDIA,KFDIA
!     IU = JPDIMZM*(JLON -1) + 1
!     IV = IU + 1
!     IS = IU + 2
!     IF(LCZDEB) THEN
!        WRITE(NULOUT,9920) JLON,JLEV,IU,IV,IS
!        WRITE(NULOUT,9971) ZM(IU,JLEV,-1)
!        WRITE(NULOUT,9972) ZM(IU,JLEV, 0)
!        WRITE(NULOUT,9973) ZM(IU,JLEV, 1)
!        WRITE(NULOUT,9974) ZM(IV,JLEV,-1)
!        WRITE(NULOUT,9975) ZM(IV,JLEV, 0)
!        WRITE(NULOUT,9976) ZM(IV,JLEV, 1)
!        WRITE(NULOUT,9977) ZM(IS,JLEV,-1)
!        WRITE(NULOUT,9978) ZM(IS,JLEV, 0)
!        WRITE(NULOUT,9979) ZM(IS,JLEV, 1)
!     ENDIF
! 712 CONTINUE
! 711 CONTINUE
!     -----------------------------------------------------------------

!     SOLVE THE TRIDIAGONAL PROBLEM

CALL TRIDIA(KLEV,IDIM,IFIRST,IEND,2,ZM,ZRHS,ZSOL)
!     -----------------------------------------------------------------

!     8. INCREMENTS COMPUTATION

DO JLEV=1,KLEV

  DO JLON=KIDIA,KFDIA
    IU = JPDIMZM*(JLON -1) + 1
    IV = IU + 1
    IS = IU + 2

!     COMPUTE INCREMENTS DUE TO VDIFLCZ
!     (TEMPERATURE INCREMENTS IN IS POSITION)

    ZSOL(IU,JLEV) = (ZSOL(IU,JLEV)-PU9(JLON,JLEV))
    ZSOL(IV,JLEV) = (ZSOL(IV,JLEV)-PV9(JLON,JLEV))
    ZSOL(IS,JLEV) = (ZSOL(IS,JLEV)-ZGEO0(JLON,JLEV))/RCPD- PT9(JLON,JLEV)
  ENDDO

ENDDO
!     -----------------------------------------------------------------

!     8.1 CONTROL

! 810 CONTINUE
!     WRITE (NULOUT,FMT='('' >> VDIFLCZ: LABEL 810'')')

!     WRITE(NULOUT,9910)
!     DO 811 JLEV=1,KLEV

!     DO 812 JLON=KIDIA,KFDIA
!     IF(LCZDEB) THEN
!      IF((JLON.GE.10.AND.JLON.LE.12).AND.JLEV.EQ.16) THEN
!        WRITE(NULOUT,9920) JLON,JLEV
!        WRITE(NULOUT,9930) PUT1(JLON,JLEV)
!        WRITE(NULOUT,9931) PVT1(JLON,JLEV)
!        WRITE(NULOUT,9932) PTT1(JLON,JLEV)
!      ENDIF
!     ENDIF
! 812 CONTINUE
! 811 CONTINUE
!     -----------------------------------------------------------------

!     9. UPDATE VARIABLES

DO JLEV=1,KLEV

  DO JLON=KIDIA,KFDIA
    IU = JPDIMZM*(JLON -1) + 1
    IV = IU + 1
    IS = IU + 2

    PUT1(JLON,JLEV) = PUT1(JLON,JLEV)+ZSOL(IU,JLEV)
    PVT1(JLON,JLEV) = PVT1(JLON,JLEV)+ZSOL(IV,JLEV)

    IF(LVDFDS) PTT1(JLON,JLEV) = PTT1(JLON,JLEV)+ZSOL(IS,JLEV)

  ENDDO

ENDDO
!     -----------------------------------------------------------------

!     10. CONTROL

!1000 CONTINUE
!     WRITE (NULOUT,FMT='('' >> VDIFLCZ: LABEL 1000'')')

!     WRITE(NULOUT,9910)
!     DO 1001 JLEV=1,KLEV

!     DO 1002 JLON=KIDIA,KFDIA
!     IF(LCZDEB) THEN
!      IF((JLON.GE.10.AND.JLON.LE.12).AND.JLEV.EQ.16) THEN
!        WRITE(NULOUT,9920) JLON,JLEV
!        WRITE(NULOUT,9940) PUT1(JLON,JLEV)
!        WRITE(NULOUT,9941) PVT1(JLON,JLEV)
!        WRITE(NULOUT,9942) PTT1(JLON,JLEV)
!      ENDIF
!     ENDIF
!1002 CONTINUE
!1001 CONTINUE
!     -----------------------------------------------------------------
9910 FORMAT(' >> VDIFLCZ_T: CONTROL')
9918 FORMAT(' JLON,JLEV= ',2(1X,I4))
9919 FORMAT(' JLON,JLEV,IU= ',3(1X,I4))
9920 FORMAT(' JLON,JLEV,IU,IV,IS= ',5(1X,I4))
9930 FORMAT(' U-COMPONENT: T-1= ',1(1X,E16.6))
9931 FORMAT(' V-COMPONENT: T-1= ',1(1X,E16.6))
9932 FORMAT(' TEMPERATURE: T-1= ',1(1X,E16.6))
9940 FORMAT(' U-COMPONENT: T+1= ',1(1X,E16.6))
9941 FORMAT(' V-COMPONENT: T+1= ',1(1X,E16.6))
9942 FORMAT(' TEMPERATURE: T+1= ',1(1X,E16.6))
9971 FORMAT(' U-COMP ZM(-1)= ',(1X,E16.6))
9972 FORMAT(' U-COMP ZM( 0)= ',(1X,E16.6))
9973 FORMAT(' U-COMP ZM( 1)= ',(1X,E16.6))
9974 FORMAT(' V-COMP ZM(-1)= ',(1X,E16.6))
9975 FORMAT(' V-COMP ZM( 0)= ',(1X,E16.6))
9976 FORMAT(' V-COMP ZM( 1)= ',(1X,E16.6))
9977 FORMAT(' STA EN ZM(-1)= ',(1X,E16.6))
9978 FORMAT(' STA EN ZM( 0)= ',(1X,E16.6))
9979 FORMAT(' STA EN ZM( 1)= ',(1X,E16.6))
!     -----------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('VDIFLCZ',1,ZHOOK_HANDLE)
END SUBROUTINE VDIFLCZ
!     -----------------------------------------------------------------

