SUBROUTINE LWAITL ( KIDIA , KFDIA, KLON  , KLEV,&
 & PCLDLD5, PCLDLU5, PCLFR5, P_AI5, P_AIC5,&
 & PCLDLD , PCLDLU , PCLFR , P_AI , P_AIC )  

!**** *LWAITL*   -  LONGWAVE RADIATION, CLOUD EFFECTS
!                      (tangent linear)

!     PURPOSE.
!     --------
!           COMPUTES THE WEIGHTS OF THE BLACK BODY-DERIVED FLUXES
!           RADIANCES

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

!        EXPLICIT ARGUMENTS :
!        --------------------
!     ==== INPUTS ===
! PCLDLD5 : (KLON,KLEV)   DOWNWARD EFFECTIVE CLOUD FRACTION (Trajectory)
! PCLDLU5 : (KLON,KLEV)   UPWARD EFFECTIVE CLOUD FRACTION   (Trajectory)
! PCLFR5  : (KLON,KLEV)   CLOUD FRACTIONAL COVER (Trajectory)
! PCLDLD : (KLON,KLEV)    DOWNWARD EFFECTIVE CLOUD FRACTION
! PCLDLU : (KLON,KLEV)    UPWARD EFFECTIVE CLOUD FRACTION
! PCLFR  : (KLON,KLEV)    CLOUD FRACTIONAL COVER

!     ==== OUTPUTS ===
! AI5                     (Trajectory)
! AI
!                     1  ==>  UPWARD   FLUX TOTAL
!                     2  ==>  DOWNWARD FLUX TOTAL

!        IMPLICIT ARGUMENTS :   NONE
!        --------------------

!     METHOD.
!     -------

!          1. INITIALIZES ALL FLUXES TO CLEAR-SKY VALUES
!          2. EFFECT OF ONE OVERCAST UNITY EMISSIVITY CLOUD LAYER
!          3. EFFECT OF SEMI-TRANSPARENT, PARTIAL OR MULTI-LAYERED
!     CLOUDS

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

!          NONE

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

!        SEE RADIATION'S PART OF THE MODEL'S DOCUMENTATION AND
!        ECMWF RESEARCH DEPARTMENT DOCUMENTATION OF THE IFS

!     AUTHOR.
!     -------
!       M. Janiskova  99-11     tangent linear of lwai 

!     MODIFICATIONS.
!     --------------
!       M. Janiskova  2001-05  TL of Raisanen overlap scheme
!       M. Janiskova  2001-11  included safety checks for pclfr
!       M. Janiskova  2003-05  TL version for optimisation of DS&JH
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!-----------------------------------------------------------------------

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

USE YOERAD   , ONLY : NOVLP
USE YOERDI   , ONLY : REPCLC
USE YOEPHLI  , ONLY : LRAISANEN

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLDLD5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLDLU5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLFR5(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: P_AI5(KLON,KLEV+1,KLEV+1,1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: P_AIC5(KLON,KLEV+1) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLDLD(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLDLU(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCLFR(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: P_AI(KLON,KLEV+1,KLEV+1,1) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: P_AIC(KLON,KLEV+1) 
!-----------------------------------------------------------------------

!*       0.1   ARGUMENTS
!              ---------

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

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

REAL(KIND=JPRB) :: ZCLFR5(KLON,KLEV)
REAL(KIND=JPRB) :: ZCLEAR5(KLON) , ZCLOUD5(KLON),  ZCLM5(KLON,KLEV+1,KLEV+1)
REAL(KIND=JPRB) :: ZEMIS5(KLON,KLEV)
REAL(KIND=JPRB) :: ZSUM5(KLON)
REAL(KIND=JPRB) :: ZTMP15(KLON,KLEV+1), ZTMP35(KLON,KLEV+1)
REAL(KIND=JPRB) :: ZTMP45(KLON,KLEV+1), ZTMP55(KLON,KLEV+1)
REAL(KIND=JPRB) :: ZCLFR(KLON,KLEV)
REAL(KIND=JPRB) :: ZCLEAR(KLON) , ZCLOUD(KLON),  ZCLM(KLON,KLEV+1,KLEV+1)
REAL(KIND=JPRB) :: ZEMIS(KLON,KLEV)
REAL(KIND=JPRB) :: ZSUM(KLON)
REAL(KIND=JPRB) :: ZTMP1(KLON,KLEV+1), ZTMP3(KLON,KLEV+1)
REAL(KIND=JPRB) :: ZTMP4(KLON,KLEV+1), ZTMP5(KLON,KLEV+1)

INTEGER(KIND=JPIM) :: IMAXC, IMXM1, IMXP1, JK, JK1, JL, IKL

REAL(KIND=JPRB) :: ZCFRAC, ZNUM, ZDEN, ZCFRAC5, ZNUM5, ZDEN5 
REAL(KIND=JPRB) :: ZTR1, ZTR2, ZCADJ, ZTR15, ZTR25, ZCADJ5
REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!*         1.     INITIALIZATION
!                 --------------

!100  CONTINUE

IF (LHOOK) CALL DR_HOOK('LWAITL',0,ZHOOK_HANDLE)
DO JL = KIDIA,KFDIA
  ZCLOUD5(JL) = 0.0_JPRB 
  ZCLOUD(JL)  = 0.0_JPRB
ENDDO

!GM*******
IMAXC=KLEV
!GM*******

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

!*         2.      EFFECT OF CLOUDINESS ON LONGWAVE FLUXES
!                  ---------------------------------------

IMXP1 = IMAXC + 1
IMXM1 = IMAXC - 1

!*         2.2     CLOUD COVER MATRIX
!                  ------------------

!*    ZCLM(JK1,JK2) IS THE OBSCURATION FACTOR BY CLOUD LAYERS BETWEEN
!     HALF-LEVELS JK1 AND JK2 AS SEEN FROM JK1

!220  CONTINUE

! warning: ZCLM(JL,KLEV+1,JK) is not initialized, because it is not used
! matrix notation for ZCLM is not standard, but:
!    z11  z21  z31  ...
!    z12  z22  z32  ...
!    z13  z23  z33  ...
!     :    :    :    :

!*         2.4     CLOUD COVER BELOW THE LEVEL OF CALCULATION
!                  ------------------------------------------

!240  CONTINUE

IF (LRAISANEN) THEN

!* maximum-random (Raisanen 1998)

  ZEMIS(:,:)  = 0.0_JPRB
  ZEMIS5(:,:) = 0.0_JPRB
  DO JK = 1,KLEV
    IKL = KLEV + 1 - JK
    DO JL = KIDIA,KFDIA
      ZCLFR (JL,IKL) = PCLFR (JL,IKL)
      ZCLFR5(JL,IKL) = PCLFR5(JL,IKL)
      IF (ZCLFR5(JL,IKL) <= REPCLC) THEN
        ZCLFR (JL,IKL) = 0.0_JPRB
        ZCLFR5(JL,IKL) = REPCLC
      ENDIF
      IF (ZCLFR5(JL,IKL) >= 1.0_JPRB-REPCLC) THEN
        ZCLFR (JL,IKL) = 0.0_JPRB
        ZCLFR5(JL,IKL) = 1.0_JPRB-REPCLC
      ENDIF
      ZEMIS(JL,JK)  = PCLDLU(JL,JK)/ZCLFR5(JL,IKL) &
       & - PCLDLU5(JL,JK)*ZCLFR(JL,IKL) &
       & /(ZCLFR5(JL,IKL)**2)  
      ZEMIS5(JL,JK) = PCLDLU5(JL,JK) / ZCLFR5(JL,IKL)
    ENDDO
  ENDDO

  DO JK = 1,KLEV-1
    IKL = KLEV + 1 - JK
    DO JL = KIDIA,KFDIA
      IF (PCLDLU5(JL,JK+1) <= REPCLC) THEN
        ZTMP1 (JL,JK) = 0.0_JPRB
        ZTMP15(JL,JK) = 1.0_JPRB/REPCLC
      ELSE
        ZTMP1 (JL,JK) = -PCLDLU(JL,JK+1)/PCLDLU5(JL,JK+1)**2
        ZTMP15(JL,JK) = 1.0_JPRB/PCLDLU5(JL,JK+1)
      ENDIF
      IF (ZCLFR5(JL,IKL) <= ZCLFR5(JL,IKL-1)) THEN
        ZCADJ  = ZCLFR(JL,IKL)
        ZCADJ5 = ZCLFR5(JL,IKL)
      ELSE
        ZCADJ  = ZCLFR(JL,IKL-1)
        ZCADJ5 = ZCLFR5(JL,IKL-1)
      ENDIF      
      ZTMP3 (JL,JK) = ZCLFR(JL,IKL-1)/(1.0_JPRB-ZCLFR5(JL,IKL-1))**2
      ZTMP35(JL,JK) = 1.0_JPRB/(1.0_JPRB-ZCLFR5(JL,IKL-1))
      ZTMP4 (JL,JK) = (1.0_JPRB-ZEMIS5(JL,JK+1))*ZCADJ &
       & - ZCADJ5*ZEMIS(JL,JK+1)      
      ZTMP45(JL,JK) = ZCADJ5*(1.0_JPRB-ZEMIS5(JL,JK+1))
      ZTMP5 (JL,JK) = ZCLFR(JL,IKL)-ZCADJ
      ZTMP55(JL,JK) = ZCLFR5(JL,IKL)-ZCADJ5
    ENDDO
  ENDDO

  DO JK1 = 2 , KLEV+1
    DO JL = KIDIA,KFDIA
      ZCLOUD (JL) = PCLDLU(JL,JK1-1)
      ZCLOUD5(JL) = PCLDLU5(JL,JK1-1)
      ZSUM (JL)  = ZCLOUD(JL)
      ZSUM5(JL)  = ZCLOUD5(JL)
      ZCLM (JL,JK1-1,JK1) = ZSUM(JL)
      ZCLM5(JL,JK1-1,JK1) = ZSUM5(JL)
    ENDDO
    DO JK = JK1 - 2 , 1 , -1
      IKL = KLEV+1 - JK
      DO JL = KIDIA,KFDIA
        ZTR1  = ZCLOUD5(JL)*ZTMP1(JL,JK) &
         & + ZTMP15(JL,JK)*ZCLOUD(JL)  
        ZTR15 = ZCLOUD5(JL)*ZTMP15(JL,JK)
        ZTR2  = (-ZSUM(JL)+ZCLOUD(JL)-ZCLFR5(JL,IKL-1)*ZTR1 &
         & - ZTR15*ZCLFR(JL,IKL-1))*ZTMP35(JL,JK) &
         & + (1.0_JPRB -ZSUM5(JL)+ZCLOUD5(JL)-ZTR15*ZCLFR5(JL,IKL-1)) &
         & * ZTMP3(JL,JK)  
        ZTR25 = (1.0_JPRB-(ZSUM5(JL)-ZCLOUD5(JL))-ZTR15*ZCLFR5(JL,IKL-1)) &
         & * ZTMP35(JL,JK)  

        ZCLOUD (JL) = (ZTMP45(JL,JK)*ZTR15 + ZTMP55(JL,JK)*ZTR25) &
         & * ZEMIS(JL,JK) &
         & + ZEMIS5(JL,JK)*(ZTMP45(JL,JK)*ZTR1 &
         & + ZTR15*ZTMP4(JL,JK)+ZTMP55(JL,JK)*ZTR2 &
         & + ZTR25*ZTMP5(JL,JK))  
        ZCLOUD5(JL) = ZEMIS5(JL,JK) &
         & * (ZTMP45(JL,JK)*ZTR15 + ZTMP55(JL,JK)*ZTR25)  

        ZSUM(JL)  = ZSUM(JL) + ZCLOUD(JL)
        ZSUM5(JL) = ZSUM5(JL) + ZCLOUD5(JL)
        ZCLM (JL,JK,JK1) = ZSUM(JL)
        ZCLM5(JL,JK,JK1) = ZSUM5(JL)
      ENDDO
    ENDDO
  ENDDO

ELSE

  DO JK1 = 2 , KLEV+1
    DO JL = KIDIA,KFDIA
      ZCLEAR(JL) =0.0_JPRB
      ZCLEAR5(JL)=1.0_JPRB
      ZCLOUD(JL) =0.0_JPRB
      ZCLOUD5(JL)=0.0_JPRB
    ENDDO

    DO JK = JK1 - 1 , 1 , -1
      DO JL = KIDIA,KFDIA
        IF (NOVLP == 1) THEN
!* maximum-random    Geleyn and Hollingsworth 1979
          IF (PCLDLU5(JL,JK) >= ZCLOUD5(JL)) THEN
            ZNUM = PCLDLU (JL,JK)
            ZNUM5= PCLDLU5(JL,JK)
          ELSE
            ZNUM = ZCLOUD(JL)
            ZNUM5= ZCLOUD5(JL)
          ENDIF
          IF (ZCLOUD5(JL) <= 1.0_JPRB-REPCLC) THEN
            ZDEN = ZCLOUD(JL)
            ZDEN5= ZCLOUD5(JL)
          ELSE
            ZDEN = 0.0_JPRB
            ZDEN5= 1.0_JPRB-REPCLC
          ENDIF
          ZCLEAR(JL)=ZCLEAR(JL)*(1.0_JPRB-ZNUM5)/(1.0_JPRB-ZDEN5)&
           & +ZCLEAR5(JL)*(  -ZNUM )/(1.0_JPRB-ZDEN5)&
           & -ZCLEAR5(JL)*(1.0_JPRB-ZNUM5)*(  -ZDEN )/((1.0_JPRB-ZDEN5)**2)  
          ZCLEAR5(JL)=ZCLEAR5(JL)*(1.0_JPRB-ZNUM5)/(1.0_JPRB-ZDEN5)
          ZCLM (JL,JK,JK1) = - ZCLEAR(JL)
          ZCLM5(JL,JK,JK1) = 1.0_JPRB - ZCLEAR5(JL)
          ZCLOUD (JL) = PCLDLU(JL,JK)
          ZCLOUD5(JL) = PCLDLU5(JL,JK)
        ELSEIF (NOVLP == 2) THEN
!* maximum
          IF (PCLDLU5(JL,JK) >= ZCLOUD5(JL)) THEN
            ZCLOUD (JL) = PCLDLU(JL,JK)
            ZCLOUD5(JL) = PCLDLU5(JL,JK)
          ENDIF
          ZCLM (JL,JK,JK1) = ZCLOUD(JL)
          ZCLM5(JL,JK,JK1) = ZCLOUD5(JL)
        ELSEIF (NOVLP == 3) THEN
!* random
          ZCLEAR(JL)  = ZCLEAR(JL)*(1.0_JPRB - PCLDLU5(JL,JK))&
           & + ZCLEAR5(JL)*(-PCLDLU(JL,JK))  
          ZCLEAR5(JL) = ZCLEAR5(JL)*(1.0_JPRB - PCLDLU5(JL,JK))
          ZCLOUD (JL) = - ZCLEAR(JL)
          ZCLOUD5(JL) = 1.0_JPRB - ZCLEAR5(JL)
          ZCLM (JL,JK,JK1) = ZCLOUD(JL)
          ZCLM5(JL,JK,JK1) = ZCLOUD5(JL)
        ENDIF
      ENDDO
    ENDDO

  ENDDO

ENDIF

!*         2.5     CLOUD COVER ABOVE THE LEVEL OF CALCULATION
!                  ------------------------------------------

!250  CONTINUE

IF (LRAISANEN) THEN
!* maximum-random (Raisanen 1998)

  ZEMIS(:,:)  = 0.0_JPRB
  ZEMIS5(:,:) = 0.0_JPRB

  DO JK = 1,KLEV
    IKL = KLEV + 1 - JK
    DO JL = KIDIA,KFDIA
      ZEMIS(JL,JK)  = PCLDLD(JL,JK)/ZCLFR5(JL,IKL) &
       & - PCLDLD5(JL,JK)*ZCLFR(JL,IKL) &
       & /(ZCLFR5(JL,IKL)**2)  
      ZEMIS5(JL,JK) = PCLDLD5(JL,JK) / ZCLFR5(JL,IKL)
    ENDDO
  ENDDO

  DO JK = 2,KLEV
    IKL = KLEV + 1 - JK
    DO JL = KIDIA,KFDIA
      IF (PCLDLD5(JL,JK-1) <= REPCLC) THEN
        ZTMP1 (JL,JK) = 0.0_JPRB
        ZTMP15(JL,JK) = 1.0_JPRB/REPCLC
      ELSE
        ZTMP1 (JL,JK) = -PCLDLD(JL,JK-1)/PCLDLD5(JL,JK-1)**2
        ZTMP15(JL,JK) = 1.0_JPRB/PCLDLD5(JL,JK-1)
      ENDIF
      IF (ZCLFR5(JL,IKL) <= ZCLFR5(JL,IKL+1)) THEN
        ZCADJ  = ZCLFR(JL,IKL)
        ZCADJ5 = ZCLFR5(JL,IKL)
      ELSE
        ZCADJ  = ZCLFR(JL,IKL+1)
        ZCADJ5 = ZCLFR5(JL,IKL+1)
      ENDIF     
      ZTMP3 (JL,JK) = ZCLFR(JL,IKL+1)/(1.0_JPRB-ZCLFR5(JL,IKL+1))**2
      ZTMP35(JL,JK) = 1.0_JPRB/(1.0_JPRB-ZCLFR5(JL,IKL+1))
      ZTMP4 (JL,JK) = (1.0_JPRB-ZEMIS5(JL,JK-1))*ZCADJ &
       & - ZCADJ5*ZEMIS(JL,JK-1)  
      ZTMP45(JL,JK) = ZCADJ5*(1.0_JPRB-ZEMIS5(JL,JK-1))
      ZTMP5 (JL,JK) = ZCLFR(JL,IKL)-ZCADJ
      ZTMP55(JL,JK) = ZCLFR5(JL,IKL)-ZCADJ5
    ENDDO
  ENDDO

  DO JK1 = 1 , KLEV
    DO JL = KIDIA,KFDIA
      ZCLOUD (JL) = PCLDLD(JL,JK1)
      ZCLOUD5(JL) = PCLDLD5(JL,JK1)
      ZSUM (JL)  = ZCLOUD(JL)
      ZSUM5(JL)  = ZCLOUD5(JL)
      ZCLM (JL,JK1,JK1) = ZSUM(JL)
      ZCLM5(JL,JK1,JK1) = ZSUM5(JL)
    ENDDO

    DO JK = JK1+1 , KLEV
      IKL = KLEV + 1 - JK
      DO JL = KIDIA,KFDIA
        ZTR1  = ZCLOUD5(JL)*ZTMP1(JL,JK) &
         & + ZTMP15(JL,JK)*ZCLOUD(JL)  
        ZTR15 = ZCLOUD5(JL)*ZTMP15(JL,JK)

        ZTR2  = (-ZSUM(JL)+ZCLOUD(JL)-ZCLFR5(JL,IKL+1)*ZTR1 &
         & - ZTR15*ZCLFR(JL,IKL+1))*ZTMP35(JL,JK) &
         & + (1.0_JPRB -ZSUM5(JL)+ZCLOUD5(JL)-ZTR15*ZCLFR5(JL,IKL+1)) &
         & * ZTMP3(JL,JK)  
        ZTR25 = (1.0_JPRB-(ZSUM5(JL)-ZCLOUD5(JL))-ZTR15*ZCLFR5(JL,IKL+1)) &
         & * ZTMP35(JL,JK)  
        ZCLOUD (JL) = (ZTMP45(JL,JK)*ZTR15 + ZTMP55(JL,JK)*ZTR25) &
         & * ZEMIS(JL,JK) &
         & + ZEMIS5(JL,JK)*(ZTMP45(JL,JK)*ZTR1 &
         & + ZTR15*ZTMP4(JL,JK)+ZTMP55(JL,JK)*ZTR2 &
         & + ZTR25*ZTMP5(JL,JK))  
        ZCLOUD5(JL) = ZEMIS5(JL,JK) &
         & * (ZTMP45(JL,JK)*ZTR15 + ZTMP55(JL,JK)*ZTR25)  

        ZSUM(JL)  = ZSUM(JL) + ZCLOUD(JL)
        ZSUM5(JL) = ZSUM5(JL) + ZCLOUD5(JL)
        ZCLM (JL,JK,JK1)  = ZSUM(JL)
        ZCLM5(JL,JK,JK1) = ZSUM5(JL)
      ENDDO
    ENDDO
  ENDDO

ELSE

  DO JK1 = 1 , KLEV
    DO JL = KIDIA,KFDIA
      ZCLEAR (JL)=0.0_JPRB
      ZCLEAR5(JL)=1.0_JPRB
      ZCLOUD (JL)=0.0_JPRB
      ZCLOUD5(JL)=0.0_JPRB
    ENDDO

    DO JK = JK1 , KLEV
      DO JL = KIDIA,KFDIA
        IF (NOVLP == 1) THEN
!* maximum-random
          IF (PCLDLD5(JL,JK) >= ZCLOUD5(JL)) THEN
            ZNUM = PCLDLD (JL,JK)
            ZNUM5= PCLDLD5(JL,JK)
          ELSE
            ZNUM = ZCLOUD(JL)
            ZNUM5= ZCLOUD5(JL)
          ENDIF
          IF (ZCLOUD5(JL) <= 1.0_JPRB-REPCLC) THEN
            ZDEN = ZCLOUD(JL)
            ZDEN5= ZCLOUD5(JL)
          ELSE
            ZDEN = 0.0_JPRB
            ZDEN5= 1.0_JPRB-REPCLC
          ENDIF
          ZCLEAR(JL)=ZCLEAR(JL)*(1.0_JPRB-ZNUM5)/(1.0_JPRB-ZDEN5)&
           & +ZCLEAR5(JL)*(  -ZNUM )/(1.0_JPRB-ZDEN5)&
           & -ZCLEAR5(JL)*(1.0_JPRB-ZNUM5)*(  -ZDEN )/((1.0_JPRB-ZDEN5)**2)  
          ZCLEAR5(JL)=ZCLEAR5(JL)*(1.0_JPRB-ZNUM5)/(1.0_JPRB-ZDEN5)
          ZCLM (JL,JK,JK1) = - ZCLEAR(JL)
          ZCLM5(JL,JK,JK1) = 1.0_JPRB - ZCLEAR5(JL)
          ZCLOUD (JL) = PCLDLD(JL,JK)
          ZCLOUD5(JL) = PCLDLD5(JL,JK)
        ELSEIF (NOVLP == 2) THEN
!* maximum
          IF (PCLDLD5(JL,JK) >= ZCLOUD5(JL)) THEN
            ZCLOUD (JL) = PCLDLD(JL,JK)
            ZCLOUD5(JL) = PCLDLD5(JL,JK)
          ENDIF
          ZCLM (JL,JK,JK1) = ZCLOUD(JL)
          ZCLM5(JL,JK,JK1) = ZCLOUD5(JL)
        ELSEIF (NOVLP == 3) THEN
!* random
          ZCLEAR(JL)  = ZCLEAR(JL)*(1.0_JPRB - PCLDLD5(JL,JK))&
           & + ZCLEAR5(JL)*(-PCLDLD(JL,JK))  
          ZCLEAR5(JL) = ZCLEAR5(JL)*(1.0_JPRB - PCLDLD5(JL,JK))
          ZCLOUD (JL) = - ZCLEAR(JL)
          ZCLOUD5(JL) = 1.0_JPRB - ZCLEAR5(JL)
          ZCLM (JL,JK,JK1) = ZCLOUD(JL)
          ZCLM5(JL,JK,JK1) = ZCLOUD5(JL)
        ENDIF
      ENDDO
    ENDDO
  ENDDO

ENDIF

!*         3.      FLUXES FOR PARTIAL/MULTIPLE LAYERED CLOUDINESS
!                  ----------------------------------------------

!300  CONTINUE

!AI  = _ZERO_
!AI5 = _ZERO_
DO JL = KIDIA,KFDIA
  P_AIC (JL,KLEV+1) = 0.0_JPRB
  P_AIC5(JL,KLEV+1) = 0.0_JPRB
ENDDO

!*         3.1     DOWNWARD FLUXES
!                  ---------------

!310  CONTINUE

DO JK1 = 1 , KLEV

!*                 CONTRIBUTION FROM CLEAR-SKY FRACTION

  DO JL = KIDIA,KFDIA
    P_AIC (JL,JK1) = - ZCLM(JL,KLEV,JK1)
    P_AIC5(JL,JK1) = (1.0_JPRB - ZCLM5(JL,KLEV,JK1))

!*                 CONTRIBUTION FROM ADJACENT CLOUD

    P_AI (JL,JK1+1,JK1,1) = ZCLM(JL,JK1,JK1)
    P_AI5(JL,JK1+1,JK1,1) = ZCLM5(JL,JK1,JK1)
  ENDDO

!*                 CONTRIBUTION FROM OTHER CLOUDY FRACTIONS

  DO JK = JK1, KLEV-1 
    DO JL = KIDIA,KFDIA
      ZCFRAC  = ZCLM(JL,JK+1,JK1) - ZCLM(JL,JK,JK1)
      ZCFRAC5 = ZCLM5(JL,JK+1,JK1) - ZCLM5(JL,JK,JK1)
      P_AI (JL,JK+2,JK1,1) =  ZCFRAC
      P_AI5(JL,JK+2,JK1,1) =  ZCFRAC5
    ENDDO
  ENDDO

ENDDO

!*         3.2     UPWARD FLUX AT THE SURFACE
!                  --------------------------

DO JL = KIDIA,KFDIA
  P_AI (JL,1,1,1) = 0.0_JPRB
  P_AI5(JL,1,1,1) = 1.0_JPRB
ENDDO

!320  CONTINUE

!*         3.3     UPWARD FLUXES
!                  -------------

!330  CONTINUE

DO JK1 = 2 , KLEV+1

!*                 CONTRIBUTION FROM CLEAR-SKY FRACTION

  DO JL = KIDIA,KFDIA
    P_AI (JL,1,JK1,1) = - ZCLM(JL,1,JK1)
    P_AI5(JL,1,JK1,1) = (1.0_JPRB - ZCLM5(JL,1,JK1))
  ENDDO

!*                 CONTRIBUTION FROM OTHER CLOUDY FRACTIONS

  DO JK = 2 , JK1-1
    DO JL = KIDIA,KFDIA
      ZCFRAC  = ZCLM(JL,JK-1,JK1) - ZCLM(JL,JK,JK1)
      ZCFRAC5 = ZCLM5(JL,JK-1,JK1) - ZCLM5(JL,JK,JK1)
      P_AI (JL,JK,JK1,1) = ZCFRAC
      P_AI5(JL,JK,JK1,1) = ZCFRAC5
    ENDDO
  ENDDO

!*                 CONTRIBUTION FROM ADJACENT CLOUD

  DO JL = KIDIA,KFDIA
    P_AI (JL,JK1,JK1,1) =  ZCLM(JL,JK1-1,JK1)
    P_AI5(JL,JK1,JK1,1) =  ZCLM5(JL,JK1-1,JK1)
  ENDDO

ENDDO

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

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

