!OPTIONS XOPT(NOEVAL)
SUBROUTINE ACNEBN ( KIDIA,KFDIA,KLON,KTDIA,KLEV,&
 !-----------------------------------------------------------------------
 ! - INPUT  2D .
 & PAPHIF,PCP,PQ,PQL,PQI,PQSAT,PT,PFPLC,PDELP,PRDELP,PAPRSF,PUNEBH,&
 & PNEBS,PQLIS,&
 ! - OUTPUT 2D .
 & PNEB,PNEBC,PQICE,PQLI,&
 ! - CONSTANTES .
 & PHUC,&
 ! - LOCK ARGUMENTS .
 & CDLOCK)  

!**** *ACNEBN * - CALCUL DES DIAGNOSTICS NUAGEUX .

!     Sujet.
!     ------
!     - ROUTINE DE CALCUL ACTIF .
!       CALCUL DES DIAGNOSTICS NUAGEUX .

!**   Interface.
!     ----------
!        *CALL* *ACNEBN*

!-----------------------------------------------------------------------
! WARNING: THE ENGLISH VERSION OF VARIABLES' NAMES IS TO BE READ IN THE
!          "APLPAR" CODE.
!-----------------------------------------------------------------------

! -   ARGUMENTS D'ENTREE.
!     -------------------

! - NOM DES PARAMETRES DE DIMENSIONNEMENT DE LA PHYSIQUE.

! KIDIA      : INDICE DE DEPART DES BOUCLES VECTORISEES SUR L'HORIZONT..
! KFDIA      : INDICE DE FIN DES BOUCLES VECTORISEES SUR L'HORIZONTALE.
! KLON       : DIMENSION HORIZONTALE DES TABLEAUX.
! KTDIA      : INDICE DE DEPART DES BOUCLES VERTICALES (1 EN GENERAL).
! KLEV       : DIMENSION VERTICALE DES TABLEAUX "FULL LEVEL".

! - NOM DES VARIABLES DE LA PHYSIQUE (PAR ORDRE ALPHABETIQUE DANS CHAQUE
!   CATEGORIE).

! - 2D (1:KLEV) .

! PFPLC      : PRECIPITATIONS CONVECTIVES.
! PQ         : HUMIDITE SPECIFIQUE DE LA VAPEUR D'EAU.
! PQL        : HUMIDITE SPECIFIQUE DE L'EAU CONDENSEE LIQUIDE.
! PQI        : HUMIDITE SPECIFIQUE DE L'EAU CONDENSEE SOLIDE.
! PQSAT      : HUMIDITE SPECIFIQUE DE SATURATION.
! PRDELP     : INVERSE DE L'EPAISSEUR EN PRESSION DE LA COUCHE.
! PAPRSF     : PRESSION AUX NIVEAUX DES COUCHES.
! PT         : TEMPERATURE.

! - 2D (1:KLEV) .
                                                                                
! PNEBS      : NEBULOSITE STRATIFORME
! PQLIS      : CONDENSATS NUAGEUX STRATIFORME
                                                                                
! - CONSTANTES .

! PHUC(KLEV) : HUMIDITE CRITIQUE (PAR NIVEAU) POUR LE CALCUL DE PNEB.

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

! -   ARGUMENTS DE SORTIE.
!     --------------------

! - 2D (1:KLEV) .

! PNEB       : NEBULOSITE PARTIELLE "RADIATIVE".
! PNEBC      : NEBULOSITE CONVECTIVE "RADIATIVE".
! PQICE      : HUMIDITE SPECIFIQUE SOLIDE "RADIATIVE".
! PQLI       : HUMIDITE SPECIFIQUE LIQUIDE "RADIATIVE".

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

! -   ARGUMENTS IMPLICITES.
!     ---------------------

! COMMON/YOMPHY /
! COMMON/YOMCST /
! COMMON/YOMPHY0/
! COMMON/YOMPHY2/

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

!     Externes.
!     ---------

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

!     Auteur.
!     -------
!        97-02, J.M. Piriou.

!     Modifications.
!     --------------
!        97-04, QLIMI renomme en QSUSX - J.M. Piriou.
!        97-08, PZx variables > Zx, introduce CDLOCK - J.M. Piriou.
!        99-09, Nebulosite de convection peu profonde (QSSC) - J.M.Piriou.
!      2001-11, Reproductibilite en KLON du calcul - J.M. Piriou / J.F. Geleyn.
!      2002-02, (i) Eau liquide de CVPP par simple boucle verticale (ii) nebulosite type Xu et Randall - J.M. Piriou.
!      2002-10, Change definition of PQLI and PQICE: they become grid-size values - J.M. Piriou. (phasage Y. Bouteloup)
!      2003-02, Suppression THETAV - F. Bouyssel
!      2003-03, Remplacement de QSUSX par QSUSXC et QSUSXS - F. Bouyssel
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!      2004-09, Amelioration de QSAT en cas d'inversion thermique - R. Brozkova
!      2005-03, Change due to Lopez microphysics - F. Bouyssel
!      2005-07, Introduction of LRNUMX in Lopez microphysics - F. Bouyssel
!      2006-10, Specific autoconversion thresholds for convection - F. Bouyssel
!      03-Oct-2006, ALARO-0 phasing (PQL, PQI, L3MT, LSTRAPRO) - M. Bellus
!      2007-02, Convective condensate calculation for 3MT - J.F. Geleyn
!      2007-02, Externalisation du calcul des nebulosites partielles - R. Brozkova
!      2007-05, Cloud water from the shallow convection (used with the AROME scheme) - E. Bazile
!      2008-05, remove the Cloud water from the shallow convection ! - E. Bazile
!-----------------------------------------------------------------------

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

USE YOMPHY   , ONLY : LNEIGE   ,LRNUMX   ,LNEBNXR  ,LCONDWT  ,LPROCLD,&
            &L3MT     ,LSTRAPRO ,LQXRTGH
USE YOMCST   , ONLY : RG       ,RV       ,RCPV     ,RETV     ,&
            &RCW      ,RCS      ,RLVTT    ,RLSTT    ,RTT      ,&
            &RALPW    ,RBETW    ,RGAMW    ,RALPS    ,RBETS    ,&
            &RGAMS    ,RALPD    ,RBETD    ,RGAMD    ,RDT
USE YOMPHY0  , ONLY : QSNEBC   ,QSNEBS   ,QSSUSC   ,QSSUSS, &
 & QSSC     ,QSUSXC  ,QSUSXS   ,QSSUSV   ,RPHI0    , RPHIR    ,&
 & RDTFAC   ,RQLCV   ,RQICVMAX ,RQICVMIN ,RQICRT1  , RQICRT2  ,&
 & SXNBCO   ,QXRAL   ,QXRHX    ,QXRTGH
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)    :: PAPHIF(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQL(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQI(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQSAT(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PFPLC(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDELP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PRDELP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPRSF(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PUNEBH(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PNEBS(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQLIS(KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PNEB(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PNEBC(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQICE(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQLI(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PHUC(KLEV) 
CHARACTER(LEN=7)  ,INTENT(IN)    :: CDLOCK 
REAL(KIND=JPRB) :: ZS(KLON,KLEV),ZSDELP(KLON,KLEV)&
 & ,ZQSSTPP(KLON,KLEV),ZQS(KLON,KLEV), ZQSC(KLON,KLEV), ZQSS(KLON,KLEV)&
 & ,ZQSAT(KLON,KLEV),ZTGRAD(KLON,KLEV),ZINQ(KLON,KLEV)&
 & ,ZSNEBC(KLON,KLEV),ZSUSXC(KLON,KLEV),ZFACQSC(KLON,KLEV)&
 & ,ZSUM(KLON),ZPHIREL1(KLON)  
INTEGER(KIND=JPIM) :: ILEVREF(KLON),ILEVTES(KLON)

INTEGER(KIND=JPIM) :: JLEV, JLON, JLEV2, ILEV, ILEVX, ILEVSIG

REAL(KIND=JPRB) :: ZDELTA, ZEPSNEB, ZNEBS, ZPLS &
 & ,ZDELTAP,ZEPSDS,ZDSN,ZD3,ZMASK1,ZMASK2,ZFRAC,ZEPSQC,ZSSUSS &
 & ,ZTFROID, ZEW, ZESP, ZPHIRSQ, ZPHIREL2, ZICE, ZQCR, ZQICR &
 & ,ZARG1,ZARG2,ZALPH,ZBETA,ZTAUX,ZQTOT,ZEPS1,ZEPS2,ZARGLI &
 & ,ZRH,ZRHEXP,ZRHLIM,ZRHLIMPR,ZEXPR,ZBIN

LOGICAL :: LLSC, LLSTRAT
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "abor1.intfb.h"
#include "acnebxrs.intfb.h"
#include "fcttrm.h"
#include "fctdoi.h"

!     CHECK RELIABILITY OF INPUT ARGUMENTS.

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

!     ------------------------------------------------------------------
!     INITIALISATION.

ZEPSNEB=1.E-10_JPRB
ZEPSQC=1.E-10_JPRB
ZEPSDS=1._JPRB
ZTAUX=3600._JPRB
LLSC=QSSC /= 0.0_JPRB
LLSTRAT=RPHI0 /= 0.0_JPRB
ZQSAT(:,:)=PQSAT(:,:)
ZPHIRSQ=SQRT(MAX(ZEPSNEB,RPHIR))

ZEPS1=1.E-6_JPRB
ZEPS2=1.E-10_JPRB
ZARGLI=125._JPRB**(1.0_JPRB/QXRTGH)

IF (QSNEBC >= 0.0_JPRB) THEN
  DO JLEV=KTDIA,KLEV
    DO JLON=KIDIA,KFDIA
      ZSNEBC (JLON,JLEV)=QSNEBC
      ZSUSXC (JLON,JLEV)=QSUSXC
      ZFACQSC(JLON,JLEV)=TSPHY/QSUSXC
    ENDDO
  ENDDO
ELSE
  ZARG1 = 2.0_JPRB*RQICVMAX*(1.0_JPRB-0.999_JPRB)/(RQICVMAX-RQICVMIN)-1.0_JPRB
  ZARG2 = 2.0_JPRB*(RQICVMAX-1.5_JPRB*RQICVMIN)/(RQICVMAX-RQICVMIN)-1.0_JPRB
  ZARG1 = 0.5_JPRB*LOG(ABS((1.0_JPRB+ZARG1)/(1.0_JPRB-ZARG1)))
  ZARG2 = 0.5_JPRB*LOG(ABS((1.0_JPRB+ZARG2)/(1.0_JPRB-ZARG2)))
  ZALPH = (ZARG1 - ZARG2)/(RQICRT2-RQICRT1)
  ZBETA = ZARG1 - RQICRT2 * ZALPH
  DO JLEV=KTDIA,KLEV
    DO JLON=KIDIA,KFDIA
      ZICE = FONICE(PT(JLON,JLEV))
      ZQICR = RQICVMAX-(RQICVMAX-RQICVMIN)*0.5_JPRB &
     & *(1.0_JPRB+TANH(ZALPH*(PT(JLON,JLEV)-RTT)+ZBETA))
      ZQCR = RQLCV*(1.0_JPRB-ZICE)+ZQICR*ZICE
      ZSNEBC (JLON,JLEV)=1.0_JPRB/ZQCR
      ZSUSXC (JLON,JLEV)=SXNBCO*ZQCR
      ZFACQSC(JLON,JLEV)=ZTAUX/PQSAT(JLON,JLEV)
    ENDDO
  ENDDO
ENDIF

IF (.NOT.(LCONDWT.AND.LPROCLD)) THEN

! ENERGIE STATIQUE SECHE.

   DO JLEV=KTDIA,KLEV
     DO JLON=KIDIA,KFDIA
       ZS(JLON,JLEV)=PCP(JLON,JLEV)*PT(JLON,JLEV)+PAPHIF(JLON,JLEV)
       ZQSSTPP(JLON,JLEV)=0.0_JPRB
       ZSDELP(JLON,JLEV)=0.0_JPRB
     ENDDO
   ENDDO

! SATURATION MODIFIEE EN CAS D'INVERSION THERMIQUE
! MODIFIED SATURATION IN CASE OF TEMPERATURE INVERSION 

   IF(LLSTRAT) THEN
                                                                                
     DO JLEV=KTDIA+1,KLEV
       DO JLON=KIDIA,KFDIA
         ZTGRAD(JLON,JLEV)=MAX(ZEPSNEB,(PT(JLON,JLEV)-PT(JLON,JLEV-1))*RPHI0/&
      &                    (PAPHIF(JLON,JLEV)-PAPHIF(JLON,JLEV-1)))
         ZINQ(JLON,JLEV)=1.0_JPRB/ZTGRAD(JLON,JLEV)
       ENDDO
     ENDDO
     DO JLEV=KLEV,KTDIA+2,-1
       ZSUM(:)=0.0_JPRB
       ZPHIREL1(:)=0.0_JPRB
!cdir unroll=8
       DO ILEV=JLEV,KTDIA+1,-1
         DO JLON=KIDIA,KFDIA
           ZPHIREL2=MIN(SQRT(PAPHIF(JLON,ILEV-1)-PAPHIF(JLON,JLEV)),ZPHIRSQ)
           ZSUM(JLON) = ZSUM(JLON) + ZINQ(JLON,ILEV)*(ZPHIREL2-ZPHIREL1(JLON))
           ZPHIREL1(JLON)=ZPHIREL2
         ENDDO
       ENDDO
       DO JLON=KIDIA,KFDIA
         ZTGRAD(JLON,JLEV)=ZPHIRSQ/ZSUM(JLON)
       ENDDO
     ENDDO
                                                                                
     DO JLEV=KTDIA+1,KLEV
       DO JLON=KIDIA,KFDIA
         ZTFROID = PT(JLON,JLEV)- ZTGRAD(JLON,JLEV)
         IF (LNEIGE) THEN
           ZDELTA=MAX(0.0_JPRB,SIGN(1.0_JPRB,RTT-ZTFROID))
         ELSE
           ZDELTA=0.0_JPRB
         ENDIF
                                                                                
         ZEW= FOEW (ZTFROID,ZDELTA)
         ZESP=ZEW/PAPRSF(JLON,JLEV)
         ZQSAT(JLON,JLEV)=FOQS (ZESP)
       ENDDO
     ENDDO
   ENDIF
!     ------------------------------------------------------------------
!     DIAGNOSTICS D'EAU CONDENSEE ET DE NEBULOSITE.
!     CLOUD CONTENTS AND CLOUDINESS DIAGNOSTIC.

   DO JLON=KIDIA,KFDIA
     ILEVTES(JLON)=KLEV
   ENDDO
   ILEVX=KLEV
   DO JLEV=KLEV,KTDIA,-1

! INTEGRALE VERTICALE DE L'ECART ENTRE L'HUMIDITE
! DU NIVEAU COURANT JLEV2 ET LA VALEUR SATURANTE AU NIVEAU HAUT JLEV.
! ZDELTAP EST LE PRODUIT DE L'EPAISSEUR-PRESSION DE LA COUCHE COURANTE
! ET D'UN REEL VALANT 1 SI L'ENERGIE STATIQUE SECHE DE LA COUCHE
! COURANTE EST SUPERIEURE A CELLE DU HAUT, ET TENDANT VERS ZERO
! LORSQUE SON ECART A CELLE DU HAUT DESCEND EN DECA DU SEUIL -QSSC.

     IF(LLSC) THEN
       ILEV=ILEVX
     ELSE
       ILEV=JLEV
     ENDIF
     ILEVX=JLEV

     DO JLON=KIDIA,KFDIA
       IF(LLSC) THEN
         ILEVREF(JLON)=ILEVTES(JLON)
       ELSE
         ILEVREF(JLON)=JLEV
       ENDIF
       ILEVTES(JLON)=JLEV
     ENDDO

     DO JLEV2=JLEV,ILEV
       DO JLON=KIDIA,KFDIA
         ZDSN=(ZS(JLON,JLEV)-ZS(JLON,JLEV2))/MAX(ZEPSDS,QSSC)
         ZD3=1.0_JPRB+ZDSN*ZDSN*(2.0_JPRB*ZDSN-3._JPRB)
         ZMASK1=MAX(0.0_JPRB,SIGN(1.0_JPRB,0.0_JPRB-ZDSN))
         ZMASK2=MAX(0.0_JPRB,SIGN(1.0_JPRB,1.0_JPRB-ZDSN))
         ZFRAC=ZMASK1+(1.0_JPRB-ZMASK1)*ZMASK2*ZD3
         ZFRAC=ZFRAC*REAL(MAX(0,MIN(1,ILEVREF(JLON)-JLEV2+1)),JPRB)
         ZDELTAP=ZFRAC*PDELP(JLON,JLEV2)
         ILEVSIG=JLEV2*NINT(MAX(0.0_JPRB,-SIGN(1.0_JPRB,0.0_JPRB-ZFRAC)))
         ILEVX=MAX(ILEVX,ILEVSIG)
         ILEVTES(JLON)=MAX(ILEVTES(JLON),ILEVSIG)
         IF(L3MT.OR.LSTRAPRO) THEN
           ZQTOT=PQ(JLON,JLEV2)+PQL(JLON,JLEV2)+PQI(JLON,JLEV2)
         ELSE
           ZQTOT=PQ(JLON,JLEV2)
         ENDIF
         ZQSSTPP(JLON,JLEV)=ZQSSTPP(JLON,JLEV) &
          & +MAX(0.0_JPRB,ZQTOT-PHUC(JLEV)*ZQSAT(JLON,JLEV)) &
          & *ZDELTAP  
         ZSDELP(JLON,JLEV)=ZSDELP(JLON,JLEV)+ZDELTAP
       ENDDO
     ENDDO

     DO JLON=KIDIA,KFDIA

!             PARTIE STRATIFORME.
!             STRATIFORM PART.

       ZSSUSS=QSSUSS/SQRT(1.0_JPRB+(QSSUSV*PQSAT(JLON,JLEV))**2)
       ZQSS(JLON,JLEV)=ZSSUSS*ZQSSTPP(JLON,JLEV)/ZSDELP(JLON,JLEV)
       ZQSS(JLON,JLEV)=QSUSXS*(1.0_JPRB-EXP(-ZQSS(JLON,JLEV)/QSUSXS))

     ENDDO
   ENDDO

ELSE

   DO JLEV=KTDIA,KLEV
     DO JLON=KIDIA,KFDIA
       ZQSS(JLON,JLEV)=PQLIS(JLON,JLEV)
     ENDDO
   ENDDO

ENDIF

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

!             PARTIE CONVECTIVE.
!             CONVECTIVE PART.

    IF (L3MT) THEN

      !   Calculate convective condensate using the convective cloudiness
      !   of last time step

      IF (LQXRTGH) THEN
        ZRH=MIN(ZARGLI,MAX(ZEPS1,PQ(JLON,JLEV)/PQSAT(JLON,JLEV)))
        ZRHEXP=EXP(-2.0_JPRB*ZRH**QXRTGH)
        ZRH=((1.0_JPRB-ZRHEXP)/(1.0_JPRB+ZRHEXP))**(1.0_JPRB/QXRTGH)
        ZBIN=0.0_JPRB
      ELSE
        ZRH=MIN(QXRHX,PQ(JLON,JLEV)/PQSAT(JLON,JLEV))
        ZBIN=MAX(0.0_JPRB,SIGN(1.0_JPRB,ZRH-1.0_JPRB))
      ENDIF

      ZRHLIM=MAX(ZEPS1,MIN(1.0_JPRB-ZEPS1,ZRH))

! Caution: Expressions bolow for ZRHLIMPR and ZEXPR are valid for
! QXRDEL=0.5 and QXRR=0.25 only, which is more efficient
! than the full formula which reads:
!!! ZQSC(JLON,JLEV)=-((1.0_JPRB-ZRHLIM)*PQSAT(JLON,JLEV))**QXRDEL/QXRAL*&
!!!  &LOG(MAX(ZEPS2,1.0_JPRB-PUNEBH(JLON,JLEV)/(ZRHLIM**QXRR) ))

      ZRHLIMPR=SQRT(SQRT(ZRHLIM))
      ZEXPR=SQRT((1.0_JPRB-ZRHLIM)*PQSAT(JLON,JLEV))*(1.0_JPRB/QXRAL)
      ZQSC(JLON,JLEV)=-ZEXPR*LOG(MAX(ZEPS2,1.0_JPRB-PUNEBH(JLON,JLEV)/ZRHLIMPR))

      ZQS(JLON,JLEV)=ZQSS(JLON,JLEV)+ZQSC(JLON,JLEV)

      PNEB(JLON,JLEV)=ZRHLIMPR*(1.0_JPRB-EXP(-(ZQS(JLON,JLEV)/ZEXPR)))
      PNEB(JLON,JLEV)=MAX(ZEPS1,MIN(1.0_JPRB-ZEPS1,ZBIN+(1.0_JPRB-ZBIN)&
       &*PNEB(JLON,JLEV)))

      PNEBC(JLON,JLEV)=ZQSC(JLON,JLEV) &
       & /MAX(ZEPSQC,ZQS(JLON,JLEV))*PNEB(JLON,JLEV)

    ELSE

      ZQSC(JLON,JLEV)=MAX(0.0_JPRB,PFPLC(JLON,JLEV)-PFPLC(JLON,JLEV-1))&
       & *PRDELP(JLON,JLEV)*RG*QSSUSC
      ZQSC(JLON,JLEV)=ZSUSXC(JLON,JLEV)&
       & *(1.0_JPRB-EXP(-ZQSC(JLON,JLEV)*ZFACQSC(JLON,JLEV)))

!             TOTAL.

      IF ( LCONDWT.AND.LPROCLD.AND.LRNUMX ) THEN
        ZQS(JLON,JLEV)=MAX(ZQSS(JLON,JLEV),ZQSC(JLON,JLEV))
      ELSE
        ZQS(JLON,JLEV)=ZQSS(JLON,JLEV)+ZQSC(JLON,JLEV)
      ENDIF

    ENDIF


!             CALCULS DEPENDANT DE L'OPTION NEIGE.
!             SNOW OPTION DEPENDENT CALCULATIONS.

    IF (LNEIGE) THEN
      ZPLS=FONICE(PT(JLON,JLEV))
      PQLI(JLON,JLEV)=ZQS(JLON,JLEV)*(1.0_JPRB-ZPLS)
      PQICE(JLON,JLEV)=ZQS(JLON,JLEV)*ZPLS
    ELSE
      PQLI(JLON,JLEV)=ZQS(JLON,JLEV)
      PQICE(JLON,JLEV)=0.0_JPRB
    ENDIF
  ENDDO
ENDDO

!            NEBULOSITE
!            CLOUDINESS

IF ( LNEBNXR ) THEN
  IF(.NOT.L3MT) THEN
    CALL ACNEBXRS(KIDIA,KFDIA,KLON,KTDIA,KLEV,PQ,ZQS,PQSAT,PNEB)
    DO JLEV=KTDIA,KLEV
      DO JLON=KIDIA,KFDIA
        PNEBC(JLON,JLEV)=ZQSC(JLON,JLEV) &
         & /MAX(ZEPSQC,ZQS(JLON,JLEV))*PNEB(JLON,JLEV)
      ENDDO
    ENDDO
  ENDIF
ELSEIF ( LCONDWT.AND.LPROCLD.AND.LRNUMX ) THEN
  DO JLEV=KTDIA,KLEV
    DO JLON=KIDIA,KFDIA
      ZNEBS=PNEBS(JLON,JLEV)
      PNEBC(JLON,JLEV)=MAX(ZEPSNEB,MIN(1.0_JPRB-ZEPSNEB,&
       & ZSNEBC(JLON,JLEV)*ZQSC(JLON,JLEV)))
      PNEB(JLON,JLEV)=MAX(ZNEBS,PNEBC(JLON,JLEV))
    ENDDO
  ENDDO
ELSEIF ( LCONDWT.AND.LPROCLD.AND.(.NOT.LRNUMX) ) THEN
  DO JLEV=KTDIA,KLEV
    DO JLON=KIDIA,KFDIA
      ZNEBS=PNEBS(JLON,JLEV)
      PNEBC(JLON,JLEV)=MAX(ZEPSNEB,MIN(1.0_JPRB-ZEPSNEB,&
       & ZSNEBC(JLON,JLEV)*ZQSC(JLON,JLEV)))
      PNEB(JLON,JLEV)=ZNEBS+(1.0_JPRB-ZNEBS)*PNEBC(JLON,JLEV)
    ENDDO
  ENDDO
ELSE
  DO JLEV=KTDIA,KLEV
    DO JLON=KIDIA,KFDIA
      ZNEBS=MAX(ZEPSNEB,MIN(1.0_JPRB-ZEPSNEB,&
       & QSNEBS*SQRT(ZQSS(JLON,JLEV)/PQSAT(JLON,JLEV))))
      PNEBC(JLON,JLEV)=MAX(ZEPSNEB,MIN(1.0_JPRB-ZEPSNEB,&
       & ZSNEBC(JLON,JLEV)*ZQSC(JLON,JLEV)))
      PNEB(JLON,JLEV)=ZNEBS+(1.0_JPRB-ZNEBS)*PNEBC(JLON,JLEV)
    ENDDO
  ENDDO
ENDIF

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