Index: LMDZ5/trunk/libf/phylmd/phys_output_write_mod.F90
===================================================================
--- LMDZ5/trunk/libf/phylmd/phys_output_write_mod.F90	(revision 1816)
+++ LMDZ5/trunk/libf/phylmd/phys_output_write_mod.F90	(revision 1821)
@@ -67,5 +67,5 @@
     INTEGER, PARAMETER :: jjmp1=jjm+1-1/jjm
     INTEGER :: itau_w
-    INTEGER :: i, iff, iq, nsrf, k, ll, naero
+    INTEGER :: i, iinit, iinitend=1, iff, iq, nsrf, k, ll, naero
     REAL, DIMENSION (klon) :: zx_tmp_fi2d
     REAL, DIMENSION (klon,klev) :: zx_tmp_fi3d, zpt_conv
@@ -76,11 +76,16 @@
     REAL, PARAMETER :: dobson_u = 2.1415e-05 ! Dobson unit, in kg m-2
 
-    IF(vars_defined) THEN
-! On calcul le nouveau tau:
+     ! On calcul le nouveau tau:
      itau_w = itau_phy + itap + start_time * day_step / iphysiq
-! On le donne à iophy pour que les histwrite y aient accès:
+     ! On le donne à iophy pour que les histwrite y aient accès:
      CALL set_itau_iophy(itau_w)
+    IF(.NOT.vars_defined) THEN
+        iinitend = 2
+    ELSE
+        iinitend = 1
     ENDIF
-
+    
+! ug la boucle qui suit ne sert qu'une fois, pour l'initialisation, sinon il n'y a toujours qu'un seul passage:
+DO iinit=1, iinitend
 ! On procède à l'écriture ou à la définition des nombreuses variables:
 !!! Champs 1D !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
@@ -88,5 +93,5 @@
       CALL histwrite_phy(o_aire, airephy)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       DO i=1, klon
        zx_tmp_fi2d(i)=pctsrf(i,is_ter)+pctsrf(i,is_lic)
@@ -105,5 +110,5 @@
       CALL histwrite_phy(o_t2m_max, zt2m)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       DO i=1, klon
        zx_tmp_fi2d(i)=SQRT(zu10m(i)*zu10m(i)+zv10m(i)*zv10m(i))
@@ -112,5 +117,5 @@
       CALL histwrite_phy(o_wind10m, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       DO i=1, klon
        zx_tmp_fi2d(i)=SQRT(zu10m(i)*zu10m(i)+zv10m(i)*zv10m(i))
@@ -119,5 +124,5 @@
       CALL histwrite_phy(o_wind10max, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       DO i = 1, klon
          zx_tmp_fi2d(i) = pctsrf(i,is_sic)
@@ -130,5 +135,5 @@
       CALL histwrite_phy(o_v10m, zv10m)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       DO i = 1, klon
          zx_tmp_fi2d(i) = paprs(i,1)
@@ -143,5 +148,5 @@
 ENDIF
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
        DO i = 1, klon
          zx_tmp_fi2d(i) = rain_fall(i) + snow_fall(i)
@@ -152,5 +157,5 @@
       CALL histwrite_phy(o_ndayrain, nday_rain)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
        DO i = 1, klon
          zx_tmp_fi2d(i) = rain_lsc(i) + snow_lsc(i)
@@ -159,5 +164,5 @@
       CALL histwrite_phy(o_plul, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       DO i = 1, klon
          zx_tmp_fi2d(i) = rain_con(i) + snow_con(i)
@@ -174,25 +179,25 @@
       CALL histwrite_phy(o_topl0, toplw0)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi2d(1 : klon) = swup ( 1 : klon, klevp1 )
 ENDIF
       CALL histwrite_phy(o_SWupTOA, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi2d(1 : klon) = swup0 ( 1 : klon, klevp1 )
 ENDIF
       CALL histwrite_phy(o_SWupTOAclr, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi2d(1 : klon) = swdn ( 1 : klon, klevp1 )
 ENDIF
       CALL histwrite_phy(o_SWdnTOA, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi2d(1 : klon) = swdn0 ( 1 : klon, klevp1 )
 ENDIF
       CALL histwrite_phy(o_SWdnTOAclr, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi2d(:) = topsw(:)-toplw(:)
 ENDIF
@@ -212,25 +217,25 @@
       CALL histwrite_phy(o_soll0, sollw0)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi2d(1 : klon) = swup ( 1 : klon, 1 )
 ENDIF
       CALL histwrite_phy(o_SWupSFC, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi2d(1 : klon) = swup0 ( 1 : klon, 1 )
 ENDIF
       CALL histwrite_phy(o_SWupSFCclr, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi2d(1 : klon) = swdn ( 1 : klon, 1 )
 ENDIF
       CALL histwrite_phy(o_SWdnSFC, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi2d(1 : klon) = swdn0 ( 1 : klon, 1 )
 ENDIF
       CALL histwrite_phy(o_SWdnSFCclr, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi2d(1:klon)=sollwdown(1:klon)-sollw(1:klon)
 ENDIF
@@ -238,5 +243,5 @@
       CALL histwrite_phy(o_LWdnSFC, sollwdown)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
        sollwdownclr(1:klon) = -1.*lwdn0(1:klon,1)
       zx_tmp_fi2d(1:klon)=sollwdownclr(1:klon)-sollw0(1:klon)
@@ -252,5 +257,5 @@
       CALL histwrite_phy(o_bils_enthalp, bils_enthalp)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi2d(1:klon)=-1*sens(1:klon)
 ENDIF
@@ -260,5 +265,5 @@
       CALL histwrite_phy(o_fqcalving, zxfqcalving)
       CALL histwrite_phy(o_fqfonte, zxfqfonte)
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi2d=0.
       DO nsrf=1,nbsrf
@@ -268,5 +273,5 @@
       CALL histwrite_phy(o_taux, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi2d=0.
       DO nsrf=1,nbsrf
@@ -278,37 +283,37 @@
 
          DO nsrf = 1, nbsrf
-IF (itap > 0)             zx_tmp_fi2d(1 : klon) = pctsrf( 1 : klon, nsrf)*100.
+IF (vars_defined)             zx_tmp_fi2d(1 : klon) = pctsrf( 1 : klon, nsrf)*100.
       CALL histwrite_phy(o_pourc_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)           zx_tmp_fi2d(1 : klon) = pctsrf( 1 : klon, nsrf)
+IF (vars_defined)           zx_tmp_fi2d(1 : klon) = pctsrf( 1 : klon, nsrf)
       CALL histwrite_phy(o_fract_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)         zx_tmp_fi2d(1 : klon) = fluxu( 1 : klon, 1, nsrf)
+IF (vars_defined)         zx_tmp_fi2d(1 : klon) = fluxu( 1 : klon, 1, nsrf)
       CALL histwrite_phy(o_taux_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)         zx_tmp_fi2d(1 : klon) = fluxv( 1 : klon, 1, nsrf)
+IF (vars_defined)         zx_tmp_fi2d(1 : klon) = fluxv( 1 : klon, 1, nsrf)
       CALL histwrite_phy(o_tauy_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)         zx_tmp_fi2d(1 : klon) = ftsol( 1 : klon, nsrf)
+IF (vars_defined)         zx_tmp_fi2d(1 : klon) = ftsol( 1 : klon, nsrf)
       CALL histwrite_phy(o_tsol_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)         zx_tmp_fi2d(1 : klon) = evap_pot( 1 : klon, nsrf)
+IF (vars_defined)         zx_tmp_fi2d(1 : klon) = evap_pot( 1 : klon, nsrf)
       CALL histwrite_phy(o_evappot_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)       zx_tmp_fi2d(1 : klon) = ustar(1 : klon, nsrf)
+IF (vars_defined)       zx_tmp_fi2d(1 : klon) = ustar(1 : klon, nsrf)
       CALL histwrite_phy(o_ustar_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)       zx_tmp_fi2d(1 : klon) = u10m(1 : klon, nsrf)
+IF (vars_defined)       zx_tmp_fi2d(1 : klon) = u10m(1 : klon, nsrf)
       CALL histwrite_phy(o_u10m_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)       zx_tmp_fi2d(1 : klon) = v10m(1 : klon, nsrf)
+IF (vars_defined)       zx_tmp_fi2d(1 : klon) = v10m(1 : klon, nsrf)
       CALL histwrite_phy(o_v10m_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)       zx_tmp_fi2d(1 : klon) = t2m(1 : klon, nsrf)
+IF (vars_defined)       zx_tmp_fi2d(1 : klon) = t2m(1 : klon, nsrf)
       CALL histwrite_phy(o_t2m_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)       zx_tmp_fi2d(1 : klon) = fevap(1 : klon, nsrf)
+IF (vars_defined)       zx_tmp_fi2d(1 : klon) = fevap(1 : klon, nsrf)
       CALL histwrite_phy(o_evap_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)        zx_tmp_fi2d(1 : klon) = fluxt( 1 : klon, 1, nsrf)
+IF (vars_defined)        zx_tmp_fi2d(1 : klon) = fluxt( 1 : klon, 1, nsrf)
       CALL histwrite_phy(o_sens_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)         zx_tmp_fi2d(1 : klon) = fluxlat( 1 : klon, nsrf)
+IF (vars_defined)         zx_tmp_fi2d(1 : klon) = fluxlat( 1 : klon, nsrf)
       CALL histwrite_phy(o_lat_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)         zx_tmp_fi2d(1 : klon) = fsollw( 1 : klon, nsrf)
+IF (vars_defined)         zx_tmp_fi2d(1 : klon) = fsollw( 1 : klon, nsrf)
       CALL histwrite_phy(o_flw_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)         zx_tmp_fi2d(1 : klon) = fsolsw( 1 : klon, nsrf)
+IF (vars_defined)         zx_tmp_fi2d(1 : klon) = fsolsw( 1 : klon, nsrf)
       CALL histwrite_phy(o_fsw_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)         zx_tmp_fi2d(1 : klon) = wfbils( 1 : klon, nsrf)
+IF (vars_defined)         zx_tmp_fi2d(1 : klon) = wfbils( 1 : klon, nsrf)
       CALL histwrite_phy(o_wbils_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0)         zx_tmp_fi2d(1 : klon) = wfbilo( 1 : klon, nsrf)
+IF (vars_defined)         zx_tmp_fi2d(1 : klon) = wfbilo( 1 : klon, nsrf)
       CALL histwrite_phy(o_wbilo_srf(nsrf), zx_tmp_fi2d)
 
@@ -330,7 +335,7 @@
       CALL histwrite_phy(o_cldt, cldt)
       CALL histwrite_phy(o_cldq, cldq)
-IF (itap > 0)       zx_tmp_fi2d(1:klon) = flwp(1:klon)
+IF (vars_defined)       zx_tmp_fi2d(1:klon) = flwp(1:klon)
       CALL histwrite_phy(o_lwp, zx_tmp_fi2d)
-IF (itap > 0)       zx_tmp_fi2d(1:klon) = fiwp(1:klon)
+IF (vars_defined)       zx_tmp_fi2d(1:klon) = fiwp(1:klon)
       CALL histwrite_phy(o_iwp, zx_tmp_fi2d)
       CALL histwrite_phy(o_ue, ue)
@@ -355,7 +360,7 @@
       CALL histwrite_phy(o_dnwd, dnwd)
       CALL histwrite_phy(o_dnwd0, dnwd0)
-IF (itap > 0)         zx_tmp_fi2d=float(itau_con)/float(itap)
+IF (vars_defined)         zx_tmp_fi2d=float(itau_con)/float(itap)
       CALL histwrite_phy(o_ftime_con, zx_tmp_fi2d)
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       IF(iflag_thermals>=1)THEN
          zx_tmp_fi3d=dnwd+dnwd0+upwd+fm_therm(:,1:klev)
@@ -425,5 +430,5 @@
 
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       DO i=1, klon
        IF (pctsrf(i,is_oce).GT.epsfra.OR. &
@@ -460,8 +465,8 @@
       CALL histwrite_phy(o_wake_deltaq, wake_deltaq)
       CALL histwrite_phy(o_wake_omg, wake_omg)
-IF (itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_t_wake(1:klon,1:klev) &
+IF (vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_t_wake(1:klon,1:klev) &
                                               /pdtphys
       CALL histwrite_phy(o_dtwak, zx_tmp_fi3d)
-IF (itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_q_wake(1:klon,1:klev)/pdtphys
+IF (vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_q_wake(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dqwak, zx_tmp_fi3d)
       ENDIF ! iflag_wake>=1
@@ -506,5 +511,5 @@
       CALL histwrite_phy(o_re, re)
       CALL histwrite_phy(o_fl, fl)
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       DO i=1, klon
        zx_tmp_fi2d(i)=MIN(100.,rh2m(i)*100.)
@@ -513,5 +518,5 @@
       CALL histwrite_phy(o_rh2m, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       DO i=1, klon
        zx_tmp_fi2d(i)=MIN(100.,rh2m(i)*100.)
@@ -520,5 +525,5 @@
       CALL histwrite_phy(o_rh2m_min, zx_tmp_fi2d)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       DO i=1, klon
        zx_tmp_fi2d(i)=MIN(100.,rh2m(i)*100.)
@@ -530,7 +535,7 @@
       CALL histwrite_phy(o_tpot, tpot)
       CALL histwrite_phy(o_tpote, tpote)
-IF (itap > 0) zx_tmp_fi2d(1 : klon) = fsolsw( 1 : klon, is_ter)
+IF (vars_defined) zx_tmp_fi2d(1 : klon) = fsolsw( 1 : klon, is_ter)
       CALL histwrite_phy(o_SWnetOR,  zx_tmp_fi2d)
-IF (itap > 0) zx_tmp_fi2d(1:klon) = solsw(1:klon)/(1.-albsol1(1:klon))
+IF (vars_defined) zx_tmp_fi2d(1:klon) = solsw(1:klon)/(1.-albsol1(1:klon))
       CALL histwrite_phy(o_SWdownOR,  zx_tmp_fi2d)
       CALL histwrite_phy(o_LWdownOR, sollwdown)
@@ -632,5 +637,5 @@
       CALL histwrite_phy(o_pres, pplay)
       CALL histwrite_phy(o_paprs, paprs(:,1:klev))
-IF (itap > 0) THEN
+IF (vars_defined) THEN
          DO i=1, klon
           zx_tmp_fi3d1(i,1)= pphis(i)/RG
@@ -649,5 +654,5 @@
 !020611    $o_zfull%name,itau_w,zx_tmp_fi3d)
 
-IF (itap > 0)  THEN
+IF (vars_defined)  THEN
          DO i=1, klon
           zx_tmp_fi3d(i,1)= pphis(i)/RG - ( &
@@ -679,9 +684,9 @@
       CALL histwrite_phy(o_dqphy,  d_qx(:,:,ivap))
         DO nsrf=1, nbsrf
-IF (itap > 0) zx_tmp_fi2d(1 : klon) = falb1( 1 : klon, nsrf)
+IF (vars_defined) zx_tmp_fi2d(1 : klon) = falb1( 1 : klon, nsrf)
       CALL histwrite_phy(o_albe_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0) zx_tmp_fi2d(1 : klon) = frugs( 1 : klon, nsrf)
+IF (vars_defined) zx_tmp_fi2d(1 : klon) = frugs( 1 : klon, nsrf)
       CALL histwrite_phy(o_rugs_srf(nsrf), zx_tmp_fi2d)
-IF (itap > 0) zx_tmp_fi2d(1 : klon) = agesno( 1 : klon, nsrf)
+IF (vars_defined) zx_tmp_fi2d(1 : klon) = agesno( 1 : klon, nsrf)
       CALL histwrite_phy(o_ages_srf(nsrf), zx_tmp_fi2d)
         ENDDO !nsrf=1, nbsrf
@@ -691,5 +696,5 @@
       if (iflag_pbl>1) then
       zx_tmp_fi3d=0.
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       do nsrf=1,nbsrf
          do k=1,klev
@@ -714,10 +719,10 @@
       CALL histwrite_phy(o_dvdyn, d_v_dyn)
 
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi3d(1:klon,1:klev)=d_t_con(1:klon,1:klev)/pdtphys
 ENDIF
       CALL histwrite_phy(o_dtcon, zx_tmp_fi3d)
       if(iflag_thermals.eq.1)then
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi3d(1:klon,1:klev)=d_t_con(1:klon,1:klev)/pdtphys + &
                                  d_t_ajsb(1:klon,1:klev)/pdtphys
@@ -725,5 +730,5 @@
       CALL histwrite_phy(o_tntc, zx_tmp_fi3d)
       else if(iflag_thermals.gt.1.and.iflag_wake.EQ.1)then
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi3d(1:klon,1:klev)=d_t_con(1:klon,1:klev)/pdtphys + &
                                  d_t_ajs(1:klon,1:klev)/pdtphys + &
@@ -732,16 +737,16 @@
       CALL histwrite_phy(o_tntc, zx_tmp_fi3d)
       endif
-IF (itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_u_con(1:klon,1:klev)/pdtphys
+IF (vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_u_con(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_ducon, zx_tmp_fi3d)
-IF (itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_v_con(1:klon,1:klev)/pdtphys
+IF (vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_v_con(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dvcon, zx_tmp_fi3d)
-IF (itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_q_con(1:klon,1:klev)/pdtphys
+IF (vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_q_con(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dqcon, zx_tmp_fi3d)
 
       IF(iflag_thermals.EQ.1) THEN
-IF (itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_q_con(1:klon,1:klev)/pdtphys
+IF (vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_q_con(1:klon,1:klev)/pdtphys
         CALL histwrite_phy(o_tnhusc, zx_tmp_fi3d)
       ELSE IF(iflag_thermals.GT.1.AND.iflag_wake.EQ.1) THEN
-IF (itap > 0) THEN
+IF (vars_defined) THEN
          zx_tmp_fi3d(1:klon,1:klev)=d_q_con(1:klon,1:klev)/pdtphys + &
                                      d_q_ajs(1:klon,1:klev)/pdtphys + &
@@ -751,27 +756,27 @@
       ENDIF
 
-IF (itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_t_lsc(1:klon,1:klev)/pdtphys
+IF (vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_t_lsc(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dtlsc, zx_tmp_fi3d)
-IF (itap > 0) zx_tmp_fi3d(1:klon, 1:klev)=(d_t_lsc(1:klon,1:klev)+ &
+IF (vars_defined) zx_tmp_fi3d(1:klon, 1:klev)=(d_t_lsc(1:klon,1:klev)+ &
                                  d_t_eva(1:klon,1:klev))/pdtphys
       CALL histwrite_phy(o_dtlschr, zx_tmp_fi3d)
-IF (itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_q_lsc(1:klon,1:klev)/pdtphys
+IF (vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_q_lsc(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dqlsc, zx_tmp_fi3d)
-IF (itap > 0) zx_tmp_fi3d(1:klon,1:klev)=beta_prec(1:klon,1:klev)
+IF (vars_defined) zx_tmp_fi3d(1:klon,1:klev)=beta_prec(1:klon,1:klev)
       CALL histwrite_phy(o_beta_prec, zx_tmp_fi3d)
 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 ! Sorties specifiques a la separation thermiques/non thermiques
        if (iflag_thermals>=1) then
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_t_lscth(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_t_lscth(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dtlscth, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_t_lscst(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_t_lscst(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dtlscst, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_q_lscth(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_q_lscth(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dqlscth, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_q_lscst(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_q_lscst(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dqlscst, zx_tmp_fi3d)
       CALL histwrite_phy(o_plulth, plul_th)
       CALL histwrite_phy(o_plulst, plul_st)
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       do k=1,klev
       do i=1,klon
@@ -785,5 +790,5 @@
 ENDIF
       CALL histwrite_phy(o_ptconvth, zx_tmp_fi3d)
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       do i=1,klon
            zx_tmp_fi2d(1:klon)=lmax_th(:)
@@ -793,13 +798,13 @@
       endif ! iflag_thermals>=1
 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-IF (itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_t_vdf(1:klon,1:klev)/pdtphys
+IF (vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_t_vdf(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dtvdf, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_t_diss(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_t_diss(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dtdis, zx_tmp_fi3d)
-IF (itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_q_vdf(1:klon,1:klev)/pdtphys
+IF (vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_q_vdf(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dqvdf, zx_tmp_fi3d)
-IF (itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_t_eva(1:klon,1:klev)/pdtphys
+IF (vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_t_eva(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dteva, zx_tmp_fi3d)
-IF (itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_q_eva(1:klon,1:klev)/pdtphys
+IF (vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_q_eva(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dqeva, zx_tmp_fi3d)
       zpt_conv = 0.
@@ -807,5 +812,5 @@
       CALL histwrite_phy(o_ptconv, zpt_conv)
       CALL histwrite_phy(o_ratqs, ratqs)
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi3d(1:klon,1:klev)=d_t_ajs(1:klon,1:klev)/pdtphys - &
                                  d_t_ajsb(1:klon,1:klev)/pdtphys
@@ -824,5 +829,5 @@
       CALL histwrite_phy(o_f0_th, f0)
       CALL histwrite_phy(o_zmax_th, zmax_th)
-IF (itap > 0) THEN
+IF (vars_defined) THEN
       zx_tmp_fi3d(1:klon,1:klev)=d_q_ajs(1:klon,1:klev)/pdtphys - &
                                  d_q_ajsb(1:klon,1:klev)/pdtphys
@@ -830,46 +835,46 @@
       CALL histwrite_phy(o_dqthe, zx_tmp_fi3d)
       ENDIF !iflag_thermals
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_t_ajsb(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_t_ajsb(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dtajs, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_q_ajsb(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_q_ajsb(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dqajs, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=heat(1:klon,1:klev)/RDAY
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=heat(1:klon,1:klev)/RDAY
       CALL histwrite_phy(o_dtswr, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=heat0(1:klon,1:klev)/RDAY
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=heat0(1:klon,1:klev)/RDAY
       CALL histwrite_phy(o_dtsw0, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=-1.*cool(1:klon,1:klev)/RDAY
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=-1.*cool(1:klon,1:klev)/RDAY
       CALL histwrite_phy(o_dtlwr, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=-1.*cool0(1:klon,1:klev)/RDAY
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=-1.*cool0(1:klon,1:klev)/RDAY
       CALL histwrite_phy(o_dtlw0, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_t_ec(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_t_ec(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dtec, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_u_vdf(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_u_vdf(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_duvdf, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_v_vdf(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_v_vdf(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dvvdf, zx_tmp_fi3d)
        IF (ok_orodr) THEN
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_u_oro(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_u_oro(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_duoro, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_v_oro(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_v_oro(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dvoro, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_t_oro(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_t_oro(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dtoro, zx_tmp_fi3d)
        ENDIF
         IF (ok_orolf) THEN
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_u_lif(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_u_lif(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dulif, zx_tmp_fi3d)
        ENDIF
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_v_lif(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_v_lif(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dvlif, zx_tmp_fi3d)
 
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_t_lif(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_t_lif(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dtlif, zx_tmp_fi3d)
 
        IF (ok_hines) THEN
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_u_hin(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_u_hin(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_duhin, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_v_hin(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_v_hin(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dvhin, zx_tmp_fi3d)
-IF(itap > 0) zx_tmp_fi3d(1:klon,1:klev)=d_t_hin(1:klon,1:klev)/pdtphys
+IF(vars_defined) zx_tmp_fi3d(1:klon,1:klev)=d_t_hin(1:klon,1:klev)/pdtphys
       CALL histwrite_phy(o_dthin, zx_tmp_fi3d)
         ENDIF
@@ -882,15 +887,15 @@
       CALL histwrite_phy(o_rlucs, lwup0)
       CALL histwrite_phy(o_rldcs, lwdn0)
-IF(itap > 0) THEN
+IF(vars_defined) THEN
       zx_tmp_fi3d(1:klon,1:klev)=d_t(1:klon,1:klev)+ &
       d_t_dyn(1:klon,1:klev)
 ENDIF
       CALL histwrite_phy(o_tnt, zx_tmp_fi3d)
-IF(itap > 0) THEN
+IF(vars_defined) THEN
       zx_tmp_fi3d(1:klon,1:klev)=heat(1:klon,1:klev)/RDAY - &
       cool(1:klon,1:klev)/RDAY
 ENDIF
       CALL histwrite_phy(o_tntr, zx_tmp_fi3d)
-IF(itap > 0) THEN
+IF(vars_defined) THEN
       zx_tmp_fi3d(1:klon,1:klev)= (d_t_lsc(1:klon,1:klev)+ &
                                    d_t_eva(1:klon,1:klev)+ &
@@ -898,10 +903,10 @@
 ENDIF
       CALL histwrite_phy(o_tntscpbl, zx_tmp_fi3d)
-IF(itap > 0) THEN
+IF(vars_defined) THEN
       zx_tmp_fi3d(1:klon,1:klev)=d_qx(1:klon,1:klev,ivap)+ &
       d_q_dyn(1:klon,1:klev)
 ENDIF
       CALL histwrite_phy(o_tnhus, zx_tmp_fi3d)
-IF(itap > 0) THEN
+IF(vars_defined) THEN
       zx_tmp_fi3d(1:klon,1:klev)=d_q_lsc(1:klon,1:klev)/pdtphys+ &
                                  d_q_eva(1:klon,1:klev)/pdtphys
@@ -909,5 +914,5 @@
       CALL histwrite_phy(o_tnhusscpbl, zx_tmp_fi3d)
       CALL histwrite_phy(o_evu, coefm(:,:,is_ave))
-IF(itap > 0) THEN
+IF(vars_defined) THEN
       zx_tmp_fi3d(1:klon,1:klev)=q_seri(1:klon,1:klev)+ &
                                  ql_seri(1:klon,1:klev) 
@@ -915,10 +920,10 @@
       CALL histwrite_phy(o_h2o, zx_tmp_fi3d)
        if (iflag_con >= 3) then
-IF(itap > 0) THEN
+IF(vars_defined) THEN
              zx_tmp_fi3d(1:klon,1:klev)=-1 * (dnwd(1:klon,1:klev)+ &
                   dnwd0(1:klon,1:klev)) 
 ENDIF
       CALL histwrite_phy(o_mcd, zx_tmp_fi3d)
-IF(itap > 0) THEN
+IF(vars_defined) THEN
              zx_tmp_fi3d(1:klon,1:klev)=upwd(1:klon,1:klev) + &
                   dnwd(1:klon,1:klev)+ dnwd0(1:klon,1:klev) 
@@ -934,11 +939,11 @@
        RN2O_per.NE.RN2O_act.OR.RCFC11_per.NE.RCFC11_act.OR. &
        RCFC12_per.NE.RCFC12_act) THEN
-IF(itap > 0) zx_tmp_fi2d(1 : klon) = swupp ( 1 : klon, klevp1 )
+IF(vars_defined) zx_tmp_fi2d(1 : klon) = swupp ( 1 : klon, klevp1 )
       CALL histwrite_phy(o_rsut4co2, zx_tmp_fi2d)
-IF(itap > 0) zx_tmp_fi2d(1 : klon) = lwupp ( 1 : klon, klevp1 )
+IF(vars_defined) zx_tmp_fi2d(1 : klon) = lwupp ( 1 : klon, klevp1 )
       CALL histwrite_phy(o_rlut4co2, zx_tmp_fi2d)
-IF(itap > 0) zx_tmp_fi2d(1 : klon) = swup0p ( 1 : klon, klevp1 )
+IF(vars_defined) zx_tmp_fi2d(1 : klon) = swup0p ( 1 : klon, klevp1 )
       CALL histwrite_phy(o_rsutcs4co2, zx_tmp_fi2d)
-IF(itap > 0) zx_tmp_fi2d(1 : klon) = lwup0p ( 1 : klon, klevp1 )
+IF(vars_defined) zx_tmp_fi2d(1 : klon) = lwup0p ( 1 : klon, klevp1 )
       CALL histwrite_phy(o_rlutcs4co2, zx_tmp_fi2d)
       CALL histwrite_phy(o_rsu4co2, swupp)
@@ -968,5 +973,5 @@
              CALL histwrite_phy(o_dtr_uscav(iq-2),d_tr_uscav(:,:,iq-2))
          zx_tmp_fi2d=0.
-IF(itap > 0) THEN
+IF(vars_defined) THEN
          DO k=1,klev
             zx_tmp_fi2d(:)=zx_tmp_fi2d(:)+zmasse(:,k)*qx(:,k,iq)
@@ -978,16 +983,32 @@
 
 
+        IF(.NOT.vars_defined) THEN
+!$OMP MASTER
+            DO iff=1,nfiles
+                IF (clef_files(iff)) THEN
+                  CALL histend(nid_files(iff))
+                  ndex2d = 0
+                  ndex3d = 0
+
+                ENDIF ! clef_files
+            ENDDO !  iff
+!$OMP END MASTER
+!$OMP BARRIER
+            vars_defined = .TRUE.
+
+
+        END IF
+
+    END DO
+
     IF(vars_defined) THEN
 ! On synchronise les fichiers pour IOIPSL
+!$OMP MASTER
       DO iff=1,nfiles
           IF (ok_sync .AND. clef_files(iff)) THEN
-!$OMP MASTER
               CALL histsync(nid_files(iff))
-!$OMP END MASTER
           ENDIF
       END DO
-
-    ELSE
-    vars_defined = .TRUE.
+!$OMP END MASTER
     ENDIF
 
