      IF (ok_journe) THEN
c
         zsto = dtime
         zout = dtime * FLOAT(ecrit_day)
c zsto1: pour des flux radiatifs calcules tous les radpas appels physiq
         zsto1= dtime * FLOAT(radpas)
c
         idayref = day_ref
         CALL ymds2ju(annee_ref, 1, idayref, zero, zjulian)
c
         CALL gr_fi_ecrit(1,klon,iim,jjmp1,rlond,zx_lon)
         DO i = 1, iim
            zx_lon(i,1) = rlond(i+jjmp1-jjm)
            zx_lon(i,jjmp1) = rlond(i+jjmp1-jjm)
         ENDDO
         CALL gr_fi_ecrit(1,klon,iim,jjmp1,rlatd,zx_lat)
         CALL histbeg("histday", iim,zx_lon(:,1), jjmp1,zx_lat(1,:),
     .                 1,iim,1,jjmp1, itau_phy, zjulian, dtime, 
     .                 nhori, nid_day)
         write(*,*)'Journee ', itau_phy, zjulian
         CALL histvert(nid_day, "presnivs", "Vertical levels", "mb",
     .                 klev, presnivs, nvert)

c-------------------------------------------------------
      IF(lev_histday.GE.1) THEN
c
ccccccccccccc 2D fields, invariables
c
         CALL histdef(nid_day, "phis", "Surface geop. height", "-",
     .                iim,jjmp1,nhori, 1,1,1, -99, 32, 
     .                "once",  zsto,zout)
c
         CALL histdef(nid_day, "aire", "Grid area", "-",
     .                iim,jjmp1,nhori, 1,1,1, -99, 32, 
     .                "once",  zsto,zout)
c
ccccccc axe Ls
         CALL histdef(nid_day, "ls", "Solar longitude", "degrees",
     .                iim,jjmp1,nhori, 1,1,1, -99, 32, 
     .                "ave(X)", zsto,zout)
c
ccccccccccccc 2D fields, variables
c
         CALL histdef(nid_day, "tsol", "Surface Temperature", "K",
     .                iim,jjmp1,nhori, 1,1,1, -99, 32, 
     .                "ave(X)", zsto,zout)
c
         CALL histdef(nid_day, "psol", "Surface Pressure", "Pa",
     .                iim,jjmp1,nhori, 1,1,1, -99, 32, 
     .                "ave(X)", zsto,zout)
c
c        CALL histdef(nid_day, "ue", "Zonal energy transport", "-",
c    .                iim,jjmp1,nhori, 1,1,1, -99, 32, 
c    .                "ave(X)", zsto,zout)
c
c        CALL histdef(nid_day, "ve", "Merid energy transport", "-",
c     .                iim,jjmp1,nhori, 1,1,1, -99, 32, 
c     .                "ave(X)", zsto,zout)
c
      ENDIF !lev_histday.GE.1
c
c-------------------------------------------------------
      IF(lev_histday.GE.2) THEN
c
ccccccccccccc 3D fields, basics
c
         CALL histdef(nid_day, "temp", "Air temperature", "K",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
         CALL histdef(nid_day, "pres", "Air pressure", "Pa",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
         CALL histdef(nid_day, "geop", "Geopotential height", "m",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
         CALL histdef(nid_day, "vitu", "Zonal wind", "m/s",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
         CALL histdef(nid_day, "vitv", "Meridional wind", "m/s",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
         CALL histdef(nid_day, "vitw", "Vertical wind", "Pa/s",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
         CALL histdef(nid_day, "tops", "Solar rad. at TOA", "W/m2",
     .                iim,jjmp1,nhori, 1,1,1, -99, 32, 
     .                "ave(X)", zsto1,zout)
c
         CALL histdef(nid_day, "dudyn", "Dynamics dU", "m/s2",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
         CALL histdef(nid_day, "duvdf", "Boundary-layer dU", "m/s2",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
cccccccccccccccccc  Tracers
c
         if (iflag_trac.eq.1) THEN
          if (microfi.ge.1) then
c           DO iq=1,nmicro
c             CALL histdef(nid_day, tname(iq), ttext(iq), "n/m2",
c     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
c     .                "ave(X)", zsto,zout)
c           ENDDO
             CALL histdef(nid_day, "qaer","nb tot aer" , "n/m2",
     .                    iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                    "ave(X)", zsto,zout)

            if (clouds.eq.1) then
             CALL histdef(nid_day, "qnoy","nb tot noy" , "n/m2",
     .                    iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                    "ave(X)", zsto,zout)
             CALL histdef(nid_day, "qgl1","V tot gl1" , "m3/m2",
     .                    iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                    "ave(X)", zsto,zout)
             CALL histdef(nid_day, "qgl2","V tot gl2" , "m3/m2",
     .                    iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                    "ave(X)", zsto,zout)
             CALL histdef(nid_day, "qgl3","V tot gl3" , "m3/m2",
     .                    iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                    "ave(X)", zsto,zout)
c--------------
c ----- SATURATION ESP NUAGES
               CALL histdef(nid_day,"ch4sat", "saturation CH4", "--",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
               CALL histdef(nid_day,"c2h6sat", "saturation C2H6", "--",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
               CALL histdef(nid_day,"c2h2sat", "saturation C2H2", "--",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c --------------
c ----- RESERVOIR DE SURFACE
               CALL histdef(nid_day, "reserv", "Reservoir surface","m",
     .                iim,jjmp1,nhori, 1,1,1, -99, 32,
     .                "ave(X)", zsto,zout)
c --------------
c ----- ECHANGE GAZ SURF/ATM (evaporation)
               CALL histdef(nid_day, "evapch4", "Evaporation CH4","m",
     .                iim,jjmp1,nhori, 1,1,1, -99, 32,
     .                "ave(X)", zsto,zout)
c --------------
c ----- PRECIPITATIONS (precipitations cumulatives)
               CALL histdef(nid_day,"prech4","Precip CH4","m",
     .                iim,jjmp1,nhori, 1,1,1, -99, 32,
     .                "ave(X)", zsto,zout)
               CALL histdef(nid_day,"prec2h6","Precip C2H6",
     .                "m",iim,jjmp1,nhori, 1,1,1, -99, 32,
     .                "ave(X)", zsto,zout)
               CALL histdef(nid_day,"prec2h2","Precip C2H2",
     .                "m",iim,jjmp1,nhori, 1,1,1, -99, 32,
     .                "ave(X)", zsto,zout)
c --------------
c ----- FLUX GLACE
               CALL histdef(nid_day,"flxgl1", "flux gl CH4",
     .              "kg/m2/s",iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .              "ave(X)", zsto,zout)
               CALL histdef(nid_day,"flxgl2", "flux gl C2H6",
     .              "kg/m2/s",iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .              "ave(X)", zsto,zout)
               CALL histdef(nid_day,"flxgl3", "flux gl C2H2",
     .              "kg/m2/s",iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .              "ave(X)", zsto,zout)
c --------------
c ----- RAYON DES GOUTTES
               CALL histdef(nid_day,"rcldbar", "rayon moyen goutte",
     .                "m",iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
            endif
	  endif
c --------------
c ----- TRACEURS CHIMIQUES
	  if (nmicro.lt.nqmax) then
           DO iq=nmicro+1,nqmax
         CALL histdef(nid_day, tname(iq), ttext(iq), "ppm",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
           ENDDO
	  endif
         endif
c
      ENDIF !lev_histday.GE.2
c
c-------------------------------------------------------
      IF(lev_histday.GE.3) THEN
c
cccccccccccccccccc  Radiative transfer
c
c 2D
c
         CALL histdef(nid_day, "topl", "IR rad. at TOA", "W/m2",
     .                iim,jjmp1,nhori, 1,1,1, -99, 32, 
     .                "ave(X)", zsto1,zout)
c
         CALL histdef(nid_day, "sols", "Solar rad. at surf.", "W/m2",
     .                iim,jjmp1,nhori, 1,1,1, -99, 32, 
     .                "ave(X)", zsto1,zout)
c
         CALL histdef(nid_day, "soll", "IR rad. at surface", "W/m2",
     .                iim,jjmp1,nhori, 1,1,1, -99, 32, 
     .                "ave(X)", zsto1,zout)
c
c 3D
c
         CALL histdef(nid_day, "SWnet", "Net SW flux","W/m2",
     .                iim,jjmp1,nhori, klev,1,klev,nvert,
     .                32, "ave(X)", zsto1,zout)
c
         CALL histdef(nid_day, "LWnet", "Net LW flux","W/m2",
     .                iim,jjmp1,nhori, klev,1,klev,nvert,
     .                32, "ave(X)", zsto1,zout)
c
c --------------
c ----- OPACITE BRUME
         DO k=7,NSPECV,10
           write(str1,'(i2.2)') k
         CALL histdef(nid_day,"thv"//str1,"Haze Opa Vis",
     .                "--",iim,jjmp1,nhori,klev,1,klev,nvert,32,
     .                "ave(X)",zsto1,zout)
         ENDDO
c
         DO k=8,NSPECI,10
           write(str1,'(i2.2)') k
         CALL histdef(nid_day,"thi"//str1,"Haze Opa IR",
     .                "--",iim,jjmp1,nhori,klev,1,klev,nvert,32,
     .                "ave(X)",zsto1,zout)
         ENDDO
c
c --------------
c ----- EXTINCTION BRUME
         DO k=7,NSPECV,10
           write(str1,'(i2.2)') k
         CALL histdef(nid_day,"khv"//str1,"Haze ext Vis ",
     .                "m-1",iim,jjmp1,nhori,klev,1,klev,nvert,32,
     .                "ave(X)",zsto1,zout)
         ENDDO
c
         DO k=8,NSPECI,10
           write(str1,'(i2.2)') k
         CALL histdef(nid_day,"khi"//str1,"Haze ext IR ",
     .                "m-1",iim,jjmp1,nhori,klev,1,klev,nvert,32,
     .                "ave(X)",zsto1,zout)
         ENDDO
c
c --------------
c ----- OPACITE GAZ
         DO k=7,NSPECV,10
           write(str1,'(i2.2)') k
         CALL histdef(nid_day,"tgv"//str1,"Gas Opa Vis",
     .                "--",iim,jjmp1,nhori,klev,1,klev,nvert,32,
     .                "ave(X)",zsto1,zout)
         ENDDO
c
         DO k=8,NSPECI,10
           write(str1,'(i2.2)') k
         CALL histdef(nid_day,"tgi"//str1,"Gas Opa IR",
     .                "--",iim,jjmp1,nhori,klev,1,klev,nvert,32,
     .                "ave(X)",zsto1,zout)
         ENDDO
c
c --------------
c ----- EXTINCTION GAZ
         DO k=7,NSPECV,10
           write(str1,'(i2.2)') k
         CALL histdef(nid_day,"kgv"//str1,"Gas ext Vis ",
     .                "m-1",iim,jjmp1,nhori,klev,1,klev,nvert,32,
     .                "ave(X)",zsto1,zout)
         ENDDO
c
         DO k=8,NSPECI,10
           write(str1,'(i2.2)') k
         CALL histdef(nid_day,"kgi"//str1,"Gas ext IR ",
     .                "m-1",iim,jjmp1,nhori,klev,1,klev,nvert,32,
     .                "ave(X)",zsto1,zout)
         ENDDO
c
c --------------
c ----- OPACITE NUAGES
         if (clouds.eq.1) then
           CALL histdef(nid_day,"tcld","Cld Opa proxy",
     .                "--",iim,jjmp1,nhori,klev,1,klev,nvert,32,
     .                "ave(X)",zsto,zout)
c
c --------------
c ----- EXTINCTION NUAGES
           CALL histdef(nid_day,"kcld","Cld Ext proxy",
     .                "m-1",iim,jjmp1,nhori,klev,1,klev,nvert,32,
     .                "ave(X)",zsto,zout)
         endif
c
      ENDIF !lev_histday.GE.3
c
c-------------------------------------------------------
      IF(lev_histday.GE.4) THEN
c
         CALL histdef(nid_day, "dtdyn", "Dynamics dT", "K/s",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
         CALL histdef(nid_day, "dtphy", "Physics dT", "K/s",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
         CALL histdef(nid_day, "dtvdf", "Boundary-layer dT", "K/s",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
         CALL histdef(nid_day, "dtajs", "Dry adjust. dT", "K/s",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
         CALL histdef(nid_day, "dtswr", "SW radiation dT", "K/s",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
         CALL histdef(nid_day, "dtlwr", "LW radiation dT", "K/s",
     .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
     .                "ave(X)", zsto,zout)
c
c        CALL histdef(nid_day, "dtec", "Cinetic dissip dT", "K/s",
c    .                iim,jjmp1,nhori, klev,1,klev,nvert, 32,
c    .                "ave(X)", zsto,zout)
c
      ENDIF !lev_histday.GE.4
c
c-------------------------------------------------------
      IF(lev_histday.GE.5) THEN
c
c
c        call histdef(nid_day, "taux", 
c    $         "Zonal wind stress", "Pa",  
c    $         iim,jjmp1,nhori, 1,1,1, -99, 32,
c    $         "ave(X)", zsto,zout)
c
c        call histdef(nid_day, "tauy", 
c    $         "Meridional xind stress", "Pa",  
c    $         iim,jjmp1,nhori, 1,1,1, -99, 32,
c    $         "ave(X)", zsto,zout)
c
c        CALL histdef(nid_day, "cdrm", "Momentum drag coef.", "-",
c    .                iim,jjmp1,nhori, 1,1,1, -99, 32, 
c    .                "ave(X)", zsto,zout)
c
c        CALL histdef(nid_day, "cdrh", "Heat drag coef.", "-",
c    .                iim,jjmp1,nhori, 1,1,1, -99, 32, 
c    .                "ave(X)", zsto,zout)
c
      ENDIF !lev_histday.GE.5
c-------------------------------------------------------
c
         CALL histend(nid_day)
c
         ndex2d = 0
         ndex3d = 0
c
      ENDIF ! fin de test sur ok_journe
