!
! $Header$
!

      IF (ok_hf) THEN
c$OMP MASTER
c
       zstohf = ecrit_hf
       zstophy = dtime
       zstoday = ecrit_day
       zout = ecrit_hf
c
         idayref = day_ref
         CALL ymds2ju(annee_ref, 1, idayref, 0.0, zjulian)

c
cym         CALL gr_fi_ecrit(1,klon,iim,jjmp1,rlon,zx_lon)
cym         DO i = 1, iim
cym            zx_lon(i,1) = rlon(i+1)
cym            zx_lon(i,jjmp1) = rlon(i+1)
cym         ENDDO

cym         CALL gr_fi_ecrit(1,klon,iim,jjmp1,rlat,zx_lat)

cccIM      CALL histbeg("histhf", iim,zx_lon, jjmp1,zx_lat,
cym         CALL histbeg("histhf", iim,zx_lon(:,1), jjmp1,zx_lat(1,:),
cym     .                 1,iim,1,jjmp1, itau_phy, zjulian, dtime, 
cym     .                 nhori, nid_hf)

         CALL histbeg_phy("histhf", itau_phy, zjulian, dtime, 
     .                 nhori, nid_hf)

         CALL histvert(nid_hf, "presnivs", "Vertical levels", "mb",
     .                 klev, presnivs/100., nvert)
c

      IF(type_run.EQ."CLIM".OR.type_run.EQ."ENSP") THEN
c
      IF(lev_histhf.GE.1) THEN
c
         CALL histdef(nid_hf, "aireTER","Grid area CONT","-",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "once", zstohf,zout)
c
         CALL histdef(nid_hf, "contfracATM","% sfce ter+lic ","-",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "once", zstohf,zout)
c
         CALL histdef(nid_hf, "contfracOR","% sfce terre OR", "-",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "once", zstohf,zout)
c
c Champs 2D:
c
         CALL histdef(nid_hf, "t2m", "Temperature 2m", "K",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32, 
     .                "inst(X)", zstohf,zout)
c
         CALL histdef(nid_hf, "q2m", "Specific humidity", "kg/kg",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zout)
c
         CALL histdef(nid_hf, "psol", "Surface Pressure", "Pa",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32, 
     .                "inst(X)", zstohf,zout)
c
         CALL histdef(nid_hf, "precip", "Precipitation", "kg/(s*m2)",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32, 
     .                "ave(X)", zstophy,zout)
c
cIM ENSEMBLES BEG
         CALL histdef(nid_hf, "tsol", "Surface Temperature", "K",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zout)
c
         CALL histdef(nid_hf, "slp", "Sea Level Pressure", "Pa",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zout)
c
         CALL histdef(nid_hf, "u10m", "Vent zonal 10m", "m/s",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zout)
c
         CALL histdef(nid_hf, "v10m", "Vent meridien 10m", "m/s",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zout)
c
         CALL histdef(nid_hf, "wind10m","10-m wind speed","m/s",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zout)
c
         DO k=1, nlevSTD
c
         bb2=clevSTD(k)
c
c        IF(k.GE.2) THEN
c         aa=clevSTD(k)
c         bb=aa(1:lnblnk1(aa))
c        ENDIF
c
         bb2=clevSTD(k)
         IF(bb2.EQ."500") THEN
         CALL histdef(nid_hf, "phi"//bb2,
     .                "Geopotential "//bb2//"mb", "m",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zout)
         ENDIF !bb2.EQ."500"
         ENDDO !k=1, nlevSTD
c
      ENDIF !lev_histhf.GE.1
c
      IF(lev_histhf.GE.2) THEN
c
         CALL histdef(nid_hf, "cldt", "Total cloudiness", "%",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "ave(X)", zstophy,zout)
c
         CALL histdef(nid_hf, "SWnetOR",
     .                "Sfce net SW radiation OR", "W/m2",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "ave(X)", zstophy,zout)
c
         CALL histdef(nid_hf, "SWdownOR",
     .                "Sfce incident SW radiation OR", "W/m2",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "ave(X)", zstophy,zout)
c
         CALL histdef(nid_hf, "LWdownOR",
     .                "Sfce incident LW radiation OR", "W/m2",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "ave(X)", zstophy,zout)
      ENDIF !lev_histhf.GE.2
c
cIM ENSEMBLES END
c
      IF(lev_histhf.GE.3) THEN
c
         DO k=1, nlevSTD
c
c        bb=clevSTD(k)
c
c        IF(k.GE.2) THEN
c         IF(k.GE.1.AND.k.LE.10) bb2=clevSTD(k)
c         IF(k.GE.11.AND.k.LE.17) bb3=clevSTD(k)
c         aa=clevSTD(k)
c         bb=aa(1:lnblnk1(aa))
c        ENDIF
c
         bb2=clevSTD(k)
         IF(bb2.EQ."1000".OR.bb2.EQ."925".OR.
     $      bb2.EQ."850".OR.bb2.EQ."700".OR.
     $      bb2.EQ."500".OR.bb2.EQ."300".OR.
     $      bb2.EQ."250".OR.bb2.EQ."200".OR.
     $      bb2.EQ."150".OR.bb2.EQ."100".OR.
     $      bb2.EQ."50".OR.bb2.EQ."30".OR.
     $      bb2.EQ."10")THEN
c
          CALL histdef(nid_hf, "t"//bb2,
     .                 "Temperature "//bb2//"mb","K",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zout)
c
          IF(bb2.NE."500") THEN
          CALL histdef(nid_hf, "phi"//bb2, 
     .                "Geopotential "//bb2//"mb", "m",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zout)
          ENDIF
c
          IF(bb2.GE."200") THEN
          CALL histdef(nid_hf, "q"//bb2,
     .                 "Specific humidity "//bb2//"mb","kg/kg",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zout)
          ENDIF
c
          CALL histdef(nid_hf, "u"//bb2,
     .                 "Zonal wind "//bb2//"mb","m/s",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zout)
c
          CALL histdef(nid_hf, "v"//bb2,
     .                 "Meridional wind "//bb2//"mb","m/s",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zout)
c
          CALL histdef(nid_hf, "w"//bb2,
     .                 "Vertical motion "//bb2//"mb","Pa/s",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zstohf)
         ENDIF
c
         ENDDO !nlevSTD
c
      ENDIF !lev_histhf.GE.3
c
      IF(lev_histhf.GE.4) THEN
c
cIM sorties hf 3D
c
#undef histhf3d
#define histhf3d
#ifdef histhf3d
#include "ini_histhf3d.h"
#endif
      ENDIF !lev_histhf.GE.4
c
      ELSE IF(type_run.EQ."AMIP".OR.type_run.EQ."CFMI") THEN
c
      IF(lev_histhf.GE.1) THEN
c
        DO k=1, nlevSTD 
c
c
c        bb=clevSTD(k)
c
c        IF(k.GE.2) THEN
         IF(k.GE.2.AND.k.LE.12) bb2=clevSTD(k)
         IF(k.GE.13.AND.k.LE.17) bb3=clevSTD(k)
c         aa=clevSTD(k)
c         bb=aa(1:lnblnk1(aa))
c        ENDIF
c
         IF(bb2.EQ."850".or.bb2.EQ."200") THEN
c
          CALL histdef(nid_hf, "u"//bb2,
     .                 "Zonal wind "//bb2//"mb","m/s",
     .         iim,jj_nb,nhori, 1,1,1, -99, 32,
     .         "inst(X)", zstohf,zstohf)
c
          CALL histdef(nid_hf, "v"//bb2,
     .                 "Meridional wind "//bb2//"mb","m/s",
     .         iim,jj_nb,nhori, 1,1,1, -99, 32,
     .         "inst(X)", zstohf,zstohf)
c
         ENDIF !bb2.EQ."850".or.bb2.EQ."200"
        ENDDO !k=1, nlevSTD
c
        CALL histdef(nid_hf, "topl", 
     .       "Outgoing longwave radiation", "W/m2",
     .       iim,jj_nb,nhori, 1,1,1, -99, 32,
     .       "ave(X)", zstophy,zout)
c
        CALL histdef(nid_hf, "precip","Total precipitation rate",
     .       "kg/(s*m2)",iim,jj_nb,nhori, 1,1,1, -99, 32, 
     .       "ave(X)", zstophy,zout)
c
        CALL histdef(nid_hf, "slp", 
     .       "Mean sea-level pressure", "Pa",
     .       iim,jj_nb,nhori, 1,1,1, -99, 32,
     .       "inst(X)", zstohf,zout)
c
cIM 280504 BEG
      ENDIF !(lev_histhf.GE.1) THEN
c
      IF(lev_histhf.GE.2) THEN
c
        DO k=1, nlevSTD
c
c        bb=clevSTD(k)
c
c        IF(k.GE.2) THEN
         IF(k.GE.2.AND.k.LE.12) bb2=clevSTD(k)
         IF(k.GE.13.AND.k.LE.17) bb3=clevSTD(k)
c         aa=clevSTD(k)
c         bb=aa(1:lnblnk1(aa))
c        ENDIF
c
         IF(bb2.EQ."850".OR.bb2.EQ."500") THEN
c
          CALL histdef(nid_hf, "t"//bb2,
     .                 "Temperature "//bb2//"mb","K",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zstohf)
c
          CALL histdef(nid_hf, "q"//bb2,
     .                 "Specific humidity "//bb2//"mb","kg/kg",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zstohf)
c
         ENDIF !bb2
c
         IF(bb2.EQ."500") THEN
c
          CALL histdef(nid_hf, "phi"//bb2,
     .                "Geopotential "//bb2//"mb", "m",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zstohf)
c
          CALL histdef(nid_hf, "w"//bb2,
     .                 "Vertical motion "//bb2//"mb","Pa/s",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zstohf)
         ENDIF !bb2.EQ."500"
c
         IF(bb3.EQ."50") THEN
c
          CALL histdef(nid_hf, "t"//bb3,
     .                 "Temperature "//bb3//"mb","K",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "inst(X)", zstohf,zstohf)
c
          CALL histdef(nid_hf, "u"//bb3,
     .                 "Zonal wind "//bb3//"mb","m/s",
     .         iim,jj_nb,nhori, 1,1,1, -99, 32,
     .         "inst(X)", zstohf,zstohf)
c
          CALL histdef(nid_hf, "v"//bb3,
     .                 "Meridional wind "//bb3//"mb","m/s",
     .         iim,jj_nb,nhori, 1,1,1, -99, 32,
     .         "inst(X)", zstohf,zstohf)
c
         ENDIF !bb3
c
        ENDDO !k=1, nlevSTD
c
        CALL histdef(nid_hf, "u10m", "Vent zonal 10m", "m/s",
     .               iim,jj_nb,nhori, 1,1,1, -99, 32,
     .               "inst(X)", zstohf,zout)
c
        CALL histdef(nid_hf, "v10m", "Vent meridien 10m", "m/s",
     .               iim,jj_nb,nhori, 1,1,1, -99, 32,
     .               "inst(X)", zstohf,zout)
c
        CALL histdef(nid_hf, "t2m", "Temperature 2m", "K",
     .               iim,jj_nb,nhori, 1,1,1, -99, 32,
     .               "inst(X)", zstohf,zout)
c
        CALL histdef(nid_hf, "q2m", "Specific humidity", "kg/kg",
     .               iim,jj_nb,nhori, 1,1,1, -99, 32,
     .               "inst(X)", zstohf,zout)
c
        CALL histdef(nid_hf, "psol", "Surface Pressure", "Pa",
     .               iim,jj_nb,nhori, 1,1,1, -99, 32, 
     .               "inst(X)", zstohf,zout)
c
        CALL histdef(nid_hf, "cldt", "Total cloudiness", "%",
     .               iim,jj_nb,nhori, 1,1,1, -99, 32,
     .               "ave(X)", zstohf,zout)
c
        CALL histdef(nid_hf, "taux",
     $         "Zonal wind stress","Pa",
     $         iim,jj_nb,nhori, 1,1,1, -99, 32,
     $         "ave(X)", zstohf,zout)
c
        CALL histdef(nid_hf, "tauy",
     $         "Meridional wind stress ", "Pa",
     $         iim,jj_nb,nhori, 1,1,1, -99, 32,
     $         "ave(X)", zstohf,zout)
c
        CALL histdef(nid_hf, "prw", "Precipitable water", "kg/m2",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
cIM 090904  .                "ave(X)", zstohf,zout)
     .                "ave(X)", zstophy,zout)
c
        CALL histdef(nid_hf, "sens", "Sensible heat flux", "W/m2",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
cIM 090904  .                "ave(X)", zstohf,zout)
     .                "ave(X)", zstophy,zout)
c
        CALL histdef(nid_hf, "flat", "Latent heat flux", "W/m2",
     .                iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "ave(X)", zstohf,zout)
c
c       CALL histdef(nid_hf, "snow_mass", "Snow Mass", "kg/m2",
c    .                iim,jj_nb,nhori, 1,1,1, -99, 32,
c    .                "ave(X)", zstohf,zout)
c
        CALL histdef(nid_hf, "SWdnSFC", "SWdn at surface","W/m2",
     .                iim,jj_nb,nhori, 1,1,1,-99,
     .                32, "ave(X)", zstohf,zout)
c
        CALL histdef(nid_hf, "SWupSFC", "SWup at surface","W/m2",
     .                iim,jj_nb,nhori, 1,1,1,-99,
     .                32, "ave(X)", zstohf,zout)
c
        CALL histdef(nid_hf,"LWdnSFC","Down. IR rad. at surface",
     .                "W/m2", iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "ave(X)", zstohf,zout)
c
        CALL histdef(nid_hf,"LWupSFC","Upwd. IR rad. at surface",
     .                "W/m2", iim,jj_nb,nhori, 1,1,1, -99, 32,
     .                "ave(X)", zstohf,zout)
c
        CALL histdef(nid_hf, "SWdnTOA", "SWdn at TOA","W/m2",
     .                iim,jj_nb,nhori, 1,1,1,-99,
     .                32, "ave(X)", zstohf,zout)
c
        CALL histdef(nid_hf, "SWupTOA", "SWup at TOA","W/m2",
     .                iim,jj_nb,nhori, 1,1,1,-99,
     .                32, "ave(X)", zstohf,zout)
c
        DO k=1, nbteta
         CALL histdef(nid_hf, "PV"//ctetaSTD(k),
     $               "Vorticite potentielle "//ctetaSTD(k)//"K",
     $               "1/(Pa*s)",iim,jj_nb,nhori, 1,1,1, -99,
     $                 32, "inst(X)", zstohf,zout) 
        ENDDO !k=1, nbteta
c
c planetary boundary layer height
c
c       ENDIF !1.EQ.0
c
      ENDIF !(lev_histhf.GE.2) THEN
cIM 280504 END
c
      ENDIF !type_run
c
      CALL histend(nid_hf)
c
c$OMP END MASTER
      ENDIF ! ok_hf
