SUBROUTINE CONDAD &
 & ( KIDIA , KFDIA , KLON , KLEV,&
 & PTSPHY,&
 & PTM15  , PQM15,   PAPHP15, PAPP15,&
 & PTENT5,  PTENQ5,&
 & PFHPSL5, PFHPSN5, PFPLSL5, PFPLSN5,&
 & PTM1  , PQM1,   PAPHP1, PAPP1,&
 & PTENT,  PTENQ,&
 & PFHPSL, PFHPSN, PFPLSL, PFPLSN                      )  

!**** *CONDAD* - COMPUTES LARGE-SCALE WATER PHASE CHANGES.
!                (Adjoint)

!     PURPOSE.
!     --------

!          THIS ROUTINE COMPUTES THE PHYSICAL TENDENCIES OF THE TWO
!     PROGNOSTIC VARIABLES T AND Q DUE TO THE WATER PHASE CHANGES:
!     CONDENSATION (ASSUMED TO BE TOTAL FOR ANY SUPERSATURATION) AS SNOW
!     OR LIQUID WATER DEPENDING ON TEMPERATURE, EVAPORATION OF FALLING
!     PRECIPITATIONS IN UNSATURATED LAYERS AND MELTING OF
!     THE FALLING SNOW. THE WHOLE COMPUTATION IS AN INSTANTANEOUS
!     ADAPTATION PROCESS ON THE T+1 VALUES. RAIN AND SNOWFALL SURFACE
!     FLUXES, LATER TO BE USED FOR SOIL PROCESS ARE STORED.

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

!          *CONDAD* IS CALLED FROM *CALLPARAD*.
!          THE ROUTINE TAKES ITS INPUT FROM THE LONG-TERM STORAGE:
!     T AND Q AT T+1 AND P ON LEVEL BOUNDARIES AT T+1 FROM WHICH THE P
!     FOR THE PROGNOSTIC LEVELS (NECESSARY FOR THE SATURATION MIXING
!     RATIO COMPUTATIONS) HAVE BEEN COMPUTED ASSUMING A 0.5/0.5 LINEAR
!     INTERPOLATION. IT RETURNS ITS OUTPUT TO THE SAME SPACE: TENDENCIES
!     OF T AND Q AND PRECIPITATION FLUXES FOR RAIN AND SNOW.

!     METHOD.
!     -------

!          THE ATMOSPHERIC COLUMN IS SCANNED FROM TOP TO BOTTOM
!     OBVIOUSLY. THE POSSIBLE MELTING OF INCOMING SNOW IN THE LAYER
!     IS ENVISAGED FIRST (NO ACTION IN THE UPMOST LAYER) AND OCCURS
!     AS SOON AS THE TEMPERATURE IS LARGER THAN 2 CELSIUS. THE
!     MELTING IS LIMITED BY THE SNOW AMOUNT EVIDENTLY AND THE
!     INDUCED COOLING WHICH SHOULD MAINTAIN THE TEMPERATURE ABOVE
!     THE 2 CELSIUS THRESHOLD BUT IRRESPECTIVELY OF ANY SATURATION
!     CRITERIA AT THIS STAGE.
!         THEN WITH REFERENCE TO THE NEW TEMPERATURE
!     AFTER MELTING A TEST OF THE
!     RELATIVE HUMIDITY IS FIRST PERFORMED. IF THERE IS SUPERSATURATION
!     TEMPERATURE AND RELATIVE HUMIDITY ARE MODIFIED TO BRING THE LAYER
!     BACK TO A STATE OF QUASI EXACT SATURATION (ESTIMATED FROM A FIRST
!     ORDER *TAYLOR DEVELOPMENT OF QS(T) FROM THE INITIAL TEMPERATURE).
!     THE AMOUNT OF PRECIPITATIONS OBTAINED IS ASSIGNED TO THE SNOW OR
!     RAIN FLUX DEPENDING ON THE LOCAL TEMPERATURE RELATIVE TO THE
!     FREEZING POINT OF WATER. IF THERE IS UNDERSATURATION THE
!     REMAINING PRECIPITATION (SNOW + RAIN) EVAPORATES
!     ACROSS THE LAYER:THE RATE IS SUCH AS TO EFFECTIVELY MOISTEN THE
!     SUB CLOUD-LAYERS.
!          AT THE END THE SURFACE VALUES FOR RAIN AND SNOW FALL ARE
!     PREPARED.

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

!    *KIDIA*        START POINT
!    *KFDIA*        END POINT
!    *KLON*         NUMBER OF GRID POINTS PER PACKET
!    *KLEV*         NUMBER OF LEVELS

!     INPUT PARAMETERS (LOGICAL)

!     INPUT PARAMETERS (REAL)

!    *PTSPHY*       TIME STEP FOR THE PHYSICS                         S
!    *PTM15*        TEMPERATURE (T-1)                           (Trajectory)            
!    *PQM15*        SPECIFIC HUMIDITY (T-1)                     (Trajectory)           
!    *PAPHP15*      PROVISIONAL PRESSURE ON HALF LEVELS         (Trajectory)
!    *PAPP15*       PROVISIONAL PRESSURE ON FULL LEVELS         (Trajectory)
!    *PTM1*         TEMPERATURE (T-1)                                 K
!    *PQM1*         SPECIFIC HUMIDITY (T-1)                           KG/KG
!    *PAPHP1*       PROVISIONAL PRESSURE ON HALF LEVELS               PA
!    *PAPP1*        PROVISIONAL PRESSURE ON FULL LEVELS               PA

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

!    *PFHPSL5*      ENTHALPY FLUX DUE TO LARGE SCALE RAIN       (Trajectory)
!    *PFHPSN5*      ENTHALPY FLUX DUE TO LARGE SCALE SNOW       (Trajectory)
!    *PFPLSL5*      LARGE SCALE RAIN FLUX                       (Trajectory)
!    *PFPLSN5*      LARGE SCALE SNOW FLUX                       (Trajectory)
!    *PFHPSL*       ENTHALPY FLUX DUE TO LARGE SCALE RAIN         J/(M2*S)
!    *PFHPSN*       ENTHALPY FLUX DUE TO LARGE SCALE SNOW         J/(M2*S)
!    *PFPLSL*       LARGE SCALE RAIN FLUX                        KG/(M2*S)
!    *PFPLSN*       LARGE SCALE SNOW FLUX                        KG/(M2*S)

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

!          *CUADJTQ*   ADJUST T AND Q DUE TO CONDENSATION
!          *CUADJTQAD* ADJUST T AND Q DUE TO CONDENSATION (Adjoint)

!     REFERENCE.
!     ----------

!          SEE LARGE SCALE PHASE CHANGES' PART OF THE MODEL'S
!     DOCUMENTATION FOR DETAILS ABOUT THE MATHEMATICS OF THIS ROUTINE.

!     MODIFICATIONS
!     -------------
!        92-09-17 : Update to Cy44      J.-J. MORCRETTE
!        95-11-09 : Adaptation for TL/AD versions J.-F. MAHFOUF
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!        M.Ko"hler     03-Dec-2004 cpmoist using RVTMP2
!     ------------------------------------------------------------------

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

USE YOMCST   , ONLY : RG       ,R        ,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   ,RVTMP2  
USE YOECND   , ONLY : REPFLM
USE YOEPHLI  , ONLY : LPHYLIN  ,RLPTRC   ,RLPEVAP
USE YOMLCZ   , ONLY : LRAIN    ,RLRAIN   ,NRAINSTART
USE YOMCT3   , ONLY : NSTEP

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)    :: PTSPHY 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQM15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHP15(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPP15(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENT5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENQ5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFHPSL5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFHPSN5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFPLSL5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFPLSN5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQM1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAPHP1(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAPP1(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENT(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PTENQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFHPSL(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFHPSN(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFPLSL(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFPLSN(KLON,KLEV+1) 

LOGICAL ::  LLFLAG(KLON)

REAL(KIND=JPRB) :: ZRFL5(KLON),                ZSFL5(KLON),&
 & ZTP15(KLON),                ZQP15(KLON),&
 & ZLVDCP5(KLON),              ZLSDCP5(KLON),&
 & ZDP5(KLON),                 ZDR5(KLON),&
 & ZLFDCP5(KLON),&
 & ZRFLN5(KLON),               ZSFLN5(KLON),&
 & ZFLN5(KLON),                 &
 & ZDTDT5(KLON),               ZDQDT5(KLON)  
REAL(KIND=JPRB) :: ZPP5(KLON)     , ZQOLD5(KLON)
REAL(KIND=JPRB) :: ZTT5(KLON,KLEV), ZQQ5(KLON,KLEV)

REAL(KIND=JPRB) :: ZRFL(KLON),                 ZSFL(KLON),&
 & ZTP1(KLON),                 ZQP1(KLON),&
 & ZLVDCP(KLON),               ZLSDCP(KLON),&
 & ZDP(KLON),                  ZDR(KLON),&
 & ZLFDCP(KLON),&
 & ZRFLN(KLON),                ZSFLN(KLON),&
 & ZFLN(KLON),                 &
 & ZDTDT(KLON),                ZDQDT(KLON)  
REAL(KIND=JPRB) :: ZPP(KLON)     , ZQOLD(KLON)
REAL(KIND=JPRB) :: ZTT(KLON,KLEV), ZQQ(KLON,KLEV)

!     Extra storage for trajectory

REAL(KIND=JPRB) :: ZDP55   (KLON,KLEV),ZQP155  (KLON,KLEV),&
 & ZLFDCP55(KLON,KLEV),ZLVDCP55(KLON,KLEV),ZLSDCP55(KLON,KLEV),&
 & ZQOLD55 (KLON,KLEV),ZTP155A (KLON,KLEV),ZCONS55 (KLON,KLEV),&
 & ZSNMLT55(KLON,KLEV),ZRFLN55A(KLON,KLEV),&
 & ZSFLN55A(KLON,KLEV),ZTP155B (KLON,KLEV),ZDR55   (KLON,KLEV),&
 & ZALFAW55(KLON,KLEV),Z2S55   (KLON,KLEV),ZRFLN55B(KLON,KLEV),&
 & ZSFLN55B(KLON,KLEV),ZFLN55A (KLON,KLEV),ZRFLN55C(KLON,KLEV),&
 & ZSFLN55C(KLON,KLEV),ZFLN55B (KLON,KLEV),&
 & ZSFL55  (KLON,KLEV),ZRFL55  (KLON,KLEV),&
 & ZZ2S55  (KLON,KLEV),Z4S55   (KLON,KLEV),Z3S55   (KLON,KLEV),&
 & ZTT55   (KLON,KLEV),ZQQ55   (KLON,KLEV)  

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

REAL(KIND=JPRB) :: Z2S, Z2S5, Z3S, Z3S5, Z4S, Z4S5,&
 & ZALFAW, ZALFAW5, ZCEVAP, ZCONS, ZCONS2, &
 & ZCONS5, ZMELTP2, ZRN, ZRN5, ZSN, ZSN5, ZSNMLT, &
 & ZSNMLT5, ZTMST, ZZ2S, ZZ2S5, ZZZ, ZZZ5  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "cuadjtqs.intfb.h"
#include "cuadjtqsad.intfb.h"

#include "fcttre.h"
!     ------------------------------------------------------------------

!*    Computational constants
!     -----------------------

IF (LHOOK) CALL DR_HOOK('CONDAD',0,ZHOOK_HANDLE)
ZTMST=PTSPHY
ZCONS2 =1.0_JPRB/(ZTMST*RG)
ZMELTP2=RTT+2.0_JPRB

!     Fraction of rainfall evaporation in subsaturated layers
!     -------------------------------------------------------

IF (LPHYLIN) THEN
  ZCEVAP=RLPEVAP
ELSE
  ZCEVAP=0.95_JPRB
ENDIF

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

!*              ------  TRAJECTORY COMPUTATIONS  ------

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

!*         2.1     SET TO ZERO PRECIPITATION FLUXES AT THE TOP.

DO JL=KIDIA,KFDIA
  ZRFL5(JL)=0.0_JPRB
  ZSFL5(JL)=0.0_JPRB
  PFPLSL5(JL,1)=0.0_JPRB
  PFPLSN5(JL,1)=0.0_JPRB
ENDDO

!*         2.2     VERTICAL LOOP.

DO JK=1,KLEV

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

!*         3.     COMPUTATION OF THE CONDENSATION RATES.
!                 ----------- -- --- ------------ ------

!*         3.1     T AND Q PROVISIONAL VALUES AT T+1 AND EFFECTIVES L/CP
!*                 FOR VAPORIZATION AND SUBLIMATION.

  DO JL=KIDIA,KFDIA
    ZDP5(JL)=PAPHP15(JL,JK+1)-PAPHP15(JL,JK)
    ZDP55(JL,JK)=ZDP5(JL)
    ZTP15(JL)=PTM15(JL,JK)+ZTMST*PTENT5(JL,JK)
    ZTP155A(JL,JK)=ZTP15(JL)
    ZQP15(JL)=PQM15(JL,JK)+ZTMST*PTENQ5(JL,JK)
    ZQP155(JL,JK)=ZQP15(JL)
    ZZZ5=1.0_JPRB/(RCPD+RCPD*RVTMP2*ZQP15(JL))
    ZLFDCP5(JL)=RLMLT*ZZZ5
    ZLFDCP55(JL,JK)=ZLFDCP5(JL)
    ZLVDCP5(JL)=RLVTT*ZZZ5
    ZLVDCP55(JL,JK)=ZLVDCP5(JL)
    ZLSDCP5(JL)=RLSTT*ZZZ5
    ZLSDCP55(JL,JK)=ZLSDCP5(JL)
    ZPP5(JL)=PAPP15(JL,JK)
    ZTT5(JL,JK)=ZTP15(JL)
    ZTT55(JL,JK)=ZTP15(JL)
    ZQQ5(JL,JK)=ZQP15(JL)
    ZQQ55(JL,JK)=ZQP15(JL)
    ZQOLD5(JL)=ZQP15(JL)
    ZQOLD55(JL,JK)=ZQOLD5(JL)
    LLFLAG(JL)=.TRUE.
  ENDDO

!*         3.2    MELTING OF INCOMING SNOW

  DO JL=KIDIA,KFDIA

    ZSFL55 (JL,JK)=ZSFL5(JL)
    ZRFL55 (JL,JK)=ZRFL5(JL)
    IF (ZSFL5(JL) /= 0.0_JPRB) THEN
      ZCONS5=ZCONS2*ZDP5(JL)/ZLFDCP5(JL)
      ZCONS55(JL,JK)=ZCONS5
      IF ((ZTP15(JL)-ZMELTP2) > 0.0_JPRB) THEN
        ZZ2S5=ZCONS5*(ZTP15(JL)-ZMELTP2)
      ELSE
        ZZ2S5=0.0_JPRB
      ENDIF
      ZZ2S55(JL,JK)=ZZ2S5
      IF (ZSFL5(JL) <= ZZ2S5) THEN
        ZSNMLT5=ZSFL5(JL)
      ELSE
        ZSNMLT5=ZZ2S5
      ENDIF
      ZSNMLT55(JL,JK)=ZSNMLT5
      ZRFLN5(JL)=ZRFL5(JL)+ZSNMLT5
      ZSFLN5(JL)=ZSFL5(JL)-ZSNMLT5
      ZTP15(JL)=ZTP15(JL)-ZSNMLT5/ZCONS5
    ELSE
      ZRFLN5(JL)=ZRFL5(JL)
      ZSFLN5(JL)=ZSFL5(JL)
    ENDIF
    ZRFLN55A(JL,JK)=ZRFLN5(JL)
    ZSFLN55A(JL,JK)=ZSFLN5(JL)
    ZTP155B (JL,JK)=ZTP15(JL)
  ENDDO

!*              3.3    NEW PRECIPITATION FLUXES AFTER CONDENSATION
!*                     AND TOTAL FLUXES

  IK=JK
  ICALL=0
  CALL CUADJTQS  ( KIDIA, KFDIA, KLON, KLEV, IK,&
   & ZPP5 , ZTT5 , ZQQ5, LLFLAG, ICALL  )   

  DO JL=KIDIA,KFDIA
    ZDR5(JL)=ZCONS2*ZDP5(JL)*(ZQOLD5(JL)-ZQQ5(JL,JK))
    ZDR55(JL,JK)=ZDR5(JL)
    ZALFAW5=0.545_JPRB*(1.0_JPRB+TANH(0.17_JPRB*(ZTP15(JL)-RLPTRC)))
    ZALFAW55(JL,JK)=ZALFAW5
    IF (ZDR5(JL) > 0.0_JPRB) THEN
      Z2S5=ZDR5(JL)
    ELSE
      Z2S5=0.0_JPRB
    ENDIF
    Z2S55(JL,JK)=Z2S5
    ZRN5=    ZALFAW5 * Z2S5
    ZSN5=(1.0_JPRB-ZALFAW5) * Z2S5

!          NEW PRECIPITATION FLUXES

    ZRFLN5(JL)=ZRFLN5(JL)+ZRN5
    ZSFLN5(JL)=ZSFLN5(JL)+ZSN5
    ZRFLN55B(JL,JK)=ZRFLN5(JL)
    ZSFLN55B(JL,JK)=ZSFLN5(JL)

!          TOTAL FLUXES

    ZFLN5(JL)=ZRFLN5(JL)+ZSFLN5(JL)
    ZFLN55A(JL,JK)=ZFLN5(JL)

  ENDDO

!*         3.4     AT TOP LAYER OR IF THERE ARE NO PRECIPITATION AVOID
!*                 EVAPORATION COMPUTATION

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

!*         4.     COMPUTATION OF EVAPORATION
!*                ----------- -- -----------

  DO JL=KIDIA,KFDIA
    IF (ZFLN5(JL) /= 0.0_JPRB.AND.JK > 1) THEN
      IF ((ZDR5(JL)*ZCEVAP) <= 0.0_JPRB) THEN
        Z3S5=ZDR5(JL)*ZCEVAP
      ELSE
        Z3S5=0.0_JPRB
      ENDIF
      Z3S55(JL,JK)=Z3S5
      IF ((ZFLN5(JL)+Z3S5) > 0.0_JPRB) THEN
        ZFLN5(JL)=ZFLN5(JL)+Z3S5
      ELSE
        ZFLN5(JL)=0.0_JPRB
      ENDIF
      IF ((ZRFLN5(JL)+ZSFLN5(JL)) > REPFLM) THEN
        Z4S5=ZRFLN5(JL)+ZSFLN5(JL)
      ELSE
        Z4S5=REPFLM
      ENDIF
      Z4S55(JL,JK)=Z4S5
      ZFLN55B(JL,JK)=ZFLN5(JL)
      ZRFLN5(JL)=ZRFLN5(JL)*ZFLN5(JL)/Z4S5
      ZSFLN5(JL)=ZSFLN5(JL)*ZFLN5(JL)/Z4S5
    ENDIF
    ZRFLN55C(JL,JK)=ZRFLN5(JL)
    ZSFLN55C(JL,JK)=ZSFLN5(JL)
  ENDDO

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

!*         6.     INCREMENTATION OF T AND Q TENDENCIES AND FLUXES' SWAP.
!                 -------------- -- - --- - ---------- --- ------- -----

!*         6.1     RESUME COMPUTATIONS FOR THE TOP LAYER.

!*         6.2     MODIFICATION OF THE T AND Q TENDENCIES.

  DO JL=KIDIA,KFDIA
    ZDQDT5(JL)=-((ZRFLN5(JL)-ZRFL5(JL))+(ZSFLN5(JL)-ZSFL5(JL)))&
     & *(RG/ZDP5(JL))  
    PTENQ5(JL,JK)=PTENQ5(JL,JK)+ZDQDT5(JL)
    ZDTDT5(JL)=(ZLVDCP5(JL)*(ZRFLN5(JL)-ZRFL5(JL))+ZLSDCP5(JL)&
     & *(ZSFLN5(JL)-ZSFL5(JL)))*(RG/ZDP5(JL))  
    PTENT5(JL,JK)=PTENT5(JL,JK)+ZDTDT5(JL)
    PFPLSL5(JL,JK+1)=ZRFLN5(JL)
    PFPLSN5(JL,JK+1)=ZSFLN5(JL)
  ENDDO

!*         6.4     SWAP OF THE FLUXES AND END OF THE VERTICAL LOOP.

  DO JL=KIDIA,KFDIA
    ZRFL5(JL)=ZRFLN5(JL)
    ZSFL5(JL)=ZSFLN5(JL)
  ENDDO

ENDDO

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

!*         7.     ENTHALPY FLUXES DUE TO PRECIPITATION
!                 ------------------------------------

DO JK=1,KLEV+1
  DO JL=KIDIA,KFDIA
    PFHPSL5(JL,JK)=-PFPLSL5(JL,JK)*RLVTT
    PFHPSN5(JL,JK)=-PFPLSN5(JL,JK)*RLSTT
  ENDDO
ENDDO

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

!*                 ------ ADJOINT COMPUTATIONS ------

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

!*         8.     INITIALIZATIONS
!                 ---------------

DO JL=KIDIA,KFDIA
  ZSFLN(JL)  = 0.0_JPRB
  ZSFL(JL)   = 0.0_JPRB
  ZRFLN(JL)  = 0.0_JPRB
  ZRFL(JL)   = 0.0_JPRB
  ZDTDT(JL)  = 0.0_JPRB
  ZLVDCP(JL) = 0.0_JPRB
  ZLSDCP(JL) = 0.0_JPRB
  ZLFDCP(JL) = 0.0_JPRB
  ZDQDT(JL)  = 0.0_JPRB
  ZDP(JL)    = 0.0_JPRB
  ZFLN(JL)   = 0.0_JPRB
  ZDR(JL)    = 0.0_JPRB
  ZTP1(JL)   = 0.0_JPRB
  ZQP1(JL)   = 0.0_JPRB
  ZPP(JL)    = 0.0_JPRB
  ZQOLD(JL)  = 0.0_JPRB
ENDDO

DO JK=1,KLEV
  DO JL=KIDIA,KFDIA
    ZQQ(JL,JK) = 0.0_JPRB
    ZTT(JL,JK) = 0.0_JPRB
  ENDDO
ENDDO

!*         7.     ENTHALPY FLUXES DUE TO PRECIPITATION
!                 ------------------------------------

DO JK=1,KLEV+1
  DO JL=KIDIA,KFDIA
    PFPLSN(JL,JK)=PFPLSN(JL,JK)-PFHPSN(JL,JK)*RLSTT
    PFHPSN(JL,JK)=0.0_JPRB
    PFPLSL(JL,JK)=PFPLSL(JL,JK)-PFHPSL(JL,JK)*RLVTT
    PFHPSL(JL,JK)=0.0_JPRB
  ENDDO
ENDDO

!*         6.5     CUMULATIVE RAIN

IF (LRAIN.AND.(NSTEP > (NRAINSTART-1))) THEN
  DO JL=KIDIA,KFDIA
    PFPLSL(JL,KLEV+1)=PFPLSL(JL,KLEV+1)+RLRAIN(JL)
  ENDDO
ENDIF

!*         6.2     VERTICAL LOOP.

DO JK=KLEV,1,-1

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

!*         6.4     SWAP OF THE FLUXES AND END OF THE VERTICAL LOOP.

  DO JL=KIDIA,KFDIA
    ZSFLN(JL)=ZSFLN(JL)+ZSFL(JL)
    ZSFL (JL)=0.0_JPRB
    ZRFLN(JL)=ZRFLN(JL)+ZRFL(JL)
    ZRFL (JL)=0.0_JPRB
  ENDDO

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

!*         6.     INCREMENTATION OF T AND Q TENDENCIES AND FLUXES' SWAP.
!                 -------------- -- - --- - ---------- --- ------- -----

!*         6.1     RESUME COMPUTATIONS FOR THE TOP LAYER.

!*         6.2     MODIFICATION OF THE T AND Q TENDENCIES.

  DO JL=KIDIA,KFDIA
    ZSFLN(JL)=ZSFLN(JL)+PFPLSN(JL,JK+1)
    PFPLSN(JL,JK+1)=0.0_JPRB
    ZRFLN(JL)=ZRFLN(JL)+PFPLSL(JL,JK+1)
    PFPLSL(JL,JK+1)=0.0_JPRB
    ZDTDT(JL)=ZDTDT(JL)+PTENT(JL,JK)
    ZDP(JL) = ZDP(JL)-ZDTDT(JL)*&
     & (ZLVDCP55(JL,JK)*(ZRFLN55C(JL,JK)-ZRFL55(JL,JK))+&
     & ZLSDCP55(JL,JK)*(ZSFLN55C(JL,JK)-ZSFL55(JL,JK)))*&
     & RG/ZDP55(JL,JK)**2  
    ZRFLN(JL)=ZRFLN(JL)+ZDTDT(JL)*ZLVDCP55(JL,JK)*RG/ZDP55(JL,JK)
    ZRFL(JL) =ZRFL (JL)-ZDTDT(JL)*ZLVDCP55(JL,JK)*RG/ZDP55(JL,JK)
    ZSFLN(JL)=ZSFLN(JL)+ZDTDT(JL)*ZLSDCP55(JL,JK)*RG/ZDP55(JL,JK)
    ZSFL(JL) =ZSFL(JL)-ZDTDT(JL)*ZLSDCP55(JL,JK)*RG/ZDP55(JL,JK)
    ZLVDCP(JL)=ZLVDCP(JL)+ZDTDT(JL)*(ZRFLN55C(JL,JK)-ZRFL55(JL,JK))*&
     & RG/ZDP55(JL,JK)  
    ZLSDCP(JL)=ZLSDCP(JL)+ZDTDT(JL)*(ZSFLN55C(JL,JK)-ZSFL55(JL,JK))*&
     & RG/ZDP55(JL,JK)  
    ZDTDT(JL)=0.0_JPRB

    ZDQDT(JL)=ZDQDT(JL)+PTENQ(JL,JK)
    ZDP(JL)  =ZDP(JL)+ZDQDT(JL)*((ZRFLN55C(JL,JK)-ZRFL55(JL,JK))+&
     & (ZSFLN55C(JL,JK)-ZSFL55(JL,JK)))*RG/ZDP55(JL,JK)**2  
    ZRFLN(JL)=ZRFLN(JL)-ZDQDT(JL)*RG/ZDP55(JL,JK)
    ZRFL(JL)=ZRFL(JL)+ZDQDT(JL)*RG/ZDP55(JL,JK)
    ZSFLN(JL)=ZSFLN(JL)-ZDQDT(JL)*RG/ZDP55(JL,JK)
    ZSFL(JL)=ZSFL(JL)+ZDQDT(JL)*RG/ZDP55(JL,JK)
    ZDQDT(JL)=0.0_JPRB
  ENDDO

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

!*         4.     COMPUTATION OF EVAPORATION
!*                ----------- -- -----------

  DO JL=KIDIA,KFDIA
    IF (ZFLN55A(JL,JK) /= 0.0_JPRB.AND.JK > 1) THEN

      Z4S=0.0_JPRB
      Z3S=0.0_JPRB

      ZFLN(JL)=ZFLN(JL)+ZSFLN(JL)*ZSFLN55B(JL,JK)/Z4S55(JL,JK)
      Z4S=Z4S-ZSFLN(JL)*ZSFLN55B(JL,JK)*ZFLN55B(JL,JK)/Z4S55(JL,JK)**2
      ZSFLN(JL)=ZSFLN(JL)*ZFLN55B(JL,JK)/Z4S55(JL,JK)
      ZFLN(JL)=ZFLN(JL)+ZRFLN(JL)*ZRFLN55B(JL,JK)/Z4S55(JL,JK)
      Z4S=Z4S-ZRFLN(JL)*ZRFLN55B(JL,JK)*ZFLN55B(JL,JK)/Z4S55(JL,JK)**2
      ZRFLN(JL)=ZRFLN(JL)*ZFLN55B(JL,JK)/Z4S55(JL,JK)
      IF ((ZRFLN55B(JL,JK)+ZSFLN55B(JL,JK)) > REPFLM) THEN
        ZRFLN(JL)=ZRFLN(JL)+Z4S
        ZSFLN(JL)=ZSFLN(JL)+Z4S
      ENDIF
      IF ((ZFLN55A(JL,JK)+Z3S55(JL,JK)) > 0.0_JPRB) THEN
        Z3S=Z3S+ZFLN(JL)
      ELSE
        ZFLN(JL)=0.0_JPRB
      ENDIF
      IF ((ZDR55(JL,JK)*ZCEVAP) <= 0.0_JPRB) THEN
        ZDR(JL)=ZDR(JL)+Z3S*ZCEVAP
      ENDIF
    ENDIF
  ENDDO

!*              3.3    NEW PRECIPITATION FLUXES AFTER CONDENSATION
!*                     AND TOTAL FLUXES

  DO JL=KIDIA,KFDIA

!          TOTAL FLUXES

    ZRFLN(JL)=ZRFLN(JL)+ZFLN(JL)
    ZSFLN(JL)=ZSFLN(JL)+ZFLN(JL)
    ZFLN (JL)=0.0_JPRB

!          NEW PRECIPITATION FLUXES

    ZSN=0.0_JPRB
    ZRN=0.0_JPRB

    ZSN=ZSN+ZSFLN(JL)
    ZRN=ZRN+ZRFLN(JL)

    ZALFAW=0.0_JPRB
    Z2S=0.0_JPRB

    ZALFAW=ZALFAW-ZSN*Z2S55(JL,JK)
    Z2S = Z2S + ZSN*(1.0_JPRB-ZALFAW55(JL,JK))
    ZALFAW=ZALFAW+ZRN*Z2S55(JL,JK)
    Z2S = Z2S + ZRN*ZALFAW55(JL,JK)
    IF (ZDR55(JL,JK) > 0.0_JPRB) THEN
      ZDR(JL)=ZDR(JL)+Z2S
    ENDIF
    ZTP1(JL)=ZTP1(JL)+ZALFAW*&
     & 0.545_JPRB*0.17_JPRB/COSH(0.17_JPRB*(ZTP155B(JL,JK)-RLPTRC))**2  
    ZDP(JL)=ZDP(JL)+ZDR(JL)*ZCONS2*(ZQOLD55(JL,JK)-ZQQ5(JL,JK))
    ZQOLD(JL)=ZQOLD(JL)+ZDR(JL)*ZCONS2*ZDP55(JL,JK)
    ZQQ(JL,JK)=ZQQ(JL,JK)-ZDR(JL)*ZCONS2*ZDP55(JL,JK)
    ZDR(JL)=0.0_JPRB
    ZPP5(JL)=PAPP15(JL,JK)
  ENDDO

  IK=JK
  ICALL=0
  CALL CUADJTQSAD ( KIDIA, KFDIA, KLON, KLEV, IK,&
   & ZPP5 , ZTT55 , ZQQ55,&
   & ZPP  , ZTT   , ZQQ , LLFLAG, ICALL  )  

!*         3.2    MELTING OF INCOMING SNOW

  DO JL=KIDIA,KFDIA

    ZCONS=0.0_JPRB
    ZZ2S=0.0_JPRB
    ZSNMLT=0.0_JPRB

    IF (ZSFL55(JL,JK) /= 0.0_JPRB) THEN
      ZCONS=ZCONS+ZTP1(JL)*ZSNMLT55(JL,JK)/ZCONS55(JL,JK)**2
      ZSNMLT=ZSNMLT-ZTP1(JL)/ZCONS55(JL,JK)
      ZSNMLT=ZSNMLT-ZSFLN(JL)
      ZSFL(JL)=ZSFL(JL)+ZSFLN(JL)
      ZSFLN(JL)=0.0_JPRB
      ZSNMLT=ZSNMLT+ZRFLN(JL)
      ZRFL(JL)=ZRFL(JL)+ZRFLN(JL)
      ZRFLN(JL)=0.0_JPRB
      IF (ZSFL55(JL,JK) <= ZZ2S55(JL,JK)) THEN
        ZSFL(JL)=ZSFL(JL)+ZSNMLT
      ELSE
        ZZ2S=ZZ2S+ZSNMLT
      ENDIF
      IF ((ZTP155A(JL,JK)-ZMELTP2) > 0.0_JPRB) THEN
        ZTP1(JL)=ZTP1(JL)+ZCONS55(JL,JK)*ZZ2S
        ZCONS=ZCONS+(ZTP155A(JL,JK)-ZMELTP2)*ZZ2S
      ENDIF
      ZDP(JL)   =ZDP(JL)+ZCONS*ZCONS2/ZLFDCP55(JL,JK)
      ZLFDCP(JL)=ZLFDCP(JL)-ZCONS*ZCONS2*ZDP55(JL,JK)/ZLFDCP55(JL,JK)**2
    ELSE
      ZSFL(JL)=ZSFL(JL)+ZSFLN(JL)
      ZSFLN(JL)=0.0_JPRB
      ZRFL(JL)=ZRFL(JL)+ZRFLN(JL)
      ZRFLN(JL)=0.0_JPRB
    ENDIF
  ENDDO

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

!*         3.     COMPUTATION OF THE CONDENSATION RATES.
!                 ----------- -- --- ------------ ------

!*         3.1     T AND Q PROVISIONAL VALUES AT T+1 AND EFFECTIVES L/CP
!*                 FOR VAPORIZATION AND SUBLIMATION.

  DO JL=KIDIA,KFDIA

    ZZZ=0.0_JPRB

    ZQP1(JL)=ZQP1(JL)+ZQOLD(JL)
    ZQOLD(JL)=0.0_JPRB
    ZQP1(JL)=ZQP1(JL)+ZQQ(JL,JK)
    ZQQ(JL,JK)=0.0_JPRB
    ZTP1(JL)=ZTP1(JL)+ZTT(JL,JK)
    ZTT(JL,JK)=0.0_JPRB
    PAPP1(JL,JK)=PAPP1(JL,JK)+ZPP(JL)
    ZPP(JL)=0.0_JPRB
    ZZZ=ZZZ+ZLSDCP(JL)*RLSTT
    ZLSDCP(JL)=0.0_JPRB
    ZZZ=ZZZ+ZLVDCP(JL)*RLVTT
    ZLVDCP(JL)=0.0_JPRB
    ZZZ=ZZZ+ZLFDCP(JL)*RLMLT
    ZLFDCP(JL)=0.0_JPRB
    ZQP1(JL)=ZQP1(JL)-ZZZ*RCPD*RVTMP2/(RCPD+RCPD*RVTMP2*ZQP155(JL,JK))**2
    PQM1(JL,JK)=PQM1(JL,JK)+ZQP1(JL)
    PTENQ(JL,JK)=PTENQ(JL,JK) + ZQP1(JL)*ZTMST
    ZQP1(JL)=0.0_JPRB
    PTM1(JL,JK)=PTM1(JL,JK)+ZTP1(JL)
    PTENT(JL,JK)=PTENT(JL,JK) + ZTP1(JL)*ZTMST
    ZTP1(JL)=0.0_JPRB
    PAPHP1(JL,JK+1)=PAPHP1(JL,JK+1)+ZDP(JL)
    PAPHP1(JL,JK)=PAPHP1(JL,JK)-ZDP(JL)
    ZDP(JL)=0.0_JPRB
  ENDDO
ENDDO

!*         2.1     SET TO ZERO PRECIPITATION FLUXES AT THE TOP.

DO JL=KIDIA,KFDIA
  ZRFL(JL)=0.0_JPRB
  ZSFL(JL)=0.0_JPRB
  PFPLSL(JL,1)=0.0_JPRB
  PFPLSN(JL,1)=0.0_JPRB
ENDDO

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