SUBROUTINE CUDUDV2AD &
 & ( KIDIA,    KFDIA,    KLON,     KLEV,&
 & KTOPM2,   KTYPE,    KCBOT,    KCTOP,    LDCUM,    PTSPHY,&
 & PAPH5,    PUEN5,    PVEN5,    PMFU5,    PMFD5,&
 & PUU5,     PUD5,     PVU5,     PVD5,&
 & PTENU5,   PTENV5,&
 & PAPH,     PUEN,     PVEN,     PMFU,     PMFD,&
 & PUU,      PUD,      PVU,      PVD,&
 & PTENU,    PTENV )  

!**** *CUDUDV2AD* - UPDATES U AND V TENDENCIES,
!                  DOES GLOBAL DIAGNOSTIC OF DISSIPATION
!                  (ADJOINT VERSION)

!          J.F. MAHFOUF      E.C.M.W.F.     06/12/96

!          Adapted from 

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

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

!          *CUDUDV2AD* IS CALLED FROM *CUMASTRN2AD*

!     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

!    INPUT PARAMETERS (LOGICAL):

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

!    INPUT PARAMETERS (REAL):

!    *PTSPHY*       TIME STEP FOR THE PHYSICS                      S
!    *PAPH5*        PROVISIONAL PRESSURE ON HALF LEVELS        (Trajectory)  
!    *PUEN5*        PROVISIONAL ENVIRONMENT U-VELOCITY (T+1)   (Trajectory)  
!    *PVEN5*        PROVISIONAL ENVIRONMENT V-VELOCITY (T+1)   (Trajectory)  
!    *PMFU5*        MASSFLUX UPDRAFTS                          (Trajectory)  
!    *PMFD5*        MASSFLUX DOWNDRAFTS                        (Trajectory)  
!    *PUU5*         U-VELOCITY IN UPDRAFTS                     (Trajectory)  
!    *PUD5*         U-VELOCITY IN DOWNDRAFTS                   (Trajectory)  
!    *PVU5*         V-VELOCITY IN UPDRAFTS                     (Trajectory)  
!    *PVD5*         V-VELOCITY IN DOWNDRAFTS                   (Trajectory)  

!    *PAPH*         PROVISIONAL PRESSURE ON HALF LEVELS            PA
!    *PUEN*         PROVISIONAL ENVIRONMENT U-VELOCITY (T+1)       M/S
!    *PVEN*         PROVISIONAL ENVIRONMENT V-VELOCITY (T+1)       M/S
!    *PMFU*         MASSFLUX UPDRAFTS                             KG/(M2*S)
!    *PMFD*         MASSFLUX DOWNDRAFTS                           KG/(M2*S)
!    *PUU*          U-VELOCITY IN UPDRAFTS                         M/S
!    *PUD*          U-VELOCITY IN DOWNDRAFTS                       M/S
!    *PVU*          V-VELOCITY IN UPDRAFTS                         M/S
!    *PVD*          V-VELOCITY IN DOWNDRAFTS                       M/S

!    UPDATED PARAMETERS (REAL):

!    *PTENU5*       TENDENCY OF U-COMP. OF WIND                (Trajectory)  
!    *PTENV5*       TENDENCY OF V-COMP. OF WIND                (Trajectory)  

!    *PTENU*        TENDENCY OF U-COMP. OF WIND                    M/S2
!    *PTENV*        TENDENCY OF V-COMP. OF WIND                    M/S2

!          MODIFICATIONS
!          -------------
!           1992-09-21 : Update to Cy44      J.-J. MORCRETTE
!           2002-08-06 : Full adjoint version   Ph. LOPEZ 
!           2003-10-01 : CY28 Cleaning  M.Hamrud      
!           2007-10-22 : Added implicit version  Ph. LOPEZ 
!----------------------------------------------------------------------

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

USE YOMCST   , ONLY : RG
USE YOECUMF2 , ONLY : RMFSOLUV2

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) 
LOGICAL           ,INTENT(IN)    :: LDCUM(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTSPHY
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPH5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVEN5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PMFD5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUD5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVD5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENV5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAPH(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUEN(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVEN(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PMFD(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PUD(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PVD(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTENV(KLON,KLEV) 

REAL(KIND=JPRB) :: ZUEN5(KLON,KLEV),   ZVEN5(KLON,KLEV),&
                 & ZMFUU5(KLON,KLEV),  ZMFDU5(KLON,KLEV),&
                 & ZMFUV5(KLON,KLEV),  ZMFDV5(KLON,KLEV)  
REAL(KIND=JPRB) :: ZMFUU15(KLON,KLEV)

REAL(KIND=JPRB) :: ZUEN(KLON,KLEV),   ZVEN(KLON,KLEV),&
                 & ZMFUU(KLON,KLEV),  ZMFDU(KLON,KLEV),&
                 & ZMFUV(KLON,KLEV),  ZMFDV(KLON,KLEV)  

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

REAL(KIND=JPRB) :: ZZP5, ZZP15, ZZP25, ZZP35
REAL(KIND=JPRB) :: ZP15, ZP25, ZDELP5
REAL(KIND=JPRB) :: ZZP, ZP1, ZP2, ZDELP
REAL(KIND=JPRB) :: ZIMP, ZTSPHY

! ALLOCATABLE ARAYS
REAL(KIND=JPRB),   DIMENSION(:,:), ALLOCATABLE :: ZDUDT5, ZDVDT5, ZGDP5
REAL(KIND=JPRB),   DIMENSION(:,:), ALLOCATABLE :: ZDUDT , ZDVDT , 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 "cubidiag.intfb.h"
#include "cubidiagad.intfb.h"
!----------------------------------------------------------------------

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

ZIMP=1.0_JPRB-RMFSOLUV2
ZTSPHY=1.0_JPRB/PTSPHY

ALLOCATE(ZDUDT5(KLON,KLEV))
ALLOCATE(ZDVDT5(KLON,KLEV))
ALLOCATE(ZGDP5(KLON,KLEV))

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    IF (LDCUM(JL)) THEN
      ZUEN5(JL,JK)=PUEN5(JL,JK)
      ZVEN5(JL,JK)=PVEN5(JL,JK)
      ZDELP5=1.0_JPRB/(PAPH5(JL,JK+1)-PAPH5(JL,JK))
      ZGDP5(JL,JK)=RG*ZDELP5
    ENDIF
  ENDDO
ENDDO

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

!*    1.0          CALCULATE FLUXES AND UPDATE U AND V TENDENCIES

DO JK=KTOPM2,KLEV
  IK=JK-1
  DO JL=KIDIA,KFDIA
    IF(LDCUM(JL)) THEN
      ZMFUU5(JL,JK)=PMFU5(JL,JK)*(PUU5(JL,JK)-ZIMP*ZUEN5(JL,IK))
      ZMFUV5(JL,JK)=PMFU5(JL,JK)*(PVU5(JL,JK)-ZIMP*ZVEN5(JL,IK))
      ZMFDU5(JL,JK)=PMFD5(JL,JK)*(PUD5(JL,JK)-ZIMP*ZUEN5(JL,IK))
      ZMFDV5(JL,JK)=PMFD5(JL,JK)*(PVD5(JL,JK)-ZIMP*ZVEN5(JL,IK))
    ENDIF
  ENDDO
ENDDO

! linear fluxes below cloud
IF (RMFSOLUV2 == 0.0_JPRB) THEN
  DO JK=KTOPM2,KLEV
!DIR$ IVDEP
!OCL NOVREC
    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL).AND.JK > KCBOT(JL)) THEN
        IKB=KCBOT(JL)
  
        ZP15=PAPH5(JL,KLEV+1)-PAPH5(JL,JK)
        ZP25=PAPH5(JL,KLEV+1)-PAPH5(JL,IKB)
        ZZP5=ZP15/ZP25
  
        IF(KTYPE(JL) == 3) THEN
          ZZP5=ZZP5*ZZP5
        ENDIF
  
        ZMFUU5(JL,JK)=ZMFUU5(JL,IKB)*ZZP5
        ZMFUV5(JL,JK)=ZMFUV5(JL,IKB)*ZZP5
        ZMFDU5(JL,JK)=ZMFDU5(JL,IKB)*ZZP5
        ZMFDV5(JL,JK)=ZMFDV5(JL,IKB)*ZZP5
      ENDIF
    ENDDO
  ENDDO
ENDIF

!*    1.2          COMPUTE TENDENCIES
!                  ------------------

DO JK=KTOPM2,KLEV

  IF(JK < KLEV) THEN
    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL)) THEN
        ZDUDT5(JL,JK)=ZGDP5(JL,JK)*&
         & (ZMFUU5(JL,JK+1)-ZMFUU5(JL,JK)+&
         &  ZMFDU5(JL,JK+1)-ZMFDU5(JL,JK))  
        ZDVDT5(JL,JK)=ZGDP5(JL,JK)*&
         & (ZMFUV5(JL,JK+1)-ZMFUV5(JL,JK)+&
         &  ZMFDV5(JL,JK+1)-ZMFDV5(JL,JK))  
      ENDIF
    ENDDO

  ELSE
    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL)) THEN
        ZDUDT5(JL,JK)=-ZGDP5(JL,JK)*(ZMFUU5(JL,JK)+ZMFDU5(JL,JK))
        ZDVDT5(JL,JK)=-ZGDP5(JL,JK)*(ZMFUV5(JL,JK)+ZMFDV5(JL,JK))
      ENDIF
    ENDDO
  ENDIF

ENDDO

IF (RMFSOLUV2 == 0.0_JPRB) THEN

!*    1.3          UPDATE TENDENCIES
!                  -----------------

  DO JK=KTOPM2,KLEV
    DO JL=KIDIA,KFDIA
      IF (LDCUM(JL)) THEN
        PTENU5(JL,JK)=PTENU5(JL,JK)+ZDUDT5(JL,JK)
        PTENV5(JL,JK)=PTENV5(JL,JK)+ZDVDT5(JL,JK)
      ENDIF
    ENDDO
  ENDDO

ELSE
!----------------------------------------------------------------------
   
!*      1.6          IMPLICIT SOLUTION
!                    -----------------

! Fill bi-diagonal Matrix vectors A=k-1, B=k;
! reuse ZMFUU=A and ZB=B; 
! ZDUDT and ZDVDT correspond to the RHS ("constants") of the equation
! The solution is in ZR1 and ZR2
  
   ALLOCATE(ZB5(KLON,KLEV))
   ALLOCATE(ZR15(KLON,KLEV))
   ALLOCATE(ZR25(KLON,KLEV))
   ALLOCATE(LLCUMBAS(KLON,KLEV))

   LLCUMBAS(:,:)=.FALSE.
   ZB5(:,:)=1.0_JPRB
   ZMFUU15(:,:)=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=RMFSOLUV2*PTSPHY*ZGDP5(JL,JK)
          ZMFUU15(JL,JK)=-ZZP5*(PMFU5(JL,JK)+PMFD5(JL,JK))
          ZDUDT5(JL,JK) = ZDUDT5(JL,JK)*PTSPHY + ZUEN5(JL,JK)
          ZDVDT5(JL,JK) = ZDVDT5(JL,JK)*PTSPHY + ZVEN5(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
        ENDIF
      ENDDO
   ENDDO
  
   CALL CUBIDIAG &
      &( KIDIA, KFDIA, KLON, KLEV, &
      &  KCTOP, LLCUMBAS, &
      &  ZMFUU15,  ZB5,  ZDUDT5, ZR15 )
  
   CALL CUBIDIAG &
      &( KIDIA, KFDIA, KLON, KLEV, &
      &  KCTOP, LLCUMBAS, &
      &  ZMFUU15,  ZB5,  ZDVDT5, ZR25 )
  
! Compute tendencies
  
   DO JK=KTOPM2,KLEV
      DO JL=KIDIA,KFDIA
        IF (LLCUMBAS(JL,JK)) THEN
          PTENU5(JL,JK)=PTENU5(JL,JK)+(ZR15(JL,JK)-ZUEN5(JL,JK))*ZTSPHY
          PTENV5(JL,JK)=PTENV5(JL,JK)+(ZR25(JL,JK)-ZVEN5(JL,JK))*ZTSPHY
        ENDIF
      ENDDO
   ENDDO

ENDIF

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

!     *****               ADJOINT CALCULATIONS             ******

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

!*         0.     INITIALIZATIONS
!                 ---------------

ZMFUU(:,:)=0.0_JPRB
ZMFUV(:,:)=0.0_JPRB
ZMFDU(:,:)=0.0_JPRB
ZMFDV(:,:)=0.0_JPRB
ZUEN (:,:)=0.0_JPRB
ZVEN (:,:)=0.0_JPRB

ALLOCATE(ZDUDT(KLON,KLEV))
ALLOCATE(ZDVDT(KLON,KLEV))
ALLOCATE(ZGDP(KLON,KLEV))

ZDUDT(:,:)=0.0_JPRB
ZDVDT(:,:)=0.0_JPRB
ZGDP (:,:)=0.0_JPRB

IF (RMFSOLUV2 == 0.0_JPRB) THEN

!*    1.3          UPDATE TENDENCIES
!                  -----------------

  DO JK=KLEV,KTOPM2,-1
    DO JL=KIDIA,KFDIA
      IF (LDCUM(JL)) THEN
        ZDVDT(JL,JK)=ZDVDT(JL,JK)+PTENV(JL,JK)
        ZDUDT(JL,JK)=ZDUDT(JL,JK)+PTENU(JL,JK)
      ENDIF
    ENDDO
  ENDDO

ELSE
!----------------------------------------------------------------------
   
!*      1.6          IMPLICIT SOLUTION
!                    -----------------

! Fill bi-diagonal Matrix vectors A=k-1, B=k;
! reuse ZMFUU=A and ZB=B; 
! ZDUDT and ZDVDT correspond to the RHS ("constants") of the equation
! The solution is in ZR1 and ZR2
  
   ALLOCATE(ZB(KLON,KLEV))
   ALLOCATE(ZR1(KLON,KLEV))
   ALLOCATE(ZR2(KLON,KLEV))

   ZR1(:,:)=0.0_JPRB
   ZR2(:,:)=0.0_JPRB
   ZB (:,:)=0.0_JPRB

! Compute tendencies
  
   DO JK=KLEV,KTOPM2,-1
      DO JL=KIDIA,KFDIA
        IF (LLCUMBAS(JL,JK)) THEN
          ZR2(JL,JK)  = ZR2(JL,JK)  + PTENV(JL,JK)*ZTSPHY
          ZVEN(JL,JK) = ZVEN(JL,JK) - PTENV(JL,JK)*ZTSPHY

          ZR1(JL,JK)  = ZR1(JL,JK)  + PTENU(JL,JK)*ZTSPHY
          ZUEN(JL,JK) = ZUEN(JL,JK) - PTENU(JL,JK)*ZTSPHY
        ENDIF
      ENDDO
   ENDDO

   CALL CUBIDIAGAD &
      &( KIDIA, KFDIA, KLON, KLEV, &
      &  KCTOP, LLCUMBAS, &
      &  ZMFUU15,  ZB5,  ZDVDT5, ZR25, &
      &  ZMFUU  ,  ZB ,  ZDVDT , ZR2 )

   CALL CUBIDIAGAD &
      &( KIDIA, KFDIA, KLON, KLEV, &
      &  KCTOP, LLCUMBAS, &
      &  ZMFUU15,  ZB5,  ZDUDT5, ZR15, &
      &  ZMFUU  ,  ZB ,  ZDUDT , ZR1 )

! Fill vectors A, B and RHS 
  
   DO JK=KLEV,KTOPM2,-1
      IK=JK+1
      DO JL=KIDIA,KFDIA
        IF (LLCUMBAS(JL,JK)) THEN
          ZZP35=RMFSOLUV2*PTSPHY*ZGDP5(JL,JK)

          ZZP=0.0_JPRB

          IF (JK < KLEV) THEN
            PMFU(JL,IK) = PMFU(JL,IK) + ZB(JL,JK)*ZZP35
            PMFD(JL,IK) = PMFD(JL,IK) + ZB(JL,JK)*ZZP35
            ZZP = ZZP + ZB(JL,JK)*(PMFU5(JL,IK)+PMFD5(JL,IK))
          ENDIF
          ZB(JL,JK)=0.0_JPRB

          ZVEN(JL,JK) = ZVEN(JL,JK)+ZDVDT(JL,JK)
          ZDVDT(JL,JK)= ZDVDT(JL,JK)*PTSPHY

          ZUEN(JL,JK) = ZUEN(JL,JK)+ZDUDT(JL,JK)
          ZDUDT(JL,JK)= ZDUDT(JL,JK)*PTSPHY

          ZZP = ZZP - (PMFU5(JL,JK)+PMFD5(JL,JK))*ZMFUU(JL,JK)
          PMFU(JL,JK) = PMFU(JL,JK) - ZZP35*ZMFUU(JL,JK) 
          PMFD(JL,JK) = PMFD(JL,JK) - ZZP35*ZMFUU(JL,JK) 
          ZMFUU(JL,JK)=0.0_JPRB

          ZGDP(JL,JK) = ZGDP(JL,JK) + RMFSOLUV2*PTSPHY*ZZP
          ZZP=0._JPRB 
        ENDIF
      ENDDO
   ENDDO

   ZMFUU(:,:)=0.0_JPRB
!   ZB(:,:)=0.0_JPRB

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

ENDIF

!*    1.2          COMPUTE TENDENCIES
!                  ------------------

DO JK=KLEV,KTOPM2,-1
  IF(JK < KLEV) THEN
    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL)) THEN
        ZMFUV(JL,JK+1)=ZMFUV(JL,JK+1)+ZGDP5(JL,JK)*ZDVDT(JL,JK)
        ZMFUV(JL,JK  )=ZMFUV(JL,JK  )-ZGDP5(JL,JK)*ZDVDT(JL,JK)
        ZMFDV(JL,JK+1)=ZMFDV(JL,JK+1)+ZGDP5(JL,JK)*ZDVDT(JL,JK)
        ZMFDV(JL,JK  )=ZMFDV(JL,JK  )-ZGDP5(JL,JK)*ZDVDT(JL,JK)
        ZGDP(JL,JK)=ZGDP(JL,JK)+ZDVDT(JL,JK) &
                 & *(ZMFUV5(JL,JK+1)-ZMFUV5(JL,JK) &
                 & + ZMFDV5(JL,JK+1)-ZMFDV5(JL,JK))  
        ZDVDT(JL,JK)=0.0_JPRB
        
        ZMFUU(JL,JK+1)=ZMFUU(JL,JK+1)+ZGDP5(JL,JK)*ZDUDT(JL,JK)
        ZMFUU(JL,JK  )=ZMFUU(JL,JK  )-ZGDP5(JL,JK)*ZDUDT(JL,JK)
        ZMFDU(JL,JK+1)=ZMFDU(JL,JK+1)+ZGDP5(JL,JK)*ZDUDT(JL,JK)
        ZMFDU(JL,JK  )=ZMFDU(JL,JK  )-ZGDP5(JL,JK)*ZDUDT(JL,JK)
        ZGDP(JL,JK)=ZGDP(JL,JK)+ZDUDT(JL,JK) &
                 & *(ZMFUU5(JL,JK+1)-ZMFUU5(JL,JK) &
                 & + ZMFDU5(JL,JK+1)-ZMFDU5(JL,JK))  
        ZDUDT(JL,JK)=0.0_JPRB
      ENDIF
    ENDDO
  ELSE
    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL)) THEN
        ZGDP(JL,JK) =ZGDP(JL,JK) -ZDVDT(JL,JK)*(ZMFUV5(JL,JK)+ZMFDV5(JL,JK))  
        ZMFUV(JL,JK)=ZMFUV(JL,JK)-ZDVDT(JL,JK)*ZGDP5(JL,JK)
        ZMFDV(JL,JK)=ZMFDV(JL,JK)-ZDVDT(JL,JK)*ZGDP5(JL,JK)
        ZDVDT(JL,JK)=0.0_JPRB

        ZGDP(JL,JK) =ZGDP(JL,JK) -ZDUDT(JL,JK)*(ZMFUU5(JL,JK)+ZMFDU5(JL,JK))  
        ZMFUU(JL,JK)=ZMFUU(JL,JK)-ZDUDT(JL,JK)*ZGDP5(JL,JK)
        ZMFDU(JL,JK)=ZMFDU(JL,JK)-ZDUDT(JL,JK)*ZGDP5(JL,JK)
        ZDUDT(JL,JK)=0.0_JPRB
      ENDIF
    ENDDO
  ENDIF
ENDDO
        
! linear fluxes below cloud
IF (RMFSOLUV2 == 0.0_JPRB) THEN
  DO JK=KLEV,KTOPM2,-1
!DIR$ IVDEP
!OCL NOVREC
    DO JL=KIDIA,KFDIA
      IF(LDCUM(JL).AND.JK > KCBOT(JL)) THEN
        IKB=KCBOT(JL)
  
        ZP15=PAPH5(JL,KLEV+1)-PAPH5(JL,JK)
        ZP25=PAPH5(JL,KLEV+1)-PAPH5(JL,IKB)
        ZZP15=ZP15/ZP25
  
        IF(KTYPE(JL) == 3) THEN
          ZZP25=ZZP15*ZZP15
        ELSE
          ZZP25=ZZP15
        ENDIF
  
        ZZP=0.0_JPRB
        ZP1=0.0_JPRB
        ZP2=0.0_JPRB
  
        ZMFDV(JL,IKB)=ZMFDV(JL,IKB)+ZMFDV(JL,JK)*ZZP25
        ZZP=ZZP+ZMFDV(JL,JK)*ZMFDV5(JL,IKB)
        ZMFDV(JL,JK)=0.0_JPRB
  
        ZMFDU(JL,IKB)=ZMFDU(JL,IKB)+ZMFDU(JL,JK)*ZZP25
        ZZP=ZZP+ZMFDU(JL,JK)*ZMFDU5(JL,IKB)
        ZMFDU(JL,JK)=0.0_JPRB
  
        ZMFUV(JL,IKB)=ZMFUV(JL,IKB)+ZMFUV(JL,JK)*ZZP25
        ZZP=ZZP+ZMFUV(JL,JK)*ZMFUV5(JL,IKB)
        ZMFUV(JL,JK)=0.0_JPRB
  
        ZMFUU(JL,IKB)=ZMFUU(JL,IKB)+ZMFUU(JL,JK)*ZZP25
        ZZP=ZZP+ZMFUU(JL,JK)*ZMFUU5(JL,IKB)
        ZMFUU(JL,JK)=0.0_JPRB
  
        IF(KTYPE(JL) == 3) THEN
          ZZP=2.0_JPRB*ZZP*ZZP15
        ENDIF
  
        ZP1=ZP1+ZZP/ZP25
        ZP2=ZP2-ZZP*ZP15/(ZP25*ZP25)
        ZZP=0.0_JPRB
    
        PAPH(JL,KLEV+1)=PAPH(JL,KLEV+1)+ZP2
        PAPH(JL,IKB   )=PAPH(JL,IKB   )-ZP2
        ZP2=0.0_JPRB
  
        PAPH(JL,KLEV+1)=PAPH(JL,KLEV+1)+ZP1
        PAPH(JL,JK    )=PAPH(JL,JK    )-ZP1
        ZP1=0.0_JPRB
  
      ENDIF
    ENDDO           
  ENDDO           
ENDIF

DO JK=KLEV,KTOPM2,-1
  IK=JK-1
  DO JL=KIDIA,KFDIA
    IF(LDCUM(JL)) THEN
      PMFD(JL,JK)=PMFD(JL,JK)+ZMFDV(JL,JK)*(PVD5(JL,JK)-ZIMP*ZVEN5(JL,IK))  
      PVD(JL,JK) =PVD(JL,JK) +ZMFDV(JL,JK)*PMFD5(JL,JK)
      ZVEN(JL,IK)=ZVEN(JL,IK)-ZMFDV(JL,JK)*ZIMP*PMFD5(JL,JK)
      ZMFDV(JL,JK)=0.0_JPRB

      PMFD(JL,JK)=PMFD(JL,JK)+ZMFDU(JL,JK)*(PUD5(JL,JK)-ZIMP*ZUEN5(JL,IK))  
      PUD(JL,JK) =PUD(JL,JK) +ZMFDU(JL,JK)*PMFD5(JL,JK)
      ZUEN(JL,IK)=ZUEN(JL,IK)-ZMFDU(JL,JK)*ZIMP*PMFD5(JL,JK)
      ZMFDU(JL,JK)=0.0_JPRB
 
      PMFU(JL,JK)=PMFU(JL,JK)+ZMFUV(JL,JK)*(PVU5(JL,JK)-ZIMP*ZVEN5(JL,IK))  
      PVU(JL,JK) =PVU(JL,JK) +ZMFUV(JL,JK)*PMFU5(JL,JK)
      ZVEN(JL,IK)=ZVEN(JL,IK)-ZMFUV(JL,JK)*ZIMP*PMFU5(JL,JK)
      ZMFUV(JL,JK)=0.0_JPRB

      PMFU(JL,JK)=PMFU(JL,JK)+ZMFUU(JL,JK)*(PUU5(JL,JK)-ZIMP*ZUEN5(JL,IK))  
      PUU(JL,JK) =PUU(JL,JK) +ZMFUU(JL,JK)*PMFU5(JL,JK)
      ZUEN(JL,IK)=ZUEN(JL,IK)-ZMFUU(JL,JK)*ZIMP*PMFU5(JL,JK)
      ZMFUU(JL,JK)=0.0_JPRB     
    ENDIF
  ENDDO
ENDDO

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

DO JK=KLEV,1,-1
  DO JL=KIDIA,KFDIA
    IF (LDCUM(JL)) THEN
      ZDELP5=1.0_JPRB/(PAPH5(JL,JK+1)-PAPH5(JL,JK))

      ZDELP=RG*ZGDP(JL,JK)
      ZGDP(JL,JK)=0.0_JPRB

      PAPH(JL,JK+1)=PAPH(JL,JK+1)-ZDELP*ZDELP5**2
      PAPH(JL,JK  )=PAPH(JL,JK  )+ZDELP*ZDELP5**2
      ZDELP=0.0_JPRB

      PVEN(JL,JK)=PVEN(JL,JK)+ZVEN(JL,JK)
      ZVEN(JL,JK)=0.0_JPRB

      PUEN(JL,JK)=PUEN(JL,JK)+ZUEN(JL,JK)
      ZUEN(JL,JK)=0.0_JPRB
    ENDIF
  ENDDO
ENDDO

DEALLOCATE(ZGDP5)
DEALLOCATE(ZGDP)
DEALLOCATE(ZDVDT5)
DEALLOCATE(ZDVDT)
DEALLOCATE(ZDUDT5)
DEALLOCATE(ZDUDT)

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