!OPTIONS XOPT(NOEVAL)
SUBROUTINE ACQWLSRAD ( KIDIA,KFDIA,KLON,KTDIA,KLEV,&
 !-----------------------------------------------------------------------
 ! - INPUT  2D .
 & PQ5,PT5,PAPRSF5,PDELP5,&
 & PQ,PT,PAPRSF,PDELP,&
 ! - OUTPUT 2D .
 & PFPLSL5,PFPLSN5,&
 & PFPLSL,PFPLSN,&
 ! - LOCK ARGUMENTS .
 & CDLOCK)  

!**** *ACQWLSRAD* - ADJOINT COMPUTATION OF WET-BULB
!                   CHARACTERISTICS AND LSR.

!     Subject.
!     --------
!     Adjoint computation of:
!     - SATURATION POINT AND WET-BULB CHARACTERISTICS .
!     - STRATIFORM PRECIPITATION FLUXES (WATER AND SNOW) .

!**   Interface.
!     ----------
!        *CALL* *ACQWLSRAD*

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

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

! - DIMENSIONAL PARAMETERS OF PHYSICS.

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

! - THE NAME OF THE PHYSICAL VARIABLES

! - 2D (1:KLEV) .

! PAPRSF     : PRESSURE ON FULL LEVELS.
! PQ         : SPECIFIC HUMIDITY OF WATER VAPOR.
! PT         : TEMPERATURE.
! PAPRS      : PRESSION ON HALF LEVELS.
! PDELP      : LAYER THICKNESS IN PRESSURE UNITS.

! The same but for trajectory
! ---------------------------
! PAPRSF5    : PRESSURE ON FULL LEVELS.
! PQ5        : SPECIFIC HUMIDITY OF WATER VAPOR.
! PT5        : TEMPERATURE.
! PAPRS5     : PRESSION ON HALF LEVELS.
! PDELP5     : LAYER THICKNESS IN PRESSURE UNITS.

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

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

! - 2D (0:KLEV) .

! PFPLSL     : STRATIFORM PRECIPITATION AS RAIN.
! PFPLSN     : STRATIFORM PRECIPITATION AS SNOW.

! The same but for trajectory
! ---------------------------
! PFPLSL5    : STRATIFORM PRECIPITATION AS RAIN.
! PFPLSN5    : STRATIFORM PRECIPITATION AS SNOW.

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

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

! COMMON/YOMPHY /
! COMMON/YOMPHY0/
! COMMON/YOMPHY2/
! COMMON/YOMCST /
! COMMON/YOMSIMPHL/
! COMMON/FCTTRM /
! COMMON/FCTTRMAD /

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

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

!     Method.
!     -------

!     Author.
!     -------
!        95-05-29  M. Janiskova

!     Modifications.
!     --------------
!        97-08, introduce CDLOCK - J.M. Piriou.
!        99-12, no evaporation under rain (if LSMOOTHB=.T.) - F. Bouyssel.
!        03-06, move rnlcurv rnegat into yomphy0 (F. Bouyssel, C. Fischer)
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

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

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

USE YOMPHY   , ONLY : NBITER   ,LNEIGE
USE YOMPHY0  , ONLY : RNEGAT   ,RNLCURV
USE YOMPHY2  , ONLY : TSPHY
USE YOMCST   , ONLY : RG       ,RV       ,RCPD     ,RCPV     ,&
 & RETV     ,RCW      ,RCS      ,RLVTT    ,RLSTT    ,&
 & RTT      ,RALPW    ,RBETW    ,RGAMW    ,RALPS    ,&
 & RBETS    ,RGAMS    ,RALPD    ,RBETD    ,RGAMD  
USE YOMSIMPHL, ONLY : LSMOOTHA ,LSMOOTHB

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)    :: PQ5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPRSF5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDELP5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PT(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PAPRSF(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PDELP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFPLSL5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PFPLSN5(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFPLSL(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFPLSN(KLON,0:KLEV) 
CHARACTER(LEN=7)  ,INTENT(IN)    :: CDLOCK 
REAL(KIND=JPRB) :: ZCP(KLON),ZZLH(KLON),ZQWI(KLON)&
 & ,ZLH(KLON,KLEV),ZQSAT(KLON,KLEV)&
 & ,ZPQW(KLON,KLEV),ZTW(KLON,KLEV)  
REAL(KIND=JPRB) :: ZPOID(KLON,KLEV),ZDQ(KLON),ZFPT(KLON,KLEV)

REAL(KIND=JPRB) :: ZCP5(KLON,KLEV,NBITER),ZZLH5(KLON,KLEV,NBITER)&
 & ,ZQWI5(KLON,KLEV)&
 & ,ZLH5(KLON,KLEV),ZQSAT5(KLON,KLEV)&
 & ,ZPQW5(KLON,KLEV,NBITER),ZTW5(KLON,KLEV,NBITER)  
REAL(KIND=JPRB) :: ZPOID5(KLON,KLEV),ZDQ5(KLON,KLEV),ZFPT5(KLON,KLEV)

REAL(KIND=JPRB) :: ZEW5(KLON,KLEV,NBITER),ZESP5(KLON,KLEV,NBITER)&
 & ,ZQW5(KLON,KLEV,NBITER),ZDLEW5(KLON,KLEV,NBITER)&
 & ,ZDQW5(KLON,KLEV,NBITER),ZDELQ5(KLON,KLEV,NBITER)&
 & ,ZDELT5(KLON,KLEV,NBITER)  

REAL(KIND=JPRB) :: ZCP15(KLON,KLEV),ZZLH15(KLON,KLEV)&
 & ,ZPQW15(KLON,KLEV),ZTW15(KLON,KLEV)&
 & ,ZFPT15(KLON,KLEV),ZQ5(KLON,KLEV)  

INTEGER(KIND=JPIM) :: JIT, JJ, JLEV, JLON

REAL(KIND=JPRB) :: ZABS5, ZCPVMS, ZCPVMW, ZDCP, ZDELQ, ZDELT,&
 & ZDELTA, ZDLEW, ZDQW, ZESP, ZEW, ZGDTI, ZLOG2COSH5, &
 & ZNLMINQ, ZQW, ZZZ  
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"

#include "fcttrm.h"
#include "fcttrmad.h"

!     CHECK RELIABILITY OF INPUT ARGUMENTS.

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

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

ZCPVMW=RCPV-RCW
ZCPVMS=RCPV-RCS

!*
!     ------------------------------------------------------------------
!     0. 2 - Compute trajectory - passive loop on vertical levels.

DO JLEV=KTDIA,KLEV

!*
!     ------------------------------------------------------------------
!     0. 3 - Computation of the local atmospheric latent heat.

  DO JLON=KIDIA,KFDIA

!     Snow option dependent computations.

    IF (LNEIGE) THEN
      ZDELTA=MAX(0.0_JPRB,SIGN(1.0_JPRB,RTT-PT5(JLON,JLEV) ))
    ELSE
      ZDELTA=0.0_JPRB
    ENDIF

!     Call to the defined function to compute LH.

    ZLH5(JLON,JLEV)= FOLH (PT5(JLON,JLEV),ZDELTA)

  ENDDO

!*
!     ------------------------------------------------------------------
!     0. 4 - Initialization of the variables in the Newton loop, the
!     temporary CP and LH values having to evolve at the same time as
!     the iterative TW and QW.

! - TEMPORARY 3D .

! For trajectory
! --------------
! ZCP5      : CURRENT VALUE OF ATMOSPHERIC CP DURING THE "NBITER" LOOP.
! ZLH5      : AS ZCP5 BUT FOR THE LATENT HEAT.

  DO JLON=KIDIA,KFDIA
    ZPQW5(JLON,JLEV,1)=PQ5(JLON,JLEV)
    ZCP5(JLON,JLEV,1)=RCPD*(1.0_JPRB-PQ5(JLON,JLEV))+RCPV*PQ5(JLON,JLEV)
    ZTW5(JLON,JLEV,1)=PT5(JLON,JLEV)
    ZZLH5(JLON,JLEV,1)=ZLH5(JLON,JLEV)

    ZPQW15(JLON,JLEV)=PQ5(JLON,JLEV)
    ZCP15(JLON,JLEV)=RCPD*(1.0_JPRB-PQ5(JLON,JLEV))+RCPV*PQ5(JLON,JLEV)
    ZTW15(JLON,JLEV)=PT5(JLON,JLEV)
    ZZLH15(JLON,JLEV)=ZLH5(JLON,JLEV)
  ENDDO

!*
!     ------------------------------------------------------------------
!     0. 5 - Newton loop for the "wet bulb" point.

  DO JIT=1,NBITER
    DO JLON=KIDIA,KFDIA

      IF (JIT == 1) THEN
        JJ=0
      ELSE
        JJ=1
      ENDIF

!     Snow option dependent computations.

      IF (LNEIGE) THEN
        ZDELTA=MAX(0.0_JPRB,SIGN(1.0_JPRB,RTT-ZTW5(JLON,JLEV,JIT-JJ) ))
      ELSE
        ZDELTA=0.0_JPRB
      ENDIF

!     Saturation calculations using the defined functions and temporary
!     storage of QSAT(TW).

! - TEMPORARY 2D .

! For trajectory
! --------------
! ZQWI5     : CURRENT VALUE OF ZQW TO BE REUSED AS QSAT AT JIT=1.

      ZEW5(JLON,JLEV,JIT)= FOEW (ZTW5(JLON,JLEV,JIT-JJ),ZDELTA)
      ZESP5(JLON,JLEV,JIT)=ZEW5(JLON,JLEV,JIT)/PAPRSF5(JLON,JLEV)
      ZQW5(JLON,JLEV,JIT)= FOQS (ZESP5(JLON,JLEV,JIT))

      ZDLEW5(JLON,JLEV,JIT)=FODLEW (ZTW5(JLON,JLEV,JIT-JJ),ZDELTA)
      ZDQW5(JLON,JLEV,JIT)=&
       & FODQS (ZQW5(JLON,JLEV,JIT),ZESP5(JLON,JLEV,JIT)&
       & ,ZDLEW5(JLON,JLEV,JIT))  
      ZQWI5(JLON,JLEV)=ZQW5(JLON,JLEV,JIT)

!     Incrementations.

      ZDCP=ZCPVMW+ZDELTA*(ZCPVMS-ZCPVMW)
      ZDELQ5(JLON,JLEV,JIT)=&
       & (ZQW5(JLON,JLEV,JIT)-ZPQW5(JLON,JLEV,JIT-JJ))&
       & *ZCP5(JLON,JLEV,JIT-JJ)&
       & /(ZCP5(JLON,JLEV,JIT-JJ)+ZZLH5(JLON,JLEV,JIT-JJ)&
       & *ZDQW5(JLON,JLEV,JIT))  
      ZCP5(JLON,JLEV,JIT)=ZCP5(JLON,JLEV,JIT-JJ)+ZDCP*ZDELQ5(JLON,JLEV,JIT)
      ZDELT5(JLON,JLEV,JIT)=&
       & -ZDELQ5(JLON,JLEV,JIT)*ZZLH5(JLON,JLEV,JIT-JJ)&
       & /ZCP5(JLON,JLEV,JIT)  
      ZZLH5(JLON,JLEV,JIT)=ZZLH5(JLON,JLEV,JIT-JJ)+ZDCP*ZDELT5(JLON,JLEV,JIT)
      ZPQW5(JLON,JLEV,JIT)=ZPQW5(JLON,JLEV,JIT-JJ)+ZDELQ5(JLON,JLEV,JIT)
      ZTW5(JLON,JLEV,JIT)=ZTW5(JLON,JLEV,JIT-JJ)+ZDELT5(JLON,JLEV,JIT)

!     Saving the saturation humidity after the first iteration.

      IF (JIT == 1) THEN
        ZQSAT5(JLON,JLEV)=ZQWI5(JLON,JLEV)
      ENDIF

    ENDDO

  ENDDO

!*
!     ------------------------------------------------------------------

!     0. 6 - End of the vertical loop.

ENDDO

!*
!     ------------------------------------------------------------------
!     0. 7 - Computation of derived parameters
!            (for the precipitation fluxes).

ZGDTI=1.0_JPRB/(RG*TSPHY)

!*
!     ------------------------------------------------------------------

!     0. 8 - Initialization of the fields of precipitation.

! - TEMPORARY 2D .

! For trajectory
! ---------------
! ZFPT5     : TOTAL PRECIPITATION FLUX AT THE LAYER.

DO JLON=KIDIA,KFDIA
  PFPLSL5(JLON,KTDIA-1)=0.0_JPRB
  PFPLSN5(JLON,KTDIA-1)=0.0_JPRB
  ZFPT15(JLON,KTDIA)=0.0_JPRB
ENDDO

!*
!     ------------------------------------------------------------------
!     0. 9 - Efeective calculations in a vertical loop where the
!            information is passed from layer to layer.

DO JLEV=KTDIA,KLEV

!     Preliminary computations ("wet bulb" correction for Q and inverse
!     of the pressure at boundaries, the layer's pressure thickness
!     divided by G*DT for all levels)  and provisional flux

! - TEMPORARY 2D .

! For trajectory
! --------------
! ZDQ5      : CORRECTION OF Q TO REACH THE SATURATION BALANCE.

! - TEMPORARY 2D (1:KLEV) .

! For trajectory
! --------------
! ZPOID5    : DP/(RG*DT) FOR A GIVEN LEVEL AND A GIVEN TIME STEP.

  DO JLON=KIDIA,KFDIA
    IF (JLEV == KTDIA) THEN
      ZFPT15(JLON,JLEV)=0.0_JPRB
    ELSE
      ZPOID5(JLON,JLEV)=PDELP5(JLON,JLEV)*ZGDTI

      ZDQ5(JLON,JLEV)=ZPQW5(JLON,JLEV,NBITER)-PQ5(JLON,JLEV)

      IF (LSMOOTHB) THEN
        ZFPT15(JLON,JLEV)=ZFPT15(JLON,JLEV-1)+&
         & MAX(0.0_JPRB,-ZPOID5(JLON,JLEV)*ZDQ5(JLON,JLEV))  
      ELSE
        ZFPT15(JLON,JLEV)=MAX(0.0_JPRB &
         & ,ZFPT15(JLON,JLEV-1)-ZPOID5(JLON,JLEV)&
         & *ZDQ5(JLON,JLEV))  
      ENDIF
    ENDIF
  ENDDO
ENDDO

DO JLEV=KTDIA,KLEV
  DO JLON=KIDIA,KFDIA

!     Some smoothing if it is required

    IF (LSMOOTHA) THEN
      IF (ZFPT15(JLON,JLEV) > 0.0_JPRB)THEN
        ZNLMINQ=0.0_JPRB
        ZFPT5(JLON,JLEV)=RNEGAT+ZFPT15(JLON,JLEV)

        ZQ5(JLON,JLEV)=RNLCURV*(ZFPT5(JLON,JLEV)-ZNLMINQ)
        IF (ZQ5(JLON,JLEV) > 17._JPRB) THEN
          ZFPT5(JLON,JLEV)=ZFPT5(JLON,JLEV)
        ELSEIF (ZQ5(JLON,JLEV) < -17._JPRB) THEN
          ZFPT5(JLON,JLEV)=ZNLMINQ
        ELSE
          ZABS5=ABS(ZQ5(JLON,JLEV))
          ZLOG2COSH5=ZABS5+LOG(1.0_JPRB+EXP(-ZABS5-ZABS5))
          ZFPT5(JLON,JLEV)=0.5_JPRB*(ZLOG2COSH5/RNLCURV &
           & +ZFPT5(JLON,JLEV)+ZNLMINQ)  
        ENDIF
      ELSE
        ZFPT5(JLON,JLEV)=ZFPT15(JLON,JLEV)
      ENDIF
    ELSE
      ZFPT5(JLON,JLEV)=ZFPT15(JLON,JLEV)
    ENDIF

!     Swap at the end of the layer's computations and flux values.

    IF (PT5(JLON,JLEV) > RTT) THEN
      PFPLSL5(JLON,JLEV)=ZFPT5(JLON,JLEV)
      PFPLSN5(JLON,JLEV)=0.0_JPRB
    ELSE
      PFPLSL5(JLON,JLEV)=0.0_JPRB
      PFPLSN5(JLON,JLEV)=ZFPT5(JLON,JLEV)
    ENDIF

  ENDDO
ENDDO

!     INITIALIZATION OF THE LOCAL VARIABLES

ZDQ(:)=0.0_JPRB
ZQWI(:)=0.0_JPRB
ZZLH(:)=0.0_JPRB
ZCP(:)=0.0_JPRB

ZPOID(:,:)=0.0_JPRB
ZTW(:,:)=0.0_JPRB
ZPQW(:,:)=0.0_JPRB
ZQSAT(:,:)=0.0_JPRB
ZLH(:,:)=0.0_JPRB

!*
!     ------------------------------------------------------------------

!     VIII - INITIALIZATION OF THE FIELDS OF PRECIPITATION.

! - TEMPORARY 2D .

! ZFPT      : TOTAL PRECIPITATION FLUX AT THE LAYER.

ZFPT(:,:)=0.0_JPRB

!*
!     ------------------------------------------------------------------
!     IX - EFFECTIVE CALCULATIONS IN A VERTICAL LOOP WHERE THE
!     INFORMATION IS PASSED FROM LAYER TO LAYER.

! - TEMPORARY 1D .

! ZDQ       : CORRECTION OF Q TO REACH THE SATURATION BALANCE.

! - TEMPORARY 2D (1:KLEV) .

! ZPOID     : DP/(RG*DT) FOR A GIVEN LEVEL AND A GIVEN TIME STEP.

DO JLEV=KLEV,KTDIA,-1

!     SWAP AT THE END OF THE LAYER'S COMPUTATIONS AND FLUX VALUES.

  DO JLON=KIDIA,KFDIA
    IF (PT5(JLON,JLEV) > RTT) THEN
      ZFPT(JLON,JLEV)=ZFPT(JLON,JLEV)+PFPLSL(JLON,JLEV)
      PFPLSL(JLON,JLEV)=0.0_JPRB
      PFPLSN(JLON,JLEV)=0.0_JPRB
    ELSE
      ZFPT(JLON,JLEV)=ZFPT(JLON,JLEV)+PFPLSN(JLON,JLEV)
      PFPLSN(JLON,JLEV)=0.0_JPRB
      PFPLSL(JLON,JLEV)=0.0_JPRB
    ENDIF

!     SOME SMOOTHING IF IT IS REQUIRED

    IF (LSMOOTHA) THEN
      IF (ZFPT15(JLON,JLEV) > 0.0_JPRB)THEN
        ZNLMINQ=0.0_JPRB

        IF (ZQ5(JLON,JLEV) > 17._JPRB) THEN
          ZFPT(JLON,JLEV)=ZFPT(JLON,JLEV)
        ELSEIF (ZQ5(JLON,JLEV) < -17._JPRB) THEN
          ZFPT(JLON,JLEV)=0.0_JPRB
        ELSE
          ZFPT(JLON,JLEV)=0.5_JPRB*(TANH(ZQ5(JLON,JLEV))+1.0_JPRB)*ZFPT(JLON,JLEV)
        ENDIF
        ZFPT(JLON,JLEV)=ZFPT(JLON,JLEV)
      ENDIF
    ENDIF
  ENDDO
ENDDO

!     PRELIMINARY COMPUTATIONS ("WET BULB" CORRECTION FOR Q AND INVERSE
!     OF THE PRESSURE AT BOUNDARIES, THE LAYER'S PRESSURE THICKNESS
!     DIVIDED BY G*DT FOR ALL LEVELS)  AND PROVISIONAL FLUX

DO JLEV=KLEV,KTDIA,-1
  DO JLON=KIDIA,KFDIA
    IF (JLEV == KTDIA) THEN
      ZFPT(JLON,JLEV)=0.0_JPRB
    ELSE
      IF (LSMOOTHB) THEN
        ZZZ=-ZPOID5(JLON,JLEV)*ZDQ5(JLON,JLEV)
        IF (ZZZ > 0) THEN
          ZDQ(JLON)=ZDQ(JLON)-ZPOID5(JLON,JLEV)*ZFPT(JLON,JLEV)
          ZPOID(JLON,JLEV)=ZPOID(JLON,JLEV)-ZFPT(JLON,JLEV)*ZDQ5(JLON,JLEV)
          ZFPT(JLON,JLEV-1)=ZFPT(JLON,JLEV-1)+ZFPT(JLON,JLEV)
          ZFPT(JLON,JLEV)=0.0_JPRB
        ELSE
          ZFPT(JLON,JLEV-1)=ZFPT(JLON,JLEV-1)+ZFPT(JLON,JLEV)
          ZFPT(JLON,JLEV)=0.0_JPRB
        ENDIF
      ELSE
        ZZZ=ZFPT15(JLON,JLEV-1)-ZPOID5(JLON,JLEV)*ZDQ5(JLON,JLEV)
        IF (ZZZ > 0) THEN
          ZDQ(JLON)=ZDQ(JLON)-ZPOID5(JLON,JLEV)*ZFPT(JLON,JLEV)
          ZPOID(JLON,JLEV)=ZPOID(JLON,JLEV)-ZFPT(JLON,JLEV)*ZDQ5(JLON,JLEV)
          ZFPT(JLON,JLEV-1)=ZFPT(JLON,JLEV-1)+ZFPT(JLON,JLEV)
          ZFPT(JLON,JLEV)=0.0_JPRB
        ELSE
          ZFPT(JLON,JLEV)=0.0_JPRB
        ENDIF
      ENDIF

      ZPQW(JLON,JLEV)=ZPQW(JLON,JLEV)+ZDQ(JLON)
      PQ(JLON,JLEV)=PQ(JLON,JLEV)-ZDQ(JLON)
      ZDQ(JLON)=0.0_JPRB

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

DO JLON=KIDIA,KFDIA
  PFPLSL(JLON,KTDIA-1)=0.0_JPRB
  PFPLSN(JLON,KTDIA-1)=0.0_JPRB
  ZFPT(JLON,KTDIA)=0.0_JPRB
ENDDO
!*
!     ------------------------------------------------------------------
!     II - PASSIVE LOOP ON VERTICAL LEVELS.

DO JLEV=KTDIA,KLEV

!*
!     ------------------------------------------------------------------
!     V - NEWTON LOOP FOR THE "WET BULB" POINT.

  DO JIT=NBITER,1,-1
    DO JLON=KIDIA,KFDIA
      ZDELT=0.0_JPRB
      ZDELQ=0.0_JPRB
      ZQW=0.0_JPRB
      ZDQW=0.0_JPRB
      ZEW=0.0_JPRB
      ZDLEW=0.0_JPRB
      ZESP=0.0_JPRB

      IF (JIT == 1) THEN
        JJ=0
      ELSE
        JJ=1
      ENDIF

!     SNOW OPTION DEPENDENT COMPUTATIONS.

      IF (LNEIGE) THEN
        IF (JIT == 1) THEN
          ZDELTA=MAX(0.0_JPRB,SIGN(1.0_JPRB,RTT-ZTW15(JLON,JLEV) ))
        ELSE
          ZDELTA=MAX(0.0_JPRB,SIGN(1.0_JPRB,RTT-ZTW5(JLON,JLEV,JIT-JJ) ))
        ENDIF
      ELSE
        ZDELTA=0.0_JPRB
      ENDIF

!     SAVING THE SATURATION HUMIDITY AFTER THE FIRST ITERATION.

      IF (JIT == 1) THEN
        ZQWI(JLON)=ZQWI(JLON)+ZQSAT(JLON,JLEV)
        ZQSAT(JLON,JLEV)=0.0_JPRB
      ENDIF

!     INCREMENTATIONS.

      ZDCP=ZCPVMW+ZDELTA*(ZCPVMS-ZCPVMW)

      ZDELT=ZDELT+ZTW(JLON,JLEV)
      ZTW(JLON,JLEV)=ZTW(JLON,JLEV)

      ZDELQ=ZDELQ+ZPQW(JLON,JLEV)
      ZPQW(JLON,JLEV)=ZPQW(JLON,JLEV)

      ZDELT=ZDELT+ZDCP*ZZLH(JLON)
      ZZLH(JLON)=ZZLH(JLON)

      IF (JIT == 1) THEN
        ZDELQ=ZDELQ-(ZZLH15(JLON,JLEV)/ZCP5(JLON,JLEV,JIT))*ZDELT
        ZZLH(JLON)=ZZLH(JLON)&
         & -(ZDELQ5(JLON,JLEV,JIT)/ZCP5(JLON,JLEV,JIT))*ZDELT  
        ZCP(JLON)=ZCP(JLON)+ZDELQ5(JLON,JLEV,JIT)&
         & *ZZLH15(JLON,JLEV)*ZDELT &
         & /ZCP5(JLON,JLEV,JIT)**2  

        ZDELQ=ZDELQ+ZDCP*ZCP(JLON)

        ZQW=ZQW+ZCP15(JLON,JLEV)*ZDELQ &
         & /(ZCP15(JLON,JLEV)+ZZLH15(JLON,JLEV)&
         & *ZDQW5(JLON,JLEV,JIT))  
        ZPQW(JLON,JLEV)=ZPQW(JLON,JLEV)&
         & -ZCP15(JLON,JLEV)*ZDELQ &
         & /(ZCP15(JLON,JLEV)+ZZLH15(JLON,JLEV)&
         & *ZDQW5(JLON,JLEV,JIT))  
        ZCP(JLON)=ZCP(JLON)&
         & +(ZQW5(JLON,JLEV,JIT)-ZPQW15(JLON,JLEV))*ZDELQ &
         & /(ZCP15(JLON,JLEV)+ZZLH15(JLON,JLEV)&
         & *ZDQW5(JLON,JLEV,JIT))  
        ZCP(JLON)=ZCP(JLON)&
         & -(ZQW5(JLON,JLEV,JIT)-ZPQW15(JLON,JLEV))&
         & *ZCP15(JLON,JLEV)*ZDELQ &
         & /(ZCP15(JLON,JLEV)+ZZLH15(JLON,JLEV)&
         & *ZDQW5(JLON,JLEV,JIT))**2  
        ZZLH(JLON)=ZZLH(JLON)&
         & -(ZQW5(JLON,JLEV,JIT)-ZPQW15(JLON,JLEV))&
         & *ZCP15(JLON,JLEV)&
         & *ZDQW5(JLON,JLEV,JIT)*ZDELQ &
         & /(ZCP15(JLON,JLEV)+ZZLH15(JLON,JLEV)&
         & *ZDQW5(JLON,JLEV,JIT))**2  
        ZDQW=ZDQW-(ZQW5(JLON,JLEV,JIT)-ZPQW15(JLON,JLEV))&
         & *ZCP15(JLON,JLEV)&
         & *ZZLH15(JLON,JLEV)*ZDELQ &
         & /(ZCP15(JLON,JLEV)+ZZLH15(JLON,JLEV)&
         & *ZDQW5(JLON,JLEV,JIT))**2  

!     SATURATION CALCULATIONS USING THE DEFINED FUNCTIONS AND TEMPORARY
!     STORAGE OF QSAT(TW).

! - TEMPORARY 1D .

! ZQWI      : CURRENT VALUE OF ZQW TO BE REUSED AS QSAT AT JIT=1.

        ZQW=ZQW+ZQWI(JLON)

        ZQW=ZQW &
         & +FODQSAD1(ZDQW,ZQW5(JLON,JLEV,JIT),ZESP5(JLON,JLEV,JIT)&
         & ,ZDLEW5(JLON,JLEV,JIT))  
        ZESP=ZESP &
         & +FODQSAD2(ZQW5(JLON,JLEV,JIT),ZDQW,ZESP5(JLON,JLEV,JIT)&
         & ,ZDLEW5(JLON,JLEV,JIT))  
        ZDLEW=ZDLEW &
         & +FODQSAD3(ZQW5(JLON,JLEV,JIT),ZESP5(JLON,JLEV,JIT)&
         & ,ZDQW,ZDLEW5(JLON,JLEV,JIT))  

        ZTW(JLON,JLEV)=ZTW(JLON,JLEV)+FODLEWAD(ZDLEW,ZTW15(JLON,JLEV),ZDELTA)

        IF ((1.0_JPRB-ZESP5(JLON,JLEV,JIT)) > 0.0_JPRB) THEN
          ZESP=ZESP+FOQSAD1(ZQW,ZESP5(JLON,JLEV,JIT))
        ELSE
          ZESP=ZESP+FOQSAD2(ZQW,ZESP5(JLON,JLEV,JIT))
        ENDIF

        ZEW=ZEW+ZESP/PAPRSF5(JLON,JLEV)
        PAPRSF(JLON,JLEV)=PAPRSF(JLON,JLEV)&
         & -(ZEW5(JLON,JLEV,JIT)&
         & /PAPRSF5(JLON,JLEV)**2)*ZESP  

        ZTW(JLON,JLEV)=ZTW(JLON,JLEV)+FOEWAD(ZEW,ZTW15(JLON,JLEV),ZDELTA)

      ELSE

        ZDELQ=ZDELQ-(ZZLH5(JLON,JLEV,JIT-JJ)/ZCP5(JLON,JLEV,JIT))*ZDELT
        ZZLH(JLON)=ZZLH(JLON)&
         & -(ZDELQ5(JLON,JLEV,JIT)/ZCP5(JLON,JLEV,JIT))*ZDELT  
        ZCP(JLON)=ZCP(JLON)+ZDELQ5(JLON,JLEV,JIT)&
         & *ZZLH5(JLON,JLEV,JIT-JJ)*ZDELT &
         & /ZCP5(JLON,JLEV,JIT)**2  

        ZDELQ=ZDELQ+ZDCP*ZCP(JLON)

        ZQW=ZQW+ZCP5(JLON,JLEV,JIT-JJ)*ZDELQ &
         & /(ZCP5(JLON,JLEV,JIT-JJ)+ZZLH5(JLON,JLEV,JIT-JJ)&
         & *ZDQW5(JLON,JLEV,JIT))  
        ZPQW(JLON,JLEV)=ZPQW(JLON,JLEV)&
         & -ZCP5(JLON,JLEV,JIT-JJ)*ZDELQ &
         & /(ZCP5(JLON,JLEV,JIT-JJ)&
         & +ZZLH5(JLON,JLEV,JIT-JJ)*ZDQW5(JLON,JLEV,JIT))  
        ZCP(JLON)=ZCP(JLON)&
         & +(ZQW5(JLON,JLEV,JIT)-ZPQW5(JLON,JLEV,JIT-JJ))*ZDELQ &
         & /(ZCP5(JLON,JLEV,JIT-JJ)+ZZLH5(JLON,JLEV,JIT-JJ)&
         & *ZDQW5(JLON,JLEV,JIT))  
        ZCP(JLON)=ZCP(JLON)&
         & -(ZQW5(JLON,JLEV,JIT)-ZPQW5(JLON,JLEV,JIT-JJ))&
         & *ZCP5(JLON,JLEV,JIT-JJ)*ZDELQ &
         & /(ZCP5(JLON,JLEV,JIT-JJ)+ZZLH5(JLON,JLEV,JIT-JJ)&
         & *ZDQW5(JLON,JLEV,JIT))**2  
        ZZLH(JLON)=ZZLH(JLON)&
         & -(ZQW5(JLON,JLEV,JIT)-ZPQW5(JLON,JLEV,JIT-JJ))&
         & *ZCP5(JLON,JLEV,JIT-JJ)&
         & *ZDQW5(JLON,JLEV,JIT)*ZDELQ &
         & /(ZCP5(JLON,JLEV,JIT-JJ)+ZZLH5(JLON,JLEV,JIT-JJ)&
         & *ZDQW5(JLON,JLEV,JIT))**2  
        ZDQW=ZDQW-(ZQW5(JLON,JLEV,JIT)-ZPQW5(JLON,JLEV,JIT-JJ))&
         & *ZCP5(JLON,JLEV,JIT-JJ)&
         & *ZZLH5(JLON,JLEV,JIT-JJ)*ZDELQ &
         & /(ZCP5(JLON,JLEV,JIT-JJ)+ZZLH5(JLON,JLEV,JIT-JJ)&
         & *ZDQW5(JLON,JLEV,JIT))**2  

!     SATURATION CALCULATIONS USING THE DEFINED FUNCTIONS AND TEMPORARY
!     STORAGE OF QSAT(TW).

! - TEMPORARY 1D .

! ZQWI      : CURRENT VALUE OF ZQW TO BE REUSED AS QSAT AT JIT=1.

        ZQW=ZQW+ZQWI(JLON)

        ZQW=ZQW &
         & +FODQSAD1(ZDQW,ZQW5(JLON,JLEV,JIT),ZESP5(JLON,JLEV,JIT)&
         & ,ZDLEW5(JLON,JLEV,JIT))  
        ZESP=ZESP &
         & +FODQSAD2(ZQW5(JLON,JLEV,JIT),ZDQW,ZESP5(JLON,JLEV,JIT)&
         & ,ZDLEW5(JLON,JLEV,JIT))  
        ZDLEW=ZDLEW &
         & +FODQSAD3(ZQW5(JLON,JLEV,JIT),ZESP5(JLON,JLEV,JIT)&
         & ,ZDQW,ZDLEW5(JLON,JLEV,JIT))  

        ZTW(JLON,JLEV)=ZTW(JLON,JLEV)&
         & +FODLEWAD(ZDLEW,ZTW5(JLON,JLEV,JIT-JJ),ZDELTA)  

        IF ((1.0_JPRB-ZESP5(JLON,JLEV,JIT)) > 0.0_JPRB) THEN
          ZESP=ZESP+FOQSAD1(ZQW,ZESP5(JLON,JLEV,JIT))
        ELSE
          ZESP=ZESP+FOQSAD2(ZQW,ZESP5(JLON,JLEV,JIT))
        ENDIF

        ZEW=ZEW+ZESP/PAPRSF5(JLON,JLEV)
        PAPRSF(JLON,JLEV)=PAPRSF(JLON,JLEV)&
         & -(ZEW5(JLON,JLEV,JIT)&
         & /PAPRSF5(JLON,JLEV)**2)*ZESP  

        ZTW(JLON,JLEV)=ZTW(JLON,JLEV)&
         & +FOEWAD(ZEW,ZTW5(JLON,JLEV,JIT-JJ),ZDELTA)  

      ENDIF

    ENDDO

  ENDDO

!*
!     ------------------------------------------------------------------
!     IV - INITIALIZATION OF THE VARIABLES IN THE NEWTON LOOP, THE
!     TEMPORARY CP AND LH VALUES HAVING TO EVOLVE AT THE SAME TIME AS
!     THE ITERATIVE TW AND QW.

! - TEMPORARY 1D .

! ZCP       : CURRENT VALUE OF ATMOSPHERIC CP DURING THE "NBITER" LOOP.
! ZLH       : AS ZCP BUT FOR THE LATENT HEAT.

  DO JLON=KIDIA,KFDIA
    ZLH(JLON,JLEV)=ZLH(JLON,JLEV)+ZZLH(JLON)
    ZZLH(JLON)=0.0_JPRB

    PT(JLON,JLEV)=PT(JLON,JLEV)+ZTW(JLON,JLEV)
    ZTW(JLON,JLEV)=0.0_JPRB

    PQ(JLON,JLEV)=PQ(JLON,JLEV)-RCPD*ZCP(JLON)
    PQ(JLON,JLEV)=PQ(JLON,JLEV)+RCPV*ZCP(JLON)
    ZCP(JLON)=0.0_JPRB

    PQ(JLON,JLEV)=PQ(JLON,JLEV)+ZPQW(JLON,JLEV)
    ZPQW(JLON,JLEV)=0.0_JPRB
  ENDDO
!*
!     ------------------------------------------------------------------
!     III - COMPUTATION OF THE LOCAL ATMOSPHERIC LATENT HEAT.

  DO JLON=KIDIA,KFDIA

!     SNOW OPTION DEPENDENT COMPUTATIONS.

    IF (LNEIGE) THEN
      ZDELTA=MAX(0.0_JPRB,SIGN(1.0_JPRB,RTT-PT5(JLON,JLEV) ))
    ELSE
      ZDELTA=0.0_JPRB
    ENDIF

!     CALL TO THE DEFINED FUNCTION TO COMPUTE LH.

    PT(JLON,JLEV)=PT(JLON,JLEV)+FOLHAD(ZLH(JLON,JLEV),ZDELTA)
    ZLH(JLON,JLEV)= 0.0_JPRB

  ENDDO

!*
!     ------------------------------------------------------------------

!     VI - END OF THE VERTICAL LOOP.

ENDDO

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