SUBROUTINE CUDTDQNTL &
 & (  KIDIA,    KFDIA,    KLON,    KLEV,&
 & KTOPM2,   KTYPE,    KCBOT,    KCTOP,    KDTOP,&
 & LDCUM,    LDDRAF,   PTSPHY,&
 & PAPH5,    PGEOH5,   PGEO5,&
 & PTEN5,    PTENH5,   PQEN5,    PQENH5,   PQSEN5,&
 & PLGLAC5,  PLUDE5,&
 & PMFUS5,   PMFDS5,   PMFUQ5,   PMFDQ5,&
 & PMFUL5,   PDMFUP5,  PDPMEL5,&
 & PTENT5,   PTENQ5,   PENTH5,&
 & PMFU5,    PMFD5,&
 & PDMFEN5,  PDMFDE5,&
 & PGEOH,    PGEO,&
 & PTEN,     PTENH,    PQEN,     PQENH,    PQSEN,&
 & PTENT,    PTENQ   )  

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

!          J.F. MAHFOUF      E.C.M.W.F      05/12/96
!          P.LOPEZ+P.BECHTOLD E.C.M.W.F     11/12/05 (Implicit Solver)

!          Adapted from

!          M.TIEDTKE         E.C.M.W.F.     7/86 MODIF. 12/89

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

!          *CUDTDQNTL* 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
!    *KTYPE*        TYPE OF CONVECTION
!                       1 = PENETRATIVE CONVECTION
!                       2 = SHALLOW CONVECTION
!                       3 = MIDLEVEL CONVECTION
!    *KCBOT*        CLOUD BASE LEVEL
!    *KCTOP*        CLOUD TOP LEVEL
!    *KDTOP*        TOP LEVEL OF DOWNDRAFTS

!    INPUT PARAMETERS (LOGICAL): 

!    *LDCUM*        FLAG: .TRUE. FOR CONVECTIVE POINTS 
!    *LDDRAF*       FLAG: .TRUE. FOR DOWNDRAUGHTS

!    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*        PROV. ENV. SPEC.HUMIDITY FULL LEVELS (T+1)  (Trajectory) 
!    *PTENH5*       PROV. ENV. TEMPERATURE HALF LEVELS (T+1)    (Trajectory)  
!    *PQENH5*       PROV. ENV. SPEC.HUMIDITY HALF LEVELS (T+1)  (Trajectory)  
!    *PQSEN5*       ENV. SATURAT SPEC.HUMIDITY FULL LEVELS (T+1)(Trajectory)  
!    *PLGLAC5*      FLUX OF FROZEN CLOUDWATER IN UPDRAFTS       (Trajectory) 
!    *PLUDE5*       DETRAINED LIQUID WATER                      (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.                  (Trajectory) 
!    *PDPMEL5*      CHANGE IN PRECIP.-FLUXES DUE TO MELTING     (Trajectory) 

!    *PGEOH*        GEOPOTENTIAL ON HALF LEVELS                   M2/S2
!    *PGEO*         GEOPOTENTIAL ON FULL LEVELS                   M2/S2
!    *PTEN*         PROVISIONAL ENVIRONMENT TEMPERATURE             K
!    *PQEN*         PROV. ENV. SPEC.HUMIDITY FULL LEVELS          KG/KG
!    *PTENH*        PROV. ENV. TEMPERATURE HALF LEVELS              K
!    *PQENH*        PROV. ENV. SPEC.HUMIDITY HALF LEVELS          KG/KG  
!    *PQSEN*        ENV. SATURAT SPEC.HUMIDITY FULL LEVELS        KG/KG

!    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)

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

!               MODIFICATIONS
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

!               96-09-20        : changed so can be used with diagnostic 
!                                         cloud scheme      D.GREGORY

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

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

USE YOMCST   , ONLY : RG       ,RCPD     ,RLVTT    ,RLSTT    ,RLMLT    ,RTT
USE YOETHF   , ONLY : R2ES     ,R3LES    ,R3IES    ,R4LES    ,&
 & R4IES    ,R5LES    ,R5IES    ,R5ALVCP  ,R5ALSCP  ,&
 & RALVDCP  ,RALSDCP  ,RTWAT    ,RTICE    ,RTICECU  ,&
 & RTWAT_RTICE_R      ,RTWAT_RTICECU_R  
USE YOECUMF  , ONLY : RMFSOLTQ
USE YOEPHY   , ONLY : LEPCLD
USE YOEPHLI  , ONLY : LPHYLIN  ,RLPTRC
USE YOPHNC   , ONLY : LENCLD2

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)    :: KTYPE(KLON) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCBOT(KLON) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KCTOP(KLON) 
INTEGER(KIND=JPIM),INTENT(IN)    :: KDTOP(KLON) 
LOGICAL           ,INTENT(INOUT) :: 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)    :: PGEOH5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEO5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENH5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQEN5(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)    :: 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)    :: 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)    :: PMFU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFD5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDMFEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDMFDE5(KLON,KLEV) 
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)    :: PGEOH(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGEO(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENT(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENQ(KLON,KLEV) 

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

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

REAL(KIND=JPRB) :: ZALV5, ZOEALFA5, ZTARG5, ZZP5, ZORCPD5,&
                  &ZGS5, ZGQ5, ZGS, ZGQ, ZGH5, ZGH, ZS5, ZQ5, ZS, ZQ, ZFAC5,&
                  &ZTSPHY, ZIMP

LOGICAL :: LLTEST

!       ALLOCATABLE ARAYS
REAL(KIND=JPRB),   DIMENSION(:,:), ALLOCATABLE :: ZDP5,  ZDTDT5,   ZDQDT5,&
                                                & ZA5,   ZB5,  ZR15,  ZR25
REAL(KIND=JPRB),   DIMENSION(:,:), ALLOCATABLE ::        ZDTDT,    ZDQDT, &
                                                & ZA,    ZB,   ZR1,  ZR2

LOGICAL,DIMENSION(:,:),   ALLOCATABLE ::  LLCUMBAS

REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "cubidiagtl.intfb.h"
#include "fcttre.h"

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

!         MASS-FLUX APPROACH SWITCHED ON FOR DEEP CONVECTION ONLY
!         -------------------------------------------------------

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

do jl=kidia,kfdia
  if (ktype(jl) /= 1) ldcum(jl)=.false.
enddo

!*    1.0          SETUP AND INITIALIZATIONS
!                  -------------------------

ZIMP=1.0_JPRB-RMFSOLTQ
ZTSPHY=1.0_JPRB/PTSPHY
ZORCPD5=1.0_JPRB/RCPD

! Trajectory arrays

ALLOCATE(ZDTDT5(KLON,KLEV))
ALLOCATE(ZDQDT5(KLON,KLEV))
ALLOCATE(ZDP5(KLON,KLEV))

! Tangent-linear perturbation arrays

ALLOCATE(ZDTDT(KLON,KLEV))
ALLOCATE(ZDQDT(KLON,KLEV))

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    PENTH5(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.LEPCLD.AND..NOT.LENCLD2).OR.(LPHYLIN.AND..NOT.LENCLD2)

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

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    IF(LDCUM(JL)) THEN
      ZDP5(JL,JK)=RG/(PAPH5(JL,JK+1)-PAPH5(JL,JK))
      ZMFUS5(JL,JK)=PMFUS5(JL,JK)
      ZMFDS5(JL,JK)=PMFDS5(JL,JK)
      ZMFUQ5(JL,JK)=PMFUQ5(JL,JK)
      ZMFDQ5(JL,JK)=PMFDQ5(JL,JK)
      ZMFUS (JL,JK)=0.0_JPRB
      ZMFUQ (JL,JK)=0.0_JPRB
      ZMFDS (JL,JK)=0.0_JPRB
      ZMFDQ (JL,JK)=0.0_JPRB
    ENDIF
  ENDDO
ENDDO

IF ( RMFSOLTQ>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
          ZGQ5= (PQENH5(JL,JK)-PQEN5(JL,IK))/PQSEN5(JL,JK)
          ZFAC5=1.0_JPRB/(PQSEN5(JL,JK)*PQSEN5(JL,JK))
          ZGQ =((PQENH (JL,JK)-PQEN (JL,IK))*PQSEN5(JL,JK)-&
               &(PQENH5(JL,JK)-PQEN5(JL,IK))*PQSEN (JL,JK))*ZFAC5
          ZGH5=RCPD*PTEN5(JL,JK)+PGEO5(JL,JK)
          ZGH =RCPD*PTEN (JL,JK)+PGEO (JL,JK)
          ZGS5= (RCPD*(PTENH5(JL,JK)-PTEN5(JL,IK))+PGEOH5(JL,JK)-PGEO5(JL,IK))/ZGH5
          ZFAC5=1.0_JPRB/(ZGH5*ZGH5)
          ZGS =((RCPD*(PTENH (JL,JK)-PTEN (JL,IK))+PGEOH (JL,JK)-PGEO (JL,IK))*ZGH5-& 
               &(RCPD*(PTENH5(JL,JK)-PTEN5(JL,IK))+PGEOH5(JL,JK)-PGEO5(JL,IK))*ZGH )&
               &*ZFAC5

         !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)+ZGS *PGEO5(JL,JK)+ZGS5*PGEO (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)=-PMFU5(JL,JK)*ZS
          ZMFUQ5(JL,JK)=PMFUQ5(JL,JK)-PMFU5(JL,JK)*ZQ5
          ZMFUQ (JL,JK)=-PMFU5(JL,JK)*ZQ
          IF(LDDRAF(JL).AND.JK >= KDTOP(JL)) THEN
            ZMFDS5(JL,JK)=PMFDS5(JL,JK)-PMFD5(JL,JK)*ZS5
            ZMFDS (JL,JK)=-PMFD5(JL,JK)*ZS
            ZMFDQ5(JL,JK)=PMFDQ5(JL,JK)-PMFD5(JL,JK)*ZQ5
            ZMFDQ (JL,JK)=-PMFD5(JL,JK)*ZQ
          ENDIF
        ENDIF
      ENDDO
    ENDDO

ELSEIF (RMFSOLTQ==0.0_JPRB ) THEN

!*    2.1          COMPUTE PERTURBED FLUXES FOR EXPLICIT
!                  -------------------------------------

    DO JK=KTOPM2,KLEV
      DO JL=KIDIA,KFDIA
        IF(LDCUM(JL).AND.JK>=KCTOP(JL)-1) THEN
          ZMFUS (JL,JK)=-PMFU5(JL,JK)*(RCPD*PTENH(JL,JK)+PGEOH(JL,JK))
          ZMFUQ (JL,JK)=-PMFU5(JL,JK)* PQENH(JL,JK)
          ZMFDS (JL,JK)=-PMFD5(JL,JK)*(RCPD*PTENH(JL,JK)+PGEOH(JL,JK))
          ZMFDQ (JL,JK)=-PMFD5(JL,JK)* PQENH(JL,JK)
        ENDIF
      ENDDO
    ENDDO
    
    DO JK=KTOPM2,KLEV
    !DIR$ IVDEP
    !OCL NOVREC
      DO JL=KIDIA,KFDIA
        IF(LDCUM(JL).AND.JK > KCBOT(JL)) THEN
          IKB=KCBOT(JL)
          ZZP5=((PAPH5(JL,KLEV+1)-PAPH5(JL,JK))/(PAPH5(JL,KLEV+1)-PAPH5(JL,IKB)))
          IF(KTYPE(JL) == 3) THEN
            ZZP5=ZZP5*ZZP5
          ENDIF
          ZMFUS (JL,JK)=ZMFUS (JL,IKB)*ZZP5
          ZMFUQ (JL,JK)=ZMFUQ (JL,IKB)*ZZP5
          ZMFDS (JL,JK)=ZMFDS (JL,IKB)*ZZP5
          ZMFDQ (JL,JK)=ZMFDQ (JL,IKB)*ZZP5
        ENDIF
      ENDDO
    ENDDO

ENDIF

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

DO JK=KTOPM2,KLEV

  IF(JK < KLEV) THEN
    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL)) THEN
        IF (LPHYLIN) THEN
          ZTARG5=PTEN5(JL,JK)
          ZOEALFA5=0.545_JPRB*(TANH(0.17_JPRB*(ZTARG5-RLPTRC))+1.0_JPRB)
          ZALV5=ZOEALFA5*RLVTT+(1.0_JPRB-ZOEALFA5)*RLSTT
        ELSE
          ZALV5=FOELHM(PTEN5(JL,JK))
        ENDIF
        ZDTDT5(JL,JK)=ZDP5(JL,JK)*ZORCPD5*&
         & (ZMFUS5(JL,JK+1)-ZMFUS5(JL,JK)+&
         & ZMFDS5(JL,JK+1)-ZMFDS5(JL,JK)&
         & +RLMLT*PLGLAC5(JL,JK)&
         & -RLMLT*PDPMEL5(JL,JK)&
         & -ZALV5*(PMFUL5(JL,JK+1)-PMFUL5(JL,JK)-&
         & PLUDE5(JL,JK)-PDMFUP5(JL,JK)))  

        ZDQDT5(JL,JK)=ZDP5(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))  
       ENDIF
     ENDDO

   !      computation of the perturbed tendencies
   !      dPsi/dt=M*dPsi/dp=d/dp(M*Psi)-dM/dp*Psi
   !      ---------------------------------------
   
     DO JL=KIDIA,KFDIA
       IF(LDCUM(JL)) THEN
         IKB=KCBOT(JL)-1
         IF(JK <= IKB) THEN
           ZDTDT(JL,JK) =ZDP5(JL,JK)*ZORCPD5*&
            & (ZMFUS (JL,JK+1)-ZMFUS (JL,JK)+&
            &  ZMFDS (JL,JK+1)-ZMFDS (JL,JK)-&
            & (PDMFEN5(JL,JK)+PDMFDE5(JL,JK))*&
            & (RCPD*PTEN(JL,JK)+PGEO(JL,JK)))    
           ZDQDT(JL,JK) =ZDP5(JL,JK)*&
            & (ZMFUQ (JL,JK+1)-ZMFUQ (JL,JK)+&
            &  ZMFDQ (JL,JK+1)-ZMFDQ (JL,JK)-&
            & (PDMFEN5(JL,JK)+PDMFDE5(JL,JK))*PQEN(JL,JK))  
         ELSEIF(JK>IKB) THEN
           ZZP5=((PAPH5(JL,KLEV+1)-PAPH5(JL,JK))/&
            & (PAPH5(JL,KLEV+1)-PAPH5(JL,IKB)))  
           ZDTDT(JL,JK) =ZDTDT(JL,IKB)*ZZP5
           ZDQDT(JL,JK) =ZDQDT(JL,IKB)*ZZP5
         ENDIF
       ENDIF
     ENDDO

  ELSE
     DO JL=KIDIA,KFDIA
       IF(LDCUM(JL)) THEN
         IF (LPHYLIN) THEN
           ZTARG5=PTEN5(JL,JK)
           ZOEALFA5=0.545_JPRB*(TANH(0.17_JPRB*(ZTARG5-RLPTRC))+1.0_JPRB)
           ZALV5=ZOEALFA5*RLVTT+(1.0_JPRB-ZOEALFA5)*RLSTT
         ELSE
           ZALV5=FOELHM(PTEN5(JL,JK))
         ENDIF
         ZDTDT5(JL,JK)=-ZDP5(JL,JK)*ZORCPD5*&
          & (ZMFUS5(JL,JK)+ZMFDS5(JL,JK)+RLMLT*PDPMEL5(JL,JK)-ZALV5*&
          & (PMFUL5(JL,JK)+PDMFUP5(JL,JK)))  

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

         IKB=KCBOT(JL)-1
         ZZP5=((PAPH5(JL,KLEV+1)-PAPH5(JL,JK))/&
          & (PAPH5(JL,KLEV+1)-PAPH5(JL,IKB)))  
         ZDTDT(JL,JK) =ZDTDT(JL,IKB)*ZZP5
         ZDQDT(JL,JK) =ZDQDT(JL,IKB)*ZZP5
       ENDIF
     ENDDO
  ENDIF

ENDDO

IF ( RMFSOLTQ==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;
     ! ZDTDT and ZDQDT correspond to the RHS ("constants") of the equation
     ! The solution is in ZR1 and ZR2

      ALLOCATE(ZA5(KLON,KLEV))
      ALLOCATE(ZB5(KLON,KLEV))
      ALLOCATE(ZA (KLON,KLEV))
      ALLOCATE(ZB (KLON,KLEV))
      ALLOCATE(ZR15(KLON,KLEV))
      ALLOCATE(ZR25(KLON,KLEV))
      ALLOCATE(ZR1 (KLON,KLEV))
      ALLOCATE(ZR2 (KLON,KLEV))
      ALLOCATE(LLCUMBAS(KLON,KLEV))
      LLCUMBAS(:,:)=.FALSE.
      ZB5(:,:)=1.0_JPRB
      ZA5(:,:)=0.0_JPRB
      ZB (:,:)=0.0_JPRB
      ZA (:,:)=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=RMFSOLTQ*ZDP5(JL,JK)*PTSPHY
             ZA5(JL,JK)   =-ZZP5*(PMFU5(JL,JK)+PMFD5(JL,JK))
             ZA (JL,JK)   =0.0_JPRB
             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)
           ! ZDTDT5(JL,JK)= (ZDTDT5(JL,JK)+PTENT5(JL,JK))*PTSPHY+PTEN5(JL,JK)
           ! ZDQDT5(JL,JK)= (ZDQDT5(JL,JK)+PTENQ5(JL,JK))*PTSPHY+PQEN5(JL,JK)
           ! ZDTDT (JL,JK)= (ZDTDT (JL,JK)+PTENT (JL,JK))*PTSPHY+PTEN (JL,JK)
           ! ZDQDT (JL,JK)= (ZDQDT (JL,JK)+PTENQ (JL,JK))*PTSPHY+PQEN (JL,JK)
             IF(JK<KLEV) THEN
               ZB5(JL,JK)=1.0_JPRB+ZZP5*(PMFU5(JL,IK)+PMFD5(JL,IK))
             ELSE
               ZB5(JL,JK)=1.0_JPRB
             ENDIF
             ZB(JL,JK)   =0.0_JPRB
           ENDIF
         ENDDO
      ENDDO

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

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

     ! Compute 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
           ! PTENT5(JL,JK)=(ZR15(JL,JK)-PTEN5(JL,JK))*ZTSPHY
           ! PTENQ5(JL,JK)=(ZR25(JL,JK)-PQEN5(JL,JK))*ZTSPHY
           ! PTENT (JL,JK)=(ZR1 (JL,JK)-PTEN (JL,JK))*ZTSPHY
           ! 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(ZR2)
      DEALLOCATE(ZR1)
      DEALLOCATE(ZR25)
      DEALLOCATE(ZR15)
      DEALLOCATE(ZB)
      DEALLOCATE(ZA)
      DEALLOCATE(ZB5)
      DEALLOCATE(ZA5)

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

DEALLOCATE(ZDQDT)
DEALLOCATE(ZDTDT)

DEALLOCATE(ZDP5)
DEALLOCATE(ZDQDT5)
DEALLOCATE(ZDTDT5)

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