!OPTIONS XOPT(NOEVAL)
SUBROUTINE ACNEBCOND ( KIDIA,KFDIA,KLON,KTDIA,KLEV,&
 !-----------------------------------------------------------------------
 ! - INPUT  1D VERTICAL.
 & PHUC,PVETAF,&
 !-----------------------------------------------------------------------
 ! - INPUT  2D .
 & PAPHI,PAPHIF,PAPRSF,PCP,PDELP,PQ,PQI,PQL,PQW,PT,PNCV,&
 ! - INPUT  1D .
 & PGM,PTS,&
 ! - OUTPUT 2D .
 & PNEBCOND,PHCRICS,PQSATS,PRMF,&
 ! - LOCK ARGUMENTS .
 & CDLOCK)  

!**** *ACNEBCOND * - CALCUL DE NEBULOSITE ET HUMIDITE CRITIQUE POUR LA
!                    CONDENSATION RESOLUE

!     Sujet.
!     ------
!     - ROUTINE DE CALCUL ACTIF .
!       CALCUL DE NEBULOSITE ET HUMIDITE CRITIQUE POUR LA CONDENSATION RESOLUE.
!     - COMPUTATION OF CLOUDINESS AND CRITICAL HUMIDITY FOR THE RESOLVED
!              CONDENSATION .

!**   Interface.
!     ----------
!        *CALL* *ACNEBCOND*

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

! - 1D (1:KLEV) .

! PHUC       : PROFIL DE BASE POUR L'HUMIDITE RELATIVE CRITIQUE.
! PVETAF     : LA COORDONNEE VERTICALE AUX COUCHES.

! - 2D (0:KLEV) .

! PAPHI      : HALF LEVEL GEOPOTENTIAL.

! - 2D (1:KLEV) .

! PAPHIF     : FULL LEVEL GEOPOTENTIAL.
! PAPRSF     : PRESSION AUX NIVEAUX PLEIN.
! PCP        : CHALEUR MASSIQUE A PRESSION CONSTANTE.
! PDELP      : EPAISSEUR EN PRESSION DE LA COUCHE.
! PQ         : HUMIDITE SPECIFIQUE DE LA VAPEUR D'EAU.
! PQW        : HUMIDITE SPECIFIQUE DU THERMOMETRE MOUILLE.
! PT         : TEMPERATURE.
! PNCV       : CONVECTIVE CLOUDINESS (HISTORIC). 

! ADDITIONAL PROGNOSTIC VARIABLES

! PQI        : RATIO OF SUSPENDED ICE.
! PQL        : RATIO OF SUSPENDED LIQUID WATER.

! - 1D .

! PGM        : FACTEUR D'ECHELLE.
! PTS        : TEMPERATURE DE SURFACE.

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

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

! - 2D (KLEV) .

! PNEBCOND   : NEBULOSITE POUR LA CONDENSATION RESOLUE.
! PHCRICS    : HUMIDITE CRITIQUE RELATIVE POUR LA CONDENSATION RESOLUE.
! PQSATS     : HUMIDITE SPECIFIQUE DE SATURATION.
! PRMF       : PROPORTION DE LA GLACE.

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

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

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

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

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

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

!     Auteur.
!     -------
!        07-02, R. Brozkova.

!     Modifications.
!     --------------

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

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

USE YOMGEM   , ONLY : TEQH     ,REFLRHC
USE YOMPHY   , ONLY : L3MT     ,LSMTPS    ,NSMTBOT  ,NSMDNEB ,&
 & LSMGCDEV ,LXRCDEV
USE YOMCST   , ONLY : RALPD    ,RDT       ,RGAMD    ,&
 & RV       ,RBETD    ,RCPV    ,RETV      ,RCW      ,&
 & RCS      ,RLVTT    ,RLSTT   ,RTT       ,RALPW    ,&
 & RBETW    ,RGAMW    ,RALPS   ,RBETS     ,RGAMS    
USE YOMPHY0  , ONLY : RDTFAC   ,QXRAL     ,SCLESPR  ,SCLESPS ,GRHCMOD,&
 & RHCRIT1  ,RHCRIT2  ,RETAMIN ,RDPHIC    ,NSMTPA   ,NSMTPB  ,RSMDNEBX,&
 & HUCRED

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)    :: PHUC(KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVETAF(KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHI(KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHIF(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPRSF(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDELP(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQ(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQI(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQL(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQW(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PNCV(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGM(KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTS(KLON) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PNEBCOND(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PHCRICS(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PQSATS(KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(OUT)   :: PRMF(KLON,KLEV) 
CHARACTER(LEN=9) ,INTENT(IN)    :: CDLOCK 

REAL(KIND=JPRB) :: ZQTOT(KLON,KLEV),ZRHL(KLON,KLEV),ZS(KLON,KLEV)

REAL(KIND=JPRB) :: ZDC(KLON),ZAW(KLON),ZX0(KLON),ZSITER(KLON),ZNEB1(KLON),&
 & ZA(KLON),ZB(KLON),ZC(KLON),ZRMF(KLON),ZS1(KLON),ZS2(KLON)

INTEGER(KIND=JPIM) :: ILIQ(KLON)

INTEGER(KIND=JPIM) :: JLEV, JLON, IITER, JITER, ILEV, JLEV2

LOGICAL :: LLRHXR,LLRHSM,LLNEBXR,LLNEBSM

REAL(KIND=JPRB) :: ZEPS1, ZEPS2, ZEPS3, ZESAT, ZESATL, ZESP,&
 & ZNI, ZNPI, ZDNII, ZFAC, ZXN, ZDXN, ZQXRAL, ZALFI, ZTKLEV, ZZ,&
 & ZLEN0, ZLESEFR, ZLESEFS, ZMESH, ZQCST, ZRHC, ZRATQ, ZNEB0, ZTEST,&
 & ZRAT2, ZDNEB, ZDCRI, ZFACT, ZFACT0, ZFACTA, ZFACTB, ZFACTC, ZDS,&
 & ZDELTAP, ZNCVNEB, ZNIM, ZZRHC

REAL(KIND=JPRB) :: ZHOOK_HANDLE

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

!     CHECK RELIABILITY OF INPUT ARGUMENTS.

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

!*
!     ------------------------------------------------------------------
!     I - CALCUL DE PARAMETRES DERIVES ET CONSTANTES DE SECURITE.

!         COMPUTATION OF DERIVED PARAMETERS AND SECURITY CONSTANTS.

ZEPS1=1.E-14_JPRB
ZEPS2=1.E-02_JPRB
ZEPS3=1.E-20_JPRB

ZQXRAL=QXRAL
ZLESEFR=1._JPRB/SCLESPR
ZLESEFS=1._JPRB/SCLESPS
IITER=3

!*
!     ------------------------------------------------------------------
!     II - CALCULS PRELIMINAIRES DE LA PROPORTION DE LA GLACE.

!          PRELIMINARY COMPUTATIONS OF ICE PROPORTION.


DO JLEV=KTDIA,KLEV
  DO JLON=KIDIA,KFDIA
    PRMF(JLON,JLEV)=FONICE(PT(JLON,JLEV))
  ENDDO
ENDDO

!*
!     ------------------------------------------------------------------
!     III - CALCULS DE CONDENSATION-EVAPORATION.

!           CONDENSATION-EVAPORATION COMPUTATIONS.

IF(LXRCDEV) THEN
!*
!     ------------------------------------------------------------------
!     IIIa - CALCUL DE L'HUMIDITE CRITIQUE (FORMULE ACPLUIE_PROG). 

!            CRITICAL HUMIDITY (ACPLUIE_PROG). 


  DO JLEV=KTDIA,KLEV

    DO JLON=KIDIA,KFDIA

!     CALCUL DE L'INFLUENCE DE LA TAILLE DE MAILLE.
!     COMPUTATION OF THE MESH-SIZE'S INFLUENCE.

      ZLEN0=1.0_JPRB/(PRMF(JLON,JLEV)*ZLESEFS+(1.0_JPRB-PRMF(JLON,JLEV))&
       &*ZLESEFR)
      ZMESH=REFLRHC/(TEQH*PGM(JLON))
      PHCRICS(JLON,JLEV)=((HUCRED*PHUC(JLEV)+1.0_JPRB-HUCRED)*ZMESH+ZLEN0)&
       &/(ZMESH+ZLEN0)

    ENDDO

  ENDDO ! JLEV

ENDIF ! LXRCDEV

IF(LSMGCDEV) THEN
!*
!     ------------------------------------------------------------------
!     IIIb - CALCUL DE L'HUMIDITE CRITIQUE (APRES LOPEZ).

!            CRITICAL HUMIDITY (AFTER LOPEZ).

! CRITICAL RELATIVE HUMIDITY PROFILE
! DEPENDENT ON THE LOCAL RESOLUTION.
! ----------------------------------

  ZFACT0=(RETAMIN*(RETAMIN-1.0_JPRB))**2
  ZFACTA=(2.0_JPRB*RETAMIN-1.0_JPRB)
  ZFACTB=(1.0_JPRB-3._JPRB*RETAMIN**2)
  ZFACTC=(3._JPRB*RETAMIN-2.0_JPRB)*RETAMIN
  ZDCRI=(RHCRIT2-RHCRIT1)/ZFACT0

  DO JLON=KIDIA,KFDIA
    ZFACT=ZDCRI*(1.0_JPRB-GRHCMOD * EXP(-1.0_JPRB/(TEQH*PGM(JLON))))
    ZA(JLON)=ZFACTA*ZFACT
    ZB(JLON)=ZFACTB*ZFACT
    ZC(JLON)=ZFACTC*ZFACT
  ENDDO

  DO JLEV=1,KLEV
    DO JLON=KIDIA,KFDIA
      PHCRICS(JLON,JLEV)=RHCRIT2+PVETAF(JLEV)*(ZC(JLON)+PVETAF(JLEV) &
                       & *(ZB(JLON)+PVETAF(JLEV)*ZA(JLON)))
    ENDDO
  ENDDO

ENDIF ! LSMGCDEV

IF(LXRCDEV) THEN
!*
!     ------------------------------------------------------------------
!     IVa - CALCUL DE LA NEBULOSITE (FORMULE XU-RANDAL MODIFIE).

!           CLOUDINESS DIAGNOSED BY MODIFIED XU-RANDAL SCHEME.

  ZALFI=1._JPRB/ZQXRAL

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

! - TEMPORAIRE(S) 1D .

! ZAW       : CONSTANTE CENTRALE DU CALCUL NEBULOSITE/CONDENSAT.
!           : CENTRAL CONSTANT OF THE CLOUD-COVER/CONDENSATE COMP..
! ZDC       : UN MOINS L'HUMIDITE RELATIVE CRITIQUE LOCALE.
!           : ONE MINUS THE LOCAL CRITICAL RELATIVE HUMIDITY.
! ZSITER    : VALEUR ITERATIVE DE LA VARIABLE DE CONTROLE (1/(1-N)).
!           : ITERATIVE VALUE OF THE CONTROL VARIABLE (1/(1-N)).
! ZX0       : VALEUR CIBLE POUR Q/QW-HUC = FONCTION DE (1/(1-N)).
!           : TARGET VALUE FOR Q/QW-HUC = FUNCTION OF (1/(1-N)).
!     PREPARATION DES PARAMETRES DE LA BOUCLE DE NEWTON ET PREMIER PAS
!     ANALYTIQUE.
!     PREPARATION OF THE NEWTON LOOP'S PARAMETERS AND ANALYTICAL FIRST
!     STEP.

      ZDC(JLON)=1._JPRB-PHCRICS(JLON,JLEV)
      ZAW(JLON)=ZALFI*SQRT(ZDC(JLON)/PQW(JLON,JLEV))
      ZX0(JLON)=MAX(0._JPRB,(PQ(JLON,JLEV)+PQL(JLON,JLEV)+PQI(JLON,JLEV))&
       &/PQW(JLON,JLEV)-PHCRICS(JLON,JLEV))
      ZDNII=1._JPRB/SQRT(SQRT(PHCRICS(JLON,JLEV)))
      ZXN=PNCV(JLON,JLEV)*(ZDC(JLON)+ZAW(JLON)*ZDNII)
      ZDXN=(1._JPRB-PNCV(JLON,JLEV))*ZDC(JLON)+ZAW(JLON)*ZDNII*(1._JPRB&
       &+PNCV(JLON,JLEV)*(ZDNII-0.25_JPRB*ZDC(JLON)/PHCRICS(JLON,JLEV)&
       &-1.5_JPRB))
      ZSITER(JLON)=MAX(1._JPRB,MAX(0._JPRB,(1._JPRB+0.5_JPRB*(ZX0(JLON)-ZXN)&
       &/MAX(ZEPS1,ZDXN)))**2)
    ENDDO

!     BOUCLE DE NEWTON; LA VARIABLE DE CONTROLE EST L'INVERSE DE UN
!     MOINS LA NEBULOSITE.
!     NEWTON LOOP; THE CONTROL VARIABLE IS THE INVERSE OF ONE MINUS THE
!     CLOUD COVER.

    DO JITER=1,IITER

      DO JLON=KIDIA,KFDIA
        ZNCVNEB=1._JPRB-PNCV(JLON,JLEV)
        ZNI=1._JPRB-(1._JPRB/ZSITER(JLON))
        ZNIM=1._JPRB-(ZNCVNEB/ZSITER(JLON))
        ZNPI=1._JPRB-ZDC(JLON)*(1._JPRB/ZSITER(JLON))
        ZDNII=1._JPRB/(SQRT(SQRT(ZNPI))-ZNI)
        ZFAC=ZAW(JLON)/(ZDC(JLON)*SQRT(ZSITER(JLON)))
        ZXN=ZDC(JLON)*ZNIM*(1._JPRB+(ZFAC*ZDNII))
        ZDXN=(ZDC(JLON)/ZSITER(JLON)**2)*(ZNCVNEB+(ZFAC*ZDNII)*(ZNCVNEB+ZNIM&
         &*(ZDNII*(1._JPRB-0.25_JPRB*ZDC(JLON)*SQRT(SQRT(ZNPI))/ZNPI)-0.5_JPRB&
         &*ZSITER(JLON))))
        ZSITER(JLON)=MAX(1._JPRB,MAX(0._JPRB,(ZSITER(JLON)+0.5_JPRB*(ZX0(JLON)&
         &-ZXN)/MAX(ZEPS1,ZDXN)))**2/ZSITER(JLON))
      ENDDO

    ENDDO

!     CALCULS FINAUX.
!     FINAL COMPUTATIONS.


    DO JLON=KIDIA,KFDIA
      PNEBCOND(JLON,JLEV)=1._JPRB-(1._JPRB/ZSITER(JLON))
      PQSATS(JLON,JLEV)=PQW(JLON,JLEV)
    ENDDO

  ENDDO ! JLEV

ENDIF ! LXRCDEV

IF(LSMGCDEV) THEN
!*
!     ------------------------------------------------------------------
!     IVa - CALCUL DE LA NEBULOSITE (FORMULE SMITH-GERARD).

!           CLOUDINESS DIAGNOSED BY SMITH-GERARD SCHEME.

! Initialisations for the Smith scheme.

! Compute the saturation with respect to ice and liquid.

  DO JLEV=KTDIA,KLEV-1
    DO JLON=KIDIA,KFDIA
      ZQTOT(JLON,JLEV)=PQI(JLON,JLEV)+PQL(JLON,JLEV)+PQ(JLON,JLEV)
      ZS(JLON,JLEV)=PCP(JLON,JLEV)*PT(JLON,JLEV)+PAPHIF(JLON,JLEV)

      ZESATL=FOEW(PT(JLON,JLEV),0.0_JPRB)
      ZESAT=ZESATL*(1.0_JPRB-PRMF(JLON,JLEV))+&
     &     FOEW(PT(JLON,JLEV),1.0_JPRB)*PRMF(JLON,JLEV)

      ZESP=ZESATL/PAPRSF(JLON,JLEV)
      ZRHL(JLON,JLEV)=ZQTOT(JLON,JLEV)/FOQS(ZESP)

      ZESP=ZESAT/PAPRSF(JLON,JLEV)
      PQSATS(JLON,JLEV)=FOQS(ZESP)
    ENDDO
  ENDDO

! Lowest level: use optionally the surface T in the estimation.

  DO JLON=KIDIA,KFDIA

    IF (NSMTBOT == 0) THEN
      ZTKLEV=(PTS(JLON)+PT(JLON,KLEV))*0.5_JPRB
    ELSE
      ZTKLEV=PT(JLON,KLEV)
    ENDIF

    ZRMF(JLON)=PRMF(JLON,KLEV)
    PRMF(JLON,KLEV)=FONICE(ZTKLEV)

    ZQTOT(JLON,KLEV)=PQI(JLON,KLEV)+PQL(JLON,KLEV)+PQ(JLON,KLEV)
    ZS(JLON,KLEV)=PCP(JLON,KLEV)*ZTKLEV+PAPHIF(JLON,KLEV)

    ZESATL=FOEW(ZTKLEV,0.0_JPRB)
    ZESAT=ZESATL*(1.0_JPRB-PRMF(JLON,KLEV))+&
   &      FOEW(ZTKLEV,1.0_JPRB)*PRMF(JLON,KLEV)

    ZESP=ZESATL/PAPRSF(JLON,KLEV)
    ZRHL(JLON,KLEV)=ZQTOT(JLON,KLEV)/FOQS(ZESP)

    ZESP=ZESAT/PAPRSF(JLON,KLEV)
    PQSATS(JLON,KLEV)=FOQS(ZESP)
  ENDDO

! Treble point smoothing: on PQSATS only.

  IF (LSMTPS) THEN

! Computation done only for liquid containing levels.

    DO JLEV=KTDIA,KLEV
      DO JLON=KIDIA,KFDIA
        ZS1(JLON)=(ZQTOT(JLON,JLEV)/PQSATS(JLON,JLEV))*PDELP(JLON,JLEV)
        ZS2(JLON)=PDELP(JLON,JLEV)
        ILIQ(JLON)=MAX(0,SIGN(1,1-NINT(1000._JPRB*PRMF(JLON,JLEV))))
      ENDDO

! Use NSMTPB layers below when dry adiabatic unstable.

      ILEV=MIN(JLEV+NSMTPB,KLEV)

      DO JLEV2=JLEV+1,ILEV
        DO JLON=KIDIA,KFDIA
          ZDS=ZS(JLON,JLEV2)-ZS(JLON,JLEV)
          ZDELTAP=MAX(0.0_JPRB,SIGN(1.0_JPRB,ZDS))*PDELP(JLON,JLEV2)
          ZS1(JLON)=ZS1(JLON)+(ZDELTAP*ILIQ(JLON))*ZRHL(JLON,JLEV2)
          ZS2(JLON)=ZS2(JLON)+(ZDELTAP*ILIQ(JLON))
        ENDDO
      ENDDO

! Use NSMTPA layers above when dry adiabatic unstable (reversal of ZDS sign).

      ILEV=MAX(JLEV-NSMTPA,KTDIA)

      DO JLEV2=ILEV,JLEV-1
        DO JLON=KIDIA,KFDIA
          ZDS=ZS(JLON,JLEV)-ZS(JLON,JLEV2)
          ZDELTAP=MAX(0.0_JPRB,SIGN(1.0_JPRB,ZDS))*PDELP(JLON,JLEV2)
          ZS1(JLON)=ZS1(JLON)+(ZDELTAP*ILIQ(JLON))*ZRHL(JLON,JLEV2)
          ZS2(JLON)=ZS2(JLON)+(ZDELTAP*ILIQ(JLON))
        ENDDO
      ENDDO

! Keep the original if ZS1 small but ALSO if ZQTOT is too small (desert)!

      DO JLON=KIDIA,KFDIA
        ZZ=MAX(0.0_JPRB,SIGN(1.0_JPRB,ZS1(JLON)-ZEPS1))*&
          &MAX(0.0_JPRB,SIGN(1.0_JPRB,ZQTOT(JLON,JLEV)-ZEPS1))
        PQSATS(JLON,JLEV)=ZZ*ZS2(JLON)/(ZS1(JLON)+(1.0_JPRB-ZZ))*&
          &ZQTOT(JLON,JLEV)+(1.0_JPRB-ZZ)*PQSATS(JLON,JLEV)
      ENDDO

    ENDDO

  ENDIF ! LSMTPS

! Reinstalling the true ice proportion at the bottom (for modular use).

  DO JLON=KIDIA,KFDIA
    PRMF(JLON,KLEV)=ZRMF(JLON)
  ENDDO

! Stratiform cloud cover, based on PQSATS.

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

      ZQCST=PQI(JLON,JLEV)+PQL(JLON,JLEV)
      ZRHC=PHCRICS(JLON,JLEV)
      ZRATQ=ZQTOT(JLON,JLEV)/PQSATS(JLON,JLEV)
      ZZRHC=MIN(ZRHC,1.0_JPRB-ZEPS1)
      ZRATQ=MAX(ZZRHC,MIN(2.0_JPRB-ZZRHC,ZRATQ))
      ZRAT2=(ZRATQ-1.0_JPRB)/(1.0_JPRB-ZZRHC)

      ZTEST=MAX(0.0_JPRB,SIGN(1.0_JPRB,ZRATQ-1.0_JPRB))
      PNEBCOND(JLON,JLEV)=ZTEST*(1.0_JPRB-0.5_JPRB*(1.0_JPRB-ZRAT2)**2)+&
        &(1.0_JPRB-ZTEST)*(0.5_JPRB*(1.0_JPRB+ZRAT2)**2)

! Where there is condensate impose a minimum of cloudiness.

      PNEBCOND(JLON,JLEV)=MAX(PNEBCOND(JLON,JLEV),ZEPS2*&
        &MAX(0.0_JPRB,SIGN(1.0_JPRB,ZQCST-ZEPS3)))

! Cloud cover reduction to avoid excessive values
! when overlapping is performed in radiation.

      PNEBCOND(JLON,JLEV) = PNEBCOND(JLON,JLEV)/(1.0_JPRB+&
        &(PAPHI(JLON,JLEV-1)-PAPHI(JLON,JLEV))/RDPHIC)

    ENDDO
  ENDDO

! Smoothing of the cloudiness where there are too big jumps
! diluting an excess condensate and preventing its autoconversion.

  IF (NSMDNEB == 1) THEN

! Smoothing the profile.

    DO JLON=KIDIA,KFDIA
      ZNEB1(JLON)=0._JPRB
    ENDDO

    DO JLEV=KTDIA,KLEV-1
      DO JLON=KIDIA,KFDIA
        ZNEB0=0.5_JPRB*(PNEBCOND(JLON,JLEV)+PNEBCOND(JLON,JLEV+1))
        PNEBCOND(JLON,JLEV)=(ZNEB1(JLON)+ZNEB0+PNEBCOND(JLON,JLEV)*&
                   & 2.0_JPRB)*0.25_JPRB
        ZNEB1(JLON)=ZNEB0
      ENDDO
    ENDDO

    DO JLON=KIDIA,KFDIA
      ZNEB0=PNEBCOND(JLON,KLEV)
      PNEBCOND(JLON,KLEV)=(ZNEB1(JLON)+ZNEB0+PNEBCOND(JLON,KLEV)*&
                 & 2.0_JPRB)*0.25_JPRB
    ENDDO

  ELSEIF (NSMDNEB == 2) THEN

! Limitation of the gradient.

    DO JLON=KIDIA,KFDIA
      ZNEB1(JLON)=0._JPRB
    ENDDO

    DO JLEV=KTDIA,KLEV
      DO JLON=KIDIA,KFDIA
        ZNEB0=PNEBCOND(JLON,JLEV)
        ZDNEB=ZNEB0-ZNEB1(JLON)
        ZNEB1(JLON)=ZNEB1(JLON)+SIGN(MIN(ABS(ZDNEB),RSMDNEBX),ZDNEB)
        PNEBCOND(JLON,JLEV)=ZNEB1(JLON)
      ENDDO
    ENDDO

  ENDIF ! NSMDNEB

ENDIF ! LSMGCDEV

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