!-----------------------------------------------------------------------
SUBROUTINE ACPLUIZ( KIDIA, KFDIA, KLON, KTDIA, KLEV,&
 !-----------------------------------------------------------------------
 ! - INPUT -
 & PT, PQ, PQL, PQI, PQR, PQS,&
 & PDELP, PAPRSF, PCP, PR, PNEBS, PQLIS,&
 & PAPHI, PDIFTQ, PDIFTS,&
 & PTS, PNEIJ, PLSM, PGM, PVETAF, &
 ! - OUTPUT -
 & PFCSQL, PFCSQN, PFPLSL, PFPLSN,&
 & PFPEVPL, PFPEVPN, PFPFPL, PFPFPN )

!**** *ACPLUIZ * - PROGNOSTIC CLOUD SCMEME (LOPEZ)

!**   Interface.
!     ----------
!        *CALL* *ACPLUIZ*

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

! -   ARGUMENTS D'ENTREE./INPUT ARGUMENTS.
!     ------------------------------------

! - NOM DES PARAMETRES DE DIMENSIONNEMENT DE LA PHYSIQUE.

! KIDIA      : INDICE DE DEPART DES BOUCLES VECTORISEES SUR L'HORIZONT..
!            : START OF HORIZONTAL LOOP 
! KFDIA      : INDICE DE FIN DES BOUCLES VECTORISEES SUR L'HORIZONTALE.
!            : END OF HORIZONTAL LOOP
! KLON       : DIMENSION HORIZONTALE DES TABLEAUX.
!            : HORIZONTAL DIMENSION  
! KTDIA      : INDICE DE DEPART DES BOUCLES VERTICALES. 
!            : START OF THE VERTICAL LOOP IN THE PHYSICS. 
! KLEV       : DIMENSION VERTICALE DES TABLEAUX "FULL LEVEL".
!            : END OF VERTICAL LOOP AND VERTICAL DIMENSION

! - 2D (1:KLEV)

! PT         : TEMPERATURE.
!            : TEMPERATURE.
! PQ         : HUMIDITE SPECIFIQUE DE LA VAPEUR D'EAU.
!            : SPECIFIC HUMIDITY OF WATER VAPOUR.
! PQL        : HUMIDITE SPECIFIQUE DE L'EAU CONDENSEE LIQUIDE.
!            : SPECIFIC HUMIDITY OF LIQUID CONDENSATED WATER.
! PQI        : HUMIDITE SPECIFIQUE DE L'EAU CONDENSEE GLACE.
!            : SPECIFIC HUMIDITY OF SOLID CONDENSATED WATER.
! PQR        : PRECIPITATIONS LIQUIDES.
!            : LIQUID PECIPITATIONS.
! PQS        : PRECIPITATIONS SOLIDES.
!            : SOLID PECIPITATIONS.
! PDELP      : EPAISSEUR EN PRESSION DE LA COUCHE.
!            : LAYER THICKNESS IN PRESSURE UNITS.  
! PAPRSF     : PRESSION AUX NIVEAUX DES COUCHES.
!            : PRESSURE ON FULL LEVELS.
! PCP        : CHALEUR MASSIQUE A PRESSION CONSTANTE DE L'AIR.
!            : SPECIFIC HEAT AT CONSTANT PRESSURE FOR AIR.
! PR         : CONSTANTE DES GAZ POUR L'AIR.
!            : GAS CONSTANT FOR AIR.
! PNEBS      : NEBULOSITE PARTIELLE STRATIFORME.
!            : STRATIFORM FRACTIONAL CLOUDINESS.
! PQCS       : CONTENU "STRATIFORME" EN CONDENSAT NUAGEUX (LIQ. + SOL.).
!            : STRATIRORM CLOUD WATER (LIQUID + SOLID).

! - 2D (0:KLEV)

! PAPHI      : GEOPOTENTIEL SUR DEMI-NIVEAUX.
!            : GEOPOTENTIAL ON HALF-LEVELS.
! PDIFTQ     : FLUX TURBULENT (ET Q NEGATIF) D'HUMIDITE SPECIFIQUE.
!            : TURBULENT FLUX (INC. Q NEGATIVE) OF SPECIFIC HUMIDITY.
! PDIFTS     : FLUX TURBULENT D'ENTHALPIE (OU D'ENERGIE STATIQUE SECHE).
!            : TURBULENT FLUX OF ENTHALPY (OR DRY STATIC ENERGY).

! - 1D (1:KLON) .
                                                                                
! PTS        : TEMPERATURE DE SURFACE.
!            : SURFACE TEMPERATURE.
! PNEIJ      : PROPORTION DE LA MAILLE RECOUVERTE DE NEIGE.
!            : SNOW FRACTION.
! PLSM       : INDICE TERRE/MER.
!            : LAND/SEA MASK.
! PGM        : FACTEUR D'ECHELLE.
!            : MAPPING FACTOR.
! PVETAF     : COORDONNEE VERTICALE ETA.
!            : VERTICAL COORDINATE ETA.

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

! -   ARGUMENTS EN SORTIE.
!     ---------------------------

! - 2D (0:KLEV) .

! PFCSQN     : FLUX DE CONDENSATION LIE AUX RR STRATIFORMES NEIGEUSES.
!            : STRATIFORM CONDENSATION FLUX FOR ICE.
! PFCSQL     : FLUX DE CONDENSATION LIE AUX RR STRATIFORMES LIQUIDES.
!            : STRATIFORM CONDENSATION FLUX FOR LIQUID WATER.
! PFPLSL     : FLUX DE PRECIPITATION LIQUIDE (PLUIE).
!            : RAIN FLUX.
! PFPLSN     : FLUX DE PRECIPITATION SOLIDE  (NEIGE).
!            : ICE PRECIPITATION FLUX.
! PFPEVPL    : FLUX ASSOCIE A L'EVAPORATION DES PRECIP.
!            : FLUX ASSOCIATED TO EVAPORATION OF PRECIPITATIONS.
! PFPEVPN    : FLUX ASSOCIE A LA SUBLIMATION DES PRECIP.
!            : FLUX ASSOCIATED TO SUBLIMATION OF PRECIPITATIONS.
! PFPFPL     : FLUX DE GENERATION DE PRECIPITATIONS LIQUIDES.
!            : FLUX OF LIQUID PRECIPITATION GENERATION.
! PFPFPN     : FLUX DE GENERATION DE PRECIPITATIONS SOLIDES.
!            : FLUX OF SOLID PRECIPITATION GENERATION.

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

!     Auteur.
!     -------
!         04-10, Francois Bouyssel

!     Modifications.
!     --------------
!         05-05, F.Bouyssel : Introduction of vertical coordinate eta
!         05-07, F.Bouyssel : New arguments from aplpar to acmicro
!         05-11, F.Bouyssel : Remove ZAUTO,ZQCMIC,ZQPMIC
!         06-01, F.Bouyssel : Introduction of PQS, PFPEVPL, PFPEVPN
!         06-01, F.Bouyssel : Microphysical adjustment at the beginning
!         06-04, F.Bouyssel : Key for Smith's adjustment LADJCLD 
!         06-06, Y.Bouteloup: Statistical sedimentation (ADVPRCS)

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

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

USE YOMPHY    , ONLY : LADJCLD, LSSD, LECT
USE YOMPHY0   , ONLY : RDTFAC
USE YOMPHY2   , ONLY : TSPHY
USE YOMCST    , ONLY : RG   , RV   , RTT  , RDT  ,&
 & RCS  , RCW  , RCPV , RLVTT, RLSTT, RETV , RALPW, RALPS,&
 & RALPD, RBETW, RBETS, RBETD, RGAMW, RGAMS, RGAMD

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KIDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KFDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLON 
INTEGER(KIND=JPIM),INTENT(IN)    :: KTDIA 
INTEGER(KIND=JPIM),INTENT(IN)    :: KLEV 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PT     (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)    :: PQR    (KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQS    (KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDELP  (KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPRSF (KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PCP    (KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PR     (KLON,KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PNEBS  (KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PQLIS  (KLON,KLEV)
REAL(KIND=JPRB)   ,INTENT(IN)    :: PAPHI  (KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFTQ (KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PDIFTS (KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PTS    (KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PNEIJ  (KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PLSM   (KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PGM   (KLON) 
REAL(KIND=JPRB)   ,INTENT(IN)    :: PVETAF(KLEV) 

REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFCSQL (KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFCSQN (KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFPLSL (KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFPLSN (KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFPEVPL(KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFPEVPN(KLON,0:KLEV)
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFPFPL (KLON,0:KLEV) 
REAL(KIND=JPRB)   ,INTENT(INOUT) :: PFPFPN (KLON,0:KLEV) 

REAL(KIND=JPRB) :: ZQCS   (KLON,KLEV)
REAL(KIND=JPRB) :: ZNEBS  (KLON,KLEV)
REAL(KIND=JPRB) :: ZQCS0  (KLON,KLEV)
REAL(KIND=JPRB) :: ZNEBS0 (KLON,KLEV)
REAL(KIND=JPRB) :: ZAUTOL (KLON,KLEV)
REAL(KIND=JPRB) :: ZAUTOI (KLON,KLEV)
REAL(KIND=JPRB) :: ZGDTSDP(KLON,KLEV)
REAL(KIND=JPRB) :: ZQL    (KLON,KLEV)
REAL(KIND=JPRB) :: ZQI    (KLON,KLEV)
REAL(KIND=JPRB) :: ZQT    (KLON,KLEV)
REAL(KIND=JPRB) :: ZTL    (KLON,KLEV)
REAL(KIND=JPRB) :: ZERO   (KLON,KLEV)

REAL(KIND=JPRB) :: ZDPSGDT,ZICE,ZLV,ZLS
INTEGER(KIND=JPIM) :: JLON,JLEV
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "acnebsm.intfb.h"
#include "acmicro.intfb.h"
#include "advprc.intfb.h"
#include "advprcs.intfb.h"

#include "fcttrm.h"
#include "fctdoi.h"

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

!     CHECK RELIABILITY OF INPUT ARGUMENTS.

IF (LHOOK) CALL DR_HOOK('ACPLUIZ',0,ZHOOK_HANDLE)

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

! INITIALIZATION
! --------------
                                                                                
DO JLEV = KTDIA, KLEV
  DO JLON = KIDIA, KFDIA
    ZGDTSDP(JLON,JLEV) = ( RG * TSPHY ) / PDELP(JLON,JLEV)
  ENDDO
ENDDO
 
IF (LADJCLD.AND.(.NOT.LECT)) THEN

  ! AJUSTEMENT AFTER TURBULENCE.
  ! ----------------------------

  DO JLEV = KTDIA, KLEV
    DO JLON = KIDIA, KFDIA
      ZLV  = FOLH(PT(JLON,JLEV),0.0_JPRB)
      ZLS  = FOLH(PT(JLON,JLEV),1.0_JPRB)
      ZQT  (JLON,JLEV) = PQ(JLON,JLEV)+PQL(JLON,JLEV)+PQI(JLON,JLEV) &
       & + ZGDTSDP(JLON,JLEV)*(PDIFTQ(JLON,JLEV-1)-PDIFTQ(JLON,JLEV))
      ZTL  (JLON,JLEV) = PT(JLON,JLEV) &
       & + ( ZGDTSDP(JLON,JLEV)*(PDIFTS(JLON,JLEV-1)-PDIFTS(JLON,JLEV)) &
       & - ZLV*PQL(JLON,JLEV) - ZLS*PQI(JLON,JLEV) ) / PCP(JLON,JLEV)
      ZERO (JLON,JLEV) = 0.0_JPRB
    ENDDO
  ENDDO
                                                                                
  CALL ACNEBSM ( KIDIA,KFDIA,KLON,KTDIA,KLEV,&
               & ZTL,ZQT,ZERO,ZERO,&
               & PAPHI,PAPRSF,PCP,PR,&
               & PTS,PNEIJ,PLSM,PGM,PVETAF,&
               & ZQCS,ZNEBS,ZQCS0,ZNEBS0 )

ELSE

  DO JLEV = KTDIA, KLEV
    DO JLON = KIDIA, KFDIA
      ZQCS (JLON,JLEV) = PQLIS(JLON,JLEV)
      ZNEBS(JLON,JLEV) = PNEBS(JLON,JLEV)
    ENDDO
  ENDDO

ENDIF

DO JLEV = KTDIA, KLEV
  DO JLON = KIDIA, KFDIA
    ZDPSGDT  = 1.0_JPRB / ZGDTSDP(JLON,JLEV)
    ZICE     = FONICE(PT(JLON,JLEV))
    ZQL(JLON,JLEV) = ZQCS(JLON,JLEV)*(1.0_JPRB-ZICE)
    ZQI(JLON,JLEV) = ZQCS(JLON,JLEV)*ZICE
    PFCSQL(JLON,JLEV) = PFCSQL(JLON,JLEV-1) &
     & + ( ZQL(JLON,JLEV) - PQL(JLON,JLEV) ) * ZDPSGDT
    PFCSQN(JLON,JLEV) = PFCSQN(JLON,JLEV-1) &
     & + ( ZQI(JLON,JLEV) - PQI(JLON,JLEV) ) * ZDPSGDT
  ENDDO
ENDDO
 
! - - - - - - - -
! AUTOCONVERSION
! - - - - - - - -

CALL ACMICRO ( KIDIA, KFDIA, KLON, KTDIA, KLEV,&
             & ZNEBS, PT, ZQL, ZQI, PTS, PNEIJ, PLSM,&
             & ZAUTOL, ZAUTOI )

! - - - - - - - - - - - - - - - - - - -
! FALLING OF PRECIPITATING PARTICLES,
! COLLECTION AND EVAPORATION PROCESSES.
! - - - - - - - - - - - - - - - - - - -

IF (LSSD) THEN
   CALL ADVPRCS(KIDIA, KFDIA, KLON, KTDIA, KLEV,&
                &PT, PQ, ZQL, ZQI, ZAUTOL, ZAUTOI,&
                &PQR, PQS, ZNEBS,&
                &PCP, PR, PAPHI, PAPRSF, PDELP,&
                &PFPLSL , PFPLSN, PFPEVPL, PFPEVPN, PFPFPL, PFPFPN )
ELSE                
   CALL ADVPRC (KIDIA, KFDIA, KLON, KTDIA, KLEV,&
                &PT, PQ, ZQL, ZQI, ZAUTOL, ZAUTOI,&
                &PQR, PQS, ZNEBS,&
                &PCP, PR, PAPHI, PAPRSF, PDELP,&
                &PFPLSL , PFPLSN, PFPEVPL, PFPEVPN, PFPFPL, PFPFPN )
ENDIF                

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