!
! $Header$
!
      if (ok_hf) then

c
      ndex2d = 0
      ndex3d = 0
c
      itau_w = itau_phy + itap
c
      IF(type_run.EQ."CLIM".OR.type_run.EQ."ENSP") THEN
c
      IF(lev_histhf.GE.1) THEN
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, paire_ter, zx_tmp_2d)
      CALL histwrite(nid_hf,"aireTER",itau_w,
     $      zx_tmp_2d,iim*jjmp1,ndex2d)
c
      DO i=1, klon
       zx_tmp_fi2d(i)=pctsrf(i,is_ter)+pctsrf(i,is_lic)
      ENDDO
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d, zx_tmp_2d)
      CALL histwrite(nid_hf,"contfracATM",itau_w,
     $      zx_tmp_2d,iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1,klon,iim,jjmp1,pctsrf_new(:,is_ter),zx_tmp_2d)
      CALL histwrite(nid_hf,"contfracOR",itau_w,
     $      zx_tmp_2d,iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zt2m,zx_tmp_2d)
      CALL histwrite(nid_hf,"t2m",itau_w,zx_tmp_2d,iim*jjmp1,
     .               ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zq2m,zx_tmp_2d)
      CALL histwrite(nid_hf,"q2m",itau_w,zx_tmp_2d,iim*jjmp1,
     .               ndex2d)
c
      DO i = 1, klon
         zx_tmp_fi2d(i) = paprs(i,1)
      ENDDO
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d,zx_tmp_2d)
      CALL histwrite(nid_hf,"psol",itau_w,zx_tmp_2d,iim*jjmp1,
     .               ndex2d)
c
      DO i = 1, klon
         zx_tmp_fi2d(i) = rain_fall(i) + snow_fall(i)
      ENDDO
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d,zx_tmp_2d)
      CALL histwrite(nid_hf,"precip",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
c ENSEMBLES BEG
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zxtsol,zx_tmp_2d)
      CALL histwrite(nid_hf,"tsol",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, slp,zx_tmp_2d)
      CALL histwrite(nid_hf,"slp",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zu10m,zx_tmp_2d)
      CALL histwrite(nid_hf,"u10m",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zv10m,zx_tmp_2d)
      CALL histwrite(nid_hf,"v10m",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      DO i=1, klon
       zx_tmp_fi2d(i)=SQRT(zu10m(i)*zu10m(i)+zv10m(i)*zv10m(i))
      ENDDO
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d,zx_tmp_2d)
      CALL histwrite(nid_hf,"wind10m",itau_w,zx_tmp_2d,
     $     iim*jjmp1,ndex2d)
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."500") THEN
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, philevSTD(:,k),zx_tmp_2d)
      CALL histwrite(nid_hf,"phi"//bb2,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
      ENDIF !bb2.EQ."500"
      ENDDO
c
      ENDIF !lev_histhf.GE.1
c
      IF(lev_histhf.GE.2) THEN
c
cIM 140904 BEG
      DO i = 1, klon
         zx_tmp_fi2d(i) = cldt(i)*100.
      ENDDO
cIM 140904 END
cIM 140904   CALL gr_fi_ecrit(1, klon,iim,jjmp1, cldt,zx_tmp_2d)
      CALL gr_fi_ecrit(1, klon,iim,jjmp1,zx_tmp_fi2d,zx_tmp_2d)
      CALL histwrite(nid_hf,"cldt",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      zx_tmp_fi2d(1 : klon) = fsolsw( 1 : klon, is_ter)
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d , zx_tmp_2d)
      CALL histwrite(nid_hf,"SWnetOR",itau_w,
     $     zx_tmp_2d,iim*jjmp1,ndex2d)
c
      zx_tmp_fi2d(1:klon) = solsw(1:klon)/(1.-albsol(1:klon))
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d, zx_tmp_2d)
      CALL histwrite(nid_hf,"SWdownOR",itau_w,
     $     zx_tmp_2d,iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, sollwdown,zx_tmp_2d)
      CALL histwrite(nid_hf,"LWdownOR",itau_w,zx_tmp_2d,iim*jjmp1,
     $     ndex2d) 
c
c
      ENDIF !lev_histhf.GE.2
c
      IF(lev_histhf.GE.3) 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."700".OR.
     $   bb2.EQ."500".OR.bb2.EQ."200") THEN
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, tlevSTD(:,k),zx_tmp_2d)
      CALL histwrite(nid_hf,"t"//bb2,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
c
      IF(bb2.NE."500") THEN !bb2.NE."500"
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1,philevSTD(:,k),zx_tmp_2d)
      CALL histwrite(nid_hf,"phi"//bb2,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
      ENDIF !bb2.NE."500"
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, qlevSTD(:,k),zx_tmp_2d)
      CALL histwrite(nid_hf,"q"//bb2,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, ulevSTD(:,k),zx_tmp_2d)
      CALL histwrite(nid_hf,"u"//bb2,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, vlevSTD(:,k),zx_tmp_2d)
      CALL histwrite(nid_hf,"v"//bb2,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
c
      ENDIF !bb2.EQ."850".OR.bb2.EQ."700"
      ENDDO !nlevENS
c
      IF(1.EQ.0) THEN
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, cdragm,zx_tmp_2d)
      CALL histwrite(nid_hf,"cdrm",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, cdragh,zx_tmp_2d)
      CALL histwrite(nid_hf,"cdrh",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
      ENDIF !(1.EQ.0) THEN
c
      ENDIF !lev_histhf.GE.3
c
      IF(lev_histhf.GE.4) THEN 
c
#undef histhf3d
#define histhf3d
#ifdef histhf3d
#include "write_histhf3d.h"
#endif
c
      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        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 gr_fi_ecrit(1, klon,iim,jjmp1, ulevSTD(:,k),zx_tmp_2d)
        CALL histwrite(nid_hf,"u"//bb2,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
c
        CALL gr_fi_ecrit(1, klon,iim,jjmp1, vlevSTD(:,k),zx_tmp_2d)
        CALL histwrite(nid_hf,"v"//bb2,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
c
        ENDIF !bb2.EQ."850".or.bb2.EQ."200"
c
      ENDDO !nlevSTD
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, toplw,zx_tmp_2d)
      CALL histwrite(nid_hf,"topl",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      DO i = 1, klon
         zx_tmp_fi2d(i) = rain_fall(i) + snow_fall(i)
      ENDDO
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d,zx_tmp_2d)
      CALL histwrite(nid_hf,"precip",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, slp,zx_tmp_2d)
      CALL histwrite(nid_hf,"slp",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
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 gr_fi_ecrit(1, klon,iim,jjmp1, tlevSTD(:,k),zx_tmp_2d)
         CALL histwrite(nid_hf,"t"//bb2,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
c
         CALL gr_fi_ecrit(1, klon,iim,jjmp1, qlevSTD(:,k),zx_tmp_2d)
         CALL histwrite(nid_hf,"q"//bb2,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
c
        ENDIF !bb2.EQ."850".or.bb2.EQ."500"
c
        IF(bb2.EQ."500") THEN
c
         CALL gr_fi_ecrit(1, klon,iim,jjmp1, philevSTD(:,k),zx_tmp_2d)
         CALL histwrite(nid_hf,"phi"//bb2,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
c
        CALL gr_fi_ecrit(1, klon,iim,jjmp1, wlevSTD(:,k),zx_tmp_2d)
        CALL histwrite(nid_hf,"w"//bb2,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
c
        ENDIF !bb2.EQ."500"
c
        IF(bb3.EQ."50") THEN
c
         CALL gr_fi_ecrit(1, klon,iim,jjmp1, tlevSTD(:,k),zx_tmp_2d)
         CALL histwrite(nid_hf,"t"//bb3,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
c
        CALL gr_fi_ecrit(1, klon,iim,jjmp1, ulevSTD(:,k),zx_tmp_2d)
        CALL histwrite(nid_hf,"u"//bb3,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
c
        CALL gr_fi_ecrit(1, klon,iim,jjmp1, vlevSTD(:,k),zx_tmp_2d)
        CALL histwrite(nid_hf,"v"//bb3,itau_w,zx_tmp_2d,
     $       iim*jjmp1,ndex2d)
c
        ENDIF !bb3.EQ."50"
c
       ENDDO !k=1, nlevSTD
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zu10m,zx_tmp_2d)
      CALL histwrite(nid_hf,"u10m",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zv10m,zx_tmp_2d)
      CALL histwrite(nid_hf,"v10m",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zt2m,zx_tmp_2d)
      CALL histwrite(nid_hf,"t2m",itau_w,zx_tmp_2d,iim*jjmp1,
     $               ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zq2m,zx_tmp_2d)
      CALL histwrite(nid_hf,"q2m",itau_w,zx_tmp_2d,iim*jjmp1,
     $               ndex2d)
c
      DO i = 1, klon
         zx_tmp_fi2d(i) = paprs(i,1)
      ENDDO
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d,zx_tmp_2d)
      CALL histwrite(nid_hf,"psol",itau_w,zx_tmp_2d,iim*jjmp1,
     $               ndex2d)
c
cIM 140904 BEG
      DO i = 1, klon
         zx_tmp_fi2d(i) = cldt(i)*100.
      ENDDO
cIM 140904 END
cIM 140904   CALL gr_fi_ecrit(1, klon,iim,jjmp1, cldt,zx_tmp_2d)
      CALL gr_fi_ecrit(1, klon,iim,jjmp1,zx_tmp_fi2d,zx_tmp_2d)
      CALL histwrite(nid_hf,"cldt",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      zx_tmp_fi2d(1 : klon) = zxfluxu(1 : klon, 1)
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d , zx_tmp_2d)
      CALL histwrite(nid_hf,"taux",itau_w,
     $      zx_tmp_2d,iim*jjmp1,ndex2d)
C
      zx_tmp_fi2d(1 : klon) = zxfluxv(1 : klon, 1)
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d , zx_tmp_2d)
      CALL histwrite(nid_hf,"tauy",itau_w,
     $      zx_tmp_2d,iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, prw,zx_tmp_2d)
      CALL histwrite(nid_hf,"prw",itau_w,zx_tmp_2d,
     $               iim*jjmp1,ndex2d)
c
      zx_tmp_fi2d(1:klon)=-1*sens(1:klon)
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d,zx_tmp_2d)
      CALL histwrite(nid_hf,"sens",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zxfluxlat,zx_tmp_2d)
      CALL histwrite(nid_hf,"flat",itau_w,zx_tmp_2d,iim*jjmp1,ndex2d)
c
c     CALL gr_fi_ecrit(1, klon,iim,jjmp1, zsnow_mass,zx_tmp_2d)
c     CALL histwrite(nid_hf,"snow_mass",itau_w,zx_tmp_2d,iim*jjmp1,
c    $               ndex2d)
c
      zx_tmp_fi2d(1 : klon) = swdn ( 1 : klon, 1 )
      CALL gr_fi_ecrit(1,klon,iim,jjmp1,zx_tmp_fi2d,zx_tmp_2d)
      CALL histwrite(nid_hf, "SWdnSFC",itau_w,zx_tmp_2d,
     $                               iim*jjmp1,ndex2d)
c
      zx_tmp_fi2d(1 : klon) = swup ( 1 : klon, 1 )
      CALL gr_fi_ecrit(1,klon,iim,jjmp1,zx_tmp_fi2d,zx_tmp_2d)
      CALL histwrite(nid_hf, "SWupSFC",itau_w,zx_tmp_2d,
     $                               iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, sollwdown,zx_tmp_2d)
      CALL histwrite(nid_hf,"LWdnSFC",itau_w,zx_tmp_2d,iim*jjmp1,
     $               ndex2d)
c
cIM 090904   zx_tmp_fi2d(1:klon)=sollw(1:klon)+sollwdown(1:klon)
cIM 130904   zx_tmp_fi2d(1:klon)=sollw(1:klon)-sollwdown(1:klon)
      zx_tmp_fi2d(1:klon)=sollwdown(1:klon)-sollw(1:klon)
      CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d, zx_tmp_2d)
      CALL histwrite(nid_hf,"LWupSFC",itau_w,zx_tmp_2d,iim*jjmp1,
     $               ndex2d)                    
c
      zx_tmp_fi2d(1 : klon) = swdn ( 1 : klon, klevp1 )
      CALL gr_fi_ecrit(1,klon,iim,jjmp1,zx_tmp_fi2d,zx_tmp_2d)
      CALL histwrite(nid_hf, "SWdnTOA",itau_w,zx_tmp_2d,
     $                               iim*jjmp1,ndex2d)
c
      zx_tmp_fi2d(1 : klon) = swup ( 1 : klon, klevp1 )
      CALL gr_fi_ecrit(1,klon,iim,jjmp1,zx_tmp_fi2d,zx_tmp_2d)
      CALL histwrite(nid_hf, "SWupTOA",itau_w,zx_tmp_2d,
     $                               iim*jjmp1,ndex2d)
c
      CALL gr_fi_ecrit(nbteta,klon,iim,jjmp1,PVteta,zx_tmp_3dte)
      DO k=1, nbteta
       CALL histwrite(nid_hf,"PV"//ctetaSTD(k),
     .      itau_w,zx_tmp_3dte(:,:,k),iim*jjmp1,ndex2d)
      ENDDO !k=1, nbteta
c
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
      IF (ok_sync) THEN
        call histsync(nid_hf)
      ENDIF

      ENDIF
