SUBROUTINE CUDTDQN2TL &
 & (  KIDIA,    KFDIA,    KLON,    KLEV,&
 & KTOPM2,   KCTOP,    KDTOP,&
 & LDRAIN1D, LDCUM,    LDDRAF,   PTSPHY,&
 & PAPH5,    PGEOH5,   PGEO5,&
 & PTEN5,    PTENH5,   PQEN5,    PQENH5,    PQSEN5,&
 & PLGLAC5,  PLUDE5,   PMFU5,    PMFD5,&
 & PMFUS5,   PMFDS5,   PMFUQ5,   PMFDQ5,&
 & PMFUL5,   PDMFUP5,  PDMFDP5,  PDPMEL5,&
 & PTENT5,   PTENQ5,   PENTH5,&
 & PAPH,     PGEOH,    PGEO,&
 & PTEN,     PTENH,    PQEN,     PQENH,     PQSEN,&
 & PLGLAC,   PLUDE,    PMFU,     PMFD,&
 & PMFUS,    PMFDS,    PMFUQ,    PMFDQ,&
 & PMFUL,    PDMFUP,   PDMFDP,   PDPMEL,&
 & PTENT,    PTENQ,    PENTH )  

!**** *CUDTDQN2TL* - UPDATES T AND Q TENDENCIES, PRECIPITATION RATES
!                   DOES GLOBAL DIAGNOSTICS
!                   (Tangent linear)

!          P. LOPEZ      E.C.M.W.F      15/12/2003

!          Adapted from

!          M.TIEDTKE         E.C.M.W.F.     7/86 MODIF. 12/89
!          J.F. MAHFOUF      E.C.M.W.F      05/12/96

!**   INTERFACE.
!     ----------

!          *CUDTDQN2TL* IS CALLED FROM *CUMASTRNTL*

!     PARAMETER     DESCRIPTION                                   UNITS
!     ---------     -----------                                   -----
!     INPUT PARAMETERS (INTEGER):

!    *KIDIA*        START POINT
!    *KFDIA*        END POINT
!    *KLON*         NUMBER OF GRID POINTS PER PACKET
!    *KLEV*         NUMBER OF LEVELS
!    *KCTOP*        CLOUD TOP LEVEL
!    *KDTOP*        TOP LEVEL OF DOWNDRAFTS

!    INPUT PARAMETERS (LOGICAL): 

!    *LDCUM*        FLAG: .TRUE. FOR CONVECTIVE POINTS 
!    *LDDRAF*       FLAG: .TRUE. FOR DOWNDRAFT LEVEL

!    INPUT PARAMETERS (REAL):

!    *PTSPHY*       TIME STEP FOR THE PHYSICS                       S
!    *PAPH5*        PROVISIONAL PRESSURE ON HALF LEVELS         (Trajectory)  
!    *PGEOH5*       GEOPOTENTIAL ON HALF LEVELS                 (Trajectory)
!    *PGEO5*        GEOPOTENTIAL ON FULL LEVELS                 (Trajectory) 
!    *PTEN5*        PROVISIONAL ENVIRONMENT TEMPERATURE (T+1)   (Trajectory)  
!    *PQEN5*        PROVISIONAL ENVIRONMENT SPEC. HUMIDITY (T+1)(Trajectory) 
!    *PTENH5*       ENV. TEMPERATURE (T+1) ON HALF LEVELS       (Trajectory)  
!    *PQENH5*       ENV. SPEC. HUMIDITY (T+1) ON HALF LEVELS    (Trajectory)  
!    *PQSEN5*       SATURATION ENV. SPEC. HUMIDITY (T+1)        (Trajectory) 
!    *PLGLAC5*      FLUX OF FROZEN CLOUDWATER IN UPDRAFTS       (Trajectory) 
!    *PLUDE5*       DETRAINED LIQUID WATER                      (Trajectory) 
!    *PMFU5*        MASSFLUX UPDRAFTS                           (Trajectory) 
!    *PMFD5*        MASSFLUX DOWNDRAFTS                         (Trajectory) 
!    *PMFUS5*       FLUX OF DRY STATIC ENERGY IN UPDRAFTS       (Trajectory) 
!    *PMFDS5*       FLUX OF DRY STATIC ENERGY IN DOWNDRAFTS     (Trajectory) 
!    *PMFUQ5*       FLUX OF SPEC. HUMIDITY IN UPDRAFTS          (Trajectory) 
!    *PMFDQ5*       FLUX OF SPEC. HUMIDITY IN DOWNDRAFTS        (Trajectory) 
!    *PMFUL5*       FLUX OF LIQUID WATER IN UPDRAFTS            (Trajectory) 
!    *PDMFUP5*      FLUX DIFFERENCE OF PRECIP. IN UPDRAFTS      (Trajectory) 
!    *PDMFDP5*      FLUX DIFFERENCE OF PRECIP. IN DOWNDRAFTS    (Trajectory) 
!    *PDPMEL5*      CHANGE IN PRECIP.-FLUXES DUE TO MELTING     (Trajectory) 

!    *PAPH*         PROVISIONAL PRESSURE ON HALF LEVELS            PA
!    *PGEOH*        GEOPOTENTIAL ON HALF LEVELS                   M2/S2
!    *PGEO*         GEOPOTENTIAL ON FULL LEVELS                   M2/S2
!    *PTEN*         PROVISIONAL ENVIRONMENT TEMPERATURE (T+1)       K
!    *PQEN*         PROVISIONAL ENVIRONMENT SPEC. HUMIDITY (T+1)  KG/KG
!    *PTENH*        ENV. TEMPERATURE (T+1) ON HALF LEVELS           K
!    *PQENH*        ENV. SPEC. HUMIDITY (T+1) ON HALF LEVELS      KG/KG
!    *PQSEN*        SATURATION ENV. SPEC. HUMIDITY (T+1)          KG/KG
!    *PLGLAC*       FLUX OF FROZEN CLOUDWATER IN UPDRAFTS         KG/(M2*S) 
!    *PLUDE*        DETRAINED LIQUID WATER                        KG/(M3*S)
!    *PMFU*         MASSFLUX UPDRAFTS                             KG/(M2*S)
!    *PMFD*         MASSFLUX DOWNDRAFTS                           KG/(M2*S)
!    *PMFUS*        FLUX OF DRY STATIC ENERGY IN UPDRAFTS          J/(M2*S)
!    *PMFDS*        FLUX OF DRY STATIC ENERGY IN DOWNDRAFTS        J/(M2*S)
!    *PMFUQ*        FLUX OF SPEC. HUMIDITY IN UPDRAFTS            KG/(M2*S)
!    *PMFDQ*        FLUX OF SPEC. HUMIDITY IN DOWNDRAFTS          KG/(M2*S)
!    *PMFUL*        FLUX OF LIQUID WATER IN UPDRAFTS              KG/(M2*S)
!    *PDMFUP*       FLUX DIFFERENCE OF PRECIP. IN UPDRAFTS        KG/(M2*S)
!    *PDMFDP*       FLUX DIFFERENCE OF PRECIP. IN DOWNDRAFTS      KG/(M2*S)
!    *PDPMEL*       CHANGE IN PRECIP.-FLUXES DUE TO MELTING       KG/(M2*S)

!    UPDATED PARAMETERS (REAL):

!    *PTENT5*       TEMPERATURE TENDENCY                        (Trajectory)
!    *PTENQ5*       MOISTURE TENDENCY                           (Trajectory)

!    *PTENT*        TEMPERATURE TENDENCY                           K/S
!    *PTENQ*        MOISTURE TENDENCY                             KG/(KG S)

!    OUTPUT PARAMETERS (REAL):

!    *PENTH5*       INCREMENT OF DRY STATIC ENERGY              (Trajectory)

!    *PENTH*        INCREMENT OF DRY STATIC ENERGY                 J/(KG*S) 

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

!               MODIFICATIONS

!        M.Hamrud     01-Oct-2003 CY28 Cleaning

!        D.Gregory    1996-09-20  Changed so can be used with diagnostic 
!                                 cloud scheme 

!        P. Lopez     11-01-2007  Removed LPHYLIN switch
!        P. Lopez     22-10-2007  Implicit option added
!        A. Geer      01-10-2008  LDRAIN1D name change to reflect usage

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

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

USE YOMCST   , ONLY : RG       ,RCPD     ,RLVTT    ,RLSTT    ,RLMLT
USE YOEPHLI  , ONLY : RLPTRC
USE YOPHNC   , ONLY : LENCLD2
USE YOECUMF2 , ONLY : RMFSOLTQ2

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 
INTEGER(KIND=JPIM),INTENT(IN)    :: KTOPM2 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCTOP(KLON)
INTEGER(KIND=JPIM),INTENT(IN)    :: KDTOP(KLON) 
LOGICAL           ,INTENT(IN)    :: LDRAIN1D
LOGICAL           ,INTENT(IN)    :: LDCUM(KLON) 
LOGICAL           ,INTENT(IN)    :: LDDRAF(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSPHY
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPH5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEO5(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOH5(KLON,KLEV+1)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQEN5(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENH5(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQENH5(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQSEN5(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLGLAC5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLUDE5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFD5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFUS5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFDS5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFUQ5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFDQ5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFUL5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDMFUP5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDMFDP5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDPMEL5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENT5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENQ5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PENTH5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPH(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEO(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEOH(KLON,KLEV+1)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTEN(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQEN(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENH(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQENH(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQSEN(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLGLAC(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PLUDE(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFD(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFUS(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFDS(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFUQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFDQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFUL(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDMFUP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDMFDP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDPMEL(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENT(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PENTH(KLON,KLEV) 

INTEGER(KIND=JPIM) :: JK, JL, IK

REAL(KIND=JPRB) :: ZMFUS5(KLON,KLEV), ZMFUQ5(KLON,KLEV),&
                 & ZMFDS5(KLON,KLEV), ZMFDQ5(KLON,KLEV)
REAL(KIND=JPRB) :: ZMFUS (KLON,KLEV), ZMFUQ (KLON,KLEV),&
                 & ZMFDS (KLON,KLEV), ZMFDQ (KLON,KLEV)

REAL(KIND=JPRB) :: ZALV5, ZOEALFA5, ZTARG5
REAL(KIND=JPRB) :: ZALV , ZOEALFA , ZTARG
REAL(KIND=JPRB) :: ZFACT1, ZFACT2, ZFACT3, ZGDPCP
REAL(KIND=JPRB) :: ZGLMDP, ZGLVDP, ZRCPD, ZGLVDP5 
REAL(KIND=JPRB) :: ZTSPHY, ZIMP
REAL(KIND=JPRB) :: ZZP5, ZGQ5, ZGS5, ZGH5, ZS5, ZQ5, ZDELP5
REAL(KIND=JPRB) :: ZZP , ZGQ , ZGS , ZGH , ZS , ZQ , ZDELP

LOGICAL :: LLTEST

!       ALLOCATABLE ARAYS
REAL(KIND=JPRB),   DIMENSION(:,:), ALLOCATABLE :: ZDTDT5, ZDQDT5, ZGDP5
REAL(KIND=JPRB),   DIMENSION(:,:), ALLOCATABLE :: ZDTDT , ZDQDT , ZGDP
REAL(KIND=JPRB),   DIMENSION(:,:), ALLOCATABLE :: ZB5,    ZR15,   ZR25
REAL(KIND=JPRB),   DIMENSION(:,:), ALLOCATABLE :: ZB ,    ZR1 ,   ZR2
LOGICAL,           DIMENSION(:,:), ALLOCATABLE :: LLCUMBAS

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "cubidiagtl.intfb.h"

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

!*    1.0          INCREMENTATION OF T AND Q TENDENCIES
!                  ------------------------------------

IF (LHOOK) CALL DR_HOOK('CUDTDQN2TL',0,ZHOOK_HANDLE)

ZIMP=1.0_JPRB-RMFSOLTQ2
ZTSPHY=1.0_JPRB/PTSPHY
ZRCPD=1.0_JPRB/RCPD

ALLOCATE(ZDTDT5(KLON,KLEV))
ALLOCATE(ZDTDT (KLON,KLEV))
ALLOCATE(ZDQDT5(KLON,KLEV))
ALLOCATE(ZDQDT (KLON,KLEV))
ALLOCATE(ZGDP5(KLON,KLEV))
ALLOCATE(ZGDP (KLON,KLEV))

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    PENTH5(JL,JK)=0.0_JPRB
    PENTH (JL,JK)=0.0_JPRB
  ENDDO
ENDDO

! zero detrained liquid water if diagnostic cloud scheme to be used

! this means that detrained liquid water will be evaporated in the 
! cloud environment and not fed directly into a cloud liquid water 
! variable

LLTEST = .NOT.LENCLD2.AND..NOT.LDRAIN1D

IF (LLTEST) THEN
  DO JK=1,KLEV
    DO JL=KIDIA,KFDIA
      PLUDE5(JL,JK)=0.0_JPRB
      PLUDE (JL,JK)=0.0_JPRB
    ENDDO
  ENDDO
ENDIF

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    IF(LDCUM(JL)) THEN
      ZDELP5=1.0_JPRB/(PAPH5(JL,JK+1)-PAPH5(JL,JK))
      ZDELP =-(PAPH (JL,JK+1)-PAPH (JL,JK))*ZDELP5**2
      ZGDP5(JL,JK)=RG*ZDELP5
      ZGDP (JL,JK)=RG*ZDELP 
      ZMFUS5(JL,JK)=PMFUS5(JL,JK)
      ZMFUS (JL,JK)=PMFUS (JL,JK)
      ZMFDS5(JL,JK)=PMFDS5(JL,JK)
      ZMFDS (JL,JK)=PMFDS (JL,JK)
      ZMFUQ5(JL,JK)=PMFUQ5(JL,JK)
      ZMFUQ (JL,JK)=PMFUQ (JL,JK)
      ZMFDQ5(JL,JK)=PMFDQ5(JL,JK)
      ZMFDQ (JL,JK)=PMFDQ (JL,JK)
    ENDIF
  ENDDO
ENDDO

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

IF (RMFSOLTQ2 > 0.0_JPRB) THEN

!*    2.0          RECOMPUTE CONVECTIVE FLUXES IF IMPLICIT

    DO JK=KTOPM2,KLEV
      IK=JK-1
!DIR$ IVDEP
!OCL NOVREC
      DO JL=KIDIA,KFDIA
        IF(LDCUM(JL) .AND. JK >= KCTOP(JL)-1) THEN
! Compute interpolating coefficients ZGS and ZGQ for half-level values
          ZFACT2=1.0_JPRB/PQSEN5(JL,JK)
          ZGQ5=(PQENH5(JL,JK)-PQEN5(JL,IK))*ZFACT2
          ZGQ =((PQENH (JL,JK)-PQEN (JL,IK)) - PQSEN (JL,JK)*ZGQ5)*ZFACT2

          ZGH5 =RCPD*PTEN5(JL,JK)+PGEO5(JL,JK)
          ZGH  =RCPD*PTEN (JL,JK)+PGEO (JL,JK)

          ZFACT3=1.0_JPRB/ZGH5
          ZGS5=(RCPD*(PTENH5(JL,JK)-PTEN5(JL,IK))&
            & + PGEOH5(JL,JK)-PGEO5(JL,IK))*ZFACT3
          ZGS =( (RCPD*(PTENH (JL,JK)-PTEN (JL,IK))&
            & + PGEOH (JL,JK)-PGEO (JL,IK)) &
            & - ZGH *ZGS5 )*ZFACT3

! Half-level environmental values for S and Q
          ZS5 =RCPD*(ZIMP*PTEN5(JL,IK)+ZGS5*PTEN5(JL,JK))&
            & +PGEO5(JL,IK)+ZGS5*PGEO5(JL,JK)
          ZS  =RCPD*(ZIMP*PTEN (JL,IK)+ZGS *PTEN5(JL,JK)+ZGS5*PTEN (JL,JK))&
            & +PGEO (JL,IK)+ZGS5*PGEO (JL,JK)+ZGS *PGEO5(JL,JK)

          ZQ5 =ZIMP*PQEN5(JL,IK)+ZGQ5*PQSEN5(JL,JK)
          ZQ  =ZIMP*PQEN (JL,IK)+ZGQ *PQSEN5(JL,JK)+ZGQ5*PQSEN (JL,JK)

          ZMFUS5(JL,JK)=PMFUS5(JL,JK)-PMFU5(JL,JK)*ZS5
          ZMFUS (JL,JK)=PMFUS (JL,JK)-PMFU5(JL,JK)*ZS -PMFU (JL,JK)*ZS5

          ZMFUQ5(JL,JK)=PMFUQ5(JL,JK)-PMFU5(JL,JK)*ZQ5
          ZMFUQ (JL,JK)=PMFUQ (JL,JK)-PMFU5(JL,JK)*ZQ -PMFU (JL,JK)*ZQ5

          IF(LDDRAF(JL) .AND. JK >= KDTOP(JL)) THEN
            ZMFDS5(JL,JK)=PMFDS5(JL,JK)-PMFD5(JL,JK)*ZS5
            ZMFDS (JL,JK)=PMFDS (JL,JK)-PMFD5(JL,JK)*ZS -PMFD (JL,JK)*ZS5

            ZMFDQ5(JL,JK)=PMFDQ5(JL,JK)-PMFD5(JL,JK)*ZQ5
            ZMFDQ (JL,JK)=PMFDQ (JL,JK)-PMFD5(JL,JK)*ZQ -PMFD (JL,JK)*ZQ5
          ENDIF
        ENDIF
      ENDDO
    ENDDO

ENDIF

!*    3.0          COMPUTE TENDENCIES
!                  ------------------

DO JK=KTOPM2,KLEV

  IF(JK < KLEV) THEN
    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL)) THEN
        ZTARG5=PTEN5(JL,JK)
        ZTARG=PTEN(JL,JK)
        ZOEALFA5=0.545_JPRB*(TANH(0.17_JPRB*(ZTARG5-RLPTRC))+1.0_JPRB)
        ZOEALFA =0.545_JPRB*(1.0_JPRB-TANH(0.17_JPRB*(ZTARG5-RLPTRC))**2) &
              & *0.17_JPRB*ZTARG  
        ZALV5=ZOEALFA5*RLVTT+(1.0_JPRB-ZOEALFA5)*RLSTT
        ZALV =ZOEALFA *RLVTT-ZOEALFA *RLSTT

        ZFACT1=1.0_JPRB/(PAPH5(JL,JK+1)-PAPH5(JL,JK))
        ZGDPCP=ZGDP5(JL,JK)*ZRCPD
        ZGLMDP=ZGDPCP*RLMLT
        ZGLVDP5=ZGDPCP*ZALV5
        ZGLVDP =ZGDPCP*ZALV 

        ZDTDT5(JL,JK)=ZGDPCP &
         & *(ZMFUS5(JL,JK+1)-ZMFUS5(JL,JK)&
         & + ZMFDS5(JL,JK+1)-ZMFDS5(JL,JK))&
         & +ZGLMDP*(PLGLAC5(JL,JK)-PDPMEL5(JL,JK))&
         & -ZGLVDP5*(PMFUL5(JL,JK+1)-PMFUL5(JL,JK)&
         & -PLUDE5(JL,JK)&
         & -(PDMFUP5(JL,JK)+PDMFDP5(JL,JK)))  

        ZDTDT(JL,JK)=-(PAPH (JL,JK+1)-PAPH (JL,JK))*ZDTDT5(JL,JK)*ZFACT1 &
         & +ZGDPCP*(ZMFUS (JL,JK+1)-ZMFUS (JL,JK)&
         & +ZMFDS (JL,JK+1)-ZMFDS (JL,JK))&
         & +ZGLMDP*(PLGLAC (JL,JK)-PDPMEL (JL,JK))&
         & -ZGLVDP5*(PMFUL (JL,JK+1)-PMFUL (JL,JK)-PLUDE (JL,JK)&
         & -(PDMFUP (JL,JK)+PDMFDP (JL,JK)))&
         & -ZGLVDP *(PMFUL5(JL,JK+1)-PMFUL5(JL,JK)-PLUDE5(JL,JK)&
         & -(PDMFUP5(JL,JK)+PDMFDP5(JL,JK)))  

        ZDQDT5(JL,JK)=ZGDP5(JL,JK)&
         & *(ZMFUQ5(JL,JK+1)-ZMFUQ5(JL,JK)&
         & + ZMFDQ5(JL,JK+1)-ZMFDQ5(JL,JK)&
         & + PMFUL5(JL,JK+1)-PMFUL5(JL,JK)&
         & - PLUDE5(JL,JK)&
         & -(PDMFUP5(JL,JK)+PDMFDP5(JL,JK)))  

        ZDQDT(JL,JK)=-(PAPH(JL,JK+1)-PAPH(JL,JK))*ZDQDT5(JL,JK)*ZFACT1 &
         & +ZGDP5(JL,JK)*( ZMFUQ(JL,JK+1)-ZMFUQ(JL,JK)&
         & +ZMFDQ(JL,JK+1)-ZMFDQ(JL,JK)&
         & +PMFUL(JL,JK+1)-PMFUL(JL,JK)&
         & -PLUDE(JL,JK)&
         & -(PDMFUP(JL,JK)+PDMFDP(JL,JK)) )  

      ENDIF
    ENDDO

  ELSE
    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL)) THEN
        ZTARG5=PTEN5(JL,JK)
        ZTARG =PTEN (JL,JK)
        ZOEALFA5=0.545_JPRB*(TANH(0.17_JPRB*(ZTARG5-RLPTRC))+1.0_JPRB)
        ZOEALFA =0.545_JPRB*(1.0_JPRB-TANH(0.17_JPRB*(ZTARG5-RLPTRC))**2) &
              & *0.17_JPRB*ZTARG  
        ZALV5=ZOEALFA5*RLVTT+(1.0_JPRB-ZOEALFA5)*RLSTT
        ZALV =ZOEALFA *RLVTT-ZOEALFA *RLSTT

        ZFACT1=1.0_JPRB/(PAPH5(JL,JK+1)-PAPH5(JL,JK))
        ZGDPCP=ZGDP5(JL,JK)*ZRCPD
        ZGLMDP=ZGDPCP*RLMLT
        ZGLVDP5=ZGDPCP*ZALV5
        ZGLVDP =ZGDPCP*ZALV 

        ZDTDT5(JL,JK)=-ZGDPCP*(ZMFUS5(JL,JK)+ZMFDS5(JL,JK))&
            &  -ZGLMDP*PDPMEL5(JL,JK) &
            &  +ZGLVDP5*(PMFUL5(JL,JK)+PDMFUP5(JL,JK)+PDMFDP5(JL,JK))  

        ZDTDT(JL,JK)=-(PAPH (JL,JK+1)-PAPH (JL,JK))*ZDTDT5(JL,JK)*ZFACT1 &
            & -ZGDPCP*(ZMFUS (JL,JK)+ZMFDS (JL,JK))&
            & -ZGLMDP*PDPMEL (JL,JK) &
            & +ZGLVDP5*(PMFUL (JL,JK)+PDMFUP (JL,JK)+PDMFDP (JL,JK)) &
            & +ZGLVDP *(PMFUL5(JL,JK)+PDMFUP5(JL,JK)+PDMFDP5(JL,JK))  

        ZDQDT5(JL,JK)=-ZGDP5(JL,JK)*&
         & (ZMFUQ5(JL,JK)+ZMFDQ5(JL,JK)+&
         & (PMFUL5(JL,JK)+PDMFUP5(JL,JK)+PDMFDP5(JL,JK)))  

        ZDQDT(JL,JK)=-(PAPH(JL,JK+1)-PAPH(JL,JK))*ZDQDT5(JL,JK)*ZFACT1 &
         & -ZGDP5(JL,JK)*(ZMFUQ(JL,JK)+ZMFDQ(JL,JK)+&
         & (PMFUL(JL,JK)+PDMFUP(JL,JK)+PDMFDP(JL,JK)))  

      ENDIF
    ENDDO
  ENDIF

ENDDO

IF (RMFSOLTQ2 == 0.0_JPRB) THEN

!*    3.1          UPDATE TENDENCIES
!                  -----------------

   DO JK=KTOPM2,KLEV
      DO JL=KIDIA,KFDIA
        IF(LDCUM(JL)) THEN
          PTENT5(JL,JK)=PTENT5(JL,JK)+ZDTDT5(JL,JK)
          PTENT (JL,JK)=PTENT (JL,JK)+ZDTDT (JL,JK)
          PTENQ5(JL,JK)=PTENQ5(JL,JK)+ZDQDT5(JL,JK)
          PTENQ (JL,JK)=PTENQ (JL,JK)+ZDQDT (JL,JK)
          PENTH5(JL,JK)=ZDTDT5(JL,JK)*RCPD
          PENTH (JL,JK)=ZDTDT (JL,JK)*RCPD
        ENDIF
      ENDDO
   ENDDO

ELSE
!----------------------------------------------------------------------

!*    3.2          IMPLICIT SOLUTION
!                  -----------------

! Fill bi-diagonal Matrix vectors A=k-1, B=k, C=k+1; reuse ZMFUS=A
! ZDTDT and ZDQDT correspond to the RHS ("constants") of the equation
! The solution is in ZR1 and ZR2

   ALLOCATE(ZB5(KLON,KLEV))
   ALLOCATE(ZB (KLON,KLEV))
   ALLOCATE(ZR15(KLON,KLEV))
   ALLOCATE(ZR1 (KLON,KLEV))
   ALLOCATE(ZR25(KLON,KLEV))
   ALLOCATE(ZR2 (KLON,KLEV))
   ALLOCATE(LLCUMBAS(KLON,KLEV))

   LLCUMBAS(:,:)=.FALSE.
   ZB5(:,:)=1.0_JPRB
   ZB (:,:)=0.0_JPRB
   ZMFUS5(:,:)=0.0_JPRB
   ZMFUS (:,:)=0.0_JPRB

! Fill vectors A, B and RHS

   DO JK=KTOPM2,KLEV
      IK=JK+1
      DO JL=KIDIA,KFDIA
        LLCUMBAS(JL,JK)=LDCUM(JL) .AND. JK >= KCTOP(JL)-1
        IF(LLCUMBAS(JL,JK)) THEN
          ZZP5=RMFSOLTQ2*ZGDP5(JL,JK)*PTSPHY
          ZZP =RMFSOLTQ2*ZGDP (JL,JK)*PTSPHY
          ZMFUS5(JL,JK)=-ZZP5*(PMFU5(JL,JK)+PMFD5(JL,JK))
          ZMFUS (JL,JK)=-ZZP5*(PMFU (JL,JK)+PMFD (JL,JK))&
                     &  -ZZP *(PMFU5(JL,JK)+PMFD5(JL,JK))
          ZDTDT5(JL,JK) = ZDTDT5(JL,JK)*PTSPHY + PTEN5(JL,JK)
          ZDTDT (JL,JK) = ZDTDT (JL,JK)*PTSPHY + PTEN (JL,JK)
          ZDQDT5(JL,JK) = ZDQDT5(JL,JK)*PTSPHY + PQEN5(JL,JK)
          ZDQDT (JL,JK) = ZDQDT (JL,JK)*PTSPHY + PQEN (JL,JK)
          IF(JK < KLEV) THEN
            ZB5(JL,JK)=1.0_JPRB+ZZP5*(PMFU5(JL,IK)+PMFD5(JL,IK))
            ZB (JL,JK)=ZZP5*(PMFU (JL,IK)+PMFD (JL,IK))&
                    & +ZZP *(PMFU5(JL,IK)+PMFD5(JL,IK))
          ELSE
            ZB5(JL,JK)=1.0_JPRB
            ZB (JL,JK)=0.0_JPRB
          ENDIF
        ENDIF
      ENDDO
   ENDDO

   CALL CUBIDIAGTL &
      &( KIDIA,    KFDIA,   KLON,   KLEV, &
      &  KCTOP,    LLCUMBAS, &
      &  ZMFUS5,   ZB5,    ZDTDT5,  ZR15, &
      &  ZMFUS ,   ZB ,    ZDTDT ,  ZR1 )

   CALL CUBIDIAGTL &
      &( KIDIA,    KFDIA,   KLON,   KLEV, &
      &  KCTOP,    LLCUMBAS, &
      &  ZMFUS5,   ZB5,    ZDQDT5,  ZR25, &
      &  ZMFUS ,   ZB ,    ZDQDT ,  ZR2 )

! Compute final tendencies

   DO JK=KTOPM2,KLEV
      DO JL=KIDIA,KFDIA
        IF(LLCUMBAS(JL,JK)) THEN
          PTENT5(JL,JK)=PTENT5(JL,JK)+(ZR15(JL,JK)-PTEN5(JL,JK))*ZTSPHY
          PTENT (JL,JK)=PTENT (JL,JK)+(ZR1 (JL,JK)-PTEN (JL,JK))*ZTSPHY
          PTENQ5(JL,JK)=PTENQ5(JL,JK)+(ZR25(JL,JK)-PQEN5(JL,JK))*ZTSPHY
          PTENQ (JL,JK)=PTENQ (JL,JK)+(ZR2 (JL,JK)-PQEN (JL,JK))*ZTSPHY
          PENTH5(JL,JK)=(ZR15(JL,JK)-PTEN5(JL,JK))*ZTSPHY
          PENTH (JL,JK)=(ZR1 (JL,JK)-PTEN (JL,JK))*ZTSPHY
        ENDIF
      ENDDO
   ENDDO

   DEALLOCATE(LLCUMBAS)
   DEALLOCATE(ZR25)
   DEALLOCATE(ZR2)
   DEALLOCATE(ZR15)
   DEALLOCATE(ZR1)
   DEALLOCATE(ZB5)
   DEALLOCATE(ZB)

!----------------------------------------------------------------------
ENDIF

DEALLOCATE(ZGDP5)
DEALLOCATE(ZGDP)
DEALLOCATE(ZDQDT5)
DEALLOCATE(ZDQDT)
DEALLOCATE(ZDTDT5)
DEALLOCATE(ZDTDT)

IF (LHOOK) CALL DR_HOOK('CUDTDQN2TL',1,ZHOOK_HANDLE)
END SUBROUTINE CUDTDQN2TL
