Index: /LMDZ.3.3/branches/rel-LF/libf/phylmd/physiq.F
===================================================================
--- /LMDZ.3.3/branches/rel-LF/libf/phylmd/physiq.F	(revision 270)
+++ /LMDZ.3.3/branches/rel-LF/libf/phylmd/physiq.F	(revision 271)
@@ -10,4 +10,5 @@
       USE ioipsl
       USE histcom
+      USE writephys
 
       IMPLICIT none
@@ -576,4 +577,14 @@
       real date0
 
+C essai writephys
+      integer fid_day, fid_mth, fid_ins
+      parameter (fid_ins = 1, fid_day = 2, fid_mth = 3) 
+      integer prof2d_on, prof3d_on, prof2d_av, prof3d_av
+      parameter (prof2d_on = 1, prof3d_on = 2,
+     .           prof2d_av = 3, prof3d_av = 4)
+      character*30 nom_fichier
+      character*10 varname
+      character*40 vartitle
+      character*20 varunits
 c
 c Declaration des constantes et des fonctions thermodynamiques
@@ -758,4 +769,16 @@
          zsto = dtime
          zout = dtime * FLOAT(ecrit_day)
+C Essai writephys
+         nom_fichier = 'histday1'
+         call writephy_ini(fid_day,nom_fichier,klon,iim,jjmp1,klev,
+     .                     rlon,rlat, presnivs,
+     .                     zjulian, dtime)
+         call writephy_def(prof2d_on, fid_day, "once", zsto, zout, 0)
+         call writephy_def(prof3d_on, fid_day, "once", zsto, zout,
+     .                                                         klev)
+         call writephy_def(prof2d_av, fid_day, "ave(X)", zsto, zout, 0) 
+         call writephy_def(prof3d_av, fid_day, "ave(X)", zsto, zout, 
+     .                                                         klev)
+ 
 c
          CALL histdef(nid_day, "phis", "Surface geop. height", "-",
@@ -2348,11 +2371,23 @@
          CALL gr_fi_ecrit(1,klon,iim,jjmp1,pphis,zx_tmp_2d)
          CALL histwrite(nid_day,"phis",i,zx_tmp_2d,iim*jjmp1,ndex2d)
+         varname = 'phis'
+         vartitle= 'Surface geop. height'
+         varunits= '-'
+         call writephy(fid_day,prof2d_on,varname,pphis,vartitle,
+     .                                                    varunits)
 c
          i = NINT(zout/zsto)
          CALL gr_fi_ecrit(1,klon,iim,jjmp1,paire,zx_tmp_2d)
          CALL histwrite(nid_day,"aire",i,zx_tmp_2d,iim*jjmp1,ndex2d)
+         varname = 'aire'
+         vartitle= 'Grid area'
+         varunits= '-'
+         call writephy(fid_day,prof2d_on,varname,paire,vartitle,
+     .                                                    varunits)
 C
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, zxtsol,zx_tmp_2d)
       CALL histwrite(nid_day,"tsol",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'tsol',zxtsol,
+     .              'Surface Temperature','K')
 c
 C
@@ -2360,16 +2395,24 @@
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d ,zx_tmp_2d)
       CALL histwrite(nid_day,"tter",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'tter',ftsol(1 : klon, is_ter),
+     .              'Surface Temperature','K')
 C
       zx_tmp_fi2d(1 : klon) = ftsol(1 : klon, is_lic)
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, zxtsol,zx_tmp_2d)
       CALL histwrite(nid_day,"tlic",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'tlic',ftsol(1 : klon, is_lic),
+     .              'Surface Temperature','K')
 C
       zx_tmp_fi2d(1 : klon) = ftsol(1 : klon, is_oce)
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, zxtsol,zx_tmp_2d)
       CALL histwrite(nid_day,"toce",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'toce',ftsol(1 : klon, is_oce),
+     .              'Surface Temperature','K')
 C
       zx_tmp_fi2d(1 : klon) = ftsol(1 : klon, is_sic)
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, zxtsol,zx_tmp_2d)
       CALL histwrite(nid_day,"tsic",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'tsic',ftsol(1 : klon, is_sic),
+     .              'Surface Temperature','K')
 C
       DO i = 1, klon
@@ -2378,43 +2421,75 @@
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d,zx_tmp_2d)
       CALL histwrite(nid_day,"psol",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
-c
-      DO i = 1, klon
-         zx_tmp_fi2d(i) = rain_fall(i) + snow_fall(i)
+c Essai writephys
+      varname = 'psol'
+      vartitle= 'pression au sol'
+      varunits= 'hPa'
+      call writephy(fid_day,prof2d_av,varname,zx_tmp_fi2d,vartitle,
+     .                                                    varunits)
+c
+      DO i = 1, klon
+         zx_tmp_fi2d(i) = (rain_fall(i) + snow_fall(i))* 86400.
       ENDDO
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d,zx_tmp_2d)
       CALL histwrite(nid_day,"rain",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'rain',zx_tmp_fi2d,
+     .              'Precipitation','mm/day')
+
+
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, snow_fall,zx_tmp_2d)
       CALL histwrite(nid_day,"snow",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'snow',snow_fall,
+     .              'Snow','mm/day')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, zxsnow,zx_tmp_2d)
       CALL histwrite(nid_day,"snow_cov",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'snow_cov',zxsnow,
+     .              'Snow cover','mm')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, evap,zx_tmp_2d)
       CALL histwrite(nid_day,"evap",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'evap',evap,
+     .              'Evaporation','mm/day')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, topsw,zx_tmp_2d)
       CALL histwrite(nid_day,"tops",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'tops',topsw,
+     .              'Solar rad. at TOA','W/m2')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, toplw,zx_tmp_2d)
       CALL histwrite(nid_day,"topl",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'topl',toplw,
+     .              'IR rad. at TOA','W/m2')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, solsw,zx_tmp_2d)
       CALL histwrite(nid_day,"sols",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'sols',solsw,
+     .              'Solar rad. at surf.','W/m2')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, sollw,zx_tmp_2d)
       CALL histwrite(nid_day,"soll",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'soll',sollw,
+     .              'IR rad. at surface','W/m2')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, sollwdown,zx_tmp_2d)
       CALL histwrite(nid_day,"solldown",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'solldown',sollwdown,
+     .              'Down. IR rad. at surface','W/m2')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, bils,zx_tmp_2d)
       CALL histwrite(nid_day,"bils",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'bils',bils,
+     .              'Surf. total heat flux','W/m2')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, sens,zx_tmp_2d)
       CALL histwrite(nid_day,"sens",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'sens',sens,
+     .              'Sensible heat flux','W/m2')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, fder,zx_tmp_2d)
       CALL histwrite(nid_day,"fder",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'fder',fder,
+     .              'Heat flux derivation','W/m2')
 c
 c
@@ -2425,4 +2500,7 @@
         CALL histwrite(nid_day,"pourc_"//clnsurf(nsrf),itap,
      $      zx_tmp_2d,iim*jjmp1,ndex2d) 
+        call writephy(fid_day,prof2d_av,'pourc_'//clnsurf(nsrf),
+     .                pctsrf( 1 : klon, nsrf),
+     .                'Fraction'//clnsurf(nsrf),'-')
 C
         zx_tmp_fi2d(1 : klon) = ftsol( 1 : klon, nsrf)
@@ -2430,4 +2508,7 @@
         CALL histwrite(nid_day,"tsol_"//clnsurf(nsrf),itap,
      $      zx_tmp_2d,iim*jjmp1,ndex2d) 
+        call writephy(fid_day,prof2d_av,'tsol_'//clnsurf(nsrf),
+     .                ftsol( 1 : klon, nsrf),
+     .                'Surf. Temp'//clnsurf(nsrf),'K')
 C 
         zx_tmp_fi2d(1 : klon) = fluxt( 1 : klon, 1, nsrf)
@@ -2435,9 +2516,15 @@
         CALL histwrite(nid_day,"sens_"//clnsurf(nsrf),itap,
      $      zx_tmp_2d,iim*jjmp1,ndex2d) 
-C
+        call writephy(fid_day,prof2d_av,'sens_'//clnsurf(nsrf),
+     .                fluxt( 1 : klon, 1, nsrf),
+     .                'Sensible heat flux '//clnsurf(nsrf),'W/m2')
+C  
         zx_tmp_fi2d(1 : klon) = fluxlat( 1 : klon, nsrf)
         CALL gr_fi_ecrit(1, klon,iim,jjmp1, zx_tmp_fi2d , zx_tmp_2d)
         CALL histwrite(nid_day,"lat_"//clnsurf(nsrf),itap,
      $      zx_tmp_2d,iim*jjmp1,ndex2d) 
+        call writephy(fid_day,prof2d_av,'lat_'//clnsurf(nsrf),
+     .                fluxlat( 1 : klon, nsrf),
+     .                'Latent heat flux '//clnsurf(nsrf),'W/m2')
 C
         zx_tmp_fi2d(1 : klon) = fluxu( 1 : klon, 1, nsrf)
@@ -2445,4 +2532,7 @@
         CALL histwrite(nid_day,"taux_"//clnsurf(nsrf),itap,
      $      zx_tmp_2d,iim*jjmp1,ndex2d) 
+        call writephy(fid_day,prof2d_av,'taux_'//clnsurf(nsrf),
+     .                fluxu( 1 : klon, 1, nsrf),
+     .                'Zonal wind stress '//clnsurf(nsrf),'Pa')
 C      
         zx_tmp_fi2d(1 : klon) = fluxv( 1 : klon, 1, nsrf)
@@ -2450,4 +2540,7 @@
         CALL histwrite(nid_day,"tauy_"//clnsurf(nsrf),itap,
      $      zx_tmp_2d,iim*jjmp1,ndex2d)
+        call writephy(fid_day,prof2d_av,'tauy_'//clnsurf(nsrf),
+     .                fluxv( 1 : klon, 1, nsrf),
+     .                'Meridional wind stress '//clnsurf(nsrf),'Pa')
 C
         zx_tmp_fi2d(1 : klon) = falbe( 1 : klon, nsrf)
@@ -2455,4 +2548,7 @@
         CALL histwrite(nid_day,"albe_"//clnsurf(nsrf),itap,
      $      zx_tmp_2d,iim*jjmp1,ndex2d) 
+        call writephy(fid_day,prof2d_av,'albe_'//clnsurf(nsrf),
+     .                falbe( 1 : klon, nsrf),
+     .                'Albedo surf.'//clnsurf(nsrf),'-')
 C
         zx_tmp_fi2d(1 : klon) = frugs( 1 : klon, nsrf)
@@ -2460,4 +2556,7 @@
         CALL histwrite(nid_day,"rugs_"//clnsurf(nsrf),itap,
      $      zx_tmp_2d,iim*jjmp1,ndex2d) 
+        call writephy(fid_day,prof2d_av,'rugs_'//clnsurf(nsrf),
+     .                frugs( 1 : klon, nsrf),
+     .                'Rugosity '//clnsurf(nsrf),' - ')
 C 
       END DO  
@@ -2471,16 +2570,26 @@
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, cldl,zx_tmp_2d)
       CALL histwrite(nid_day,"cldl",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'cldl',cldl,
+     .              'Low-level cloudiness','-')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, cldm,zx_tmp_2d)
       CALL histwrite(nid_day,"cldm",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'cldm',cldm,
+     .              'Mid-level cloudiness','-')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, cldh,zx_tmp_2d)
       CALL histwrite(nid_day,"cldh",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'cldh',cldh,
+     .              'High-level cloudiness','-')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, cldt,zx_tmp_2d)
       CALL histwrite(nid_day,"cldt",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'cldt',cldt,
+     .              'Total cloudiness','-')
 c
       CALL gr_fi_ecrit(1, klon,iim,jjmp1, cldq,zx_tmp_2d)
       CALL histwrite(nid_day,"cldq",itap,zx_tmp_2d,iim*jjmp1,ndex2d)
+      call writephy(fid_day,prof2d_av,'cldq',cldq,
+     .              'Cloud liquid water path','-')
 c
 c Champs 3D:
@@ -2489,30 +2598,49 @@
       CALL histwrite(nid_day,"temp",itap,zx_tmp_3d,
      .                                   iim*jjmp1*klev,ndex3d)
+c Essai writephys
+      varname = 'temp'
+      vartitle= 'temperature 3D'
+      varunits= 'K'
+      call writephy(fid_day,prof3d_av,varname,t_seri,vartitle,varunits)
 c
       CALL gr_fi_ecrit(klev,klon,iim,jjmp1, qx(1,1,ivap), zx_tmp_3d)
       CALL histwrite(nid_day,"ovap",itap,zx_tmp_3d,
      .                                   iim*jjmp1*klev,ndex3d)
+      call writephy(fid_day,prof3d_av,'ovap',qx(1,1,ivap),
+     .              'Specific humidity','Kg/Kg')
 c
       CALL gr_fi_ecrit(klev,klon,iim,jjmp1, zphi, zx_tmp_3d)
       CALL histwrite(nid_day,"geop",itap,zx_tmp_3d,
      .                                   iim*jjmp1*klev,ndex3d)
+      call writephy(fid_day,prof3d_av,'geop',zphi,
+     .              'Geopotential height','m')
 c
       CALL gr_fi_ecrit(klev,klon,iim,jjmp1, u_seri, zx_tmp_3d)
       CALL histwrite(nid_day,"vitu",itap,zx_tmp_3d,
      .                                   iim*jjmp1*klev,ndex3d)
+      call writephy(fid_day,prof3d_av,'vitu',u_seri,
+     .              'Zonal wind','m/s')
 c
       CALL gr_fi_ecrit(klev,klon,iim,jjmp1, v_seri, zx_tmp_3d)
       CALL histwrite(nid_day,"vitv",itap,zx_tmp_3d,
      .                                   iim*jjmp1*klev,ndex3d)
+      call writephy(fid_day,prof3d_av,'vitv',v_seri,
+     .              'Meridional wind','m/s')
 c
       CALL gr_fi_ecrit(klev,klon,iim,jjmp1, omega, zx_tmp_3d)
       CALL histwrite(nid_day,"vitw",itap,zx_tmp_3d,
      .                                   iim*jjmp1*klev,ndex3d)
+      call writephy(fid_day,prof3d_av,'vitw',omega,
+     .              'Vertical wind','m/s')
 c
       CALL gr_fi_ecrit(klev,klon,iim,jjmp1, pplay, zx_tmp_3d)
       CALL histwrite(nid_day,"pres",itap,zx_tmp_3d,
      .                                   iim*jjmp1*klev,ndex3d)
+      call writephy(fid_day,prof3d_av,'pres',pplay,
+     .              'Air pressure','Pa')
+
 c
       if (ok_sync) then
+        call writephy_sync(fid_day)
         call histsync(nid_day)
       endif
