Index: /LMDZ6/trunk/libf/phylmd/borne_var_surf.F90
===================================================================
--- /LMDZ6/trunk/libf/phylmd/borne_var_surf.F90	(revision 3489)
+++ /LMDZ6/trunk/libf/phylmd/borne_var_surf.F90	(revision 3489)
@@ -0,0 +1,111 @@
+SUBROUTINE borne_var_surf(klon,klev,nbsrf,                   &
+         iflag_bug_t2m_stab_ipslcm61,                        &
+         t1,q1,u1,v1,                                        &
+         ftsol, qsurf, pctsrf, paprs,                        &
+         t2m, q2m, u10m, v10m,                               &
+         zt2m_cor, zq2m_cor, zu10m_cor, zv10m_cor,           &
+         zrh2m_cor, zqsat2m_cor)
+
+IMPLICIT NONE
+
+!==================================================================
+! Declarations
+!==================================================================
+
+! arguments
+INTEGER klon,klev,nbsrf,iflag_bug_t2m_stab_ipslcm61
+REAL,DIMENSION(klon),INTENT(IN) :: t1, q1, u1, v1
+REAL,DIMENSION(klon,nbsrf),INTENT(IN) :: t2m, q2m, u10m, v10m
+REAL,DIMENSION(klon,nbsrf),INTENT(IN) :: ftsol, pctsrf
+REAL,DIMENSION(klon,klev+1),INTENT(IN) :: paprs 
+REAL,DIMENSION(klon),INTENT(IN) :: qsurf
+REAL,DIMENSION (klon),INTENT(OUT) :: zt2m_cor, zq2m_cor, zu10m_cor, zv10m_cor
+REAL,DIMENSION (klon),INTENT(OUT)  :: zrh2m_cor, zqsat2m_cor
+
+
+! local
+INTEGER i,nsrf
+REAL,DIMENSION (klon,nbsrf) :: t2m_cor, q2m_cor, u10m_cor, v10m_cor
+REAL                               :: zx_qs1, zcor1, zdelta1
+include "YOMCST.h"
+include "YOETHF.h"
+include "FCTTRE.h"
+!==================================================================
+! Correction of sub surface variables
+!==================================================================
+
+zrh2m_cor=0.
+zqsat2m_cor=0.
+
+DO nsrf=1,nbsrf
+   DO i=1,klon
+    t2m_cor(i,nsrf)=t2m(i,nsrf)
+    q2m_cor(i,nsrf)=q2m(i,nsrf)
+    u10m_cor(i,nsrf)=u10m(i,nsrf)
+    v10m_cor(i,nsrf)=v10m(i,nsrf)
+     IF(iflag_bug_t2m_stab_ipslcm61.EQ.-2.AND.q2m(i,nsrf).LT.0.) THEN
+      t2m_cor(i,nsrf)=MIN(t2m(i,nsrf),MAX(t1(i),ftsol(i,nsrf)))
+      t2m_cor(i,nsrf)=MAX(t2m_cor(i,nsrf),MIN(t1(i),ftsol(i,nsrf)))
+      q2m_cor(i,nsrf)=MIN(q2m(i,nsrf),MAX(q1(i),qsurf(i)))
+      q2m_cor(i,nsrf)=MAX(q2m_cor(i,nsrf),MIN(q1(i),qsurf(i)))
+      q2m_cor(i,nsrf)=MAX(q2m_cor(i,nsrf),0.)
+      u10m_cor(i,nsrf)=SIGN(MIN(ABS(u1(i)),ABS(u10m(i,nsrf))),u1(i))
+      v10m_cor(i,nsrf)=SIGN(MIN(ABS(v1(i)),ABS(v10m(i,nsrf))),v1(i))
+     ELSEIF(iflag_bug_t2m_stab_ipslcm61.EQ.-1.AND.(ftsol(i,nsrf).LE.t1(i).OR.q2m(i,nsrf).LT.0.)) THEN
+      t2m_cor(i,nsrf)=MIN(t2m(i,nsrf),MAX(t1(i),ftsol(i,nsrf)))
+      t2m_cor(i,nsrf)=MAX(t2m_cor(i,nsrf),MIN(t1(i),ftsol(i,nsrf)))
+      q2m_cor(i,nsrf)=MIN(q2m(i,nsrf),MAX(q1(i),qsurf(i)))
+      q2m_cor(i,nsrf)=MAX(q2m_cor(i,nsrf),MIN(q1(i),qsurf(i)))
+      q2m_cor(i,nsrf)=MAX(q2m_cor(i,nsrf),0.)
+      u10m_cor(i,nsrf)=SIGN(MIN(ABS(u1(i)),ABS(u10m(i,nsrf))),u1(i))
+      v10m_cor(i,nsrf)=SIGN(MIN(ABS(v1(i)),ABS(v10m(i,nsrf))),v1(i))
+     ELSEIF(iflag_bug_t2m_stab_ipslcm61.EQ.1.AND.ftsol(i,nsrf).LE.t1(i)) THEN
+      t2m_cor(i,nsrf)=MIN(t2m(i,nsrf),MAX(t1(i),ftsol(i,nsrf)))
+      t2m_cor(i,nsrf)=MAX(t2m_cor(i,nsrf),MIN(t1(i),ftsol(i,nsrf)))
+      q2m_cor(i,nsrf)=MIN(q2m(i,nsrf),MAX(q1(i),qsurf(i)))
+      q2m_cor(i,nsrf)=MAX(q2m_cor(i,nsrf),MIN(q1(i),qsurf(i)))
+      q2m_cor(i,nsrf)=MAX(q2m_cor(i,nsrf),0.)
+      u10m_cor(i,nsrf)=SIGN(MIN(ABS(u1(i)),ABS(u10m(i,nsrf))),u1(i))
+      v10m_cor(i,nsrf)=SIGN(MIN(ABS(v1(i)),ABS(v10m(i,nsrf))),v1(i))
+     ELSEIF(iflag_bug_t2m_stab_ipslcm61.EQ.0) THEN
+      t2m_cor(i,nsrf)=MIN(t2m(i,nsrf),MAX(t1(i),ftsol(i,nsrf)))
+      t2m_cor(i,nsrf)=MAX(t2m_cor(i,nsrf),MIN(t1(i),ftsol(i,nsrf)))
+      q2m_cor(i,nsrf)=MIN(q2m(i,nsrf),MAX(q1(i),qsurf(i)))
+      q2m_cor(i,nsrf)=MAX(q2m_cor(i,nsrf),MIN(q1(i),qsurf(i)))
+      q2m_cor(i,nsrf)=MAX(q2m_cor(i,nsrf),0.)
+      u10m_cor(i,nsrf)=SIGN(MIN(ABS(u1(i)),ABS(u10m(i,nsrf))),u1(i))
+      v10m_cor(i,nsrf)=SIGN(MIN(ABS(v1(i)),ABS(v10m(i,nsrf))),v1(i))
+     ENDIF
+!!!
+     zdelta1 = MAX(0.,SIGN(1., rtt-t2m_cor(i,nsrf) ))
+     zx_qs1  = r2es * FOEEW(t2m_cor(i,nsrf),zdelta1)/paprs(i,1)
+     zx_qs1  = MIN(0.5,zx_qs1)
+     zcor1   = 1./(1.-RETV*zx_qs1)
+     zx_qs1  = zx_qs1*zcor1
+     zrh2m_cor(i) = zrh2m_cor(i) + q2m_cor(i,nsrf)/zx_qs1 * pctsrf(i,nsrf)
+     zqsat2m_cor(i) = zqsat2m_cor(i) + zx_qs1  * pctsrf(i,nsrf)
+!!!
+   ENDDO
+ENDDO
+
+!==================================================================
+! Agregation of sub surfaces
+!==================================================================
+
+zt2m_cor=0.
+zq2m_cor=0.
+zu10m_cor=0.
+zv10m_cor=0.
+DO nsrf = 1, nbsrf
+   DO i = 1, klon
+      zt2m_cor(i)  = zt2m_cor(i)  + t2m_cor(i,nsrf)  * pctsrf(i,nsrf)
+      zq2m_cor(i)  = zq2m_cor(i)  + q2m_cor(i,nsrf)  * pctsrf(i,nsrf)
+      zu10m_cor(i) = zu10m_cor(i) + u10m_cor(i,nsrf) * pctsrf(i,nsrf)
+      zv10m_cor(i) = zv10m_cor(i) + v10m_cor(i,nsrf) * pctsrf(i,nsrf)
+   ENDDO
+ENDDO
+
+RETURN
+END
+
+
Index: /LMDZ6/trunk/libf/phylmd/phys_local_var_mod.F90
===================================================================
--- /LMDZ6/trunk/libf/phylmd/phys_local_var_mod.F90	(revision 3488)
+++ /LMDZ6/trunk/libf/phylmd/phys_local_var_mod.F90	(revision 3489)
@@ -380,4 +380,10 @@
       REAL,ALLOCATABLE,SAVE,DIMENSION(:) :: t2m_min_mon, t2m_max_mon
 !$OMP THREADPRIVATE(t2m_min_mon, t2m_max_mon)
+      REAL,ALLOCATABLE,SAVE,DIMENSION(:) :: zq2m_cor, zt2m_cor
+!$OMP THREADPRIVATE(zq2m_cor, zt2m_cor)
+      REAL,ALLOCATABLE,SAVE,DIMENSION(:) :: zu10m_cor, zv10m_cor
+!$OMP THREADPRIVATE(zu10m_cor, zv10m_cor)
+      REAL,ALLOCATABLE,SAVE,DIMENSION(:) :: zrh2m_cor, zqsat2m_cor
+!$OMP THREADPRIVATE(zrh2m_cor, zqsat2m_cor)
       REAL,ALLOCATABLE,SAVE,DIMENSION(:) :: weak_inversion
 !$OMP THREADPRIVATE(weak_inversion)
@@ -752,4 +758,6 @@
       ALLOCATE(zt2m_min_mon(klon), zt2m_max_mon(klon))
       ALLOCATE(t2m_min_mon(klon), t2m_max_mon(klon))
+      ALLOCATE(zq2m_cor(klon), zt2m_cor(klon), zu10m_cor(klon), zv10m_cor(klon))
+      ALLOCATE(zrh2m_cor(klon), zqsat2m_cor(klon))
       ALLOCATE(sens(klon), flwp(klon), fiwp(klon))
       ALLOCATE(alp_bl_conv(klon), alp_bl_det(klon))
@@ -1046,4 +1054,6 @@
       DEALLOCATE(zt2m_min_mon, zt2m_max_mon)
       DEALLOCATE(t2m_min_mon, t2m_max_mon)
+      DEALLOCATE(zq2m_cor, zt2m_cor, zu10m_cor, zv10m_cor)
+      DEALLOCATE(zrh2m_cor, zqsat2m_cor)
       DEALLOCATE(sens, flwp, fiwp)
       DEALLOCATE(alp_bl_conv,alp_bl_det)
Index: /LMDZ6/trunk/libf/phylmd/phys_output_write_mod.F90
===================================================================
--- /LMDZ6/trunk/libf/phylmd/phys_output_write_mod.F90	(revision 3488)
+++ /LMDZ6/trunk/libf/phylmd/phys_output_write_mod.F90	(revision 3489)
@@ -249,4 +249,5 @@
 
     USE phys_local_var_mod, ONLY: zxfluxlat, slp, ptstar, pt0, zxtsol, zt2m, &
+         zt2m_cor,zq2m_cor,zu10m_cor,zv10m_cor, zrh2m_cor, zqsat2m_cor, &
          t2m_min_mon, t2m_max_mon, evap, &
          l_mixmin,l_mix, &
@@ -665,7 +666,7 @@
        CALL histwrite_phy(o_slp, slp)
        CALL histwrite_phy(o_tsol, zxtsol)
-       CALL histwrite_phy(o_t2m, zt2m)
-       CALL histwrite_phy(o_t2m_min, zt2m)
-       CALL histwrite_phy(o_t2m_max, zt2m)
+       CALL histwrite_phy(o_t2m, zt2m_cor)
+       CALL histwrite_phy(o_t2m_min, zt2m_cor)
+       CALL histwrite_phy(o_t2m_max, zt2m_cor)
        CALL histwrite_phy(o_t2m_max_mon, t2m_max_mon)
        CALL histwrite_phy(o_t2m_min_mon, t2m_min_mon)
@@ -673,5 +674,5 @@
        IF (vars_defined) THEN
           DO i=1, klon
-             zx_tmp_fi2d(i)=SQRT(zu10m(i)*zu10m(i)+zv10m(i)*zv10m(i))
+             zx_tmp_fi2d(i)=SQRT(zu10m_cor(i)*zu10m_cor(i)+zv10m_cor(i)*zv10m_cor(i))
           ENDDO
        ENDIF
@@ -680,5 +681,5 @@
        IF (vars_defined) THEN
           DO i=1, klon
-             zx_tmp_fi2d(i)=SQRT(zu10m(i)*zu10m(i)+zv10m(i)*zv10m(i))
+             zx_tmp_fi2d(i)=SQRT(zu10m_cor(i)*zu10m_cor(i)+zv10m_cor(i)*zv10m_cor(i))
           ENDDO
        ENDIF
@@ -693,8 +694,8 @@
        ENDIF
        CALL histwrite_phy(o_sicf, zx_tmp_fi2d)
-       CALL histwrite_phy(o_q2m, zq2m)
+       CALL histwrite_phy(o_q2m, zq2m_cor)
        CALL histwrite_phy(o_ustar, zustar)
-       CALL histwrite_phy(o_u10m, zu10m)
-       CALL histwrite_phy(o_v10m, zv10m)
+       CALL histwrite_phy(o_u10m, zu10m_cor)
+       CALL histwrite_phy(o_v10m, zv10m_cor)
 
        IF (vars_defined) THEN
@@ -1330,5 +1331,5 @@
 !       CALL histwrite_phy(o_rh2m_max, zx_tmp_fi2d)
 
-       CALL histwrite_phy(o_qsat2m, qsat2m)
+       CALL histwrite_phy(o_qsat2m, zqsat2m_cor)
        CALL histwrite_phy(o_tpot, tpot)
        CALL histwrite_phy(o_tpote, tpote)
Index: /LMDZ6/trunk/libf/phylmd/physiq_mod.F90
===================================================================
--- /LMDZ6/trunk/libf/phylmd/physiq_mod.F90	(revision 3488)
+++ /LMDZ6/trunk/libf/phylmd/physiq_mod.F90	(revision 3489)
@@ -117,4 +117,6 @@
        zustar, zu10m, zv10m, rh2m, qsat2m, &
        zq2m, zt2m, weak_inversion, &
+       zq2m_cor,zt2m_cor,zu10m_cor,zv10m_cor, & ! pour corriger d'un bug
+       zrh2m_cor,zqsat2m_cor, &
        zt2m_min_mon, zt2m_max_mon,   &         ! pour calcul_divers.h
        t2m_min_mon, t2m_max_mon,  &            ! pour calcul_divers.h
@@ -593,4 +595,10 @@
                                                         ! gust-front in the grid cell.
     !$OMP THREADPRIVATE(iflag_alp_wk_cond)
+
+    INTEGER,  SAVE               :: iflag_bug_t2m_ipslcm61=0 !
+    !$OMP THREADPRIVATE(iflag_bug_t2m_ipslcm61)
+    INTEGER,  SAVE               :: iflag_bug_t2m_stab_ipslcm61=1 !
+    !$OMP THREADPRIVATE(iflag_bug_t2m_stab_ipslcm61)
+
     REAL t_w(klon,klev),q_w(klon,klev) ! temperature and moisture profiles in the wake region
     REAL t_x(klon,klev),q_x(klon,klev) ! temperature and moisture profiles in the off-wake region
@@ -1295,4 +1303,9 @@
        tau_gl=86400.*tau_gl
        WRITE(lunout,*) 'debut physiq_mod tau_gl=',tau_gl
+
+       iflag_bug_t2m_ipslcm61 = 0
+       CALL getin_p('iflag_bug_t2m_ipslcm61', iflag_bug_t2m_ipslcm61)
+       iflag_bug_t2m_stab_ipslcm61 = 0
+       CALL getin_p('iflag_bug_t2m_stab_ipslcm61', iflag_bug_t2m_stab_ipslcm61)
 
        CALL getin_p('iflag_alp_wk_cond', iflag_alp_wk_cond) 
@@ -2458,4 +2471,19 @@
        ENDIF
 
+!add limitation for t,q at and wind at 10m
+        if ( iflag_bug_t2m_ipslcm61 == 0 ) THEN
+          CALL borne_var_surf( klon,klev,nbsrf,                 &
+            iflag_bug_t2m_stab_ipslcm61,                        &
+            t_seri(:,1),q_seri(:,1),u_seri(:,1),v_seri(:,1),    &
+            ftsol,zxqsurf,pctsrf,paprs,                         &
+            t2m, q2m, u10m, v10m,                               &
+            zt2m_cor, zq2m_cor, zu10m_cor, zv10m_cor,           &
+            zrh2m_cor, zqsat2m_cor)
+        ELSE
+          zt2m_cor(:)=zt2m(:)
+          zq2m_cor(:)=zq2m(:)
+          zu10m_cor(:)=zu10m(:)
+          zv10m_cor(:)=zv10m(:)
+        ENDIF
 
        !---------------------------------------------------------------------
