!OPTIONS XOPT(NOEVAL)
SUBROUTINE ACDIFSPAD (KIDIA,KFDIA,KLON,KTDIA,KLEV,&
 !-----------------------------------------------------------------------
 ! - INPUT  2D .
 & PAPHI5,PAPHIF5,PDELP5,PRDELP5,PT5,PQ5,&
 & PU5,PV5,PKTROV5,PKUROV5,PXTROV5,PXUROV5,&
 & PAPHI,PAPHIF,PDELP,PRDELP,PT,PQ,&
 & PU,PV,PKTROV,PKUROV,PXTROV,PXUROV,&
 ! - INPUT  1D .
 & PCDROV5,PCHROV5,PXDROV5,PXHROV5,&
 & PCDROV,PCHROV,PXDROV,PXHROV,&
 & PLSM,PNEIJ,PQS,PTS,PNTS,PTS1,PQS1,&
 ! - OUTPUT 2D .
 & PDIFTQ5,PDIFTS5,PSTRTU5,PSTRTV5,&
 & PDIFTQ,PDIFTS,PSTRTU,PSTRTV,&
 ! - OUTPUT 1D .
 & PFCLL,PFCLN,PFCS,PFEVL,PFEVN,PLHS,&
 ! - LOCK ARGUMENTS .
 & CDLOCK)  

!**** *ACDIFSPAD * - ADJOINT COMPUTATION OF VERTICAL TURBULENT
!                    DIFFUSION - SIMPLIFIED

!     Subject.
!     --------
!     Adjoint computation of:
!     - COMPUTATION OF VERTICAL TURBULENT DIFFUSION INCLUDING A NEGATIVE
!       HUMIDITY CORRECTION : VERTICAL TURBULENT FLUXES, CALL TO THE
!       CHARNOCK FORMULA, THERMAL RADIATION FLUXES' CORRECTION AND ALL
!       CORRESPONDING FLUXES AT THE SURFACE OR IN THE SOIL .
!       SIMPLIFICATION OF ORIGINAL ACDIFUS IS BASED ON ASSUMPTION
!       OF CONSTANT FIELDS FOR Ts and Qs.

!**   Interface.
!     ----------
!        *CALL* *ACDIFSPAD*

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

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

! - DIMENSIONAL PARAMETERS OF PHYSICS.

! KIDIA      : START OF HORIZONTAL LOOP (IST in CPG)
! KFDIA      : END OF HORIZONTAL LOOP.
! KLON       : HORIZONTAL DIMENSION.
! KTDIA      : START OF THE VERTICAL LOOP IN THE PHYSICS (1 IN GENERAL)
! KLEV       : END OF VERTICAL LOOP AND VERTICAL DIMENSION "FULL LEVEL"
!              (NFLEVG in CPG)

! - THE NAME OF THE PHYSICAL VARIABLES.

! - 2D (0:KLEV) .

! PAPHI      : GEOPOTENTIAL ON HALF LEVEL.
! PKTROV     : VERTICAL EXCHANGE COEFFICIENT OF T AND Q IN KG/(M*M*S).
! PKUROV     : VERTICAL EXCHANGE COEFFICIENT OF U AND V IN KG/(M*M*S).
! PXTROV     : "ANTI-FIBRILLATION" MULTIPLICATOR OF PKTROV.
! PXUROV     : "ANTI-FIBRILLATION" MULTIPLICATOR OF PKUROV.

! The same but for the trajectory
! -------------------------------
! PAPHI5     : GEOPOTENTIAL ON HALF LEVEL.
! PKTROV5    : VERTICAL EXCHANGE COEFFICIENT OF T AND Q IN KG/(M*M*S).
! PKUROV5    : VERTICAL EXCHANGE COEFFICIENT OF U AND V IN KG/(M*M*S).
! PXTROV5    : "ANTI-FIBRILLATION" MULTIPLICATOR OF PKTROV.
! PXUROV5    : "ANTI-FIBRILLATION" MULTIPLICATOR OF PKUROV.

! - 2D (1:KLEV) .

! PAPHIF     : GEOPOTENTIAL ON FULL LEVELS.
! PDELP      : LAYER THICKNESS IN PRESSURE UNITS.
! PQ         : SPECIFIC HUMIDITY OF WATER VAPOR.
! PRDELP     : INVERSE VALUE OF LAYER THICKNESS (OF PDELP).
! PT         : TEMPERATURE.
! PU         : X - COMPONENT OF WIND.
! PV         : Y - COMPONENT OF WIND.

! The same but for trajectory
! ---------------------------
! PAPHIF5    : GEOPOTENTIAL ON FULL LEVELS.
! PDELP5     : LAYER THICKNESS IN PRESSURE UNITS.
! PQ5        : SPECIFIC HUMIDITY OF WATER VAPOR.
! PRDELP5    : INVERSE VALUE OF LAYER THICKNESS (OF PDELP).
! PT5        : TEMPERATURE.
! PU5        : X - COMPONENT OF WIND.
! PV5        : Y - COMPONENT OF WIND.

! - 1D (PROGNOSTIC) .

! PTS        : SURFACE TEMPERATURE.
! PNTS       : SURFACE TEMPERATURE AT TIME t+dt.
! PQS        : SURFACE SPECIFIC HUMIDITY.
! PQS1       : SURFACE SPECIFIC HUMIDITY AT t+dt.

! - 1D (GEOGRAPHIQUE) .

! PLSM       : LAND/SEA MASK.

! - 1D (DIAGNOSTIQUE) .

! PCDROV     : PCD RENORME EN DENSITE FOIS VITESSE.
! PCHROV     : PCH RENORME EN DENSITE FOIS VITESSE.
! PNEIJ      : FRACTION OF SOIL COVERED BY SNOW.
! PQS        : SURFACE SPECIFIC HUMIDITY.
! PXDROV     : "ANTI-FIBRILLATION" MULTIPLICATOR OF PCDROV.
! PXHROV     : "ANTI-FIBRILLATION" MULTIPLICATOR OF PCHROV.

! The same but for trajectory
! ---------------------------
! PCDROV5    : PCD RENORME EN DENSITE FOIS VITESSE.
! PCHROV5    : PCH RENORME EN DENSITE FOIS VITESSE.
! PXDROV5    : "ANTI-FIBRILLATION" MULTIPLICATOR OF PCDROV.
! PXHROV5    : "ANTI-FIBRILLATION" MULTIPLICATOR OF PCHROV.

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

! -   ARGUMENTS FOR OUTPUT.
!     ---------------------

! - 2D (0:KLEV) .

! PDIFTQ     : TURBULENT FLUX (INC. Q NEGATIVE) OF SPECIFIC HUMIDITY.
! PDIFTS     : TURBULENT FLUX OF ENTHALPY (OR DRY STATIC ENERGY).
! PSTRTU     : TURBULENT FLUX OF MOMENTUM "U".
! PSTRTV     : TURBULENT FLUX OF MOMENTUM "V".

! The same but for trajectory
! ---------------------------
! PDIFTQ5    : TURBULENT FLUX (INC. Q NEGATIVE) OF SPECIFIC HUMIDITY.
! PDIFTS5    : TURBULENT FLUX OF ENTHALPY (OR DRY STATIC ENERGY).
! PSTRTU5    : TURBULENT FLUX OF MOMENTUM "U".
! PSTRTV5    : TURBULENT FLUX OF MOMENTUM "V".

! - 1D (DIAGNOSTIC) .

! PFCLL      : LATENT HEAT FLUX OVER LIQUID WATER (OR WET SOIL).
! PFCLN      : LATENT HEAT FLUX OVER SNOW (OR ICE).
! PFCS       : SENSIBLE HEAT FLUX AT SURFACE LEVEL.
! PFEVL      : WATER VAPOR FLUX OVER LIQUID WATER (OR WET SOIL).
! PFEVN      : WATER VAPOR FLUX OVER SNOW (OR ICE).
! PLHS       : LATENT HEAT AT THE SURFACE.

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

! -   IMPLICIT ARGUMENTS
!     -------------------

! COMMON/YOMPHY /
! COMMON/YOMCST /
! COMMON/YOMPHY0/
! COMMON/YOMPHY1/
! COMMON/YOMPHY2/
! COMMON/FCTTRM/

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

!     Externals.
!     ----------

!     Methode.
!     --------

!     Authors.
!     --------
!          95-11, M.Janiskova

!     Modifications.
!     --------------
!        97-08, introduce CDLOCK - J.M. Piriou.
!        97-11, double precision changed to single one - M. Janiskova
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

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

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

USE YOMPHY   , ONLY : LNEIGE
USE YOMCST   , ONLY : RPI      ,RDAY     ,RG       ,RV       ,&
 & RCPD     ,RCPV     ,RCW      ,RCS      ,RBETW    ,&
 & RGAMW    ,RBETD    ,RGAMD  
USE YOMPHY1  , ONLY : TMERGL
USE YOMPHY2  , ONLY : TSPHY

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)    :: KTDIA 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHI5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHIF5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDELP5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDELP5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQ5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PV5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKTROV5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKUROV5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXTROV5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXUROV5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAPHI(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAPHIF(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDELP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PRDELP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PT(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PV(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PKTROV(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PKUROV(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PXTROV(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PXUROV(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCDROV5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCHROV5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXDROV5(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXHROV5(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCDROV(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PCHROV(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PXDROV(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PXHROV(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLSM(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PNEIJ(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQS(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTS(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PNTS(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTS1(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQS1(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDIFTQ5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDIFTS5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PSTRTU5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PSTRTV5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDIFTQ(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PDIFTS(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PSTRTU(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PSTRTV(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFCLL(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFCLN(KLON) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFCS(KLON) 
REAL(KIND=JPRB)   ,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DLLS5(KLON),ZDLLW5(KLON),ZDLS5(KLON)&
 & ,ZXDRO5(KLON),ZXSRO5(KLON),ZDLDQ5(KLON),ZFQ5(KLON)&
 & ,ZFENT5(KLON),ZDSES15(KLON)  

INTEGER(KIND=JPIM) :: JLEV, JLON

REAL(KIND=JPRB) :: ZCPVMD, ZCPVMS, ZCPVMW, ZDLDQ, ZELIM1, ZELIM2,&
 & ZEPS1, ZFENT, ZFQ, ZGDT, ZGDTI, ZMUL1, ZMUL2, &
 & ZPUL, ZQCORP, ZZ0, ZZ1  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"

!     CHECK RELIABILITY OF INPUT ARGUMENTS.

IF (LHOOK) CALL DR_HOOK('ACDIFSPAD',0,ZHOOK_HANDLE)
IF(CDLOCK /= 'DIFSPAD') CALL ABOR1('BAD NUMBER OF ARGUMENTS!')

!*
!     ------------------------------------------------------------------
!     I - AUXILIARY CONSTANTS.

ZCPVMD=RCPV-RCPD
ZCPVMW=RCPV-RCW
ZCPVMS=RCPV-RCS

!*
!     ------------------------------------------------------------------
!     II - COMPUTATION OF DERIVED PARAMETERS AND SECURITY CONSTANT (FOR
!     CSDT).

ZGDT=RG*TSPHY
ZGDTI=1.0_JPRB/ZGDT
ZPUL=2.0_JPRB*RPI/RDAY

ZEPS1=1.E-08_JPRB

ZZ0=0.0_JPRB
ZZ1=1.0_JPRB

!*
!     ------------------------------------------------------------------
!     0. 3 - Computation of specific heat at constant pressure for air
!     Cp and at the surface Cps

! - TEMPORARY 1D.

! ZCPS1    : VALUE OF Cp AT THE SURFACE

! For the trajectory
! ------------------
! - TEMPORARY 2D.

! ZCP5     : VALUE OF ATMOSPHERIC Cp

DO JLON=KIDIA,KFDIA
  ZCPS1(JLON)=RCPD+ZCPVMD*PQS1(JLON)
ENDDO

DO JLEV=KTDIA,KLEV
  DO JLON=KIDIA,KFDIA
    ZCP5(JLON,JLEV)=RCPD*(1.0_JPRB-PQ5(JLON,JLEV))+RCPV*PQ5(JLON,JLEV)
  ENDDO
ENDDO

!*
!     ------------------------------------------------------------------
!     0. 4 - Preliminary computations of dry static energy, specific
!     humidity corrected of its negative values through upwards pumping
!     (expect in the case of the lowest level assumed to posses a
!     fictitious source at the surface) and of layers' characteristics
!     in the units of the normalized exchange coefficients (kg/m**2/s)
!     for "POID" and its inverse for "IPOI".

! For the trajectory
! ------------------
! - TEMPORARY(S) 2D (0:KLEV) .

! ZXURO5    : PKUROV MODIFIED TO ATTENUATE FIBRILLATIONS.
! ZFCQNG5   : PSEUDO-FLUX OF WATER TO CORRECT FOR Q<0.

! - TEMPORARY(S) 2D (1:KLEV) .

! ZPOID5    : DP/(RG*DT) FOR A GIVEN LAYER AND A GIVEN TIME STEP.
! ZIPOI5    : INVERSE OF ZPOID.
! ZDSE5     : LOCAL ARRAY FOR DRY STATIC ENERGIES.
! ZQCOR5    : LOCAL ARRAY FOR Q AFTER CORRECTION OF NEGATIVE VALUES.

! - TEMPORARY 1D.

! ZXDRO5    : PCDROV MODIFIED TO ATTENUATE FIBRILLATIONS.
! ZDSES15   : LOCAL ARRAY FOR DRY STATIC ENERGIES AT THE SURFACE.

DO JLON=KIDIA,KFDIA
  ZFCQNG5(JLON,KTDIA-1)=0.0_JPRB
  ZDSES15(JLON)=ZCPS1(JLON)*PTS1(JLON)+PAPHI5(JLON,KLEV)
ENDDO

DO JLEV=KTDIA,KLEV
  DO JLON=KIDIA,KFDIA
    ZPOID5(JLON,JLEV)=PDELP5(JLON,JLEV)*ZGDTI
    ZIPOI5(JLON,JLEV)=PRDELP5(JLON,JLEV)*ZGDT
    ZDSE5(JLON,JLEV)=ZCP5(JLON,JLEV)*PT5(JLON,JLEV)+PAPHIF5(JLON,JLEV)

    ZQCORP5(JLON,JLEV)=PQ5(JLON,JLEV)+ZFCQNG5(JLON,JLEV-1)*ZIPOI5(JLON,JLEV)
    ZFCQNG5(JLON,JLEV)=-MAX(ZZ0,-ZQCORP5(JLON,JLEV))*ZPOID5(JLON,JLEV)
    ZQCOR5(JLON,JLEV)=ZQCORP5(JLON,JLEV)+MAX(ZZ0,-ZQCORP5(JLON,JLEV))
  ENDDO
ENDDO

!     Potential "anti/fibrillation" amplification of the exchange
!     coefficients.

DO JLON=KIDIA,KFDIA
  ZXDRO5(JLON)=PCDROV5(JLON)*PXDROV5(JLON)
ENDDO

DO JLEV=KTDIA,KLEV-1
  DO JLON=KIDIA,KFDIA
    ZXURO5(JLON,JLEV)=PKUROV5(JLON,JLEV)*PXUROV5(JLON,JLEV)

  ENDDO
ENDDO

!*
!     ------------------------------------------------------------------
!     0. 5  - Computations for momentum / elimination part. In all the
!     following one will use ZELIM1 and ZELIM2 (1 for the couple U,V
!     and 2 for the couple S,Q) to name the subdiagonal element by
!     which one should multiply the preceeding line to eliminate it by
!     substraction and ZMUL1 and ZMUL2 (1 for the couple U,V and
!     2 for the couple S,Q) to name the multyplying factor to put the
!     main diagonal to "1". ZSUB1 and ZSUB2 will contain the modified
!     superdiagonal coefficients that will be used in the substitution
!     (1 for the couple U,V and 2 for Q,S) and ZN1, ZN2, ZN3 and ZN4
!     will contain the right hand sides due to become the final values
!     (1 for U, 2 for V, 3 for S and 4 for Q).

! For the trajectory
! ------------------
! - TEMPORARY(S) 2D (1:KLEV) .

! ZSUB15    : ARRAY OF COEFFICIENTS FOR THE U,V SUBSTITUTION.
! ZSUB25    : ARRAY OF COEFFICIENTS FOR THE S,Q SUBSTITUTION.
! ZN15      : ARRAY OF RIGH HAND SIDES FOR U.
! ZN25      : ARRAY OF RIGHT HAND SIDES FOR V.
! ZN35      : ARRAY OF RIGH HAND SIDES FOR S (S=CP*T+PHI).
! ZN45      : ARRAY OF RIGHT HAND SIDES FOR Q.

!     Elimination at the top.

DO JLON=KIDIA,KFDIA
  ZMUL15(JLON,KTDIA)=1.0_JPRB/(1.0_JPRB+ZXURO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA))
  ZSUB15(JLON,KTDIA)=ZMUL15(JLON,KTDIA)*ZXURO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA)

  ZN15(JLON,KTDIA)=ZMUL15(JLON,KTDIA)*PU5(JLON,KTDIA)
  ZN25(JLON,KTDIA)=ZMUL15(JLON,KTDIA)*PV5(JLON,KTDIA)
ENDDO

!     Elimination for a standard level.

DO JLEV=KTDIA+1,KLEV-1
  DO JLON=KIDIA,KFDIA
    ZELIM15(JLON,JLEV)=ZXURO5(JLON,JLEV-1)*ZIPOI5(JLON,JLEV)
    ZMUL15(JLON,JLEV)=1.0_JPRB/(1.0_JPRB+ZELIM15(JLON,JLEV)&
     & *(1.0_JPRB-ZSUB15(JLON,JLEV-1))+ZXURO5(JLON,JLEV)&
     & *ZIPOI5(JLON,JLEV))  
    ZSUB15(JLON,JLEV)=ZMUL15(JLON,JLEV)*ZXURO5(JLON,JLEV)*ZIPOI5(JLON,JLEV)

    ZN15(JLON,JLEV)=ZMUL15(JLON,JLEV)*(PU5(JLON,JLEV)&
     & +ZELIM15(JLON,JLEV)*ZN15(JLON,JLEV-1))  
    ZN25(JLON,JLEV)=ZMUL15(JLON,JLEV)*(PV5(JLON,JLEV)&
     & +ZELIM15(JLON,JLEV)*ZN25(JLON,JLEV-1))  
  ENDDO
ENDDO

!*
!     ------------------------------------------------------------------
!     0. 6 - Computation for momentum: surface condition, "Charnock-type"
!     calculation then substitution with fluxes' computation at all
!     levels.

!     Surface calculations.

DO JLON=KIDIA,KFDIA
  ZELIM15(JLON,KLEV)=ZXURO5(JLON,KLEV-1)*ZIPOI5(JLON,KLEV)
  ZMUL15(JLON,KLEV)=1.0_JPRB/(1.0_JPRB+ZELIM15(JLON,KLEV)&
   & *(1.0_JPRB-ZSUB15(JLON,KLEV-1))+ZXDRO5(JLON)&
   & *ZIPOI5(JLON,KLEV))  

  ZN15(JLON,KLEV)=ZMUL15(JLON,KLEV)*(PU5(JLON,KLEV)&
   & +ZELIM15(JLON,KLEV)*ZN15(JLON,KLEV-1))  
  ZN25(JLON,KLEV)=ZMUL15(JLON,KLEV)*(PV5(JLON,KLEV)&
   & +ZELIM15(JLON,KLEV)*ZN25(JLON,KLEV-1))  

  PSTRTU5(JLON,KLEV)=PCDROV5(JLON)*ZN15(JLON,KLEV)
  PSTRTV5(JLON,KLEV)=PCDROV5(JLON)*ZN25(JLON,KLEV)
ENDDO

!     Back-substitution for a standard layer and at the top.

DO JLEV=KLEV-1,KTDIA,-1
  DO JLON=KIDIA,KFDIA
    IF (JLEV == (KLEV-1)) THEN
      ZNB15(JLON,JLEV)=ZN15(JLON,JLEV)+ZSUB15(JLON,JLEV)*ZN15(JLON,JLEV+1)
      ZNB25(JLON,JLEV)=ZN25(JLON,JLEV)+ZSUB15(JLON,JLEV)*ZN25(JLON,JLEV+1)

      PSTRTU5(JLON,JLEV)=PKUROV5(JLON,JLEV)*(ZNB15(JLON,JLEV)&
       & -ZN15(JLON,JLEV+1))  
      PSTRTV5(JLON,JLEV)=PKUROV5(JLON,JLEV)*(ZNB25(JLON,JLEV)&
       & -ZN25(JLON,JLEV+1))  
    ELSE
      ZNB15(JLON,JLEV)=ZN15(JLON,JLEV)+ZSUB15(JLON,JLEV)*ZNB15(JLON,JLEV+1)
      ZNB25(JLON,JLEV)=ZN25(JLON,JLEV)+ZSUB15(JLON,JLEV)*ZNB25(JLON,JLEV+1)

      PSTRTU5(JLON,JLEV)=PKUROV5(JLON,JLEV)*(ZNB15(JLON,JLEV)&
       & -ZNB15(JLON,JLEV+1))  
      PSTRTV5(JLON,JLEV)=PKUROV5(JLON,JLEV)*(ZNB25(JLON,JLEV)&
       & -ZNB25(JLON,JLEV+1))  
    ENDIF
  ENDDO
ENDDO

!     Upper boundary condition.

DO JLON=KIDIA,KFDIA
  PSTRTU5(JLON,KTDIA-1)=0.0_JPRB
  PSTRTV5(JLON,KTDIA-1)=0.0_JPRB
ENDDO

!*
!     ------------------------------------------------------------------
!     0. 7 - Preliminary computations at the surface for the coupling of
!     the implicit solutions in S and Q through the TS value (one will
!     have two values, a priori identical, of the normalized CH
!     coeeficient to allow sensitivity studies for sensible/latent
!     heats).

DO JLON=KIDIA,KFDIA

!     Computations corresponding to evaporation/sublimation.

! - TEMPORARY(S) 1D .

! ZPN       : PROPORTION OF THE SURFACE EVAPORATING IN ICE PHASE.

  IF (LNEIGE) THEN
    ZPN(JLON)=PLSM(JLON)*PNEIJ(JLON)+(1.0_JPRB-PLSM(JLON))&
     & *MAX(ZZ0,SIGN(ZZ1,TMERGL-PTS(JLON)))  
  ELSE
    ZPN(JLON)=0.0_JPRB
  ENDIF

!     Computation of the weighted latent heat and of its two components
!     through calls to the defined function.

! - TEMPORARY(S) 1D .

! ZLHW      : CONTRIBUTION OF THE LIQUID PHASE TO THE LATENT HEAT.
! ZLHS      : CONTRIBUTION OF THE ICE PHASE TO THE LATENT HEAT.

  ZLHW(JLON)=(1.0_JPRB-ZPN(JLON))*RV * (&
   & ( RBETW+ZZ0*RBETD )&
   & - ( RGAMW+ZZ0*RGAMD ) * PTS(JLON))  
  ZLHS(JLON)=ZPN(JLON)*RV * (&
   & ( RBETW+ZZ1*RBETD )&
   & - ( RGAMW+ZZ1*RGAMD ) * PTS(JLON))  
  PLHS(JLON)=ZLHW(JLON)+ZLHS(JLON)

!     Differentiation between the surface exchange coefficients for
!     latent/sensible heat.

!     Potential "anti-fibrillation" amplification of the exchange
!     coefficients.

! For trajectory
! ---------------
! - TEMPORARY(S) 1D .

! ZCHRS5    : EXCHANGE COEFFICIENT PCHROV FOR SENSIBLE HEAT.
! ZXSRO5    : ZCHRS MODIFIED TO ATTENUATE FIBRILLATIONS.

  ZCHRS5(JLON)=PCHROV5(JLON)
  ZXSRO5(JLON)=ZCHRS5(JLON)*PXHROV5(JLON)

!     Contribution to the coefficients of the tridiagonal matrix.

! For trajectory
! --------------
! - TEMPORARY(S) 1D .

! ZDLS5     : TEMPERATURE DERIVATIVE OF THE TOTAL FLUX ; SENSIBLE PART.
! ZDLLW5    : AS ZDLS ; MINUS LATENT PART, LIQUID PHASE.
! ZDLLS5    : AS ZDLS ; MINUS LATENT PART, ICE PHASE.

  ZDLS5(JLON)=(PQS(JLON)-PQ5(JLON,KLEV))*ZCHRS5(JLON)*ZCPVMD
  ZDLLW5(JLON)=(PQS(JLON)-PQ5(JLON,KLEV))*ZCHRS5(JLON)*ZCPVMW*(1.0_JPRB &
   & -ZPN(JLON))  
  ZDLLS5(JLON)=(PQS(JLON)-PQ5(JLON,KLEV))*ZCHRS5(JLON)*ZCPVMS*ZPN(JLON)
  ZDLDQ5(JLON)=ZDLLW5(JLON)+ZDLLS5(JLON)-ZDLS5(JLON)*PTS(JLON)
ENDDO

!*
!     ------------------------------------------------------------------
!     0. 8 - Computation for temperature and specific humidity:
!     elimination part. Like at the surface one differentiates the
!     (a priori equal) normalized coefficients for S and Q.

! The same but for trajectory
! ---------------------------
! - TEMPORARY(S) 2D (0:KLEV) .

! ZKSRV5    : VALUE (DUPLICATED FROM PKTROV) FOR THE S DIFFUSION.
! ZXTRO5    : ZKSRV MODIFIED TO ATTENUATE FIBRILLATIONS.

!     The vertical exchange coefficient for S and Q.

!     Potential "anti-fibrillation" amplification of the exchange
!     coefficient.

DO JLEV=KTDIA,KLEV-1
  DO JLON=KIDIA,KFDIA
    ZKSRV5(JLON,JLEV)=PKTROV5(JLON,JLEV)
    ZXTRO5(JLON,JLEV)=ZKSRV5(JLON,JLEV)*PXTROV5(JLON,JLEV)
  ENDDO
ENDDO

!     Top elimination for the dry static energy, S and for the specific
!     humidity corrected from negative values, Q.

DO JLON=KIDIA,KFDIA
  ZMUL25(JLON,KTDIA)=1.0_JPRB/(1.0_JPRB+ZXTRO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA))
  ZSUB25(JLON,KTDIA)=ZMUL25(JLON,KTDIA)*ZXTRO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA)

  ZN35(JLON,KTDIA)=ZMUL25(JLON,KTDIA)*ZDSE5(JLON,KTDIA)
  ZN45(JLON,KTDIA)=ZMUL25(JLON,KTDIA)*ZQCOR5(JLON,KTDIA)
ENDDO

!     Elimination for a standard layer for S and Q.

DO JLEV=KTDIA+1,KLEV-1
  DO JLON=KIDIA,KFDIA
    ZELIM25(JLON,JLEV)=ZXTRO5(JLON,JLEV-1)*ZIPOI5(JLON,JLEV)
    ZMUL25(JLON,JLEV)=1.0_JPRB/(1.0_JPRB+ZELIM25(JLON,JLEV)&
     & *(1.0_JPRB-ZSUB25(JLON,JLEV-1))+ZXTRO5(JLON,JLEV)&
     & *ZIPOI5(JLON,JLEV))  
    ZSUB25(JLON,JLEV)=ZMUL25(JLON,JLEV)*ZXTRO5(JLON,JLEV)*ZIPOI5(JLON,JLEV)

    ZN35(JLON,JLEV)=ZMUL25(JLON,JLEV)*(ZDSE5(JLON,JLEV)&
     & +ZELIM25(JLON,JLEV)*ZN35(JLON,JLEV-1))  
    ZN45(JLON,JLEV)=ZMUL25(JLON,JLEV)*(ZQCOR5(JLON,JLEV)&
     & +ZELIM25(JLON,JLEV)*ZN45(JLON,JLEV-1))  
  ENDDO
ENDDO

!     Surface elimination calculation for S and Q.

DO JLON=KIDIA,KFDIA
  ZELIM25(JLON,KLEV)=ZXTRO5(JLON,KLEV-1)*ZIPOI5(JLON,KLEV)
  ZMUL25(JLON,KLEV)=1.0_JPRB/(1.0_JPRB+ZELIM25(JLON,KLEV)&
   & *(1.0_JPRB-ZSUB25(JLON,KLEV-1))&
   & +ZXSRO5(JLON)*ZIPOI5(JLON,KLEV))  

  ZN35(JLON,KLEV)=ZMUL25(JLON,KLEV)*(ZDSE5(JLON,KLEV)&
   & +ZXSRO5(JLON)*ZIPOI5(JLON,KLEV)*ZDSES15(JLON)&
   & +ZELIM25(JLON,KLEV)*ZN35(JLON,KLEV-1))  
  ZN45(JLON,KLEV)=ZMUL25(JLON,KLEV)*(ZQCOR5(JLON,KLEV)&
   & +ZXSRO5(JLON)*ZIPOI5(JLON,KLEV)*PQS1(JLON)&
   & +ZELIM25(JLON,KLEV)*ZN45(JLON,KLEV-1))  
ENDDO

!*
!     ------------------------------------------------------------------
!     0. 9 - Computations for temperature and specific humidity :
!     back-substitution part and calculation of fluxes at all levels as
!     well as at the surface and in the soil.

!     Surface and soil fluxes.

DO JLON=KIDIA,KFDIA
  ZFQ5(JLON)=ZCHRS5(JLON)*(ZN45(JLON,KLEV)-PQS1(JLON))

  ZFENT5(JLON)=ZCHRS5(JLON)*(ZN35(JLON,KLEV)-ZDSES15(JLON))

  PDIFTQ5(JLON,KLEV)=ZFQ5(JLON)+ZFCQNG5(JLON,KLEV)
  PDIFTS5(JLON,KLEV)=ZFENT5(JLON)
ENDDO

!     Back-substitution for a standard level and at the top for S and Q.
!     Atmospheric fluxes.

DO JLEV=KLEV-1,KTDIA,-1
  DO JLON=KIDIA,KFDIA
    IF (JLEV == (KLEV-1)) THEN
      ZNB35(JLON,JLEV)=ZN35(JLON,JLEV)+ZSUB25(JLON,JLEV)*ZN35(JLON,JLEV+1)

      ZNB45(JLON,JLEV)=ZN45(JLON,JLEV)+ZSUB25(JLON,JLEV)*ZN45(JLON,JLEV+1)
    ELSE
      ZNB35(JLON,JLEV)=ZN35(JLON,JLEV)+ZSUB25(JLON,JLEV)*ZNB35(JLON,JLEV+1)

      ZNB45(JLON,JLEV)=ZN45(JLON,JLEV)+ZSUB25(JLON,JLEV)*ZNB45(JLON,JLEV+1)
    ENDIF

    IF (JLEV == (KLEV-1)) THEN
      PDIFTQ5(JLON,JLEV)=ZKSRV5(JLON,JLEV)*(ZNB45(JLON,JLEV)&
       & -ZN45(JLON,JLEV+1))+ZFCQNG5(JLON,JLEV)  
      PDIFTS5(JLON,JLEV)=ZKSRV5(JLON,JLEV)*(ZNB35(JLON,JLEV)&
       & -ZN35(JLON,JLEV+1))  
    ELSE
      PDIFTQ5(JLON,JLEV)=ZKSRV5(JLON,JLEV)*(ZNB45(JLON,JLEV)&
       & -ZNB45(JLON,JLEV+1))+ZFCQNG5(JLON,JLEV)  
      PDIFTS5(JLON,JLEV)=ZKSRV5(JLON,JLEV)*(ZNB35(JLON,JLEV)&
       & -ZNB35(JLON,JLEV+1))  
    ENDIF
  ENDDO
ENDDO

!     Upper boundary condition.

DO JLON=KIDIA,KFDIA
  PDIFTQ5(JLON,KTDIA-1)=0.0_JPRB
  PDIFTS5(JLON,KTDIA-1)=0.0_JPRB
ENDDO

!*
!     INITIALIZATION OF THE LOCAL VARIABLES

ZCHRS(:)=0.0_JPRB
ZDLLS(:)=0.0_JPRB
ZDLLW(:)=0.0_JPRB
ZDLS(:)=0.0_JPRB
ZXDRO(:)=0.0_JPRB
ZXSRO(:)=0.0_JPRB
ZDSES1(:)=0.0_JPRB

ZDSE(:,:)=0.0_JPRB
ZIPOI(:,:)=0.0_JPRB
ZPOID(:,:)=0.0_JPRB
ZN1(:,:)=0.0_JPRB
ZN2(:,:)=0.0_JPRB
ZN3(:,:)=0.0_JPRB
ZN4(:,:)=0.0_JPRB
ZQCOR(:,:)=0.0_JPRB
ZSUB1(:,:)=0.0_JPRB
ZSUB2(:,:)=0.0_JPRB
ZCP(:,:)=0.0_JPRB

ZKSRV(:,:)=0.0_JPRB
ZXTRO(:,:)=0.0_JPRB
ZXURO(:,:)=0.0_JPRB
ZFCQNG(:,:)=0.0_JPRB

!*
!     ------------------------------------------------------------------
!     IX - COMPUTATIONS FOR TEMPERATURE AND SPECIFIC HUMIDITY :
!     BACK-SUBSTITUTION PART AND CALCULATION OF FLUXES AT ALL LEVELS AS
!     WELL AS AT THE SURFACE AND IN THE SOIL.

!     UPPER BOUNDARY CONDITION.

DO JLON=KIDIA,KFDIA
  PDIFTS(JLON,KTDIA-1)=0.0_JPRB
  PDIFTQ(JLON,KTDIA-1)=0.0_JPRB
ENDDO

!     ATMOSPHERIC FLUXES.
!     BACK-SUBSTITUTION FOR A STANDART LEVEL AND AT THE TOP FOR S AND Q.

DO JLEV=KTDIA,KLEV-1
  DO JLON=KIDIA,KFDIA
    IF (JLEV == (KLEV-1)) THEN
      ZKSRV(JLON,JLEV)=ZKSRV(JLON,JLEV)&
       & +(ZNB35(JLON,JLEV)-ZN35(JLON,JLEV+1))&
       & *PDIFTS(JLON,JLEV)  
    ELSE
      ZKSRV(JLON,JLEV)=ZKSRV(JLON,JLEV)&
       & +(ZNB35(JLON,JLEV)-ZNB35(JLON,JLEV+1))&
       & *PDIFTS(JLON,JLEV)  
    ENDIF
    ZN3(JLON,JLEV)=ZN3(JLON,JLEV)+ZKSRV5(JLON,JLEV)*PDIFTS(JLON,JLEV)
    ZN3(JLON,JLEV+1)=ZN3(JLON,JLEV+1)-ZKSRV5(JLON,JLEV)*PDIFTS(JLON,JLEV)
    PDIFTS(JLON,JLEV)=0.0_JPRB

    IF (JLEV == (KLEV-1)) THEN
      ZKSRV(JLON,JLEV)=ZKSRV(JLON,JLEV)&
       & +(ZNB45(JLON,JLEV)-ZN45(JLON,JLEV+1))&
       & *PDIFTQ(JLON,JLEV)  
    ELSE
      ZKSRV(JLON,JLEV)=ZKSRV(JLON,JLEV)&
       & +(ZNB45(JLON,JLEV)-ZNB45(JLON,JLEV+1))&
       & *PDIFTQ(JLON,JLEV)  
    ENDIF
    ZN4(JLON,JLEV)=ZN4(JLON,JLEV)+ZKSRV5(JLON,JLEV)*PDIFTQ(JLON,JLEV)
    ZN4(JLON,JLEV+1)=ZN4(JLON,JLEV+1)-ZKSRV5(JLON,JLEV)*PDIFTQ(JLON,JLEV)
    ZFCQNG(JLON,JLEV)=ZFCQNG(JLON,JLEV)+PDIFTQ(JLON,JLEV)
    PDIFTQ(JLON,JLEV)=0.0_JPRB

    IF (JLEV == (KLEV-1)) THEN
      ZSUB2(JLON,JLEV)=ZSUB2(JLON,JLEV)+ZN45(JLON,JLEV+1)*ZN4(JLON,JLEV)
    ELSE
      ZSUB2(JLON,JLEV)=ZSUB2(JLON,JLEV)+ZNB45(JLON,JLEV+1)*ZN4(JLON,JLEV)
    ENDIF
    ZN4(JLON,JLEV+1)=ZN4(JLON,JLEV+1)+ZSUB25(JLON,JLEV)*ZN4(JLON,JLEV)

    IF (JLEV == (KLEV-1)) THEN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JLON)=0.0_JPRB

  ZFQ=ZFQ+(1.0_JPRB-ZPN(JLON))*PFEVL(JLON)
  PFEVL(JLON)=0.0_JPRB

  ZCHRS(JLON)=ZCHRS(JLON)+(ZN45(JLON,KLEV)-PQS1(JLON))*ZFQ
  ZN4(JLON,KLEV)=ZN4(JLON,KLEV)+ZCHRS5(JLON)*ZFQ

ENDDO

!*
!     ------------------------------------------------------------------
!     VIII - COMPUTATION FOR TEMPERATURE AND SPECIFIC HUMIDITY :
!     ELIMINATION PART. LIKE AT THE SURFACE ONE DIFFERENTIATES THE
!     (A PRIORI EQUAL) NORMALIZED COEFFICIENTS FOR S AND Q.

! - TEMPORARY(S) 2D (0:KLEV) .

! ZKSRV     : VALUE (DUPLICATED FROM PKTROV) FOR THE S DIFFUSION.
! ZXTRO     : ZKSRV MODIFIED TO ATTENUATE FIBRILLATIONS.

!     SURFACE ELIMINATION CALCULATION FOR S AND Q.

DO JLON=KIDIA,KFDIA
  ZMUL2=0.0_JPRB
  ZELIM2=0.0_JPRB
  ZMUL2=ZMUL2+(ZQCOR5(JLON,KLEV)&
   & +ZXSRO5(JLON)*ZIPOI5(JLON,KLEV)*PQS1(JLON)&
   & +ZELIM25(JLON,KLEV)*ZN45(JLON,KLEV-1))*ZN4(JLON,KLEV)  
  ZQCOR(JLON,KLEV)=ZQCOR(JLON,KLEV)+ZMUL25(JLON,KLEV)*ZN4(JLON,KLEV)
  ZIPOI(JLON,KLEV)=ZIPOI(JLON,KLEV)+PQS1(JLON)*ZMUL25(JLON,KLEV)&
   & *ZXSRO5(JLON)*ZN4(JLON,KLEV)  
  ZXSRO(JLON)=ZXSRO(JLON)+PQS1(JLON)*ZMUL25(JLON,KLEV)&
   & *ZIPOI5(JLON,KLEV)*ZN4(JLON,KLEV)  
  ZN4(JLON,KLEV-1)=ZN4(JLON,KLEV-1)+ZMUL25(JLON,KLEV)&
   & *ZELIM25(JLON,KLEV)*ZN4(JLON,KLEV)  
  ZELIM2=ZELIM2+ZMUL25(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(JLON,KLEV))**2  
  ZIPOI(JLON,KLEV)=ZIPOI(JLON,KLEV)-ZXSRO5(JLON)*ZMUL2 &
   & /(1.0_JPRB+ZELIM25(JLON,KLEV)*(1.0_JPRB-ZSUB25(JLON,KLEV-1))&
   & +ZXSRO5(JLON)*ZIPOI5(JLON,KLEV))**2  
  ZXSRO(JLON)=ZXSRO(JLON)-ZIPOI5(JLON,KLEV)*ZMUL2 &
   & /(1.0_JPRB+ZELIM25(JLON,KLEV)*(1.0_JPRB-ZSUB25(JLON,KLEV-1))&
   & +ZXSRO5(JLON)*ZIPOI5(JLON,KLEV))**2  

  ZIPOI(JLON,KLEV)=ZIPOI(JLON,KLEV)+ZXTRO5(JLON,KLEV-1)*ZELIM2
  ZXTRO(JLON,KLEV-1)=ZXTRO(JLON,KLEV-1)+ZIPOI5(JLON,KLEV)*ZELIM2
ENDDO

!     ELIMINATION FOR A STANDART LAYER FOR S AND Q.

DO JLEV=KLEV-1,KTDIA+1,-1
  DO JLON=KIDIA,KFDIA
    ZMUL2=0.0_JPRB
    ZELIM2=0.0_JPRB
    ZMUL2=ZMUL2+(ZQCOR5(JLON,JLEV)+ZELIM25(JLON,JLEV)&
     & *ZN45(JLON,JLEV-1))*ZN4(JLON,JLEV)  
    ZQCOR(JLON,JLEV)=ZQCOR(JLON,JLEV)+ZMUL25(JLON,JLEV)*ZN4(JLON,JLEV)
    ZN4(JLON,JLEV-1)=ZN4(JLON,JLEV-1)+ZMUL25(JLON,JLEV)&
     & *ZELIM25(JLON,JLEV)*ZN4(JLON,JLEV)  
    ZELIM2=ZELIM2+ZMUL25(JLON,JLEV)*ZN45(JLON,JLEV-1)*ZN4(JLON,JLEV)
    ZN4(JLON,JLEV)=0.0_JPRB

    ZMUL2=ZMUL2+(ZDSE5(JLON,JLEV)+ZELIM25(JLON,JLEV)&
     & *ZN35(JLON,JLEV-1))*ZN3(JLON,JLEV)  
    ZDSE(JLON,JLEV)=ZDSE(JLON,JLEV)+ZMUL25(JLON,JLEV)*ZN3(JLON,JLEV)
    ZN3(JLON,JLEV-1)=ZN3(JLON,JLEV-1)+ZMUL25(JLON,JLEV)&
     & *ZELIM25(JLON,JLEV)*ZN3(JLON,JLEV)  
    ZELIM2=ZELIM2+ZMUL25(JLON,JLEV)*ZN35(JLON,JLEV-1)*ZN3(JLON,JLEV)
    ZN3(JLON,JLEV)=0.0_JPRB

    ZMUL2=ZMUL2+ZXTRO5(JLON,JLEV)*ZIPOI5(JLON,JLEV)*ZSUB2(JLON,JLEV)
    ZXTRO(JLON,JLEV)=ZXTRO(JLON,JLEV)+ZMUL25(JLON,JLEV)&
     & *ZIPOI5(JLON,JLEV)*ZSUB2(JLON,JLEV)  
    ZIPOI(JLON,JLEV)=ZIPOI(JLON,JLEV)+ZMUL25(JLON,JLEV)&
     & *ZXTRO5(JLON,JLEV)*ZSUB2(JLON,JLEV)  
    ZSUB2(JLON,JLEV)=0.0_JPRB

    ZSUB2(JLON,JLEV-1)=ZSUB2(JLON,JLEV-1)+ZELIM25(JLON,JLEV)*ZMUL2 &
     & /(1.0_JPRB+ZELIM25(JLON,JLEV)*(1.0_JPRB-ZSUB25(JLON,JLEV-1))&
     & +ZXTRO5(JLON,JLEV)*ZIPOI5(JLON,JLEV))**2  
    ZELIM2=ZELIM2-(1.0_JPRB-ZSUB25(JLON,JLEV-1))*ZMUL2 &
     & /(1.0_JPRB+ZELIM25(JLON,JLEV)*(1.0_JPRB-ZSUB25(JLON,JLEV-1))&
     & +ZXTRO5(JLON,JLEV)*ZIPOI5(JLON,JLEV))**2  
    ZIPOI(JLON,JLEV)=ZIPOI(JLON,JLEV)-ZXTRO5(JLON,JLEV)*ZMUL2 &
     & /(1.0_JPRB+ZELIM25(JLON,JLEV)*(1.0_JPRB-ZSUB25(JLON,JLEV-1))&
     & +ZXTRO5(JLON,JLEV)*ZIPOI5(JLON,JLEV))**2  
    ZXTRO(JLON,JLEV)=ZXTRO(JLON,JLEV)-ZIPOI5(JLON,JLEV)*ZMUL2 &
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5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA))**2  
  ZXTRO(JLON,KTDIA)=ZXTRO(JLON,KTDIA)-ZIPOI5(JLON,KTDIA)*ZMUL2 &
   & /(1.0_JPRB+ZXTRO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA))**2  
ENDDO

!     THE VERTICAL EXCHANGE COEFFICIENT FOR S AND Q.

!     POTENTIAL "ANTI-FIBRILLATION" AMPLIFICATION OF THE EXCHANGE
!     COEFFICIENT.

DO JLEV=KTDIA,KLEV-1
  DO JLON=KIDIA,KFDIA
    PXTROV(JLON,JLEV)=PXTROV(JLON,JLEV)+ZKSRV5(JLON,JLEV)*ZXTRO(JLON,JLEV)
    ZKSRV(JLON,JLEV)=ZKSRV(JLON,JLEV)+PXTROV5(JLON,JLEV)*ZXTRO(JLON,JLEV)
    ZXTRO(JLON,JLEV)=0.0_JPRB

    PKTROV(JLON,JLEV)=PKTROV(JLON,JLEV)+ZKSRV(JLON,JLEV)
    ZKSRV(JLON,JLEV)=0.0_JPRB
  ENDDO
ENDDO

!*
!     ------------------------------------------------------------------
!     VII - PRELIMINARY COMPUTATIONS AT THE SURFACE FOR THE COUPLING OF
!     THE IMPLICIT SOLUTIONS IN S AND Q THROUGH THE TS VALUE (ONE WILL
!     HAVE TWO VALUES, A PRIORI IDENTICAL, OF THE NORMALIZED CH
!     COEFFICIENT TO ALLOW SENSITIVITY STUDIES FOR SENSIBLE/LATENT
!     HEATS).

DO JLON=KIDIA,KFDIA
  ZDLDQ=0.0_JPRB

!     CONTRIBUTION TO THE COEFFICIENTS OF THE TRIDIAGONAL MATRIX.

! - TEMPORARY(S) 1D .

! ZDLS      : TEMPERATURE DERIVATIVE OF THE TOTAL FLUX ; SENSIBLE PART.
! ZDLLW     : AS ZDLS ; MINUS LATENT PART, LIQUID PHASE.
! ZDLLS     : AS ZDLS ; MINUS LATENT PART, ICE PHASE.

  ZDLLW(JLON)=ZDLLW(JLON)+ZDLDQ
  ZDLLS(JLON)=ZDLLS(JLON)+ZDLDQ
  ZDLS(JLON)=ZDLS(JLON)-PTS(JLON)*ZDLDQ

  ZCHRS(JLON)=ZCHRS(JLON)+(PQS(JLON)-PQ5(JLON,KLEV))*ZCPVMS &
   & *ZPN(JLON)*ZDLLS(JLON)  
  PQ(JLON,KLEV)=PQ(JLON,KLEV)-ZCHRS5(JLON)*ZCPVMS*ZPN(JLON)*ZDLLS(JLON)
  ZDLLS(JLON)=0.0_JPRB

  ZCHRS(JLON)=ZCHRS(JLON)+(PQS(JLON)-PQ5(JLON,KLEV))*ZCPVMW &
   & *(1.0_JPRB-ZPN(JLON))*ZDLLW(JLON)  
  PQ(JLON,KLEV)=PQ(JLON,KLEV)-ZCPVMW*(1.0_JPRB-ZPN(JLON))*ZCHRS5(JLON)&
   & *ZDLLW(JLON)  
  ZDLLW(JLON)=0.0_JPRB

  ZCHRS(JLON)=ZCHRS(JLON)+(PQS(JLON)-PQ5(JLON,KLEV))*ZCPVMD*ZDLS(JLON)
  PQ(JLON,KLEV)=PQ(JLON,KLEV)-ZCPVMD*ZCHRS5(JLON)*ZDLS(JLON)
  ZDLS(JLON)=0.0_JPRB

!     DIFFERENTIATION BETWEEN THE SURFACE EXCHANGE COEFFICIENTS FOR
!     LATENT/SENSIBLE HEAT.

!     POTENTIAL "ANTI-FIBRILLATION" AMPLIFICATION OF THE EXCHANGE
!     COEFFICIENTS.

! - TEMPORARY(S) 1D .

! ZCHRS     : EXCHANGE COEFFICIENT PCHROV FOR SENSIBLE HEAT.
! ZXSRO     : ZCHRS MODIFIED TO ATTENUATE FIBRILLATIONS.

  PXHROV(JLON)=PXHROV(JLON)+ZCHRS5(JLON)*ZXSRO(JLON)
  ZCHRS(JLON)=ZCHRS(JLON)+PXHROV5(JLON)*ZXSRO(JLON)
  ZXSRO(JLON)=0.0_JPRB

  PCHROV(JLON)=PCHROV(JLON)+ZCHRS(JLON)
  ZCHRS(JLON)=0.0_JPRB
ENDDO

!*
!     ------------------------------------------------------------------
!     VI - COMPUTATION FOR MOMENTUM: SURFACE CONDITION, "CHARNOCK-TYPE"
!     CALCULATION THEN SUBSTITUTION WITH FLUXES' COMPUTATION AT ALL
!     LEVELS.

!     UPPER BOUNDARY CONDITION.

DO JLON=KIDIA,KFDIA
  PSTRTV(JLON,KTDIA-1)=0.0_JPRB
  PSTRTU(JLON,KTDIA-1)=0.0_JPRB
ENDDO

!     BACK-SUBSTITUTION FOR A STANDART LAYER AND AT THE TOP.

DO JLEV=KTDIA,KLEV-1
  DO JLON=KIDIA,KFDIA
    IF (JLEV == (KLEV-1)) THEN
      PKUROV(JLON,JLEV)=PKUROV(JLON,JLEV)&
       & +(ZNB25(JLON,JLEV)-ZN25(JLON,JLEV+1))*PSTRTV(JLON,JLEV)  
    ELSE
      PKUROV(JLON,JLEV)=PKUROV(JLON,JLEV)&
       & +(ZNB25(JLON,JLEV)-ZNB25(JLON,JLEV+1))*PSTRTV(JLON,JLEV)  
    ENDIF
    ZN2(JLON,JLEV)=ZN2(JLON,JLEV)+PKUROV5(JLON,JLEV)*PSTRTV(JLON,JLEV)
    ZN2(JLON,JLEV+1)=ZN2(JLON,JLEV+1)-PKUROV5(JLON,JLEV)*PSTRTV(JLON,JLEV)
    PSTRTV(JLON,JLEV)=0.0_JPRB

    IF (JLEV == (KLEV-1)) THEN
      PKUROV(JLON,JLEV)=PKUROV(JLON,JLEV)&
       & +(ZNB15(JLON,JLEV)-ZN15(JLON,JLEV+1))*PSTRTU(JLON,JLEV)  
    ELSE
      PKUROV(JLON,JLEV)=PKUROV(JLON,JLEV)&
       & +(ZNB15(JLON,JLEV)-ZNB15(JLON,JLEV+1))*PSTRTU(JLON,JLEV)  
    ENDIF
    ZN1(JLON,JLEV)=ZN1(JLON,JLEV)+PKUROV5(JLON,JLEV)*PSTRTU(JLON,JLEV)
    ZN1(JLON,JLEV+1)=ZN1(JLON,JLEV+1)-PKUROV5(JLON,JLEV)*PSTRTU(JLON,JLEV)
    PSTRTU(JLON,JLEV)=0.0_JPRB

    IF (JLEV == (KLEV-1)) THEN
      ZSUB1(JLON,JLEV)=ZSUB1(JLON,JLEV)+ZN25(JLON,JLEV+1)*ZN2(JLON,JLEV)
    ELSE
      ZSUB1(JLON,JLEV)=ZSUB1(JLON,JLEV)+ZNB25(JLON,JLEV+1)*ZN2(JLON,JLEV)
    ENDIF
    ZN2(JLON,JLEV+1)=ZN2(JLON,JLEV+1)+ZSUB15(JLON,JLEV)*ZN2(JLON,JLEV)

    IF (JLEV == (KLEV-1)) THEN
      ZSUB1(JLON,JLEV)=ZSUB1(JLON,JLEV)+ZN15(JLON,JLEV+1)*ZN1(JLON,JLEV)
    ELSE
      ZSUB1(JLON,JLEV)=ZSUB1(JLON,JLEV)+ZNB15(JLON,JLEV+1)*ZN1(JLON,JLEV)
    ENDIF
    ZN1(JLON,JLEV+1)=ZN1(JLON,JLEV+1)+ZSUB15(JLON,JLEV)*ZN1(JLON,JLEV)
  ENDDO
ENDDO

!     SURFACE CALCULATIONS.

DO JLON=KIDIA,KFDIA
  ZMUL1=0.0_JPRB
  ZELIM1=0.0_JPRB
  ZN2(JLON,KLEV)=ZN2(JLON,KLEV)+PCDROV5(JLON)*PSTRTV(JLON,KLEV)
  PCDROV(JLON)=PCDROV(JLON)+ZN25(JLON,KLEV)*PSTRTV(JLON,KLEV)
  PSTRTV(JLON,KLEV)=0.0_JPRB

  ZN1(JLON,KLEV)=ZN1(JLON,KLEV)+PCDROV5(JLON)*PSTRTU(JLON,KLEV)
  PCDROV(JLON)=PCDROV(JLON)+ZN15(JLON,KLEV)*PSTRTU(JLON,KLEV)
  PSTRTU(JLON,KLEV)=0.0_JPRB

  ZMUL1=ZMUL1+(PV5(JLON,KLEV)+ZELIM15(JLON,KLEV)&
   & *ZN25(JLON,KLEV-1))&
   & *ZN2(JLON,KLEV)  
  PV(JLON,KLEV)=PV(JLON,KLEV)+ZMUL15(JLON,KLEV)*ZN2(JLON,KLEV)
  ZN2(JLON,KLEV-1)=ZN2(JLON,KLEV-1)+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B-ZSUB15(JLON,KLEV-1))&
   & +ZXDRO5(JLON)*ZIPOI5(JLON,KLEV))**2  

  ZIPOI(JLON,KLEV)=ZIPOI(JLON,KLEV)+ZXURO5(JLON,KLEV-1)*ZELIM1
  ZXURO(JLON,KLEV-1)=ZXURO(JLON,KLEV-1)+ZIPOI5(JLON,KLEV)*ZELIM1
ENDDO

!*
!     ------------------------------------------------------------------
!     V - COMPUTATIONS FOR MOMENTUM / ELIMINATION PART. IN ALL THE
!     FOLLOWING ONE WILL USE ZELIM1 AND ZELIM2 (1 FOR THE COUPLE U,V
!     AND 2 FOR THE COUPLE S,Q) TO NAME THE SUBDIAGONAL ELEMENT BY
!     WHICH ONE SHOULD MULTIPLY THE PRECEEDING LINE TO ELIMINATE IT BY
!     SUBSTRACTION AND ZMUL1 AND ZMUL2 (1 FOR THE COUPLE U,V AND
!     2 FOR THE COUPLE S,Q) TO NAME THE MULTIPLYING FACTOR TO PUT THE
!     MAIN DIAGONAL TO "1". ZSUB1 AND ZSUB2 WILL CONTAIN THE MODIFIED
!     SUPERDIAGONAL COEFFICIENTS THAT WILL BE USED IN THE SUBSTITUTION
!     (1 FOR THE COUPLE U,V AND 2 FOR Q,S) AND ZN1, ZN2, ZN3 AND ZN4
!     WILL CONTAIN THE RIGHT HAND SIDES DUE TO BECOME THE FINAL VALUES
!     (1 FOR U, 2 FOR V, 3 FOR S AND 4 FOR Q).

! - TEMPORARY(S) 2D (1:KLEV) .

! ZSUB1     : ARRAY OF COEFFICIENTS FOR THE U,V SUBSTITUTION.
! ZSUB2     : ARRAY OF COEFFICIENTS FOR THE S,Q SUBSTITUTION.
! ZN1       : ARRAY OF RIGH HAND SIDES FOR U.
! ZN2       : ARRAY OF RIGHT HAND SIDES FOR V.
! ZN3       : ARRAY OF RIGH HAND SIDES FOR S (S=CP*T+PHI).
! ZN4       : ARRAY OF RIGHT HAND SIDES FOR Q.

!     ELIMINATION FOR A STANDART LEVEL.

DO JLEV=KLEV-1,KTDIA+1,-1
  DO JLON=KIDIA,KFDIA
    ZMUL1=0.0_JPRB
    ZELIM1=0.0_JPRB
    ZMUL1=ZMUL1+(PV5(JLON,JLEV)+ZELIM15(JLON,JLEV)&
     & *ZN25(JLON,JLEV-1))*ZN2(JLON,JLEV)  
    PV(JLON,JLEV)=PV(JLON,JLEV)+ZMUL15(JLON,JLEV)*ZN2(JLON,JLEV)
    ZN2(JLON,JLEV-1)=ZN2(JLON,JLEV-1)+ZMUL15(JLON,JLEV)&
     & *ZELIM15(JLON,JLEV)*ZN2(JLON,JLEV)  
    ZELIM1=ZELIM1+ZMUL15(JLON,JLEV)*ZN25(JLON,JLEV-1)*ZN2(JLON,JLEV)
    ZN2(JLON,JLEV)=0.0_JPRB

    ZMUL1=ZMUL1+(PU5(JLON,JLEV)+ZELIM15(JLON,JLEV)&
     & *ZN15(JLON,JLEV-1))*ZN1(JLON,JLEV)  
    PU(JLON,JLEV)=PU(JLON,JLEV)+ZMUL15(JLON,JLEV)*ZN1(JLON,JLEV)
    ZN1(JLON,JLEV-1)=ZN1(JLON,JLEV-1)+ZMUL15(JLON,JLEV)&
     & *ZELIM15(JLON,JLEV)*ZN1(JLON,JLEV)  
    ZELIM1=ZELIM1+ZMUL15(JLON,JLEV)*ZN15(JLON,JLEV-1)*ZN1(JLON,JLEV)
    ZN1(JLON,JLEV)=0.0_JPRB

    ZMUL1=ZMUL1+ZXURO5(JLON,JLEV)*ZIPOI5(JLON,JLEV)*ZSUB1(JLON,JLEV)
    ZXURO(JLON,JLEV)=ZXURO(JLON,JLEV)+ZMUL15(JLON,JLEV)&
     & *ZIPOI5(JLON,JLEV)*ZSUB1(JLON,JLEV)  
    ZIPOI(JLON,JLEV)=ZIPOI(JLON,JLEV)+ZMUL15(JLON,JLEV)&
     & *ZXURO5(JLON,JLEV)*ZSUB1(JLON,JLEV)  
    ZSUB1(JLON,JLEV)=0.0_JPRB

    ZSUB1(JLON,JLEV-1)=ZSUB1(JLON,JLEV-1)+ZELIM15(JLON,JLEV)*ZMUL1 &
     & /(1.0_JPRB+ZELIM15(JLON,JLEV)*(1.0_JPRB-ZSUB15(JLON,JLEV-1))&
     & +ZXURO5(JLON,JLEV)*ZIPOI5(JLON,JLEV))**2  
    ZELIM1=ZELIM1-(1.0_JPRB-ZSUB15(JLON,JLEV-1))*ZMUL1 &
     & /(1.0_JPRB+ZELIM15(JLON,JLEV)*(1.0_JPRB-ZSUB15(JLON,JLEV-1))&
     & +ZXURO5(JLON,JLEV)*ZIPOI5(JLON,JLEV))**2  
    ZIPOI(JLON,JLEV)=ZIPOI(JLON,JLEV)-ZXURO5(JLON,JLEV)*ZMUL1 &
     & /(1.0_JPRB+ZELIM15(JLON,JLEV)*(1.0_JPRB-ZSUB15(JLON,JLEV-1))&
     & +ZXURO5(JLON,JLEV)*ZIPOI5(JLON,JLEV))**2  
    ZXURO(JLON,JLEV)=ZXURO(JLON,JLEV)-ZIPOI5(JLON,JLEV)*ZMUL1 &
     & /(1.0_JPRB+ZELIM15(JLON,JLEV)*(1.0_JPRB-ZSUB15(JLON,JLEV-1))&
     & +ZXURO5(JLON,JLEV)*ZIPOI5(JLON,JLEV))**2  

    ZIPOI(JLON,JLEV)=ZIPOI(JLON,JLEV)+ZXURO5(JLON,JLEV-1)*ZELIM1
    ZXURO(JLON,JLEV-1)=ZXURO(JLON,JLEV-1)+ZIPOI5(JLON,JLEV)*ZELIM1
  ENDDO
ENDDO

!     ELIMINATION AT THE TOP.

DO JLON=KIDIA,KFDIA
  ZMUL1=0.0_JPRB
  PV(JLON,KTDIA)=PV(JLON,KTDIA)+ZMUL15(JLON,KTDIA)*ZN2(JLON,KTDIA)
  ZMUL1=ZMUL1+PV5(JLON,KTDIA)*ZN2(JLON,KTDIA)
  ZN2(JLON,KTDIA)=0.0_JPRB

  PU(JLON,KTDIA)=PU(JLON,KTDIA)+ZMUL15(JLON,KTDIA)*ZN1(JLON,KTDIA)
  ZMUL1=ZMUL1+PU5(JLON,KTDIA)*ZN1(JLON,KTDIA)
  ZN1(JLON,KTDIA)=0.0_JPRB

  ZMUL1=ZMUL1+ZXURO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA)*ZSUB1(JLON,KTDIA)
  ZXURO(JLON,KTDIA)=ZXURO(JLON,KTDIA)+ZMUL15(JLON,KTDIA)&
   & *ZIPOI5(JLON,KTDIA)*ZSUB1(JLON,KTDIA)  
  ZIPOI(JLON,KTDIA)=ZIPOI(JLON,KTDIA)+ZMUL15(JLON,KTDIA)&
   & *ZXURO5(JLON,KTDIA)*ZSUB1(JLON,KTDIA)  
  ZSUB1(JLON,KTDIA)=0.0_JPRB

  ZIPOI(JLON,KTDIA)=ZIPOI(JLON,KTDIA)-ZXURO5(JLON,KTDIA)*ZMUL1 &
   & /(1.0_JPRB+ZXURO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA))**2  
  ZXURO(JLON,KTDIA)=ZXURO(JLON,KTDIA)-ZIPOI5(JLON,KTDIA)*ZMUL1 &
   & /(1.0_JPRB+ZXURO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA))**2  
ENDDO

!*
!     ------------------------------------------------------------------
!     IV - PRELIMINARY COMPUTATIONS OF DRY STATIC ENERGY, SPECIFIC
!     HUMIDITY CORRECTED OF ITS NEGATIVE VALUES THROUGH UPWARDS PUMPING
!     (EXCEPT IN THE CASE OF THE LOWEST LEVEL ASSUMED TO POSSES A
!     FICTITIOUS SOURCE AT THE SURFACE) AND OF LAYERS' CHARACTERISTICS
!     IN THE UNITS OF THE NORMALIZED EXCHANGE COEFFICIENTS (KG/M**2/S)
!     FOR "POID" AND ITS INVERSE FOR "IPOI".

! - TEMPORARY(S) 2D (0:KLEV) .

! ZXURO     : PKUROV MODIFIED TO ATTENUATE FIBRILLATIONS.
! ZFCQNG    : PSEUDO-FLUX OF WATER TO CORRECT FOR Q<0.

! - TEMPORARY(S) 2D (1:KLEV) .

! ZPOID     : DP/(RG*DT) FOR A GIVEN LAYER AND A GIVEN TIME STEP.
! ZIPOI     : INVERSE OF ZPOID.
! ZDSE      : LOCAL ARRAY FOR DRY STATIC ENERGIES.
! ZQCOR     : LOCAL ARRAY FOR Q AFTER CORRECTION OF NEGATIVE VALUES.

! - TEMPORARY 1D.

! ZXDRO     : PCDROV MODIFIED TO ATTENUATE FIBRILLATIONS.
! ZDSES     : LOCAL ARRAY FOR DRY STATIC ENERGIES AT THE SURFACE.

!     POTENTIAL "ANTI-FIBRILLATION" AMPLIFICATION OF THE EXCHANGE
!     COEFFICIENTS.

DO JLEV=KTDIA,KLEV-1
  DO JLON=KIDIA,KFDIA
    PXUROV(JLON,JLEV)=PXUROV(JLON,JLEV)+PKUROV5(JLON,JLEV)*ZXURO(JLON,JLEV)
    PKUROV(JLON,JLEV)=PKUROV(JLON,JLEV)+PXUROV5(JLON,JLEV)*ZXURO(JLON,JLEV)
    ZXURO(JLON,JLEV)=0.0_JPRB
  ENDDO
ENDDO

DO JLON=KIDIA,KFDIA
  PXDROV(JLON)=PXDROV(JLON)+PCDROV5(JLON)*ZXDRO(JLON)
  PCDROV(JLON)=PCDROV(JLON)+PXDROV5(JLON)*ZXDRO(JLON)
  ZXDRO(JLON)=0.0_JPRB
ENDDO

DO JLEV=KLEV,KTDIA,-1
  DO JLON=KIDIA,KFDIA
    ZQCORP=0.0_JPRB
    IF ((-ZQCORP5(JLON,JLEV)) > 0.0_JPRB) THEN
      ZQCOR(JLON,JLEV)=0.0_JPRB
    ELSE
      ZQCORP=ZQCORP+ZQCOR(JLON,JLEV)
      ZQCOR(JLON,JLEV)=0.0_JPRB
    ENDIF

    IF ((-ZQCORP5(JLON,JLEV)) > 0.0_JPRB) THEN
      ZPOID(JLON,JLEV)=ZPOID(JLON,JLEV)+ZQCORP5(JLON,JLEV)*ZFCQNG(JLON,JLEV)
      ZQCORP=ZQCORP+ZPOID5(JLON,JLEV)*ZFCQNG(JLON,JLEV)
      ZFCQNG(JLON,JLEV)=0.0_JPRB
    ELSE
      ZFCQNG(JLON,JLEV)=0.0_JPRB
    ENDIF

    PQ(JLON,JLEV)=PQ(JLON,JLEV)+ZQCORP
    ZIPOI(JLON,JLEV)=ZIPOI(JLON,JLEV)+ZFCQNG5(JLON,JLEV-1)*ZQCORP
    ZFCQNG(JLON,JLEV-1)=ZFCQNG(JLON,JLEV-1)+ZIPOI5(JLON,JLEV)*ZQCORP

    PT(JLON,JLEV)=PT(JLON,JLEV)+ZCP5(JLON,JLEV)*ZDSE(JLON,JLEV)
    ZCP(JLON,JLEV)=ZCP(JLON,JLEV)+PT5(JLON,JLEV)*ZDSE(JLON,JLEV)
    PAPHIF(JLON,JLEV)=PAPHIF(JLON,JLEV)+ZDSE(JLON,JLEV)
    ZDSE(JLON,JLEV)=0.0_JPRB

    PRDELP(JLON,JLEV)=PRDELP(JLON,JLEV)+ZGDT*ZIPOI(JLON,JLEV)
    ZIPOI(JLON,JLEV)=0.0_JPRB

    PDELP(JLON,JLEV)=PDELP(JLON,JLEV)+ZGDTI*ZPOID(JLON,JLEV)
    ZPOID(JLON,JLEV)=0.0_JPRB
  ENDDO
ENDDO

DO JLON=KIDIA,KFDIA
  PAPHI(JLON,KLEV)=PAPHI(JLON,KLEV)+ZDSES1(JLON)
  ZDSES1(JLON)=0.0_JPRB
  ZFCQNG(JLON,KTDIA-1)=0.0_JPRB
ENDDO

!*
!     ------------------------------------------------------------------
!     III - COMPUTATION OF SPECIFIC HEAT AT CONSTANT PRESSURE FOR AIR
!     Cp AND AT THE SURFACE Cps

! - TEMPORARY 2D.

! ZCP      : VALUE OF ATMOSPHERIC Cp

! - TEMPORARY 1D.

! ZCPS     : VALUE OF Cp AT THE SURFACE

DO JLEV=KTDIA,KLEV
  DO JLON=KIDIA,KFDIA
    PQ(JLON,JLEV)=PQ(JLON,JLEV)-RCPD*ZCP(JLON,JLEV)
    PQ(JLON,JLEV)=PQ(JLON,JLEV)+RCPV*ZCP(JLON,JLEV)
    ZCP(JLON,JLEV)=0.0_JPRB
  ENDDO
ENDDO

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