Index: LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/bcscav_spl.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/bcscav_spl.f90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/bcscav_spl.f90	(revision 5142)
@@ -2,4 +2,6 @@
 
   USE dimphy
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE
   !=====================================================================
@@ -12,5 +14,4 @@
   INCLUDE "chem.h"
   INCLUDE "YOMCST.h"
-  INCLUDE "YOECUMF.h"
   !
   REAL :: pdtime, alpha_r, alpha_s, R_r, R_s
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/gastoparticle.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/gastoparticle.f90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/gastoparticle.f90	(revision 5142)
@@ -6,4 +6,5 @@
   USE dimphy
   USE infotrac
+  USE lmdz_YOECUMF
   ! USE indice_sol_mod
 
@@ -14,5 +15,4 @@
   INCLUDE "chem_spla.h"
   INCLUDE "YOMCST.h"
-  INCLUDE "YOECUMF.h"
   !
   REAL :: pdtphys
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/inscav_spl.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/inscav_spl.f90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/inscav_spl.f90	(revision 5142)
@@ -3,4 +3,6 @@
         his_dh)
   USE dimphy
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE
   !=====================================================================
@@ -13,5 +15,4 @@
   INCLUDE "chem.h"
   INCLUDE "YOMCST.h"
-  INCLUDE "YOECUMF.h"
   !
   INTEGER :: it
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/lsc_scav_orig.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/lsc_scav_orig.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/lsc_scav_orig.F90	(revision 5142)
@@ -12,4 +12,5 @@
   USE iophy
   USE lmdz_yomcst
+  USE lmdz_YOECUMF
 
   IMPLICIT NONE 
@@ -23,5 +24,4 @@
   INCLUDE "dimensions.h"
   INCLUDE "chem.h"
-  INCLUDE "YOECUMF.h"
 
   REAL,INTENT(IN)                        :: pdtime ! time step (s)
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/lsc_scav_spl.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/lsc_scav_spl.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/lsc_scav_spl.F90	(revision 5142)
@@ -14,4 +14,6 @@
   USE iophy
   USE lmdz_yomcst
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE 
 !=====================================================================
@@ -25,5 +27,4 @@
   INCLUDE "dimensions.h"
   INCLUDE "chem.h"
-  INCLUDE "YOECUMF.h"
 
   REAL,INTENT(IN)                        :: pdtime ! time step (s)
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/seasalt.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/seasalt.f90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/seasalt.f90	(revision 5142)
@@ -5,4 +5,6 @@
 
   USE dimphy
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE
   !
@@ -11,5 +13,4 @@
   INCLUDE "chem_spla.h"
   INCLUDE "YOMCST.h"
-  INCLUDE "YOECUMF.h"
   !
   INTEGER :: i, bin                 !local variables
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/sediment_mod.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/sediment_mod.f90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/Dust/sediment_mod.f90	(revision 5142)
@@ -10,4 +10,6 @@
   USE dimphy
   USE infotrac
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE
   !
@@ -15,5 +17,4 @@
   INCLUDE "chem.h"
   INCLUDE "YOMCST.h"
-  INCLUDE "YOECUMF.h"
   !
   REAL :: RHcl(klon, klev)     ! humidite relative ciel clair
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/YOECUMF.h
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/YOECUMF.h	(revision 5141)
+++ 	(revision )
@@ -1,49 +1,0 @@
-
-! $Id$
-
-!  ATTENTION!!!!: ce fichier include est compatible format fixe/format libre
-!                 veillez n'utiliser que des ! pour les commentaires
-!                 et bien positionner les & des lignes de continuation
-!                 (les placer en colonne 6 et en colonne 73)
-
-!     ----------------------------------------------------------------
-!*    *COMMON* *YOECUMF* - PARAMETERS FOR CUMULUS MASSFLUX SCHEME
-!     ----------------------------------------------------------------
-
-      COMMON /YOECUMF/                                                  &
-                       ENTRPEN,ENTRSCV,ENTRMID,ENTRDD,CMFCTOP,          &
-                       CMFCMAX,CMFCMIN,CMFDEPS,RHCDD,CPRCON,            &
-                       LMFPEN,LMFSCV,LMFMID,LMFDD,LMFDUDV
-
-
-      LOGICAL          LMFPEN,LMFSCV,LMFMID,LMFDD,LMFDUDV
-      REAL ENTRPEN, ENTRSCV, ENTRMID, ENTRDD
-      REAL CMFCTOP, CMFCMAX, CMFCMIN, CMFDEPS, RHCDD, CPRCON
-!$OMP THREADPRIVATE(/YOECUMF/)
-
-!*if (DOC,declared) <> 'UNKNOWN'
-!*    *COMMON* *YOECUMF* - PARAMETERS FOR CUMULUS MASSFLUX SCHEME
-
-!     M.TIEDTKE       E. C. M. W. F.      18/1/89
-
-!     NAME      TYPE      PURPOSE
-!     ----      ----      -------
-
-!     LMFPEN    LOGICAL  TRUE IF PENETRATIVE CONVECTION IS SWITCHED ON
-!     LMFSCV    LOGICAL  TRUE IF SHALLOW     CONVECTION IS SWITCHED ON
-!     LMFMID    LOGICAL  TRUE IF MIDLEVEL    CONVECTION IS SWITCHED ON
-!     LMFDD     LOGICAL  TRUE IF CUMULUS DOWNDRAFT      IS SWITCHED ON
-!     LMFDUDV   LOGICAL  TRUE IF CUMULUS FRICTION       IS SWITCHED ON
-!     ENTRPEN   REAL     ENTRAINMENT RATE FOR PENETRATIVE CONVECTION
-!     ENTRSCV   REAL     ENTRAINMENT RATE FOR SHALLOW CONVECTION
-!     ENTRMID   REAL     ENTRAINMENT RATE FOR MIDLEVEL CONVECTION
-!     ENTRDD    REAL     ENTRAINMENT RATE FOR CUMULUS DOWNDRAFTS
-!     CMFCTOP   REAL     RELAT. CLOUD MASSFLUX AT LEVEL ABOVE NONBUOYANC
-!     CMFCMAX   REAL     MAXIMUM MASSFLUX VALUE ALLOWED FOR
-!     CMFCMIN   REAL     MINIMUM MASSFLUX VALUE (FOR SAFETY)
-!     CMFDEPS   REAL     FRACTIONAL MASSFLUX FOR DOWNDRAFTS AT LFS
-!     RHCDD     REAL     RELATIVE SATURATION IN DOWNDRAFTS
-!     CPRCON    REAL     COEFFICIENTS FOR DETERMINING CONVERSION
-!                        FROM CLOUD WATER TO RAIN
-!*ifend
-!     ----------------------------------------------------------------
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/alpale_th.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/alpale_th.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/alpale_th.F90	(revision 5142)
@@ -22,4 +22,5 @@
   USE lmdz_abort_physic, ONLY: abort_physic
   USE lmdz_alpale
+  USE lmdz_cv, ONLY: cv_feed
 
   IMPLICIT NONE
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/conflx.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/conflx.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/conflx.F90	(revision 5142)
@@ -1,8 +1,7 @@
-
 ! $Header$
 
 SUBROUTINE conflx(dtime, pres_h, pres_f, t, q, con_t, con_q, pqhfl, w, d_t, &
-    d_q, rain, snow, pmfu, pmfd, pen_u, pde_u, pen_d, pde_d, kcbot, kctop, &
-    kdtop, pmflxr, pmflxs)
+        d_q, rain, snow, pmfu, pmfd, pen_u, pde_u, pen_d, pde_d, kcbot, kctop, &
+        kdtop, pmflxr, pmflxs)
 
   USE dimphy
@@ -20,5 +19,5 @@
   ! Entree:
   REAL dtime ! pas d'integration (s)
-  REAL pres_h(klon, klev+1) ! pression half-level (Pa)
+  REAL pres_h(klon, klev + 1) ! pression half-level (Pa)
   REAL pres_f(klon, klev) ! pression full-level (Pa)
   REAL t(klon, klev) ! temperature (K)
@@ -39,6 +38,6 @@
   REAL rain(klon) ! pluie (mm/s)
   REAL snow(klon) ! neige (mm/s)
-  REAL pmflxr(klon, klev+1)
-  REAL pmflxs(klon, klev+1)
+  REAL pmflxr(klon, klev + 1)
+  REAL pmflxs(klon, klev + 1)
   INTEGER kcbot(klon) ! niveau du bas de la convection
   INTEGER kctop(klon) ! niveau du haut de la convection
@@ -53,5 +52,5 @@
   REAL d_t_bis(klon, klev)
   REAL d_q_bis(klon, klev)
-  REAL paprs(klon, klev+1)
+  REAL paprs(klon, klev + 1)
   REAL paprsf(klon, klev)
   REAL zgeom(klon, klev)
@@ -65,8 +64,7 @@
   REAL zde_u(klon, klev)
   REAL zde_d(klon, klev)
-  REAL zmflxr(klon, klev+1)
-  REAL zmflxs(klon, klev+1)
+  REAL zmflxr(klon, klev + 1)
+  REAL zmflxs(klon, klev + 1)
   ! AA
-
 
   INTEGER i, k
@@ -117,28 +115,28 @@
   DO k = 1, klev
     DO i = 1, klon
-      pt(i, k) = t(i, klev-k+1)
-      pq(i, k) = q(i, klev-k+1)
-      paprsf(i, k) = pres_f(i, klev-k+1)
-      paprs(i, k) = pres_h(i, klev+1-k+1)
-      pvervel(i, k) = w(i, klev+1-k)
-      zcvgt(i, k) = con_t(i, klev-k+1)
-      zcvgq(i, k) = con_q(i, klev-k+1)
-
-      zdelta = max(0., sign(1.,rtt-pt(i,k)))
-      zqsat = r2es*foeew(pt(i,k), zdelta)/paprsf(i, k)
+      pt(i, k) = t(i, klev - k + 1)
+      pq(i, k) = q(i, klev - k + 1)
+      paprsf(i, k) = pres_f(i, klev - k + 1)
+      paprs(i, k) = pres_h(i, klev + 1 - k + 1)
+      pvervel(i, k) = w(i, klev + 1 - k)
+      zcvgt(i, k) = con_t(i, klev - k + 1)
+      zcvgq(i, k) = con_q(i, klev - k + 1)
+
+      zdelta = max(0., sign(1., rtt - pt(i, k)))
+      zqsat = r2es * foeew(pt(i, k), zdelta) / paprsf(i, k)
       zqsat = min(0.5, zqsat)
-      zqsat = zqsat/(1.-retv*zqsat)
+      zqsat = zqsat / (1. - retv * zqsat)
       pqs(i, k) = zqsat
     END DO
   END DO
   DO i = 1, klon
-    paprs(i, klev+1) = pres_h(i, 1)
-    zgeom(i, klev) = rd*pt(i, klev)/(0.5*(paprs(i,klev+1)+paprsf(i, &
-      klev)))*(paprs(i,klev+1)-paprsf(i,klev))
+    paprs(i, klev + 1) = pres_h(i, 1)
+    zgeom(i, klev) = rd * pt(i, klev) / (0.5 * (paprs(i, klev + 1) + paprsf(i, &
+            klev))) * (paprs(i, klev + 1) - paprsf(i, klev))
   END DO
   DO k = klev - 1, 1, -1
     DO i = 1, klon
-      zgeom(i, k) = zgeom(i, k+1) + rd*0.5*(pt(i,k+1)+pt(i,k))/paprs(i, k+1)* &
-        (paprsf(i,k+1)-paprsf(i,k))
+      zgeom(i, k) = zgeom(i, k + 1) + rd * 0.5 * (pt(i, k + 1) + pt(i, k)) / paprs(i, k + 1) * &
+              (paprsf(i, k + 1) - paprsf(i, k))
     END DO
   END DO
@@ -147,6 +145,6 @@
 
   CALL flxmain(dtime, pt, pq, pqs, pqhfl, paprsf, paprs, zgeom, land, zcvgt, &
-    zcvgq, pvervel, rain, snow, kcbot, kctop, kdtop, zmfu, zmfd, zen_u, &
-    zde_u, zen_d, zde_d, d_t_bis, d_q_bis, zmflxr, zmflxs)
+          zcvgq, pvervel, rain, snow, kcbot, kctop, kdtop, zmfu, zmfd, zen_u, &
+          zde_u, zen_d, zde_d, d_t_bis, d_q_bis, zmflxr, zmflxs)
 
   ! AA--------------------------------------------------------
@@ -158,6 +156,6 @@
   DO k = 1, klev
     DO i = 1, klon
-      d_q(i, klev+1-k) = dtime*d_q_bis(i, k)
-      d_t(i, klev+1-k) = dtime*d_t_bis(i, k)
+      d_q(i, klev + 1 - k) = dtime * d_q_bis(i, k)
+      d_t(i, klev + 1 - k) = dtime * d_t_bis(i, k)
     END DO
   END DO
@@ -172,6 +170,6 @@
   DO k = 2, klev
     DO i = 1, klon
-      pmfu(i, klev+2-k) = zmfu(i, k)
-      pmfd(i, klev+2-k) = zmfd(i, k)
+      pmfu(i, klev + 2 - k) = zmfu(i, k)
+      pmfd(i, klev + 2 - k) = zmfd(i, k)
     END DO
   END DO
@@ -179,6 +177,6 @@
   DO k = 1, klev
     DO i = 1, klon
-      pen_u(i, klev+1-k) = zen_u(i, k)
-      pde_u(i, klev+1-k) = zde_u(i, k)
+      pen_u(i, klev + 1 - k) = zen_u(i, k)
+      pde_u(i, klev + 1 - k) = zde_u(i, k)
     END DO
   END DO
@@ -186,6 +184,6 @@
   DO k = 1, klev - 1
     DO i = 1, klon
-      pen_d(i, klev+1-k) = -zen_d(i, k+1)
-      pde_d(i, klev+1-k) = -zde_d(i, k+1)
+      pen_d(i, klev + 1 - k) = -zen_d(i, k + 1)
+      pde_d(i, klev + 1 - k) = -zde_d(i, k + 1)
     END DO
   END DO
@@ -193,22 +191,22 @@
   DO k = 1, klev + 1
     DO i = 1, klon
-      pmflxr(i, klev+2-k) = zmflxr(i, k)
-      pmflxs(i, klev+2-k) = zmflxs(i, k)
-    END DO
-  END DO
-
+      pmflxr(i, klev + 2 - k) = zmflxr(i, k)
+      pmflxs(i, klev + 2 - k) = zmflxs(i, k)
+    END DO
+  END DO
 
 END SUBROUTINE conflx
 ! --------------------------------------------------------------------
 SUBROUTINE flxmain(pdtime, pten, pqen, pqsen, pqhfl, pap, paph, pgeo, ldland, &
-    ptte, pqte, pvervel, prsfc, pssfc, kcbot, kctop, kdtop, & ! *
-                                                              ! ldcum, ktype,
-    pmfu, pmfd, pen_u, pde_u, pen_d, pde_d, dt_con, dq_con, pmflxr, pmflxs)
+        ptte, pqte, pvervel, prsfc, pssfc, kcbot, kctop, kdtop, & ! *
+        ! ldcum, ktype,
+        pmfu, pmfd, pen_u, pde_u, pen_d, pde_d, dt_con, dq_con, pmflxr, pmflxs)
   USE dimphy
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE
   ! ------------------------------------------------------------------
   include "YOMCST.h"
   include "YOETHF.h"
-  include "YOECUMF.h"
   ! ----------------------------------------------------------------
   REAL pten(klon, klev), pqen(klon, klev), pqsen(klon, klev)
@@ -216,5 +214,5 @@
   REAL pqte(klon, klev)
   REAL pvervel(klon, klev)
-  REAL pgeo(klon, klev), pap(klon, klev), paph(klon, klev+1)
+  REAL pgeo(klon, klev), pap(klon, klev), paph(klon, klev + 1)
   REAL pqhfl(klon)
 
@@ -234,6 +232,6 @@
   REAL zdqpbl(klon), zdqcv(klon), zdhpbl(klon)
   REAL zrfl(klon)
-  REAL pmflxr(klon, klev+1)
-  REAL pmflxs(klon, klev+1)
+  REAL pmflxr(klon, klev + 1)
+  REAL pmflxs(klon, klev + 1)
   INTEGER ilab(klon, klev), ictop0(klon)
   LOGICAL llo1
@@ -275,6 +273,6 @@
   ! ----------------------------------------------------------------------
   CALL flxini(pten, pqen, pqsen, pgeo, paph, zgeoh, ztenh, zqenh, zqsenh, &
-    ptu, pqu, ptd, pqd, pmfd, zmfds, zmfdq, zdmfdp, pmfu, zmfus, zmfuq, &
-    zdmfup, zdpmel, plu, plude, ilab, pen_u, pde_u, pen_d, pde_d)
+          ptu, pqu, ptd, pqd, pmfd, zmfds, zmfdq, zdmfdp, pmfu, zmfus, zmfuq, &
+          zdmfup, zdpmel, plu, plude, ilab, pen_u, pde_u, pen_d, pde_d)
   ! ---------------------------------------------------------------------
   ! determiner les valeurs au niveau de base de la tour convective
@@ -290,5 +288,5 @@
   k = 1
   DO i = 1, klon
-    zdqcv(i) = pqte(i, k)*(paph(i,k+1)-paph(i,k))
+    zdqcv(i) = pqte(i, k) * (paph(i, k + 1) - paph(i, k))
     zdhpbl(i) = 0.0
     zdqpbl(i) = 0.0
@@ -297,9 +295,9 @@
   DO k = 2, klev
     DO i = 1, klon
-      zdqcv(i) = zdqcv(i) + pqte(i, k)*(paph(i,k+1)-paph(i,k))
+      zdqcv(i) = zdqcv(i) + pqte(i, k) * (paph(i, k + 1) - paph(i, k))
       IF (k>=kcbot(i)) THEN
-        zdqpbl(i) = zdqpbl(i) + pqte(i, k)*(paph(i,k+1)-paph(i,k))
-        zdhpbl(i) = zdhpbl(i) + (rcpd*ptte(i,k)+rlvtt*pqte(i,k))*(paph(i,k+1) &
-          -paph(i,k))
+        zdqpbl(i) = zdqpbl(i) + pqte(i, k) * (paph(i, k + 1) - paph(i, k))
+        zdhpbl(i) = zdhpbl(i) + (rcpd * ptte(i, k) + rlvtt * pqte(i, k)) * (paph(i, k + 1) &
+                - paph(i, k))
       END IF
     END DO
@@ -308,5 +306,5 @@
   DO i = 1, klon
     ktype(i) = 2
-    IF (zdqcv(i)>max(0.,-1.5*pqhfl(i)*rg)) ktype(i) = 1
+    IF (zdqcv(i)>max(0., -1.5 * pqhfl(i) * rg)) ktype(i) = 1
     ! cc         if (zdqcv(i).GT.MAX(0.,-1.1*pqhfl(i)*RG)) ktype(i) = 1
   END DO
@@ -319,7 +317,7 @@
     ikb = kcbot(i)
     zqumqe = pqu(i, ikb) + plu(i, ikb) - zqenh(i, ikb)
-    zdqmin = max(0.01*zqenh(i,ikb), 1.E-10)
+    zdqmin = max(0.01 * zqenh(i, ikb), 1.E-10)
     IF (zdqpbl(i)>0. .AND. zqumqe>zdqmin .AND. ldcum(i)) THEN
-      zmfub(i) = zdqpbl(i)/(rg*max(zqumqe,zdqmin))
+      zmfub(i) = zdqpbl(i) / (rg * max(zqumqe, zdqmin))
     ELSE
       zmfub(i) = 0.01
@@ -327,9 +325,9 @@
     END IF
     IF (ktype(i)==2) THEN
-      zdh = rcpd*(ptu(i,ikb)-ztenh(i,ikb)) + rlvtt*zqumqe
-      zdh = rg*max(zdh, 1.0E5*zdqmin)
-      IF (zdhpbl(i)>0. .AND. ldcum(i)) zmfub(i) = zdhpbl(i)/zdh
+      zdh = rcpd * (ptu(i, ikb) - ztenh(i, ikb)) + rlvtt * zqumqe
+      zdh = rg * max(zdh, 1.0E5 * zdqmin)
+      IF (zdhpbl(i)>0. .AND. ldcum(i)) zmfub(i) = zdhpbl(i) / zdh
     END IF
-    zmfmax = (paph(i,ikb)-paph(i,ikb-1))/(rg*pdtime)
+    zmfmax = (paph(i, ikb) - paph(i, ikb - 1)) / (rg * pdtime)
     zmfub(i) = min(zmfub(i), zmfmax)
     zentr(i) = entrscv
@@ -345,17 +343,17 @@
   DO i = 1, klon
     ikb = kcbot(i)
-    zhcbase(i) = rcpd*ptu(i, ikb) + zgeoh(i, ikb) + rlvtt*pqu(i, ikb)
+    zhcbase(i) = rcpd * ptu(i, ikb) + zgeoh(i, ikb) + rlvtt * pqu(i, ikb)
     ictop0(i) = kcbot(i) - 1
   END DO
 
-  zalvdcp = rlvtt/rcpd
+  zalvdcp = rlvtt / rcpd
   DO k = klev - 1, 3, -1
     DO i = 1, klon
-      zhsat = rcpd*ztenh(i, k) + zgeoh(i, k) + rlvtt*zqsenh(i, k)
-      zgam = r5les*zalvdcp*zqsenh(i, k)/((1.-retv*zqsenh(i,k))*(ztenh(i, &
-        k)-r4les)**2)
-      zzz = rcpd*ztenh(i, k)*0.608
-      zhhat = zhsat - (zzz+zgam*zzz)/(1.+zgam*zzz/rlvtt)*max(zqsenh(i,k)- &
-        zqenh(i,k), 0.)
+      zhsat = rcpd * ztenh(i, k) + zgeoh(i, k) + rlvtt * zqsenh(i, k)
+      zgam = r5les * zalvdcp * zqsenh(i, k) / ((1. - retv * zqsenh(i, k)) * (ztenh(i, &
+              k) - r4les)**2)
+      zzz = rcpd * ztenh(i, k) * 0.608
+      zhhat = zhsat - (zzz + zgam * zzz) / (1. + zgam * zzz / rlvtt) * max(zqsenh(i, k) - &
+              zqenh(i, k), 0.)
       IF (k<ictop0(i) .AND. zhcbase(i)>zhhat) ictop0(i) = k
     END DO
@@ -365,7 +363,7 @@
 
   CALL flxasc(pdtime, ztenh, zqenh, pten, pqen, pqsen, pgeo, zgeoh, pap, &
-    paph, pqte, pvervel, ldland, ldcum, ktype, ilab, ptu, pqu, plu, pmfu, &
-    zmfub, zentr, zmfus, zmfuq, zmful, plude, zdmfup, kcbot, kctop, ictop0, &
-    kcum, pen_u, pde_u)
+          paph, pqte, pvervel, ldland, ldcum, ktype, ilab, ptu, pqu, plu, pmfu, &
+          zmfub, zentr, zmfus, zmfuq, zmful, plude, zdmfup, kcbot, kctop, ictop0, &
+          kcum, pen_u, pde_u)
   IF (kcum==0) GO TO 1000
 
@@ -395,9 +393,9 @@
     ! determiner le LFS (level of free sinking: niveau de plonge libre)
     CALL flxdlfs(ztenh, zqenh, zgeoh, paph, ptu, pqu, ldcum, kcbot, kctop, &
-      zmfub, zrfl, ptd, pqd, pmfd, zmfds, zmfdq, zdmfdp, kdtop, lddraf)
+            zmfub, zrfl, ptd, pqd, pmfd, zmfds, zmfdq, zdmfdp, kdtop, lddraf)
 
     ! calculer le panache descendant
     CALL flxddraf(ztenh, zqenh, zgeoh, paph, zrfl, ptd, pqd, pmfd, zmfds, &
-      zmfdq, zdmfdp, lddraf, pen_d, pde_d)
+            zmfdq, zdmfdp, lddraf, pen_d, pde_d)
 
     ! calculer de nouveau le flux de masse entrant a travers la base
@@ -410,22 +408,22 @@
         zeps = 0.
         IF (llo1) zeps = cmfdeps
-        zqumqe = pqu(i, ikb) + plu(i, ikb) - zeps*pqd(i, ikb) - &
-          (1.-zeps)*zqenh(i, ikb)
-        zdqmin = max(0.01*zqenh(i,ikb), 1.E-10)
-        zmfmax = (paph(i,ikb)-paph(i,ikb-1))/(rg*pdtime)
+        zqumqe = pqu(i, ikb) + plu(i, ikb) - zeps * pqd(i, ikb) - &
+                (1. - zeps) * zqenh(i, ikb)
+        zdqmin = max(0.01 * zqenh(i, ikb), 1.E-10)
+        zmfmax = (paph(i, ikb) - paph(i, ikb - 1)) / (rg * pdtime)
         IF (zdqpbl(i)>0. .AND. zqumqe>zdqmin .AND. ldcum(i) .AND. &
-            zmfub(i)<zmfmax) THEN
-          zmfub1(i) = zdqpbl(i)/(rg*max(zqumqe,zdqmin))
+                zmfub(i)<zmfmax) THEN
+          zmfub1(i) = zdqpbl(i) / (rg * max(zqumqe, zdqmin))
         ELSE
           zmfub1(i) = zmfub(i)
         END IF
         IF (ktype(i)==2) THEN
-          zdh = rcpd*(ptu(i,ikb)-zeps*ptd(i,ikb)-(1.-zeps)*ztenh(i,ikb)) + &
-            rlvtt*zqumqe
-          zdh = rg*max(zdh, 1.0E5*zdqmin)
-          IF (zdhpbl(i)>0. .AND. ldcum(i)) zmfub1(i) = zdhpbl(i)/zdh
+          zdh = rcpd * (ptu(i, ikb) - zeps * ptd(i, ikb) - (1. - zeps) * ztenh(i, ikb)) + &
+                  rlvtt * zqumqe
+          zdh = rg * max(zdh, 1.0E5 * zdqmin)
+          IF (zdhpbl(i)>0. .AND. ldcum(i)) zmfub1(i) = zdhpbl(i) / zdh
         END IF
-        IF (.NOT. ((ktype(i)==1 .OR. ktype(i)==2) .AND. abs(zmfub1(i)-zmfub(i &
-          ))<0.2*zmfub(i))) zmfub1(i) = zmfub(i)
+        IF (.NOT. ((ktype(i)==1 .OR. ktype(i)==2) .AND. abs(zmfub1(i) - zmfub(i &
+                ))<0.2 * zmfub(i))) zmfub1(i) = zmfub(i)
       END IF
     END DO
@@ -433,11 +431,11 @@
       DO i = 1, klon
         IF (lddraf(i)) THEN
-          zfac = zmfub1(i)/max(zmfub(i), 1.E-10)
-          pmfd(i, k) = pmfd(i, k)*zfac
-          zmfds(i, k) = zmfds(i, k)*zfac
-          zmfdq(i, k) = zmfdq(i, k)*zfac
-          zdmfdp(i, k) = zdmfdp(i, k)*zfac
-          pen_d(i, k) = pen_d(i, k)*zfac
-          pde_d(i, k) = pde_d(i, k)*zfac
+          zfac = zmfub1(i) / max(zmfub(i), 1.E-10)
+          pmfd(i, k) = pmfd(i, k) * zfac
+          zmfds(i, k) = zmfds(i, k) * zfac
+          zmfdq(i, k) = zmfdq(i, k) * zfac
+          zdmfdp(i, k) = zdmfdp(i, k) * zfac
+          pen_d(i, k) = pen_d(i, k) * zfac
+          pde_d(i, k) = pde_d(i, k) * zfac
         END IF
       END DO
@@ -453,7 +451,7 @@
   ! -----------------------------------------------------------------------
   CALL flxasc(pdtime, ztenh, zqenh, pten, pqen, pqsen, pgeo, zgeoh, pap, &
-    paph, pqte, pvervel, ldland, ldcum, ktype, ilab, ptu, pqu, plu, pmfu, &
-    zmfub, zentr, zmfus, zmfuq, zmful, plude, zdmfup, kcbot, kctop, ictop0, &
-    kcum, pen_u, pde_u)
+          paph, pqte, pvervel, ldland, ldcum, ktype, ilab, ptu, pqu, plu, pmfu, &
+          zmfub, zentr, zmfus, zmfuq, zmful, plude, zdmfup, kcbot, kctop, ictop0, &
+          kcum, pen_u, pde_u)
 
   ! -----------------------------------------------------------------------
@@ -462,7 +460,7 @@
   ! -----------------------------------------------------------------------
   CALL flxflux(pdtime, pqen, pqsen, ztenh, zqenh, pap, paph, ldland, zgeoh, &
-    kcbot, kctop, lddraf, kdtop, ktype, ldcum, pmfu, pmfd, zmfus, zmfds, &
-    zmfuq, zmfdq, zmful, plude, zdmfup, zdmfdp, pten, prsfc, pssfc, zdpmel, &
-    itopm2, pmflxr, pmflxs)
+          kcbot, kctop, lddraf, kdtop, ktype, ldcum, pmfu, pmfd, zmfus, zmfds, &
+          zmfuq, zmfdq, zmful, plude, zdmfup, zdmfdp, pten, prsfc, pssfc, zdpmel, &
+          itopm2, pmflxr, pmflxs)
 
   ! ----------------------------------------------------------------------
@@ -470,12 +468,12 @@
   ! ----------------------------------------------------------------------
   CALL flxdtdq(pdtime, itopm2, paph, ldcum, pten, zmfus, zmfds, zmfuq, zmfdq, &
-    zmful, zdmfup, zdmfdp, zdpmel, dt_con, dq_con)
-
-1000 CONTINUE
+          zmful, zdmfup, zdmfdp, zdpmel, dt_con, dq_con)
+
+  1000 CONTINUE
 
 END SUBROUTINE flxmain
 SUBROUTINE flxini(pten, pqen, pqsen, pgeo, paph, pgeoh, ptenh, pqenh, pqsenh, &
-    ptu, pqu, ptd, pqd, pmfd, pmfds, pmfdq, pdmfdp, pmfu, pmfus, pmfuq, &
-    pdmfup, pdpmel, plu, plude, klab, pen_u, pde_u, pen_d, pde_d)
+        ptu, pqu, ptd, pqd, pmfd, pmfds, pmfdq, pdmfdp, pmfu, pmfus, pmfuq, &
+        pdmfup, pdpmel, plu, plude, klab, pen_u, pde_u, pen_d, pde_d)
   USE dimphy
   IMPLICIT NONE
@@ -493,5 +491,5 @@
   REAL pgeo(klon, klev) ! geopotentiel (g * metre)
   REAL pgeoh(klon, klev) ! geopotentiel aux demi-niveaux
-  REAL paph(klon, klev+1) ! pression aux demi-niveaux
+  REAL paph(klon, klev + 1) ! pression aux demi-niveaux
   REAL ptenh(klon, klev) ! temperature aux demi-niveaux
   REAL pqenh(klon, klev) ! humidite aux demi-niveaux
@@ -532,24 +530,24 @@
 
     DO i = 1, klon
-      pgeoh(i, k) = pgeo(i, k) + (pgeo(i,k-1)-pgeo(i,k))*0.5
-      ptenh(i, k) = (max(rcpd*pten(i,k-1)+pgeo(i,k-1),rcpd*pten(i,k)+pgeo(i, &
-        k))-pgeoh(i,k))/rcpd
-      pqsenh(i, k) = pqsen(i, k-1)
+      pgeoh(i, k) = pgeo(i, k) + (pgeo(i, k - 1) - pgeo(i, k)) * 0.5
+      ptenh(i, k) = (max(rcpd * pten(i, k - 1) + pgeo(i, k - 1), rcpd * pten(i, k) + pgeo(i, &
+              k)) - pgeoh(i, k)) / rcpd
+      pqsenh(i, k) = pqsen(i, k - 1)
       llflag(i) = .TRUE.
     END DO
 
     iCALL = 0
-    CALL flxadjtq(paph(1,k), ptenh(1,k), pqsenh(1,k), llflag, icall)
-
-    DO i = 1, klon
-      pqenh(i, k) = min(pqen(i,k-1), pqsen(i,k-1)) + &
-        (pqsenh(i,k)-pqsen(i,k-1))
-      pqenh(i, k) = max(pqenh(i,k), 0.)
-    END DO
-
-  END DO
-
-  DO i = 1, klon
-    ptenh(i, klev) = (rcpd*pten(i,klev)+pgeo(i,klev)-pgeoh(i,klev))/rcpd
+    CALL flxadjtq(paph(1, k), ptenh(1, k), pqsenh(1, k), llflag, icall)
+
+    DO i = 1, klon
+      pqenh(i, k) = min(pqen(i, k - 1), pqsen(i, k - 1)) + &
+              (pqsenh(i, k) - pqsen(i, k - 1))
+      pqenh(i, k) = max(pqenh(i, k), 0.)
+    END DO
+
+  END DO
+
+  DO i = 1, klon
+    ptenh(i, klev) = (rcpd * pten(i, klev) + pgeo(i, klev) - pgeoh(i, klev)) / rcpd
     pqenh(i, klev) = pqen(i, klev)
     ptenh(i, 1) = pten(i, 1)
@@ -560,6 +558,6 @@
   DO k = klev - 1, 2, -1
     DO i = 1, klon
-      zzs = max(rcpd*ptenh(i,k)+pgeoh(i,k), rcpd*ptenh(i,k+1)+pgeoh(i,k+1))
-      ptenh(i, k) = (zzs-pgeoh(i,k))/rcpd
+      zzs = max(rcpd * ptenh(i, k) + pgeoh(i, k), rcpd * ptenh(i, k + 1) + pgeoh(i, k + 1))
+      ptenh(i, k) = (zzs - pgeoh(i, k)) / rcpd
     END DO
   END DO
@@ -596,8 +594,7 @@
   END DO
 
-
 END SUBROUTINE flxini
 SUBROUTINE flxbase(ptenh, pqenh, pgeoh, paph, ptu, pqu, plu, ldcum, kcbot, &
-    klab)
+        klab)
   USE dimphy
   IMPLICIT NONE
@@ -617,5 +614,5 @@
   ! ----------------------------------------------------------------
   REAL ptenh(klon, klev), pqenh(klon, klev)
-  REAL pgeoh(klon, klev), paph(klon, klev+1)
+  REAL pgeoh(klon, klev), paph(klon, klev + 1)
 
   REAL ptu(klon, klev), pqu(klon, klev), plu(klon, klev)
@@ -643,7 +640,7 @@
     is = 0
     DO i = 1, klon
-      IF (klab(i,k+1)==1) is = is + 1
+      IF (klab(i, k + 1)==1) is = is + 1
       llflag(i) = .FALSE.
-      IF (klab(i,k+1)==1) llflag(i) = .TRUE.
+      IF (klab(i, k + 1)==1) llflag(i) = .TRUE.
     END DO
     IF (is==0) GO TO 290
@@ -651,8 +648,8 @@
     DO i = 1, klon
       IF (llflag(i)) THEN
-        pqu(i, k) = pqu(i, k+1)
-        ptu(i, k) = ptu(i, k+1) + (pgeoh(i,k+1)-pgeoh(i,k))/rcpd
-        zbuo = ptu(i, k)*(1.+retv*pqu(i,k)) - ptenh(i, k)*(1.+retv*pqenh(i,k) &
-          ) + 0.5
+        pqu(i, k) = pqu(i, k + 1)
+        ptu(i, k) = ptu(i, k + 1) + (pgeoh(i, k + 1) - pgeoh(i, k)) / rcpd
+        zbuo = ptu(i, k) * (1. + retv * pqu(i, k)) - ptenh(i, k) * (1. + retv * pqenh(i, k) &
+                ) + 0.5
         IF (zbuo>0.) klab(i, k) = 1
         zqold(i) = pqu(i, k)
@@ -661,12 +658,12 @@
 
     iCALL = 1
-    CALL flxadjtq(paph(1,k), ptu(1,k), pqu(1,k), llflag, icall)
-
-    DO i = 1, klon
-      IF (llflag(i) .AND. pqu(i,k)/=zqold(i)) THEN
+    CALL flxadjtq(paph(1, k), ptu(1, k), pqu(1, k), llflag, icall)
+
+    DO i = 1, klon
+      IF (llflag(i) .AND. pqu(i, k)/=zqold(i)) THEN
         klab(i, k) = 2
         plu(i, k) = plu(i, k) + zqold(i) - pqu(i, k)
-        zbuo = ptu(i, k)*(1.+retv*pqu(i,k)) - ptenh(i, k)*(1.+retv*pqenh(i,k) &
-          ) + 0.5
+        zbuo = ptu(i, k) * (1. + retv * pqu(i, k)) - ptenh(i, k) * (1. + retv * pqenh(i, k) &
+                ) + 0.5
         IF (zbuo>0.) kcbot(i) = k
         IF (zbuo>0.) ldcum(i) = .TRUE.
@@ -674,13 +671,14 @@
     END DO
 
-290 END DO
-
+  290 END DO
 
 END SUBROUTINE flxbase
 SUBROUTINE flxasc(pdtime, ptenh, pqenh, pten, pqen, pqsen, pgeo, pgeoh, pap, &
-    paph, pqte, pvervel, ldland, ldcum, ktype, klab, ptu, pqu, plu, pmfu, &
-    pmfub, pentr, pmfus, pmfuq, pmful, plude, pdmfup, kcbot, kctop, kctop0, &
-    kcum, pen_u, pde_u)
+        paph, pqte, pvervel, ldland, ldcum, ktype, klab, ptu, pqu, plu, pmfu, &
+        pmfub, pentr, pmfus, pmfuq, pmful, plude, pdmfup, kcbot, kctop, kctop0, &
+        kcum, pen_u, pde_u)
   USE dimphy
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE
   ! ----------------------------------------------------------------------
@@ -690,5 +688,4 @@
   include "YOMCST.h"
   include "YOETHF.h"
-  include "YOECUMF.h"
 
   REAL pdtime
@@ -696,5 +693,5 @@
   REAL pqen(klon, klev), pqenh(klon, klev), pqsen(klon, klev)
   REAL pgeo(klon, klev), pgeoh(klon, klev)
-  REAL pap(klon, klev), paph(klon, klev+1)
+  REAL pap(klon, klev), paph(klon, klev + 1)
   REAL pqte(klon, klev)
   REAL pvervel(klon, klev) ! vitesse verticale en Pa/s
@@ -735,5 +732,5 @@
   DO k = klev, 3, -1
     DO i = 1, klon
-      IF (pvervel(i,k)<zwmax(i)) THEN
+      IF (pvervel(i, k)<zwmax(i)) THEN
         zwmax(i) = pvervel(i, k)
         klwmin(i) = k
@@ -758,5 +755,5 @@
       pdmfup(i, k) = 0.
       IF (.NOT. ldcum(i) .OR. ktype(i)==3) klab(i, k) = 0
-      IF (.NOT. ldcum(i) .AND. paph(i,k)<4.E4) kctop0(i) = k
+      IF (.NOT. ldcum(i) .AND. paph(i, k)<4.E4) kctop0(i) = k
     END DO
   END DO
@@ -766,5 +763,5 @@
       zdland(i) = 3.0E4
       zdphi = pgeoh(i, kctop0(i)) - pgeoh(i, kcbot(i))
-      IF (ptu(i,kctop0(i))>=ztglace) zdland(i) = zdphi
+      IF (ptu(i, kctop0(i))>=ztglace) zdland(i) = zdphi
       zdland(i) = max(3.0E4, zdland(i))
       zdland(i) = min(5.0E4, zdland(i))
@@ -782,6 +779,6 @@
     END IF
     pmfu(i, klev) = pmfub(i)
-    pmfus(i, klev) = pmfub(i)*(rcpd*ptu(i,klev)+pgeoh(i,klev))
-    pmfuq(i, klev) = pmfub(i)*pqu(i, klev)
+    pmfus(i, klev) = pmfub(i) * (rcpd * ptu(i, klev) + pgeoh(i, klev))
+    pmfuq(i, klev) = pmfub(i) * pqu(i, klev)
   END DO
 
@@ -797,21 +794,21 @@
   DO k = klev - 1, 3, -1
 
-    IF (lmfmid .AND. k<klev-1) THEN
+    IF (lmfmid .AND. k<klev - 1) THEN
       DO i = 1, klon
-        IF (.NOT. ldcum(i) .AND. klab(i,k+1)==0 .AND. &
-            pqen(i,k)>0.9*pqsen(i,k) .AND. pap(i,k)/paph(i,klev+1)>0.4) THEN
-          ptu(i, k+1) = pten(i, k) + (pgeo(i,k)-pgeoh(i,k+1))/rcpd
-          pqu(i, k+1) = pqen(i, k)
-          plu(i, k+1) = 0.0
-          zzzmb = max(cmfcmin, -pvervel(i,k)/rg)
-          zmfmax = (paph(i,k)-paph(i,k-1))/(rg*pdtime)
+        IF (.NOT. ldcum(i) .AND. klab(i, k + 1)==0 .AND. &
+                pqen(i, k)>0.9 * pqsen(i, k) .AND. pap(i, k) / paph(i, klev + 1)>0.4) THEN
+          ptu(i, k + 1) = pten(i, k) + (pgeo(i, k) - pgeoh(i, k + 1)) / rcpd
+          pqu(i, k + 1) = pqen(i, k)
+          plu(i, k + 1) = 0.0
+          zzzmb = max(cmfcmin, -pvervel(i, k) / rg)
+          zmfmax = (paph(i, k) - paph(i, k - 1)) / (rg * pdtime)
           pmfub(i) = min(zzzmb, zmfmax)
-          pmfu(i, k+1) = pmfub(i)
-          pmfus(i, k+1) = pmfub(i)*(rcpd*ptu(i,k+1)+pgeoh(i,k+1))
-          pmfuq(i, k+1) = pmfub(i)*pqu(i, k+1)
-          pmful(i, k+1) = 0.0
-          pdmfup(i, k+1) = 0.0
+          pmfu(i, k + 1) = pmfub(i)
+          pmfus(i, k + 1) = pmfub(i) * (rcpd * ptu(i, k + 1) + pgeoh(i, k + 1))
+          pmfuq(i, k + 1) = pmfub(i) * pqu(i, k + 1)
+          pmful(i, k + 1) = 0.0
+          pdmfup(i, k + 1) = 0.0
           kcbot(i) = k
-          klab(i, k+1) = 1
+          klab(i, k + 1) = 1
           ktype(i) = 3
           pentr(i) = entrmid
@@ -822,8 +819,8 @@
     is = 0
     DO i = 1, klon
-      is = is + klab(i, k+1)
-      IF (klab(i,k+1)==0) klab(i, k) = 0
+      is = is + klab(i, k + 1)
+      IF (klab(i, k + 1)==0) klab(i, k) = 0
       llflag(i) = .FALSE.
-      IF (klab(i,k+1)>0) llflag(i) = .TRUE.
+      IF (klab(i, k + 1)>0) llflag(i) = .TRUE.
     END DO
     IF (is==0) GO TO 480
@@ -834,5 +831,5 @@
       pen_u(i, k) = 0.0
       pde_u(i, k) = 0.0
-      zrho(i) = paph(i, k+1)/(rd*ptenh(i,k+1))
+      zrho(i) = paph(i, k + 1) / (rd * ptenh(i, k + 1))
       zpbot(i) = paph(i, kcbot(i))
       zptop(i) = paph(i, kctop0(i))
@@ -841,24 +838,24 @@
     DO i = 1, klon
       IF (ldcum(i)) THEN
-        zdprho = (paph(i,k+1)-paph(i,k))/(rg*zrho(i))
-        zentr = pentr(i)*pmfu(i, k+1)*zdprho
+        zdprho = (paph(i, k + 1) - paph(i, k)) / (rg * zrho(i))
+        zentr = pentr(i) * pmfu(i, k + 1) * zdprho
         llo1 = k < kcbot(i)
         IF (llo1) pde_u(i, k) = zentr
-        zpmid = 0.5*(zpbot(i)+zptop(i))
-        llo2 = llo1 .AND. ktype(i) == 2 .AND. (zpbot(i)-paph(i,k)<0.2E5 .OR. &
-          paph(i,k)>zpmid)
+        zpmid = 0.5 * (zpbot(i) + zptop(i))
+        llo2 = llo1 .AND. ktype(i) == 2 .AND. (zpbot(i) - paph(i, k)<0.2E5 .OR. &
+                paph(i, k)>zpmid)
         IF (llo2) pen_u(i, k) = zentr
         llo2 = llo1 .AND. (ktype(i)==1 .OR. ktype(i)==3) .AND. &
-          (k>=max(klwmin(i),kctop0(i)+2) .OR. pap(i,k)>zpmid)
+                (k>=max(klwmin(i), kctop0(i) + 2) .OR. pap(i, k)>zpmid)
         IF (llo2) pen_u(i, k) = zentr
         llo1 = pen_u(i, k) > 0. .AND. (ktype(i)==1 .OR. ktype(i)==2)
         IF (llo1) THEN
-          fact = 1. + 3.*(1.-min(1.,(zpbot(i)-pap(i,k))/1.5E4))
-          zentr = zentr*fact
-          pen_u(i, k) = pen_u(i, k)*fact
-          pde_u(i, k) = pde_u(i, k)*fact
+          fact = 1. + 3. * (1. - min(1., (zpbot(i) - pap(i, k)) / 1.5E4))
+          zentr = zentr * fact
+          pen_u(i, k) = pen_u(i, k) * fact
+          pde_u(i, k) = pde_u(i, k) * fact
         END IF
-        IF (llo2 .AND. pqenh(i,k+1)>1.E-5) pen_u(i, k) = zentr + &
-          max(pqte(i,k), 0.)/pqenh(i, k+1)*zrho(i)*zdprho
+        IF (llo2 .AND. pqenh(i, k + 1)>1.E-5) pen_u(i, k) = zentr + &
+                max(pqte(i, k), 0.) / pqenh(i, k + 1) * zrho(i) * zdprho
       END IF
     END DO
@@ -871,26 +868,26 @@
       IF (llflag(i)) THEN
         IF (k<kcbot(i)) THEN
-          zmftest = pmfu(i, k+1) + pen_u(i, k) - pde_u(i, k)
-          zmfmax = min(zmftest, (paph(i,k)-paph(i,k-1))/(rg*pdtime))
-          pen_u(i, k) = max(pen_u(i,k)-max(0.0,zmftest-zmfmax), 0.0)
+          zmftest = pmfu(i, k + 1) + pen_u(i, k) - pde_u(i, k)
+          zmfmax = min(zmftest, (paph(i, k) - paph(i, k - 1)) / (rg * pdtime))
+          pen_u(i, k) = max(pen_u(i, k) - max(0.0, zmftest - zmfmax), 0.0)
         END IF
-        pde_u(i, k) = min(pde_u(i,k), 0.75*pmfu(i,k+1))
+        pde_u(i, k) = min(pde_u(i, k), 0.75 * pmfu(i, k + 1))
         ! calculer le flux de masse du niveau k a partir de celui du k+1
-        pmfu(i, k) = pmfu(i, k+1) + pen_u(i, k) - pde_u(i, k)
+        pmfu(i, k) = pmfu(i, k + 1) + pen_u(i, k) - pde_u(i, k)
         ! calculer les valeurs Su, Qu et l du niveau k dans le panache
         ! montant
-        zqeen = pqenh(i, k+1)*pen_u(i, k)
-        zseen = (rcpd*ptenh(i,k+1)+pgeoh(i,k+1))*pen_u(i, k)
-        zscde = (rcpd*ptu(i,k+1)+pgeoh(i,k+1))*pde_u(i, k)
-        zqude = pqu(i, k+1)*pde_u(i, k)
-        plude(i, k) = plu(i, k+1)*pde_u(i, k)
-        zmfusk = pmfus(i, k+1) + zseen - zscde
-        zmfuqk = pmfuq(i, k+1) + zqeen - zqude
-        zmfulk = pmful(i, k+1) - plude(i, k)
-        plu(i, k) = zmfulk*(1./max(cmfcmin,pmfu(i,k)))
-        pqu(i, k) = zmfuqk*(1./max(cmfcmin,pmfu(i,k)))
-        ptu(i, k) = (zmfusk*(1./max(cmfcmin,pmfu(i,k)))-pgeoh(i,k))/rcpd
-        ptu(i, k) = max(100., ptu(i,k))
-        ptu(i, k) = min(400., ptu(i,k))
+        zqeen = pqenh(i, k + 1) * pen_u(i, k)
+        zseen = (rcpd * ptenh(i, k + 1) + pgeoh(i, k + 1)) * pen_u(i, k)
+        zscde = (rcpd * ptu(i, k + 1) + pgeoh(i, k + 1)) * pde_u(i, k)
+        zqude = pqu(i, k + 1) * pde_u(i, k)
+        plude(i, k) = plu(i, k + 1) * pde_u(i, k)
+        zmfusk = pmfus(i, k + 1) + zseen - zscde
+        zmfuqk = pmfuq(i, k + 1) + zqeen - zqude
+        zmfulk = pmful(i, k + 1) - plude(i, k)
+        plu(i, k) = zmfulk * (1. / max(cmfcmin, pmfu(i, k)))
+        pqu(i, k) = zmfuqk * (1. / max(cmfcmin, pmfu(i, k)))
+        ptu(i, k) = (zmfusk * (1. / max(cmfcmin, pmfu(i, k))) - pgeoh(i, k)) / rcpd
+        ptu(i, k) = max(100., ptu(i, k))
+        ptu(i, k) = min(400., ptu(i, k))
         zqold(i) = pqu(i, k)
       ELSE
@@ -904,14 +901,14 @@
 
     iCALL = 1
-    CALL flxadjtq(paph(1,k), ptu(1,k), pqu(1,k), llflag, icall)
-
-    DO i = 1, klon
-      IF (llflag(i) .AND. pqu(i,k)/=zqold(i)) THEN
+    CALL flxadjtq(paph(1, k), ptu(1, k), pqu(1, k), llflag, icall)
+
+    DO i = 1, klon
+      IF (llflag(i) .AND. pqu(i, k)/=zqold(i)) THEN
         klab(i, k) = 2
         plu(i, k) = plu(i, k) + zqold(i) - pqu(i, k)
-        zbuo = ptu(i, k)*(1.+retv*pqu(i,k)) - ptenh(i, k)*(1.+retv*pqenh(i,k) &
-          )
-        IF (klab(i,k+1)==1) zbuo = zbuo + 0.5
-        IF (zbuo>0. .AND. pmfu(i,k)>=0.1*pmfub(i)) THEN
+        zbuo = ptu(i, k) * (1. + retv * pqu(i, k)) - ptenh(i, k) * (1. + retv * pqenh(i, k) &
+                )
+        IF (klab(i, k + 1)==1) zbuo = zbuo + 0.5
+        IF (zbuo>0. .AND. pmfu(i, k)>=0.1 * pmfub(i)) THEN
           kctop(i) = k
           ldcum(i) = .TRUE.
@@ -919,7 +916,7 @@
           IF (ldland(i)) zdnoprc = zdland(i)
           zprcon = cprcon
-          IF ((zpbot(i)-paph(i,k))<zdnoprc) zprcon = 0.0
-          zlnew = plu(i, k)/(1.+zprcon*(pgeoh(i,k)-pgeoh(i,k+1)))
-          pdmfup(i, k) = max(0., (plu(i,k)-zlnew)*pmfu(i,k))
+          IF ((zpbot(i) - paph(i, k))<zdnoprc) zprcon = 0.0
+          zlnew = plu(i, k) / (1. + zprcon * (pgeoh(i, k) - pgeoh(i, k + 1)))
+          pdmfup(i, k) = max(0., (plu(i, k) - zlnew) * pmfu(i, k))
           plu(i, k) = zlnew
         ELSE
@@ -931,11 +928,11 @@
     DO i = 1, klon
       IF (llflag(i)) THEN
-        pmful(i, k) = plu(i, k)*pmfu(i, k)
-        pmfus(i, k) = (rcpd*ptu(i,k)+pgeoh(i,k))*pmfu(i, k)
-        pmfuq(i, k) = pqu(i, k)*pmfu(i, k)
-      END IF
-    END DO
-
-480 END DO
+        pmful(i, k) = plu(i, k) * pmfu(i, k)
+        pmfus(i, k) = (rcpd * ptu(i, k) + pgeoh(i, k)) * pmfu(i, k)
+        pmfuq(i, k) = pqu(i, k) * pmfu(i, k)
+      END IF
+    END DO
+
+  480 END DO
   ! ----------------------------------------------------------------------
   ! DETERMINE CONVECTIVE FLUXES ABOVE NON-BUOYANCY LEVEL
@@ -945,5 +942,5 @@
   ! ----------------------------------------------------------------------
   DO i = 1, klon
-    IF (kctop(i)==klev-1) ldcum(i) = .FALSE.
+    IF (kctop(i)==klev - 1) ldcum(i) = .FALSE.
     kcbot(i) = max(kcbot(i), kctop(i))
   END DO
@@ -961,26 +958,28 @@
     IF (ldcum(i)) THEN
       k = kctop(i) - 1
-      pde_u(i, k) = (1.-cmfctop)*pmfu(i, k+1)
-      plude(i, k) = pde_u(i, k)*plu(i, k+1)
-      pmfu(i, k) = pmfu(i, k+1) - pde_u(i, k)
+      pde_u(i, k) = (1. - cmfctop) * pmfu(i, k + 1)
+      plude(i, k) = pde_u(i, k) * plu(i, k + 1)
+      pmfu(i, k) = pmfu(i, k + 1) - pde_u(i, k)
       zlnew = plu(i, k)
-      pdmfup(i, k) = max(0., (plu(i,k)-zlnew)*pmfu(i,k))
+      pdmfup(i, k) = max(0., (plu(i, k) - zlnew) * pmfu(i, k))
       plu(i, k) = zlnew
-      pmfus(i, k) = (rcpd*ptu(i,k)+pgeoh(i,k))*pmfu(i, k)
-      pmfuq(i, k) = pqu(i, k)*pmfu(i, k)
-      pmful(i, k) = plu(i, k)*pmfu(i, k)
-      plude(i, k-1) = pmful(i, k)
+      pmfus(i, k) = (rcpd * ptu(i, k) + pgeoh(i, k)) * pmfu(i, k)
+      pmfuq(i, k) = pqu(i, k) * pmfu(i, k)
+      pmful(i, k) = plu(i, k) * pmfu(i, k)
+      plude(i, k - 1) = pmful(i, k)
     END IF
   END DO
 
-800 CONTINUE
+  800 CONTINUE
 
 END SUBROUTINE flxasc
 SUBROUTINE flxflux(pdtime, pqen, pqsen, ptenh, pqenh, pap, paph, ldland, &
-    pgeoh, kcbot, kctop, lddraf, kdtop, ktype, ldcum, pmfu, pmfd, pmfus, &
-    pmfds, pmfuq, pmfdq, pmful, plude, pdmfup, pdmfdp, pten, prfl, psfl, &
-    pdpmel, ktopm2, pmflxr, pmflxs)
+        pgeoh, kcbot, kctop, lddraf, kdtop, ktype, ldcum, pmfu, pmfd, pmfus, &
+        pmfds, pmfuq, pmfdq, pmful, plude, pdmfup, pdmfdp, pten, prfl, psfl, &
+        pdpmel, ktopm2, pmflxr, pmflxs)
   USE dimphy
   USE lmdz_print_control, ONLY: prt_level
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE
   ! ----------------------------------------------------------------------
@@ -990,5 +989,4 @@
   include "YOMCST.h"
   include "YOETHF.h"
-  include "YOECUMF.h"
 
   REAL cevapcu(klon, klev)
@@ -996,5 +994,5 @@
   REAL pqen(klon, klev), pqenh(klon, klev), pqsen(klon, klev)
   REAL pten(klon, klev), ptenh(klon, klev)
-  REAL paph(klon, klev+1), pgeoh(klon, klev)
+  REAL paph(klon, klev + 1), pgeoh(klon, klev)
 
   REAL pap(klon, klev)
@@ -1011,5 +1009,5 @@
   REAL pdmfdp(klon, klev), maxpdmfdp(klon, klev)
   REAL prfl(klon), psfl(klon)
-  REAL pmflxr(klon, klev+1), pmflxs(klon, klev+1)
+  REAL pmflxr(klon, klev + 1), pmflxs(klon, klev + 1)
   INTEGER kcbot(klon), kctop(klon), ktype(klon)
   LOGICAL ldland(klon), ldcum(klon)
@@ -1028,6 +1026,6 @@
   DO k = 1, klev
     DO i = 1, klon
-      cevapcu(i, k) = 1.93E-6*261.*sqrt(1.E3/(38.3*0.293)*sqrt(0.5*(paph(i,k) &
-        +paph(i,k+1))/paph(i,klev+1)))*0.5/rg
+      cevapcu(i, k) = 1.93E-6 * 261. * sqrt(1.E3 / (38.3 * 0.293) * sqrt(0.5 * (paph(i, k) &
+              + paph(i, k + 1)) / paph(i, klev + 1))) * 0.5 / rg
     END DO
   END DO
@@ -1035,6 +1033,6 @@
   ! SPECIFY CONSTANTS
 
-  zcons1 = rcpd/(rlmlt*rg*pdtime)
-  zcons2 = 1./(rg*pdtime)
+  zcons1 = rcpd / (rlmlt * rg * pdtime)
+  zcons2 = 1. / (rg * pdtime)
   zcucov = 0.05
   ztmelp2 = rtt + 2.
@@ -1052,7 +1050,7 @@
   DO k = ktopm2, klev
     DO i = 1, klon
-      IF (ldcum(i) .AND. k>=kctop(i)-1) THEN
-        pmfus(i, k) = pmfus(i, k) - pmfu(i, k)*(rcpd*ptenh(i,k)+pgeoh(i,k))
-        pmfuq(i, k) = pmfuq(i, k) - pmfu(i, k)*pqenh(i, k)
+      IF (ldcum(i) .AND. k>=kctop(i) - 1) THEN
+        pmfus(i, k) = pmfus(i, k) - pmfu(i, k) * (rcpd * ptenh(i, k) + pgeoh(i, k))
+        pmfuq(i, k) = pmfuq(i, k) - pmfu(i, k) * pqenh(i, k)
         zdp = 1.5E4
         IF (ldland(i)) zdp = 3.E4
@@ -1062,15 +1060,15 @@
         ! evaporee dans l'environnement)
 
-        IF (paph(i,kcbot(i))-paph(i,kctop(i))>=zdp .AND. pqen(i,k-1)>0.8* &
-          pqsen(i,k-1)) pdmfup(i, k-1) = pdmfup(i, k-1) + plude(i, k-1)
+        IF (paph(i, kcbot(i)) - paph(i, kctop(i))>=zdp .AND. pqen(i, k - 1)>0.8 * &
+                pqsen(i, k - 1)) pdmfup(i, k - 1) = pdmfup(i, k - 1) + plude(i, k - 1)
 
         IF (lddraf(i) .AND. k>=kdtop(i)) THEN
-          pmfds(i, k) = pmfds(i, k) - pmfd(i, k)*(rcpd*ptenh(i,k)+pgeoh(i,k))
-          pmfdq(i, k) = pmfdq(i, k) - pmfd(i, k)*pqenh(i, k)
+          pmfds(i, k) = pmfds(i, k) - pmfd(i, k) * (rcpd * ptenh(i, k) + pgeoh(i, k))
+          pmfdq(i, k) = pmfdq(i, k) - pmfd(i, k) * pqenh(i, k)
         ELSE
           pmfd(i, k) = 0.
           pmfds(i, k) = 0.
           pmfdq(i, k) = 0.
-          pdmfdp(i, k-1) = 0.
+          pdmfdp(i, k - 1) = 0.
         END IF
       ELSE
@@ -1079,10 +1077,10 @@
         pmfuq(i, k) = 0.
         pmful(i, k) = 0.
-        pdmfup(i, k-1) = 0.
-        plude(i, k-1) = 0.
+        pdmfup(i, k - 1) = 0.
+        plude(i, k - 1) = 0.
         pmfd(i, k) = 0.
         pmfds(i, k) = 0.
         pmfdq(i, k) = 0.
-        pdmfdp(i, k-1) = 0.
+        pdmfdp(i, k - 1) = 0.
       END IF
     END DO
@@ -1093,10 +1091,10 @@
       IF (ldcum(i) .AND. k>kcbot(i)) THEN
         ikb = kcbot(i)
-        zzp = ((paph(i,klev+1)-paph(i,k))/(paph(i,klev+1)-paph(i,ikb)))
+        zzp = ((paph(i, klev + 1) - paph(i, k)) / (paph(i, klev + 1) - paph(i, ikb)))
         IF (ktype(i)==3) zzp = zzp**2
-        pmfu(i, k) = pmfu(i, ikb)*zzp
-        pmfus(i, k) = pmfus(i, ikb)*zzp
-        pmfuq(i, k) = pmfuq(i, ikb)*zzp
-        pmful(i, k) = pmful(i, ikb)*zzp
+        pmfu(i, k) = pmfu(i, ikb) * zzp
+        pmfus(i, k) = pmfus(i, ikb) * zzp
+        pmfuq(i, k) = pmfuq(i, ikb) * zzp
+        pmful(i, k) = pmful(i, ikb) * zzp
       END IF
     END DO
@@ -1116,29 +1114,29 @@
     DO i = 1, klon
       IF (ldcum(i)) THEN
-        IF (pmflxs(i,k)>0.0 .AND. pten(i,k)>ztmelp2) THEN
-          zfac = zcons1*(paph(i,k+1)-paph(i,k))
-          zsnmlt = min(pmflxs(i,k), zfac*(pten(i,k)-ztmelp2))
+        IF (pmflxs(i, k)>0.0 .AND. pten(i, k)>ztmelp2) THEN
+          zfac = zcons1 * (paph(i, k + 1) - paph(i, k))
+          zsnmlt = min(pmflxs(i, k), zfac * (pten(i, k) - ztmelp2))
           pdpmel(i, k) = zsnmlt
-          ztmsmlt = pten(i, k) - zsnmlt/zfac
-          zdelta = max(0., sign(1.,rtt-ztmsmlt))
-          zqsat = r2es*foeew(ztmsmlt, zdelta)/pap(i, k)
+          ztmsmlt = pten(i, k) - zsnmlt / zfac
+          zdelta = max(0., sign(1., rtt - ztmsmlt))
+          zqsat = r2es * foeew(ztmsmlt, zdelta) / pap(i, k)
           zqsat = min(0.5, zqsat)
-          zqsat = zqsat/(1.-retv*zqsat)
+          zqsat = zqsat / (1. - retv * zqsat)
           pqsen(i, k) = zqsat
         END IF
-        IF (pten(i,k)>rtt) THEN
-          pmflxr(i, k+1) = pmflxr(i, k) + pdmfup(i, k) + pdmfdp(i, k) + &
-            pdpmel(i, k)
-          pmflxs(i, k+1) = pmflxs(i, k) - pdpmel(i, k)
+        IF (pten(i, k)>rtt) THEN
+          pmflxr(i, k + 1) = pmflxr(i, k) + pdmfup(i, k) + pdmfdp(i, k) + &
+                  pdpmel(i, k)
+          pmflxs(i, k + 1) = pmflxs(i, k) - pdpmel(i, k)
         ELSE
-          pmflxs(i, k+1) = pmflxs(i, k) + pdmfup(i, k) + pdmfdp(i, k)
-          pmflxr(i, k+1) = pmflxr(i, k)
+          pmflxs(i, k + 1) = pmflxs(i, k) + pdmfup(i, k) + pdmfdp(i, k)
+          pmflxr(i, k + 1) = pmflxr(i, k)
         END IF
         ! si la precipitation est negative, on ajuste le plux du
         ! panache descendant pour eliminer la negativite
-        IF ((pmflxr(i,k+1)+pmflxs(i,k+1))<0.0) THEN
+        IF ((pmflxr(i, k + 1) + pmflxs(i, k + 1))<0.0) THEN
           pdmfdp(i, k) = -pmflxr(i, k) - pmflxs(i, k) - pdmfup(i, k)
-          pmflxr(i, k+1) = 0.0
-          pmflxs(i, k+1) = 0.0
+          pmflxr(i, k + 1) = 0.0
+          pmflxs(i, k + 1) = 0.0
           pdpmel(i, k) = 0.0
         END IF
@@ -1174,12 +1172,12 @@
         zrfl = pmflxr(i, k) + pmflxs(i, k)
         IF (zrfl>1.0E-20) THEN
-          zrnew = (max(0.,sqrt(zrfl/zcucov)-cevapcu(i, &
-            k)*(paph(i,k+1)-paph(i,k))*max(0.,pqsen(i,k)-pqen(i,k))))**2* &
-            zcucov
-          zrmin = zrfl - zcucov*max(0., 0.8*pqsen(i,k)-pqen(i,k))*zcons2*( &
-            paph(i,k+1)-paph(i,k))
+          zrnew = (max(0., sqrt(zrfl / zcucov) - cevapcu(i, &
+                  k) * (paph(i, k + 1) - paph(i, k)) * max(0., pqsen(i, k) - pqen(i, k))))**2 * &
+                  zcucov
+          zrmin = zrfl - zcucov * max(0., 0.8 * pqsen(i, k) - pqen(i, k)) * zcons2 * (&
+                  paph(i, k + 1) - paph(i, k))
           zrnew = max(zrnew, zrmin)
           zrfln = max(zrnew, 0.)
-          zdrfl = min(0., zrfln-zrfl)
+          zdrfl = min(0., zrfln - zrfl)
           ! jq At least the amount of precipiation needed to feed the
           ! downdraft
@@ -1187,10 +1185,10 @@
           ! can't
           ! jq be evaporated (surely the evaporation can't be positive):
-          zdrfl = max(zdrfl, min(-pmflxr(i,k)-pmflxs(i,k)-maxpdmfdp(i, &
-            k),0.0))
+          zdrfl = max(zdrfl, min(-pmflxr(i, k) - pmflxs(i, k) - maxpdmfdp(i, &
+                  k), 0.0))
           ! jq End of insertion
 
-          zdenom = 1.0/max(1.0E-20, pmflxr(i,k)+pmflxs(i,k))
-          IF (pten(i,k)>rtt) THEN
+          zdenom = 1.0 / max(1.0E-20, pmflxr(i, k) + pmflxs(i, k))
+          IF (pten(i, k)>rtt) THEN
             zpdr = pdmfdp(i, k)
             zpds = 0.0
@@ -1199,31 +1197,32 @@
             zpds = pdmfdp(i, k)
           END IF
-          pmflxr(i, k+1) = pmflxr(i, k) + zpdr + pdpmel(i, k) + &
-            zdrfl*pmflxr(i, k)*zdenom
-          pmflxs(i, k+1) = pmflxs(i, k) + zpds - pdpmel(i, k) + &
-            zdrfl*pmflxs(i, k)*zdenom
+          pmflxr(i, k + 1) = pmflxr(i, k) + zpdr + pdpmel(i, k) + &
+                  zdrfl * pmflxr(i, k) * zdenom
+          pmflxs(i, k + 1) = pmflxs(i, k) + zpds - pdpmel(i, k) + &
+                  zdrfl * pmflxs(i, k) * zdenom
           pdmfup(i, k) = pdmfup(i, k) + zdrfl
         ELSE
-          pmflxr(i, k+1) = 0.0
-          pmflxs(i, k+1) = 0.0
+          pmflxr(i, k + 1) = 0.0
+          pmflxs(i, k + 1) = 0.0
           pdmfdp(i, k) = 0.0
           pdpmel(i, k) = 0.0
         END IF
-        IF (pmflxr(i,k)+pmflxs(i,k)<-1.E-26 .AND. prt_level>=1) WRITE (*, *) &
-          'precip. < 1e-16 ', pmflxr(i, k) + pmflxs(i, k)
-      END IF
-    END DO
-  END DO
-
-  DO i = 1, klon
-    prfl(i) = pmflxr(i, klev+1)
-    psfl(i) = pmflxs(i, klev+1)
-  END DO
-
+        IF (pmflxr(i, k) + pmflxs(i, k)<-1.E-26 .AND. prt_level>=1) WRITE (*, *) &
+                'precip. < 1e-16 ', pmflxr(i, k) + pmflxs(i, k)
+      END IF
+    END DO
+  END DO
+
+  DO i = 1, klon
+    prfl(i) = pmflxr(i, klev + 1)
+    psfl(i) = pmflxs(i, klev + 1)
+  END DO
 
 END SUBROUTINE flxflux
 SUBROUTINE flxdtdq(pdtime, ktopm2, paph, ldcum, pten, pmfus, pmfds, pmfuq, &
-    pmfdq, pmful, pdmfup, pdmfdp, pdpmel, dt_con, dq_con)
+        pmfdq, pmful, pdmfup, pdmfdp, pdpmel, dt_con, dq_con)
   USE dimphy
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE
   ! ----------------------------------------------------------------------
@@ -1232,9 +1231,8 @@
   include "YOMCST.h"
   include "YOETHF.h"
-  include "YOECUMF.h"
   ! -----------------------------------------------------------------
   LOGICAL llo1
 
-  REAL pten(klon, klev), paph(klon, klev+1)
+  REAL pten(klon, klev), paph(klon, klev + 1)
   REAL pmfus(klon, klev), pmfuq(klon, klev), pmful(klon, klev)
   REAL pmfds(klon, klev), pmfdq(klon, klev)
@@ -1254,13 +1252,13 @@
     DO i = 1, klon
       IF (ldcum(i)) THEN
-        llo1 = (pten(i,k)-rtt) > 0.
+        llo1 = (pten(i, k) - rtt) > 0.
         zalv = rlstt
         IF (llo1) zalv = rlvtt
-        zdtdt = rg/(paph(i,k+1)-paph(i,k))/rcpd*(pmfus(i,k+1)-pmfus(i,k)+ &
-          pmfds(i,k+1)-pmfds(i,k)-rlmlt*pdpmel(i,k)-zalv*(pmful(i, &
-          k+1)-pmful(i,k)-pdmfup(i,k)-pdmfdp(i,k)))
+        zdtdt = rg / (paph(i, k + 1) - paph(i, k)) / rcpd * (pmfus(i, k + 1) - pmfus(i, k) + &
+                pmfds(i, k + 1) - pmfds(i, k) - rlmlt * pdpmel(i, k) - zalv * (pmful(i, &
+                k + 1) - pmful(i, k) - pdmfup(i, k) - pdmfdp(i, k)))
         dt_con(i, k) = zdtdt
-        zdqdt = rg/(paph(i,k+1)-paph(i,k))*(pmfuq(i,k+1)-pmfuq(i,k)+pmfdq(i,k &
-          +1)-pmfdq(i,k)+pmful(i,k+1)-pmful(i,k)-pdmfup(i,k)-pdmfdp(i,k))
+        zdqdt = rg / (paph(i, k + 1) - paph(i, k)) * (pmfuq(i, k + 1) - pmfuq(i, k) + pmfdq(i, k &
+                + 1) - pmfdq(i, k) + pmful(i, k + 1) - pmful(i, k) - pdmfup(i, k) - pdmfdp(i, k))
         dq_con(i, k) = zdqdt
       END IF
@@ -1271,21 +1269,22 @@
   DO i = 1, klon
     IF (ldcum(i)) THEN
-      llo1 = (pten(i,k)-rtt) > 0.
+      llo1 = (pten(i, k) - rtt) > 0.
       zalv = rlstt
       IF (llo1) zalv = rlvtt
-      zdtdt = -rg/(paph(i,k+1)-paph(i,k))/rcpd*(pmfus(i,k)+pmfds(i,k)+rlmlt* &
-        pdpmel(i,k)-zalv*(pmful(i,k)+pdmfup(i,k)+pdmfdp(i,k)))
+      zdtdt = -rg / (paph(i, k + 1) - paph(i, k)) / rcpd * (pmfus(i, k) + pmfds(i, k) + rlmlt * &
+              pdpmel(i, k) - zalv * (pmful(i, k) + pdmfup(i, k) + pdmfdp(i, k)))
       dt_con(i, k) = zdtdt
-      zdqdt = -rg/(paph(i,k+1)-paph(i,k))*(pmfuq(i,k)+pmfdq(i,k)+pmful(i,k)+ &
-        pdmfup(i,k)+pdmfdp(i,k))
+      zdqdt = -rg / (paph(i, k + 1) - paph(i, k)) * (pmfuq(i, k) + pmfdq(i, k) + pmful(i, k) + &
+              pdmfup(i, k) + pdmfdp(i, k))
       dq_con(i, k) = zdqdt
     END IF
   END DO
 
-
 END SUBROUTINE flxdtdq
 SUBROUTINE flxdlfs(ptenh, pqenh, pgeoh, paph, ptu, pqu, ldcum, kcbot, kctop, &
-    pmfub, prfl, ptd, pqd, pmfd, pmfds, pmfdq, pdmfdp, kdtop, lddraf)
+        pmfub, prfl, ptd, pqd, pmfd, pmfds, pmfdq, pdmfdp, kdtop, lddraf)
   USE dimphy
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE
 
@@ -1307,9 +1306,8 @@
   include "YOMCST.h"
   include "YOETHF.h"
-  include "YOECUMF.h"
 
   REAL ptenh(klon, klev)
   REAL pqenh(klon, klev)
-  REAL pgeoh(klon, klev), paph(klon, klev+1)
+  REAL pgeoh(klon, klev), paph(klon, klev + 1)
   REAL ptu(klon, klev), pqu(klon, klev)
   REAL pmfub(klon)
@@ -1354,5 +1352,5 @@
       zqenwb(i, k) = pqenh(i, k)
       llo2(i) = ldcum(i) .AND. prfl(i) > 0. .AND. .NOT. lddraf(i) .AND. &
-        (k<kcbot(i) .AND. k>kctop(i))
+              (k<kcbot(i) .AND. k>kctop(i))
       IF (llo2(i)) is = is + 1
     END DO
@@ -1360,5 +1358,5 @@
 
     iCALL = 2
-    CALL flxadjtq(paph(1,k), ztenwb(1,k), zqenwb(1,k), llo2, icall)
+    CALL flxadjtq(paph(1, k), ztenwb(1, k), zqenwb(1, k), llo2, icall)
 
     ! ----------------------------------------------------------------------
@@ -1369,10 +1367,10 @@
     DO i = 1, klon
       IF (llo2(i)) THEN
-        zttest = 0.5*(ptu(i,k)+ztenwb(i,k))
-        zqtest = 0.5*(pqu(i,k)+zqenwb(i,k))
-        zbuo = zttest*(1.+retv*zqtest) - ptenh(i, k)*(1.+retv*pqenh(i,k))
+        zttest = 0.5 * (ptu(i, k) + ztenwb(i, k))
+        zqtest = 0.5 * (pqu(i, k) + zqenwb(i, k))
+        zbuo = zttest * (1. + retv * zqtest) - ptenh(i, k) * (1. + retv * pqenh(i, k))
         zcond(i) = pqenh(i, k) - zqenwb(i, k)
-        zmftop = -cmfdeps*pmfub(i)
-        IF (zbuo<0. .AND. prfl(i)>10.*zmftop*zcond(i)) THEN
+        zmftop = -cmfdeps * pmfub(i)
+        IF (zbuo<0. .AND. prfl(i)>10. * zmftop * zcond(i)) THEN
           kdtop(i) = k
           lddraf(i) = .TRUE.
@@ -1380,19 +1378,20 @@
           pqd(i, k) = zqtest
           pmfd(i, k) = zmftop
-          pmfds(i, k) = pmfd(i, k)*(rcpd*ptd(i,k)+pgeoh(i,k))
-          pmfdq(i, k) = pmfd(i, k)*pqd(i, k)
-          pdmfdp(i, k-1) = -0.5*pmfd(i, k)*zcond(i)
-          prfl(i) = prfl(i) + pdmfdp(i, k-1)
+          pmfds(i, k) = pmfd(i, k) * (rcpd * ptd(i, k) + pgeoh(i, k))
+          pmfdq(i, k) = pmfd(i, k) * pqd(i, k)
+          pdmfdp(i, k - 1) = -0.5 * pmfd(i, k) * zcond(i)
+          prfl(i) = prfl(i) + pdmfdp(i, k - 1)
         END IF
       END IF
     END DO
 
-290 END DO
-
+  290 END DO
 
 END SUBROUTINE flxdlfs
 SUBROUTINE flxddraf(ptenh, pqenh, pgeoh, paph, prfl, ptd, pqd, pmfd, pmfds, &
-    pmfdq, pdmfdp, lddraf, pen_d, pde_d)
+        pmfdq, pdmfdp, lddraf, pen_d, pde_d)
   USE dimphy
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE
 
@@ -1414,8 +1413,7 @@
   include "YOMCST.h"
   include "YOETHF.h"
-  include "YOECUMF.h"
 
   REAL ptenh(klon, klev), pqenh(klon, klev)
-  REAL pgeoh(klon, klev), paph(klon, klev+1)
+  REAL pgeoh(klon, klev), paph(klon, klev + 1)
 
   REAL ptd(klon, klev), pqd(klon, klev)
@@ -1443,5 +1441,5 @@
     is = 0
     DO i = 1, klon
-      llo2(i) = lddraf(i) .AND. pmfd(i, k-1) < 0.
+      llo2(i) = lddraf(i) .AND. pmfd(i, k - 1) < 0.
       IF (llo2(i)) is = is + 1
     END DO
@@ -1450,6 +1448,6 @@
     DO i = 1, klon
       IF (llo2(i)) THEN
-        zentr = entrdd*pmfd(i, k-1)*rd*ptenh(i, k-1)/(rg*paph(i,k-1))* &
-          (paph(i,k)-paph(i,k-1))
+        zentr = entrdd * pmfd(i, k - 1) * rd * ptenh(i, k - 1) / (rg * paph(i, k - 1)) * &
+                (paph(i, k) - paph(i, k - 1))
         pen_d(i, k) = zentr
         pde_d(i, k) = zentr
@@ -1462,6 +1460,6 @@
         IF (llo2(i)) THEN
           pen_d(i, k) = 0.
-          pde_d(i, k) = pmfd(i, itopde)*(paph(i,k)-paph(i,k-1))/ &
-            (paph(i,klev+1)-paph(i,itopde))
+          pde_d(i, k) = pmfd(i, itopde) * (paph(i, k) - paph(i, k - 1)) / &
+                  (paph(i, klev + 1) - paph(i, itopde))
         END IF
       END DO
@@ -1470,15 +1468,15 @@
     DO i = 1, klon
       IF (llo2(i)) THEN
-        pmfd(i, k) = pmfd(i, k-1) + pen_d(i, k) - pde_d(i, k)
-        zseen = (rcpd*ptenh(i,k-1)+pgeoh(i,k-1))*pen_d(i, k)
-        zqeen = pqenh(i, k-1)*pen_d(i, k)
-        zsdde = (rcpd*ptd(i,k-1)+pgeoh(i,k-1))*pde_d(i, k)
-        zqdde = pqd(i, k-1)*pde_d(i, k)
-        zmfdsk = pmfds(i, k-1) + zseen - zsdde
-        zmfdqk = pmfdq(i, k-1) + zqeen - zqdde
-        pqd(i, k) = zmfdqk*(1./min(-cmfcmin,pmfd(i,k)))
-        ptd(i, k) = (zmfdsk*(1./min(-cmfcmin,pmfd(i,k)))-pgeoh(i,k))/rcpd
-        ptd(i, k) = min(400., ptd(i,k))
-        ptd(i, k) = max(100., ptd(i,k))
+        pmfd(i, k) = pmfd(i, k - 1) + pen_d(i, k) - pde_d(i, k)
+        zseen = (rcpd * ptenh(i, k - 1) + pgeoh(i, k - 1)) * pen_d(i, k)
+        zqeen = pqenh(i, k - 1) * pen_d(i, k)
+        zsdde = (rcpd * ptd(i, k - 1) + pgeoh(i, k - 1)) * pde_d(i, k)
+        zqdde = pqd(i, k - 1) * pde_d(i, k)
+        zmfdsk = pmfds(i, k - 1) + zseen - zsdde
+        zmfdqk = pmfdq(i, k - 1) + zqeen - zqdde
+        pqd(i, k) = zmfdqk * (1. / min(-cmfcmin, pmfd(i, k)))
+        ptd(i, k) = (zmfdsk * (1. / min(-cmfcmin, pmfd(i, k))) - pgeoh(i, k)) / rcpd
+        ptd(i, k) = min(400., ptd(i, k))
+        ptd(i, k) = max(100., ptd(i, k))
         zcond(i) = pqd(i, k)
       END IF
@@ -1486,22 +1484,22 @@
 
     iCALL = 2
-    CALL flxadjtq(paph(1,k), ptd(1,k), pqd(1,k), llo2, icall)
+    CALL flxadjtq(paph(1, k), ptd(1, k), pqd(1, k), llo2, icall)
 
     DO i = 1, klon
       IF (llo2(i)) THEN
         zcond(i) = zcond(i) - pqd(i, k)
-        zbuo = ptd(i, k)*(1.+retv*pqd(i,k)) - ptenh(i, k)*(1.+retv*pqenh(i,k) &
-          )
-        llo1 = zbuo < 0. .AND. (prfl(i)-pmfd(i,k)*zcond(i)>0.)
+        zbuo = ptd(i, k) * (1. + retv * pqd(i, k)) - ptenh(i, k) * (1. + retv * pqenh(i, k) &
+                )
+        llo1 = zbuo < 0. .AND. (prfl(i) - pmfd(i, k) * zcond(i)>0.)
         IF (.NOT. llo1) pmfd(i, k) = 0.0
-        pmfds(i, k) = (rcpd*ptd(i,k)+pgeoh(i,k))*pmfd(i, k)
-        pmfdq(i, k) = pqd(i, k)*pmfd(i, k)
-        zdmfdp = -pmfd(i, k)*zcond(i)
-        pdmfdp(i, k-1) = zdmfdp
+        pmfds(i, k) = (rcpd * ptd(i, k) + pgeoh(i, k)) * pmfd(i, k)
+        pmfdq(i, k) = pqd(i, k) * pmfd(i, k)
+        zdmfdp = -pmfd(i, k) * zcond(i)
+        pdmfdp(i, k - 1) = zdmfdp
         prfl(i) = prfl(i) + zdmfdp
       END IF
     END DO
 
-180 END DO
+  180 END DO
 
 END SUBROUTINE flxddraf
@@ -1530,9 +1528,8 @@
   include "FCTTRE.h"
 
-  z5alvcp = r5les*rlvtt/rcpd
-  z5alscp = r5ies*rlstt/rcpd
-  zalvdcp = rlvtt/rcpd
-  zalsdcp = rlstt/rcpd
-
+  z5alvcp = r5les * rlvtt / rcpd
+  z5alscp = r5ies * rlstt / rcpd
+  zalvdcp = rlvtt / rcpd
+  zalsdcp = rlstt / rcpd
 
   DO i = 1, klon
@@ -1542,15 +1539,15 @@
   DO i = 1, klon
     IF (ldflag(i)) THEN
-      zdelta = max(0., sign(1.,rtt-pt(i)))
-      zcvm5 = z5alvcp*(1.-zdelta) + zdelta*z5alscp
-      zldcp = zalvdcp*(1.-zdelta) + zdelta*zalsdcp
-      zqsat = r2es*foeew(pt(i), zdelta)/pp(i)
+      zdelta = max(0., sign(1., rtt - pt(i)))
+      zcvm5 = z5alvcp * (1. - zdelta) + zdelta * z5alscp
+      zldcp = zalvdcp * (1. - zdelta) + zdelta * zalsdcp
+      zqsat = r2es * foeew(pt(i), zdelta) / pp(i)
       zqsat = min(0.5, zqsat)
-      zcor = 1./(1.-retv*zqsat)
-      zqsat = zqsat*zcor
-      zcond(i) = (pq(i)-zqsat)/(1.+foede(pt(i),zdelta,zcvm5,zqsat,zcor))
+      zcor = 1. / (1. - retv * zqsat)
+      zqsat = zqsat * zcor
+      zcond(i) = (pq(i) - zqsat) / (1. + foede(pt(i), zdelta, zcvm5, zqsat, zcor))
       IF (kcall==1) zcond(i) = max(zcond(i), 0.)
       IF (kcall==2) zcond(i) = min(zcond(i), 0.)
-      pt(i) = pt(i) + zldcp*zcond(i)
+      pt(i) = pt(i) + zldcp * zcond(i)
       pq(i) = pq(i) - zcond(i)
     END IF
@@ -1565,26 +1562,26 @@
   DO i = 1, klon
     IF (ldflag(i) .AND. zcond(i)/=0.) THEN
-      zdelta = max(0., sign(1.,rtt-pt(i)))
-      zcvm5 = z5alvcp*(1.-zdelta) + zdelta*z5alscp
-      zldcp = zalvdcp*(1.-zdelta) + zdelta*zalsdcp
-      zqsat = r2es*foeew(pt(i), zdelta)/pp(i)
+      zdelta = max(0., sign(1., rtt - pt(i)))
+      zcvm5 = z5alvcp * (1. - zdelta) + zdelta * z5alscp
+      zldcp = zalvdcp * (1. - zdelta) + zdelta * zalsdcp
+      zqsat = r2es * foeew(pt(i), zdelta) / pp(i)
       zqsat = min(0.5, zqsat)
-      zcor = 1./(1.-retv*zqsat)
-      zqsat = zqsat*zcor
-      zcond1 = (pq(i)-zqsat)/(1.+foede(pt(i),zdelta,zcvm5,zqsat,zcor))
-      pt(i) = pt(i) + zldcp*zcond1
+      zcor = 1. / (1. - retv * zqsat)
+      zqsat = zqsat * zcor
+      zcond1 = (pq(i) - zqsat) / (1. + foede(pt(i), zdelta, zcvm5, zqsat, zcor))
+      pt(i) = pt(i) + zldcp * zcond1
       pq(i) = pq(i) - zcond1
     END IF
   END DO
 
-230 CONTINUE
+  230 CONTINUE
 
 END SUBROUTINE flxadjtq
 SUBROUTINE flxsetup
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE
 
   ! THIS ROUTINE DEFINES DISPOSABLE PARAMETERS FOR MASSFLUX SCHEME
-
-  include "YOECUMF.h"
 
   entrpen = 1.0E-4 ! ENTRAINMENT RATE FOR PENETRATIVE CONVECTION
@@ -1605,4 +1602,3 @@
   lmfdudv = .TRUE.
 
-
 END SUBROUTINE flxsetup
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/cv_driver.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/cv_driver.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/cv_driver.F90	(revision 5142)
@@ -14,4 +14,6 @@
   USE lmdz_cv30, ONLY: cv30_param, cv30_prelim, cv30_feed, cv30_undilute1, cv30_trigger, cv30_compress, cv30_undilute2, &
           cv30_closure, cv30_epmax_fn_cape, cv30_mixing, cv30_unsat, cv30_yield, cv30_tracer, cv30_uncompress
+  USE lmdz_cv, ONLY: cv_param, cv_prelim, cv_feed, cv_undilute1, cv_trigger, cv_compress, &
+          cv_undilute2, cv_closure, cv_mixing, cv_unsat, cv_yield, cv_uncompress
 
   IMPLICIT NONE
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/cv_routines.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/cv_routines.F90	(revision 5141)
+++ 	(revision )
@@ -1,1698 +1,0 @@
-! $Id$
-
-SUBROUTINE cv_param(nd)
-  IMPLICIT NONE
-
-  ! ------------------------------------------------------------
-  ! Set parameters for convectL
-  ! (includes microphysical parameters and parameters that
-  ! control the rate of approach to quasi-equilibrium)
-  ! ------------------------------------------------------------
-
-  ! *** ELCRIT IS THE AUTOCONVERSION THERSHOLD WATER CONTENT (gm/gm) ***
-  ! ***  TLCRIT IS CRITICAL TEMPERATURE BELOW WHICH THE AUTO-        ***
-  ! ***       CONVERSION THRESHOLD IS ASSUMED TO BE ZERO             ***
-  ! ***     (THE AUTOCONVERSION THRESHOLD VARIES LINEARLY            ***
-  ! ***               BETWEEN 0 C AND TLCRIT)                        ***
-  ! ***   ENTP IS THE COEFFICIENT OF MIXING IN THE ENTRAINMENT       ***
-  ! ***                       FORMULATION                            ***
-  ! ***  SIGD IS THE FRACTIONAL AREA COVERED BY UNSATURATED DNDRAFT  ***
-  ! ***  SIGS IS THE FRACTION OF PRECIPITATION FALLING OUTSIDE       ***
-  ! ***                        OF CLOUD                              ***
-  ! ***        OMTRAIN IS THE ASSUMED FALL SPEED (P/s) OF RAIN       ***
-  ! ***     OMTSNOW IS THE ASSUMED FALL SPEED (P/s) OF SNOW          ***
-  ! ***  COEFFR IS A COEFFICIENT GOVERNING THE RATE OF EVAPORATION   ***
-  ! ***                          OF RAIN                             ***
-  ! ***  COEFFS IS A COEFFICIENT GOVERNING THE RATE OF EVAPORATION   ***
-  ! ***                          OF SNOW                             ***
-  ! ***     CU IS THE COEFFICIENT GOVERNING CONVECTIVE MOMENTUM      ***
-  ! ***                         TRANSPORT                            ***
-  ! ***    DTMAX IS THE MAXIMUM NEGATIVE TEMPERATURE PERTURBATION    ***
-  ! ***        A LIFTED PARCEL IS ALLOWED TO HAVE BELOW ITS LFC      ***
-  ! ***    ALPHA AND DAMP ARE PARAMETERS THAT CONTROL THE RATE OF    ***
-  ! ***                 APPROACH TO QUASI-EQUILIBRIUM                ***
-  ! ***   (THEIR STANDARD VALUES ARE  0.20 AND 0.1, RESPECTIVELY)    ***
-  ! ***                   (DAMP MUST BE LESS THAN 1)                 ***
-
-  include "cvparam.h"
-  INTEGER nd
-  CHARACTER (LEN = 20) :: modname = 'cv_routines'
-  CHARACTER (LEN = 80) :: abort_message
-
-  ! noff: integer limit for convection (nd-noff)
-  ! minorig: First level of convection
-
-  noff = 2
-  minorig = 2
-
-  nl = nd - noff
-  nlp = nl + 1
-  nlm = nl - 1
-
-  elcrit = 0.0011
-  tlcrit = -55.0
-  entp = 1.5
-  sigs = 0.12
-  sigd = 0.05
-  omtrain = 50.0
-  omtsnow = 5.5
-  coeffr = 1.0
-  coeffs = 0.8
-  dtmax = 0.9
-
-  cu = 0.70
-
-  betad = 10.0
-
-  damp = 0.1
-  alpha = 0.2
-
-  delta = 0.01 ! cld
-
-END SUBROUTINE cv_param
-
-SUBROUTINE cv_prelim(len, nd, ndp1, t, q, p, ph, lv, cpn, tv, gz, h, hm)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  ! =====================================================================
-  ! --- CALCULATE ARRAYS OF GEOPOTENTIAL, HEAT CAPACITY & STATIC ENERGY
-  ! =====================================================================
-
-  ! inputs:
-  INTEGER len, nd, ndp1
-  REAL t(len, nd), q(len, nd), p(len, nd), ph(len, ndp1)
-
-  ! outputs:
-  REAL lv(len, nd), cpn(len, nd), tv(len, nd)
-  REAL gz(len, nd), h(len, nd), hm(len, nd)
-
-  ! local variables:
-  INTEGER k, i
-  REAL cpx(len, nd)
-
-  include "cvparam.h"
-
-  DO k = 1, nlp
-    DO i = 1, len
-      lv(i, k) = lv0 - clmcpv * (t(i, k) - t0)
-      cpn(i, k) = cpd * (1.0 - q(i, k)) + cpv * q(i, k)
-      cpx(i, k) = cpd * (1.0 - q(i, k)) + cl * q(i, k)
-      tv(i, k) = t(i, k) * (1.0 + q(i, k) * epsim1)
-    END DO
-  END DO
-
-  ! gz = phi at the full levels (same as p).
-
-  DO i = 1, len
-    gz(i, 1) = 0.0
-  END DO
-  DO k = 2, nlp
-    DO i = 1, len
-      gz(i, k) = gz(i, k - 1) + hrd * (tv(i, k - 1) + tv(i, k)) * (p(i, k - 1) - p(i, k)) / ph(i, &
-              k)
-    END DO
-  END DO
-
-  ! h  = phi + cpT (dry static energy).
-  ! hm = phi + cp(T-Tbase)+Lq
-
-  DO k = 1, nlp
-    DO i = 1, len
-      h(i, k) = gz(i, k) + cpn(i, k) * t(i, k)
-      hm(i, k) = gz(i, k) + cpx(i, k) * (t(i, k) - t(i, 1)) + lv(i, k) * q(i, k)
-    END DO
-  END DO
-
-END SUBROUTINE cv_prelim
-
-SUBROUTINE cv_feed(len, nd, t, q, qs, p, hm, gz, nk, icb, icbmax, iflag, tnk, &
-        qnk, gznk, plcl)
-  IMPLICIT NONE
-
-  ! ================================================================
-  ! Purpose: CONVECTIVE FEED
-  ! ================================================================
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER len, nd
-  REAL t(len, nd), q(len, nd), qs(len, nd), p(len, nd)
-  REAL hm(len, nd), gz(len, nd)
-
-  ! outputs:
-  INTEGER iflag(len), nk(len), icb(len), icbmax
-  REAL tnk(len), qnk(len), gznk(len), plcl(len)
-
-  ! local variables:
-  INTEGER i, k
-  INTEGER ihmin(len)
-  REAL work(len)
-  REAL pnk(len), qsnk(len), rh(len), chi(len)
-
-  ! -------------------------------------------------------------------
-  ! --- Find level of minimum moist static energy
-  ! --- If level of minimum moist static energy coincides with
-  ! --- or is lower than minimum allowable parcel origin level,
-  ! --- set iflag to 6.
-  ! -------------------------------------------------------------------
-
-  DO i = 1, len
-    work(i) = 1.0E12
-    ihmin(i) = nl
-  END DO
-  DO k = 2, nlp
-    DO i = 1, len
-      IF ((hm(i, k)<work(i)) .AND. (hm(i, k)<hm(i, k - 1))) THEN
-        work(i) = hm(i, k)
-        ihmin(i) = k
-      END IF
-    END DO
-  END DO
-  DO i = 1, len
-    ihmin(i) = min(ihmin(i), nlm)
-    IF (ihmin(i)<=minorig) THEN
-      iflag(i) = 6
-    END IF
-  END DO
-
-  ! -------------------------------------------------------------------
-  ! --- Find that model level below the level of minimum moist static
-  ! --- energy that has the maximum value of moist static energy
-  ! -------------------------------------------------------------------
-
-  DO i = 1, len
-    work(i) = hm(i, minorig)
-    nk(i) = minorig
-  END DO
-  DO k = minorig + 1, nl
-    DO i = 1, len
-      IF ((hm(i, k)>work(i)) .AND. (k<=ihmin(i))) THEN
-        work(i) = hm(i, k)
-        nk(i) = k
-      END IF
-    END DO
-  END DO
-  ! -------------------------------------------------------------------
-  ! --- Check whether parcel level temperature and specific humidity
-  ! --- are reasonable
-  ! -------------------------------------------------------------------
-  DO i = 1, len
-    IF (((t(i, nk(i))<250.0) .OR. (q(i, nk(i))<=0.0) .OR. (p(i, ihmin(i))< &
-            400.0)) .AND. (iflag(i)==0)) iflag(i) = 7
-  END DO
-  ! -------------------------------------------------------------------
-  ! --- Calculate lifted condensation level of air at parcel origin level
-  ! --- (Within 0.2% of formula of Bolton, MON. WEA. REV.,1980)
-  ! -------------------------------------------------------------------
-  DO i = 1, len
-    tnk(i) = t(i, nk(i))
-    qnk(i) = q(i, nk(i))
-    gznk(i) = gz(i, nk(i))
-    pnk(i) = p(i, nk(i))
-    qsnk(i) = qs(i, nk(i))
-
-    rh(i) = qnk(i) / qsnk(i)
-    rh(i) = min(1.0, rh(i))
-    chi(i) = tnk(i) / (1669.0 - 122.0 * rh(i) - tnk(i))
-    plcl(i) = pnk(i) * (rh(i)**chi(i))
-    IF (((plcl(i)<200.0) .OR. (plcl(i)>=2000.0)) .AND. (iflag(i)==0)) iflag(i &
-            ) = 8
-  END DO
-  ! -------------------------------------------------------------------
-  ! --- Calculate first level above lcl (=icb)
-  ! -------------------------------------------------------------------
-  DO i = 1, len
-    icb(i) = nlm
-  END DO
-
-  DO k = minorig, nl
-    DO i = 1, len
-      IF ((k>=(nk(i) + 1)) .AND. (p(i, k)<plcl(i))) icb(i) = min(icb(i), k)
-    END DO
-  END DO
-
-  DO i = 1, len
-    IF ((icb(i)>=nlm) .AND. (iflag(i)==0)) iflag(i) = 9
-  END DO
-
-  ! Compute icbmax.
-
-  icbmax = 2
-  DO i = 1, len
-    icbmax = max(icbmax, icb(i))
-  END DO
-
-END SUBROUTINE cv_feed
-
-SUBROUTINE cv_undilute1(len, nd, t, q, qs, gz, p, nk, icb, icbmax, tp, tvp, &
-        clw)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER len, nd
-  INTEGER nk(len), icb(len), icbmax
-  REAL t(len, nd), q(len, nd), qs(len, nd), gz(len, nd)
-  REAL p(len, nd)
-
-  ! outputs:
-  REAL tp(len, nd), tvp(len, nd), clw(len, nd)
-
-  ! local variables:
-  INTEGER i, k
-  REAL tg, qg, alv, s, ahg, tc, denom, es, rg
-  REAL ah0(len), cpp(len)
-  REAL tnk(len), qnk(len), gznk(len), ticb(len), gzicb(len)
-
-  ! -------------------------------------------------------------------
-  ! --- Calculates the lifted parcel virtual temperature at nk,
-  ! --- the actual temperature, and the adiabatic
-  ! --- liquid water content. The procedure is to solve the equation.
-  ! cp*tp+L*qp+phi=cp*tnk+L*qnk+gznk.
-  ! -------------------------------------------------------------------
-
-  DO i = 1, len
-    tnk(i) = t(i, nk(i))
-    qnk(i) = q(i, nk(i))
-    gznk(i) = gz(i, nk(i))
-    ticb(i) = t(i, icb(i))
-    gzicb(i) = gz(i, icb(i))
-  END DO
-
-  ! ***  Calculate certain parcel quantities, including static energy   ***
-
-  DO i = 1, len
-    ah0(i) = (cpd * (1. - qnk(i)) + cl * qnk(i)) * tnk(i) + qnk(i) * (lv0 - clmcpv * (tnk(i) - &
-            273.15)) + gznk(i)
-    cpp(i) = cpd * (1. - qnk(i)) + qnk(i) * cpv
-  END DO
-
-  ! ***   Calculate lifted parcel quantities below cloud base   ***
-
-  DO k = minorig, icbmax - 1
-    DO i = 1, len
-      tp(i, k) = tnk(i) - (gz(i, k) - gznk(i)) / cpp(i)
-      tvp(i, k) = tp(i, k) * (1. + qnk(i) * epsi)
-    END DO
-  END DO
-
-  ! ***  Find lifted parcel quantities above cloud base    ***
-
-  DO i = 1, len
-    tg = ticb(i)
-    qg = qs(i, icb(i))
-    alv = lv0 - clmcpv * (ticb(i) - t0)
-
-    ! First iteration.
-
-    s = cpd + alv * alv * qg / (rrv * ticb(i) * ticb(i))
-    s = 1. / s
-    ahg = cpd * tg + (cl - cpd) * qnk(i) * ticb(i) + alv * qg + gzicb(i)
-    tg = tg + s * (ah0(i) - ahg)
-    tg = max(tg, 35.0)
-    tc = tg - t0
-    denom = 243.5 + tc
-    IF (tc>=0.0) THEN
-      es = 6.112 * exp(17.67 * tc / denom)
-    ELSE
-      es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
-    END IF
-    qg = eps * es / (p(i, icb(i)) - es * (1. - eps))
-
-    ! Second iteration.
-
-    s = cpd + alv * alv * qg / (rrv * ticb(i) * ticb(i))
-    s = 1. / s
-    ahg = cpd * tg + (cl - cpd) * qnk(i) * ticb(i) + alv * qg + gzicb(i)
-    tg = tg + s * (ah0(i) - ahg)
-    tg = max(tg, 35.0)
-    tc = tg - t0
-    denom = 243.5 + tc
-    IF (tc>=0.0) THEN
-      es = 6.112 * exp(17.67 * tc / denom)
-    ELSE
-      es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
-    END IF
-    qg = eps * es / (p(i, icb(i)) - es * (1. - eps))
-
-    alv = lv0 - clmcpv * (ticb(i) - 273.15)
-    tp(i, icb(i)) = (ah0(i) - (cl - cpd) * qnk(i) * ticb(i) - gz(i, icb(i)) - alv * qg) / cpd
-    clw(i, icb(i)) = qnk(i) - qg
-    clw(i, icb(i)) = max(0.0, clw(i, icb(i)))
-    rg = qg / (1. - qnk(i))
-    tvp(i, icb(i)) = tp(i, icb(i)) * (1. + rg * epsi)
-  END DO
-
-  DO k = minorig, icbmax
-    DO i = 1, len
-      tvp(i, k) = tvp(i, k) - tp(i, k) * qnk(i)
-    END DO
-  END DO
-
-END SUBROUTINE cv_undilute1
-
-SUBROUTINE cv_trigger(len, nd, icb, cbmf, tv, tvp, iflag)
-  IMPLICIT NONE
-
-  ! -------------------------------------------------------------------
-  ! --- Test for instability.
-  ! --- If there was no convection at last time step and parcel
-  ! --- is stable at icb, then set iflag to 4.
-  ! -------------------------------------------------------------------
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER len, nd, icb(len)
-  REAL cbmf(len), tv(len, nd), tvp(len, nd)
-
-  ! outputs:
-  INTEGER iflag(len) ! also an input
-
-  ! local variables:
-  INTEGER i
-
-  DO i = 1, len
-    IF ((cbmf(i)==0.0) .AND. (iflag(i)==0) .AND. (tvp(i, &
-            icb(i))<=(tv(i, icb(i)) - dtmax))) iflag(i) = 4
-  END DO
-
-END SUBROUTINE cv_trigger
-
-SUBROUTINE cv_compress(len, nloc, ncum, nd, iflag1, nk1, icb1, cbmf1, plcl1, &
-        tnk1, qnk1, gznk1, t1, q1, qs1, u1, v1, gz1, h1, lv1, cpn1, p1, ph1, tv1, &
-        tp1, tvp1, clw1, iflag, nk, icb, cbmf, plcl, tnk, qnk, gznk, t, q, qs, u, &
-        v, gz, h, lv, cpn, p, ph, tv, tp, tvp, clw, dph)
-  USE lmdz_print_control, ONLY: lunout
-  USE lmdz_abort_physic, ONLY: abort_physic
-  IMPLICIT NONE
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER len, ncum, nd, nloc
-  INTEGER iflag1(len), nk1(len), icb1(len)
-  REAL cbmf1(len), plcl1(len), tnk1(len), qnk1(len), gznk1(len)
-  REAL t1(len, nd), q1(len, nd), qs1(len, nd), u1(len, nd), v1(len, nd)
-  REAL gz1(len, nd), h1(len, nd), lv1(len, nd), cpn1(len, nd)
-  REAL p1(len, nd), ph1(len, nd + 1), tv1(len, nd), tp1(len, nd)
-  REAL tvp1(len, nd), clw1(len, nd)
-
-  ! outputs:
-  INTEGER iflag(nloc), nk(nloc), icb(nloc)
-  REAL cbmf(nloc), plcl(nloc), tnk(nloc), qnk(nloc), gznk(nloc)
-  REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd), u(nloc, nd), v(nloc, nd)
-  REAL gz(nloc, nd), h(nloc, nd), lv(nloc, nd), cpn(nloc, nd)
-  REAL p(nloc, nd), ph(nloc, nd + 1), tv(nloc, nd), tp(nloc, nd)
-  REAL tvp(nloc, nd), clw(nloc, nd)
-  REAL dph(nloc, nd)
-
-  ! local variables:
-  INTEGER i, k, nn
-  CHARACTER (LEN = 20) :: modname = 'cv_compress'
-  CHARACTER (LEN = 80) :: abort_message
-
-  DO k = 1, nl + 1
-    nn = 0
-    DO i = 1, len
-      IF (iflag1(i)==0) THEN
-        nn = nn + 1
-        t(nn, k) = t1(i, k)
-        q(nn, k) = q1(i, k)
-        qs(nn, k) = qs1(i, k)
-        u(nn, k) = u1(i, k)
-        v(nn, k) = v1(i, k)
-        gz(nn, k) = gz1(i, k)
-        h(nn, k) = h1(i, k)
-        lv(nn, k) = lv1(i, k)
-        cpn(nn, k) = cpn1(i, k)
-        p(nn, k) = p1(i, k)
-        ph(nn, k) = ph1(i, k)
-        tv(nn, k) = tv1(i, k)
-        tp(nn, k) = tp1(i, k)
-        tvp(nn, k) = tvp1(i, k)
-        clw(nn, k) = clw1(i, k)
-      END IF
-    END DO
-  END DO
-
-  IF (nn/=ncum) THEN
-    WRITE (lunout, *) 'strange! nn not equal to ncum: ', nn, ncum
-    abort_message = ''
-    CALL abort_physic(modname, abort_message, 1)
-  END IF
-
-  nn = 0
-  DO i = 1, len
-    IF (iflag1(i)==0) THEN
-      nn = nn + 1
-      cbmf(nn) = cbmf1(i)
-      plcl(nn) = plcl1(i)
-      tnk(nn) = tnk1(i)
-      qnk(nn) = qnk1(i)
-      gznk(nn) = gznk1(i)
-      nk(nn) = nk1(i)
-      icb(nn) = icb1(i)
-      iflag(nn) = iflag1(i)
-    END IF
-  END DO
-
-  DO k = 1, nl
-    DO i = 1, ncum
-      dph(i, k) = ph(i, k) - ph(i, k + 1)
-    END DO
-  END DO
-
-END SUBROUTINE cv_compress
-
-SUBROUTINE cv_undilute2(nloc, ncum, nd, icb, nk, tnk, qnk, gznk, t, q, qs, &
-        gz, p, dph, h, tv, lv, inb, inb1, tp, tvp, clw, hp, ep, sigp, frac)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  ! ---------------------------------------------------------------------
-  ! Purpose:
-  ! FIND THE REST OF THE LIFTED PARCEL TEMPERATURES
-  ! &
-  ! COMPUTE THE PRECIPITATION EFFICIENCIES AND THE
-  ! FRACTION OF PRECIPITATION FALLING OUTSIDE OF CLOUD
-  ! &
-  ! FIND THE LEVEL OF NEUTRAL BUOYANCY
-  ! ---------------------------------------------------------------------
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER ncum, nd, nloc
-  INTEGER icb(nloc), nk(nloc)
-  REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd), gz(nloc, nd)
-  REAL p(nloc, nd), dph(nloc, nd)
-  REAL tnk(nloc), qnk(nloc), gznk(nloc)
-  REAL lv(nloc, nd), tv(nloc, nd), h(nloc, nd)
-
-  ! outputs:
-  INTEGER inb(nloc), inb1(nloc)
-  REAL tp(nloc, nd), tvp(nloc, nd), clw(nloc, nd)
-  REAL ep(nloc, nd), sigp(nloc, nd), hp(nloc, nd)
-  REAL frac(nloc)
-
-  ! local variables:
-  INTEGER i, k
-  REAL tg, qg, ahg, alv, s, tc, es, denom, rg, tca, elacrit
-  REAL by, defrac
-  REAL ah0(nloc), cape(nloc), capem(nloc), byp(nloc)
-  LOGICAL lcape(nloc)
-
-  ! =====================================================================
-  ! --- SOME INITIALIZATIONS
-  ! =====================================================================
-
-  DO k = 1, nl
-    DO i = 1, ncum
-      ep(i, k) = 0.0
-      sigp(i, k) = sigs
-    END DO
-  END DO
-
-  ! =====================================================================
-  ! --- FIND THE REST OF THE LIFTED PARCEL TEMPERATURES
-  ! =====================================================================
-
-  ! ---       The procedure is to solve the equation.
-  ! cp*tp+L*qp+phi=cp*tnk+L*qnk+gznk.
-
-  ! ***  Calculate certain parcel quantities, including static energy   ***
-
-  DO i = 1, ncum
-    ah0(i) = (cpd * (1. - qnk(i)) + cl * qnk(i)) * tnk(i) + qnk(i) * (lv0 - clmcpv * (tnk(i) - &
-            t0)) + gznk(i)
-  END DO
-
-
-  ! ***  Find lifted parcel quantities above cloud base    ***
-
-  DO k = minorig + 1, nl
-    DO i = 1, ncum
-      IF (k>=(icb(i) + 1)) THEN
-        tg = t(i, k)
-        qg = qs(i, k)
-        alv = lv0 - clmcpv * (t(i, k) - t0)
-
-        ! First iteration.
-
-        s = cpd + alv * alv * qg / (rrv * t(i, k) * t(i, k))
-        s = 1. / s
-        ahg = cpd * tg + (cl - cpd) * qnk(i) * t(i, k) + alv * qg + gz(i, k)
-        tg = tg + s * (ah0(i) - ahg)
-        tg = max(tg, 35.0)
-        tc = tg - t0
-        denom = 243.5 + tc
-        IF (tc>=0.0) THEN
-          es = 6.112 * exp(17.67 * tc / denom)
-        ELSE
-          es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
-        END IF
-        qg = eps * es / (p(i, k) - es * (1. - eps))
-
-        ! Second iteration.
-
-        s = cpd + alv * alv * qg / (rrv * t(i, k) * t(i, k))
-        s = 1. / s
-        ahg = cpd * tg + (cl - cpd) * qnk(i) * t(i, k) + alv * qg + gz(i, k)
-        tg = tg + s * (ah0(i) - ahg)
-        tg = max(tg, 35.0)
-        tc = tg - t0
-        denom = 243.5 + tc
-        IF (tc>=0.0) THEN
-          es = 6.112 * exp(17.67 * tc / denom)
-        ELSE
-          es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
-        END IF
-        qg = eps * es / (p(i, k) - es * (1. - eps))
-
-        alv = lv0 - clmcpv * (t(i, k) - t0)
-        ! PRINT*,'cpd dans convect2 ',cpd
-        ! PRINT*,'tp(i,k),ah0(i),cl,cpd,qnk(i),t(i,k),gz(i,k),alv,qg,cpd'
-        ! PRINT*,tp(i,k),ah0(i),cl,cpd,qnk(i),t(i,k),gz(i,k),alv,qg,cpd
-        tp(i, k) = (ah0(i) - (cl - cpd) * qnk(i) * t(i, k) - gz(i, k) - alv * qg) / cpd
-        ! if (.NOT.cpd.gt.1000.) THEN
-        ! PRINT*,'CPD=',cpd
-        ! stop
-        ! END IF
-        clw(i, k) = qnk(i) - qg
-        clw(i, k) = max(0.0, clw(i, k))
-        rg = qg / (1. - qnk(i))
-        tvp(i, k) = tp(i, k) * (1. + rg * epsi)
-      END IF
-    END DO
-  END DO
-
-  ! =====================================================================
-  ! --- SET THE PRECIPITATION EFFICIENCIES AND THE FRACTION OF
-  ! --- PRECIPITATION FALLING OUTSIDE OF CLOUD
-  ! --- THESE MAY BE FUNCTIONS OF TP(I), P(I) AND CLW(I)
-  ! =====================================================================
-
-  DO k = minorig + 1, nl
-    DO i = 1, ncum
-      IF (k>=(nk(i) + 1)) THEN
-        tca = tp(i, k) - t0
-        IF (tca>=0.0) THEN
-          elacrit = elcrit
-        ELSE
-          elacrit = elcrit * (1.0 - tca / tlcrit)
-        END IF
-        elacrit = max(elacrit, 0.0)
-        ep(i, k) = 1.0 - elacrit / max(clw(i, k), 1.0E-8)
-        ep(i, k) = max(ep(i, k), 0.0)
-        ep(i, k) = min(ep(i, k), 1.0)
-        sigp(i, k) = sigs
-      END IF
-    END DO
-  END DO
-
-  ! =====================================================================
-  ! --- CALCULATE VIRTUAL TEMPERATURE AND LIFTED PARCEL
-  ! --- VIRTUAL TEMPERATURE
-  ! =====================================================================
-
-  DO k = minorig + 1, nl
-    DO i = 1, ncum
-      IF (k>=(icb(i) + 1)) THEN
-        tvp(i, k) = tvp(i, k) * (1.0 - qnk(i) + ep(i, k) * clw(i, k))
-        ! PRINT*,'i,k,tvp(i,k),qnk(i),ep(i,k),clw(i,k)'
-        ! PRINT*, i,k,tvp(i,k),qnk(i),ep(i,k),clw(i,k)
-      END IF
-    END DO
-  END DO
-  DO i = 1, ncum
-    tvp(i, nlp) = tvp(i, nl) - (gz(i, nlp) - gz(i, nl)) / cpd
-  END DO
-
-  ! =====================================================================
-  ! --- FIND THE FIRST MODEL LEVEL (INB1) ABOVE THE PARCEL'S
-  ! --- HIGHEST LEVEL OF NEUTRAL BUOYANCY
-  ! --- AND THE HIGHEST LEVEL OF POSITIVE CAPE (INB)
-  ! =====================================================================
-
-  DO i = 1, ncum
-    cape(i) = 0.0
-    capem(i) = 0.0
-    inb(i) = icb(i) + 1
-    inb1(i) = inb(i)
-  END DO
-
-  ! Originial Code
-
-  ! do 530 k=minorig+1,nl-1
-  ! do 520 i=1,ncum
-  ! IF(k.ge.(icb(i)+1))THEN
-  ! by=(tvp(i,k)-tv(i,k))*dph(i,k)/p(i,k)
-  ! byp=(tvp(i,k+1)-tv(i,k+1))*dph(i,k+1)/p(i,k+1)
-  ! cape(i)=cape(i)+by
-  ! IF(by.ge.0.0)inb1(i)=k+1
-  ! IF(cape(i).gt.0.0)THEN
-  ! inb(i)=k+1
-  ! capem(i)=cape(i)
-  ! END IF
-  ! END IF
-  ! 520    continue
-  ! 530  continue
-  ! do 540 i=1,ncum
-  ! byp=(tvp(i,nl)-tv(i,nl))*dph(i,nl)/p(i,nl)
-  ! cape(i)=capem(i)+byp
-  ! defrac=capem(i)-cape(i)
-  ! defrac=max(defrac,0.001)
-  ! frac(i)=-cape(i)/defrac
-  ! frac(i)=min(frac(i),1.0)
-  ! frac(i)=max(frac(i),0.0)
-  ! 540   continue
-
-  ! K Emanuel fix
-
-  ! CALL zilch(byp,ncum)
-  ! do 530 k=minorig+1,nl-1
-  ! do 520 i=1,ncum
-  ! IF(k.ge.(icb(i)+1))THEN
-  ! by=(tvp(i,k)-tv(i,k))*dph(i,k)/p(i,k)
-  ! cape(i)=cape(i)+by
-  ! IF(by.ge.0.0)inb1(i)=k+1
-  ! IF(cape(i).gt.0.0)THEN
-  ! inb(i)=k+1
-  ! capem(i)=cape(i)
-  ! byp(i)=(tvp(i,k+1)-tv(i,k+1))*dph(i,k+1)/p(i,k+1)
-  ! END IF
-  ! END IF
-  ! 520    continue
-  ! 530  continue
-  ! do 540 i=1,ncum
-  ! inb(i)=max(inb(i),inb1(i))
-  ! cape(i)=capem(i)+byp(i)
-  ! defrac=capem(i)-cape(i)
-  ! defrac=max(defrac,0.001)
-  ! frac(i)=-cape(i)/defrac
-  ! frac(i)=min(frac(i),1.0)
-  ! frac(i)=max(frac(i),0.0)
-  ! 540   continue
-
-  ! J Teixeira fix
-
-  CALL zilch(byp, ncum)
-  DO i = 1, ncum
-    lcape(i) = .TRUE.
-  END DO
-  DO k = minorig + 1, nl - 1
-    DO i = 1, ncum
-      IF (cape(i)<0.0) lcape(i) = .FALSE.
-      IF ((k>=(icb(i) + 1)) .AND. lcape(i)) THEN
-        by = (tvp(i, k) - tv(i, k)) * dph(i, k) / p(i, k)
-        byp(i) = (tvp(i, k + 1) - tv(i, k + 1)) * dph(i, k + 1) / p(i, k + 1)
-        cape(i) = cape(i) + by
-        IF (by>=0.0) inb1(i) = k + 1
-        IF (cape(i)>0.0) THEN
-          inb(i) = k + 1
-          capem(i) = cape(i)
-        END IF
-      END IF
-    END DO
-  END DO
-  DO i = 1, ncum
-    cape(i) = capem(i) + byp(i)
-    defrac = capem(i) - cape(i)
-    defrac = max(defrac, 0.001)
-    frac(i) = -cape(i) / defrac
-    frac(i) = min(frac(i), 1.0)
-    frac(i) = max(frac(i), 0.0)
-  END DO
-
-  ! =====================================================================
-  ! ---   CALCULATE LIQUID WATER STATIC ENERGY OF LIFTED PARCEL
-  ! =====================================================================
-
-  ! initialization:
-  DO i = 1, ncum * nlp
-    hp(i, 1) = h(i, 1)
-  END DO
-
-  DO k = minorig + 1, nl
-    DO i = 1, ncum
-      IF ((k>=icb(i)) .AND. (k<=inb(i))) THEN
-        hp(i, k) = h(i, nk(i)) + (lv(i, k) + (cpd - cpv) * t(i, k)) * ep(i, k) * clw(i, k &
-                )
-      END IF
-    END DO
-  END DO
-
-END SUBROUTINE cv_undilute2
-
-SUBROUTINE cv_closure(nloc, ncum, nd, nk, icb, tv, tvp, p, ph, dph, plcl, &
-        cpn, iflag, cbmf)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  ! inputs:
-  INTEGER ncum, nd, nloc
-  INTEGER nk(nloc), icb(nloc)
-  REAL tv(nloc, nd), tvp(nloc, nd), p(nloc, nd), dph(nloc, nd)
-  REAL ph(nloc, nd + 1) ! caution nd instead ndp1 to be consistent...
-  REAL plcl(nloc), cpn(nloc, nd)
-
-  ! outputs:
-  INTEGER iflag(nloc)
-  REAL cbmf(nloc) ! also an input
-
-  ! local variables:
-  INTEGER i, k, icbmax
-  REAL dtpbl(nloc), dtmin(nloc), tvpplcl(nloc), tvaplcl(nloc)
-  REAL work(nloc)
-
-  include "cvparam.h"
-
-  ! -------------------------------------------------------------------
-  ! Compute icbmax.
-  ! -------------------------------------------------------------------
-
-  icbmax = 2
-  DO i = 1, ncum
-    icbmax = max(icbmax, icb(i))
-  END DO
-
-  ! =====================================================================
-  ! ---  CALCULATE CLOUD BASE MASS FLUX
-  ! =====================================================================
-
-  ! tvpplcl = parcel temperature lifted adiabatically from level
-  ! icb-1 to the LCL.
-  ! tvaplcl = virtual temperature at the LCL.
-
-  DO i = 1, ncum
-    dtpbl(i) = 0.0
-    tvpplcl(i) = tvp(i, icb(i) - 1) - rrd * tvp(i, icb(i) - 1) * (p(i, icb(i) - 1) - plcl(&
-            i)) / (cpn(i, icb(i) - 1) * p(i, icb(i) - 1))
-    tvaplcl(i) = tv(i, icb(i)) + (tvp(i, icb(i)) - tvp(i, icb(i) + 1)) * (plcl(i) - p(i &
-            , icb(i))) / (p(i, icb(i)) - p(i, icb(i) + 1))
-  END DO
-
-  ! -------------------------------------------------------------------
-  ! --- Interpolate difference between lifted parcel and
-  ! --- environmental temperatures to lifted condensation level
-  ! -------------------------------------------------------------------
-
-  ! dtpbl = average of tvp-tv in the PBL (k=nk to icb-1).
-
-  DO k = minorig, icbmax
-    DO i = 1, ncum
-      IF ((k>=nk(i)) .AND. (k<=(icb(i) - 1))) THEN
-        dtpbl(i) = dtpbl(i) + (tvp(i, k) - tv(i, k)) * dph(i, k)
-      END IF
-    END DO
-  END DO
-  DO i = 1, ncum
-    dtpbl(i) = dtpbl(i) / (ph(i, nk(i)) - ph(i, icb(i)))
-    dtmin(i) = tvpplcl(i) - tvaplcl(i) + dtmax + dtpbl(i)
-  END DO
-
-  ! -------------------------------------------------------------------
-  ! --- Adjust cloud base mass flux
-  ! -------------------------------------------------------------------
-
-  DO i = 1, ncum
-    work(i) = cbmf(i)
-    cbmf(i) = max(0.0, (1.0 - damp) * cbmf(i) + 0.1 * alpha * dtmin(i))
-    IF ((work(i)==0.0) .AND. (cbmf(i)==0.0)) THEN
-      iflag(i) = 3
-    END IF
-  END DO
-
-END SUBROUTINE cv_closure
-
-SUBROUTINE cv_mixing(nloc, ncum, nd, icb, nk, inb, inb1, ph, t, q, qs, u, v, &
-        h, lv, qnk, hp, tv, tvp, ep, clw, cbmf, m, ment, qent, uent, vent, nent, &
-        sij, elij)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER ncum, nd, nloc
-  INTEGER icb(nloc), inb(nloc), inb1(nloc), nk(nloc)
-  REAL cbmf(nloc), qnk(nloc)
-  REAL ph(nloc, nd + 1)
-  REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd), lv(nloc, nd)
-  REAL u(nloc, nd), v(nloc, nd), h(nloc, nd), hp(nloc, nd)
-  REAL tv(nloc, nd), tvp(nloc, nd), ep(nloc, nd), clw(nloc, nd)
-
-  ! outputs:
-  INTEGER nent(nloc, nd)
-  REAL m(nloc, nd), ment(nloc, nd, nd), qent(nloc, nd, nd)
-  REAL uent(nloc, nd, nd), vent(nloc, nd, nd)
-  REAL sij(nloc, nd, nd), elij(nloc, nd, nd)
-
-  ! local variables:
-  INTEGER i, j, k, ij
-  INTEGER num1, num2
-  REAL dbo, qti, bf2, anum, denom, dei, altem, cwat, stemp
-  REAL alt, qp1, smid, sjmin, sjmax, delp, delm
-  REAL work(nloc), asij(nloc), smin(nloc), scrit(nloc)
-  REAL bsum(nloc, nd)
-  LOGICAL lwork(nloc)
-
-  ! =====================================================================
-  ! --- INITIALIZE VARIOUS ARRAYS USED IN THE COMPUTATIONS
-  ! =====================================================================
-
-  DO i = 1, ncum * nlp
-    nent(i, 1) = 0
-    m(i, 1) = 0.0
-  END DO
-
-  DO k = 1, nlp
-    DO j = 1, nlp
-      DO i = 1, ncum
-        qent(i, k, j) = q(i, j)
-        uent(i, k, j) = u(i, j)
-        vent(i, k, j) = v(i, j)
-        elij(i, k, j) = 0.0
-        ment(i, k, j) = 0.0
-        sij(i, k, j) = 0.0
-      END DO
-    END DO
-  END DO
-
-  ! -------------------------------------------------------------------
-  ! --- Calculate rates of mixing,  m(i)
-  ! -------------------------------------------------------------------
-
-  CALL zilch(work, ncum)
-
-  DO j = minorig + 1, nl
-    DO i = 1, ncum
-      IF ((j>=(icb(i) + 1)) .AND. (j<=inb(i))) THEN
-        k = min(j, inb1(i))
-        dbo = abs(tv(i, k + 1) - tvp(i, k + 1) - tv(i, k - 1) + tvp(i, k - 1)) + &
-                entp * 0.04 * (ph(i, k) - ph(i, k + 1))
-        work(i) = work(i) + dbo
-        m(i, j) = cbmf(i) * dbo
-      END IF
-    END DO
-  END DO
-  DO k = minorig + 1, nl
-    DO i = 1, ncum
-      IF ((k>=(icb(i) + 1)) .AND. (k<=inb(i))) THEN
-        m(i, k) = m(i, k) / work(i)
-      END IF
-    END DO
-  END DO
-
-
-  ! =====================================================================
-  ! --- CALCULATE ENTRAINED AIR MASS FLUX (ment), TOTAL WATER MIXING
-  ! --- RATIO (QENT), TOTAL CONDENSED WATER (elij), AND MIXING
-  ! --- FRACTION (sij)
-  ! =====================================================================
-
-  DO i = minorig + 1, nl
-    DO j = minorig + 1, nl
-      DO ij = 1, ncum
-        IF ((i>=(icb(ij) + 1)) .AND. (j>=icb(ij)) .AND. (i<=inb(ij)) .AND. (j<= &
-                inb(ij))) THEN
-          qti = qnk(ij) - ep(ij, i) * clw(ij, i)
-          bf2 = 1. + lv(ij, j) * lv(ij, j) * qs(ij, j) / (rrv * t(ij, j) * t(ij, j) * cpd)
-          anum = h(ij, j) - hp(ij, i) + (cpv - cpd) * t(ij, j) * (qti - q(ij, j))
-          denom = h(ij, i) - hp(ij, i) + (cpd - cpv) * (q(ij, i) - qti) * t(ij, j)
-          dei = denom
-          IF (abs(dei)<0.01) dei = 0.01
-          sij(ij, i, j) = anum / dei
-          sij(ij, i, i) = 1.0
-          altem = sij(ij, i, j) * q(ij, i) + (1. - sij(ij, i, j)) * qti - qs(ij, j)
-          altem = altem / bf2
-          cwat = clw(ij, j) * (1. - ep(ij, j))
-          stemp = sij(ij, i, j)
-          IF ((stemp<0.0 .OR. stemp>1.0 .OR. altem>cwat) .AND. j>i) THEN
-            anum = anum - lv(ij, j) * (qti - qs(ij, j) - cwat * bf2)
-            denom = denom + lv(ij, j) * (q(ij, i) - qti)
-            IF (abs(denom)<0.01) denom = 0.01
-            sij(ij, i, j) = anum / denom
-            altem = sij(ij, i, j) * q(ij, i) + (1. - sij(ij, i, j)) * qti - qs(ij, j)
-            altem = altem - (bf2 - 1.) * cwat
-          END IF
-          IF (sij(ij, i, j)>0.0 .AND. sij(ij, i, j)<0.9) THEN
-            qent(ij, i, j) = sij(ij, i, j) * q(ij, i) + (1. - sij(ij, i, j)) * qti
-            uent(ij, i, j) = sij(ij, i, j) * u(ij, i) + &
-                    (1. - sij(ij, i, j)) * u(ij, nk(ij))
-            vent(ij, i, j) = sij(ij, i, j) * v(ij, i) + &
-                    (1. - sij(ij, i, j)) * v(ij, nk(ij))
-            elij(ij, i, j) = altem
-            elij(ij, i, j) = max(0.0, elij(ij, i, j))
-            ment(ij, i, j) = m(ij, i) / (1. - sij(ij, i, j))
-            nent(ij, i) = nent(ij, i) + 1
-          END IF
-          sij(ij, i, j) = max(0.0, sij(ij, i, j))
-          sij(ij, i, j) = min(1.0, sij(ij, i, j))
-        END IF
-      END DO
-    END DO
-
-    ! ***   If no air can entrain at level i assume that updraft detrains
-    ! ***
-    ! ***   at that level and calculate detrained air flux and properties
-    ! ***
-
-    DO ij = 1, ncum
-      IF ((i>=(icb(ij) + 1)) .AND. (i<=inb(ij)) .AND. (nent(ij, i)==0)) THEN
-        ment(ij, i, i) = m(ij, i)
-        qent(ij, i, i) = q(ij, nk(ij)) - ep(ij, i) * clw(ij, i)
-        uent(ij, i, i) = u(ij, nk(ij))
-        vent(ij, i, i) = v(ij, nk(ij))
-        elij(ij, i, i) = clw(ij, i)
-        sij(ij, i, i) = 1.0
-      END IF
-    END DO
-  END DO
-
-  DO i = 1, ncum
-    sij(i, inb(i), inb(i)) = 1.0
-  END DO
-
-  ! =====================================================================
-  ! ---  NORMALIZE ENTRAINED AIR MASS FLUXES
-  ! ---  TO REPRESENT EQUAL PROBABILITIES OF MIXING
-  ! =====================================================================
-
-  CALL zilch(bsum, ncum * nlp)
-  DO ij = 1, ncum
-    lwork(ij) = .FALSE.
-  END DO
-  DO i = minorig + 1, nl
-
-    num1 = 0
-    DO ij = 1, ncum
-      IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij))) num1 = num1 + 1
-    END DO
-    IF (num1<=0) GO TO 789
-
-    DO ij = 1, ncum
-      IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij))) THEN
-        lwork(ij) = (nent(ij, i)/=0)
-        qp1 = q(ij, nk(ij)) - ep(ij, i) * clw(ij, i)
-        anum = h(ij, i) - hp(ij, i) - lv(ij, i) * (qp1 - qs(ij, i))
-        denom = h(ij, i) - hp(ij, i) + lv(ij, i) * (q(ij, i) - qp1)
-        IF (abs(denom)<0.01) denom = 0.01
-        scrit(ij) = anum / denom
-        alt = qp1 - qs(ij, i) + scrit(ij) * (q(ij, i) - qp1)
-        IF (scrit(ij)<0.0 .OR. alt<0.0) scrit(ij) = 1.0
-        asij(ij) = 0.0
-        smin(ij) = 1.0
-      END IF
-    END DO
-    DO j = minorig, nl
-
-      num2 = 0
-      DO ij = 1, ncum
-        IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (j>=icb(&
-                ij)) .AND. (j<=inb(ij)) .AND. lwork(ij)) num2 = num2 + 1
-      END DO
-      IF (num2<=0) GO TO 783
-
-      DO ij = 1, ncum
-        IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (j>=icb(&
-                ij)) .AND. (j<=inb(ij)) .AND. lwork(ij)) THEN
-          IF (sij(ij, i, j)>0.0 .AND. sij(ij, i, j)<0.9) THEN
-            IF (j>i) THEN
-              smid = min(sij(ij, i, j), scrit(ij))
-              sjmax = smid
-              sjmin = smid
-              IF (smid<smin(ij) .AND. sij(ij, i, j + 1)<smid) THEN
-                smin(ij) = smid
-                sjmax = min(sij(ij, i, j + 1), sij(ij, i, j), scrit(ij))
-                sjmin = max(sij(ij, i, j - 1), sij(ij, i, j))
-                sjmin = min(sjmin, scrit(ij))
-              END IF
-            ELSE
-              sjmax = max(sij(ij, i, j + 1), scrit(ij))
-              smid = max(sij(ij, i, j), scrit(ij))
-              sjmin = 0.0
-              IF (j>1) sjmin = sij(ij, i, j - 1)
-              sjmin = max(sjmin, scrit(ij))
-            END IF
-            delp = abs(sjmax - smid)
-            delm = abs(sjmin - smid)
-            asij(ij) = asij(ij) + (delp + delm) * (ph(ij, j) - ph(ij, j + 1))
-            ment(ij, i, j) = ment(ij, i, j) * (delp + delm) * (ph(ij, j) - ph(ij, j + 1))
-          END IF
-        END IF
-      END DO
-    783 END DO
-    DO ij = 1, ncum
-      IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. lwork(ij)) THEN
-        asij(ij) = max(1.0E-21, asij(ij))
-        asij(ij) = 1.0 / asij(ij)
-        bsum(ij, i) = 0.0
-      END IF
-    END DO
-    DO j = minorig, nl + 1
-      DO ij = 1, ncum
-        IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (j>=icb(&
-                ij)) .AND. (j<=inb(ij)) .AND. lwork(ij)) THEN
-          ment(ij, i, j) = ment(ij, i, j) * asij(ij)
-          bsum(ij, i) = bsum(ij, i) + ment(ij, i, j)
-        END IF
-      END DO
-    END DO
-    DO ij = 1, ncum
-      IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (bsum(ij, &
-              i)<1.0E-18) .AND. lwork(ij)) THEN
-        nent(ij, i) = 0
-        ment(ij, i, i) = m(ij, i)
-        qent(ij, i, i) = q(ij, nk(ij)) - ep(ij, i) * clw(ij, i)
-        uent(ij, i, i) = u(ij, nk(ij))
-        vent(ij, i, i) = v(ij, nk(ij))
-        elij(ij, i, i) = clw(ij, i)
-        sij(ij, i, i) = 1.0
-      END IF
-    END DO
-  789 END DO
-
-END SUBROUTINE cv_mixing
-
-SUBROUTINE cv_unsat(nloc, ncum, nd, inb, t, q, qs, gz, u, v, p, ph, h, lv, &
-        ep, sigp, clw, m, ment, elij, iflag, mp, qp, up, vp, wt, water, evap)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER ncum, nd, nloc
-  INTEGER inb(nloc)
-  REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd)
-  REAL gz(nloc, nd), u(nloc, nd), v(nloc, nd)
-  REAL p(nloc, nd), ph(nloc, nd + 1), h(nloc, nd)
-  REAL lv(nloc, nd), ep(nloc, nd), sigp(nloc, nd), clw(nloc, nd)
-  REAL m(nloc, nd), ment(nloc, nd, nd), elij(nloc, nd, nd)
-
-  ! outputs:
-  INTEGER iflag(nloc) ! also an input
-  REAL mp(nloc, nd), qp(nloc, nd), up(nloc, nd), vp(nloc, nd)
-  REAL water(nloc, nd), evap(nloc, nd), wt(nloc, nd)
-
-  ! local variables:
-  INTEGER i, j, k, ij, num1
-  INTEGER jtt(nloc)
-  REAL awat, coeff, qsm, afac, sigt, b6, c6, revap
-  REAL dhdp, fac, qstm, rat
-  REAL wdtrain(nloc)
-  LOGICAL lwork(nloc)
-
-  ! =====================================================================
-  ! --- PRECIPITATING DOWNDRAFT CALCULATION
-  ! =====================================================================
-
-  ! Initializations:
-
-  DO i = 1, ncum
-    DO k = 1, nl + 1
-      wt(i, k) = omtsnow
-      mp(i, k) = 0.0
-      evap(i, k) = 0.0
-      water(i, k) = 0.0
-    END DO
-  END DO
-
-  DO i = 1, ncum
-    qp(i, 1) = q(i, 1)
-    up(i, 1) = u(i, 1)
-    vp(i, 1) = v(i, 1)
-  END DO
-
-  DO k = 2, nl + 1
-    DO i = 1, ncum
-      qp(i, k) = q(i, k - 1)
-      up(i, k) = u(i, k - 1)
-      vp(i, k) = v(i, k - 1)
-    END DO
-  END DO
-
-
-  ! ***  Check whether ep(inb)=0, if so, skip precipitating    ***
-  ! ***             downdraft calculation                      ***
-
-
-  ! ***  Integrate liquid water equation to find condensed water   ***
-  ! ***                and condensed water flux                    ***
-
-  DO i = 1, ncum
-    jtt(i) = 2
-    IF (ep(i, inb(i))<=0.0001) iflag(i) = 2
-    IF (iflag(i)==0) THEN
-      lwork(i) = .TRUE.
-    ELSE
-      lwork(i) = .FALSE.
-    END IF
-  END DO
-
-  ! ***                    Begin downdraft loop                    ***
-
-  CALL zilch(wdtrain, ncum)
-  DO i = nl + 1, 1, -1
-
-    num1 = 0
-    DO ij = 1, ncum
-      IF ((i<=inb(ij)) .AND. lwork(ij)) num1 = num1 + 1
-    END DO
-    IF (num1<=0) GO TO 899
-
-
-    ! ***        Calculate detrained precipitation             ***
-
-    DO ij = 1, ncum
-      IF ((i<=inb(ij)) .AND. (lwork(ij))) THEN
-        wdtrain(ij) = g * ep(ij, i) * m(ij, i) * clw(ij, i)
-      END IF
-    END DO
-
-    IF (i>1) THEN
-      DO j = 1, i - 1
-        DO ij = 1, ncum
-          IF ((i<=inb(ij)) .AND. (lwork(ij))) THEN
-            awat = elij(ij, j, i) - (1. - ep(ij, i)) * clw(ij, i)
-            awat = max(0.0, awat)
-            wdtrain(ij) = wdtrain(ij) + g * awat * ment(ij, j, i)
-          END IF
-        END DO
-      END DO
-    END IF
-
-    ! ***    Find rain water and evaporation using provisional   ***
-    ! ***              estimates of qp(i)and qp(i-1)             ***
-
-
-    ! ***  Value of terminal velocity and coeffecient of evaporation for snow
-    ! ***
-
-    DO ij = 1, ncum
-      IF ((i<=inb(ij)) .AND. (lwork(ij))) THEN
-        coeff = coeffs
-        wt(ij, i) = omtsnow
-
-        ! ***  Value of terminal velocity and coeffecient of evaporation for
-        ! rain   ***
-
-        IF (t(ij, i)>273.0) THEN
-          coeff = coeffr
-          wt(ij, i) = omtrain
-        END IF
-        qsm = 0.5 * (q(ij, i) + qp(ij, i + 1))
-        afac = coeff * ph(ij, i) * (qs(ij, i) - qsm) / (1.0E4 + 2.0E3 * ph(ij, i) * qs(ij, i))
-        afac = max(afac, 0.0)
-        sigt = sigp(ij, i)
-        sigt = max(0.0, sigt)
-        sigt = min(1.0, sigt)
-        b6 = 100. * (ph(ij, i) - ph(ij, i + 1)) * sigt * afac / wt(ij, i)
-        c6 = (water(ij, i + 1) * wt(ij, i + 1) + wdtrain(ij) / sigd) / wt(ij, i)
-        revap = 0.5 * (-b6 + sqrt(b6 * b6 + 4. * c6))
-        evap(ij, i) = sigt * afac * revap
-        water(ij, i) = revap * revap
-
-        ! ***  Calculate precipitating downdraft mass flux under     ***
-        ! ***              hydrostatic approximation                 ***
-
-        IF (i>1) THEN
-          dhdp = (h(ij, i) - h(ij, i - 1)) / (p(ij, i - 1) - p(ij, i))
-          dhdp = max(dhdp, 10.0)
-          mp(ij, i) = 100. * ginv * lv(ij, i) * sigd * evap(ij, i) / dhdp
-          mp(ij, i) = max(mp(ij, i), 0.0)
-
-          ! ***   Add small amount of inertia to downdraft              ***
-
-          fac = 20.0 / (ph(ij, i - 1) - ph(ij, i))
-          mp(ij, i) = (fac * mp(ij, i + 1) + mp(ij, i)) / (1. + fac)
-
-          ! ***      Force mp to decrease linearly to zero
-          ! ***
-          ! ***      between about 950 mb and the surface
-          ! ***
-
-          IF (p(ij, i)>(0.949 * p(ij, 1))) THEN
-            jtt(ij) = max(jtt(ij), i)
-            mp(ij, i) = mp(ij, jtt(ij)) * (p(ij, 1) - p(ij, i)) / &
-                    (p(ij, 1) - p(ij, jtt(ij)))
-          END IF
-        END IF
-
-        ! ***       Find mixing ratio of precipitating downdraft     ***
-
-        IF (i/=inb(ij)) THEN
-          IF (i==1) THEN
-            qstm = qs(ij, 1)
-          ELSE
-            qstm = qs(ij, i - 1)
-          END IF
-          IF (mp(ij, i)>mp(ij, i + 1)) THEN
-            rat = mp(ij, i + 1) / mp(ij, i)
-            qp(ij, i) = qp(ij, i + 1) * rat + q(ij, i) * (1.0 - rat) + &
-                    100. * ginv * sigd * (ph(ij, i) - ph(ij, i + 1)) * (evap(ij, i) / mp(ij, i))
-            up(ij, i) = up(ij, i + 1) * rat + u(ij, i) * (1. - rat)
-            vp(ij, i) = vp(ij, i + 1) * rat + v(ij, i) * (1. - rat)
-          ELSE
-            IF (mp(ij, i + 1)>0.0) THEN
-              qp(ij, i) = (gz(ij, i + 1) - gz(ij, i) + qp(ij, i + 1) * (lv(ij, i + 1) + t(ij, &
-                      i + 1) * (cl - cpd)) + cpd * (t(ij, i + 1) - t(ij, &
-                      i))) / (lv(ij, i) + t(ij, i) * (cl - cpd))
-              up(ij, i) = up(ij, i + 1)
-              vp(ij, i) = vp(ij, i + 1)
-            END IF
-          END IF
-          qp(ij, i) = min(qp(ij, i), qstm)
-          qp(ij, i) = max(qp(ij, i), 0.0)
-        END IF
-      END IF
-    END DO
-  899 END DO
-
-END SUBROUTINE cv_unsat
-
-SUBROUTINE cv_yield(nloc, ncum, nd, nk, icb, inb, delt, t, q, u, v, gz, p, &
-        ph, h, hp, lv, cpn, ep, clw, frac, m, mp, qp, up, vp, wt, water, evap, &
-        ment, qent, uent, vent, nent, elij, tv, tvp, iflag, wd, qprime, tprime, &
-        precip, cbmf, ft, fq, fu, fv, ma, qcondc)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  include "cvparam.h"
-
-  ! inputs
-  INTEGER ncum, nd, nloc
-  INTEGER nk(nloc), icb(nloc), inb(nloc)
-  INTEGER nent(nloc, nd)
-  REAL delt
-  REAL t(nloc, nd), q(nloc, nd), u(nloc, nd), v(nloc, nd)
-  REAL gz(nloc, nd)
-  REAL p(nloc, nd), ph(nloc, nd + 1), h(nloc, nd)
-  REAL hp(nloc, nd), lv(nloc, nd)
-  REAL cpn(nloc, nd), ep(nloc, nd), clw(nloc, nd), frac(nloc)
-  REAL m(nloc, nd), mp(nloc, nd), qp(nloc, nd)
-  REAL up(nloc, nd), vp(nloc, nd)
-  REAL wt(nloc, nd), water(nloc, nd), evap(nloc, nd)
-  REAL ment(nloc, nd, nd), qent(nloc, nd, nd), elij(nloc, nd, nd)
-  REAL uent(nloc, nd, nd), vent(nloc, nd, nd)
-  REAL tv(nloc, nd), tvp(nloc, nd)
-
-  ! outputs
-  INTEGER iflag(nloc) ! also an input
-  REAL cbmf(nloc) ! also an input
-  REAL wd(nloc), tprime(nloc), qprime(nloc)
-  REAL precip(nloc)
-  REAL ft(nloc, nd), fq(nloc, nd), fu(nloc, nd), fv(nloc, nd)
-  REAL ma(nloc, nd)
-  REAL qcondc(nloc, nd)
-
-  ! local variables
-  INTEGER i, j, ij, k, num1
-  REAL dpinv, cpinv, awat, fqold, ftold, fuold, fvold, delti
-  REAL work(nloc), am(nloc), amp1(nloc), ad(nloc)
-  REAL ents(nloc), uav(nloc), vav(nloc), lvcp(nloc, nd)
-  REAL qcond(nloc, nd), nqcond(nloc, nd), wa(nloc, nd) ! cld
-  REAL siga(nloc, nd), ax(nloc, nd), mac(nloc, nd) ! cld
-
-
-  ! -- initializations:
-
-  delti = 1.0 / delt
-
-  DO i = 1, ncum
-    precip(i) = 0.0
-    wd(i) = 0.0
-    tprime(i) = 0.0
-    qprime(i) = 0.0
-    DO k = 1, nl + 1
-      ft(i, k) = 0.0
-      fu(i, k) = 0.0
-      fv(i, k) = 0.0
-      fq(i, k) = 0.0
-      lvcp(i, k) = lv(i, k) / cpn(i, k)
-      qcondc(i, k) = 0.0 ! cld
-      qcond(i, k) = 0.0 ! cld
-      nqcond(i, k) = 0.0 ! cld
-    END DO
-  END DO
-
-
-  ! ***  Calculate surface precipitation in mm/day     ***
-
-  DO i = 1, ncum
-    IF (iflag(i)<=1) THEN
-      ! c            precip(i)=precip(i)+wt(i,1)*sigd*water(i,1)*3600.*24000.
-      ! c     &                /(rowl*g)
-      ! c            precip(i)=precip(i)*delt/86400.
-      precip(i) = wt(i, 1) * sigd * water(i, 1) * 86400 / g
-    END IF
-  END DO
-
-
-  ! ***  Calculate downdraft velocity scale and surface temperature and  ***
-  ! ***                    water vapor fluctuations                      ***
-
-  DO i = 1, ncum
-    wd(i) = betad * abs(mp(i, icb(i))) * 0.01 * rrd * t(i, icb(i)) / (sigd * p(i, icb(i)))
-    qprime(i) = 0.5 * (qp(i, 1) - q(i, 1))
-    tprime(i) = lv0 * qprime(i) / cpd
-  END DO
-
-  ! ***  Calculate tendencies of lowest level potential temperature  ***
-  ! ***                      and mixing ratio                        ***
-
-  DO i = 1, ncum
-    work(i) = 0.01 / (ph(i, 1) - ph(i, 2))
-    am(i) = 0.0
-  END DO
-  DO k = 2, nl
-    DO i = 1, ncum
-      IF ((nk(i)==1) .AND. (k<=inb(i)) .AND. (nk(i)==1)) THEN
-        am(i) = am(i) + m(i, k)
-      END IF
-    END DO
-  END DO
-  DO i = 1, ncum
-    IF ((g * work(i) * am(i))>=delti) iflag(i) = 1
-    ft(i, 1) = ft(i, 1) + g * work(i) * am(i) * (t(i, 2) - t(i, 1) + (gz(i, 2) - gz(i, &
-            1)) / cpn(i, 1))
-    ft(i, 1) = ft(i, 1) - lvcp(i, 1) * sigd * evap(i, 1)
-    ft(i, 1) = ft(i, 1) + sigd * wt(i, 2) * (cl - cpd) * water(i, 2) * (t(i, 2) - t(i, 1)) * &
-            work(i) / cpn(i, 1)
-    fq(i, 1) = fq(i, 1) + g * mp(i, 2) * (qp(i, 2) - q(i, 1)) * work(i) + &
-            sigd * evap(i, 1)
-    fq(i, 1) = fq(i, 1) + g * am(i) * (q(i, 2) - q(i, 1)) * work(i)
-    fu(i, 1) = fu(i, 1) + g * work(i) * (mp(i, 2) * (up(i, 2) - u(i, 1)) + am(i) * (u(i, &
-            2) - u(i, 1)))
-    fv(i, 1) = fv(i, 1) + g * work(i) * (mp(i, 2) * (vp(i, 2) - v(i, 1)) + am(i) * (v(i, &
-            2) - v(i, 1)))
-  END DO
-  DO j = 2, nl
-    DO i = 1, ncum
-      IF (j<=inb(i)) THEN
-        fq(i, 1) = fq(i, 1) + g * work(i) * ment(i, j, 1) * (qent(i, j, 1) - q(i, 1))
-        fu(i, 1) = fu(i, 1) + g * work(i) * ment(i, j, 1) * (uent(i, j, 1) - u(i, 1))
-        fv(i, 1) = fv(i, 1) + g * work(i) * ment(i, j, 1) * (vent(i, j, 1) - v(i, 1))
-      END IF
-    END DO
-  END DO
-
-  ! ***  Calculate tendencies of potential temperature and mixing ratio  ***
-  ! ***               at levels above the lowest level                   ***
-
-  ! ***  First find the net saturated updraft and downdraft mass fluxes  ***
-  ! ***                      through each level                          ***
-
-  DO i = 2, nl + 1
-
-    num1 = 0
-    DO ij = 1, ncum
-      IF (i<=inb(ij)) num1 = num1 + 1
-    END DO
-    IF (num1<=0) GO TO 1500
-
-    CALL zilch(amp1, ncum)
-    CALL zilch(ad, ncum)
-
-    DO k = i + 1, nl + 1
-      DO ij = 1, ncum
-        IF ((i>=nk(ij)) .AND. (i<=inb(ij)) .AND. (k<=(inb(ij) + 1))) THEN
-          amp1(ij) = amp1(ij) + m(ij, k)
-        END IF
-      END DO
-    END DO
-
-    DO k = 1, i
-      DO j = i + 1, nl + 1
-        DO ij = 1, ncum
-          IF ((j<=(inb(ij) + 1)) .AND. (i<=inb(ij))) THEN
-            amp1(ij) = amp1(ij) + ment(ij, k, j)
-          END IF
-        END DO
-      END DO
-    END DO
-    DO k = 1, i - 1
-      DO j = i, nl + 1
-        DO ij = 1, ncum
-          IF ((i<=inb(ij)) .AND. (j<=inb(ij))) THEN
-            ad(ij) = ad(ij) + ment(ij, j, k)
-          END IF
-        END DO
-      END DO
-    END DO
-
-    DO ij = 1, ncum
-      IF (i<=inb(ij)) THEN
-        dpinv = 0.01 / (ph(ij, i) - ph(ij, i + 1))
-        cpinv = 1.0 / cpn(ij, i)
-
-        ft(ij, i) = ft(ij, i) + g * dpinv * (amp1(ij) * (t(ij, i + 1) - t(ij, &
-                i) + (gz(ij, i + 1) - gz(ij, i)) * cpinv) - ad(ij) * (t(ij, i) - t(ij, &
-                i - 1) + (gz(ij, i) - gz(ij, i - 1)) * cpinv)) - sigd * lvcp(ij, i) * evap(ij, i)
-        ft(ij, i) = ft(ij, i) + g * dpinv * ment(ij, i, i) * (hp(ij, i) - h(ij, i) + t(ij &
-                , i) * (cpv - cpd) * (q(ij, i) - qent(ij, i, i))) * cpinv
-        ft(ij, i) = ft(ij, i) + sigd * wt(ij, i + 1) * (cl - cpd) * water(ij, i + 1) * (t(&
-                ij, i + 1) - t(ij, i)) * dpinv * cpinv
-        fq(ij, i) = fq(ij, i) + g * dpinv * (amp1(ij) * (q(ij, i + 1) - q(ij, &
-                i)) - ad(ij) * (q(ij, i) - q(ij, i - 1)))
-        fu(ij, i) = fu(ij, i) + g * dpinv * (amp1(ij) * (u(ij, i + 1) - u(ij, &
-                i)) - ad(ij) * (u(ij, i) - u(ij, i - 1)))
-        fv(ij, i) = fv(ij, i) + g * dpinv * (amp1(ij) * (v(ij, i + 1) - v(ij, &
-                i)) - ad(ij) * (v(ij, i) - v(ij, i - 1)))
-      END IF
-    END DO
-    DO k = 1, i - 1
-      DO ij = 1, ncum
-        IF (i<=inb(ij)) THEN
-          awat = elij(ij, k, i) - (1. - ep(ij, i)) * clw(ij, i)
-          awat = max(awat, 0.0)
-          fq(ij, i) = fq(ij, i) + g * dpinv * ment(ij, k, i) * (qent(ij, k, i) - awat - q &
-                  (ij, i))
-          fu(ij, i) = fu(ij, i) + g * dpinv * ment(ij, k, i) * (uent(ij, k, i) - u(ij, i &
-                  ))
-          fv(ij, i) = fv(ij, i) + g * dpinv * ment(ij, k, i) * (vent(ij, k, i) - v(ij, i &
-                  ))
-          ! (saturated updrafts resulting from mixing)               ! cld
-          qcond(ij, i) = qcond(ij, i) + (elij(ij, k, i) - awat) ! cld
-          nqcond(ij, i) = nqcond(ij, i) + 1. ! cld
-        END IF
-      END DO
-    END DO
-    DO k = i, nl + 1
-      DO ij = 1, ncum
-        IF ((i<=inb(ij)) .AND. (k<=inb(ij))) THEN
-          fq(ij, i) = fq(ij, i) + g * dpinv * ment(ij, k, i) * (qent(ij, k, i) - q(ij, i &
-                  ))
-          fu(ij, i) = fu(ij, i) + g * dpinv * ment(ij, k, i) * (uent(ij, k, i) - u(ij, i &
-                  ))
-          fv(ij, i) = fv(ij, i) + g * dpinv * ment(ij, k, i) * (vent(ij, k, i) - v(ij, i &
-                  ))
-        END IF
-      END DO
-    END DO
-    DO ij = 1, ncum
-      IF (i<=inb(ij)) THEN
-        fq(ij, i) = fq(ij, i) + sigd * evap(ij, i) + g * (mp(ij, i + 1) * (qp(ij, &
-                i + 1) - q(ij, i)) - mp(ij, i) * (qp(ij, i) - q(ij, i - 1))) * dpinv
-        fu(ij, i) = fu(ij, i) + g * (mp(ij, i + 1) * (up(ij, i + 1) - u(ij, &
-                i)) - mp(ij, i) * (up(ij, i) - u(ij, i - 1))) * dpinv
-        fv(ij, i) = fv(ij, i) + g * (mp(ij, i + 1) * (vp(ij, i + 1) - v(ij, &
-                i)) - mp(ij, i) * (vp(ij, i) - v(ij, i - 1))) * dpinv
-        ! (saturated downdrafts resulting from mixing)               ! cld
-        DO k = i + 1, inb(ij) ! cld
-          qcond(ij, i) = qcond(ij, i) + elij(ij, k, i) ! cld
-          nqcond(ij, i) = nqcond(ij, i) + 1. ! cld
-        END DO ! cld
-        ! (particular case: no detraining level is found)            ! cld
-        IF (nent(ij, i)==0) THEN ! cld
-          qcond(ij, i) = qcond(ij, i) + (1. - ep(ij, i)) * clw(ij, i) ! cld
-          nqcond(ij, i) = nqcond(ij, i) + 1. ! cld
-        END IF ! cld
-        IF (nqcond(ij, i)/=0.) THEN ! cld
-          qcond(ij, i) = qcond(ij, i) / nqcond(ij, i) ! cld
-        END IF ! cld
-      END IF
-    END DO
-  1500 END DO
-
-  ! *** Adjust tendencies at top of convection layer to reflect  ***
-  ! ***       actual position of the level zero cape             ***
-
-  DO ij = 1, ncum
-    fqold = fq(ij, inb(ij))
-    fq(ij, inb(ij)) = fq(ij, inb(ij)) * (1. - frac(ij))
-    fq(ij, inb(ij) - 1) = fq(ij, inb(ij) - 1) + frac(ij) * fqold * ((ph(ij, &
-            inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, &
-            inb(ij)))) * lv(ij, inb(ij)) / lv(ij, inb(ij) - 1)
-    ftold = ft(ij, inb(ij))
-    ft(ij, inb(ij)) = ft(ij, inb(ij)) * (1. - frac(ij))
-    ft(ij, inb(ij) - 1) = ft(ij, inb(ij) - 1) + frac(ij) * ftold * ((ph(ij, &
-            inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, &
-            inb(ij)))) * cpn(ij, inb(ij)) / cpn(ij, inb(ij) - 1)
-    fuold = fu(ij, inb(ij))
-    fu(ij, inb(ij)) = fu(ij, inb(ij)) * (1. - frac(ij))
-    fu(ij, inb(ij) - 1) = fu(ij, inb(ij) - 1) + frac(ij) * fuold * ((ph(ij, &
-            inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, inb(ij))))
-    fvold = fv(ij, inb(ij))
-    fv(ij, inb(ij)) = fv(ij, inb(ij)) * (1. - frac(ij))
-    fv(ij, inb(ij) - 1) = fv(ij, inb(ij) - 1) + frac(ij) * fvold * ((ph(ij, &
-            inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, inb(ij))))
-  END DO
-
-  ! ***   Very slightly adjust tendencies to force exact   ***
-  ! ***     enthalpy, momentum and tracer conservation     ***
-
-  DO ij = 1, ncum
-    ents(ij) = 0.0
-    uav(ij) = 0.0
-    vav(ij) = 0.0
-    DO i = 1, inb(ij)
-      ents(ij) = ents(ij) + (cpn(ij, i) * ft(ij, i) + lv(ij, i) * fq(ij, i)) * (ph(ij, i) - &
-              ph(ij, i + 1))
-      uav(ij) = uav(ij) + fu(ij, i) * (ph(ij, i) - ph(ij, i + 1))
-      vav(ij) = vav(ij) + fv(ij, i) * (ph(ij, i) - ph(ij, i + 1))
-    END DO
-  END DO
-  DO ij = 1, ncum
-    ents(ij) = ents(ij) / (ph(ij, 1) - ph(ij, inb(ij) + 1))
-    uav(ij) = uav(ij) / (ph(ij, 1) - ph(ij, inb(ij) + 1))
-    vav(ij) = vav(ij) / (ph(ij, 1) - ph(ij, inb(ij) + 1))
-  END DO
-  DO ij = 1, ncum
-    DO i = 1, inb(ij)
-      ft(ij, i) = ft(ij, i) - ents(ij) / cpn(ij, i)
-      fu(ij, i) = (1. - cu) * (fu(ij, i) - uav(ij))
-      fv(ij, i) = (1. - cu) * (fv(ij, i) - vav(ij))
-    END DO
-  END DO
-
-  DO k = 1, nl + 1
-    DO i = 1, ncum
-      IF ((q(i, k) + delt * fq(i, k))<0.0) iflag(i) = 10
-    END DO
-  END DO
-
-  DO i = 1, ncum
-    IF (iflag(i)>2) THEN
-      precip(i) = 0.0
-      cbmf(i) = 0.0
-    END IF
-  END DO
-  DO k = 1, nl
-    DO i = 1, ncum
-      IF (iflag(i)>2) THEN
-        ft(i, k) = 0.0
-        fq(i, k) = 0.0
-        fu(i, k) = 0.0
-        fv(i, k) = 0.0
-        qcondc(i, k) = 0.0 ! cld
-      END IF
-    END DO
-  END DO
-
-  DO k = 1, nl + 1
-    DO i = 1, ncum
-      ma(i, k) = 0.
-    END DO
-  END DO
-  DO k = nl, 1, -1
-    DO i = 1, ncum
-      ma(i, k) = ma(i, k + 1) + m(i, k)
-    END DO
-  END DO
-
-
-  ! *** diagnose the in-cloud mixing ratio   ***            ! cld
-  ! ***           of condensed water         ***            ! cld
-  ! cld
-  DO ij = 1, ncum ! cld
-    DO i = 1, nd ! cld
-      mac(ij, i) = 0.0 ! cld
-      wa(ij, i) = 0.0 ! cld
-      siga(ij, i) = 0.0 ! cld
-    END DO ! cld
-    DO i = nk(ij), inb(ij) ! cld
-      DO k = i + 1, inb(ij) + 1 ! cld
-        mac(ij, i) = mac(ij, i) + m(ij, k) ! cld
-      END DO ! cld
-    END DO ! cld
-    DO i = icb(ij), inb(ij) - 1 ! cld
-      ax(ij, i) = 0. ! cld
-      DO j = icb(ij), i ! cld
-        ax(ij, i) = ax(ij, i) + rrd * (tvp(ij, j) - tv(ij, j)) & ! cld
-                * (ph(ij, j) - ph(ij, j + 1)) / p(ij, j) ! cld
-      END DO ! cld
-      IF (ax(ij, i)>0.0) THEN ! cld
-        wa(ij, i) = sqrt(2. * ax(ij, i)) ! cld
-      END IF ! cld
-    END DO ! cld
-    DO i = 1, nl ! cld
-      IF (wa(ij, i)>0.0) &          ! cld
-              siga(ij, i) = mac(ij, i) / wa(ij, i) & ! cld
-                      * rrd * tvp(ij, i) / p(ij, i) / 100. / delta ! cld
-      siga(ij, i) = min(siga(ij, i), 1.0) ! cld
-      qcondc(ij, i) = siga(ij, i) * clw(ij, i) * (1. - ep(ij, i)) & ! cld
-              + (1. - siga(ij, i)) * qcond(ij, i) ! cld
-    END DO ! cld
-  END DO ! cld
-
-END SUBROUTINE cv_yield
-
-SUBROUTINE cv_uncompress(nloc, len, ncum, nd, idcum, iflag, precip, cbmf, ft, &
-        fq, fu, fv, ma, qcondc, iflag1, precip1, cbmf1, ft1, fq1, fu1, fv1, ma1, &
-        qcondc1)
-  IMPLICIT NONE
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER len, ncum, nd, nloc
-  INTEGER idcum(nloc)
-  INTEGER iflag(nloc)
-  REAL precip(nloc), cbmf(nloc)
-  REAL ft(nloc, nd), fq(nloc, nd), fu(nloc, nd), fv(nloc, nd)
-  REAL ma(nloc, nd)
-  REAL qcondc(nloc, nd) !cld
-
-  ! outputs:
-  INTEGER iflag1(len)
-  REAL precip1(len), cbmf1(len)
-  REAL ft1(len, nd), fq1(len, nd), fu1(len, nd), fv1(len, nd)
-  REAL ma1(len, nd)
-  REAL qcondc1(len, nd) !cld
-
-  ! local variables:
-  INTEGER i, k
-
-  DO i = 1, ncum
-    precip1(idcum(i)) = precip(i)
-    cbmf1(idcum(i)) = cbmf(i)
-    iflag1(idcum(i)) = iflag(i)
-  END DO
-
-  DO k = 1, nl
-    DO i = 1, ncum
-      ft1(idcum(i), k) = ft(i, k)
-      fq1(idcum(i), k) = fq(i, k)
-      fu1(idcum(i), k) = fu(i, k)
-      fv1(idcum(i), k) = fv(i, k)
-      ma1(idcum(i), k) = ma(i, k)
-      qcondc1(idcum(i), k) = qcondc(i, k)
-    END DO
-  END DO
-
-END SUBROUTINE cv_uncompress
-
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/cva_driver.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/cva_driver.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/cva_driver.F90	(revision 5142)
@@ -42,4 +42,7 @@
   USE add_phys_tend_mod, ONLY: fl_cor_ebil
   USE lmdz_abort_physic, ONLY: abort_physic
+  USE lmdz_cv, ONLY: cv_param, cv_prelim, cv_feed, cv_undilute1, cv_trigger, cv_compress, &
+          cv_undilute2, cv_closure, cv_mixing, cv_unsat, cv_yield, cv_uncompress
+
   IMPLICIT NONE
 
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/cvltr.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/cvltr.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/cvltr.F90	(revision 5142)
@@ -14,4 +14,5 @@
   USE infotrac_phy, ONLY: nbtr
   USE lmdz_conema3
+  USE lmdz_YOECUMF
 
   IMPLICIT NONE 
@@ -23,5 +24,4 @@
 
   include "YOMCST.h"
-  include "YOECUMF.h"
 
 ! Entree
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/cvltr_noscav.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/cvltr_noscav.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/cvltr_noscav.F90	(revision 5142)
@@ -5,4 +5,6 @@
   USE dimphy
   USE infotrac_phy, ONLY: nbtr
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE 
 !=====================================================================
@@ -11,5 +13,4 @@
 !=====================================================================
   include "YOMCST.h"
-  include "YOECUMF.h" 
 
 ! Entree
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/cvltr_scav.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/cvltr_scav.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/cvltr_scav.F90	(revision 5142)
@@ -15,4 +15,5 @@
   USE infotrac_phy, ONLY: nbtr
   USE lmdz_conema3
+  USE lmdz_YOECUMF
 
   IMPLICIT NONE 
@@ -24,5 +25,4 @@
 
   include "YOMCST.h"
-  include "YOECUMF.h"
   include "chem.h"
 
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/cvltr_spl.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/cvltr_spl.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/cvltr_spl.F90	(revision 5142)
@@ -15,4 +15,5 @@
   USE infotrac_phy, ONLY: nbtr
   USE lmdz_conema3
+  USE lmdz_YOECUMF
 
   IMPLICIT NONE 
@@ -24,5 +25,4 @@
 
   include "YOMCST.h"
-  include "YOECUMF.h"
   include "chem.h"
 
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/cvltrorig.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/cvltrorig.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/cvltrorig.F90	(revision 5142)
@@ -5,4 +5,6 @@
   USE dimphy
   USE infotrac_phy, ONLY: nbtr
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE 
 !=====================================================================
@@ -11,5 +13,4 @@
 !=====================================================================
   include "YOMCST.h"
-  include "YOECUMF.h" 
 
 ! Entree
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/cvparam.h
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/cvparam.h	(revision 5141)
+++ 	(revision )
@@ -1,29 +1,0 @@
-
-! $Header$
-
-!------------------------------------------------------------
-! Parameters for convectL:
-! (includes - microphysical parameters, 
-!			- parameters that control the rate of approach 
-!               to quasi-equilibrium)
-!			- noff & minorig (previously in input of convect1)
-!------------------------------------------------------------
-
-      INTEGER noff, minorig, nl, nlp, nlm
-      REAL elcrit, tlcrit
-      REAL entp
-      REAL sigs, sigd
-      REAL omtrain, omtsnow, coeffr, coeffs
-      REAL dtmax
-      REAL cu
-      REAL betad
-      REAL alpha, damp
-      REAL delta
-
-      COMMON /cvparam/ elcrit, tlcrit &
-                      ,entp, sigs, sigd &
-                      ,omtrain, omtsnow, coeffr, coeffs &
-                      ,dtmax, cu, betad, alpha, damp, delta &
-                      ,noff, minorig, nl, nlp, nlm
-
-!$OMP THREADPRIVATE(/cvparam/)
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/dimpft.h
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/dimpft.h	(revision 5141)
+++ 	(revision )
@@ -1,6 +1,0 @@
-
-! $Id$
-
-      INTEGER nvm_lmdz
-!      PARAMETER (nvm_lmdz=13)
-      COMMON /dimpft/ nvm_lmdz
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/dimphy.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/dimphy.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/dimphy.F90	(revision 5142)
@@ -1,62 +1,61 @@
-
 ! $Id$
 
-  MODULE dimphy
-  
-  INTEGER,SAVE :: klon
-  INTEGER,SAVE :: kdlon
-  INTEGER,SAVE :: kfdia
-  INTEGER,SAVE :: kidia
-  INTEGER,SAVE :: klev
-  INTEGER,SAVE :: klevp1
-  INTEGER,SAVE :: klevm1
-  INTEGER,SAVE :: kflev
+MODULE dimphy
 
-!$OMP THREADPRIVATE(klon,kfdia,kidia,kdlon)
-  REAL,SAVE,ALLOCATABLE,DIMENSION(:) :: zmasq
-!$OMP THREADPRIVATE(zmasq)   
+  INTEGER, SAVE :: klon
+  INTEGER, SAVE :: kdlon
+  INTEGER, SAVE :: kfdia
+  INTEGER, SAVE :: kidia
+  INTEGER, SAVE :: klev
+  INTEGER, SAVE :: klevp1
+  INTEGER, SAVE :: klevm1
+  INTEGER, SAVE :: kflev
+
+  !$OMP THREADPRIVATE(klon,kfdia,kidia,kdlon)
+  REAL, SAVE, ALLOCATABLE, DIMENSION(:) :: zmasq
+  !$OMP THREADPRIVATE(zmasq)
 
 CONTAINS
-  
-  SUBROUTINE Init_dimphy(klon0,klev0)
-  IMPLICIT NONE
-  
+
+  SUBROUTINE Init_dimphy(klon0, klev0)
+    IMPLICIT NONE
+
     INTEGER, INTENT(IN) :: klon0
     INTEGER, INTENT(IN) :: klev0
-    
-    klon=klon0
-    kdlon=klon
-    kidia=1
-    kfdia=klon
-!$OMP MASTER 
-    klev=klev0
-    klevp1=klev+1
-    klevm1=klev-1
-    kflev=klev
-!$OMP END MASTER    
-    ALLOCATE(zmasq(klon))    
-    zmasq=0.
-    
+
+    klon = klon0
+    kdlon = klon
+    kidia = 1
+    kfdia = klon
+    !$OMP MASTER
+    klev = klev0
+    klevp1 = klev + 1
+    klevm1 = klev - 1
+    kflev = klev
+    !$OMP END MASTER
+    ALLOCATE(zmasq(klon))
+    zmasq = 0.
+
   END SUBROUTINE Init_dimphy
 
-  SUBROUTINE Init_dimphy1D(klon0,klev0)
-! 1D special version of dimphy without ALLOCATE(zmasq)
-! which will be allocated in iniphysiq
-  IMPLICIT NONE
-  
+  SUBROUTINE Init_dimphy1D(klon0, klev0)
+    ! 1D special version of dimphy without ALLOCATE(zmasq)
+    ! which will be allocated in iniphysiq
+    IMPLICIT NONE
+
     INTEGER, INTENT(IN) :: klon0
     INTEGER, INTENT(IN) :: klev0
-    
-    klon=klon0
-    kdlon=klon
-    kidia=1
-    kfdia=klon
-    klev=klev0
-    klevp1=klev+1
-    klevm1=klev-1
-    kflev=klev
-    
+
+    klon = klon0
+    kdlon = klon
+    kidia = 1
+    kfdia = klon
+    klev = klev0
+    klevp1 = klev + 1
+    klevm1 = klev - 1
+    kflev = klev
+
   END SUBROUTINE Init_dimphy1D
 
-  
+
 END MODULE dimphy
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/dyn1d/lmdz_1dutils.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/dyn1d/lmdz_1dutils.f90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/dyn1d/lmdz_1dutils.f90	(revision 5142)
@@ -65,5 +65,6 @@
     USE lmdz_print_control, ONLY: lunout
     USE lmdz_flux_arp, ONLY: fsens, flat, betaevap, ust, tg, ok_flux_surf, ok_prescr_ust, ok_prescr_beta, ok_forc_tsurf
-
+    USE lmdz_fcs_gcssold, ONLY: imp_fcg_gcssold, ts_fcg_gcssold, Tp_fcg_gcssold, Tp_ini_gcssold, xTurb_fcg_gcssold
+    USE lmdz_tsoilnudge, ONLY: nudge_tsoil, isoil_nudge, Tsoil_nudge, tau_soil_nudge
     !-----------------------------------------------------------------------
     !     Auteurs :   A. Lahellec  .
@@ -73,6 +74,4 @@
 
     include "compar1d.h"
-    include "tsoilnudge.h"
-    include "fcg_gcssold.h"
     include "fcg_racmo.h"
 
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/dyn1d/lmdz_old_lmdz1d.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/dyn1d/lmdz_old_lmdz1d.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/dyn1d/lmdz_old_lmdz1d.F90	(revision 5142)
@@ -54,4 +54,6 @@
     USE lmdz_flux_arp, ONLY: fsens, flat, betaevap, ust, tg, ok_flux_surf, ok_prescr_ust, ok_prescr_beta, ok_forc_tsurf
     USE lmdz_compbl, ONLY: iflag_pbl, iflag_pbl_split, iflag_order2_sollw, ifl_pbltree
+    USE lmdz_fcs_gcssold, ONLY: imp_fcg_gcssold, ts_fcg_gcssold, Tp_fcg_gcssold, Tp_ini_gcssold, xTurb_fcg_gcssold
+    USE lmdz_tsoilnudge, ONLY: nudge_tsoil, isoil_nudge, Tsoil_nudge, tau_soil_nudge
 
     INCLUDE "dimensions.h"
@@ -60,6 +62,4 @@
     INCLUDE "compar1d.h"
     INCLUDE "date_cas.h"
-    INCLUDE "tsoilnudge.h"
-    INCLUDE "fcg_gcssold.h"
 
     !=====================================================================
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/dyn1d/lmdz_scm.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/dyn1d/lmdz_scm.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/dyn1d/lmdz_scm.F90	(revision 5142)
@@ -47,4 +47,6 @@
     USE lmdz_flux_arp, ONLY: fsens, flat, betaevap, ust, tg, ok_flux_surf, ok_prescr_ust, ok_prescr_beta, ok_forc_tsurf
     USE lmdz_compbl, ONLY: iflag_pbl, iflag_pbl_split, iflag_order2_sollw, ifl_pbltree
+    USE lmdz_fcs_gcssold, ONLY: imp_fcg_gcssold, ts_fcg_gcssold, Tp_fcg_gcssold, Tp_ini_gcssold, xTurb_fcg_gcssold
+    USE lmdz_tsoilnudge, ONLY: nudge_tsoil, isoil_nudge, Tsoil_nudge, tau_soil_nudge
 
     INCLUDE "dimensions.h"
@@ -53,6 +55,4 @@
     INCLUDE "compar1d.h"
     INCLUDE "date_cas.h"
-    INCLUDE "tsoilnudge.h"
-    INCLUDE "fcg_gcssold.h"
 
     !=====================================================================
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/fcg_gcssold.h
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/fcg_gcssold.h	(revision 5141)
+++ 	(revision )
@@ -1,46 +1,0 @@
-
-! $Id: fcg_gcssold.h 2010-08-10 17:02:56Z lahellec $
-
-      LOGICAL :: imp_fcg_gcssold,ts_fcg_gcssold,Tp_fcg_gcssold
-      LOGICAL :: Tp_ini_gcssold
-      LOGICAL :: xTurb_fcg_gcssold
-
-      common /fcg_gcssold/imp_fcg_gcssold,ts_fcg_gcssold,Tp_fcg_gcssold,        & 
-       Tp_ini_gcssold,                                                          &
-       xTurb_fcg_gcssold
-
-!$OMP THREADPRIVATE(/fcg_gcssold/)
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/flxtr.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/flxtr.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/flxtr.F90	(revision 5142)
@@ -5,4 +5,6 @@
     paprs, kcbot, kctop, kdtop, x, dx)
   USE dimphy
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE
   ! =====================================================================
@@ -26,5 +28,4 @@
 
   include "YOMCST.h"
-  include "YOECUMF.h"
 
   REAL pdtime
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/freinage.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/freinage.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/freinage.F90	(revision 5142)
@@ -9,4 +9,5 @@
     USE lmdz_clesphys
     USE lmdz_compbl, ONLY: iflag_pbl, iflag_pbl_split, iflag_order2_sollw, ifl_pbltree
+    USE lmdz_dimpft, ONLY: nvm_lmdz
 !    USE control, ONLY: nvm
 !    USE indice_sol_mod, ONLY: nvm_orch
@@ -17,6 +18,4 @@
     include "YOMCST.h"
     include "YOEGWD.h"
-!FC 
-    include "dimpft.h"
 
     ! 0. DECLARATIONS:
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/ini_COSP.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/ini_COSP.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/ini_COSP.F90	(revision 5142)
@@ -1,36 +1,36 @@
 !  A.I avril 2023
 
-  SUBROUTINE ini_COSP(ref_liq_cosp0,ref_ice_cosp0,pctsrf_cosp0,zu10m_cosp0,zv10m_cosp0, &
-                      zxtsol_cosp0,zx_rh_cosp0,cldfra_cosp0,rnebcon_cosp0,flwc_cosp0, &
-                      fiwc_cosp0,prfl_cosp0,psfl_cosp0,pmflxr_cosp0,pmflxs_cosp0, &
-                      mr_ozone_cosp0,cldtau_cosp0,cldemi_cosp0,JrNt_cosp0)
+SUBROUTINE ini_COSP(ref_liq_cosp0, ref_ice_cosp0, pctsrf_cosp0, zu10m_cosp0, zv10m_cosp0, &
+        zxtsol_cosp0, zx_rh_cosp0, cldfra_cosp0, rnebcon_cosp0, flwc_cosp0, &
+        fiwc_cosp0, prfl_cosp0, psfl_cosp0, pmflxr_cosp0, pmflxs_cosp0, &
+        mr_ozone_cosp0, cldtau_cosp0, cldemi_cosp0, JrNt_cosp0)
 
-! Routine pour initialiser les champs input pour Cosp au  1er appel de celui-ci
-!         Ce 1er appel sert uniquement a definir les axes verticaux pour les 
-!         sortie Cosp
+  ! Routine pour initialiser les champs input pour Cosp au  1er appel de celui-ci
+  !         Ce 1er appel sert uniquement a definir les axes verticaux pour les
+  !         sortie Cosp
 
-      USE dimphy
-      include "ini_COSP.h"
+  USE dimphy
+  include "ini_COSP.h"
 
-      ! Initialisations pour le 1er passage a Cosp
-        ref_liq_cosp0=1.
-        ref_ice_cosp0=1.
-        pctsrf_cosp0=0.5
-        zu10m_cosp0=1.
-        zv10m_cosp0=1.
-        zxtsol_cosp0=288.
-        zx_rh_cosp0=1.
-        cldfra_cosp0=1.
-        rnebcon_cosp0=0.
-        flwc_cosp0=0.
-        fiwc_cosp0=0.
-        prfl_cosp0(:,1:klev)=0.
-        psfl_cosp0(:,1:klev)=0.
-        pmflxr_cosp0(:,1:klev)=0.
-        pmflxs_cosp0(:,1:klev)=0.
-        mr_ozone_cosp0=0.
-        cldtau_cosp0=0.
-        cldemi_cosp0=0.
-        JrNt_cosp0=0.
+  ! Initialisations pour le 1er passage a Cosp
+  ref_liq_cosp0 = 1.
+  ref_ice_cosp0 = 1.
+  pctsrf_cosp0 = 0.5
+  zu10m_cosp0 = 1.
+  zv10m_cosp0 = 1.
+  zxtsol_cosp0 = 288.
+  zx_rh_cosp0 = 1.
+  cldfra_cosp0 = 1.
+  rnebcon_cosp0 = 0.
+  flwc_cosp0 = 0.
+  fiwc_cosp0 = 0.
+  prfl_cosp0(:, 1:klev) = 0.
+  psfl_cosp0(:, 1:klev) = 0.
+  pmflxr_cosp0(:, 1:klev) = 0.
+  pmflxs_cosp0(:, 1:klev) = 0.
+  mr_ozone_cosp0 = 0.
+  cldtau_cosp0 = 0.
+  cldemi_cosp0 = 0.
+  JrNt_cosp0 = 0.
 
-     END SUBROUTINE  ini_COSP
+END SUBROUTINE  ini_COSP
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/iniorbit.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/iniorbit.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/iniorbit.F90	(revision 5142)
@@ -1,3 +1,5 @@
 SUBROUTINE iniorbit(paphelie, pperiheli, pyear_day, pperi_day, pobliq)
+  USE lmdz_planete, ONLY: aphelie, periheli, year_day, peri_day, obliquit, timeperi, e_elips, p_elips, unitastr
+
   IMPLICIT NONE
 
@@ -18,5 +20,5 @@
   ! ----------
   ! - Doit etre appele avant d'utiliser orbite.
-  ! - initialise une partie du common planete.h
+  ! - initialise une partie du module lmdz_planete.f90
 
   ! Arguments:
@@ -33,6 +35,4 @@
   ! Declarations:
   ! -------------
-
-  include "planete.h"
   include "YOMCST.h"
 
@@ -50,5 +50,5 @@
   ! -----------------------------------------------------------------------
 
-  pi = 2.*asin(1.)
+  pi = 2. * asin(1.)
 
   aphelie = paphelie
@@ -64,6 +64,6 @@
   PRINT *, 'Date perihelie : ', peri_day
   unitastr = 149.597870
-  e_elips = (aphelie-periheli)/(periheli+aphelie)
-  p_elips = 0.5*(periheli+aphelie)*(1-e_elips*e_elips)/unitastr
+  e_elips = (aphelie - periheli) / (periheli + aphelie)
+  p_elips = 0.5 * (periheli + aphelie) * (1 - e_elips * e_elips) / unitastr
 
   PRINT *, 'e_elips', e_elips
@@ -76,6 +76,6 @@
   ! calcul de l'zanomalie moyenne
 
-  zz = (year_day-pperi_day)/year_day
-  zanom = 2.*pi*(zz-nint(zz))
+  zz = (year_day - pperi_day) / year_day
+  zanom = 2. * pi * (zz - nint(zz))
   zxref = abs(zanom)
   PRINT *, 'zanom  ', zanom
@@ -84,11 +84,11 @@
   ! methode de Newton
 
-  zx0 = zxref + r_ecc*sin(zxref)
+  zx0 = zxref + r_ecc * sin(zxref)
   DO iter = 1, 100
-    zdx = -(zx0-r_ecc*sin(zx0)-zxref)/(1.-r_ecc*cos(zx0))
+    zdx = -(zx0 - r_ecc * sin(zx0) - zxref) / (1. - r_ecc * cos(zx0))
     IF (abs(zdx)<=(1.E-12)) GO TO 120
     zx0 = zx0 + zdx
   END DO
-120 CONTINUE
+  120 CONTINUE
   zx0 = zx0 + zdx
   IF (zanom<0.) zx0 = -zx0
@@ -97,7 +97,6 @@
   ! zteta est la longitude solaire
 
-  timeperi = 2.*atan(sqrt((1.+r_ecc)/(1.-r_ecc))*tan(zx0/2.))
+  timeperi = 2. * atan(sqrt((1. + r_ecc) / (1. - r_ecc)) * tan(zx0 / 2.))
   PRINT *, 'longitude solaire du perihelie timeperi = ', timeperi
 
-
 END SUBROUTINE iniorbit
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/init_be.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/init_be.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/init_be.F90	(revision 5142)
@@ -7,4 +7,5 @@
   USE indice_sol_mod
   USE lmdz_geometry, ONLY: longitude, latitude
+  USE lmdz_YOECUMF
     
   IMPLICIT NONE 
@@ -20,5 +21,4 @@
 
   INCLUDE "YOMCST.h"
-  INCLUDE "YOECUMF.h" 
 
 ! Input Arguments
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_YOECUMF.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_YOECUMF.f90	(revision 5142)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_YOECUMF.f90	(revision 5142)
@@ -0,0 +1,42 @@
+MODULE lmdz_YOECUMF
+  !     ----------------------------------------------------------------
+  !*    *COMMON* *YOECUMF* - PARAMETERS FOR CUMULUS MASSFLUX SCHEME
+  !     ----------------------------------------------------------------
+
+  IMPLICIT NONE; PRIVATE
+  PUBLIC ENTRPEN, ENTRSCV, ENTRMID, ENTRDD, CMFCTOP, CMFCMAX, CMFCMIN, CMFDEPS, RHCDD, &
+          CPRCON, LMFPEN, LMFSCV, LMFMID, LMFDD, LMFDUDV
+
+  LOGICAL          LMFPEN, LMFSCV, LMFMID, LMFDD, LMFDUDV
+  REAL ENTRPEN, ENTRSCV, ENTRMID, ENTRDD
+  REAL CMFCTOP, CMFCMAX, CMFCMIN, CMFDEPS, RHCDD, CPRCON
+  !$OMP THREADPRIVATE(ENTRPEN, ENTRSCV, ENTRMID, ENTRDD, CMFCTOP, CMFCMAX, CMFCMIN, CMFDEPS, RHCDD, &
+  !$OMP      CPRCON, LMFPEN, LMFSCV, LMFMID, LMFDD, LMFDUDV)
+
+  !*if (DOC,declared) <> 'UNKNOWN'
+  !*    *COMMON* *YOECUMF* - PARAMETERS FOR CUMULUS MASSFLUX SCHEME
+
+  !     M.TIEDTKE       E. C. M. W. F.      18/1/89
+
+  !     NAME      TYPE      PURPOSE
+  !     ----      ----      -------
+
+  !     LMFPEN    LOGICAL  TRUE IF PENETRATIVE CONVECTION IS SWITCHED ON
+  !     LMFSCV    LOGICAL  TRUE IF SHALLOW     CONVECTION IS SWITCHED ON
+  !     LMFMID    LOGICAL  TRUE IF MIDLEVEL    CONVECTION IS SWITCHED ON
+  !     LMFDD     LOGICAL  TRUE IF CUMULUS DOWNDRAFT      IS SWITCHED ON
+  !     LMFDUDV   LOGICAL  TRUE IF CUMULUS FRICTION       IS SWITCHED ON
+  !     ENTRPEN   REAL     ENTRAINMENT RATE FOR PENETRATIVE CONVECTION
+  !     ENTRSCV   REAL     ENTRAINMENT RATE FOR SHALLOW CONVECTION
+  !     ENTRMID   REAL     ENTRAINMENT RATE FOR MIDLEVEL CONVECTION
+  !     ENTRDD    REAL     ENTRAINMENT RATE FOR CUMULUS DOWNDRAFTS
+  !     CMFCTOP   REAL     RELAT. CLOUD MASSFLUX AT LEVEL ABOVE NONBUOYANC
+  !     CMFCMAX   REAL     MAXIMUM MASSFLUX VALUE ALLOWED FOR
+  !     CMFCMIN   REAL     MINIMUM MASSFLUX VALUE (FOR SAFETY)
+  !     CMFDEPS   REAL     FRACTIONAL MASSFLUX FOR DOWNDRAFTS AT LFS
+  !     RHCDD     REAL     RELATIVE SATURATION IN DOWNDRAFTS
+  !     CPRCON    REAL     COEFFICIENTS FOR DETERMINING CONVERSION
+  !                        FROM CLOUD WATER TO RAIN
+  !*ifend
+  !     ----------------------------------------------------------------
+END MODULE lmdz_YOECUMF
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_cv.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_cv.f90	(revision 5142)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_cv.f90	(revision 5142)
@@ -0,0 +1,1719 @@
+! $Id$
+
+MODULE lmdz_cv
+  !------------------------------------------------------------
+  ! Parameters for convectL:
+  ! (includes - microphysical parameters,
+  !			- parameters that control the rate of approach
+  !               to quasi-equilibrium)
+  !			- noff & minorig (previously in input of convect1)
+  !------------------------------------------------------------
+
+  IMPLICIT NONE; PRIVATE
+  PUBLIC elcrit, tlcrit, entp, sigs, sigd, omtrain, omtsnow, coeffr, coeffs &
+          , dtmax, cu, betad, alpha, damp, delta, noff, minorig, nl, nlp, nlm, &
+          cv_param, cv_prelim, cv_feed, cv_undilute1, cv_trigger, cv_compress, &
+          cv_undilute2, cv_closure, cv_mixing, cv_unsat, cv_yield, cv_uncompress
+
+  INTEGER noff, minorig, nl, nlp, nlm
+  REAL elcrit, tlcrit
+  REAL entp
+  REAL sigs, sigd
+  REAL omtrain, omtsnow, coeffr, coeffs
+  REAL dtmax
+  REAL cu
+  REAL betad
+  REAL alpha, damp
+  REAL delta
+
+  !$OMP THREADPRIVATE(elcrit, tlcrit, entp, sigs, sigd, omtrain, omtsnow, coeffr, coeffs &
+  !$OMP      , dtmax, cu, betad, alpha, damp, delta, noff, minorig, nl, nlp, nlm)
+
+CONTAINS
+
+  SUBROUTINE cv_param(nd)
+    IMPLICIT NONE
+
+    ! ------------------------------------------------------------
+    ! Set parameters for convectL
+    ! (includes microphysical parameters and parameters that
+    ! control the rate of approach to quasi-equilibrium)
+    ! ------------------------------------------------------------
+
+    ! *** ELCRIT IS THE AUTOCONVERSION THERSHOLD WATER CONTENT (gm/gm) ***
+    ! ***  TLCRIT IS CRITICAL TEMPERATURE BELOW WHICH THE AUTO-        ***
+    ! ***       CONVERSION THRESHOLD IS ASSUMED TO BE ZERO             ***
+    ! ***     (THE AUTOCONVERSION THRESHOLD VARIES LINEARLY            ***
+    ! ***               BETWEEN 0 C AND TLCRIT)                        ***
+    ! ***   ENTP IS THE COEFFICIENT OF MIXING IN THE ENTRAINMENT       ***
+    ! ***                       FORMULATION                            ***
+    ! ***  SIGD IS THE FRACTIONAL AREA COVERED BY UNSATURATED DNDRAFT  ***
+    ! ***  SIGS IS THE FRACTION OF PRECIPITATION FALLING OUTSIDE       ***
+    ! ***                        OF CLOUD                              ***
+    ! ***        OMTRAIN IS THE ASSUMED FALL SPEED (P/s) OF RAIN       ***
+    ! ***     OMTSNOW IS THE ASSUMED FALL SPEED (P/s) OF SNOW          ***
+    ! ***  COEFFR IS A COEFFICIENT GOVERNING THE RATE OF EVAPORATION   ***
+    ! ***                          OF RAIN                             ***
+    ! ***  COEFFS IS A COEFFICIENT GOVERNING THE RATE OF EVAPORATION   ***
+    ! ***                          OF SNOW                             ***
+    ! ***     CU IS THE COEFFICIENT GOVERNING CONVECTIVE MOMENTUM      ***
+    ! ***                         TRANSPORT                            ***
+    ! ***    DTMAX IS THE MAXIMUM NEGATIVE TEMPERATURE PERTURBATION    ***
+    ! ***        A LIFTED PARCEL IS ALLOWED TO HAVE BELOW ITS LFC      ***
+    ! ***    ALPHA AND DAMP ARE PARAMETERS THAT CONTROL THE RATE OF    ***
+    ! ***                 APPROACH TO QUASI-EQUILIBRIUM                ***
+    ! ***   (THEIR STANDARD VALUES ARE  0.20 AND 0.1, RESPECTIVELY)    ***
+    ! ***                   (DAMP MUST BE LESS THAN 1)                 ***
+
+        INTEGER nd
+    CHARACTER (LEN = 20) :: modname = 'cv_routines'
+    CHARACTER (LEN = 80) :: abort_message
+
+    ! noff: integer limit for convection (nd-noff)
+    ! minorig: First level of convection
+
+    noff = 2
+    minorig = 2
+
+    nl = nd - noff
+    nlp = nl + 1
+    nlm = nl - 1
+
+    elcrit = 0.0011
+    tlcrit = -55.0
+    entp = 1.5
+    sigs = 0.12
+    sigd = 0.05
+    omtrain = 50.0
+    omtsnow = 5.5
+    coeffr = 1.0
+    coeffs = 0.8
+    dtmax = 0.9
+
+    cu = 0.70
+
+    betad = 10.0
+
+    damp = 0.1
+    alpha = 0.2
+
+    delta = 0.01 ! cld
+
+  END SUBROUTINE cv_param
+
+  SUBROUTINE cv_prelim(len, nd, ndp1, t, q, p, ph, lv, cpn, tv, gz, h, hm)
+    USE lmdz_cvthermo
+
+    IMPLICIT NONE
+
+    ! =====================================================================
+    ! --- CALCULATE ARRAYS OF GEOPOTENTIAL, HEAT CAPACITY & STATIC ENERGY
+    ! =====================================================================
+
+    ! inputs:
+    INTEGER len, nd, ndp1
+    REAL t(len, nd), q(len, nd), p(len, nd), ph(len, ndp1)
+
+    ! outputs:
+    REAL lv(len, nd), cpn(len, nd), tv(len, nd)
+    REAL gz(len, nd), h(len, nd), hm(len, nd)
+
+    ! local variables:
+    INTEGER k, i
+    REAL cpx(len, nd)
+
+
+    DO k = 1, nlp
+      DO i = 1, len
+        lv(i, k) = lv0 - clmcpv * (t(i, k) - t0)
+        cpn(i, k) = cpd * (1.0 - q(i, k)) + cpv * q(i, k)
+        cpx(i, k) = cpd * (1.0 - q(i, k)) + cl * q(i, k)
+        tv(i, k) = t(i, k) * (1.0 + q(i, k) * epsim1)
+      END DO
+    END DO
+
+    ! gz = phi at the full levels (same as p).
+
+    DO i = 1, len
+      gz(i, 1) = 0.0
+    END DO
+    DO k = 2, nlp
+      DO i = 1, len
+        gz(i, k) = gz(i, k - 1) + hrd * (tv(i, k - 1) + tv(i, k)) * (p(i, k - 1) - p(i, k)) / ph(i, &
+                k)
+      END DO
+    END DO
+
+    ! h  = phi + cpT (dry static energy).
+    ! hm = phi + cp(T-Tbase)+Lq
+
+    DO k = 1, nlp
+      DO i = 1, len
+        h(i, k) = gz(i, k) + cpn(i, k) * t(i, k)
+        hm(i, k) = gz(i, k) + cpx(i, k) * (t(i, k) - t(i, 1)) + lv(i, k) * q(i, k)
+      END DO
+    END DO
+
+  END SUBROUTINE cv_prelim
+
+  SUBROUTINE cv_feed(len, nd, t, q, qs, p, hm, gz, nk, icb, icbmax, iflag, tnk, &
+          qnk, gznk, plcl)
+    IMPLICIT NONE
+
+    ! ================================================================
+    ! Purpose: CONVECTIVE FEED
+    ! ================================================================
+
+
+    ! inputs:
+    INTEGER len, nd
+    REAL t(len, nd), q(len, nd), qs(len, nd), p(len, nd)
+    REAL hm(len, nd), gz(len, nd)
+
+    ! outputs:
+    INTEGER iflag(len), nk(len), icb(len), icbmax
+    REAL tnk(len), qnk(len), gznk(len), plcl(len)
+
+    ! local variables:
+    INTEGER i, k
+    INTEGER ihmin(len)
+    REAL work(len)
+    REAL pnk(len), qsnk(len), rh(len), chi(len)
+
+    ! -------------------------------------------------------------------
+    ! --- Find level of minimum moist static energy
+    ! --- If level of minimum moist static energy coincides with
+    ! --- or is lower than minimum allowable parcel origin level,
+    ! --- set iflag to 6.
+    ! -------------------------------------------------------------------
+
+    DO i = 1, len
+      work(i) = 1.0E12
+      ihmin(i) = nl
+    END DO
+    DO k = 2, nlp
+      DO i = 1, len
+        IF ((hm(i, k)<work(i)) .AND. (hm(i, k)<hm(i, k - 1))) THEN
+          work(i) = hm(i, k)
+          ihmin(i) = k
+        END IF
+      END DO
+    END DO
+    DO i = 1, len
+      ihmin(i) = min(ihmin(i), nlm)
+      IF (ihmin(i)<=minorig) THEN
+        iflag(i) = 6
+      END IF
+    END DO
+
+    ! -------------------------------------------------------------------
+    ! --- Find that model level below the level of minimum moist static
+    ! --- energy that has the maximum value of moist static energy
+    ! -------------------------------------------------------------------
+
+    DO i = 1, len
+      work(i) = hm(i, minorig)
+      nk(i) = minorig
+    END DO
+    DO k = minorig + 1, nl
+      DO i = 1, len
+        IF ((hm(i, k)>work(i)) .AND. (k<=ihmin(i))) THEN
+          work(i) = hm(i, k)
+          nk(i) = k
+        END IF
+      END DO
+    END DO
+    ! -------------------------------------------------------------------
+    ! --- Check whether parcel level temperature and specific humidity
+    ! --- are reasonable
+    ! -------------------------------------------------------------------
+    DO i = 1, len
+      IF (((t(i, nk(i))<250.0) .OR. (q(i, nk(i))<=0.0) .OR. (p(i, ihmin(i))< &
+              400.0)) .AND. (iflag(i)==0)) iflag(i) = 7
+    END DO
+    ! -------------------------------------------------------------------
+    ! --- Calculate lifted condensation level of air at parcel origin level
+    ! --- (Within 0.2% of formula of Bolton, MON. WEA. REV.,1980)
+    ! -------------------------------------------------------------------
+    DO i = 1, len
+      tnk(i) = t(i, nk(i))
+      qnk(i) = q(i, nk(i))
+      gznk(i) = gz(i, nk(i))
+      pnk(i) = p(i, nk(i))
+      qsnk(i) = qs(i, nk(i))
+
+      rh(i) = qnk(i) / qsnk(i)
+      rh(i) = min(1.0, rh(i))
+      chi(i) = tnk(i) / (1669.0 - 122.0 * rh(i) - tnk(i))
+      plcl(i) = pnk(i) * (rh(i)**chi(i))
+      IF (((plcl(i)<200.0) .OR. (plcl(i)>=2000.0)) .AND. (iflag(i)==0)) iflag(i &
+              ) = 8
+    END DO
+    ! -------------------------------------------------------------------
+    ! --- Calculate first level above lcl (=icb)
+    ! -------------------------------------------------------------------
+    DO i = 1, len
+      icb(i) = nlm
+    END DO
+
+    DO k = minorig, nl
+      DO i = 1, len
+        IF ((k>=(nk(i) + 1)) .AND. (p(i, k)<plcl(i))) icb(i) = min(icb(i), k)
+      END DO
+    END DO
+
+    DO i = 1, len
+      IF ((icb(i)>=nlm) .AND. (iflag(i)==0)) iflag(i) = 9
+    END DO
+
+    ! Compute icbmax.
+
+    icbmax = 2
+    DO i = 1, len
+      icbmax = max(icbmax, icb(i))
+    END DO
+
+  END SUBROUTINE cv_feed
+
+  SUBROUTINE cv_undilute1(len, nd, t, q, qs, gz, p, nk, icb, icbmax, tp, tvp, &
+          clw)
+    USE lmdz_cvthermo
+
+    IMPLICIT NONE
+
+
+    ! inputs:
+    INTEGER len, nd
+    INTEGER nk(len), icb(len), icbmax
+    REAL t(len, nd), q(len, nd), qs(len, nd), gz(len, nd)
+    REAL p(len, nd)
+
+    ! outputs:
+    REAL tp(len, nd), tvp(len, nd), clw(len, nd)
+
+    ! local variables:
+    INTEGER i, k
+    REAL tg, qg, alv, s, ahg, tc, denom, es, rg
+    REAL ah0(len), cpp(len)
+    REAL tnk(len), qnk(len), gznk(len), ticb(len), gzicb(len)
+
+    ! -------------------------------------------------------------------
+    ! --- Calculates the lifted parcel virtual temperature at nk,
+    ! --- the actual temperature, and the adiabatic
+    ! --- liquid water content. The procedure is to solve the equation.
+    ! cp*tp+L*qp+phi=cp*tnk+L*qnk+gznk.
+    ! -------------------------------------------------------------------
+
+    DO i = 1, len
+      tnk(i) = t(i, nk(i))
+      qnk(i) = q(i, nk(i))
+      gznk(i) = gz(i, nk(i))
+      ticb(i) = t(i, icb(i))
+      gzicb(i) = gz(i, icb(i))
+    END DO
+
+    ! ***  Calculate certain parcel quantities, including static energy   ***
+
+    DO i = 1, len
+      ah0(i) = (cpd * (1. - qnk(i)) + cl * qnk(i)) * tnk(i) + qnk(i) * (lv0 - clmcpv * (tnk(i) - &
+              273.15)) + gznk(i)
+      cpp(i) = cpd * (1. - qnk(i)) + qnk(i) * cpv
+    END DO
+
+    ! ***   Calculate lifted parcel quantities below cloud base   ***
+
+    DO k = minorig, icbmax - 1
+      DO i = 1, len
+        tp(i, k) = tnk(i) - (gz(i, k) - gznk(i)) / cpp(i)
+        tvp(i, k) = tp(i, k) * (1. + qnk(i) * epsi)
+      END DO
+    END DO
+
+    ! ***  Find lifted parcel quantities above cloud base    ***
+
+    DO i = 1, len
+      tg = ticb(i)
+      qg = qs(i, icb(i))
+      alv = lv0 - clmcpv * (ticb(i) - t0)
+
+      ! First iteration.
+
+      s = cpd + alv * alv * qg / (rrv * ticb(i) * ticb(i))
+      s = 1. / s
+      ahg = cpd * tg + (cl - cpd) * qnk(i) * ticb(i) + alv * qg + gzicb(i)
+      tg = tg + s * (ah0(i) - ahg)
+      tg = max(tg, 35.0)
+      tc = tg - t0
+      denom = 243.5 + tc
+      IF (tc>=0.0) THEN
+        es = 6.112 * exp(17.67 * tc / denom)
+      ELSE
+        es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
+      END IF
+      qg = eps * es / (p(i, icb(i)) - es * (1. - eps))
+
+      ! Second iteration.
+
+      s = cpd + alv * alv * qg / (rrv * ticb(i) * ticb(i))
+      s = 1. / s
+      ahg = cpd * tg + (cl - cpd) * qnk(i) * ticb(i) + alv * qg + gzicb(i)
+      tg = tg + s * (ah0(i) - ahg)
+      tg = max(tg, 35.0)
+      tc = tg - t0
+      denom = 243.5 + tc
+      IF (tc>=0.0) THEN
+        es = 6.112 * exp(17.67 * tc / denom)
+      ELSE
+        es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
+      END IF
+      qg = eps * es / (p(i, icb(i)) - es * (1. - eps))
+
+      alv = lv0 - clmcpv * (ticb(i) - 273.15)
+      tp(i, icb(i)) = (ah0(i) - (cl - cpd) * qnk(i) * ticb(i) - gz(i, icb(i)) - alv * qg) / cpd
+      clw(i, icb(i)) = qnk(i) - qg
+      clw(i, icb(i)) = max(0.0, clw(i, icb(i)))
+      rg = qg / (1. - qnk(i))
+      tvp(i, icb(i)) = tp(i, icb(i)) * (1. + rg * epsi)
+    END DO
+
+    DO k = minorig, icbmax
+      DO i = 1, len
+        tvp(i, k) = tvp(i, k) - tp(i, k) * qnk(i)
+      END DO
+    END DO
+
+  END SUBROUTINE cv_undilute1
+
+  SUBROUTINE cv_trigger(len, nd, icb, cbmf, tv, tvp, iflag)
+    IMPLICIT NONE
+
+    ! -------------------------------------------------------------------
+    ! --- Test for instability.
+    ! --- If there was no convection at last time step and parcel
+    ! --- is stable at icb, then set iflag to 4.
+    ! -------------------------------------------------------------------
+
+
+    ! inputs:
+    INTEGER len, nd, icb(len)
+    REAL cbmf(len), tv(len, nd), tvp(len, nd)
+
+    ! outputs:
+    INTEGER iflag(len) ! also an input
+
+    ! local variables:
+    INTEGER i
+
+    DO i = 1, len
+      IF ((cbmf(i)==0.0) .AND. (iflag(i)==0) .AND. (tvp(i, &
+              icb(i))<=(tv(i, icb(i)) - dtmax))) iflag(i) = 4
+    END DO
+
+  END SUBROUTINE cv_trigger
+
+  SUBROUTINE cv_compress(len, nloc, ncum, nd, iflag1, nk1, icb1, cbmf1, plcl1, &
+          tnk1, qnk1, gznk1, t1, q1, qs1, u1, v1, gz1, h1, lv1, cpn1, p1, ph1, tv1, &
+          tp1, tvp1, clw1, iflag, nk, icb, cbmf, plcl, tnk, qnk, gznk, t, q, qs, u, &
+          v, gz, h, lv, cpn, p, ph, tv, tp, tvp, clw, dph)
+    USE lmdz_print_control, ONLY: lunout
+    USE lmdz_abort_physic, ONLY: abort_physic
+    IMPLICIT NONE
+
+
+    ! inputs:
+    INTEGER len, ncum, nd, nloc
+    INTEGER iflag1(len), nk1(len), icb1(len)
+    REAL cbmf1(len), plcl1(len), tnk1(len), qnk1(len), gznk1(len)
+    REAL t1(len, nd), q1(len, nd), qs1(len, nd), u1(len, nd), v1(len, nd)
+    REAL gz1(len, nd), h1(len, nd), lv1(len, nd), cpn1(len, nd)
+    REAL p1(len, nd), ph1(len, nd + 1), tv1(len, nd), tp1(len, nd)
+    REAL tvp1(len, nd), clw1(len, nd)
+
+    ! outputs:
+    INTEGER iflag(nloc), nk(nloc), icb(nloc)
+    REAL cbmf(nloc), plcl(nloc), tnk(nloc), qnk(nloc), gznk(nloc)
+    REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd), u(nloc, nd), v(nloc, nd)
+    REAL gz(nloc, nd), h(nloc, nd), lv(nloc, nd), cpn(nloc, nd)
+    REAL p(nloc, nd), ph(nloc, nd + 1), tv(nloc, nd), tp(nloc, nd)
+    REAL tvp(nloc, nd), clw(nloc, nd)
+    REAL dph(nloc, nd)
+
+    ! local variables:
+    INTEGER i, k, nn
+    CHARACTER (LEN = 20) :: modname = 'cv_compress'
+    CHARACTER (LEN = 80) :: abort_message
+
+    DO k = 1, nl + 1
+      nn = 0
+      DO i = 1, len
+        IF (iflag1(i)==0) THEN
+          nn = nn + 1
+          t(nn, k) = t1(i, k)
+          q(nn, k) = q1(i, k)
+          qs(nn, k) = qs1(i, k)
+          u(nn, k) = u1(i, k)
+          v(nn, k) = v1(i, k)
+          gz(nn, k) = gz1(i, k)
+          h(nn, k) = h1(i, k)
+          lv(nn, k) = lv1(i, k)
+          cpn(nn, k) = cpn1(i, k)
+          p(nn, k) = p1(i, k)
+          ph(nn, k) = ph1(i, k)
+          tv(nn, k) = tv1(i, k)
+          tp(nn, k) = tp1(i, k)
+          tvp(nn, k) = tvp1(i, k)
+          clw(nn, k) = clw1(i, k)
+        END IF
+      END DO
+    END DO
+
+    IF (nn/=ncum) THEN
+      WRITE (lunout, *) 'strange! nn not equal to ncum: ', nn, ncum
+      abort_message = ''
+      CALL abort_physic(modname, abort_message, 1)
+    END IF
+
+    nn = 0
+    DO i = 1, len
+      IF (iflag1(i)==0) THEN
+        nn = nn + 1
+        cbmf(nn) = cbmf1(i)
+        plcl(nn) = plcl1(i)
+        tnk(nn) = tnk1(i)
+        qnk(nn) = qnk1(i)
+        gznk(nn) = gznk1(i)
+        nk(nn) = nk1(i)
+        icb(nn) = icb1(i)
+        iflag(nn) = iflag1(i)
+      END IF
+    END DO
+
+    DO k = 1, nl
+      DO i = 1, ncum
+        dph(i, k) = ph(i, k) - ph(i, k + 1)
+      END DO
+    END DO
+
+  END SUBROUTINE cv_compress
+
+  SUBROUTINE cv_undilute2(nloc, ncum, nd, icb, nk, tnk, qnk, gznk, t, q, qs, &
+          gz, p, dph, h, tv, lv, inb, inb1, tp, tvp, clw, hp, ep, sigp, frac)
+    USE lmdz_cvthermo
+
+    IMPLICIT NONE
+
+    ! ---------------------------------------------------------------------
+    ! Purpose:
+    ! FIND THE REST OF THE LIFTED PARCEL TEMPERATURES
+    ! &
+    ! COMPUTE THE PRECIPITATION EFFICIENCIES AND THE
+    ! FRACTION OF PRECIPITATION FALLING OUTSIDE OF CLOUD
+    ! &
+    ! FIND THE LEVEL OF NEUTRAL BUOYANCY
+    ! ---------------------------------------------------------------------
+
+
+    ! inputs:
+    INTEGER ncum, nd, nloc
+    INTEGER icb(nloc), nk(nloc)
+    REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd), gz(nloc, nd)
+    REAL p(nloc, nd), dph(nloc, nd)
+    REAL tnk(nloc), qnk(nloc), gznk(nloc)
+    REAL lv(nloc, nd), tv(nloc, nd), h(nloc, nd)
+
+    ! outputs:
+    INTEGER inb(nloc), inb1(nloc)
+    REAL tp(nloc, nd), tvp(nloc, nd), clw(nloc, nd)
+    REAL ep(nloc, nd), sigp(nloc, nd), hp(nloc, nd)
+    REAL frac(nloc)
+
+    ! local variables:
+    INTEGER i, k
+    REAL tg, qg, ahg, alv, s, tc, es, denom, rg, tca, elacrit
+    REAL by, defrac
+    REAL ah0(nloc), cape(nloc), capem(nloc), byp(nloc)
+    LOGICAL lcape(nloc)
+
+    ! =====================================================================
+    ! --- SOME INITIALIZATIONS
+    ! =====================================================================
+
+    DO k = 1, nl
+      DO i = 1, ncum
+        ep(i, k) = 0.0
+        sigp(i, k) = sigs
+      END DO
+    END DO
+
+    ! =====================================================================
+    ! --- FIND THE REST OF THE LIFTED PARCEL TEMPERATURES
+    ! =====================================================================
+
+    ! ---       The procedure is to solve the equation.
+    ! cp*tp+L*qp+phi=cp*tnk+L*qnk+gznk.
+
+    ! ***  Calculate certain parcel quantities, including static energy   ***
+
+    DO i = 1, ncum
+      ah0(i) = (cpd * (1. - qnk(i)) + cl * qnk(i)) * tnk(i) + qnk(i) * (lv0 - clmcpv * (tnk(i) - &
+              t0)) + gznk(i)
+    END DO
+
+
+    ! ***  Find lifted parcel quantities above cloud base    ***
+
+    DO k = minorig + 1, nl
+      DO i = 1, ncum
+        IF (k>=(icb(i) + 1)) THEN
+          tg = t(i, k)
+          qg = qs(i, k)
+          alv = lv0 - clmcpv * (t(i, k) - t0)
+
+          ! First iteration.
+
+          s = cpd + alv * alv * qg / (rrv * t(i, k) * t(i, k))
+          s = 1. / s
+          ahg = cpd * tg + (cl - cpd) * qnk(i) * t(i, k) + alv * qg + gz(i, k)
+          tg = tg + s * (ah0(i) - ahg)
+          tg = max(tg, 35.0)
+          tc = tg - t0
+          denom = 243.5 + tc
+          IF (tc>=0.0) THEN
+            es = 6.112 * exp(17.67 * tc / denom)
+          ELSE
+            es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
+          END IF
+          qg = eps * es / (p(i, k) - es * (1. - eps))
+
+          ! Second iteration.
+
+          s = cpd + alv * alv * qg / (rrv * t(i, k) * t(i, k))
+          s = 1. / s
+          ahg = cpd * tg + (cl - cpd) * qnk(i) * t(i, k) + alv * qg + gz(i, k)
+          tg = tg + s * (ah0(i) - ahg)
+          tg = max(tg, 35.0)
+          tc = tg - t0
+          denom = 243.5 + tc
+          IF (tc>=0.0) THEN
+            es = 6.112 * exp(17.67 * tc / denom)
+          ELSE
+            es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
+          END IF
+          qg = eps * es / (p(i, k) - es * (1. - eps))
+
+          alv = lv0 - clmcpv * (t(i, k) - t0)
+          ! PRINT*,'cpd dans convect2 ',cpd
+          ! PRINT*,'tp(i,k),ah0(i),cl,cpd,qnk(i),t(i,k),gz(i,k),alv,qg,cpd'
+          ! PRINT*,tp(i,k),ah0(i),cl,cpd,qnk(i),t(i,k),gz(i,k),alv,qg,cpd
+          tp(i, k) = (ah0(i) - (cl - cpd) * qnk(i) * t(i, k) - gz(i, k) - alv * qg) / cpd
+          ! if (.NOT.cpd.gt.1000.) THEN
+          ! PRINT*,'CPD=',cpd
+          ! stop
+          ! END IF
+          clw(i, k) = qnk(i) - qg
+          clw(i, k) = max(0.0, clw(i, k))
+          rg = qg / (1. - qnk(i))
+          tvp(i, k) = tp(i, k) * (1. + rg * epsi)
+        END IF
+      END DO
+    END DO
+
+    ! =====================================================================
+    ! --- SET THE PRECIPITATION EFFICIENCIES AND THE FRACTION OF
+    ! --- PRECIPITATION FALLING OUTSIDE OF CLOUD
+    ! --- THESE MAY BE FUNCTIONS OF TP(I), P(I) AND CLW(I)
+    ! =====================================================================
+
+    DO k = minorig + 1, nl
+      DO i = 1, ncum
+        IF (k>=(nk(i) + 1)) THEN
+          tca = tp(i, k) - t0
+          IF (tca>=0.0) THEN
+            elacrit = elcrit
+          ELSE
+            elacrit = elcrit * (1.0 - tca / tlcrit)
+          END IF
+          elacrit = max(elacrit, 0.0)
+          ep(i, k) = 1.0 - elacrit / max(clw(i, k), 1.0E-8)
+          ep(i, k) = max(ep(i, k), 0.0)
+          ep(i, k) = min(ep(i, k), 1.0)
+          sigp(i, k) = sigs
+        END IF
+      END DO
+    END DO
+
+    ! =====================================================================
+    ! --- CALCULATE VIRTUAL TEMPERATURE AND LIFTED PARCEL
+    ! --- VIRTUAL TEMPERATURE
+    ! =====================================================================
+
+    DO k = minorig + 1, nl
+      DO i = 1, ncum
+        IF (k>=(icb(i) + 1)) THEN
+          tvp(i, k) = tvp(i, k) * (1.0 - qnk(i) + ep(i, k) * clw(i, k))
+          ! PRINT*,'i,k,tvp(i,k),qnk(i),ep(i,k),clw(i,k)'
+          ! PRINT*, i,k,tvp(i,k),qnk(i),ep(i,k),clw(i,k)
+        END IF
+      END DO
+    END DO
+    DO i = 1, ncum
+      tvp(i, nlp) = tvp(i, nl) - (gz(i, nlp) - gz(i, nl)) / cpd
+    END DO
+
+    ! =====================================================================
+    ! --- FIND THE FIRST MODEL LEVEL (INB1) ABOVE THE PARCEL'S
+    ! --- HIGHEST LEVEL OF NEUTRAL BUOYANCY
+    ! --- AND THE HIGHEST LEVEL OF POSITIVE CAPE (INB)
+    ! =====================================================================
+
+    DO i = 1, ncum
+      cape(i) = 0.0
+      capem(i) = 0.0
+      inb(i) = icb(i) + 1
+      inb1(i) = inb(i)
+    END DO
+
+    ! Originial Code
+
+    ! do 530 k=minorig+1,nl-1
+    ! do 520 i=1,ncum
+    ! IF(k.ge.(icb(i)+1))THEN
+    ! by=(tvp(i,k)-tv(i,k))*dph(i,k)/p(i,k)
+    ! byp=(tvp(i,k+1)-tv(i,k+1))*dph(i,k+1)/p(i,k+1)
+    ! cape(i)=cape(i)+by
+    ! IF(by.ge.0.0)inb1(i)=k+1
+    ! IF(cape(i).gt.0.0)THEN
+    ! inb(i)=k+1
+    ! capem(i)=cape(i)
+    ! END IF
+    ! END IF
+    ! 520    continue
+    ! 530  continue
+    ! do 540 i=1,ncum
+    ! byp=(tvp(i,nl)-tv(i,nl))*dph(i,nl)/p(i,nl)
+    ! cape(i)=capem(i)+byp
+    ! defrac=capem(i)-cape(i)
+    ! defrac=max(defrac,0.001)
+    ! frac(i)=-cape(i)/defrac
+    ! frac(i)=min(frac(i),1.0)
+    ! frac(i)=max(frac(i),0.0)
+    ! 540   continue
+
+    ! K Emanuel fix
+
+    ! CALL zilch(byp,ncum)
+    ! do 530 k=minorig+1,nl-1
+    ! do 520 i=1,ncum
+    ! IF(k.ge.(icb(i)+1))THEN
+    ! by=(tvp(i,k)-tv(i,k))*dph(i,k)/p(i,k)
+    ! cape(i)=cape(i)+by
+    ! IF(by.ge.0.0)inb1(i)=k+1
+    ! IF(cape(i).gt.0.0)THEN
+    ! inb(i)=k+1
+    ! capem(i)=cape(i)
+    ! byp(i)=(tvp(i,k+1)-tv(i,k+1))*dph(i,k+1)/p(i,k+1)
+    ! END IF
+    ! END IF
+    ! 520    continue
+    ! 530  continue
+    ! do 540 i=1,ncum
+    ! inb(i)=max(inb(i),inb1(i))
+    ! cape(i)=capem(i)+byp(i)
+    ! defrac=capem(i)-cape(i)
+    ! defrac=max(defrac,0.001)
+    ! frac(i)=-cape(i)/defrac
+    ! frac(i)=min(frac(i),1.0)
+    ! frac(i)=max(frac(i),0.0)
+    ! 540   continue
+
+    ! J Teixeira fix
+
+    CALL zilch(byp, ncum)
+    DO i = 1, ncum
+      lcape(i) = .TRUE.
+    END DO
+    DO k = minorig + 1, nl - 1
+      DO i = 1, ncum
+        IF (cape(i)<0.0) lcape(i) = .FALSE.
+        IF ((k>=(icb(i) + 1)) .AND. lcape(i)) THEN
+          by = (tvp(i, k) - tv(i, k)) * dph(i, k) / p(i, k)
+          byp(i) = (tvp(i, k + 1) - tv(i, k + 1)) * dph(i, k + 1) / p(i, k + 1)
+          cape(i) = cape(i) + by
+          IF (by>=0.0) inb1(i) = k + 1
+          IF (cape(i)>0.0) THEN
+            inb(i) = k + 1
+            capem(i) = cape(i)
+          END IF
+        END IF
+      END DO
+    END DO
+    DO i = 1, ncum
+      cape(i) = capem(i) + byp(i)
+      defrac = capem(i) - cape(i)
+      defrac = max(defrac, 0.001)
+      frac(i) = -cape(i) / defrac
+      frac(i) = min(frac(i), 1.0)
+      frac(i) = max(frac(i), 0.0)
+    END DO
+
+    ! =====================================================================
+    ! ---   CALCULATE LIQUID WATER STATIC ENERGY OF LIFTED PARCEL
+    ! =====================================================================
+
+    ! initialization:
+    DO i = 1, ncum * nlp
+      hp(i, 1) = h(i, 1)
+    END DO
+
+    DO k = minorig + 1, nl
+      DO i = 1, ncum
+        IF ((k>=icb(i)) .AND. (k<=inb(i))) THEN
+          hp(i, k) = h(i, nk(i)) + (lv(i, k) + (cpd - cpv) * t(i, k)) * ep(i, k) * clw(i, k &
+                  )
+        END IF
+      END DO
+    END DO
+
+  END SUBROUTINE cv_undilute2
+
+  SUBROUTINE cv_closure(nloc, ncum, nd, nk, icb, tv, tvp, p, ph, dph, plcl, &
+          cpn, iflag, cbmf)
+    USE lmdz_cvthermo
+
+    IMPLICIT NONE
+
+    ! inputs:
+    INTEGER ncum, nd, nloc
+    INTEGER nk(nloc), icb(nloc)
+    REAL tv(nloc, nd), tvp(nloc, nd), p(nloc, nd), dph(nloc, nd)
+    REAL ph(nloc, nd + 1) ! caution nd instead ndp1 to be consistent...
+    REAL plcl(nloc), cpn(nloc, nd)
+
+    ! outputs:
+    INTEGER iflag(nloc)
+    REAL cbmf(nloc) ! also an input
+
+    ! local variables:
+    INTEGER i, k, icbmax
+    REAL dtpbl(nloc), dtmin(nloc), tvpplcl(nloc), tvaplcl(nloc)
+    REAL work(nloc)
+
+
+    ! -------------------------------------------------------------------
+    ! Compute icbmax.
+    ! -------------------------------------------------------------------
+
+    icbmax = 2
+    DO i = 1, ncum
+      icbmax = max(icbmax, icb(i))
+    END DO
+
+    ! =====================================================================
+    ! ---  CALCULATE CLOUD BASE MASS FLUX
+    ! =====================================================================
+
+    ! tvpplcl = parcel temperature lifted adiabatically from level
+    ! icb-1 to the LCL.
+    ! tvaplcl = virtual temperature at the LCL.
+
+    DO i = 1, ncum
+      dtpbl(i) = 0.0
+      tvpplcl(i) = tvp(i, icb(i) - 1) - rrd * tvp(i, icb(i) - 1) * (p(i, icb(i) - 1) - plcl(&
+              i)) / (cpn(i, icb(i) - 1) * p(i, icb(i) - 1))
+      tvaplcl(i) = tv(i, icb(i)) + (tvp(i, icb(i)) - tvp(i, icb(i) + 1)) * (plcl(i) - p(i &
+              , icb(i))) / (p(i, icb(i)) - p(i, icb(i) + 1))
+    END DO
+
+    ! -------------------------------------------------------------------
+    ! --- Interpolate difference between lifted parcel and
+    ! --- environmental temperatures to lifted condensation level
+    ! -------------------------------------------------------------------
+
+    ! dtpbl = average of tvp-tv in the PBL (k=nk to icb-1).
+
+    DO k = minorig, icbmax
+      DO i = 1, ncum
+        IF ((k>=nk(i)) .AND. (k<=(icb(i) - 1))) THEN
+          dtpbl(i) = dtpbl(i) + (tvp(i, k) - tv(i, k)) * dph(i, k)
+        END IF
+      END DO
+    END DO
+    DO i = 1, ncum
+      dtpbl(i) = dtpbl(i) / (ph(i, nk(i)) - ph(i, icb(i)))
+      dtmin(i) = tvpplcl(i) - tvaplcl(i) + dtmax + dtpbl(i)
+    END DO
+
+    ! -------------------------------------------------------------------
+    ! --- Adjust cloud base mass flux
+    ! -------------------------------------------------------------------
+
+    DO i = 1, ncum
+      work(i) = cbmf(i)
+      cbmf(i) = max(0.0, (1.0 - damp) * cbmf(i) + 0.1 * alpha * dtmin(i))
+      IF ((work(i)==0.0) .AND. (cbmf(i)==0.0)) THEN
+        iflag(i) = 3
+      END IF
+    END DO
+
+  END SUBROUTINE cv_closure
+
+  SUBROUTINE cv_mixing(nloc, ncum, nd, icb, nk, inb, inb1, ph, t, q, qs, u, v, &
+          h, lv, qnk, hp, tv, tvp, ep, clw, cbmf, m, ment, qent, uent, vent, nent, &
+          sij, elij)
+    USE lmdz_cvthermo
+
+    IMPLICIT NONE
+
+
+    ! inputs:
+    INTEGER ncum, nd, nloc
+    INTEGER icb(nloc), inb(nloc), inb1(nloc), nk(nloc)
+    REAL cbmf(nloc), qnk(nloc)
+    REAL ph(nloc, nd + 1)
+    REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd), lv(nloc, nd)
+    REAL u(nloc, nd), v(nloc, nd), h(nloc, nd), hp(nloc, nd)
+    REAL tv(nloc, nd), tvp(nloc, nd), ep(nloc, nd), clw(nloc, nd)
+
+    ! outputs:
+    INTEGER nent(nloc, nd)
+    REAL m(nloc, nd), ment(nloc, nd, nd), qent(nloc, nd, nd)
+    REAL uent(nloc, nd, nd), vent(nloc, nd, nd)
+    REAL sij(nloc, nd, nd), elij(nloc, nd, nd)
+
+    ! local variables:
+    INTEGER i, j, k, ij
+    INTEGER num1, num2
+    REAL dbo, qti, bf2, anum, denom, dei, altem, cwat, stemp
+    REAL alt, qp1, smid, sjmin, sjmax, delp, delm
+    REAL work(nloc), asij(nloc), smin(nloc), scrit(nloc)
+    REAL bsum(nloc, nd)
+    LOGICAL lwork(nloc)
+
+    ! =====================================================================
+    ! --- INITIALIZE VARIOUS ARRAYS USED IN THE COMPUTATIONS
+    ! =====================================================================
+
+    DO i = 1, ncum * nlp
+      nent(i, 1) = 0
+      m(i, 1) = 0.0
+    END DO
+
+    DO k = 1, nlp
+      DO j = 1, nlp
+        DO i = 1, ncum
+          qent(i, k, j) = q(i, j)
+          uent(i, k, j) = u(i, j)
+          vent(i, k, j) = v(i, j)
+          elij(i, k, j) = 0.0
+          ment(i, k, j) = 0.0
+          sij(i, k, j) = 0.0
+        END DO
+      END DO
+    END DO
+
+    ! -------------------------------------------------------------------
+    ! --- Calculate rates of mixing,  m(i)
+    ! -------------------------------------------------------------------
+
+    CALL zilch(work, ncum)
+
+    DO j = minorig + 1, nl
+      DO i = 1, ncum
+        IF ((j>=(icb(i) + 1)) .AND. (j<=inb(i))) THEN
+          k = min(j, inb1(i))
+          dbo = abs(tv(i, k + 1) - tvp(i, k + 1) - tv(i, k - 1) + tvp(i, k - 1)) + &
+                  entp * 0.04 * (ph(i, k) - ph(i, k + 1))
+          work(i) = work(i) + dbo
+          m(i, j) = cbmf(i) * dbo
+        END IF
+      END DO
+    END DO
+    DO k = minorig + 1, nl
+      DO i = 1, ncum
+        IF ((k>=(icb(i) + 1)) .AND. (k<=inb(i))) THEN
+          m(i, k) = m(i, k) / work(i)
+        END IF
+      END DO
+    END DO
+
+
+    ! =====================================================================
+    ! --- CALCULATE ENTRAINED AIR MASS FLUX (ment), TOTAL WATER MIXING
+    ! --- RATIO (QENT), TOTAL CONDENSED WATER (elij), AND MIXING
+    ! --- FRACTION (sij)
+    ! =====================================================================
+
+    DO i = minorig + 1, nl
+      DO j = minorig + 1, nl
+        DO ij = 1, ncum
+          IF ((i>=(icb(ij) + 1)) .AND. (j>=icb(ij)) .AND. (i<=inb(ij)) .AND. (j<= &
+                  inb(ij))) THEN
+            qti = qnk(ij) - ep(ij, i) * clw(ij, i)
+            bf2 = 1. + lv(ij, j) * lv(ij, j) * qs(ij, j) / (rrv * t(ij, j) * t(ij, j) * cpd)
+            anum = h(ij, j) - hp(ij, i) + (cpv - cpd) * t(ij, j) * (qti - q(ij, j))
+            denom = h(ij, i) - hp(ij, i) + (cpd - cpv) * (q(ij, i) - qti) * t(ij, j)
+            dei = denom
+            IF (abs(dei)<0.01) dei = 0.01
+            sij(ij, i, j) = anum / dei
+            sij(ij, i, i) = 1.0
+            altem = sij(ij, i, j) * q(ij, i) + (1. - sij(ij, i, j)) * qti - qs(ij, j)
+            altem = altem / bf2
+            cwat = clw(ij, j) * (1. - ep(ij, j))
+            stemp = sij(ij, i, j)
+            IF ((stemp<0.0 .OR. stemp>1.0 .OR. altem>cwat) .AND. j>i) THEN
+              anum = anum - lv(ij, j) * (qti - qs(ij, j) - cwat * bf2)
+              denom = denom + lv(ij, j) * (q(ij, i) - qti)
+              IF (abs(denom)<0.01) denom = 0.01
+              sij(ij, i, j) = anum / denom
+              altem = sij(ij, i, j) * q(ij, i) + (1. - sij(ij, i, j)) * qti - qs(ij, j)
+              altem = altem - (bf2 - 1.) * cwat
+            END IF
+            IF (sij(ij, i, j)>0.0 .AND. sij(ij, i, j)<0.9) THEN
+              qent(ij, i, j) = sij(ij, i, j) * q(ij, i) + (1. - sij(ij, i, j)) * qti
+              uent(ij, i, j) = sij(ij, i, j) * u(ij, i) + &
+                      (1. - sij(ij, i, j)) * u(ij, nk(ij))
+              vent(ij, i, j) = sij(ij, i, j) * v(ij, i) + &
+                      (1. - sij(ij, i, j)) * v(ij, nk(ij))
+              elij(ij, i, j) = altem
+              elij(ij, i, j) = max(0.0, elij(ij, i, j))
+              ment(ij, i, j) = m(ij, i) / (1. - sij(ij, i, j))
+              nent(ij, i) = nent(ij, i) + 1
+            END IF
+            sij(ij, i, j) = max(0.0, sij(ij, i, j))
+            sij(ij, i, j) = min(1.0, sij(ij, i, j))
+          END IF
+        END DO
+      END DO
+
+      ! ***   If no air can entrain at level i assume that updraft detrains
+      ! ***
+      ! ***   at that level and calculate detrained air flux and properties
+      ! ***
+
+      DO ij = 1, ncum
+        IF ((i>=(icb(ij) + 1)) .AND. (i<=inb(ij)) .AND. (nent(ij, i)==0)) THEN
+          ment(ij, i, i) = m(ij, i)
+          qent(ij, i, i) = q(ij, nk(ij)) - ep(ij, i) * clw(ij, i)
+          uent(ij, i, i) = u(ij, nk(ij))
+          vent(ij, i, i) = v(ij, nk(ij))
+          elij(ij, i, i) = clw(ij, i)
+          sij(ij, i, i) = 1.0
+        END IF
+      END DO
+    END DO
+
+    DO i = 1, ncum
+      sij(i, inb(i), inb(i)) = 1.0
+    END DO
+
+    ! =====================================================================
+    ! ---  NORMALIZE ENTRAINED AIR MASS FLUXES
+    ! ---  TO REPRESENT EQUAL PROBABILITIES OF MIXING
+    ! =====================================================================
+
+    CALL zilch(bsum, ncum * nlp)
+    DO ij = 1, ncum
+      lwork(ij) = .FALSE.
+    END DO
+    DO i = minorig + 1, nl
+
+      num1 = 0
+      DO ij = 1, ncum
+        IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij))) num1 = num1 + 1
+      END DO
+      IF (num1<=0) GO TO 789
+
+      DO ij = 1, ncum
+        IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij))) THEN
+          lwork(ij) = (nent(ij, i)/=0)
+          qp1 = q(ij, nk(ij)) - ep(ij, i) * clw(ij, i)
+          anum = h(ij, i) - hp(ij, i) - lv(ij, i) * (qp1 - qs(ij, i))
+          denom = h(ij, i) - hp(ij, i) + lv(ij, i) * (q(ij, i) - qp1)
+          IF (abs(denom)<0.01) denom = 0.01
+          scrit(ij) = anum / denom
+          alt = qp1 - qs(ij, i) + scrit(ij) * (q(ij, i) - qp1)
+          IF (scrit(ij)<0.0 .OR. alt<0.0) scrit(ij) = 1.0
+          asij(ij) = 0.0
+          smin(ij) = 1.0
+        END IF
+      END DO
+      DO j = minorig, nl
+
+        num2 = 0
+        DO ij = 1, ncum
+          IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (j>=icb(&
+                  ij)) .AND. (j<=inb(ij)) .AND. lwork(ij)) num2 = num2 + 1
+        END DO
+        IF (num2<=0) GO TO 783
+
+        DO ij = 1, ncum
+          IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (j>=icb(&
+                  ij)) .AND. (j<=inb(ij)) .AND. lwork(ij)) THEN
+            IF (sij(ij, i, j)>0.0 .AND. sij(ij, i, j)<0.9) THEN
+              IF (j>i) THEN
+                smid = min(sij(ij, i, j), scrit(ij))
+                sjmax = smid
+                sjmin = smid
+                IF (smid<smin(ij) .AND. sij(ij, i, j + 1)<smid) THEN
+                  smin(ij) = smid
+                  sjmax = min(sij(ij, i, j + 1), sij(ij, i, j), scrit(ij))
+                  sjmin = max(sij(ij, i, j - 1), sij(ij, i, j))
+                  sjmin = min(sjmin, scrit(ij))
+                END IF
+              ELSE
+                sjmax = max(sij(ij, i, j + 1), scrit(ij))
+                smid = max(sij(ij, i, j), scrit(ij))
+                sjmin = 0.0
+                IF (j>1) sjmin = sij(ij, i, j - 1)
+                sjmin = max(sjmin, scrit(ij))
+              END IF
+              delp = abs(sjmax - smid)
+              delm = abs(sjmin - smid)
+              asij(ij) = asij(ij) + (delp + delm) * (ph(ij, j) - ph(ij, j + 1))
+              ment(ij, i, j) = ment(ij, i, j) * (delp + delm) * (ph(ij, j) - ph(ij, j + 1))
+            END IF
+          END IF
+        END DO
+      783 END DO
+      DO ij = 1, ncum
+        IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. lwork(ij)) THEN
+          asij(ij) = max(1.0E-21, asij(ij))
+          asij(ij) = 1.0 / asij(ij)
+          bsum(ij, i) = 0.0
+        END IF
+      END DO
+      DO j = minorig, nl + 1
+        DO ij = 1, ncum
+          IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (j>=icb(&
+                  ij)) .AND. (j<=inb(ij)) .AND. lwork(ij)) THEN
+            ment(ij, i, j) = ment(ij, i, j) * asij(ij)
+            bsum(ij, i) = bsum(ij, i) + ment(ij, i, j)
+          END IF
+        END DO
+      END DO
+      DO ij = 1, ncum
+        IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (bsum(ij, &
+                i)<1.0E-18) .AND. lwork(ij)) THEN
+          nent(ij, i) = 0
+          ment(ij, i, i) = m(ij, i)
+          qent(ij, i, i) = q(ij, nk(ij)) - ep(ij, i) * clw(ij, i)
+          uent(ij, i, i) = u(ij, nk(ij))
+          vent(ij, i, i) = v(ij, nk(ij))
+          elij(ij, i, i) = clw(ij, i)
+          sij(ij, i, i) = 1.0
+        END IF
+      END DO
+    789 END DO
+
+  END SUBROUTINE cv_mixing
+
+  SUBROUTINE cv_unsat(nloc, ncum, nd, inb, t, q, qs, gz, u, v, p, ph, h, lv, &
+          ep, sigp, clw, m, ment, elij, iflag, mp, qp, up, vp, wt, water, evap)
+    USE lmdz_cvthermo
+
+    IMPLICIT NONE
+
+
+    ! inputs:
+    INTEGER ncum, nd, nloc
+    INTEGER inb(nloc)
+    REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd)
+    REAL gz(nloc, nd), u(nloc, nd), v(nloc, nd)
+    REAL p(nloc, nd), ph(nloc, nd + 1), h(nloc, nd)
+    REAL lv(nloc, nd), ep(nloc, nd), sigp(nloc, nd), clw(nloc, nd)
+    REAL m(nloc, nd), ment(nloc, nd, nd), elij(nloc, nd, nd)
+
+    ! outputs:
+    INTEGER iflag(nloc) ! also an input
+    REAL mp(nloc, nd), qp(nloc, nd), up(nloc, nd), vp(nloc, nd)
+    REAL water(nloc, nd), evap(nloc, nd), wt(nloc, nd)
+
+    ! local variables:
+    INTEGER i, j, k, ij, num1
+    INTEGER jtt(nloc)
+    REAL awat, coeff, qsm, afac, sigt, b6, c6, revap
+    REAL dhdp, fac, qstm, rat
+    REAL wdtrain(nloc)
+    LOGICAL lwork(nloc)
+
+    ! =====================================================================
+    ! --- PRECIPITATING DOWNDRAFT CALCULATION
+    ! =====================================================================
+
+    ! Initializations:
+
+    DO i = 1, ncum
+      DO k = 1, nl + 1
+        wt(i, k) = omtsnow
+        mp(i, k) = 0.0
+        evap(i, k) = 0.0
+        water(i, k) = 0.0
+      END DO
+    END DO
+
+    DO i = 1, ncum
+      qp(i, 1) = q(i, 1)
+      up(i, 1) = u(i, 1)
+      vp(i, 1) = v(i, 1)
+    END DO
+
+    DO k = 2, nl + 1
+      DO i = 1, ncum
+        qp(i, k) = q(i, k - 1)
+        up(i, k) = u(i, k - 1)
+        vp(i, k) = v(i, k - 1)
+      END DO
+    END DO
+
+
+    ! ***  Check whether ep(inb)=0, if so, skip precipitating    ***
+    ! ***             downdraft calculation                      ***
+
+
+    ! ***  Integrate liquid water equation to find condensed water   ***
+    ! ***                and condensed water flux                    ***
+
+    DO i = 1, ncum
+      jtt(i) = 2
+      IF (ep(i, inb(i))<=0.0001) iflag(i) = 2
+      IF (iflag(i)==0) THEN
+        lwork(i) = .TRUE.
+      ELSE
+        lwork(i) = .FALSE.
+      END IF
+    END DO
+
+    ! ***                    Begin downdraft loop                    ***
+
+    CALL zilch(wdtrain, ncum)
+    DO i = nl + 1, 1, -1
+
+      num1 = 0
+      DO ij = 1, ncum
+        IF ((i<=inb(ij)) .AND. lwork(ij)) num1 = num1 + 1
+      END DO
+      IF (num1<=0) GO TO 899
+
+
+      ! ***        Calculate detrained precipitation             ***
+
+      DO ij = 1, ncum
+        IF ((i<=inb(ij)) .AND. (lwork(ij))) THEN
+          wdtrain(ij) = g * ep(ij, i) * m(ij, i) * clw(ij, i)
+        END IF
+      END DO
+
+      IF (i>1) THEN
+        DO j = 1, i - 1
+          DO ij = 1, ncum
+            IF ((i<=inb(ij)) .AND. (lwork(ij))) THEN
+              awat = elij(ij, j, i) - (1. - ep(ij, i)) * clw(ij, i)
+              awat = max(0.0, awat)
+              wdtrain(ij) = wdtrain(ij) + g * awat * ment(ij, j, i)
+            END IF
+          END DO
+        END DO
+      END IF
+
+      ! ***    Find rain water and evaporation using provisional   ***
+      ! ***              estimates of qp(i)and qp(i-1)             ***
+
+
+      ! ***  Value of terminal velocity and coeffecient of evaporation for snow
+      ! ***
+
+      DO ij = 1, ncum
+        IF ((i<=inb(ij)) .AND. (lwork(ij))) THEN
+          coeff = coeffs
+          wt(ij, i) = omtsnow
+
+          ! ***  Value of terminal velocity and coeffecient of evaporation for
+          ! rain   ***
+
+          IF (t(ij, i)>273.0) THEN
+            coeff = coeffr
+            wt(ij, i) = omtrain
+          END IF
+          qsm = 0.5 * (q(ij, i) + qp(ij, i + 1))
+          afac = coeff * ph(ij, i) * (qs(ij, i) - qsm) / (1.0E4 + 2.0E3 * ph(ij, i) * qs(ij, i))
+          afac = max(afac, 0.0)
+          sigt = sigp(ij, i)
+          sigt = max(0.0, sigt)
+          sigt = min(1.0, sigt)
+          b6 = 100. * (ph(ij, i) - ph(ij, i + 1)) * sigt * afac / wt(ij, i)
+          c6 = (water(ij, i + 1) * wt(ij, i + 1) + wdtrain(ij) / sigd) / wt(ij, i)
+          revap = 0.5 * (-b6 + sqrt(b6 * b6 + 4. * c6))
+          evap(ij, i) = sigt * afac * revap
+          water(ij, i) = revap * revap
+
+          ! ***  Calculate precipitating downdraft mass flux under     ***
+          ! ***              hydrostatic approximation                 ***
+
+          IF (i>1) THEN
+            dhdp = (h(ij, i) - h(ij, i - 1)) / (p(ij, i - 1) - p(ij, i))
+            dhdp = max(dhdp, 10.0)
+            mp(ij, i) = 100. * ginv * lv(ij, i) * sigd * evap(ij, i) / dhdp
+            mp(ij, i) = max(mp(ij, i), 0.0)
+
+            ! ***   Add small amount of inertia to downdraft              ***
+
+            fac = 20.0 / (ph(ij, i - 1) - ph(ij, i))
+            mp(ij, i) = (fac * mp(ij, i + 1) + mp(ij, i)) / (1. + fac)
+
+            ! ***      Force mp to decrease linearly to zero
+            ! ***
+            ! ***      between about 950 mb and the surface
+            ! ***
+
+            IF (p(ij, i)>(0.949 * p(ij, 1))) THEN
+              jtt(ij) = max(jtt(ij), i)
+              mp(ij, i) = mp(ij, jtt(ij)) * (p(ij, 1) - p(ij, i)) / &
+                      (p(ij, 1) - p(ij, jtt(ij)))
+            END IF
+          END IF
+
+          ! ***       Find mixing ratio of precipitating downdraft     ***
+
+          IF (i/=inb(ij)) THEN
+            IF (i==1) THEN
+              qstm = qs(ij, 1)
+            ELSE
+              qstm = qs(ij, i - 1)
+            END IF
+            IF (mp(ij, i)>mp(ij, i + 1)) THEN
+              rat = mp(ij, i + 1) / mp(ij, i)
+              qp(ij, i) = qp(ij, i + 1) * rat + q(ij, i) * (1.0 - rat) + &
+                      100. * ginv * sigd * (ph(ij, i) - ph(ij, i + 1)) * (evap(ij, i) / mp(ij, i))
+              up(ij, i) = up(ij, i + 1) * rat + u(ij, i) * (1. - rat)
+              vp(ij, i) = vp(ij, i + 1) * rat + v(ij, i) * (1. - rat)
+            ELSE
+              IF (mp(ij, i + 1)>0.0) THEN
+                qp(ij, i) = (gz(ij, i + 1) - gz(ij, i) + qp(ij, i + 1) * (lv(ij, i + 1) + t(ij, &
+                        i + 1) * (cl - cpd)) + cpd * (t(ij, i + 1) - t(ij, &
+                        i))) / (lv(ij, i) + t(ij, i) * (cl - cpd))
+                up(ij, i) = up(ij, i + 1)
+                vp(ij, i) = vp(ij, i + 1)
+              END IF
+            END IF
+            qp(ij, i) = min(qp(ij, i), qstm)
+            qp(ij, i) = max(qp(ij, i), 0.0)
+          END IF
+        END IF
+      END DO
+    899 END DO
+
+  END SUBROUTINE cv_unsat
+
+  SUBROUTINE cv_yield(nloc, ncum, nd, nk, icb, inb, delt, t, q, u, v, gz, p, &
+          ph, h, hp, lv, cpn, ep, clw, frac, m, mp, qp, up, vp, wt, water, evap, &
+          ment, qent, uent, vent, nent, elij, tv, tvp, iflag, wd, qprime, tprime, &
+          precip, cbmf, ft, fq, fu, fv, ma, qcondc)
+    USE lmdz_cvthermo
+
+    IMPLICIT NONE
+
+
+    ! inputs
+    INTEGER ncum, nd, nloc
+    INTEGER nk(nloc), icb(nloc), inb(nloc)
+    INTEGER nent(nloc, nd)
+    REAL delt
+    REAL t(nloc, nd), q(nloc, nd), u(nloc, nd), v(nloc, nd)
+    REAL gz(nloc, nd)
+    REAL p(nloc, nd), ph(nloc, nd + 1), h(nloc, nd)
+    REAL hp(nloc, nd), lv(nloc, nd)
+    REAL cpn(nloc, nd), ep(nloc, nd), clw(nloc, nd), frac(nloc)
+    REAL m(nloc, nd), mp(nloc, nd), qp(nloc, nd)
+    REAL up(nloc, nd), vp(nloc, nd)
+    REAL wt(nloc, nd), water(nloc, nd), evap(nloc, nd)
+    REAL ment(nloc, nd, nd), qent(nloc, nd, nd), elij(nloc, nd, nd)
+    REAL uent(nloc, nd, nd), vent(nloc, nd, nd)
+    REAL tv(nloc, nd), tvp(nloc, nd)
+
+    ! outputs
+    INTEGER iflag(nloc) ! also an input
+    REAL cbmf(nloc) ! also an input
+    REAL wd(nloc), tprime(nloc), qprime(nloc)
+    REAL precip(nloc)
+    REAL ft(nloc, nd), fq(nloc, nd), fu(nloc, nd), fv(nloc, nd)
+    REAL ma(nloc, nd)
+    REAL qcondc(nloc, nd)
+
+    ! local variables
+    INTEGER i, j, ij, k, num1
+    REAL dpinv, cpinv, awat, fqold, ftold, fuold, fvold, delti
+    REAL work(nloc), am(nloc), amp1(nloc), ad(nloc)
+    REAL ents(nloc), uav(nloc), vav(nloc), lvcp(nloc, nd)
+    REAL qcond(nloc, nd), nqcond(nloc, nd), wa(nloc, nd) ! cld
+    REAL siga(nloc, nd), ax(nloc, nd), mac(nloc, nd) ! cld
+
+
+    ! -- initializations:
+
+    delti = 1.0 / delt
+
+    DO i = 1, ncum
+      precip(i) = 0.0
+      wd(i) = 0.0
+      tprime(i) = 0.0
+      qprime(i) = 0.0
+      DO k = 1, nl + 1
+        ft(i, k) = 0.0
+        fu(i, k) = 0.0
+        fv(i, k) = 0.0
+        fq(i, k) = 0.0
+        lvcp(i, k) = lv(i, k) / cpn(i, k)
+        qcondc(i, k) = 0.0 ! cld
+        qcond(i, k) = 0.0 ! cld
+        nqcond(i, k) = 0.0 ! cld
+      END DO
+    END DO
+
+
+    ! ***  Calculate surface precipitation in mm/day     ***
+
+    DO i = 1, ncum
+      IF (iflag(i)<=1) THEN
+        ! c            precip(i)=precip(i)+wt(i,1)*sigd*water(i,1)*3600.*24000.
+        ! c     &                /(rowl*g)
+        ! c            precip(i)=precip(i)*delt/86400.
+        precip(i) = wt(i, 1) * sigd * water(i, 1) * 86400 / g
+      END IF
+    END DO
+
+
+    ! ***  Calculate downdraft velocity scale and surface temperature and  ***
+    ! ***                    water vapor fluctuations                      ***
+
+    DO i = 1, ncum
+      wd(i) = betad * abs(mp(i, icb(i))) * 0.01 * rrd * t(i, icb(i)) / (sigd * p(i, icb(i)))
+      qprime(i) = 0.5 * (qp(i, 1) - q(i, 1))
+      tprime(i) = lv0 * qprime(i) / cpd
+    END DO
+
+    ! ***  Calculate tendencies of lowest level potential temperature  ***
+    ! ***                      and mixing ratio                        ***
+
+    DO i = 1, ncum
+      work(i) = 0.01 / (ph(i, 1) - ph(i, 2))
+      am(i) = 0.0
+    END DO
+    DO k = 2, nl
+      DO i = 1, ncum
+        IF ((nk(i)==1) .AND. (k<=inb(i)) .AND. (nk(i)==1)) THEN
+          am(i) = am(i) + m(i, k)
+        END IF
+      END DO
+    END DO
+    DO i = 1, ncum
+      IF ((g * work(i) * am(i))>=delti) iflag(i) = 1
+      ft(i, 1) = ft(i, 1) + g * work(i) * am(i) * (t(i, 2) - t(i, 1) + (gz(i, 2) - gz(i, &
+              1)) / cpn(i, 1))
+      ft(i, 1) = ft(i, 1) - lvcp(i, 1) * sigd * evap(i, 1)
+      ft(i, 1) = ft(i, 1) + sigd * wt(i, 2) * (cl - cpd) * water(i, 2) * (t(i, 2) - t(i, 1)) * &
+              work(i) / cpn(i, 1)
+      fq(i, 1) = fq(i, 1) + g * mp(i, 2) * (qp(i, 2) - q(i, 1)) * work(i) + &
+              sigd * evap(i, 1)
+      fq(i, 1) = fq(i, 1) + g * am(i) * (q(i, 2) - q(i, 1)) * work(i)
+      fu(i, 1) = fu(i, 1) + g * work(i) * (mp(i, 2) * (up(i, 2) - u(i, 1)) + am(i) * (u(i, &
+              2) - u(i, 1)))
+      fv(i, 1) = fv(i, 1) + g * work(i) * (mp(i, 2) * (vp(i, 2) - v(i, 1)) + am(i) * (v(i, &
+              2) - v(i, 1)))
+    END DO
+    DO j = 2, nl
+      DO i = 1, ncum
+        IF (j<=inb(i)) THEN
+          fq(i, 1) = fq(i, 1) + g * work(i) * ment(i, j, 1) * (qent(i, j, 1) - q(i, 1))
+          fu(i, 1) = fu(i, 1) + g * work(i) * ment(i, j, 1) * (uent(i, j, 1) - u(i, 1))
+          fv(i, 1) = fv(i, 1) + g * work(i) * ment(i, j, 1) * (vent(i, j, 1) - v(i, 1))
+        END IF
+      END DO
+    END DO
+
+    ! ***  Calculate tendencies of potential temperature and mixing ratio  ***
+    ! ***               at levels above the lowest level                   ***
+
+    ! ***  First find the net saturated updraft and downdraft mass fluxes  ***
+    ! ***                      through each level                          ***
+
+    DO i = 2, nl + 1
+
+      num1 = 0
+      DO ij = 1, ncum
+        IF (i<=inb(ij)) num1 = num1 + 1
+      END DO
+      IF (num1<=0) GO TO 1500
+
+      CALL zilch(amp1, ncum)
+      CALL zilch(ad, ncum)
+
+      DO k = i + 1, nl + 1
+        DO ij = 1, ncum
+          IF ((i>=nk(ij)) .AND. (i<=inb(ij)) .AND. (k<=(inb(ij) + 1))) THEN
+            amp1(ij) = amp1(ij) + m(ij, k)
+          END IF
+        END DO
+      END DO
+
+      DO k = 1, i
+        DO j = i + 1, nl + 1
+          DO ij = 1, ncum
+            IF ((j<=(inb(ij) + 1)) .AND. (i<=inb(ij))) THEN
+              amp1(ij) = amp1(ij) + ment(ij, k, j)
+            END IF
+          END DO
+        END DO
+      END DO
+      DO k = 1, i - 1
+        DO j = i, nl + 1
+          DO ij = 1, ncum
+            IF ((i<=inb(ij)) .AND. (j<=inb(ij))) THEN
+              ad(ij) = ad(ij) + ment(ij, j, k)
+            END IF
+          END DO
+        END DO
+      END DO
+
+      DO ij = 1, ncum
+        IF (i<=inb(ij)) THEN
+          dpinv = 0.01 / (ph(ij, i) - ph(ij, i + 1))
+          cpinv = 1.0 / cpn(ij, i)
+
+          ft(ij, i) = ft(ij, i) + g * dpinv * (amp1(ij) * (t(ij, i + 1) - t(ij, &
+                  i) + (gz(ij, i + 1) - gz(ij, i)) * cpinv) - ad(ij) * (t(ij, i) - t(ij, &
+                  i - 1) + (gz(ij, i) - gz(ij, i - 1)) * cpinv)) - sigd * lvcp(ij, i) * evap(ij, i)
+          ft(ij, i) = ft(ij, i) + g * dpinv * ment(ij, i, i) * (hp(ij, i) - h(ij, i) + t(ij &
+                  , i) * (cpv - cpd) * (q(ij, i) - qent(ij, i, i))) * cpinv
+          ft(ij, i) = ft(ij, i) + sigd * wt(ij, i + 1) * (cl - cpd) * water(ij, i + 1) * (t(&
+                  ij, i + 1) - t(ij, i)) * dpinv * cpinv
+          fq(ij, i) = fq(ij, i) + g * dpinv * (amp1(ij) * (q(ij, i + 1) - q(ij, &
+                  i)) - ad(ij) * (q(ij, i) - q(ij, i - 1)))
+          fu(ij, i) = fu(ij, i) + g * dpinv * (amp1(ij) * (u(ij, i + 1) - u(ij, &
+                  i)) - ad(ij) * (u(ij, i) - u(ij, i - 1)))
+          fv(ij, i) = fv(ij, i) + g * dpinv * (amp1(ij) * (v(ij, i + 1) - v(ij, &
+                  i)) - ad(ij) * (v(ij, i) - v(ij, i - 1)))
+        END IF
+      END DO
+      DO k = 1, i - 1
+        DO ij = 1, ncum
+          IF (i<=inb(ij)) THEN
+            awat = elij(ij, k, i) - (1. - ep(ij, i)) * clw(ij, i)
+            awat = max(awat, 0.0)
+            fq(ij, i) = fq(ij, i) + g * dpinv * ment(ij, k, i) * (qent(ij, k, i) - awat - q &
+                    (ij, i))
+            fu(ij, i) = fu(ij, i) + g * dpinv * ment(ij, k, i) * (uent(ij, k, i) - u(ij, i &
+                    ))
+            fv(ij, i) = fv(ij, i) + g * dpinv * ment(ij, k, i) * (vent(ij, k, i) - v(ij, i &
+                    ))
+            ! (saturated updrafts resulting from mixing)               ! cld
+            qcond(ij, i) = qcond(ij, i) + (elij(ij, k, i) - awat) ! cld
+            nqcond(ij, i) = nqcond(ij, i) + 1. ! cld
+          END IF
+        END DO
+      END DO
+      DO k = i, nl + 1
+        DO ij = 1, ncum
+          IF ((i<=inb(ij)) .AND. (k<=inb(ij))) THEN
+            fq(ij, i) = fq(ij, i) + g * dpinv * ment(ij, k, i) * (qent(ij, k, i) - q(ij, i &
+                    ))
+            fu(ij, i) = fu(ij, i) + g * dpinv * ment(ij, k, i) * (uent(ij, k, i) - u(ij, i &
+                    ))
+            fv(ij, i) = fv(ij, i) + g * dpinv * ment(ij, k, i) * (vent(ij, k, i) - v(ij, i &
+                    ))
+          END IF
+        END DO
+      END DO
+      DO ij = 1, ncum
+        IF (i<=inb(ij)) THEN
+          fq(ij, i) = fq(ij, i) + sigd * evap(ij, i) + g * (mp(ij, i + 1) * (qp(ij, &
+                  i + 1) - q(ij, i)) - mp(ij, i) * (qp(ij, i) - q(ij, i - 1))) * dpinv
+          fu(ij, i) = fu(ij, i) + g * (mp(ij, i + 1) * (up(ij, i + 1) - u(ij, &
+                  i)) - mp(ij, i) * (up(ij, i) - u(ij, i - 1))) * dpinv
+          fv(ij, i) = fv(ij, i) + g * (mp(ij, i + 1) * (vp(ij, i + 1) - v(ij, &
+                  i)) - mp(ij, i) * (vp(ij, i) - v(ij, i - 1))) * dpinv
+          ! (saturated downdrafts resulting from mixing)               ! cld
+          DO k = i + 1, inb(ij) ! cld
+            qcond(ij, i) = qcond(ij, i) + elij(ij, k, i) ! cld
+            nqcond(ij, i) = nqcond(ij, i) + 1. ! cld
+          END DO ! cld
+          ! (particular case: no detraining level is found)            ! cld
+          IF (nent(ij, i)==0) THEN ! cld
+            qcond(ij, i) = qcond(ij, i) + (1. - ep(ij, i)) * clw(ij, i) ! cld
+            nqcond(ij, i) = nqcond(ij, i) + 1. ! cld
+          END IF ! cld
+          IF (nqcond(ij, i)/=0.) THEN ! cld
+            qcond(ij, i) = qcond(ij, i) / nqcond(ij, i) ! cld
+          END IF ! cld
+        END IF
+      END DO
+    1500 END DO
+
+    ! *** Adjust tendencies at top of convection layer to reflect  ***
+    ! ***       actual position of the level zero cape             ***
+
+    DO ij = 1, ncum
+      fqold = fq(ij, inb(ij))
+      fq(ij, inb(ij)) = fq(ij, inb(ij)) * (1. - frac(ij))
+      fq(ij, inb(ij) - 1) = fq(ij, inb(ij) - 1) + frac(ij) * fqold * ((ph(ij, &
+              inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, &
+              inb(ij)))) * lv(ij, inb(ij)) / lv(ij, inb(ij) - 1)
+      ftold = ft(ij, inb(ij))
+      ft(ij, inb(ij)) = ft(ij, inb(ij)) * (1. - frac(ij))
+      ft(ij, inb(ij) - 1) = ft(ij, inb(ij) - 1) + frac(ij) * ftold * ((ph(ij, &
+              inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, &
+              inb(ij)))) * cpn(ij, inb(ij)) / cpn(ij, inb(ij) - 1)
+      fuold = fu(ij, inb(ij))
+      fu(ij, inb(ij)) = fu(ij, inb(ij)) * (1. - frac(ij))
+      fu(ij, inb(ij) - 1) = fu(ij, inb(ij) - 1) + frac(ij) * fuold * ((ph(ij, &
+              inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, inb(ij))))
+      fvold = fv(ij, inb(ij))
+      fv(ij, inb(ij)) = fv(ij, inb(ij)) * (1. - frac(ij))
+      fv(ij, inb(ij) - 1) = fv(ij, inb(ij) - 1) + frac(ij) * fvold * ((ph(ij, &
+              inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, inb(ij))))
+    END DO
+
+    ! ***   Very slightly adjust tendencies to force exact   ***
+    ! ***     enthalpy, momentum and tracer conservation     ***
+
+    DO ij = 1, ncum
+      ents(ij) = 0.0
+      uav(ij) = 0.0
+      vav(ij) = 0.0
+      DO i = 1, inb(ij)
+        ents(ij) = ents(ij) + (cpn(ij, i) * ft(ij, i) + lv(ij, i) * fq(ij, i)) * (ph(ij, i) - &
+                ph(ij, i + 1))
+        uav(ij) = uav(ij) + fu(ij, i) * (ph(ij, i) - ph(ij, i + 1))
+        vav(ij) = vav(ij) + fv(ij, i) * (ph(ij, i) - ph(ij, i + 1))
+      END DO
+    END DO
+    DO ij = 1, ncum
+      ents(ij) = ents(ij) / (ph(ij, 1) - ph(ij, inb(ij) + 1))
+      uav(ij) = uav(ij) / (ph(ij, 1) - ph(ij, inb(ij) + 1))
+      vav(ij) = vav(ij) / (ph(ij, 1) - ph(ij, inb(ij) + 1))
+    END DO
+    DO ij = 1, ncum
+      DO i = 1, inb(ij)
+        ft(ij, i) = ft(ij, i) - ents(ij) / cpn(ij, i)
+        fu(ij, i) = (1. - cu) * (fu(ij, i) - uav(ij))
+        fv(ij, i) = (1. - cu) * (fv(ij, i) - vav(ij))
+      END DO
+    END DO
+
+    DO k = 1, nl + 1
+      DO i = 1, ncum
+        IF ((q(i, k) + delt * fq(i, k))<0.0) iflag(i) = 10
+      END DO
+    END DO
+
+    DO i = 1, ncum
+      IF (iflag(i)>2) THEN
+        precip(i) = 0.0
+        cbmf(i) = 0.0
+      END IF
+    END DO
+    DO k = 1, nl
+      DO i = 1, ncum
+        IF (iflag(i)>2) THEN
+          ft(i, k) = 0.0
+          fq(i, k) = 0.0
+          fu(i, k) = 0.0
+          fv(i, k) = 0.0
+          qcondc(i, k) = 0.0 ! cld
+        END IF
+      END DO
+    END DO
+
+    DO k = 1, nl + 1
+      DO i = 1, ncum
+        ma(i, k) = 0.
+      END DO
+    END DO
+    DO k = nl, 1, -1
+      DO i = 1, ncum
+        ma(i, k) = ma(i, k + 1) + m(i, k)
+      END DO
+    END DO
+
+
+    ! *** diagnose the in-cloud mixing ratio   ***            ! cld
+    ! ***           of condensed water         ***            ! cld
+    ! cld
+    DO ij = 1, ncum ! cld
+      DO i = 1, nd ! cld
+        mac(ij, i) = 0.0 ! cld
+        wa(ij, i) = 0.0 ! cld
+        siga(ij, i) = 0.0 ! cld
+      END DO ! cld
+      DO i = nk(ij), inb(ij) ! cld
+        DO k = i + 1, inb(ij) + 1 ! cld
+          mac(ij, i) = mac(ij, i) + m(ij, k) ! cld
+        END DO ! cld
+      END DO ! cld
+      DO i = icb(ij), inb(ij) - 1 ! cld
+        ax(ij, i) = 0. ! cld
+        DO j = icb(ij), i ! cld
+          ax(ij, i) = ax(ij, i) + rrd * (tvp(ij, j) - tv(ij, j)) & ! cld
+                  * (ph(ij, j) - ph(ij, j + 1)) / p(ij, j) ! cld
+        END DO ! cld
+        IF (ax(ij, i)>0.0) THEN ! cld
+          wa(ij, i) = sqrt(2. * ax(ij, i)) ! cld
+        END IF ! cld
+      END DO ! cld
+      DO i = 1, nl ! cld
+        IF (wa(ij, i)>0.0) &          ! cld
+                siga(ij, i) = mac(ij, i) / wa(ij, i) & ! cld
+                        * rrd * tvp(ij, i) / p(ij, i) / 100. / delta ! cld
+        siga(ij, i) = min(siga(ij, i), 1.0) ! cld
+        qcondc(ij, i) = siga(ij, i) * clw(ij, i) * (1. - ep(ij, i)) & ! cld
+                + (1. - siga(ij, i)) * qcond(ij, i) ! cld
+      END DO ! cld
+    END DO ! cld
+
+  END SUBROUTINE cv_yield
+
+  SUBROUTINE cv_uncompress(nloc, len, ncum, nd, idcum, iflag, precip, cbmf, ft, &
+          fq, fu, fv, ma, qcondc, iflag1, precip1, cbmf1, ft1, fq1, fu1, fv1, ma1, &
+          qcondc1)
+    IMPLICIT NONE
+
+
+    ! inputs:
+    INTEGER len, ncum, nd, nloc
+    INTEGER idcum(nloc)
+    INTEGER iflag(nloc)
+    REAL precip(nloc), cbmf(nloc)
+    REAL ft(nloc, nd), fq(nloc, nd), fu(nloc, nd), fv(nloc, nd)
+    REAL ma(nloc, nd)
+    REAL qcondc(nloc, nd) !cld
+
+    ! outputs:
+    INTEGER iflag1(len)
+    REAL precip1(len), cbmf1(len)
+    REAL ft1(len, nd), fq1(len, nd), fu1(len, nd), fv1(len, nd)
+    REAL ma1(len, nd)
+    REAL qcondc1(len, nd) !cld
+
+    ! local variables:
+    INTEGER i, k
+
+    DO i = 1, ncum
+      precip1(idcum(i)) = precip(i)
+      cbmf1(idcum(i)) = cbmf(i)
+      iflag1(idcum(i)) = iflag(i)
+    END DO
+
+    DO k = 1, nl
+      DO i = 1, ncum
+        ft1(idcum(i), k) = ft(i, k)
+        fq1(idcum(i), k) = fq(i, k)
+        fu1(idcum(i), k) = fu(i, k)
+        fv1(idcum(i), k) = fv(i, k)
+        ma1(idcum(i), k) = ma(i, k)
+        qcondc1(idcum(i), k) = qcondc(i, k)
+      END DO
+    END DO
+
+  END SUBROUTINE cv_uncompress
+
+
+END MODULE lmdz_cv
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_dimpft.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_dimpft.f90	(revision 5142)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_dimpft.f90	(revision 5142)
@@ -0,0 +1,6 @@
+MODULE lmdz_dimpft
+  IMPLICIT NONE; PRIVATE
+  PUBLIC nvm_lmdz
+
+  INTEGER nvm_lmdz
+END MODULE lmdz_dimpft
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_fcg_gcssold.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_fcg_gcssold.f90	(revision 5142)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_fcg_gcssold.f90	(revision 5142)
@@ -0,0 +1,43 @@
+MODULE lmdz_fcs_gcssold
+  IMPLICIT NONE; PRIVATE
+  PUBLIC imp_fcg_gcssold, ts_fcg_gcssold, Tp_fcg_gcssold, Tp_ini_gcssold, xTurb_fcg_gcssold
+
+  LOGICAL :: imp_fcg_gcssold, ts_fcg_gcssold, Tp_fcg_gcssold
+  LOGICAL :: Tp_ini_gcssold
+  LOGICAL :: xTurb_fcg_gcssold
+
+  !$OMP THREADPRIVATE(imp_fcg_gcssold, ts_fcg_gcssold, Tp_fcg_gcssold, Tp_ini_gcssold, xTurb_fcg_gcssold)
+END MODULE lmdz_fcs_gcssold
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_planete.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_planete.f90	(revision 5142)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_planete.f90	(revision 5142)
@@ -0,0 +1,9 @@
+MODULE lmdz_planete
+  IMPLICIT NONE; PRIVATE
+  PUBLIC aphelie, periheli, year_day, peri_day, obliquit, timeperi, e_elips, p_elips, unitastr
+
+  REAL aphelie, periheli, year_day, peri_day, obliquit, timeperi, e_elips, &
+          p_elips, unitastr
+
+  !$OMP THREADPRIVATE(aphelie, periheli, year_day, peri_day, obliquit, timeperi, e_elips, p_elips, unitastr)
+END MODULE lmdz_planete
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_tsoilnudge.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_tsoilnudge.f90	(revision 5142)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/lmdz_tsoilnudge.f90	(revision 5142)
@@ -0,0 +1,8 @@
+MODULE lmdz_tsoilnudge
+  IMPLICIT NONE; PRIVATE
+  PUBLIC nudge_tsoil, isoil_nudge, Tsoil_nudge, tau_soil_nudge
+
+  LOGICAL nudge_tsoil
+  INTEGER isoil_nudge
+  REAL Tsoil_nudge, tau_soil_nudge
+END MODULE lmdz_tsoilnudge
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/lsc_scav.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/lsc_scav.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/lsc_scav.F90	(revision 5142)
@@ -12,4 +12,6 @@
   USE infotrac_phy,ONLY: nbtr
   USE iophy
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE 
 !=====================================================================
@@ -22,5 +24,4 @@
   include "chem.h"
   include "YOMCST.h"
-  include "YOECUMF.h" 
 
 ! inputs
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/nflxtr.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/nflxtr.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/nflxtr.F90	(revision 5142)
@@ -4,4 +4,6 @@
 SUBROUTINE nflxtr(pdtime,pmfu,pmfd,pen_u,pde_u,pen_d,pde_d,pplay,paprs,x,dx) 
   USE dimphy
+  USE lmdz_YOECUMF
+
   IMPLICIT NONE 
 !=====================================================================
@@ -22,5 +24,4 @@
 
   include "YOMCST.h"
-  include "YOECUMF.h" 
 
   REAL,INTENT(IN) :: pdtime  ! pdtphys
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/pbl_surface_mod.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/pbl_surface_mod.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/pbl_surface_mod.F90	(revision 5142)
@@ -417,4 +417,5 @@
   USE lmdz_flux_arp, ONLY: fsens, flat, betaevap, ust, tg, ok_flux_surf, ok_prescr_ust, ok_prescr_beta, ok_forc_tsurf
   USE lmdz_compbl, ONLY: iflag_pbl, iflag_pbl_split, iflag_order2_sollw, ifl_pbltree
+  USE lmdz_dimpft, ONLY: nvm_lmdz
 
     IMPLICIT NONE
@@ -424,6 +425,4 @@
     INCLUDE "YOETHF.h"
     INCLUDE "FCTTRE.h"
-    !FC
-    INCLUDE "dimpft.h"
 
     !****************************************************************************************
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/physiq_mod.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/physiq_mod.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/physiq_mod.F90	(revision 5142)
@@ -355,4 +355,5 @@
     USE lmdz_compbl, ONLY: iflag_pbl, iflag_pbl_split, iflag_order2_sollw, ifl_pbltree
     USE lmdz_conema3
+    USE lmdz_dimpft, ONLY: nvm_lmdz
 
     IMPLICIT NONE
@@ -409,5 +410,4 @@
     include "regdim.h"
     include "dimsoil.h"
-    include "dimpft.h"
     !======================================================================
     LOGICAL, SAVE :: ok_volcan ! pour activer les diagnostics volcaniques
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/planete.h
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/planete.h	(revision 5141)
+++ 	(revision )
@@ -1,11 +1,0 @@
-!-----------------------------------------------------------------------
-! INCLUDE planet.h
-
-      COMMON/planet/aphelie,periheli,year_day,peri_day, obliquit, timeperi,&
-           e_elips,p_elips,unitastr
-
-      REAL aphelie,periheli,year_day,peri_day, obliquit, timeperi,e_elips, &
-           p_elips,unitastr
-
-!-----------------------------------------------------------------------
-!$OMP THREADPRIVATE(/planet/)
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/solarlong.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/solarlong.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/solarlong.F90	(revision 5142)
@@ -3,4 +3,5 @@
   USE ioipsl
   USE lmdz_print_control, ONLY: lunout
+  USE lmdz_planete, ONLY: aphelie, periheli, year_day, peri_day, obliquit, timeperi, e_elips, p_elips, unitastr
 
   IMPLICIT NONE
@@ -44,6 +45,4 @@
   ! Declarations:
   ! -------------
-
-  include "planete.h"
   include "YOMCST.h"
 
@@ -83,7 +82,7 @@
   ! calcul de l'zanomalie moyenne
 
-  zz = (pday-peri_day)/year_day
-  pi = 2.*asin(1.)
-  zanom = 2.*pi*(zz-nint(zz))
+  zz = (pday - peri_day) / year_day
+  pi = 2. * asin(1.)
+  zanom = 2. * pi * (zz - nint(zz))
   xref = abs(zanom)
 
@@ -92,12 +91,12 @@
 
   ! zx0=xref+e_elips*sin(xref)
-  zx0 = xref + r_ecc*sin(xref)
+  zx0 = xref + r_ecc * sin(xref)
   DO iter = 1, 10
     ! zdx=-(zx0-e_elips*sin(zx0)-xref)/(1.-e_elips*cos(zx0))
-    zdx = -(zx0-r_ecc*sin(zx0)-xref)/(1.-r_ecc*cos(zx0))
+    zdx = -(zx0 - r_ecc * sin(zx0) - xref) / (1. - r_ecc * cos(zx0))
     IF (abs(zdx)<=(1.E-7)) GO TO 120
     zx0 = zx0 + zdx
   END DO
-120 CONTINUE
+  120 CONTINUE
   zx0 = zx0 + zdx
   IF (zanom<0.) zx0 = -zx0
@@ -106,17 +105,17 @@
 
   ! zteta=2.*atan(sqrt((1.+e_elips)/(1.-e_elips))*tan(zx0/2.))
-  zteta = 2.*atan(sqrt((1.+r_ecc)/(1.-r_ecc))*tan(zx0/2.))
+  zteta = 2. * atan(sqrt((1. + r_ecc) / (1. - r_ecc)) * tan(zx0 / 2.))
 
   psollong = zteta - timeperi
 
-  IF (psollong<0.) psollong = psollong + 2.*pi
-  IF (psollong>2.*pi) psollong = psollong - 2.*pi
+  IF (psollong<0.) psollong = psollong + 2. * pi
+  IF (psollong>2. * pi) psollong = psollong - 2. * pi
 
-  psollong = psollong*180./pi
+  psollong = psollong * 180. / pi
 
   ! distance soleil
 
-  pdist_sol = (1-r_ecc*r_ecc)/(1+r_ecc*cos(pi/180.*(psollong- &
-    (r_peri+180.0))))
+  pdist_sol = (1 - r_ecc * r_ecc) / (1 + r_ecc * cos(pi / 180. * (psollong - &
+          (r_peri + 180.0))))
   ! pdist_sol = (1-e_elips*e_elips)
   ! &      /(1+e_elips*COS(pi/180.*(psollong-(R_peri+180.0))))
@@ -131,4 +130,3 @@
   ! ENDIF
 
-
 END SUBROUTINE solarlong
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_mod.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_mod.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_mod.F90	(revision 5142)
@@ -75,8 +75,8 @@
     USE lmdz_print_control, ONLY: lunout
   USE lmdz_clesphys
+  USE lmdz_dimpft, ONLY: nvm_lmdz
 
     INCLUDE "dimsoil.h"
     INCLUDE "YOMCST.h"
-    INCLUDE "dimpft.h"
 
 ! Input variables  
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_mod.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_mod.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_mod.F90	(revision 5142)
@@ -58,4 +58,5 @@
     USE lmdz_print_control, ONLY: lunout
     USE lmdz_grid_phy, ONLY: nbp_lon, nbp_lat
+    USE lmdz_dimpft, ONLY: nvm_lmdz
 #ifdef CPP_VEGET
     USE time_phylmdz_mod, ONLY: itau_phy 
@@ -114,5 +115,4 @@
 
     INCLUDE "YOMCST.h"
-    INCLUDE "dimpft.h"
 
     ! Parametres d'entree
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_nofrein_mod.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_nofrein_mod.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_nofrein_mod.F90	(revision 5142)
@@ -61,4 +61,5 @@
     USE time_phylmdz_mod, ONLY: itau_phy 
 #endif
+USE lmdz_dimpft, ONLY: nvm_lmdz
 
 ! Cette routine sert d'interface entre le modele atmospherique et le 
@@ -115,5 +116,4 @@
 
     INCLUDE "YOMCST.h"
-    INCLUDE "dimpft.h"
 
 ! Parametres d'entree
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_nolic_mod.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_nolic_mod.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_nolic_mod.F90	(revision 5142)
@@ -57,4 +57,5 @@
     USE time_phylmdz_mod, ONLY: itau_phy 
 #endif
+USE lmdz_dimpft, ONLY: nvm_lmdz
 
 ! Cette routine sert d'interface entre le modele atmospherique et le 
@@ -110,5 +111,4 @@
 
     INCLUDE "YOMCST.h"
-    INCLUDE "dimpft.h"
 
 ! Parametres d'entree
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_noopenmp_mod.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_noopenmp_mod.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_noopenmp_mod.F90	(revision 5142)
@@ -106,8 +106,9 @@
     USE time_phylmdz_mod, ONLY: itau_phy 
 #endif
+USE lmdz_dimpft, ONLY: nvm_lmdz
+
     IMPLICIT NONE
 
     INCLUDE "YOMCST.h"
-    INCLUDE "dimpft.h"  
 
 ! Parametres d'entree
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_nounstruct_mod.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_nounstruct_mod.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_nounstruct_mod.F90	(revision 5142)
@@ -56,4 +56,5 @@
     USE time_phylmdz_mod, ONLY: itau_phy 
 #endif
+USE lmdz_dimpft, ONLY: nvm_lmdz
 
 ! Cette routine sert d'interface entre le modele atmospherique et le 
@@ -110,5 +111,4 @@
 
     INCLUDE "YOMCST.h"
-    INCLUDE "dimpft.h"
 
 ! Parametres d'entree
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_noz0h_mod.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_noz0h_mod.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmd/surf_land_orchidee_noz0h_mod.F90	(revision 5142)
@@ -59,4 +59,5 @@
     USE time_phylmdz_mod, ONLY: itau_phy 
 #endif
+USE lmdz_dimpft, ONLY: nvm_lmdz
 
 ! Cette routine sert d'interface entre le modele atmospherique et le 
@@ -113,5 +114,4 @@
 
     INCLUDE "YOMCST.h"
-    INCLUDE "dimpft.h"  
 
 ! Parametres d'entree
Index: LMDZ6/branches/Amaury_dev/libf/phylmd/tsoilnudge.h
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmd/tsoilnudge.h	(revision 5141)
+++ 	(revision )
@@ -1,7 +1,0 @@
-      LOGICAL nudge_tsoil
-      INTEGER isoil_nudge
-      REAL Tsoil_nudge, tau_soil_nudge
-
-      common /tsoilnudge/ nudge_tsoil, isoil_nudge, Tsoil_nudge,        &
-                         tau_soil_nudge
-
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/YOECUMF.h
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/YOECUMF.h	(revision 5141)
+++ 	(revision )
@@ -1,1 +1,0 @@
-link ../phylmd/YOECUMF.h
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/cv_driver.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/cv_driver.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmdiso/cv_driver.F90	(revision 5142)
@@ -44,4 +44,6 @@
 USE lmdz_cv30, ONLY: cv30_param, cv30_prelim, cv30_feed, cv30_undilute1, cv30_trigger, cv30_compress, cv30_undilute2, &
           cv30_closure, cv30_epmax_fn_cape, cv30_mixing, cv30_unsat, cv30_yield, cv30_tracer, cv30_uncompress
+USE lmdz_cv, ONLY: cv_param, cv_prelim, cv_feed, cv_undilute1, cv_trigger, cv_compress, &
+          cv_undilute2, cv_closure, cv_mixing, cv_unsat, cv_yield, cv_uncompress
 
   IMPLICIT NONE
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/cv_routines.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/cv_routines.F90	(revision 5141)
+++ 	(revision )
@@ -1,1698 +1,0 @@
-! $Id$
-
-SUBROUTINE cv_param(nd)
-  IMPLICIT NONE
-
-  ! ------------------------------------------------------------
-  ! Set parameters for convectL
-  ! (includes microphysical parameters and parameters that
-  ! control the rate of approach to quasi-equilibrium)
-  ! ------------------------------------------------------------
-
-  ! *** ELCRIT IS THE AUTOCONVERSION THERSHOLD WATER CONTENT (gm/gm) ***
-  ! ***  TLCRIT IS CRITICAL TEMPERATURE BELOW WHICH THE AUTO-        ***
-  ! ***       CONVERSION THRESHOLD IS ASSUMED TO BE ZERO             ***
-  ! ***     (THE AUTOCONVERSION THRESHOLD VARIES LINEARLY            ***
-  ! ***               BETWEEN 0 C AND TLCRIT)                        ***
-  ! ***   ENTP IS THE COEFFICIENT OF MIXING IN THE ENTRAINMENT       ***
-  ! ***                       FORMULATION                            ***
-  ! ***  SIGD IS THE FRACTIONAL AREA COVERED BY UNSATURATED DNDRAFT  ***
-  ! ***  SIGS IS THE FRACTION OF PRECIPITATION FALLING OUTSIDE       ***
-  ! ***                        OF CLOUD                              ***
-  ! ***        OMTRAIN IS THE ASSUMED FALL SPEED (P/s) OF RAIN       ***
-  ! ***     OMTSNOW IS THE ASSUMED FALL SPEED (P/s) OF SNOW          ***
-  ! ***  COEFFR IS A COEFFICIENT GOVERNING THE RATE OF EVAPORATION   ***
-  ! ***                          OF RAIN                             ***
-  ! ***  COEFFS IS A COEFFICIENT GOVERNING THE RATE OF EVAPORATION   ***
-  ! ***                          OF SNOW                             ***
-  ! ***     CU IS THE COEFFICIENT GOVERNING CONVECTIVE MOMENTUM      ***
-  ! ***                         TRANSPORT                            ***
-  ! ***    DTMAX IS THE MAXIMUM NEGATIVE TEMPERATURE PERTURBATION    ***
-  ! ***        A LIFTED PARCEL IS ALLOWED TO HAVE BELOW ITS LFC      ***
-  ! ***    ALPHA AND DAMP ARE PARAMETERS THAT CONTROL THE RATE OF    ***
-  ! ***                 APPROACH TO QUASI-EQUILIBRIUM                ***
-  ! ***   (THEIR STANDARD VALUES ARE  0.20 AND 0.1, RESPECTIVELY)    ***
-  ! ***                   (DAMP MUST BE LESS THAN 1)                 ***
-
-  include "cvparam.h"
-  INTEGER nd
-  CHARACTER (LEN = 20) :: modname = 'cv_routines'
-  CHARACTER (LEN = 80) :: abort_message
-
-  ! noff: integer limit for convection (nd-noff)
-  ! minorig: First level of convection
-
-  noff = 2
-  minorig = 2
-
-  nl = nd - noff
-  nlp = nl + 1
-  nlm = nl - 1
-
-  elcrit = 0.0011
-  tlcrit = -55.0
-  entp = 1.5
-  sigs = 0.12
-  sigd = 0.05
-  omtrain = 50.0
-  omtsnow = 5.5
-  coeffr = 1.0
-  coeffs = 0.8
-  dtmax = 0.9
-
-  cu = 0.70
-
-  betad = 10.0
-
-  damp = 0.1
-  alpha = 0.2
-
-  delta = 0.01 ! cld
-
-END SUBROUTINE cv_param
-
-SUBROUTINE cv_prelim(len, nd, ndp1, t, q, p, ph, lv, cpn, tv, gz, h, hm)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  ! =====================================================================
-  ! --- CALCULATE ARRAYS OF GEOPOTENTIAL, HEAT CAPACITY & STATIC ENERGY
-  ! =====================================================================
-
-  ! inputs:
-  INTEGER len, nd, ndp1
-  REAL t(len, nd), q(len, nd), p(len, nd), ph(len, ndp1)
-
-  ! outputs:
-  REAL lv(len, nd), cpn(len, nd), tv(len, nd)
-  REAL gz(len, nd), h(len, nd), hm(len, nd)
-
-  ! local variables:
-  INTEGER k, i
-  REAL cpx(len, nd)
-
-  include "cvparam.h"
-
-  DO k = 1, nlp
-    DO i = 1, len
-      lv(i, k) = lv0 - clmcpv * (t(i, k) - t0)
-      cpn(i, k) = cpd * (1.0 - q(i, k)) + cpv * q(i, k)
-      cpx(i, k) = cpd * (1.0 - q(i, k)) + cl * q(i, k)
-      tv(i, k) = t(i, k) * (1.0 + q(i, k) * epsim1)
-    END DO
-  END DO
-
-  ! gz = phi at the full levels (same as p).
-
-  DO i = 1, len
-    gz(i, 1) = 0.0
-  END DO
-  DO k = 2, nlp
-    DO i = 1, len
-      gz(i, k) = gz(i, k - 1) + hrd * (tv(i, k - 1) + tv(i, k)) * (p(i, k - 1) - p(i, k)) / ph(i, &
-              k)
-    END DO
-  END DO
-
-  ! h  = phi + cpT (dry static energy).
-  ! hm = phi + cp(T-Tbase)+Lq
-
-  DO k = 1, nlp
-    DO i = 1, len
-      h(i, k) = gz(i, k) + cpn(i, k) * t(i, k)
-      hm(i, k) = gz(i, k) + cpx(i, k) * (t(i, k) - t(i, 1)) + lv(i, k) * q(i, k)
-    END DO
-  END DO
-
-END SUBROUTINE cv_prelim
-
-SUBROUTINE cv_feed(len, nd, t, q, qs, p, hm, gz, nk, icb, icbmax, iflag, tnk, &
-        qnk, gznk, plcl)
-  IMPLICIT NONE
-
-  ! ================================================================
-  ! Purpose: CONVECTIVE FEED
-  ! ================================================================
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER len, nd
-  REAL t(len, nd), q(len, nd), qs(len, nd), p(len, nd)
-  REAL hm(len, nd), gz(len, nd)
-
-  ! outputs:
-  INTEGER iflag(len), nk(len), icb(len), icbmax
-  REAL tnk(len), qnk(len), gznk(len), plcl(len)
-
-  ! local variables:
-  INTEGER i, k
-  INTEGER ihmin(len)
-  REAL work(len)
-  REAL pnk(len), qsnk(len), rh(len), chi(len)
-
-  ! -------------------------------------------------------------------
-  ! --- Find level of minimum moist static energy
-  ! --- If level of minimum moist static energy coincides with
-  ! --- or is lower than minimum allowable parcel origin level,
-  ! --- set iflag to 6.
-  ! -------------------------------------------------------------------
-
-  DO i = 1, len
-    work(i) = 1.0E12
-    ihmin(i) = nl
-  END DO
-  DO k = 2, nlp
-    DO i = 1, len
-      IF ((hm(i, k)<work(i)) .AND. (hm(i, k)<hm(i, k - 1))) THEN
-        work(i) = hm(i, k)
-        ihmin(i) = k
-      END IF
-    END DO
-  END DO
-  DO i = 1, len
-    ihmin(i) = min(ihmin(i), nlm)
-    IF (ihmin(i)<=minorig) THEN
-      iflag(i) = 6
-    END IF
-  END DO
-
-  ! -------------------------------------------------------------------
-  ! --- Find that model level below the level of minimum moist static
-  ! --- energy that has the maximum value of moist static energy
-  ! -------------------------------------------------------------------
-
-  DO i = 1, len
-    work(i) = hm(i, minorig)
-    nk(i) = minorig
-  END DO
-  DO k = minorig + 1, nl
-    DO i = 1, len
-      IF ((hm(i, k)>work(i)) .AND. (k<=ihmin(i))) THEN
-        work(i) = hm(i, k)
-        nk(i) = k
-      END IF
-    END DO
-  END DO
-  ! -------------------------------------------------------------------
-  ! --- Check whether parcel level temperature and specific humidity
-  ! --- are reasonable
-  ! -------------------------------------------------------------------
-  DO i = 1, len
-    IF (((t(i, nk(i))<250.0) .OR. (q(i, nk(i))<=0.0) .OR. (p(i, ihmin(i))< &
-            400.0)) .AND. (iflag(i)==0)) iflag(i) = 7
-  END DO
-  ! -------------------------------------------------------------------
-  ! --- Calculate lifted condensation level of air at parcel origin level
-  ! --- (Within 0.2% of formula of Bolton, MON. WEA. REV.,1980)
-  ! -------------------------------------------------------------------
-  DO i = 1, len
-    tnk(i) = t(i, nk(i))
-    qnk(i) = q(i, nk(i))
-    gznk(i) = gz(i, nk(i))
-    pnk(i) = p(i, nk(i))
-    qsnk(i) = qs(i, nk(i))
-
-    rh(i) = qnk(i) / qsnk(i)
-    rh(i) = min(1.0, rh(i))
-    chi(i) = tnk(i) / (1669.0 - 122.0 * rh(i) - tnk(i))
-    plcl(i) = pnk(i) * (rh(i)**chi(i))
-    IF (((plcl(i)<200.0) .OR. (plcl(i)>=2000.0)) .AND. (iflag(i)==0)) iflag(i &
-            ) = 8
-  END DO
-  ! -------------------------------------------------------------------
-  ! --- Calculate first level above lcl (=icb)
-  ! -------------------------------------------------------------------
-  DO i = 1, len
-    icb(i) = nlm
-  END DO
-
-  DO k = minorig, nl
-    DO i = 1, len
-      IF ((k>=(nk(i) + 1)) .AND. (p(i, k)<plcl(i))) icb(i) = min(icb(i), k)
-    END DO
-  END DO
-
-  DO i = 1, len
-    IF ((icb(i)>=nlm) .AND. (iflag(i)==0)) iflag(i) = 9
-  END DO
-
-  ! Compute icbmax.
-
-  icbmax = 2
-  DO i = 1, len
-    icbmax = max(icbmax, icb(i))
-  END DO
-
-END SUBROUTINE cv_feed
-
-SUBROUTINE cv_undilute1(len, nd, t, q, qs, gz, p, nk, icb, icbmax, tp, tvp, &
-        clw)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER len, nd
-  INTEGER nk(len), icb(len), icbmax
-  REAL t(len, nd), q(len, nd), qs(len, nd), gz(len, nd)
-  REAL p(len, nd)
-
-  ! outputs:
-  REAL tp(len, nd), tvp(len, nd), clw(len, nd)
-
-  ! local variables:
-  INTEGER i, k
-  REAL tg, qg, alv, s, ahg, tc, denom, es, rg
-  REAL ah0(len), cpp(len)
-  REAL tnk(len), qnk(len), gznk(len), ticb(len), gzicb(len)
-
-  ! -------------------------------------------------------------------
-  ! --- Calculates the lifted parcel virtual temperature at nk,
-  ! --- the actual temperature, and the adiabatic
-  ! --- liquid water content. The procedure is to solve the equation.
-  ! cp*tp+L*qp+phi=cp*tnk+L*qnk+gznk.
-  ! -------------------------------------------------------------------
-
-  DO i = 1, len
-    tnk(i) = t(i, nk(i))
-    qnk(i) = q(i, nk(i))
-    gznk(i) = gz(i, nk(i))
-    ticb(i) = t(i, icb(i))
-    gzicb(i) = gz(i, icb(i))
-  END DO
-
-  ! ***  Calculate certain parcel quantities, including static energy   ***
-
-  DO i = 1, len
-    ah0(i) = (cpd * (1. - qnk(i)) + cl * qnk(i)) * tnk(i) + qnk(i) * (lv0 - clmcpv * (tnk(i) - &
-            273.15)) + gznk(i)
-    cpp(i) = cpd * (1. - qnk(i)) + qnk(i) * cpv
-  END DO
-
-  ! ***   Calculate lifted parcel quantities below cloud base   ***
-
-  DO k = minorig, icbmax - 1
-    DO i = 1, len
-      tp(i, k) = tnk(i) - (gz(i, k) - gznk(i)) / cpp(i)
-      tvp(i, k) = tp(i, k) * (1. + qnk(i) * epsi)
-    END DO
-  END DO
-
-  ! ***  Find lifted parcel quantities above cloud base    ***
-
-  DO i = 1, len
-    tg = ticb(i)
-    qg = qs(i, icb(i))
-    alv = lv0 - clmcpv * (ticb(i) - t0)
-
-    ! First iteration.
-
-    s = cpd + alv * alv * qg / (rrv * ticb(i) * ticb(i))
-    s = 1. / s
-    ahg = cpd * tg + (cl - cpd) * qnk(i) * ticb(i) + alv * qg + gzicb(i)
-    tg = tg + s * (ah0(i) - ahg)
-    tg = max(tg, 35.0)
-    tc = tg - t0
-    denom = 243.5 + tc
-    IF (tc>=0.0) THEN
-      es = 6.112 * exp(17.67 * tc / denom)
-    ELSE
-      es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
-    END IF
-    qg = eps * es / (p(i, icb(i)) - es * (1. - eps))
-
-    ! Second iteration.
-
-    s = cpd + alv * alv * qg / (rrv * ticb(i) * ticb(i))
-    s = 1. / s
-    ahg = cpd * tg + (cl - cpd) * qnk(i) * ticb(i) + alv * qg + gzicb(i)
-    tg = tg + s * (ah0(i) - ahg)
-    tg = max(tg, 35.0)
-    tc = tg - t0
-    denom = 243.5 + tc
-    IF (tc>=0.0) THEN
-      es = 6.112 * exp(17.67 * tc / denom)
-    ELSE
-      es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
-    END IF
-    qg = eps * es / (p(i, icb(i)) - es * (1. - eps))
-
-    alv = lv0 - clmcpv * (ticb(i) - 273.15)
-    tp(i, icb(i)) = (ah0(i) - (cl - cpd) * qnk(i) * ticb(i) - gz(i, icb(i)) - alv * qg) / cpd
-    clw(i, icb(i)) = qnk(i) - qg
-    clw(i, icb(i)) = max(0.0, clw(i, icb(i)))
-    rg = qg / (1. - qnk(i))
-    tvp(i, icb(i)) = tp(i, icb(i)) * (1. + rg * epsi)
-  END DO
-
-  DO k = minorig, icbmax
-    DO i = 1, len
-      tvp(i, k) = tvp(i, k) - tp(i, k) * qnk(i)
-    END DO
-  END DO
-
-END SUBROUTINE cv_undilute1
-
-SUBROUTINE cv_trigger(len, nd, icb, cbmf, tv, tvp, iflag)
-  IMPLICIT NONE
-
-  ! -------------------------------------------------------------------
-  ! --- Test for instability.
-  ! --- If there was no convection at last time step and parcel
-  ! --- is stable at icb, then set iflag to 4.
-  ! -------------------------------------------------------------------
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER len, nd, icb(len)
-  REAL cbmf(len), tv(len, nd), tvp(len, nd)
-
-  ! outputs:
-  INTEGER iflag(len) ! also an input
-
-  ! local variables:
-  INTEGER i
-
-  DO i = 1, len
-    IF ((cbmf(i)==0.0) .AND. (iflag(i)==0) .AND. (tvp(i, &
-            icb(i))<=(tv(i, icb(i)) - dtmax))) iflag(i) = 4
-  END DO
-
-END SUBROUTINE cv_trigger
-
-SUBROUTINE cv_compress(len, nloc, ncum, nd, iflag1, nk1, icb1, cbmf1, plcl1, &
-        tnk1, qnk1, gznk1, t1, q1, qs1, u1, v1, gz1, h1, lv1, cpn1, p1, ph1, tv1, &
-        tp1, tvp1, clw1, iflag, nk, icb, cbmf, plcl, tnk, qnk, gznk, t, q, qs, u, &
-        v, gz, h, lv, cpn, p, ph, tv, tp, tvp, clw, dph)
-  USE lmdz_print_control, ONLY: lunout
-  USE lmdz_abort_physic, ONLY: abort_physic
-  IMPLICIT NONE
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER len, ncum, nd, nloc
-  INTEGER iflag1(len), nk1(len), icb1(len)
-  REAL cbmf1(len), plcl1(len), tnk1(len), qnk1(len), gznk1(len)
-  REAL t1(len, nd), q1(len, nd), qs1(len, nd), u1(len, nd), v1(len, nd)
-  REAL gz1(len, nd), h1(len, nd), lv1(len, nd), cpn1(len, nd)
-  REAL p1(len, nd), ph1(len, nd + 1), tv1(len, nd), tp1(len, nd)
-  REAL tvp1(len, nd), clw1(len, nd)
-
-  ! outputs:
-  INTEGER iflag(nloc), nk(nloc), icb(nloc)
-  REAL cbmf(nloc), plcl(nloc), tnk(nloc), qnk(nloc), gznk(nloc)
-  REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd), u(nloc, nd), v(nloc, nd)
-  REAL gz(nloc, nd), h(nloc, nd), lv(nloc, nd), cpn(nloc, nd)
-  REAL p(nloc, nd), ph(nloc, nd + 1), tv(nloc, nd), tp(nloc, nd)
-  REAL tvp(nloc, nd), clw(nloc, nd)
-  REAL dph(nloc, nd)
-
-  ! local variables:
-  INTEGER i, k, nn
-  CHARACTER (LEN = 20) :: modname = 'cv_compress'
-  CHARACTER (LEN = 80) :: abort_message
-
-  DO k = 1, nl + 1
-    nn = 0
-    DO i = 1, len
-      IF (iflag1(i)==0) THEN
-        nn = nn + 1
-        t(nn, k) = t1(i, k)
-        q(nn, k) = q1(i, k)
-        qs(nn, k) = qs1(i, k)
-        u(nn, k) = u1(i, k)
-        v(nn, k) = v1(i, k)
-        gz(nn, k) = gz1(i, k)
-        h(nn, k) = h1(i, k)
-        lv(nn, k) = lv1(i, k)
-        cpn(nn, k) = cpn1(i, k)
-        p(nn, k) = p1(i, k)
-        ph(nn, k) = ph1(i, k)
-        tv(nn, k) = tv1(i, k)
-        tp(nn, k) = tp1(i, k)
-        tvp(nn, k) = tvp1(i, k)
-        clw(nn, k) = clw1(i, k)
-      END IF
-    END DO
-  END DO
-
-  IF (nn/=ncum) THEN
-    WRITE (lunout, *) 'strange! nn not equal to ncum: ', nn, ncum
-    abort_message = ''
-    CALL abort_physic(modname, abort_message, 1)
-  END IF
-
-  nn = 0
-  DO i = 1, len
-    IF (iflag1(i)==0) THEN
-      nn = nn + 1
-      cbmf(nn) = cbmf1(i)
-      plcl(nn) = plcl1(i)
-      tnk(nn) = tnk1(i)
-      qnk(nn) = qnk1(i)
-      gznk(nn) = gznk1(i)
-      nk(nn) = nk1(i)
-      icb(nn) = icb1(i)
-      iflag(nn) = iflag1(i)
-    END IF
-  END DO
-
-  DO k = 1, nl
-    DO i = 1, ncum
-      dph(i, k) = ph(i, k) - ph(i, k + 1)
-    END DO
-  END DO
-
-END SUBROUTINE cv_compress
-
-SUBROUTINE cv_undilute2(nloc, ncum, nd, icb, nk, tnk, qnk, gznk, t, q, qs, &
-        gz, p, dph, h, tv, lv, inb, inb1, tp, tvp, clw, hp, ep, sigp, frac)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  ! ---------------------------------------------------------------------
-  ! Purpose:
-  ! FIND THE REST OF THE LIFTED PARCEL TEMPERATURES
-  ! &
-  ! COMPUTE THE PRECIPITATION EFFICIENCIES AND THE
-  ! FRACTION OF PRECIPITATION FALLING OUTSIDE OF CLOUD
-  ! &
-  ! FIND THE LEVEL OF NEUTRAL BUOYANCY
-  ! ---------------------------------------------------------------------
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER ncum, nd, nloc
-  INTEGER icb(nloc), nk(nloc)
-  REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd), gz(nloc, nd)
-  REAL p(nloc, nd), dph(nloc, nd)
-  REAL tnk(nloc), qnk(nloc), gznk(nloc)
-  REAL lv(nloc, nd), tv(nloc, nd), h(nloc, nd)
-
-  ! outputs:
-  INTEGER inb(nloc), inb1(nloc)
-  REAL tp(nloc, nd), tvp(nloc, nd), clw(nloc, nd)
-  REAL ep(nloc, nd), sigp(nloc, nd), hp(nloc, nd)
-  REAL frac(nloc)
-
-  ! local variables:
-  INTEGER i, k
-  REAL tg, qg, ahg, alv, s, tc, es, denom, rg, tca, elacrit
-  REAL by, defrac
-  REAL ah0(nloc), cape(nloc), capem(nloc), byp(nloc)
-  LOGICAL lcape(nloc)
-
-  ! =====================================================================
-  ! --- SOME INITIALIZATIONS
-  ! =====================================================================
-
-  DO k = 1, nl
-    DO i = 1, ncum
-      ep(i, k) = 0.0
-      sigp(i, k) = sigs
-    END DO
-  END DO
-
-  ! =====================================================================
-  ! --- FIND THE REST OF THE LIFTED PARCEL TEMPERATURES
-  ! =====================================================================
-
-  ! ---       The procedure is to solve the equation.
-  ! cp*tp+L*qp+phi=cp*tnk+L*qnk+gznk.
-
-  ! ***  Calculate certain parcel quantities, including static energy   ***
-
-  DO i = 1, ncum
-    ah0(i) = (cpd * (1. - qnk(i)) + cl * qnk(i)) * tnk(i) + qnk(i) * (lv0 - clmcpv * (tnk(i) - &
-            t0)) + gznk(i)
-  END DO
-
-
-  ! ***  Find lifted parcel quantities above cloud base    ***
-
-  DO k = minorig + 1, nl
-    DO i = 1, ncum
-      IF (k>=(icb(i) + 1)) THEN
-        tg = t(i, k)
-        qg = qs(i, k)
-        alv = lv0 - clmcpv * (t(i, k) - t0)
-
-        ! First iteration.
-
-        s = cpd + alv * alv * qg / (rrv * t(i, k) * t(i, k))
-        s = 1. / s
-        ahg = cpd * tg + (cl - cpd) * qnk(i) * t(i, k) + alv * qg + gz(i, k)
-        tg = tg + s * (ah0(i) - ahg)
-        tg = max(tg, 35.0)
-        tc = tg - t0
-        denom = 243.5 + tc
-        IF (tc>=0.0) THEN
-          es = 6.112 * exp(17.67 * tc / denom)
-        ELSE
-          es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
-        END IF
-        qg = eps * es / (p(i, k) - es * (1. - eps))
-
-        ! Second iteration.
-
-        s = cpd + alv * alv * qg / (rrv * t(i, k) * t(i, k))
-        s = 1. / s
-        ahg = cpd * tg + (cl - cpd) * qnk(i) * t(i, k) + alv * qg + gz(i, k)
-        tg = tg + s * (ah0(i) - ahg)
-        tg = max(tg, 35.0)
-        tc = tg - t0
-        denom = 243.5 + tc
-        IF (tc>=0.0) THEN
-          es = 6.112 * exp(17.67 * tc / denom)
-        ELSE
-          es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
-        END IF
-        qg = eps * es / (p(i, k) - es * (1. - eps))
-
-        alv = lv0 - clmcpv * (t(i, k) - t0)
-        ! PRINT*,'cpd dans convect2 ',cpd
-        ! PRINT*,'tp(i,k),ah0(i),cl,cpd,qnk(i),t(i,k),gz(i,k),alv,qg,cpd'
-        ! PRINT*,tp(i,k),ah0(i),cl,cpd,qnk(i),t(i,k),gz(i,k),alv,qg,cpd
-        tp(i, k) = (ah0(i) - (cl - cpd) * qnk(i) * t(i, k) - gz(i, k) - alv * qg) / cpd
-        ! if (.NOT.cpd.gt.1000.) THEN
-        ! PRINT*,'CPD=',cpd
-        ! stop
-        ! END IF
-        clw(i, k) = qnk(i) - qg
-        clw(i, k) = max(0.0, clw(i, k))
-        rg = qg / (1. - qnk(i))
-        tvp(i, k) = tp(i, k) * (1. + rg * epsi)
-      END IF
-    END DO
-  END DO
-
-  ! =====================================================================
-  ! --- SET THE PRECIPITATION EFFICIENCIES AND THE FRACTION OF
-  ! --- PRECIPITATION FALLING OUTSIDE OF CLOUD
-  ! --- THESE MAY BE FUNCTIONS OF TP(I), P(I) AND CLW(I)
-  ! =====================================================================
-
-  DO k = minorig + 1, nl
-    DO i = 1, ncum
-      IF (k>=(nk(i) + 1)) THEN
-        tca = tp(i, k) - t0
-        IF (tca>=0.0) THEN
-          elacrit = elcrit
-        ELSE
-          elacrit = elcrit * (1.0 - tca / tlcrit)
-        END IF
-        elacrit = max(elacrit, 0.0)
-        ep(i, k) = 1.0 - elacrit / max(clw(i, k), 1.0E-8)
-        ep(i, k) = max(ep(i, k), 0.0)
-        ep(i, k) = min(ep(i, k), 1.0)
-        sigp(i, k) = sigs
-      END IF
-    END DO
-  END DO
-
-  ! =====================================================================
-  ! --- CALCULATE VIRTUAL TEMPERATURE AND LIFTED PARCEL
-  ! --- VIRTUAL TEMPERATURE
-  ! =====================================================================
-
-  DO k = minorig + 1, nl
-    DO i = 1, ncum
-      IF (k>=(icb(i) + 1)) THEN
-        tvp(i, k) = tvp(i, k) * (1.0 - qnk(i) + ep(i, k) * clw(i, k))
-        ! PRINT*,'i,k,tvp(i,k),qnk(i),ep(i,k),clw(i,k)'
-        ! PRINT*, i,k,tvp(i,k),qnk(i),ep(i,k),clw(i,k)
-      END IF
-    END DO
-  END DO
-  DO i = 1, ncum
-    tvp(i, nlp) = tvp(i, nl) - (gz(i, nlp) - gz(i, nl)) / cpd
-  END DO
-
-  ! =====================================================================
-  ! --- FIND THE FIRST MODEL LEVEL (INB1) ABOVE THE PARCEL'S
-  ! --- HIGHEST LEVEL OF NEUTRAL BUOYANCY
-  ! --- AND THE HIGHEST LEVEL OF POSITIVE CAPE (INB)
-  ! =====================================================================
-
-  DO i = 1, ncum
-    cape(i) = 0.0
-    capem(i) = 0.0
-    inb(i) = icb(i) + 1
-    inb1(i) = inb(i)
-  END DO
-
-  ! Originial Code
-
-  ! do 530 k=minorig+1,nl-1
-  ! do 520 i=1,ncum
-  ! IF(k.ge.(icb(i)+1))THEN
-  ! by=(tvp(i,k)-tv(i,k))*dph(i,k)/p(i,k)
-  ! byp=(tvp(i,k+1)-tv(i,k+1))*dph(i,k+1)/p(i,k+1)
-  ! cape(i)=cape(i)+by
-  ! IF(by.ge.0.0)inb1(i)=k+1
-  ! IF(cape(i).gt.0.0)THEN
-  ! inb(i)=k+1
-  ! capem(i)=cape(i)
-  ! END IF
-  ! END IF
-  ! 520    continue
-  ! 530  continue
-  ! do 540 i=1,ncum
-  ! byp=(tvp(i,nl)-tv(i,nl))*dph(i,nl)/p(i,nl)
-  ! cape(i)=capem(i)+byp
-  ! defrac=capem(i)-cape(i)
-  ! defrac=max(defrac,0.001)
-  ! frac(i)=-cape(i)/defrac
-  ! frac(i)=min(frac(i),1.0)
-  ! frac(i)=max(frac(i),0.0)
-  ! 540   continue
-
-  ! K Emanuel fix
-
-  ! CALL zilch(byp,ncum)
-  ! do 530 k=minorig+1,nl-1
-  ! do 520 i=1,ncum
-  ! IF(k.ge.(icb(i)+1))THEN
-  ! by=(tvp(i,k)-tv(i,k))*dph(i,k)/p(i,k)
-  ! cape(i)=cape(i)+by
-  ! IF(by.ge.0.0)inb1(i)=k+1
-  ! IF(cape(i).gt.0.0)THEN
-  ! inb(i)=k+1
-  ! capem(i)=cape(i)
-  ! byp(i)=(tvp(i,k+1)-tv(i,k+1))*dph(i,k+1)/p(i,k+1)
-  ! END IF
-  ! END IF
-  ! 520    continue
-  ! 530  continue
-  ! do 540 i=1,ncum
-  ! inb(i)=max(inb(i),inb1(i))
-  ! cape(i)=capem(i)+byp(i)
-  ! defrac=capem(i)-cape(i)
-  ! defrac=max(defrac,0.001)
-  ! frac(i)=-cape(i)/defrac
-  ! frac(i)=min(frac(i),1.0)
-  ! frac(i)=max(frac(i),0.0)
-  ! 540   continue
-
-  ! J Teixeira fix
-
-  CALL zilch(byp, ncum)
-  DO i = 1, ncum
-    lcape(i) = .TRUE.
-  END DO
-  DO k = minorig + 1, nl - 1
-    DO i = 1, ncum
-      IF (cape(i)<0.0) lcape(i) = .FALSE.
-      IF ((k>=(icb(i) + 1)) .AND. lcape(i)) THEN
-        by = (tvp(i, k) - tv(i, k)) * dph(i, k) / p(i, k)
-        byp(i) = (tvp(i, k + 1) - tv(i, k + 1)) * dph(i, k + 1) / p(i, k + 1)
-        cape(i) = cape(i) + by
-        IF (by>=0.0) inb1(i) = k + 1
-        IF (cape(i)>0.0) THEN
-          inb(i) = k + 1
-          capem(i) = cape(i)
-        END IF
-      END IF
-    END DO
-  END DO
-  DO i = 1, ncum
-    cape(i) = capem(i) + byp(i)
-    defrac = capem(i) - cape(i)
-    defrac = max(defrac, 0.001)
-    frac(i) = -cape(i) / defrac
-    frac(i) = min(frac(i), 1.0)
-    frac(i) = max(frac(i), 0.0)
-  END DO
-
-  ! =====================================================================
-  ! ---   CALCULATE LIQUID WATER STATIC ENERGY OF LIFTED PARCEL
-  ! =====================================================================
-
-  ! initialization:
-  DO i = 1, ncum * nlp
-    hp(i, 1) = h(i, 1)
-  END DO
-
-  DO k = minorig + 1, nl
-    DO i = 1, ncum
-      IF ((k>=icb(i)) .AND. (k<=inb(i))) THEN
-        hp(i, k) = h(i, nk(i)) + (lv(i, k) + (cpd - cpv) * t(i, k)) * ep(i, k) * clw(i, k &
-                )
-      END IF
-    END DO
-  END DO
-
-END SUBROUTINE cv_undilute2
-
-SUBROUTINE cv_closure(nloc, ncum, nd, nk, icb, tv, tvp, p, ph, dph, plcl, &
-        cpn, iflag, cbmf)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  ! inputs:
-  INTEGER ncum, nd, nloc
-  INTEGER nk(nloc), icb(nloc)
-  REAL tv(nloc, nd), tvp(nloc, nd), p(nloc, nd), dph(nloc, nd)
-  REAL ph(nloc, nd + 1) ! caution nd instead ndp1 to be consistent...
-  REAL plcl(nloc), cpn(nloc, nd)
-
-  ! outputs:
-  INTEGER iflag(nloc)
-  REAL cbmf(nloc) ! also an input
-
-  ! local variables:
-  INTEGER i, k, icbmax
-  REAL dtpbl(nloc), dtmin(nloc), tvpplcl(nloc), tvaplcl(nloc)
-  REAL work(nloc)
-
-  include "cvparam.h"
-
-  ! -------------------------------------------------------------------
-  ! Compute icbmax.
-  ! -------------------------------------------------------------------
-
-  icbmax = 2
-  DO i = 1, ncum
-    icbmax = max(icbmax, icb(i))
-  END DO
-
-  ! =====================================================================
-  ! ---  CALCULATE CLOUD BASE MASS FLUX
-  ! =====================================================================
-
-  ! tvpplcl = parcel temperature lifted adiabatically from level
-  ! icb-1 to the LCL.
-  ! tvaplcl = virtual temperature at the LCL.
-
-  DO i = 1, ncum
-    dtpbl(i) = 0.0
-    tvpplcl(i) = tvp(i, icb(i) - 1) - rrd * tvp(i, icb(i) - 1) * (p(i, icb(i) - 1) - plcl(&
-            i)) / (cpn(i, icb(i) - 1) * p(i, icb(i) - 1))
-    tvaplcl(i) = tv(i, icb(i)) + (tvp(i, icb(i)) - tvp(i, icb(i) + 1)) * (plcl(i) - p(i &
-            , icb(i))) / (p(i, icb(i)) - p(i, icb(i) + 1))
-  END DO
-
-  ! -------------------------------------------------------------------
-  ! --- Interpolate difference between lifted parcel and
-  ! --- environmental temperatures to lifted condensation level
-  ! -------------------------------------------------------------------
-
-  ! dtpbl = average of tvp-tv in the PBL (k=nk to icb-1).
-
-  DO k = minorig, icbmax
-    DO i = 1, ncum
-      IF ((k>=nk(i)) .AND. (k<=(icb(i) - 1))) THEN
-        dtpbl(i) = dtpbl(i) + (tvp(i, k) - tv(i, k)) * dph(i, k)
-      END IF
-    END DO
-  END DO
-  DO i = 1, ncum
-    dtpbl(i) = dtpbl(i) / (ph(i, nk(i)) - ph(i, icb(i)))
-    dtmin(i) = tvpplcl(i) - tvaplcl(i) + dtmax + dtpbl(i)
-  END DO
-
-  ! -------------------------------------------------------------------
-  ! --- Adjust cloud base mass flux
-  ! -------------------------------------------------------------------
-
-  DO i = 1, ncum
-    work(i) = cbmf(i)
-    cbmf(i) = max(0.0, (1.0 - damp) * cbmf(i) + 0.1 * alpha * dtmin(i))
-    IF ((work(i)==0.0) .AND. (cbmf(i)==0.0)) THEN
-      iflag(i) = 3
-    END IF
-  END DO
-
-END SUBROUTINE cv_closure
-
-SUBROUTINE cv_mixing(nloc, ncum, nd, icb, nk, inb, inb1, ph, t, q, qs, u, v, &
-        h, lv, qnk, hp, tv, tvp, ep, clw, cbmf, m, ment, qent, uent, vent, nent, &
-        sij, elij)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER ncum, nd, nloc
-  INTEGER icb(nloc), inb(nloc), inb1(nloc), nk(nloc)
-  REAL cbmf(nloc), qnk(nloc)
-  REAL ph(nloc, nd + 1)
-  REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd), lv(nloc, nd)
-  REAL u(nloc, nd), v(nloc, nd), h(nloc, nd), hp(nloc, nd)
-  REAL tv(nloc, nd), tvp(nloc, nd), ep(nloc, nd), clw(nloc, nd)
-
-  ! outputs:
-  INTEGER nent(nloc, nd)
-  REAL m(nloc, nd), ment(nloc, nd, nd), qent(nloc, nd, nd)
-  REAL uent(nloc, nd, nd), vent(nloc, nd, nd)
-  REAL sij(nloc, nd, nd), elij(nloc, nd, nd)
-
-  ! local variables:
-  INTEGER i, j, k, ij
-  INTEGER num1, num2
-  REAL dbo, qti, bf2, anum, denom, dei, altem, cwat, stemp
-  REAL alt, qp1, smid, sjmin, sjmax, delp, delm
-  REAL work(nloc), asij(nloc), smin(nloc), scrit(nloc)
-  REAL bsum(nloc, nd)
-  LOGICAL lwork(nloc)
-
-  ! =====================================================================
-  ! --- INITIALIZE VARIOUS ARRAYS USED IN THE COMPUTATIONS
-  ! =====================================================================
-
-  DO i = 1, ncum * nlp
-    nent(i, 1) = 0
-    m(i, 1) = 0.0
-  END DO
-
-  DO k = 1, nlp
-    DO j = 1, nlp
-      DO i = 1, ncum
-        qent(i, k, j) = q(i, j)
-        uent(i, k, j) = u(i, j)
-        vent(i, k, j) = v(i, j)
-        elij(i, k, j) = 0.0
-        ment(i, k, j) = 0.0
-        sij(i, k, j) = 0.0
-      END DO
-    END DO
-  END DO
-
-  ! -------------------------------------------------------------------
-  ! --- Calculate rates of mixing,  m(i)
-  ! -------------------------------------------------------------------
-
-  CALL zilch(work, ncum)
-
-  DO j = minorig + 1, nl
-    DO i = 1, ncum
-      IF ((j>=(icb(i) + 1)) .AND. (j<=inb(i))) THEN
-        k = min(j, inb1(i))
-        dbo = abs(tv(i, k + 1) - tvp(i, k + 1) - tv(i, k - 1) + tvp(i, k - 1)) + &
-                entp * 0.04 * (ph(i, k) - ph(i, k + 1))
-        work(i) = work(i) + dbo
-        m(i, j) = cbmf(i) * dbo
-      END IF
-    END DO
-  END DO
-  DO k = minorig + 1, nl
-    DO i = 1, ncum
-      IF ((k>=(icb(i) + 1)) .AND. (k<=inb(i))) THEN
-        m(i, k) = m(i, k) / work(i)
-      END IF
-    END DO
-  END DO
-
-
-  ! =====================================================================
-  ! --- CALCULATE ENTRAINED AIR MASS FLUX (ment), TOTAL WATER MIXING
-  ! --- RATIO (QENT), TOTAL CONDENSED WATER (elij), AND MIXING
-  ! --- FRACTION (sij)
-  ! =====================================================================
-
-  DO i = minorig + 1, nl
-    DO j = minorig + 1, nl
-      DO ij = 1, ncum
-        IF ((i>=(icb(ij) + 1)) .AND. (j>=icb(ij)) .AND. (i<=inb(ij)) .AND. (j<= &
-                inb(ij))) THEN
-          qti = qnk(ij) - ep(ij, i) * clw(ij, i)
-          bf2 = 1. + lv(ij, j) * lv(ij, j) * qs(ij, j) / (rrv * t(ij, j) * t(ij, j) * cpd)
-          anum = h(ij, j) - hp(ij, i) + (cpv - cpd) * t(ij, j) * (qti - q(ij, j))
-          denom = h(ij, i) - hp(ij, i) + (cpd - cpv) * (q(ij, i) - qti) * t(ij, j)
-          dei = denom
-          IF (abs(dei)<0.01) dei = 0.01
-          sij(ij, i, j) = anum / dei
-          sij(ij, i, i) = 1.0
-          altem = sij(ij, i, j) * q(ij, i) + (1. - sij(ij, i, j)) * qti - qs(ij, j)
-          altem = altem / bf2
-          cwat = clw(ij, j) * (1. - ep(ij, j))
-          stemp = sij(ij, i, j)
-          IF ((stemp<0.0 .OR. stemp>1.0 .OR. altem>cwat) .AND. j>i) THEN
-            anum = anum - lv(ij, j) * (qti - qs(ij, j) - cwat * bf2)
-            denom = denom + lv(ij, j) * (q(ij, i) - qti)
-            IF (abs(denom)<0.01) denom = 0.01
-            sij(ij, i, j) = anum / denom
-            altem = sij(ij, i, j) * q(ij, i) + (1. - sij(ij, i, j)) * qti - qs(ij, j)
-            altem = altem - (bf2 - 1.) * cwat
-          END IF
-          IF (sij(ij, i, j)>0.0 .AND. sij(ij, i, j)<0.9) THEN
-            qent(ij, i, j) = sij(ij, i, j) * q(ij, i) + (1. - sij(ij, i, j)) * qti
-            uent(ij, i, j) = sij(ij, i, j) * u(ij, i) + &
-                    (1. - sij(ij, i, j)) * u(ij, nk(ij))
-            vent(ij, i, j) = sij(ij, i, j) * v(ij, i) + &
-                    (1. - sij(ij, i, j)) * v(ij, nk(ij))
-            elij(ij, i, j) = altem
-            elij(ij, i, j) = max(0.0, elij(ij, i, j))
-            ment(ij, i, j) = m(ij, i) / (1. - sij(ij, i, j))
-            nent(ij, i) = nent(ij, i) + 1
-          END IF
-          sij(ij, i, j) = max(0.0, sij(ij, i, j))
-          sij(ij, i, j) = min(1.0, sij(ij, i, j))
-        END IF
-      END DO
-    END DO
-
-    ! ***   If no air can entrain at level i assume that updraft detrains
-    ! ***
-    ! ***   at that level and calculate detrained air flux and properties
-    ! ***
-
-    DO ij = 1, ncum
-      IF ((i>=(icb(ij) + 1)) .AND. (i<=inb(ij)) .AND. (nent(ij, i)==0)) THEN
-        ment(ij, i, i) = m(ij, i)
-        qent(ij, i, i) = q(ij, nk(ij)) - ep(ij, i) * clw(ij, i)
-        uent(ij, i, i) = u(ij, nk(ij))
-        vent(ij, i, i) = v(ij, nk(ij))
-        elij(ij, i, i) = clw(ij, i)
-        sij(ij, i, i) = 1.0
-      END IF
-    END DO
-  END DO
-
-  DO i = 1, ncum
-    sij(i, inb(i), inb(i)) = 1.0
-  END DO
-
-  ! =====================================================================
-  ! ---  NORMALIZE ENTRAINED AIR MASS FLUXES
-  ! ---  TO REPRESENT EQUAL PROBABILITIES OF MIXING
-  ! =====================================================================
-
-  CALL zilch(bsum, ncum * nlp)
-  DO ij = 1, ncum
-    lwork(ij) = .FALSE.
-  END DO
-  DO i = minorig + 1, nl
-
-    num1 = 0
-    DO ij = 1, ncum
-      IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij))) num1 = num1 + 1
-    END DO
-    IF (num1<=0) GO TO 789
-
-    DO ij = 1, ncum
-      IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij))) THEN
-        lwork(ij) = (nent(ij, i)/=0)
-        qp1 = q(ij, nk(ij)) - ep(ij, i) * clw(ij, i)
-        anum = h(ij, i) - hp(ij, i) - lv(ij, i) * (qp1 - qs(ij, i))
-        denom = h(ij, i) - hp(ij, i) + lv(ij, i) * (q(ij, i) - qp1)
-        IF (abs(denom)<0.01) denom = 0.01
-        scrit(ij) = anum / denom
-        alt = qp1 - qs(ij, i) + scrit(ij) * (q(ij, i) - qp1)
-        IF (scrit(ij)<0.0 .OR. alt<0.0) scrit(ij) = 1.0
-        asij(ij) = 0.0
-        smin(ij) = 1.0
-      END IF
-    END DO
-    DO j = minorig, nl
-
-      num2 = 0
-      DO ij = 1, ncum
-        IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (j>=icb(&
-                ij)) .AND. (j<=inb(ij)) .AND. lwork(ij)) num2 = num2 + 1
-      END DO
-      IF (num2<=0) GO TO 783
-
-      DO ij = 1, ncum
-        IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (j>=icb(&
-                ij)) .AND. (j<=inb(ij)) .AND. lwork(ij)) THEN
-          IF (sij(ij, i, j)>0.0 .AND. sij(ij, i, j)<0.9) THEN
-            IF (j>i) THEN
-              smid = min(sij(ij, i, j), scrit(ij))
-              sjmax = smid
-              sjmin = smid
-              IF (smid<smin(ij) .AND. sij(ij, i, j + 1)<smid) THEN
-                smin(ij) = smid
-                sjmax = min(sij(ij, i, j + 1), sij(ij, i, j), scrit(ij))
-                sjmin = max(sij(ij, i, j - 1), sij(ij, i, j))
-                sjmin = min(sjmin, scrit(ij))
-              END IF
-            ELSE
-              sjmax = max(sij(ij, i, j + 1), scrit(ij))
-              smid = max(sij(ij, i, j), scrit(ij))
-              sjmin = 0.0
-              IF (j>1) sjmin = sij(ij, i, j - 1)
-              sjmin = max(sjmin, scrit(ij))
-            END IF
-            delp = abs(sjmax - smid)
-            delm = abs(sjmin - smid)
-            asij(ij) = asij(ij) + (delp + delm) * (ph(ij, j) - ph(ij, j + 1))
-            ment(ij, i, j) = ment(ij, i, j) * (delp + delm) * (ph(ij, j) - ph(ij, j + 1))
-          END IF
-        END IF
-      END DO
-    783 END DO
-    DO ij = 1, ncum
-      IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. lwork(ij)) THEN
-        asij(ij) = max(1.0E-21, asij(ij))
-        asij(ij) = 1.0 / asij(ij)
-        bsum(ij, i) = 0.0
-      END IF
-    END DO
-    DO j = minorig, nl + 1
-      DO ij = 1, ncum
-        IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (j>=icb(&
-                ij)) .AND. (j<=inb(ij)) .AND. lwork(ij)) THEN
-          ment(ij, i, j) = ment(ij, i, j) * asij(ij)
-          bsum(ij, i) = bsum(ij, i) + ment(ij, i, j)
-        END IF
-      END DO
-    END DO
-    DO ij = 1, ncum
-      IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (bsum(ij, &
-              i)<1.0E-18) .AND. lwork(ij)) THEN
-        nent(ij, i) = 0
-        ment(ij, i, i) = m(ij, i)
-        qent(ij, i, i) = q(ij, nk(ij)) - ep(ij, i) * clw(ij, i)
-        uent(ij, i, i) = u(ij, nk(ij))
-        vent(ij, i, i) = v(ij, nk(ij))
-        elij(ij, i, i) = clw(ij, i)
-        sij(ij, i, i) = 1.0
-      END IF
-    END DO
-  789 END DO
-
-END SUBROUTINE cv_mixing
-
-SUBROUTINE cv_unsat(nloc, ncum, nd, inb, t, q, qs, gz, u, v, p, ph, h, lv, &
-        ep, sigp, clw, m, ment, elij, iflag, mp, qp, up, vp, wt, water, evap)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER ncum, nd, nloc
-  INTEGER inb(nloc)
-  REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd)
-  REAL gz(nloc, nd), u(nloc, nd), v(nloc, nd)
-  REAL p(nloc, nd), ph(nloc, nd + 1), h(nloc, nd)
-  REAL lv(nloc, nd), ep(nloc, nd), sigp(nloc, nd), clw(nloc, nd)
-  REAL m(nloc, nd), ment(nloc, nd, nd), elij(nloc, nd, nd)
-
-  ! outputs:
-  INTEGER iflag(nloc) ! also an input
-  REAL mp(nloc, nd), qp(nloc, nd), up(nloc, nd), vp(nloc, nd)
-  REAL water(nloc, nd), evap(nloc, nd), wt(nloc, nd)
-
-  ! local variables:
-  INTEGER i, j, k, ij, num1
-  INTEGER jtt(nloc)
-  REAL awat, coeff, qsm, afac, sigt, b6, c6, revap
-  REAL dhdp, fac, qstm, rat
-  REAL wdtrain(nloc)
-  LOGICAL lwork(nloc)
-
-  ! =====================================================================
-  ! --- PRECIPITATING DOWNDRAFT CALCULATION
-  ! =====================================================================
-
-  ! Initializations:
-
-  DO i = 1, ncum
-    DO k = 1, nl + 1
-      wt(i, k) = omtsnow
-      mp(i, k) = 0.0
-      evap(i, k) = 0.0
-      water(i, k) = 0.0
-    END DO
-  END DO
-
-  DO i = 1, ncum
-    qp(i, 1) = q(i, 1)
-    up(i, 1) = u(i, 1)
-    vp(i, 1) = v(i, 1)
-  END DO
-
-  DO k = 2, nl + 1
-    DO i = 1, ncum
-      qp(i, k) = q(i, k - 1)
-      up(i, k) = u(i, k - 1)
-      vp(i, k) = v(i, k - 1)
-    END DO
-  END DO
-
-
-  ! ***  Check whether ep(inb)=0, if so, skip precipitating    ***
-  ! ***             downdraft calculation                      ***
-
-
-  ! ***  Integrate liquid water equation to find condensed water   ***
-  ! ***                and condensed water flux                    ***
-
-  DO i = 1, ncum
-    jtt(i) = 2
-    IF (ep(i, inb(i))<=0.0001) iflag(i) = 2
-    IF (iflag(i)==0) THEN
-      lwork(i) = .TRUE.
-    ELSE
-      lwork(i) = .FALSE.
-    END IF
-  END DO
-
-  ! ***                    Begin downdraft loop                    ***
-
-  CALL zilch(wdtrain, ncum)
-  DO i = nl + 1, 1, -1
-
-    num1 = 0
-    DO ij = 1, ncum
-      IF ((i<=inb(ij)) .AND. lwork(ij)) num1 = num1 + 1
-    END DO
-    IF (num1<=0) GO TO 899
-
-
-    ! ***        Calculate detrained precipitation             ***
-
-    DO ij = 1, ncum
-      IF ((i<=inb(ij)) .AND. (lwork(ij))) THEN
-        wdtrain(ij) = g * ep(ij, i) * m(ij, i) * clw(ij, i)
-      END IF
-    END DO
-
-    IF (i>1) THEN
-      DO j = 1, i - 1
-        DO ij = 1, ncum
-          IF ((i<=inb(ij)) .AND. (lwork(ij))) THEN
-            awat = elij(ij, j, i) - (1. - ep(ij, i)) * clw(ij, i)
-            awat = max(0.0, awat)
-            wdtrain(ij) = wdtrain(ij) + g * awat * ment(ij, j, i)
-          END IF
-        END DO
-      END DO
-    END IF
-
-    ! ***    Find rain water and evaporation using provisional   ***
-    ! ***              estimates of qp(i)and qp(i-1)             ***
-
-
-    ! ***  Value of terminal velocity and coeffecient of evaporation for snow
-    ! ***
-
-    DO ij = 1, ncum
-      IF ((i<=inb(ij)) .AND. (lwork(ij))) THEN
-        coeff = coeffs
-        wt(ij, i) = omtsnow
-
-        ! ***  Value of terminal velocity and coeffecient of evaporation for
-        ! rain   ***
-
-        IF (t(ij, i)>273.0) THEN
-          coeff = coeffr
-          wt(ij, i) = omtrain
-        END IF
-        qsm = 0.5 * (q(ij, i) + qp(ij, i + 1))
-        afac = coeff * ph(ij, i) * (qs(ij, i) - qsm) / (1.0E4 + 2.0E3 * ph(ij, i) * qs(ij, i))
-        afac = max(afac, 0.0)
-        sigt = sigp(ij, i)
-        sigt = max(0.0, sigt)
-        sigt = min(1.0, sigt)
-        b6 = 100. * (ph(ij, i) - ph(ij, i + 1)) * sigt * afac / wt(ij, i)
-        c6 = (water(ij, i + 1) * wt(ij, i + 1) + wdtrain(ij) / sigd) / wt(ij, i)
-        revap = 0.5 * (-b6 + sqrt(b6 * b6 + 4. * c6))
-        evap(ij, i) = sigt * afac * revap
-        water(ij, i) = revap * revap
-
-        ! ***  Calculate precipitating downdraft mass flux under     ***
-        ! ***              hydrostatic approximation                 ***
-
-        IF (i>1) THEN
-          dhdp = (h(ij, i) - h(ij, i - 1)) / (p(ij, i - 1) - p(ij, i))
-          dhdp = max(dhdp, 10.0)
-          mp(ij, i) = 100. * ginv * lv(ij, i) * sigd * evap(ij, i) / dhdp
-          mp(ij, i) = max(mp(ij, i), 0.0)
-
-          ! ***   Add small amount of inertia to downdraft              ***
-
-          fac = 20.0 / (ph(ij, i - 1) - ph(ij, i))
-          mp(ij, i) = (fac * mp(ij, i + 1) + mp(ij, i)) / (1. + fac)
-
-          ! ***      Force mp to decrease linearly to zero
-          ! ***
-          ! ***      between about 950 mb and the surface
-          ! ***
-
-          IF (p(ij, i)>(0.949 * p(ij, 1))) THEN
-            jtt(ij) = max(jtt(ij), i)
-            mp(ij, i) = mp(ij, jtt(ij)) * (p(ij, 1) - p(ij, i)) / &
-                    (p(ij, 1) - p(ij, jtt(ij)))
-          END IF
-        END IF
-
-        ! ***       Find mixing ratio of precipitating downdraft     ***
-
-        IF (i/=inb(ij)) THEN
-          IF (i==1) THEN
-            qstm = qs(ij, 1)
-          ELSE
-            qstm = qs(ij, i - 1)
-          END IF
-          IF (mp(ij, i)>mp(ij, i + 1)) THEN
-            rat = mp(ij, i + 1) / mp(ij, i)
-            qp(ij, i) = qp(ij, i + 1) * rat + q(ij, i) * (1.0 - rat) + &
-                    100. * ginv * sigd * (ph(ij, i) - ph(ij, i + 1)) * (evap(ij, i) / mp(ij, i))
-            up(ij, i) = up(ij, i + 1) * rat + u(ij, i) * (1. - rat)
-            vp(ij, i) = vp(ij, i + 1) * rat + v(ij, i) * (1. - rat)
-          ELSE
-            IF (mp(ij, i + 1)>0.0) THEN
-              qp(ij, i) = (gz(ij, i + 1) - gz(ij, i) + qp(ij, i + 1) * (lv(ij, i + 1) + t(ij, &
-                      i + 1) * (cl - cpd)) + cpd * (t(ij, i + 1) - t(ij, &
-                      i))) / (lv(ij, i) + t(ij, i) * (cl - cpd))
-              up(ij, i) = up(ij, i + 1)
-              vp(ij, i) = vp(ij, i + 1)
-            END IF
-          END IF
-          qp(ij, i) = min(qp(ij, i), qstm)
-          qp(ij, i) = max(qp(ij, i), 0.0)
-        END IF
-      END IF
-    END DO
-  899 END DO
-
-END SUBROUTINE cv_unsat
-
-SUBROUTINE cv_yield(nloc, ncum, nd, nk, icb, inb, delt, t, q, u, v, gz, p, &
-        ph, h, hp, lv, cpn, ep, clw, frac, m, mp, qp, up, vp, wt, water, evap, &
-        ment, qent, uent, vent, nent, elij, tv, tvp, iflag, wd, qprime, tprime, &
-        precip, cbmf, ft, fq, fu, fv, ma, qcondc)
-  USE lmdz_cvthermo
-
-  IMPLICIT NONE
-
-  include "cvparam.h"
-
-  ! inputs
-  INTEGER ncum, nd, nloc
-  INTEGER nk(nloc), icb(nloc), inb(nloc)
-  INTEGER nent(nloc, nd)
-  REAL delt
-  REAL t(nloc, nd), q(nloc, nd), u(nloc, nd), v(nloc, nd)
-  REAL gz(nloc, nd)
-  REAL p(nloc, nd), ph(nloc, nd + 1), h(nloc, nd)
-  REAL hp(nloc, nd), lv(nloc, nd)
-  REAL cpn(nloc, nd), ep(nloc, nd), clw(nloc, nd), frac(nloc)
-  REAL m(nloc, nd), mp(nloc, nd), qp(nloc, nd)
-  REAL up(nloc, nd), vp(nloc, nd)
-  REAL wt(nloc, nd), water(nloc, nd), evap(nloc, nd)
-  REAL ment(nloc, nd, nd), qent(nloc, nd, nd), elij(nloc, nd, nd)
-  REAL uent(nloc, nd, nd), vent(nloc, nd, nd)
-  REAL tv(nloc, nd), tvp(nloc, nd)
-
-  ! outputs
-  INTEGER iflag(nloc) ! also an input
-  REAL cbmf(nloc) ! also an input
-  REAL wd(nloc), tprime(nloc), qprime(nloc)
-  REAL precip(nloc)
-  REAL ft(nloc, nd), fq(nloc, nd), fu(nloc, nd), fv(nloc, nd)
-  REAL ma(nloc, nd)
-  REAL qcondc(nloc, nd)
-
-  ! local variables
-  INTEGER i, j, ij, k, num1
-  REAL dpinv, cpinv, awat, fqold, ftold, fuold, fvold, delti
-  REAL work(nloc), am(nloc), amp1(nloc), ad(nloc)
-  REAL ents(nloc), uav(nloc), vav(nloc), lvcp(nloc, nd)
-  REAL qcond(nloc, nd), nqcond(nloc, nd), wa(nloc, nd) ! cld
-  REAL siga(nloc, nd), ax(nloc, nd), mac(nloc, nd) ! cld
-
-
-  ! -- initializations:
-
-  delti = 1.0 / delt
-
-  DO i = 1, ncum
-    precip(i) = 0.0
-    wd(i) = 0.0
-    tprime(i) = 0.0
-    qprime(i) = 0.0
-    DO k = 1, nl + 1
-      ft(i, k) = 0.0
-      fu(i, k) = 0.0
-      fv(i, k) = 0.0
-      fq(i, k) = 0.0
-      lvcp(i, k) = lv(i, k) / cpn(i, k)
-      qcondc(i, k) = 0.0 ! cld
-      qcond(i, k) = 0.0 ! cld
-      nqcond(i, k) = 0.0 ! cld
-    END DO
-  END DO
-
-
-  ! ***  Calculate surface precipitation in mm/day     ***
-
-  DO i = 1, ncum
-    IF (iflag(i)<=1) THEN
-      ! c            precip(i)=precip(i)+wt(i,1)*sigd*water(i,1)*3600.*24000.
-      ! c     &                /(rowl*g)
-      ! c            precip(i)=precip(i)*delt/86400.
-      precip(i) = wt(i, 1) * sigd * water(i, 1) * 86400 / g
-    END IF
-  END DO
-
-
-  ! ***  Calculate downdraft velocity scale and surface temperature and  ***
-  ! ***                    water vapor fluctuations                      ***
-
-  DO i = 1, ncum
-    wd(i) = betad * abs(mp(i, icb(i))) * 0.01 * rrd * t(i, icb(i)) / (sigd * p(i, icb(i)))
-    qprime(i) = 0.5 * (qp(i, 1) - q(i, 1))
-    tprime(i) = lv0 * qprime(i) / cpd
-  END DO
-
-  ! ***  Calculate tendencies of lowest level potential temperature  ***
-  ! ***                      and mixing ratio                        ***
-
-  DO i = 1, ncum
-    work(i) = 0.01 / (ph(i, 1) - ph(i, 2))
-    am(i) = 0.0
-  END DO
-  DO k = 2, nl
-    DO i = 1, ncum
-      IF ((nk(i)==1) .AND. (k<=inb(i)) .AND. (nk(i)==1)) THEN
-        am(i) = am(i) + m(i, k)
-      END IF
-    END DO
-  END DO
-  DO i = 1, ncum
-    IF ((g * work(i) * am(i))>=delti) iflag(i) = 1
-    ft(i, 1) = ft(i, 1) + g * work(i) * am(i) * (t(i, 2) - t(i, 1) + (gz(i, 2) - gz(i, &
-            1)) / cpn(i, 1))
-    ft(i, 1) = ft(i, 1) - lvcp(i, 1) * sigd * evap(i, 1)
-    ft(i, 1) = ft(i, 1) + sigd * wt(i, 2) * (cl - cpd) * water(i, 2) * (t(i, 2) - t(i, 1)) * &
-            work(i) / cpn(i, 1)
-    fq(i, 1) = fq(i, 1) + g * mp(i, 2) * (qp(i, 2) - q(i, 1)) * work(i) + &
-            sigd * evap(i, 1)
-    fq(i, 1) = fq(i, 1) + g * am(i) * (q(i, 2) - q(i, 1)) * work(i)
-    fu(i, 1) = fu(i, 1) + g * work(i) * (mp(i, 2) * (up(i, 2) - u(i, 1)) + am(i) * (u(i, &
-            2) - u(i, 1)))
-    fv(i, 1) = fv(i, 1) + g * work(i) * (mp(i, 2) * (vp(i, 2) - v(i, 1)) + am(i) * (v(i, &
-            2) - v(i, 1)))
-  END DO
-  DO j = 2, nl
-    DO i = 1, ncum
-      IF (j<=inb(i)) THEN
-        fq(i, 1) = fq(i, 1) + g * work(i) * ment(i, j, 1) * (qent(i, j, 1) - q(i, 1))
-        fu(i, 1) = fu(i, 1) + g * work(i) * ment(i, j, 1) * (uent(i, j, 1) - u(i, 1))
-        fv(i, 1) = fv(i, 1) + g * work(i) * ment(i, j, 1) * (vent(i, j, 1) - v(i, 1))
-      END IF
-    END DO
-  END DO
-
-  ! ***  Calculate tendencies of potential temperature and mixing ratio  ***
-  ! ***               at levels above the lowest level                   ***
-
-  ! ***  First find the net saturated updraft and downdraft mass fluxes  ***
-  ! ***                      through each level                          ***
-
-  DO i = 2, nl + 1
-
-    num1 = 0
-    DO ij = 1, ncum
-      IF (i<=inb(ij)) num1 = num1 + 1
-    END DO
-    IF (num1<=0) GO TO 1500
-
-    CALL zilch(amp1, ncum)
-    CALL zilch(ad, ncum)
-
-    DO k = i + 1, nl + 1
-      DO ij = 1, ncum
-        IF ((i>=nk(ij)) .AND. (i<=inb(ij)) .AND. (k<=(inb(ij) + 1))) THEN
-          amp1(ij) = amp1(ij) + m(ij, k)
-        END IF
-      END DO
-    END DO
-
-    DO k = 1, i
-      DO j = i + 1, nl + 1
-        DO ij = 1, ncum
-          IF ((j<=(inb(ij) + 1)) .AND. (i<=inb(ij))) THEN
-            amp1(ij) = amp1(ij) + ment(ij, k, j)
-          END IF
-        END DO
-      END DO
-    END DO
-    DO k = 1, i - 1
-      DO j = i, nl + 1
-        DO ij = 1, ncum
-          IF ((i<=inb(ij)) .AND. (j<=inb(ij))) THEN
-            ad(ij) = ad(ij) + ment(ij, j, k)
-          END IF
-        END DO
-      END DO
-    END DO
-
-    DO ij = 1, ncum
-      IF (i<=inb(ij)) THEN
-        dpinv = 0.01 / (ph(ij, i) - ph(ij, i + 1))
-        cpinv = 1.0 / cpn(ij, i)
-
-        ft(ij, i) = ft(ij, i) + g * dpinv * (amp1(ij) * (t(ij, i + 1) - t(ij, &
-                i) + (gz(ij, i + 1) - gz(ij, i)) * cpinv) - ad(ij) * (t(ij, i) - t(ij, &
-                i - 1) + (gz(ij, i) - gz(ij, i - 1)) * cpinv)) - sigd * lvcp(ij, i) * evap(ij, i)
-        ft(ij, i) = ft(ij, i) + g * dpinv * ment(ij, i, i) * (hp(ij, i) - h(ij, i) + t(ij &
-                , i) * (cpv - cpd) * (q(ij, i) - qent(ij, i, i))) * cpinv
-        ft(ij, i) = ft(ij, i) + sigd * wt(ij, i + 1) * (cl - cpd) * water(ij, i + 1) * (t(&
-                ij, i + 1) - t(ij, i)) * dpinv * cpinv
-        fq(ij, i) = fq(ij, i) + g * dpinv * (amp1(ij) * (q(ij, i + 1) - q(ij, &
-                i)) - ad(ij) * (q(ij, i) - q(ij, i - 1)))
-        fu(ij, i) = fu(ij, i) + g * dpinv * (amp1(ij) * (u(ij, i + 1) - u(ij, &
-                i)) - ad(ij) * (u(ij, i) - u(ij, i - 1)))
-        fv(ij, i) = fv(ij, i) + g * dpinv * (amp1(ij) * (v(ij, i + 1) - v(ij, &
-                i)) - ad(ij) * (v(ij, i) - v(ij, i - 1)))
-      END IF
-    END DO
-    DO k = 1, i - 1
-      DO ij = 1, ncum
-        IF (i<=inb(ij)) THEN
-          awat = elij(ij, k, i) - (1. - ep(ij, i)) * clw(ij, i)
-          awat = max(awat, 0.0)
-          fq(ij, i) = fq(ij, i) + g * dpinv * ment(ij, k, i) * (qent(ij, k, i) - awat - q &
-                  (ij, i))
-          fu(ij, i) = fu(ij, i) + g * dpinv * ment(ij, k, i) * (uent(ij, k, i) - u(ij, i &
-                  ))
-          fv(ij, i) = fv(ij, i) + g * dpinv * ment(ij, k, i) * (vent(ij, k, i) - v(ij, i &
-                  ))
-          ! (saturated updrafts resulting from mixing)               ! cld
-          qcond(ij, i) = qcond(ij, i) + (elij(ij, k, i) - awat) ! cld
-          nqcond(ij, i) = nqcond(ij, i) + 1. ! cld
-        END IF
-      END DO
-    END DO
-    DO k = i, nl + 1
-      DO ij = 1, ncum
-        IF ((i<=inb(ij)) .AND. (k<=inb(ij))) THEN
-          fq(ij, i) = fq(ij, i) + g * dpinv * ment(ij, k, i) * (qent(ij, k, i) - q(ij, i &
-                  ))
-          fu(ij, i) = fu(ij, i) + g * dpinv * ment(ij, k, i) * (uent(ij, k, i) - u(ij, i &
-                  ))
-          fv(ij, i) = fv(ij, i) + g * dpinv * ment(ij, k, i) * (vent(ij, k, i) - v(ij, i &
-                  ))
-        END IF
-      END DO
-    END DO
-    DO ij = 1, ncum
-      IF (i<=inb(ij)) THEN
-        fq(ij, i) = fq(ij, i) + sigd * evap(ij, i) + g * (mp(ij, i + 1) * (qp(ij, &
-                i + 1) - q(ij, i)) - mp(ij, i) * (qp(ij, i) - q(ij, i - 1))) * dpinv
-        fu(ij, i) = fu(ij, i) + g * (mp(ij, i + 1) * (up(ij, i + 1) - u(ij, &
-                i)) - mp(ij, i) * (up(ij, i) - u(ij, i - 1))) * dpinv
-        fv(ij, i) = fv(ij, i) + g * (mp(ij, i + 1) * (vp(ij, i + 1) - v(ij, &
-                i)) - mp(ij, i) * (vp(ij, i) - v(ij, i - 1))) * dpinv
-        ! (saturated downdrafts resulting from mixing)               ! cld
-        DO k = i + 1, inb(ij) ! cld
-          qcond(ij, i) = qcond(ij, i) + elij(ij, k, i) ! cld
-          nqcond(ij, i) = nqcond(ij, i) + 1. ! cld
-        END DO ! cld
-        ! (particular case: no detraining level is found)            ! cld
-        IF (nent(ij, i)==0) THEN ! cld
-          qcond(ij, i) = qcond(ij, i) + (1. - ep(ij, i)) * clw(ij, i) ! cld
-          nqcond(ij, i) = nqcond(ij, i) + 1. ! cld
-        END IF ! cld
-        IF (nqcond(ij, i)/=0.) THEN ! cld
-          qcond(ij, i) = qcond(ij, i) / nqcond(ij, i) ! cld
-        END IF ! cld
-      END IF
-    END DO
-  1500 END DO
-
-  ! *** Adjust tendencies at top of convection layer to reflect  ***
-  ! ***       actual position of the level zero cape             ***
-
-  DO ij = 1, ncum
-    fqold = fq(ij, inb(ij))
-    fq(ij, inb(ij)) = fq(ij, inb(ij)) * (1. - frac(ij))
-    fq(ij, inb(ij) - 1) = fq(ij, inb(ij) - 1) + frac(ij) * fqold * ((ph(ij, &
-            inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, &
-            inb(ij)))) * lv(ij, inb(ij)) / lv(ij, inb(ij) - 1)
-    ftold = ft(ij, inb(ij))
-    ft(ij, inb(ij)) = ft(ij, inb(ij)) * (1. - frac(ij))
-    ft(ij, inb(ij) - 1) = ft(ij, inb(ij) - 1) + frac(ij) * ftold * ((ph(ij, &
-            inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, &
-            inb(ij)))) * cpn(ij, inb(ij)) / cpn(ij, inb(ij) - 1)
-    fuold = fu(ij, inb(ij))
-    fu(ij, inb(ij)) = fu(ij, inb(ij)) * (1. - frac(ij))
-    fu(ij, inb(ij) - 1) = fu(ij, inb(ij) - 1) + frac(ij) * fuold * ((ph(ij, &
-            inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, inb(ij))))
-    fvold = fv(ij, inb(ij))
-    fv(ij, inb(ij)) = fv(ij, inb(ij)) * (1. - frac(ij))
-    fv(ij, inb(ij) - 1) = fv(ij, inb(ij) - 1) + frac(ij) * fvold * ((ph(ij, &
-            inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, inb(ij))))
-  END DO
-
-  ! ***   Very slightly adjust tendencies to force exact   ***
-  ! ***     enthalpy, momentum and tracer conservation     ***
-
-  DO ij = 1, ncum
-    ents(ij) = 0.0
-    uav(ij) = 0.0
-    vav(ij) = 0.0
-    DO i = 1, inb(ij)
-      ents(ij) = ents(ij) + (cpn(ij, i) * ft(ij, i) + lv(ij, i) * fq(ij, i)) * (ph(ij, i) - &
-              ph(ij, i + 1))
-      uav(ij) = uav(ij) + fu(ij, i) * (ph(ij, i) - ph(ij, i + 1))
-      vav(ij) = vav(ij) + fv(ij, i) * (ph(ij, i) - ph(ij, i + 1))
-    END DO
-  END DO
-  DO ij = 1, ncum
-    ents(ij) = ents(ij) / (ph(ij, 1) - ph(ij, inb(ij) + 1))
-    uav(ij) = uav(ij) / (ph(ij, 1) - ph(ij, inb(ij) + 1))
-    vav(ij) = vav(ij) / (ph(ij, 1) - ph(ij, inb(ij) + 1))
-  END DO
-  DO ij = 1, ncum
-    DO i = 1, inb(ij)
-      ft(ij, i) = ft(ij, i) - ents(ij) / cpn(ij, i)
-      fu(ij, i) = (1. - cu) * (fu(ij, i) - uav(ij))
-      fv(ij, i) = (1. - cu) * (fv(ij, i) - vav(ij))
-    END DO
-  END DO
-
-  DO k = 1, nl + 1
-    DO i = 1, ncum
-      IF ((q(i, k) + delt * fq(i, k))<0.0) iflag(i) = 10
-    END DO
-  END DO
-
-  DO i = 1, ncum
-    IF (iflag(i)>2) THEN
-      precip(i) = 0.0
-      cbmf(i) = 0.0
-    END IF
-  END DO
-  DO k = 1, nl
-    DO i = 1, ncum
-      IF (iflag(i)>2) THEN
-        ft(i, k) = 0.0
-        fq(i, k) = 0.0
-        fu(i, k) = 0.0
-        fv(i, k) = 0.0
-        qcondc(i, k) = 0.0 ! cld
-      END IF
-    END DO
-  END DO
-
-  DO k = 1, nl + 1
-    DO i = 1, ncum
-      ma(i, k) = 0.
-    END DO
-  END DO
-  DO k = nl, 1, -1
-    DO i = 1, ncum
-      ma(i, k) = ma(i, k + 1) + m(i, k)
-    END DO
-  END DO
-
-
-  ! *** diagnose the in-cloud mixing ratio   ***            ! cld
-  ! ***           of condensed water         ***            ! cld
-  ! cld
-  DO ij = 1, ncum ! cld
-    DO i = 1, nd ! cld
-      mac(ij, i) = 0.0 ! cld
-      wa(ij, i) = 0.0 ! cld
-      siga(ij, i) = 0.0 ! cld
-    END DO ! cld
-    DO i = nk(ij), inb(ij) ! cld
-      DO k = i + 1, inb(ij) + 1 ! cld
-        mac(ij, i) = mac(ij, i) + m(ij, k) ! cld
-      END DO ! cld
-    END DO ! cld
-    DO i = icb(ij), inb(ij) - 1 ! cld
-      ax(ij, i) = 0. ! cld
-      DO j = icb(ij), i ! cld
-        ax(ij, i) = ax(ij, i) + rrd * (tvp(ij, j) - tv(ij, j)) & ! cld
-                * (ph(ij, j) - ph(ij, j + 1)) / p(ij, j) ! cld
-      END DO ! cld
-      IF (ax(ij, i)>0.0) THEN ! cld
-        wa(ij, i) = sqrt(2. * ax(ij, i)) ! cld
-      END IF ! cld
-    END DO ! cld
-    DO i = 1, nl ! cld
-      IF (wa(ij, i)>0.0) &          ! cld
-              siga(ij, i) = mac(ij, i) / wa(ij, i) & ! cld
-                      * rrd * tvp(ij, i) / p(ij, i) / 100. / delta ! cld
-      siga(ij, i) = min(siga(ij, i), 1.0) ! cld
-      qcondc(ij, i) = siga(ij, i) * clw(ij, i) * (1. - ep(ij, i)) & ! cld
-              + (1. - siga(ij, i)) * qcond(ij, i) ! cld
-    END DO ! cld
-  END DO ! cld
-
-END SUBROUTINE cv_yield
-
-SUBROUTINE cv_uncompress(nloc, len, ncum, nd, idcum, iflag, precip, cbmf, ft, &
-        fq, fu, fv, ma, qcondc, iflag1, precip1, cbmf1, ft1, fq1, fu1, fv1, ma1, &
-        qcondc1)
-  IMPLICIT NONE
-
-  include "cvparam.h"
-
-  ! inputs:
-  INTEGER len, ncum, nd, nloc
-  INTEGER idcum(nloc)
-  INTEGER iflag(nloc)
-  REAL precip(nloc), cbmf(nloc)
-  REAL ft(nloc, nd), fq(nloc, nd), fu(nloc, nd), fv(nloc, nd)
-  REAL ma(nloc, nd)
-  REAL qcondc(nloc, nd) !cld
-
-  ! outputs:
-  INTEGER iflag1(len)
-  REAL precip1(len), cbmf1(len)
-  REAL ft1(len, nd), fq1(len, nd), fu1(len, nd), fv1(len, nd)
-  REAL ma1(len, nd)
-  REAL qcondc1(len, nd) !cld
-
-  ! local variables:
-  INTEGER i, k
-
-  DO i = 1, ncum
-    precip1(idcum(i)) = precip(i)
-    cbmf1(idcum(i)) = cbmf(i)
-    iflag1(idcum(i)) = iflag(i)
-  END DO
-
-  DO k = 1, nl
-    DO i = 1, ncum
-      ft1(idcum(i), k) = ft(i, k)
-      fq1(idcum(i), k) = fq(i, k)
-      fu1(idcum(i), k) = fu(i, k)
-      fv1(idcum(i), k) = fv(i, k)
-      ma1(idcum(i), k) = ma(i, k)
-      qcondc1(idcum(i), k) = qcondc(i, k)
-    END DO
-  END DO
-
-END SUBROUTINE cv_uncompress
-
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/cva_driver.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/cva_driver.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmdiso/cva_driver.F90	(revision 5142)
@@ -73,4 +73,7 @@
 #endif
 #endif
+USE lmdz_cv, ONLY: cv_param, cv_prelim, cv_feed, cv_undilute1, cv_trigger, cv_compress, &
+          cv_undilute2, cv_closure, cv_mixing, cv_unsat, cv_yield, cv_uncompress
+
   IMPLICIT NONE
 
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/cvparam.h
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/cvparam.h	(revision 5141)
+++ 	(revision )
@@ -1,1 +1,0 @@
-link ../phylmd/cvparam.h
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/dimpft.h
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/dimpft.h	(revision 5141)
+++ 	(revision )
@@ -1,1 +1,0 @@
-link ../phylmd/dimpft.h
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/fcg_gcssold.h
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/fcg_gcssold.h	(revision 5141)
+++ 	(revision )
@@ -1,1 +1,0 @@
-link ../phylmd/fcg_gcssold.h
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_YOECUMF.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_YOECUMF.f90	(revision 5142)
+++ LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_YOECUMF.f90	(revision 5142)
@@ -0,0 +1,1 @@
+link ../phylmd/lmdz_YOECUMF.f90
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_cv.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_cv.F90	(revision 5142)
+++ LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_cv.F90	(revision 5142)
@@ -0,0 +1,1719 @@
+! $Id$
+
+MODULE lmdz_cv
+  !------------------------------------------------------------
+  ! Parameters for convectL:
+  ! (includes - microphysical parameters,
+  !			- parameters that control the rate of approach
+  !               to quasi-equilibrium)
+  !			- noff & minorig (previously in input of convect1)
+  !------------------------------------------------------------
+
+  IMPLICIT NONE; PRIVATE
+  PUBLIC elcrit, tlcrit, entp, sigs, sigd, omtrain, omtsnow, coeffr, coeffs &
+          , dtmax, cu, betad, alpha, damp, delta, noff, minorig, nl, nlp, nlm, &
+          cv_param, cv_prelim, cv_feed, cv_undilute1, cv_trigger, cv_compress, &
+          cv_undilute2, cv_closure, cv_mixing, cv_unsat, cv_yield, cv_uncompress
+
+  INTEGER noff, minorig, nl, nlp, nlm
+  REAL elcrit, tlcrit
+  REAL entp
+  REAL sigs, sigd
+  REAL omtrain, omtsnow, coeffr, coeffs
+  REAL dtmax
+  REAL cu
+  REAL betad
+  REAL alpha, damp
+  REAL delta
+
+  !$OMP THREADPRIVATE(elcrit, tlcrit, entp, sigs, sigd, omtrain, omtsnow, coeffr, coeffs &
+  !$OMP      , dtmax, cu, betad, alpha, damp, delta, noff, minorig, nl, nlp, nlm)
+
+CONTAINS
+
+SUBROUTINE cv_param(nd)
+  IMPLICIT NONE
+
+  ! ------------------------------------------------------------
+  ! Set parameters for convectL
+  ! (includes microphysical parameters and parameters that
+  ! control the rate of approach to quasi-equilibrium)
+  ! ------------------------------------------------------------
+
+  ! *** ELCRIT IS THE AUTOCONVERSION THERSHOLD WATER CONTENT (gm/gm) ***
+  ! ***  TLCRIT IS CRITICAL TEMPERATURE BELOW WHICH THE AUTO-        ***
+  ! ***       CONVERSION THRESHOLD IS ASSUMED TO BE ZERO             ***
+  ! ***     (THE AUTOCONVERSION THRESHOLD VARIES LINEARLY            ***
+  ! ***               BETWEEN 0 C AND TLCRIT)                        ***
+  ! ***   ENTP IS THE COEFFICIENT OF MIXING IN THE ENTRAINMENT       ***
+  ! ***                       FORMULATION                            ***
+  ! ***  SIGD IS THE FRACTIONAL AREA COVERED BY UNSATURATED DNDRAFT  ***
+  ! ***  SIGS IS THE FRACTION OF PRECIPITATION FALLING OUTSIDE       ***
+  ! ***                        OF CLOUD                              ***
+  ! ***        OMTRAIN IS THE ASSUMED FALL SPEED (P/s) OF RAIN       ***
+  ! ***     OMTSNOW IS THE ASSUMED FALL SPEED (P/s) OF SNOW          ***
+  ! ***  COEFFR IS A COEFFICIENT GOVERNING THE RATE OF EVAPORATION   ***
+  ! ***                          OF RAIN                             ***
+  ! ***  COEFFS IS A COEFFICIENT GOVERNING THE RATE OF EVAPORATION   ***
+  ! ***                          OF SNOW                             ***
+  ! ***     CU IS THE COEFFICIENT GOVERNING CONVECTIVE MOMENTUM      ***
+  ! ***                         TRANSPORT                            ***
+  ! ***    DTMAX IS THE MAXIMUM NEGATIVE TEMPERATURE PERTURBATION    ***
+  ! ***        A LIFTED PARCEL IS ALLOWED TO HAVE BELOW ITS LFC      ***
+  ! ***    ALPHA AND DAMP ARE PARAMETERS THAT CONTROL THE RATE OF    ***
+  ! ***                 APPROACH TO QUASI-EQUILIBRIUM                ***
+  ! ***   (THEIR STANDARD VALUES ARE  0.20 AND 0.1, RESPECTIVELY)    ***
+  ! ***                   (DAMP MUST BE LESS THAN 1)                 ***
+
+    INTEGER nd
+  CHARACTER (LEN = 20) :: modname = 'cv_routines'
+  CHARACTER (LEN = 80) :: abort_message
+
+  ! noff: integer limit for convection (nd-noff)
+  ! minorig: First level of convection
+
+  noff = 2
+  minorig = 2
+
+  nl = nd - noff
+  nlp = nl + 1
+  nlm = nl - 1
+
+  elcrit = 0.0011
+  tlcrit = -55.0
+  entp = 1.5
+  sigs = 0.12
+  sigd = 0.05
+  omtrain = 50.0
+  omtsnow = 5.5
+  coeffr = 1.0
+  coeffs = 0.8
+  dtmax = 0.9
+
+  cu = 0.70
+
+  betad = 10.0
+
+  damp = 0.1
+  alpha = 0.2
+
+  delta = 0.01 ! cld
+
+END SUBROUTINE cv_param
+
+SUBROUTINE cv_prelim(len, nd, ndp1, t, q, p, ph, lv, cpn, tv, gz, h, hm)
+  USE lmdz_cvthermo
+
+  IMPLICIT NONE
+
+  ! =====================================================================
+  ! --- CALCULATE ARRAYS OF GEOPOTENTIAL, HEAT CAPACITY & STATIC ENERGY
+  ! =====================================================================
+
+  ! inputs:
+  INTEGER len, nd, ndp1
+  REAL t(len, nd), q(len, nd), p(len, nd), ph(len, ndp1)
+
+  ! outputs:
+  REAL lv(len, nd), cpn(len, nd), tv(len, nd)
+  REAL gz(len, nd), h(len, nd), hm(len, nd)
+
+  ! local variables:
+  INTEGER k, i
+  REAL cpx(len, nd)
+
+  
+  DO k = 1, nlp
+    DO i = 1, len
+      lv(i, k) = lv0 - clmcpv * (t(i, k) - t0)
+      cpn(i, k) = cpd * (1.0 - q(i, k)) + cpv * q(i, k)
+      cpx(i, k) = cpd * (1.0 - q(i, k)) + cl * q(i, k)
+      tv(i, k) = t(i, k) * (1.0 + q(i, k) * epsim1)
+    END DO
+  END DO
+
+  ! gz = phi at the full levels (same as p).
+
+  DO i = 1, len
+    gz(i, 1) = 0.0
+  END DO
+  DO k = 2, nlp
+    DO i = 1, len
+      gz(i, k) = gz(i, k - 1) + hrd * (tv(i, k - 1) + tv(i, k)) * (p(i, k - 1) - p(i, k)) / ph(i, &
+              k)
+    END DO
+  END DO
+
+  ! h  = phi + cpT (dry static energy).
+  ! hm = phi + cp(T-Tbase)+Lq
+
+  DO k = 1, nlp
+    DO i = 1, len
+      h(i, k) = gz(i, k) + cpn(i, k) * t(i, k)
+      hm(i, k) = gz(i, k) + cpx(i, k) * (t(i, k) - t(i, 1)) + lv(i, k) * q(i, k)
+    END DO
+  END DO
+
+END SUBROUTINE cv_prelim
+
+SUBROUTINE cv_feed(len, nd, t, q, qs, p, hm, gz, nk, icb, icbmax, iflag, tnk, &
+        qnk, gznk, plcl)
+  IMPLICIT NONE
+
+  ! ================================================================
+  ! Purpose: CONVECTIVE FEED
+  ! ================================================================
+
+  
+  ! inputs:
+  INTEGER len, nd
+  REAL t(len, nd), q(len, nd), qs(len, nd), p(len, nd)
+  REAL hm(len, nd), gz(len, nd)
+
+  ! outputs:
+  INTEGER iflag(len), nk(len), icb(len), icbmax
+  REAL tnk(len), qnk(len), gznk(len), plcl(len)
+
+  ! local variables:
+  INTEGER i, k
+  INTEGER ihmin(len)
+  REAL work(len)
+  REAL pnk(len), qsnk(len), rh(len), chi(len)
+
+  ! -------------------------------------------------------------------
+  ! --- Find level of minimum moist static energy
+  ! --- If level of minimum moist static energy coincides with
+  ! --- or is lower than minimum allowable parcel origin level,
+  ! --- set iflag to 6.
+  ! -------------------------------------------------------------------
+
+  DO i = 1, len
+    work(i) = 1.0E12
+    ihmin(i) = nl
+  END DO
+  DO k = 2, nlp
+    DO i = 1, len
+      IF ((hm(i, k)<work(i)) .AND. (hm(i, k)<hm(i, k - 1))) THEN
+        work(i) = hm(i, k)
+        ihmin(i) = k
+      END IF
+    END DO
+  END DO
+  DO i = 1, len
+    ihmin(i) = min(ihmin(i), nlm)
+    IF (ihmin(i)<=minorig) THEN
+      iflag(i) = 6
+    END IF
+  END DO
+
+  ! -------------------------------------------------------------------
+  ! --- Find that model level below the level of minimum moist static
+  ! --- energy that has the maximum value of moist static energy
+  ! -------------------------------------------------------------------
+
+  DO i = 1, len
+    work(i) = hm(i, minorig)
+    nk(i) = minorig
+  END DO
+  DO k = minorig + 1, nl
+    DO i = 1, len
+      IF ((hm(i, k)>work(i)) .AND. (k<=ihmin(i))) THEN
+        work(i) = hm(i, k)
+        nk(i) = k
+      END IF
+    END DO
+  END DO
+  ! -------------------------------------------------------------------
+  ! --- Check whether parcel level temperature and specific humidity
+  ! --- are reasonable
+  ! -------------------------------------------------------------------
+  DO i = 1, len
+    IF (((t(i, nk(i))<250.0) .OR. (q(i, nk(i))<=0.0) .OR. (p(i, ihmin(i))< &
+            400.0)) .AND. (iflag(i)==0)) iflag(i) = 7
+  END DO
+  ! -------------------------------------------------------------------
+  ! --- Calculate lifted condensation level of air at parcel origin level
+  ! --- (Within 0.2% of formula of Bolton, MON. WEA. REV.,1980)
+  ! -------------------------------------------------------------------
+  DO i = 1, len
+    tnk(i) = t(i, nk(i))
+    qnk(i) = q(i, nk(i))
+    gznk(i) = gz(i, nk(i))
+    pnk(i) = p(i, nk(i))
+    qsnk(i) = qs(i, nk(i))
+
+    rh(i) = qnk(i) / qsnk(i)
+    rh(i) = min(1.0, rh(i))
+    chi(i) = tnk(i) / (1669.0 - 122.0 * rh(i) - tnk(i))
+    plcl(i) = pnk(i) * (rh(i)**chi(i))
+    IF (((plcl(i)<200.0) .OR. (plcl(i)>=2000.0)) .AND. (iflag(i)==0)) iflag(i &
+            ) = 8
+  END DO
+  ! -------------------------------------------------------------------
+  ! --- Calculate first level above lcl (=icb)
+  ! -------------------------------------------------------------------
+  DO i = 1, len
+    icb(i) = nlm
+  END DO
+
+  DO k = minorig, nl
+    DO i = 1, len
+      IF ((k>=(nk(i) + 1)) .AND. (p(i, k)<plcl(i))) icb(i) = min(icb(i), k)
+    END DO
+  END DO
+
+  DO i = 1, len
+    IF ((icb(i)>=nlm) .AND. (iflag(i)==0)) iflag(i) = 9
+  END DO
+
+  ! Compute icbmax.
+
+  icbmax = 2
+  DO i = 1, len
+    icbmax = max(icbmax, icb(i))
+  END DO
+
+END SUBROUTINE cv_feed
+
+SUBROUTINE cv_undilute1(len, nd, t, q, qs, gz, p, nk, icb, icbmax, tp, tvp, &
+        clw)
+  USE lmdz_cvthermo
+
+  IMPLICIT NONE
+
+  
+  ! inputs:
+  INTEGER len, nd
+  INTEGER nk(len), icb(len), icbmax
+  REAL t(len, nd), q(len, nd), qs(len, nd), gz(len, nd)
+  REAL p(len, nd)
+
+  ! outputs:
+  REAL tp(len, nd), tvp(len, nd), clw(len, nd)
+
+  ! local variables:
+  INTEGER i, k
+  REAL tg, qg, alv, s, ahg, tc, denom, es, rg
+  REAL ah0(len), cpp(len)
+  REAL tnk(len), qnk(len), gznk(len), ticb(len), gzicb(len)
+
+  ! -------------------------------------------------------------------
+  ! --- Calculates the lifted parcel virtual temperature at nk,
+  ! --- the actual temperature, and the adiabatic
+  ! --- liquid water content. The procedure is to solve the equation.
+  ! cp*tp+L*qp+phi=cp*tnk+L*qnk+gznk.
+  ! -------------------------------------------------------------------
+
+  DO i = 1, len
+    tnk(i) = t(i, nk(i))
+    qnk(i) = q(i, nk(i))
+    gznk(i) = gz(i, nk(i))
+    ticb(i) = t(i, icb(i))
+    gzicb(i) = gz(i, icb(i))
+  END DO
+
+  ! ***  Calculate certain parcel quantities, including static energy   ***
+
+  DO i = 1, len
+    ah0(i) = (cpd * (1. - qnk(i)) + cl * qnk(i)) * tnk(i) + qnk(i) * (lv0 - clmcpv * (tnk(i) - &
+            273.15)) + gznk(i)
+    cpp(i) = cpd * (1. - qnk(i)) + qnk(i) * cpv
+  END DO
+
+  ! ***   Calculate lifted parcel quantities below cloud base   ***
+
+  DO k = minorig, icbmax - 1
+    DO i = 1, len
+      tp(i, k) = tnk(i) - (gz(i, k) - gznk(i)) / cpp(i)
+      tvp(i, k) = tp(i, k) * (1. + qnk(i) * epsi)
+    END DO
+  END DO
+
+  ! ***  Find lifted parcel quantities above cloud base    ***
+
+  DO i = 1, len
+    tg = ticb(i)
+    qg = qs(i, icb(i))
+    alv = lv0 - clmcpv * (ticb(i) - t0)
+
+    ! First iteration.
+
+    s = cpd + alv * alv * qg / (rrv * ticb(i) * ticb(i))
+    s = 1. / s
+    ahg = cpd * tg + (cl - cpd) * qnk(i) * ticb(i) + alv * qg + gzicb(i)
+    tg = tg + s * (ah0(i) - ahg)
+    tg = max(tg, 35.0)
+    tc = tg - t0
+    denom = 243.5 + tc
+    IF (tc>=0.0) THEN
+      es = 6.112 * exp(17.67 * tc / denom)
+    ELSE
+      es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
+    END IF
+    qg = eps * es / (p(i, icb(i)) - es * (1. - eps))
+
+    ! Second iteration.
+
+    s = cpd + alv * alv * qg / (rrv * ticb(i) * ticb(i))
+    s = 1. / s
+    ahg = cpd * tg + (cl - cpd) * qnk(i) * ticb(i) + alv * qg + gzicb(i)
+    tg = tg + s * (ah0(i) - ahg)
+    tg = max(tg, 35.0)
+    tc = tg - t0
+    denom = 243.5 + tc
+    IF (tc>=0.0) THEN
+      es = 6.112 * exp(17.67 * tc / denom)
+    ELSE
+      es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
+    END IF
+    qg = eps * es / (p(i, icb(i)) - es * (1. - eps))
+
+    alv = lv0 - clmcpv * (ticb(i) - 273.15)
+    tp(i, icb(i)) = (ah0(i) - (cl - cpd) * qnk(i) * ticb(i) - gz(i, icb(i)) - alv * qg) / cpd
+    clw(i, icb(i)) = qnk(i) - qg
+    clw(i, icb(i)) = max(0.0, clw(i, icb(i)))
+    rg = qg / (1. - qnk(i))
+    tvp(i, icb(i)) = tp(i, icb(i)) * (1. + rg * epsi)
+  END DO
+
+  DO k = minorig, icbmax
+    DO i = 1, len
+      tvp(i, k) = tvp(i, k) - tp(i, k) * qnk(i)
+    END DO
+  END DO
+
+END SUBROUTINE cv_undilute1
+
+SUBROUTINE cv_trigger(len, nd, icb, cbmf, tv, tvp, iflag)
+  IMPLICIT NONE
+
+  ! -------------------------------------------------------------------
+  ! --- Test for instability.
+  ! --- If there was no convection at last time step and parcel
+  ! --- is stable at icb, then set iflag to 4.
+  ! -------------------------------------------------------------------
+
+  
+  ! inputs:
+  INTEGER len, nd, icb(len)
+  REAL cbmf(len), tv(len, nd), tvp(len, nd)
+
+  ! outputs:
+  INTEGER iflag(len) ! also an input
+
+  ! local variables:
+  INTEGER i
+
+  DO i = 1, len
+    IF ((cbmf(i)==0.0) .AND. (iflag(i)==0) .AND. (tvp(i, &
+            icb(i))<=(tv(i, icb(i)) - dtmax))) iflag(i) = 4
+  END DO
+
+END SUBROUTINE cv_trigger
+
+SUBROUTINE cv_compress(len, nloc, ncum, nd, iflag1, nk1, icb1, cbmf1, plcl1, &
+        tnk1, qnk1, gznk1, t1, q1, qs1, u1, v1, gz1, h1, lv1, cpn1, p1, ph1, tv1, &
+        tp1, tvp1, clw1, iflag, nk, icb, cbmf, plcl, tnk, qnk, gznk, t, q, qs, u, &
+        v, gz, h, lv, cpn, p, ph, tv, tp, tvp, clw, dph)
+  USE lmdz_print_control, ONLY: lunout
+  USE lmdz_abort_physic, ONLY: abort_physic
+  IMPLICIT NONE
+
+  
+  ! inputs:
+  INTEGER len, ncum, nd, nloc
+  INTEGER iflag1(len), nk1(len), icb1(len)
+  REAL cbmf1(len), plcl1(len), tnk1(len), qnk1(len), gznk1(len)
+  REAL t1(len, nd), q1(len, nd), qs1(len, nd), u1(len, nd), v1(len, nd)
+  REAL gz1(len, nd), h1(len, nd), lv1(len, nd), cpn1(len, nd)
+  REAL p1(len, nd), ph1(len, nd + 1), tv1(len, nd), tp1(len, nd)
+  REAL tvp1(len, nd), clw1(len, nd)
+
+  ! outputs:
+  INTEGER iflag(nloc), nk(nloc), icb(nloc)
+  REAL cbmf(nloc), plcl(nloc), tnk(nloc), qnk(nloc), gznk(nloc)
+  REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd), u(nloc, nd), v(nloc, nd)
+  REAL gz(nloc, nd), h(nloc, nd), lv(nloc, nd), cpn(nloc, nd)
+  REAL p(nloc, nd), ph(nloc, nd + 1), tv(nloc, nd), tp(nloc, nd)
+  REAL tvp(nloc, nd), clw(nloc, nd)
+  REAL dph(nloc, nd)
+
+  ! local variables:
+  INTEGER i, k, nn
+  CHARACTER (LEN = 20) :: modname = 'cv_compress'
+  CHARACTER (LEN = 80) :: abort_message
+
+  DO k = 1, nl + 1
+    nn = 0
+    DO i = 1, len
+      IF (iflag1(i)==0) THEN
+        nn = nn + 1
+        t(nn, k) = t1(i, k)
+        q(nn, k) = q1(i, k)
+        qs(nn, k) = qs1(i, k)
+        u(nn, k) = u1(i, k)
+        v(nn, k) = v1(i, k)
+        gz(nn, k) = gz1(i, k)
+        h(nn, k) = h1(i, k)
+        lv(nn, k) = lv1(i, k)
+        cpn(nn, k) = cpn1(i, k)
+        p(nn, k) = p1(i, k)
+        ph(nn, k) = ph1(i, k)
+        tv(nn, k) = tv1(i, k)
+        tp(nn, k) = tp1(i, k)
+        tvp(nn, k) = tvp1(i, k)
+        clw(nn, k) = clw1(i, k)
+      END IF
+    END DO
+  END DO
+
+  IF (nn/=ncum) THEN
+    WRITE (lunout, *) 'strange! nn not equal to ncum: ', nn, ncum
+    abort_message = ''
+    CALL abort_physic(modname, abort_message, 1)
+  END IF
+
+  nn = 0
+  DO i = 1, len
+    IF (iflag1(i)==0) THEN
+      nn = nn + 1
+      cbmf(nn) = cbmf1(i)
+      plcl(nn) = plcl1(i)
+      tnk(nn) = tnk1(i)
+      qnk(nn) = qnk1(i)
+      gznk(nn) = gznk1(i)
+      nk(nn) = nk1(i)
+      icb(nn) = icb1(i)
+      iflag(nn) = iflag1(i)
+    END IF
+  END DO
+
+  DO k = 1, nl
+    DO i = 1, ncum
+      dph(i, k) = ph(i, k) - ph(i, k + 1)
+    END DO
+  END DO
+
+END SUBROUTINE cv_compress
+
+SUBROUTINE cv_undilute2(nloc, ncum, nd, icb, nk, tnk, qnk, gznk, t, q, qs, &
+        gz, p, dph, h, tv, lv, inb, inb1, tp, tvp, clw, hp, ep, sigp, frac)
+  USE lmdz_cvthermo
+
+  IMPLICIT NONE
+
+  ! ---------------------------------------------------------------------
+  ! Purpose:
+  ! FIND THE REST OF THE LIFTED PARCEL TEMPERATURES
+  ! &
+  ! COMPUTE THE PRECIPITATION EFFICIENCIES AND THE
+  ! FRACTION OF PRECIPITATION FALLING OUTSIDE OF CLOUD
+  ! &
+  ! FIND THE LEVEL OF NEUTRAL BUOYANCY
+  ! ---------------------------------------------------------------------
+
+  
+  ! inputs:
+  INTEGER ncum, nd, nloc
+  INTEGER icb(nloc), nk(nloc)
+  REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd), gz(nloc, nd)
+  REAL p(nloc, nd), dph(nloc, nd)
+  REAL tnk(nloc), qnk(nloc), gznk(nloc)
+  REAL lv(nloc, nd), tv(nloc, nd), h(nloc, nd)
+
+  ! outputs:
+  INTEGER inb(nloc), inb1(nloc)
+  REAL tp(nloc, nd), tvp(nloc, nd), clw(nloc, nd)
+  REAL ep(nloc, nd), sigp(nloc, nd), hp(nloc, nd)
+  REAL frac(nloc)
+
+  ! local variables:
+  INTEGER i, k
+  REAL tg, qg, ahg, alv, s, tc, es, denom, rg, tca, elacrit
+  REAL by, defrac
+  REAL ah0(nloc), cape(nloc), capem(nloc), byp(nloc)
+  LOGICAL lcape(nloc)
+
+  ! =====================================================================
+  ! --- SOME INITIALIZATIONS
+  ! =====================================================================
+
+  DO k = 1, nl
+    DO i = 1, ncum
+      ep(i, k) = 0.0
+      sigp(i, k) = sigs
+    END DO
+  END DO
+
+  ! =====================================================================
+  ! --- FIND THE REST OF THE LIFTED PARCEL TEMPERATURES
+  ! =====================================================================
+
+  ! ---       The procedure is to solve the equation.
+  ! cp*tp+L*qp+phi=cp*tnk+L*qnk+gznk.
+
+  ! ***  Calculate certain parcel quantities, including static energy   ***
+
+  DO i = 1, ncum
+    ah0(i) = (cpd * (1. - qnk(i)) + cl * qnk(i)) * tnk(i) + qnk(i) * (lv0 - clmcpv * (tnk(i) - &
+            t0)) + gznk(i)
+  END DO
+
+
+  ! ***  Find lifted parcel quantities above cloud base    ***
+
+  DO k = minorig + 1, nl
+    DO i = 1, ncum
+      IF (k>=(icb(i) + 1)) THEN
+        tg = t(i, k)
+        qg = qs(i, k)
+        alv = lv0 - clmcpv * (t(i, k) - t0)
+
+        ! First iteration.
+
+        s = cpd + alv * alv * qg / (rrv * t(i, k) * t(i, k))
+        s = 1. / s
+        ahg = cpd * tg + (cl - cpd) * qnk(i) * t(i, k) + alv * qg + gz(i, k)
+        tg = tg + s * (ah0(i) - ahg)
+        tg = max(tg, 35.0)
+        tc = tg - t0
+        denom = 243.5 + tc
+        IF (tc>=0.0) THEN
+          es = 6.112 * exp(17.67 * tc / denom)
+        ELSE
+          es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
+        END IF
+        qg = eps * es / (p(i, k) - es * (1. - eps))
+
+        ! Second iteration.
+
+        s = cpd + alv * alv * qg / (rrv * t(i, k) * t(i, k))
+        s = 1. / s
+        ahg = cpd * tg + (cl - cpd) * qnk(i) * t(i, k) + alv * qg + gz(i, k)
+        tg = tg + s * (ah0(i) - ahg)
+        tg = max(tg, 35.0)
+        tc = tg - t0
+        denom = 243.5 + tc
+        IF (tc>=0.0) THEN
+          es = 6.112 * exp(17.67 * tc / denom)
+        ELSE
+          es = exp(23.33086 - 6111.72784 / tg + 0.15215 * log(tg))
+        END IF
+        qg = eps * es / (p(i, k) - es * (1. - eps))
+
+        alv = lv0 - clmcpv * (t(i, k) - t0)
+        ! PRINT*,'cpd dans convect2 ',cpd
+        ! PRINT*,'tp(i,k),ah0(i),cl,cpd,qnk(i),t(i,k),gz(i,k),alv,qg,cpd'
+        ! PRINT*,tp(i,k),ah0(i),cl,cpd,qnk(i),t(i,k),gz(i,k),alv,qg,cpd
+        tp(i, k) = (ah0(i) - (cl - cpd) * qnk(i) * t(i, k) - gz(i, k) - alv * qg) / cpd
+        ! if (.NOT.cpd.gt.1000.) THEN
+        ! PRINT*,'CPD=',cpd
+        ! stop
+        ! END IF
+        clw(i, k) = qnk(i) - qg
+        clw(i, k) = max(0.0, clw(i, k))
+        rg = qg / (1. - qnk(i))
+        tvp(i, k) = tp(i, k) * (1. + rg * epsi)
+      END IF
+    END DO
+  END DO
+
+  ! =====================================================================
+  ! --- SET THE PRECIPITATION EFFICIENCIES AND THE FRACTION OF
+  ! --- PRECIPITATION FALLING OUTSIDE OF CLOUD
+  ! --- THESE MAY BE FUNCTIONS OF TP(I), P(I) AND CLW(I)
+  ! =====================================================================
+
+  DO k = minorig + 1, nl
+    DO i = 1, ncum
+      IF (k>=(nk(i) + 1)) THEN
+        tca = tp(i, k) - t0
+        IF (tca>=0.0) THEN
+          elacrit = elcrit
+        ELSE
+          elacrit = elcrit * (1.0 - tca / tlcrit)
+        END IF
+        elacrit = max(elacrit, 0.0)
+        ep(i, k) = 1.0 - elacrit / max(clw(i, k), 1.0E-8)
+        ep(i, k) = max(ep(i, k), 0.0)
+        ep(i, k) = min(ep(i, k), 1.0)
+        sigp(i, k) = sigs
+      END IF
+    END DO
+  END DO
+
+  ! =====================================================================
+  ! --- CALCULATE VIRTUAL TEMPERATURE AND LIFTED PARCEL
+  ! --- VIRTUAL TEMPERATURE
+  ! =====================================================================
+
+  DO k = minorig + 1, nl
+    DO i = 1, ncum
+      IF (k>=(icb(i) + 1)) THEN
+        tvp(i, k) = tvp(i, k) * (1.0 - qnk(i) + ep(i, k) * clw(i, k))
+        ! PRINT*,'i,k,tvp(i,k),qnk(i),ep(i,k),clw(i,k)'
+        ! PRINT*, i,k,tvp(i,k),qnk(i),ep(i,k),clw(i,k)
+      END IF
+    END DO
+  END DO
+  DO i = 1, ncum
+    tvp(i, nlp) = tvp(i, nl) - (gz(i, nlp) - gz(i, nl)) / cpd
+  END DO
+
+  ! =====================================================================
+  ! --- FIND THE FIRST MODEL LEVEL (INB1) ABOVE THE PARCEL'S
+  ! --- HIGHEST LEVEL OF NEUTRAL BUOYANCY
+  ! --- AND THE HIGHEST LEVEL OF POSITIVE CAPE (INB)
+  ! =====================================================================
+
+  DO i = 1, ncum
+    cape(i) = 0.0
+    capem(i) = 0.0
+    inb(i) = icb(i) + 1
+    inb1(i) = inb(i)
+  END DO
+
+  ! Originial Code
+
+  ! do 530 k=minorig+1,nl-1
+  ! do 520 i=1,ncum
+  ! IF(k.ge.(icb(i)+1))THEN
+  ! by=(tvp(i,k)-tv(i,k))*dph(i,k)/p(i,k)
+  ! byp=(tvp(i,k+1)-tv(i,k+1))*dph(i,k+1)/p(i,k+1)
+  ! cape(i)=cape(i)+by
+  ! IF(by.ge.0.0)inb1(i)=k+1
+  ! IF(cape(i).gt.0.0)THEN
+  ! inb(i)=k+1
+  ! capem(i)=cape(i)
+  ! END IF
+  ! END IF
+  ! 520    continue
+  ! 530  continue
+  ! do 540 i=1,ncum
+  ! byp=(tvp(i,nl)-tv(i,nl))*dph(i,nl)/p(i,nl)
+  ! cape(i)=capem(i)+byp
+  ! defrac=capem(i)-cape(i)
+  ! defrac=max(defrac,0.001)
+  ! frac(i)=-cape(i)/defrac
+  ! frac(i)=min(frac(i),1.0)
+  ! frac(i)=max(frac(i),0.0)
+  ! 540   continue
+
+  ! K Emanuel fix
+
+  ! CALL zilch(byp,ncum)
+  ! do 530 k=minorig+1,nl-1
+  ! do 520 i=1,ncum
+  ! IF(k.ge.(icb(i)+1))THEN
+  ! by=(tvp(i,k)-tv(i,k))*dph(i,k)/p(i,k)
+  ! cape(i)=cape(i)+by
+  ! IF(by.ge.0.0)inb1(i)=k+1
+  ! IF(cape(i).gt.0.0)THEN
+  ! inb(i)=k+1
+  ! capem(i)=cape(i)
+  ! byp(i)=(tvp(i,k+1)-tv(i,k+1))*dph(i,k+1)/p(i,k+1)
+  ! END IF
+  ! END IF
+  ! 520    continue
+  ! 530  continue
+  ! do 540 i=1,ncum
+  ! inb(i)=max(inb(i),inb1(i))
+  ! cape(i)=capem(i)+byp(i)
+  ! defrac=capem(i)-cape(i)
+  ! defrac=max(defrac,0.001)
+  ! frac(i)=-cape(i)/defrac
+  ! frac(i)=min(frac(i),1.0)
+  ! frac(i)=max(frac(i),0.0)
+  ! 540   continue
+
+  ! J Teixeira fix
+
+  CALL zilch(byp, ncum)
+  DO i = 1, ncum
+    lcape(i) = .TRUE.
+  END DO
+  DO k = minorig + 1, nl - 1
+    DO i = 1, ncum
+      IF (cape(i)<0.0) lcape(i) = .FALSE.
+      IF ((k>=(icb(i) + 1)) .AND. lcape(i)) THEN
+        by = (tvp(i, k) - tv(i, k)) * dph(i, k) / p(i, k)
+        byp(i) = (tvp(i, k + 1) - tv(i, k + 1)) * dph(i, k + 1) / p(i, k + 1)
+        cape(i) = cape(i) + by
+        IF (by>=0.0) inb1(i) = k + 1
+        IF (cape(i)>0.0) THEN
+          inb(i) = k + 1
+          capem(i) = cape(i)
+        END IF
+      END IF
+    END DO
+  END DO
+  DO i = 1, ncum
+    cape(i) = capem(i) + byp(i)
+    defrac = capem(i) - cape(i)
+    defrac = max(defrac, 0.001)
+    frac(i) = -cape(i) / defrac
+    frac(i) = min(frac(i), 1.0)
+    frac(i) = max(frac(i), 0.0)
+  END DO
+
+  ! =====================================================================
+  ! ---   CALCULATE LIQUID WATER STATIC ENERGY OF LIFTED PARCEL
+  ! =====================================================================
+
+  ! initialization:
+  DO i = 1, ncum * nlp
+    hp(i, 1) = h(i, 1)
+  END DO
+
+  DO k = minorig + 1, nl
+    DO i = 1, ncum
+      IF ((k>=icb(i)) .AND. (k<=inb(i))) THEN
+        hp(i, k) = h(i, nk(i)) + (lv(i, k) + (cpd - cpv) * t(i, k)) * ep(i, k) * clw(i, k &
+                )
+      END IF
+    END DO
+  END DO
+
+END SUBROUTINE cv_undilute2
+
+SUBROUTINE cv_closure(nloc, ncum, nd, nk, icb, tv, tvp, p, ph, dph, plcl, &
+        cpn, iflag, cbmf)
+  USE lmdz_cvthermo
+
+  IMPLICIT NONE
+
+  ! inputs:
+  INTEGER ncum, nd, nloc
+  INTEGER nk(nloc), icb(nloc)
+  REAL tv(nloc, nd), tvp(nloc, nd), p(nloc, nd), dph(nloc, nd)
+  REAL ph(nloc, nd + 1) ! caution nd instead ndp1 to be consistent...
+  REAL plcl(nloc), cpn(nloc, nd)
+
+  ! outputs:
+  INTEGER iflag(nloc)
+  REAL cbmf(nloc) ! also an input
+
+  ! local variables:
+  INTEGER i, k, icbmax
+  REAL dtpbl(nloc), dtmin(nloc), tvpplcl(nloc), tvaplcl(nloc)
+  REAL work(nloc)
+
+  
+  ! -------------------------------------------------------------------
+  ! Compute icbmax.
+  ! -------------------------------------------------------------------
+
+  icbmax = 2
+  DO i = 1, ncum
+    icbmax = max(icbmax, icb(i))
+  END DO
+
+  ! =====================================================================
+  ! ---  CALCULATE CLOUD BASE MASS FLUX
+  ! =====================================================================
+
+  ! tvpplcl = parcel temperature lifted adiabatically from level
+  ! icb-1 to the LCL.
+  ! tvaplcl = virtual temperature at the LCL.
+
+  DO i = 1, ncum
+    dtpbl(i) = 0.0
+    tvpplcl(i) = tvp(i, icb(i) - 1) - rrd * tvp(i, icb(i) - 1) * (p(i, icb(i) - 1) - plcl(&
+            i)) / (cpn(i, icb(i) - 1) * p(i, icb(i) - 1))
+    tvaplcl(i) = tv(i, icb(i)) + (tvp(i, icb(i)) - tvp(i, icb(i) + 1)) * (plcl(i) - p(i &
+            , icb(i))) / (p(i, icb(i)) - p(i, icb(i) + 1))
+  END DO
+
+  ! -------------------------------------------------------------------
+  ! --- Interpolate difference between lifted parcel and
+  ! --- environmental temperatures to lifted condensation level
+  ! -------------------------------------------------------------------
+
+  ! dtpbl = average of tvp-tv in the PBL (k=nk to icb-1).
+
+  DO k = minorig, icbmax
+    DO i = 1, ncum
+      IF ((k>=nk(i)) .AND. (k<=(icb(i) - 1))) THEN
+        dtpbl(i) = dtpbl(i) + (tvp(i, k) - tv(i, k)) * dph(i, k)
+      END IF
+    END DO
+  END DO
+  DO i = 1, ncum
+    dtpbl(i) = dtpbl(i) / (ph(i, nk(i)) - ph(i, icb(i)))
+    dtmin(i) = tvpplcl(i) - tvaplcl(i) + dtmax + dtpbl(i)
+  END DO
+
+  ! -------------------------------------------------------------------
+  ! --- Adjust cloud base mass flux
+  ! -------------------------------------------------------------------
+
+  DO i = 1, ncum
+    work(i) = cbmf(i)
+    cbmf(i) = max(0.0, (1.0 - damp) * cbmf(i) + 0.1 * alpha * dtmin(i))
+    IF ((work(i)==0.0) .AND. (cbmf(i)==0.0)) THEN
+      iflag(i) = 3
+    END IF
+  END DO
+
+END SUBROUTINE cv_closure
+
+SUBROUTINE cv_mixing(nloc, ncum, nd, icb, nk, inb, inb1, ph, t, q, qs, u, v, &
+        h, lv, qnk, hp, tv, tvp, ep, clw, cbmf, m, ment, qent, uent, vent, nent, &
+        sij, elij)
+  USE lmdz_cvthermo
+
+  IMPLICIT NONE
+
+  
+  ! inputs:
+  INTEGER ncum, nd, nloc
+  INTEGER icb(nloc), inb(nloc), inb1(nloc), nk(nloc)
+  REAL cbmf(nloc), qnk(nloc)
+  REAL ph(nloc, nd + 1)
+  REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd), lv(nloc, nd)
+  REAL u(nloc, nd), v(nloc, nd), h(nloc, nd), hp(nloc, nd)
+  REAL tv(nloc, nd), tvp(nloc, nd), ep(nloc, nd), clw(nloc, nd)
+
+  ! outputs:
+  INTEGER nent(nloc, nd)
+  REAL m(nloc, nd), ment(nloc, nd, nd), qent(nloc, nd, nd)
+  REAL uent(nloc, nd, nd), vent(nloc, nd, nd)
+  REAL sij(nloc, nd, nd), elij(nloc, nd, nd)
+
+  ! local variables:
+  INTEGER i, j, k, ij
+  INTEGER num1, num2
+  REAL dbo, qti, bf2, anum, denom, dei, altem, cwat, stemp
+  REAL alt, qp1, smid, sjmin, sjmax, delp, delm
+  REAL work(nloc), asij(nloc), smin(nloc), scrit(nloc)
+  REAL bsum(nloc, nd)
+  LOGICAL lwork(nloc)
+
+  ! =====================================================================
+  ! --- INITIALIZE VARIOUS ARRAYS USED IN THE COMPUTATIONS
+  ! =====================================================================
+
+  DO i = 1, ncum * nlp
+    nent(i, 1) = 0
+    m(i, 1) = 0.0
+  END DO
+
+  DO k = 1, nlp
+    DO j = 1, nlp
+      DO i = 1, ncum
+        qent(i, k, j) = q(i, j)
+        uent(i, k, j) = u(i, j)
+        vent(i, k, j) = v(i, j)
+        elij(i, k, j) = 0.0
+        ment(i, k, j) = 0.0
+        sij(i, k, j) = 0.0
+      END DO
+    END DO
+  END DO
+
+  ! -------------------------------------------------------------------
+  ! --- Calculate rates of mixing,  m(i)
+  ! -------------------------------------------------------------------
+
+  CALL zilch(work, ncum)
+
+  DO j = minorig + 1, nl
+    DO i = 1, ncum
+      IF ((j>=(icb(i) + 1)) .AND. (j<=inb(i))) THEN
+        k = min(j, inb1(i))
+        dbo = abs(tv(i, k + 1) - tvp(i, k + 1) - tv(i, k - 1) + tvp(i, k - 1)) + &
+                entp * 0.04 * (ph(i, k) - ph(i, k + 1))
+        work(i) = work(i) + dbo
+        m(i, j) = cbmf(i) * dbo
+      END IF
+    END DO
+  END DO
+  DO k = minorig + 1, nl
+    DO i = 1, ncum
+      IF ((k>=(icb(i) + 1)) .AND. (k<=inb(i))) THEN
+        m(i, k) = m(i, k) / work(i)
+      END IF
+    END DO
+  END DO
+
+
+  ! =====================================================================
+  ! --- CALCULATE ENTRAINED AIR MASS FLUX (ment), TOTAL WATER MIXING
+  ! --- RATIO (QENT), TOTAL CONDENSED WATER (elij), AND MIXING
+  ! --- FRACTION (sij)
+  ! =====================================================================
+
+  DO i = minorig + 1, nl
+    DO j = minorig + 1, nl
+      DO ij = 1, ncum
+        IF ((i>=(icb(ij) + 1)) .AND. (j>=icb(ij)) .AND. (i<=inb(ij)) .AND. (j<= &
+                inb(ij))) THEN
+          qti = qnk(ij) - ep(ij, i) * clw(ij, i)
+          bf2 = 1. + lv(ij, j) * lv(ij, j) * qs(ij, j) / (rrv * t(ij, j) * t(ij, j) * cpd)
+          anum = h(ij, j) - hp(ij, i) + (cpv - cpd) * t(ij, j) * (qti - q(ij, j))
+          denom = h(ij, i) - hp(ij, i) + (cpd - cpv) * (q(ij, i) - qti) * t(ij, j)
+          dei = denom
+          IF (abs(dei)<0.01) dei = 0.01
+          sij(ij, i, j) = anum / dei
+          sij(ij, i, i) = 1.0
+          altem = sij(ij, i, j) * q(ij, i) + (1. - sij(ij, i, j)) * qti - qs(ij, j)
+          altem = altem / bf2
+          cwat = clw(ij, j) * (1. - ep(ij, j))
+          stemp = sij(ij, i, j)
+          IF ((stemp<0.0 .OR. stemp>1.0 .OR. altem>cwat) .AND. j>i) THEN
+            anum = anum - lv(ij, j) * (qti - qs(ij, j) - cwat * bf2)
+            denom = denom + lv(ij, j) * (q(ij, i) - qti)
+            IF (abs(denom)<0.01) denom = 0.01
+            sij(ij, i, j) = anum / denom
+            altem = sij(ij, i, j) * q(ij, i) + (1. - sij(ij, i, j)) * qti - qs(ij, j)
+            altem = altem - (bf2 - 1.) * cwat
+          END IF
+          IF (sij(ij, i, j)>0.0 .AND. sij(ij, i, j)<0.9) THEN
+            qent(ij, i, j) = sij(ij, i, j) * q(ij, i) + (1. - sij(ij, i, j)) * qti
+            uent(ij, i, j) = sij(ij, i, j) * u(ij, i) + &
+                    (1. - sij(ij, i, j)) * u(ij, nk(ij))
+            vent(ij, i, j) = sij(ij, i, j) * v(ij, i) + &
+                    (1. - sij(ij, i, j)) * v(ij, nk(ij))
+            elij(ij, i, j) = altem
+            elij(ij, i, j) = max(0.0, elij(ij, i, j))
+            ment(ij, i, j) = m(ij, i) / (1. - sij(ij, i, j))
+            nent(ij, i) = nent(ij, i) + 1
+          END IF
+          sij(ij, i, j) = max(0.0, sij(ij, i, j))
+          sij(ij, i, j) = min(1.0, sij(ij, i, j))
+        END IF
+      END DO
+    END DO
+
+    ! ***   If no air can entrain at level i assume that updraft detrains
+    ! ***
+    ! ***   at that level and calculate detrained air flux and properties
+    ! ***
+
+    DO ij = 1, ncum
+      IF ((i>=(icb(ij) + 1)) .AND. (i<=inb(ij)) .AND. (nent(ij, i)==0)) THEN
+        ment(ij, i, i) = m(ij, i)
+        qent(ij, i, i) = q(ij, nk(ij)) - ep(ij, i) * clw(ij, i)
+        uent(ij, i, i) = u(ij, nk(ij))
+        vent(ij, i, i) = v(ij, nk(ij))
+        elij(ij, i, i) = clw(ij, i)
+        sij(ij, i, i) = 1.0
+      END IF
+    END DO
+  END DO
+
+  DO i = 1, ncum
+    sij(i, inb(i), inb(i)) = 1.0
+  END DO
+
+  ! =====================================================================
+  ! ---  NORMALIZE ENTRAINED AIR MASS FLUXES
+  ! ---  TO REPRESENT EQUAL PROBABILITIES OF MIXING
+  ! =====================================================================
+
+  CALL zilch(bsum, ncum * nlp)
+  DO ij = 1, ncum
+    lwork(ij) = .FALSE.
+  END DO
+  DO i = minorig + 1, nl
+
+    num1 = 0
+    DO ij = 1, ncum
+      IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij))) num1 = num1 + 1
+    END DO
+    IF (num1<=0) GO TO 789
+
+    DO ij = 1, ncum
+      IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij))) THEN
+        lwork(ij) = (nent(ij, i)/=0)
+        qp1 = q(ij, nk(ij)) - ep(ij, i) * clw(ij, i)
+        anum = h(ij, i) - hp(ij, i) - lv(ij, i) * (qp1 - qs(ij, i))
+        denom = h(ij, i) - hp(ij, i) + lv(ij, i) * (q(ij, i) - qp1)
+        IF (abs(denom)<0.01) denom = 0.01
+        scrit(ij) = anum / denom
+        alt = qp1 - qs(ij, i) + scrit(ij) * (q(ij, i) - qp1)
+        IF (scrit(ij)<0.0 .OR. alt<0.0) scrit(ij) = 1.0
+        asij(ij) = 0.0
+        smin(ij) = 1.0
+      END IF
+    END DO
+    DO j = minorig, nl
+
+      num2 = 0
+      DO ij = 1, ncum
+        IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (j>=icb(&
+                ij)) .AND. (j<=inb(ij)) .AND. lwork(ij)) num2 = num2 + 1
+      END DO
+      IF (num2<=0) GO TO 783
+
+      DO ij = 1, ncum
+        IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (j>=icb(&
+                ij)) .AND. (j<=inb(ij)) .AND. lwork(ij)) THEN
+          IF (sij(ij, i, j)>0.0 .AND. sij(ij, i, j)<0.9) THEN
+            IF (j>i) THEN
+              smid = min(sij(ij, i, j), scrit(ij))
+              sjmax = smid
+              sjmin = smid
+              IF (smid<smin(ij) .AND. sij(ij, i, j + 1)<smid) THEN
+                smin(ij) = smid
+                sjmax = min(sij(ij, i, j + 1), sij(ij, i, j), scrit(ij))
+                sjmin = max(sij(ij, i, j - 1), sij(ij, i, j))
+                sjmin = min(sjmin, scrit(ij))
+              END IF
+            ELSE
+              sjmax = max(sij(ij, i, j + 1), scrit(ij))
+              smid = max(sij(ij, i, j), scrit(ij))
+              sjmin = 0.0
+              IF (j>1) sjmin = sij(ij, i, j - 1)
+              sjmin = max(sjmin, scrit(ij))
+            END IF
+            delp = abs(sjmax - smid)
+            delm = abs(sjmin - smid)
+            asij(ij) = asij(ij) + (delp + delm) * (ph(ij, j) - ph(ij, j + 1))
+            ment(ij, i, j) = ment(ij, i, j) * (delp + delm) * (ph(ij, j) - ph(ij, j + 1))
+          END IF
+        END IF
+      END DO
+    783 END DO
+    DO ij = 1, ncum
+      IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. lwork(ij)) THEN
+        asij(ij) = max(1.0E-21, asij(ij))
+        asij(ij) = 1.0 / asij(ij)
+        bsum(ij, i) = 0.0
+      END IF
+    END DO
+    DO j = minorig, nl + 1
+      DO ij = 1, ncum
+        IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (j>=icb(&
+                ij)) .AND. (j<=inb(ij)) .AND. lwork(ij)) THEN
+          ment(ij, i, j) = ment(ij, i, j) * asij(ij)
+          bsum(ij, i) = bsum(ij, i) + ment(ij, i, j)
+        END IF
+      END DO
+    END DO
+    DO ij = 1, ncum
+      IF ((i>=icb(ij) + 1) .AND. (i<=inb(ij)) .AND. (bsum(ij, &
+              i)<1.0E-18) .AND. lwork(ij)) THEN
+        nent(ij, i) = 0
+        ment(ij, i, i) = m(ij, i)
+        qent(ij, i, i) = q(ij, nk(ij)) - ep(ij, i) * clw(ij, i)
+        uent(ij, i, i) = u(ij, nk(ij))
+        vent(ij, i, i) = v(ij, nk(ij))
+        elij(ij, i, i) = clw(ij, i)
+        sij(ij, i, i) = 1.0
+      END IF
+    END DO
+  789 END DO
+
+END SUBROUTINE cv_mixing
+
+SUBROUTINE cv_unsat(nloc, ncum, nd, inb, t, q, qs, gz, u, v, p, ph, h, lv, &
+        ep, sigp, clw, m, ment, elij, iflag, mp, qp, up, vp, wt, water, evap)
+  USE lmdz_cvthermo
+
+  IMPLICIT NONE
+
+  
+  ! inputs:
+  INTEGER ncum, nd, nloc
+  INTEGER inb(nloc)
+  REAL t(nloc, nd), q(nloc, nd), qs(nloc, nd)
+  REAL gz(nloc, nd), u(nloc, nd), v(nloc, nd)
+  REAL p(nloc, nd), ph(nloc, nd + 1), h(nloc, nd)
+  REAL lv(nloc, nd), ep(nloc, nd), sigp(nloc, nd), clw(nloc, nd)
+  REAL m(nloc, nd), ment(nloc, nd, nd), elij(nloc, nd, nd)
+
+  ! outputs:
+  INTEGER iflag(nloc) ! also an input
+  REAL mp(nloc, nd), qp(nloc, nd), up(nloc, nd), vp(nloc, nd)
+  REAL water(nloc, nd), evap(nloc, nd), wt(nloc, nd)
+
+  ! local variables:
+  INTEGER i, j, k, ij, num1
+  INTEGER jtt(nloc)
+  REAL awat, coeff, qsm, afac, sigt, b6, c6, revap
+  REAL dhdp, fac, qstm, rat
+  REAL wdtrain(nloc)
+  LOGICAL lwork(nloc)
+
+  ! =====================================================================
+  ! --- PRECIPITATING DOWNDRAFT CALCULATION
+  ! =====================================================================
+
+  ! Initializations:
+
+  DO i = 1, ncum
+    DO k = 1, nl + 1
+      wt(i, k) = omtsnow
+      mp(i, k) = 0.0
+      evap(i, k) = 0.0
+      water(i, k) = 0.0
+    END DO
+  END DO
+
+  DO i = 1, ncum
+    qp(i, 1) = q(i, 1)
+    up(i, 1) = u(i, 1)
+    vp(i, 1) = v(i, 1)
+  END DO
+
+  DO k = 2, nl + 1
+    DO i = 1, ncum
+      qp(i, k) = q(i, k - 1)
+      up(i, k) = u(i, k - 1)
+      vp(i, k) = v(i, k - 1)
+    END DO
+  END DO
+
+
+  ! ***  Check whether ep(inb)=0, if so, skip precipitating    ***
+  ! ***             downdraft calculation                      ***
+
+
+  ! ***  Integrate liquid water equation to find condensed water   ***
+  ! ***                and condensed water flux                    ***
+
+  DO i = 1, ncum
+    jtt(i) = 2
+    IF (ep(i, inb(i))<=0.0001) iflag(i) = 2
+    IF (iflag(i)==0) THEN
+      lwork(i) = .TRUE.
+    ELSE
+      lwork(i) = .FALSE.
+    END IF
+  END DO
+
+  ! ***                    Begin downdraft loop                    ***
+
+  CALL zilch(wdtrain, ncum)
+  DO i = nl + 1, 1, -1
+
+    num1 = 0
+    DO ij = 1, ncum
+      IF ((i<=inb(ij)) .AND. lwork(ij)) num1 = num1 + 1
+    END DO
+    IF (num1<=0) GO TO 899
+
+
+    ! ***        Calculate detrained precipitation             ***
+
+    DO ij = 1, ncum
+      IF ((i<=inb(ij)) .AND. (lwork(ij))) THEN
+        wdtrain(ij) = g * ep(ij, i) * m(ij, i) * clw(ij, i)
+      END IF
+    END DO
+
+    IF (i>1) THEN
+      DO j = 1, i - 1
+        DO ij = 1, ncum
+          IF ((i<=inb(ij)) .AND. (lwork(ij))) THEN
+            awat = elij(ij, j, i) - (1. - ep(ij, i)) * clw(ij, i)
+            awat = max(0.0, awat)
+            wdtrain(ij) = wdtrain(ij) + g * awat * ment(ij, j, i)
+          END IF
+        END DO
+      END DO
+    END IF
+
+    ! ***    Find rain water and evaporation using provisional   ***
+    ! ***              estimates of qp(i)and qp(i-1)             ***
+
+
+    ! ***  Value of terminal velocity and coeffecient of evaporation for snow
+    ! ***
+
+    DO ij = 1, ncum
+      IF ((i<=inb(ij)) .AND. (lwork(ij))) THEN
+        coeff = coeffs
+        wt(ij, i) = omtsnow
+
+        ! ***  Value of terminal velocity and coeffecient of evaporation for
+        ! rain   ***
+
+        IF (t(ij, i)>273.0) THEN
+          coeff = coeffr
+          wt(ij, i) = omtrain
+        END IF
+        qsm = 0.5 * (q(ij, i) + qp(ij, i + 1))
+        afac = coeff * ph(ij, i) * (qs(ij, i) - qsm) / (1.0E4 + 2.0E3 * ph(ij, i) * qs(ij, i))
+        afac = max(afac, 0.0)
+        sigt = sigp(ij, i)
+        sigt = max(0.0, sigt)
+        sigt = min(1.0, sigt)
+        b6 = 100. * (ph(ij, i) - ph(ij, i + 1)) * sigt * afac / wt(ij, i)
+        c6 = (water(ij, i + 1) * wt(ij, i + 1) + wdtrain(ij) / sigd) / wt(ij, i)
+        revap = 0.5 * (-b6 + sqrt(b6 * b6 + 4. * c6))
+        evap(ij, i) = sigt * afac * revap
+        water(ij, i) = revap * revap
+
+        ! ***  Calculate precipitating downdraft mass flux under     ***
+        ! ***              hydrostatic approximation                 ***
+
+        IF (i>1) THEN
+          dhdp = (h(ij, i) - h(ij, i - 1)) / (p(ij, i - 1) - p(ij, i))
+          dhdp = max(dhdp, 10.0)
+          mp(ij, i) = 100. * ginv * lv(ij, i) * sigd * evap(ij, i) / dhdp
+          mp(ij, i) = max(mp(ij, i), 0.0)
+
+          ! ***   Add small amount of inertia to downdraft              ***
+
+          fac = 20.0 / (ph(ij, i - 1) - ph(ij, i))
+          mp(ij, i) = (fac * mp(ij, i + 1) + mp(ij, i)) / (1. + fac)
+
+          ! ***      Force mp to decrease linearly to zero
+          ! ***
+          ! ***      between about 950 mb and the surface
+          ! ***
+
+          IF (p(ij, i)>(0.949 * p(ij, 1))) THEN
+            jtt(ij) = max(jtt(ij), i)
+            mp(ij, i) = mp(ij, jtt(ij)) * (p(ij, 1) - p(ij, i)) / &
+                    (p(ij, 1) - p(ij, jtt(ij)))
+          END IF
+        END IF
+
+        ! ***       Find mixing ratio of precipitating downdraft     ***
+
+        IF (i/=inb(ij)) THEN
+          IF (i==1) THEN
+            qstm = qs(ij, 1)
+          ELSE
+            qstm = qs(ij, i - 1)
+          END IF
+          IF (mp(ij, i)>mp(ij, i + 1)) THEN
+            rat = mp(ij, i + 1) / mp(ij, i)
+            qp(ij, i) = qp(ij, i + 1) * rat + q(ij, i) * (1.0 - rat) + &
+                    100. * ginv * sigd * (ph(ij, i) - ph(ij, i + 1)) * (evap(ij, i) / mp(ij, i))
+            up(ij, i) = up(ij, i + 1) * rat + u(ij, i) * (1. - rat)
+            vp(ij, i) = vp(ij, i + 1) * rat + v(ij, i) * (1. - rat)
+          ELSE
+            IF (mp(ij, i + 1)>0.0) THEN
+              qp(ij, i) = (gz(ij, i + 1) - gz(ij, i) + qp(ij, i + 1) * (lv(ij, i + 1) + t(ij, &
+                      i + 1) * (cl - cpd)) + cpd * (t(ij, i + 1) - t(ij, &
+                      i))) / (lv(ij, i) + t(ij, i) * (cl - cpd))
+              up(ij, i) = up(ij, i + 1)
+              vp(ij, i) = vp(ij, i + 1)
+            END IF
+          END IF
+          qp(ij, i) = min(qp(ij, i), qstm)
+          qp(ij, i) = max(qp(ij, i), 0.0)
+        END IF
+      END IF
+    END DO
+  899 END DO
+
+END SUBROUTINE cv_unsat
+
+SUBROUTINE cv_yield(nloc, ncum, nd, nk, icb, inb, delt, t, q, u, v, gz, p, &
+        ph, h, hp, lv, cpn, ep, clw, frac, m, mp, qp, up, vp, wt, water, evap, &
+        ment, qent, uent, vent, nent, elij, tv, tvp, iflag, wd, qprime, tprime, &
+        precip, cbmf, ft, fq, fu, fv, ma, qcondc)
+  USE lmdz_cvthermo
+
+  IMPLICIT NONE
+
+  
+  ! inputs
+  INTEGER ncum, nd, nloc
+  INTEGER nk(nloc), icb(nloc), inb(nloc)
+  INTEGER nent(nloc, nd)
+  REAL delt
+  REAL t(nloc, nd), q(nloc, nd), u(nloc, nd), v(nloc, nd)
+  REAL gz(nloc, nd)
+  REAL p(nloc, nd), ph(nloc, nd + 1), h(nloc, nd)
+  REAL hp(nloc, nd), lv(nloc, nd)
+  REAL cpn(nloc, nd), ep(nloc, nd), clw(nloc, nd), frac(nloc)
+  REAL m(nloc, nd), mp(nloc, nd), qp(nloc, nd)
+  REAL up(nloc, nd), vp(nloc, nd)
+  REAL wt(nloc, nd), water(nloc, nd), evap(nloc, nd)
+  REAL ment(nloc, nd, nd), qent(nloc, nd, nd), elij(nloc, nd, nd)
+  REAL uent(nloc, nd, nd), vent(nloc, nd, nd)
+  REAL tv(nloc, nd), tvp(nloc, nd)
+
+  ! outputs
+  INTEGER iflag(nloc) ! also an input
+  REAL cbmf(nloc) ! also an input
+  REAL wd(nloc), tprime(nloc), qprime(nloc)
+  REAL precip(nloc)
+  REAL ft(nloc, nd), fq(nloc, nd), fu(nloc, nd), fv(nloc, nd)
+  REAL ma(nloc, nd)
+  REAL qcondc(nloc, nd)
+
+  ! local variables
+  INTEGER i, j, ij, k, num1
+  REAL dpinv, cpinv, awat, fqold, ftold, fuold, fvold, delti
+  REAL work(nloc), am(nloc), amp1(nloc), ad(nloc)
+  REAL ents(nloc), uav(nloc), vav(nloc), lvcp(nloc, nd)
+  REAL qcond(nloc, nd), nqcond(nloc, nd), wa(nloc, nd) ! cld
+  REAL siga(nloc, nd), ax(nloc, nd), mac(nloc, nd) ! cld
+
+
+  ! -- initializations:
+
+  delti = 1.0 / delt
+
+  DO i = 1, ncum
+    precip(i) = 0.0
+    wd(i) = 0.0
+    tprime(i) = 0.0
+    qprime(i) = 0.0
+    DO k = 1, nl + 1
+      ft(i, k) = 0.0
+      fu(i, k) = 0.0
+      fv(i, k) = 0.0
+      fq(i, k) = 0.0
+      lvcp(i, k) = lv(i, k) / cpn(i, k)
+      qcondc(i, k) = 0.0 ! cld
+      qcond(i, k) = 0.0 ! cld
+      nqcond(i, k) = 0.0 ! cld
+    END DO
+  END DO
+
+
+  ! ***  Calculate surface precipitation in mm/day     ***
+
+  DO i = 1, ncum
+    IF (iflag(i)<=1) THEN
+      ! c            precip(i)=precip(i)+wt(i,1)*sigd*water(i,1)*3600.*24000.
+      ! c     &                /(rowl*g)
+      ! c            precip(i)=precip(i)*delt/86400.
+      precip(i) = wt(i, 1) * sigd * water(i, 1) * 86400 / g
+    END IF
+  END DO
+
+
+  ! ***  Calculate downdraft velocity scale and surface temperature and  ***
+  ! ***                    water vapor fluctuations                      ***
+
+  DO i = 1, ncum
+    wd(i) = betad * abs(mp(i, icb(i))) * 0.01 * rrd * t(i, icb(i)) / (sigd * p(i, icb(i)))
+    qprime(i) = 0.5 * (qp(i, 1) - q(i, 1))
+    tprime(i) = lv0 * qprime(i) / cpd
+  END DO
+
+  ! ***  Calculate tendencies of lowest level potential temperature  ***
+  ! ***                      and mixing ratio                        ***
+
+  DO i = 1, ncum
+    work(i) = 0.01 / (ph(i, 1) - ph(i, 2))
+    am(i) = 0.0
+  END DO
+  DO k = 2, nl
+    DO i = 1, ncum
+      IF ((nk(i)==1) .AND. (k<=inb(i)) .AND. (nk(i)==1)) THEN
+        am(i) = am(i) + m(i, k)
+      END IF
+    END DO
+  END DO
+  DO i = 1, ncum
+    IF ((g * work(i) * am(i))>=delti) iflag(i) = 1
+    ft(i, 1) = ft(i, 1) + g * work(i) * am(i) * (t(i, 2) - t(i, 1) + (gz(i, 2) - gz(i, &
+            1)) / cpn(i, 1))
+    ft(i, 1) = ft(i, 1) - lvcp(i, 1) * sigd * evap(i, 1)
+    ft(i, 1) = ft(i, 1) + sigd * wt(i, 2) * (cl - cpd) * water(i, 2) * (t(i, 2) - t(i, 1)) * &
+            work(i) / cpn(i, 1)
+    fq(i, 1) = fq(i, 1) + g * mp(i, 2) * (qp(i, 2) - q(i, 1)) * work(i) + &
+            sigd * evap(i, 1)
+    fq(i, 1) = fq(i, 1) + g * am(i) * (q(i, 2) - q(i, 1)) * work(i)
+    fu(i, 1) = fu(i, 1) + g * work(i) * (mp(i, 2) * (up(i, 2) - u(i, 1)) + am(i) * (u(i, &
+            2) - u(i, 1)))
+    fv(i, 1) = fv(i, 1) + g * work(i) * (mp(i, 2) * (vp(i, 2) - v(i, 1)) + am(i) * (v(i, &
+            2) - v(i, 1)))
+  END DO
+  DO j = 2, nl
+    DO i = 1, ncum
+      IF (j<=inb(i)) THEN
+        fq(i, 1) = fq(i, 1) + g * work(i) * ment(i, j, 1) * (qent(i, j, 1) - q(i, 1))
+        fu(i, 1) = fu(i, 1) + g * work(i) * ment(i, j, 1) * (uent(i, j, 1) - u(i, 1))
+        fv(i, 1) = fv(i, 1) + g * work(i) * ment(i, j, 1) * (vent(i, j, 1) - v(i, 1))
+      END IF
+    END DO
+  END DO
+
+  ! ***  Calculate tendencies of potential temperature and mixing ratio  ***
+  ! ***               at levels above the lowest level                   ***
+
+  ! ***  First find the net saturated updraft and downdraft mass fluxes  ***
+  ! ***                      through each level                          ***
+
+  DO i = 2, nl + 1
+
+    num1 = 0
+    DO ij = 1, ncum
+      IF (i<=inb(ij)) num1 = num1 + 1
+    END DO
+    IF (num1<=0) GO TO 1500
+
+    CALL zilch(amp1, ncum)
+    CALL zilch(ad, ncum)
+
+    DO k = i + 1, nl + 1
+      DO ij = 1, ncum
+        IF ((i>=nk(ij)) .AND. (i<=inb(ij)) .AND. (k<=(inb(ij) + 1))) THEN
+          amp1(ij) = amp1(ij) + m(ij, k)
+        END IF
+      END DO
+    END DO
+
+    DO k = 1, i
+      DO j = i + 1, nl + 1
+        DO ij = 1, ncum
+          IF ((j<=(inb(ij) + 1)) .AND. (i<=inb(ij))) THEN
+            amp1(ij) = amp1(ij) + ment(ij, k, j)
+          END IF
+        END DO
+      END DO
+    END DO
+    DO k = 1, i - 1
+      DO j = i, nl + 1
+        DO ij = 1, ncum
+          IF ((i<=inb(ij)) .AND. (j<=inb(ij))) THEN
+            ad(ij) = ad(ij) + ment(ij, j, k)
+          END IF
+        END DO
+      END DO
+    END DO
+
+    DO ij = 1, ncum
+      IF (i<=inb(ij)) THEN
+        dpinv = 0.01 / (ph(ij, i) - ph(ij, i + 1))
+        cpinv = 1.0 / cpn(ij, i)
+
+        ft(ij, i) = ft(ij, i) + g * dpinv * (amp1(ij) * (t(ij, i + 1) - t(ij, &
+                i) + (gz(ij, i + 1) - gz(ij, i)) * cpinv) - ad(ij) * (t(ij, i) - t(ij, &
+                i - 1) + (gz(ij, i) - gz(ij, i - 1)) * cpinv)) - sigd * lvcp(ij, i) * evap(ij, i)
+        ft(ij, i) = ft(ij, i) + g * dpinv * ment(ij, i, i) * (hp(ij, i) - h(ij, i) + t(ij &
+                , i) * (cpv - cpd) * (q(ij, i) - qent(ij, i, i))) * cpinv
+        ft(ij, i) = ft(ij, i) + sigd * wt(ij, i + 1) * (cl - cpd) * water(ij, i + 1) * (t(&
+                ij, i + 1) - t(ij, i)) * dpinv * cpinv
+        fq(ij, i) = fq(ij, i) + g * dpinv * (amp1(ij) * (q(ij, i + 1) - q(ij, &
+                i)) - ad(ij) * (q(ij, i) - q(ij, i - 1)))
+        fu(ij, i) = fu(ij, i) + g * dpinv * (amp1(ij) * (u(ij, i + 1) - u(ij, &
+                i)) - ad(ij) * (u(ij, i) - u(ij, i - 1)))
+        fv(ij, i) = fv(ij, i) + g * dpinv * (amp1(ij) * (v(ij, i + 1) - v(ij, &
+                i)) - ad(ij) * (v(ij, i) - v(ij, i - 1)))
+      END IF
+    END DO
+    DO k = 1, i - 1
+      DO ij = 1, ncum
+        IF (i<=inb(ij)) THEN
+          awat = elij(ij, k, i) - (1. - ep(ij, i)) * clw(ij, i)
+          awat = max(awat, 0.0)
+          fq(ij, i) = fq(ij, i) + g * dpinv * ment(ij, k, i) * (qent(ij, k, i) - awat - q &
+                  (ij, i))
+          fu(ij, i) = fu(ij, i) + g * dpinv * ment(ij, k, i) * (uent(ij, k, i) - u(ij, i &
+                  ))
+          fv(ij, i) = fv(ij, i) + g * dpinv * ment(ij, k, i) * (vent(ij, k, i) - v(ij, i &
+                  ))
+          ! (saturated updrafts resulting from mixing)               ! cld
+          qcond(ij, i) = qcond(ij, i) + (elij(ij, k, i) - awat) ! cld
+          nqcond(ij, i) = nqcond(ij, i) + 1. ! cld
+        END IF
+      END DO
+    END DO
+    DO k = i, nl + 1
+      DO ij = 1, ncum
+        IF ((i<=inb(ij)) .AND. (k<=inb(ij))) THEN
+          fq(ij, i) = fq(ij, i) + g * dpinv * ment(ij, k, i) * (qent(ij, k, i) - q(ij, i &
+                  ))
+          fu(ij, i) = fu(ij, i) + g * dpinv * ment(ij, k, i) * (uent(ij, k, i) - u(ij, i &
+                  ))
+          fv(ij, i) = fv(ij, i) + g * dpinv * ment(ij, k, i) * (vent(ij, k, i) - v(ij, i &
+                  ))
+        END IF
+      END DO
+    END DO
+    DO ij = 1, ncum
+      IF (i<=inb(ij)) THEN
+        fq(ij, i) = fq(ij, i) + sigd * evap(ij, i) + g * (mp(ij, i + 1) * (qp(ij, &
+                i + 1) - q(ij, i)) - mp(ij, i) * (qp(ij, i) - q(ij, i - 1))) * dpinv
+        fu(ij, i) = fu(ij, i) + g * (mp(ij, i + 1) * (up(ij, i + 1) - u(ij, &
+                i)) - mp(ij, i) * (up(ij, i) - u(ij, i - 1))) * dpinv
+        fv(ij, i) = fv(ij, i) + g * (mp(ij, i + 1) * (vp(ij, i + 1) - v(ij, &
+                i)) - mp(ij, i) * (vp(ij, i) - v(ij, i - 1))) * dpinv
+        ! (saturated downdrafts resulting from mixing)               ! cld
+        DO k = i + 1, inb(ij) ! cld
+          qcond(ij, i) = qcond(ij, i) + elij(ij, k, i) ! cld
+          nqcond(ij, i) = nqcond(ij, i) + 1. ! cld
+        END DO ! cld
+        ! (particular case: no detraining level is found)            ! cld
+        IF (nent(ij, i)==0) THEN ! cld
+          qcond(ij, i) = qcond(ij, i) + (1. - ep(ij, i)) * clw(ij, i) ! cld
+          nqcond(ij, i) = nqcond(ij, i) + 1. ! cld
+        END IF ! cld
+        IF (nqcond(ij, i)/=0.) THEN ! cld
+          qcond(ij, i) = qcond(ij, i) / nqcond(ij, i) ! cld
+        END IF ! cld
+      END IF
+    END DO
+  1500 END DO
+
+  ! *** Adjust tendencies at top of convection layer to reflect  ***
+  ! ***       actual position of the level zero cape             ***
+
+  DO ij = 1, ncum
+    fqold = fq(ij, inb(ij))
+    fq(ij, inb(ij)) = fq(ij, inb(ij)) * (1. - frac(ij))
+    fq(ij, inb(ij) - 1) = fq(ij, inb(ij) - 1) + frac(ij) * fqold * ((ph(ij, &
+            inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, &
+            inb(ij)))) * lv(ij, inb(ij)) / lv(ij, inb(ij) - 1)
+    ftold = ft(ij, inb(ij))
+    ft(ij, inb(ij)) = ft(ij, inb(ij)) * (1. - frac(ij))
+    ft(ij, inb(ij) - 1) = ft(ij, inb(ij) - 1) + frac(ij) * ftold * ((ph(ij, &
+            inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, &
+            inb(ij)))) * cpn(ij, inb(ij)) / cpn(ij, inb(ij) - 1)
+    fuold = fu(ij, inb(ij))
+    fu(ij, inb(ij)) = fu(ij, inb(ij)) * (1. - frac(ij))
+    fu(ij, inb(ij) - 1) = fu(ij, inb(ij) - 1) + frac(ij) * fuold * ((ph(ij, &
+            inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, inb(ij))))
+    fvold = fv(ij, inb(ij))
+    fv(ij, inb(ij)) = fv(ij, inb(ij)) * (1. - frac(ij))
+    fv(ij, inb(ij) - 1) = fv(ij, inb(ij) - 1) + frac(ij) * fvold * ((ph(ij, &
+            inb(ij)) - ph(ij, inb(ij) + 1)) / (ph(ij, inb(ij) - 1) - ph(ij, inb(ij))))
+  END DO
+
+  ! ***   Very slightly adjust tendencies to force exact   ***
+  ! ***     enthalpy, momentum and tracer conservation     ***
+
+  DO ij = 1, ncum
+    ents(ij) = 0.0
+    uav(ij) = 0.0
+    vav(ij) = 0.0
+    DO i = 1, inb(ij)
+      ents(ij) = ents(ij) + (cpn(ij, i) * ft(ij, i) + lv(ij, i) * fq(ij, i)) * (ph(ij, i) - &
+              ph(ij, i + 1))
+      uav(ij) = uav(ij) + fu(ij, i) * (ph(ij, i) - ph(ij, i + 1))
+      vav(ij) = vav(ij) + fv(ij, i) * (ph(ij, i) - ph(ij, i + 1))
+    END DO
+  END DO
+  DO ij = 1, ncum
+    ents(ij) = ents(ij) / (ph(ij, 1) - ph(ij, inb(ij) + 1))
+    uav(ij) = uav(ij) / (ph(ij, 1) - ph(ij, inb(ij) + 1))
+    vav(ij) = vav(ij) / (ph(ij, 1) - ph(ij, inb(ij) + 1))
+  END DO
+  DO ij = 1, ncum
+    DO i = 1, inb(ij)
+      ft(ij, i) = ft(ij, i) - ents(ij) / cpn(ij, i)
+      fu(ij, i) = (1. - cu) * (fu(ij, i) - uav(ij))
+      fv(ij, i) = (1. - cu) * (fv(ij, i) - vav(ij))
+    END DO
+  END DO
+
+  DO k = 1, nl + 1
+    DO i = 1, ncum
+      IF ((q(i, k) + delt * fq(i, k))<0.0) iflag(i) = 10
+    END DO
+  END DO
+
+  DO i = 1, ncum
+    IF (iflag(i)>2) THEN
+      precip(i) = 0.0
+      cbmf(i) = 0.0
+    END IF
+  END DO
+  DO k = 1, nl
+    DO i = 1, ncum
+      IF (iflag(i)>2) THEN
+        ft(i, k) = 0.0
+        fq(i, k) = 0.0
+        fu(i, k) = 0.0
+        fv(i, k) = 0.0
+        qcondc(i, k) = 0.0 ! cld
+      END IF
+    END DO
+  END DO
+
+  DO k = 1, nl + 1
+    DO i = 1, ncum
+      ma(i, k) = 0.
+    END DO
+  END DO
+  DO k = nl, 1, -1
+    DO i = 1, ncum
+      ma(i, k) = ma(i, k + 1) + m(i, k)
+    END DO
+  END DO
+
+
+  ! *** diagnose the in-cloud mixing ratio   ***            ! cld
+  ! ***           of condensed water         ***            ! cld
+  ! cld
+  DO ij = 1, ncum ! cld
+    DO i = 1, nd ! cld
+      mac(ij, i) = 0.0 ! cld
+      wa(ij, i) = 0.0 ! cld
+      siga(ij, i) = 0.0 ! cld
+    END DO ! cld
+    DO i = nk(ij), inb(ij) ! cld
+      DO k = i + 1, inb(ij) + 1 ! cld
+        mac(ij, i) = mac(ij, i) + m(ij, k) ! cld
+      END DO ! cld
+    END DO ! cld
+    DO i = icb(ij), inb(ij) - 1 ! cld
+      ax(ij, i) = 0. ! cld
+      DO j = icb(ij), i ! cld
+        ax(ij, i) = ax(ij, i) + rrd * (tvp(ij, j) - tv(ij, j)) & ! cld
+                * (ph(ij, j) - ph(ij, j + 1)) / p(ij, j) ! cld
+      END DO ! cld
+      IF (ax(ij, i)>0.0) THEN ! cld
+        wa(ij, i) = sqrt(2. * ax(ij, i)) ! cld
+      END IF ! cld
+    END DO ! cld
+    DO i = 1, nl ! cld
+      IF (wa(ij, i)>0.0) &          ! cld
+              siga(ij, i) = mac(ij, i) / wa(ij, i) & ! cld
+                      * rrd * tvp(ij, i) / p(ij, i) / 100. / delta ! cld
+      siga(ij, i) = min(siga(ij, i), 1.0) ! cld
+      qcondc(ij, i) = siga(ij, i) * clw(ij, i) * (1. - ep(ij, i)) & ! cld
+              + (1. - siga(ij, i)) * qcond(ij, i) ! cld
+    END DO ! cld
+  END DO ! cld
+
+END SUBROUTINE cv_yield
+
+SUBROUTINE cv_uncompress(nloc, len, ncum, nd, idcum, iflag, precip, cbmf, ft, &
+        fq, fu, fv, ma, qcondc, iflag1, precip1, cbmf1, ft1, fq1, fu1, fv1, ma1, &
+        qcondc1)
+  IMPLICIT NONE
+
+  
+  ! inputs:
+  INTEGER len, ncum, nd, nloc
+  INTEGER idcum(nloc)
+  INTEGER iflag(nloc)
+  REAL precip(nloc), cbmf(nloc)
+  REAL ft(nloc, nd), fq(nloc, nd), fu(nloc, nd), fv(nloc, nd)
+  REAL ma(nloc, nd)
+  REAL qcondc(nloc, nd) !cld
+
+  ! outputs:
+  INTEGER iflag1(len)
+  REAL precip1(len), cbmf1(len)
+  REAL ft1(len, nd), fq1(len, nd), fu1(len, nd), fv1(len, nd)
+  REAL ma1(len, nd)
+  REAL qcondc1(len, nd) !cld
+
+  ! local variables:
+  INTEGER i, k
+
+  DO i = 1, ncum
+    precip1(idcum(i)) = precip(i)
+    cbmf1(idcum(i)) = cbmf(i)
+    iflag1(idcum(i)) = iflag(i)
+  END DO
+
+  DO k = 1, nl
+    DO i = 1, ncum
+      ft1(idcum(i), k) = ft(i, k)
+      fq1(idcum(i), k) = fq(i, k)
+      fu1(idcum(i), k) = fu(i, k)
+      fv1(idcum(i), k) = fv(i, k)
+      ma1(idcum(i), k) = ma(i, k)
+      qcondc1(idcum(i), k) = qcondc(i, k)
+    END DO
+  END DO
+
+END SUBROUTINE cv_uncompress
+
+
+END MODULE lmdz_cv
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_dimpft.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_dimpft.f90	(revision 5142)
+++ LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_dimpft.f90	(revision 5142)
@@ -0,0 +1,1 @@
+link ../phylmd/lmdz_dimpft.f90
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_fcg_gcssold.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_fcg_gcssold.f90	(revision 5142)
+++ LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_fcg_gcssold.f90	(revision 5142)
@@ -0,0 +1,1 @@
+link ../phylmd/lmdz_fcg_gcssold.f90
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_planete.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_planete.f90	(revision 5142)
+++ LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_planete.f90	(revision 5142)
@@ -0,0 +1,1 @@
+link ../phylmd/lmdz_planete.f90
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_tsoilnudge.f90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_tsoilnudge.f90	(revision 5142)
+++ LMDZ6/branches/Amaury_dev/libf/phylmdiso/lmdz_tsoilnudge.f90	(revision 5142)
@@ -0,0 +1,1 @@
+link ../phylmd/lmdz_tsoilnudge.f90
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/physiq_mod.F90
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/physiq_mod.F90	(revision 5141)
+++ LMDZ6/branches/Amaury_dev/libf/phylmdiso/physiq_mod.F90	(revision 5142)
@@ -423,4 +423,5 @@
     USE lmdz_compbl, ONLY: iflag_pbl, iflag_pbl_split, iflag_order2_sollw, ifl_pbltree
     USE lmdz_conema3
+    USE lmdz_dimpft, ONLY: nvm_lmdz
 
     IMPLICIT NONE
@@ -477,5 +478,4 @@
     include "regdim.h"
     include "dimsoil.h"
-    include "dimpft.h"
     !======================================================================
     LOGICAL, SAVE :: ok_volcan ! pour activer les diagnostics volcaniques
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/planete.h
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/planete.h	(revision 5141)
+++ 	(revision )
@@ -1,1 +1,0 @@
-link ../phylmd/planete.h
Index: LMDZ6/branches/Amaury_dev/libf/phylmdiso/tsoilnudge.h
===================================================================
--- LMDZ6/branches/Amaury_dev/libf/phylmdiso/tsoilnudge.h	(revision 5141)
+++ 	(revision )
@@ -1,1 +1,0 @@
-link ../phylmd/tsoilnudge.h
