!OPTIONS XOPT(NOEVAL)
SUBROUTINE ACDIFSPTL (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)  

!**** *ACDIFSPTL * - TANGENT LINEAR OF VERTICAL TURBULENT
!                    DIFFUSION - SIMPLIFIED

!     Subject.
!     --------
!     Tangent linear 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* *ACDIFSPTL*

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

! -   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 LEVELS.
! 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 LEVELS.
! 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.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(IN)    :: PAPHI(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHIF(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDELP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDELP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PV(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKTROV(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PKUROV(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXTROV(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: 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(IN)    :: PCDROV(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCHROV(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PXDROV(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: 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(OUT)   :: PFCLL(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFCLN(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFCS(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFEVL(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFEVN(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PLHS(KLON) 
CHARACTER(LEN=7)  ,INTENT(IN)    :: CDLOCK 
REAL(KIND=JPRB) :: ZKSRV(KLON,0:KLEV),ZXTRO(KLON,0:KLEV)&
 & ,ZXURO(KLON,0:KLEV)&
 & ,ZFCQNG(KLON,0:KLEV)&
 & ,ZDSE(KLON,KLEV),ZIPOI(KLON,KLEV),ZPOID(KLON,KLEV)&
 & ,ZN1(KLON,KLEV),ZN2(KLON,KLEV),ZN3(KLON,KLEV),ZN4(KLON,KLEV)&
 & ,ZQCOR(KLON,KLEV),ZSUB1(KLON,KLEV),ZSUB2(KLON,KLEV)&
 & ,ZCP(KLON,KLEV)&
 & ,ZCHRS(KLON),ZDSES1(KLON),ZDLLS(KLON),ZDLLW(KLON)&
 & ,ZDLS(KLON),ZLHS(KLON),ZLHW(KLON),ZPN(KLON)&
 & ,ZXDRO(KLON),ZXSRO(KLON),ZCPS1(KLON)  

REAL(KIND=JPRB) :: ZKSRV5(KLON,0:KLEV),ZXTRO5(KLON,0:KLEV)&
 & ,ZXURO5(KLON,0:KLEV),ZFCQNG5(KLON,0:KLEV)&
 & ,ZDSE5(KLON,KLEV),ZIPOI5(KLON,KLEV),ZPOID5(KLON,KLEV)&
 & ,ZN15(KLON,KLEV),ZN25(KLON,KLEV),ZN35(KLON,KLEV)&
 & ,ZN45(KLON,KLEV),ZQCOR5(KLON,KLEV),ZSUB15(KLON,KLEV)&
 & ,ZSUB25(KLON,KLEV),ZCP5(KLON,KLEV)&
 & ,ZCHRS5(KLON),ZDLLS5(KLON),ZDLLW5(KLON)&
 & ,ZDLS5(KLON),ZXDRO5(KLON),ZXSRO5(KLON),ZDSES15(KLON)  

INTEGER(KIND=JPIM) :: JLEV, JLON

REAL(KIND=JPRB) :: ZCPVMD, ZCPVMS, ZCPVMW, ZDLDQ, ZDLDQ5, ZELIM1,&
 & ZELIM15, ZELIM2, ZELIM25, ZEPS1, ZFENT, ZFENT5, &
 & ZFQ, ZFQ5, ZGDT, ZGDTI, ZMUL1, ZMUL15, ZMUL2, &
 & ZMUL25, ZPUL, ZQCORP, ZQCORP5, ZZ0, ZZ1  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"

!     CHECK RELIABILITY OF INPUT ARGUMENTS.

IF (LHOOK) CALL DR_HOOK('ACDIFSPTL',0,ZHOOK_HANDLE)
IF(CDLOCK /= 'DIFSPTL') 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

ZZ1=1.0_JPRB
ZZ0=0.0_JPRB

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

! - TEMPORARY 2D.

! ZCP      : VALUE OF ATMOSPHERIC Cp

! The same but for the trajectory
! -------------------------------
! ZCP5     : VALUE OF ATMOSPHERIC Cp

! - TEMPORARY 1D.

! ZCPS1    : VALUE OF Cp AT THE SURFACE AT t+dt

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)
    ZCP(JLON,JLEV)=RCPD*(-PQ(JLON,JLEV))+RCPV*PQ(JLON,JLEV)
  ENDDO
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.

! The same but for the trajectory
! -------------------------------
! ZXURO5    : PKUROV MODIFIED TO ATTENUATE FIBRILLATIONS.
! ZFCQNG5   : 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.

! The same but for the trajectory
! -------------------------------
! 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.

! ZXDRO     : PCDROV MODIFIED TO ATTENUATE FIBRILLATIONS.
! ZDSES1    : LOCAL ARRAY FOR DRY STATIC ENERGIES AT THE SURFACE
!             AT t+dt.

! The same but for the trajectory
! -------------------------------
! ZXDRO5    : PCDROV MODIFIED TO ATTENUATE FIBRILLATIONS.
! ZDSES15   : LOCAL ARRAY FOR DRY STATIC ENERGIES AT THE SURFACE
!             AT t+dt.

DO JLON=KIDIA,KFDIA
  ZFCQNG5(JLON,KTDIA-1)=0.0_JPRB
  ZFCQNG(JLON,KTDIA-1)=0.0_JPRB

  ZDSES15(JLON)=ZCPS1(JLON)*PTS1(JLON)+PAPHI5(JLON,KLEV)
  ZDSES1(JLON)=PAPHI(JLON,KLEV)
ENDDO

DO JLEV=KTDIA,KLEV
  DO JLON=KIDIA,KFDIA
    ZPOID5(JLON,JLEV)=PDELP5(JLON,JLEV)*ZGDTI
    ZPOID(JLON,JLEV)=PDELP(JLON,JLEV)*ZGDTI

    ZIPOI5(JLON,JLEV)=PRDELP5(JLON,JLEV)*ZGDT
    ZIPOI(JLON,JLEV)=PRDELP(JLON,JLEV)*ZGDT

    ZDSE5(JLON,JLEV)=ZCP5(JLON,JLEV)*PT5(JLON,JLEV)+PAPHIF5(JLON,JLEV)
    ZDSE(JLON,JLEV)=ZCP5(JLON,JLEV)*PT(JLON,JLEV)&
     & +PT5(JLON,JLEV)*ZCP(JLON,JLEV)+PAPHIF(JLON,JLEV)  

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

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

    ZQCOR5(JLON,JLEV)=ZQCORP5+MAX(ZZ0,-ZQCORP5)
    IF ((-ZQCORP5) > 0.0_JPRB) THEN
      ZQCOR(JLON,JLEV)=0.0_JPRB
    ELSE
      ZQCOR(JLON,JLEV)=ZQCORP
    ENDIF
  ENDDO
ENDDO

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

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

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

  ENDDO
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.

! The same but for the trajectory
! -------------------------------
! 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=1.0_JPRB/(1.0_JPRB+ZXURO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA))
  ZMUL1=-ZXURO5(JLON,KTDIA)*ZIPOI(JLON,KTDIA)&
   & /(1.0_JPRB+ZXURO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA))**2 &
   & -ZIPOI5(JLON,KTDIA)*ZXURO(JLON,KTDIA)&
   & /(1.0_JPRB+ZXURO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA))**2  

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

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

  ZN25(JLON,KTDIA)=ZMUL15*PV5(JLON,KTDIA)
  ZN2(JLON,KTDIA)=ZMUL15*PV(JLON,KTDIA)+PV5(JLON,KTDIA)*ZMUL1
ENDDO

!     ELIMINATION FOR A STANDART LEVEL.

DO JLEV=KTDIA+1,KLEV-1
  DO JLON=KIDIA,KFDIA
    ZELIM15=ZXURO5(JLON,JLEV-1)*ZIPOI5(JLON,JLEV)
    ZELIM1=ZXURO5(JLON,JLEV-1)*ZIPOI(JLON,JLEV)&
     & +ZIPOI5(JLON,JLEV)*ZXURO(JLON,JLEV-1)  

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

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

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

    ZN25(JLON,JLEV)=ZMUL15*(PV5(JLON,JLEV)+ZELIM15*ZN25(JLON,JLEV-1))
    ZN2(JLON,JLEV)=(PV5(JLON,JLEV)+ZELIM15*ZN25(JLON,JLEV-1))*&
     & ZMUL1 &
     & +ZMUL15*PV(JLON,JLEV)&
     & +ZMUL15*ZELIM15*ZN2(JLON,JLEV-1)&
     & +ZMUL15*ZN25(JLON,JLEV-1)*ZELIM1  
  ENDDO
ENDDO

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

!     SURFACE CALCULATIONS.

DO JLON=KIDIA,KFDIA
  ZELIM15=ZXURO5(JLON,KLEV-1)*ZIPOI5(JLON,KLEV)
  ZELIM1=ZXURO5(JLON,KLEV-1)*ZIPOI(JLON,KLEV)&
   & +ZIPOI5(JLON,KLEV)*ZXURO(JLON,KLEV-1)  

  ZMUL15=1.0_JPRB/(1.0_JPRB+ZELIM15*(1.0_JPRB-ZSUB15(JLON,KLEV-1))+ZXDRO5(JLON)&
   & *ZIPOI5(JLON,KLEV))  
  ZMUL1=(ZELIM15*ZSUB1(JLON,KLEV-1)&
   & -(1.0_JPRB-ZSUB15(JLON,KLEV-1))*ZELIM1 &
   & -ZXDRO5(JLON)*ZIPOI(JLON,KLEV)&
   & -ZIPOI5(JLON,KLEV)*ZXDRO(JLON))&
   & /(1.0_JPRB+ZELIM15*(1.0_JPRB-ZSUB15(JLON,KLEV-1))+ZXDRO5(JLON)&
   & *ZIPOI5(JLON,KLEV))**2  

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

  ZN25(JLON,KLEV)=ZMUL15*(PV5(JLON,KLEV)+ZELIM15*ZN25(JLON,KLEV-1))
  ZN2(JLON,KLEV)=(PV5(JLON,KLEV)+ZELIM15*ZN25(JLON,KLEV-1))*ZMUL1 &
   & +ZMUL15*PV(JLON,KLEV)&
   & +ZMUL15*ZELIM15*ZN2(JLON,KLEV-1)&
   & +ZMUL15*ZN25(JLON,KLEV-1)*ZELIM1  

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

  PSTRTV5(JLON,KLEV)=PCDROV5(JLON)*ZN25(JLON,KLEV)
  PSTRTV(JLON,KLEV)=PCDROV5(JLON)*ZN2(JLON,KLEV)+ZN25(JLON,KLEV)*PCDROV(JLON)
ENDDO

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

DO JLEV=KLEV-1,KTDIA,-1
  DO JLON=KIDIA,KFDIA
    ZN15(JLON,JLEV)=ZN15(JLON,JLEV)+ZSUB15(JLON,JLEV)*ZN15(JLON,JLEV+1)
    ZN1(JLON,JLEV)=ZN1(JLON,JLEV)&
     & +ZSUB15(JLON,JLEV)*ZN1(JLON,JLEV+1)&
     & +ZN15(JLON,JLEV+1)*ZSUB1(JLON,JLEV)  

    ZN25(JLON,JLEV)=ZN25(JLON,JLEV)+ZSUB15(JLON,JLEV)*ZN25(JLON,JLEV+1)
    ZN2(JLON,JLEV)=ZN2(JLON,JLEV)&
     & +ZSUB15(JLON,JLEV)*ZN2(JLON,JLEV+1)&
     & +ZN25(JLON,JLEV+1)*ZSUB1(JLON,JLEV)  

    PSTRTU5(JLON,JLEV)=PKUROV5(JLON,JLEV)*(ZN15(JLON,JLEV)-ZN15(JLON,JLEV+1))
    PSTRTU(JLON,JLEV)=(ZN15(JLON,JLEV)-ZN15(JLON,JLEV+1))&
     & *PKUROV(JLON,JLEV)&
     & +PKUROV5(JLON,JLEV)*ZN1(JLON,JLEV)&
     & -PKUROV5(JLON,JLEV)*ZN1(JLON,JLEV+1)  

    PSTRTV5(JLON,JLEV)=PKUROV5(JLON,JLEV)*(ZN25(JLON,JLEV)-ZN25(JLON,JLEV+1))
    PSTRTV(JLON,JLEV)=(ZN25(JLON,JLEV)-ZN25(JLON,JLEV+1))&
     & *PKUROV(JLON,JLEV)&
     & +PKUROV5(JLON,JLEV)*ZN2(JLON,JLEV)&
     & -PKUROV5(JLON,JLEV)*ZN2(JLON,JLEV+1)  
  ENDDO
ENDDO

!     UPPER BOUNDARY CONDITION.

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

  PSTRTV5(JLON,KTDIA-1)=0.0_JPRB
  PSTRTV(JLON,KTDIA-1)=0.0_JPRB
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

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

! - TEMPORARY(S) 1D .

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

! The same but for trajectory
! ---------------------------
! ZCHRS5    : EXCHANGE COEFFICIENT PCHROV FOR SENSIBLE HEAT.
! ZXSRO5    : ZCHRS MODIFIED TO ATTENUATE FIBRILLATIONS.

  ZCHRS5(JLON)=PCHROV5(JLON)
  ZCHRS(JLON)=PCHROV(JLON)

  ZXSRO5(JLON)=ZCHRS5(JLON)*PXHROV5(JLON)
  ZXSRO(JLON)=ZCHRS5(JLON)*PXHROV(JLON)+PXHROV5(JLON)*ZCHRS(JLON)

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

! The same but for trajectory
! ---------------------------
! 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
  ZDLS(JLON)=(PQS(JLON)-PQ5(JLON,KLEV))*ZCPVMD*ZCHRS(JLON)&
   & -ZCPVMD*ZCHRS5(JLON)*PQ(JLON,KLEV)  

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

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

  ZDLDQ5=ZDLLW5(JLON)+ZDLLS5(JLON)-ZDLS5(JLON)*PTS(JLON)
  ZDLDQ=ZDLLW(JLON)+ZDLLS(JLON)-ZDLS(JLON)*PTS(JLON)
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.

! The same but for trajectory
! ---------------------------
! 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)
    ZKSRV(JLON,JLEV)=PKTROV(JLON,JLEV)

    ZXTRO5(JLON,JLEV)=ZKSRV5(JLON,JLEV)*PXTROV5(JLON,JLEV)
    ZXTRO(JLON,JLEV)=ZKSRV5(JLON,JLEV)*PXTROV(JLON,JLEV)&
     & +PXTROV5(JLON,JLEV)*ZKSRV(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=1.0_JPRB/(1.0_JPRB+ZXTRO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA))
  ZMUL2=-ZXTRO5(JLON,KTDIA)*ZIPOI(JLON,KTDIA)&
   & /(1.0_JPRB+ZXTRO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA))**2 &
   & -ZIPOI5(JLON,KTDIA)*ZXTRO(JLON,KTDIA)&
   & /(1.0_JPRB+ZXTRO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA))**2  

  ZSUB25(JLON,KTDIA)=ZMUL25*ZXTRO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA)
  ZSUB2(JLON,KTDIA)=ZXTRO5(JLON,KTDIA)*ZIPOI5(JLON,KTDIA)*ZMUL2 &
   & +ZMUL25*ZIPOI5(JLON,KTDIA)*ZXTRO(JLON,KTDIA)&
   & +ZMUL25*ZXTRO5(JLON,KTDIA)*ZIPOI(JLON,KTDIA)  

  ZN35(JLON,KTDIA)=ZMUL25*ZDSE5(JLON,KTDIA)
  ZN3(JLON,KTDIA)=ZMUL25*ZDSE(JLON,KTDIA)+ZDSE5(JLON,KTDIA)*ZMUL2

  ZN45(JLON,KTDIA)=ZMUL25*ZQCOR5(JLON,KTDIA)
  ZN4(JLON,KTDIA)=ZMUL25*ZQCOR(JLON,KTDIA)+ZQCOR5(JLON,KTDIA)*ZMUL2
ENDDO

!     ELIMINATION FOR A STANDART LAYER FOR S AND Q.

DO JLEV=KTDIA+1,KLEV-1
  DO JLON=KIDIA,KFDIA
    ZELIM25=ZXTRO5(JLON,JLEV-1)*ZIPOI5(JLON,JLEV)
    ZELIM2=ZXTRO5(JLON,JLEV-1)*ZIPOI(JLON,JLEV)&
     & +ZIPOI5(JLON,JLEV)*ZXTRO(JLON,JLEV-1)  

    ZMUL25=1.0_JPRB/(1.0_JPRB+ZELIM25*(1.0_JPRB-ZSUB25(JLON,JLEV-1))+ZXTRO5(JLON,&
     & JLEV)&
     & *ZIPOI5(JLON,JLEV))  
    ZMUL2=(ZELIM25*ZSUB2(JLON,JLEV-1)&
     & -(1.0_JPRB-ZSUB25(JLON,JLEV-1))*ZELIM2 &
     & -ZXTRO5(JLON,JLEV)*ZIPOI(JLON,JLEV)&
     & -ZIPOI5(JLON,JLEV)*ZXTRO(JLON,JLEV))&
     & /(1.0_JPRB+ZELIM25*(1.0_JPRB-ZSUB25(JLON,JLEV-1))+ZXTRO5(JLON,JLEV)&
     & *ZIPOI5(JLON,JLEV))**2  

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

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

    ZN45(JLON,JLEV)=ZMUL25*(ZQCOR5(JLON,JLEV)+ZELIM25*ZN45(JLON,JLEV-1))
    ZN4(JLON,JLEV)=(ZQCOR5(JLON,JLEV)+ZELIM25*ZN45(JLON,JLEV-1))*&
     & ZMUL2 &
     & +ZMUL25*ZQCOR(JLON,JLEV)&
     & +ZMUL25*ZELIM25*ZN4(JLON,JLEV-1)&
     & +ZMUL25*ZN45(JLON,JLEV-1)*ZELIM2  
  ENDDO
ENDDO

!     SURFACE ELIMINATION CALCULATION FOR S AND Q.

DO JLON=KIDIA,KFDIA
  ZELIM25=ZXTRO5(JLON,KLEV-1)*ZIPOI5(JLON,KLEV)
  ZELIM2=ZXTRO5(JLON,KLEV-1)*ZIPOI(JLON,KLEV)&
   & +ZIPOI5(JLON,KLEV)*ZXTRO(JLON,KLEV-1)  

  ZMUL25=1.0_JPRB/(1.0_JPRB+ZELIM25*(1.0_JPRB-ZSUB25(JLON,KLEV-1))&
   & +ZXSRO5(JLON)*ZIPOI5(JLON,KLEV))  
  ZMUL2=(ZELIM25*ZSUB2(JLON,KLEV-1)&
   & -(1.0_JPRB-ZSUB25(JLON,KLEV-1))*ZELIM2 &
   & -ZXSRO5(JLON)*ZIPOI(JLON,KLEV)&
   & -ZIPOI5(JLON,KLEV)*ZXSRO(JLON))&
   & /(1.0_JPRB+ZELIM25*(1.0_JPRB-ZSUB25(JLON,KLEV-1))+ZXSRO5(JLON)&
   & *ZIPOI5(JLON,KLEV))**2  

  ZN35(JLON,KLEV)=ZMUL25*(ZDSE5(JLON,KLEV)&
   & +ZXSRO5(JLON)*ZIPOI5(JLON,KLEV)*ZDSES15(JLON)&
   & +ZELIM25*ZN35(JLON,KLEV-1))  
  ZN3(JLON,KLEV)=(ZDSE5(JLON,KLEV)&
   & +ZXSRO5(JLON)*ZIPOI5(JLON,KLEV)*ZDSES15(JLON)&
   & +ZELIM25*ZN35(JLON,KLEV-1))*ZMUL2 &
   & +ZMUL25*ZDSE(JLON,KLEV)&
   & +ZDSES15(JLON)*ZMUL25*ZXSRO5(JLON)&
   & *ZIPOI(JLON,KLEV)&
   & +ZDSES15(JLON)*ZMUL25*ZIPOI5(JLON,KLEV)&
   & *ZXSRO(JLON)&
   & +ZMUL25*ZXSRO5(JLON)*ZIPOI5(JLON,KLEV)&
   & *ZDSES1(JLON)&
   & +ZMUL25*ZELIM25*ZN3(JLON,KLEV-1)&
   & +ZMUL25*ZN35(JLON,KLEV-1)*ZELIM2  

  ZN45(JLON,KLEV)=ZMUL25*(ZQCOR5(JLON,KLEV)&
   & +ZXSRO5(JLON)*ZIPOI5(JLON,KLEV)*PQS1(JLON)&
   & +ZELIM25*ZN45(JLON,KLEV-1))  
  ZN4(JLON,KLEV)=(ZQCOR5(JLON,KLEV)&
   & +ZXSRO5(JLON)*ZIPOI5(JLON,KLEV)*PQS1(JLON)&
   & +ZELIM25*ZN45(JLON,KLEV-1))*ZMUL2 &
   & +ZMUL25*ZQCOR(JLON,KLEV)&
   & +PQS1(JLON)*ZMUL25*ZXSRO5(JLON)*ZIPOI(JLON,KLEV)&
   & +PQS1(JLON)*ZMUL25*ZIPOI5(JLON,KLEV)*ZXSRO(JLON)&
   & +ZMUL25*ZELIM25*ZN4(JLON,KLEV-1)&
   & +ZMUL25*ZN45(JLON,KLEV-1)*ZELIM2  
ENDDO

!*
!     ------------------------------------------------------------------
!     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.

!     SURFACE AND SOIL FLUXES.

DO JLON=KIDIA,KFDIA
  ZFQ5=ZCHRS5(JLON)*(ZN45(JLON,KLEV)-PQS1(JLON))
  ZFQ=(ZN45(JLON,KLEV)-PQS1(JLON))*ZCHRS(JLON)+ZCHRS5(JLON)*ZN4(JLON,KLEV)

  PFEVL(JLON)=ZFQ*(1.0_JPRB-ZPN(JLON))
  PFEVN(JLON)=ZFQ*ZPN(JLON)

  PFCLL(JLON)=ZLHW(JLON)*ZFQ-ZDLLW(JLON)*(PNTS(JLON)-PTS(JLON))
  PFCLN(JLON)=ZLHS(JLON)*ZFQ-ZDLLS(JLON)*(PNTS(JLON)-PTS(JLON))

  ZFENT5=ZCHRS5(JLON)*(ZN35(JLON,KLEV)-ZDSES15(JLON))
  ZFENT=(ZN35(JLON,KLEV)-ZDSES15(JLON))*ZCHRS(JLON)&
   & +ZCHRS5(JLON)*ZN3(JLON,KLEV)&
   & -ZCHRS5(JLON)*ZDSES1(JLON)  

  PFCS(JLON)=ZFENT-ZCPVMD*PTS(JLON)*ZFQ+ZDLS(JLON)*(PNTS(JLON)-PTS(JLON))
  PDIFTQ5(JLON,KLEV)=ZFQ5+ZFCQNG5(JLON,KLEV)
  PDIFTQ(JLON,KLEV)=ZFQ+ZFCQNG(JLON,KLEV)

  PDIFTS5(JLON,KLEV)=ZFENT5
  PDIFTS(JLON,KLEV)=ZFENT

ENDDO

!     BACK-SUBSTITUTION FOR A STANDART LEVEL.
!     ATMOSPHERIC FLUXES.

DO JLEV=KLEV-1,KTDIA,-1
  DO JLON=KIDIA,KFDIA
    ZN35(JLON,JLEV)=ZN35(JLON,JLEV)+ZSUB25(JLON,JLEV)*ZN35(JLON,JLEV+1)
    ZN3(JLON,JLEV)=ZN3(JLON,JLEV)&
     & +ZSUB25(JLON,JLEV)*ZN3(JLON,JLEV+1)&
     & +ZN35(JLON,JLEV+1)*ZSUB2(JLON,JLEV)  

    ZN45(JLON,JLEV)=ZN45(JLON,JLEV)+ZSUB25(JLON,JLEV)*ZN45(JLON,JLEV+1)
    ZN4(JLON,JLEV)=ZN4(JLON,JLEV)&
     & +ZSUB25(JLON,JLEV)*ZN4(JLON,JLEV+1)&
     & +ZN45(JLON,JLEV+1)*ZSUB2(JLON,JLEV)  

    PDIFTQ5(JLON,JLEV)=ZKSRV5(JLON,JLEV)*(ZN45(JLON,JLEV)&
     & -ZN45(JLON,JLEV+1))+ZFCQNG5(JLON,JLEV)  
    PDIFTQ(JLON,JLEV)=(ZN45(JLON,JLEV)-ZN45(JLON,JLEV+1))&
     & *ZKSRV(JLON,JLEV)&
     & +ZKSRV5(JLON,JLEV)*ZN4(JLON,JLEV)&
     & -ZKSRV5(JLON,JLEV)*ZN4(JLON,JLEV+1)&
     & +ZFCQNG(JLON,JLEV)  

    PDIFTS5(JLON,JLEV)=ZKSRV5(JLON,JLEV)*(ZN35(JLON,JLEV)-ZN35(JLON,JLEV+1))
    PDIFTS(JLON,JLEV)=(ZN35(JLON,JLEV)-ZN35(JLON,JLEV+1))&
     & *ZKSRV(JLON,JLEV)&
     & +ZKSRV5(JLON,JLEV)*ZN3(JLON,JLEV)&
     & -ZKSRV5(JLON,JLEV)*ZN3(JLON,JLEV+1)  
  ENDDO
ENDDO

!     UPPER BOUNDARY CONDITION.

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

  PDIFTS5(JLON,KTDIA-1)=0.0_JPRB
  PDIFTS(JLON,KTDIA-1)=0.0_JPRB
ENDDO

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