Index: LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/com_io_dyn.h
===================================================================
--- LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/com_io_dyn.h	(revision 1346)
+++ 	(revision )
@@ -1,5 +1,0 @@
-!
-! $Header$
-!
-      integer histid, histvid, histaveid
-      common/com_io_dyn/histid, histvid, histaveid
Index: LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/com_io_dyn_mod.F90
===================================================================
--- LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/com_io_dyn_mod.F90	(revision 1357)
+++ LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/com_io_dyn_mod.F90	(revision 1357)
@@ -0,0 +1,31 @@
+!
+! $Id $
+!
+module com_io_dyn_mod
+
+  implicit none 
+
+! Names of various files for outputs (in the dynamics)
+  ! to store instantaneous values:
+  character(len=18),parameter :: dynhist_file="dyn_hist.nc" ! on scalar grid
+  character(len=18),parameter :: dynhistv_file="dyn_histv.nc" ! on v grid
+  character(len=18),parameter :: dynhistu_file="dyn_histu.nc" ! on u grid
+
+  ! to store averaged values:
+  character(len=18),parameter :: dynhistave_file="dyn_hist_ave.nc"
+  character(len=18),parameter :: dynhistvave_file="dyn_histv_ave.nc"
+  character(len=18),parameter :: dynhistuave_file="dyn_histu_ave.nc"
+  
+! Ids of various files for outputs (in the dynamics)
+
+  ! instantaneous (these are set by inithist.F)
+  integer :: histid
+  integer :: histvid
+  integer :: histuid
+  
+  ! averages (these are set by initdynav.F)
+  integer :: histaveid
+  integer :: histvaveid
+  integer :: histuaveid
+  
+end module com_io_dyn_mod
Index: LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/conf_gcm.F
===================================================================
--- LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/conf_gcm.F	(revision 1346)
+++ LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/conf_gcm.F	(revision 1357)
@@ -601,4 +601,19 @@
       CALL getin('ok_dynzon',ok_dynzon) 
 
+!Config  Key  = ok_dyn_ins
+!Config  Desc = sorties instantanees dans la dynamique
+!Config  Def  = n 
+!Config  Help = 
+!Config          
+      ok_dyn_ins = .FALSE. 
+      CALL getin('ok_dyn_ins',ok_dyn_ins) 
+
+!Config  Key  = ok_dyn_ave
+!Config  Desc = sorties moyennes dans la dynamique
+!Config  Def  = n 
+!Config  Help = 
+!Config          
+      ok_dyn_ave = .FALSE. 
+      CALL getin('ok_dyn_ave',ok_dyn_ave) 
 
       write(lunout,*)' #########################################'
@@ -641,4 +656,6 @@
       write(lunout,*)' config_inca = ', config_inca
       write(lunout,*)' ok_dynzon = ', ok_dynzon 
+      write(lunout,*)' ok_dyn_ins = ', ok_dyn_ins 
+      write(lunout,*)' ok_dyn_ave = ', ok_dyn_ave 
 
       RETURN
@@ -773,4 +790,20 @@
       ok_dynzon = .FALSE. 
       CALL getin('ok_dynzon',ok_dynzon) 
+
+!Config  Key  = ok_dyn_ins
+!Config  Desc = sorties instantanees dans la dynamique
+!Config  Def  = n 
+!Config  Help = 
+!Config          
+      ok_dyn_ins = .FALSE. 
+      CALL getin('ok_dyn_ins',ok_dyn_ins) 
+
+!Config  Key  = ok_dyn_ave
+!Config  Desc = sorties moyennes dans la dynamique
+!Config  Def  = n 
+!Config  Help = 
+!Config          
+      ok_dyn_ave = .FALSE. 
+      CALL getin('ok_dyn_ave',ok_dyn_ave) 
 
 !Config  Key  = use_filtre_fft
@@ -866,4 +899,6 @@
       write(lunout,*)' config_inca = ', config_inca
       write(lunout,*)' ok_dynzon = ', ok_dynzon 
+      write(lunout,*)' ok_dyn_ins = ', ok_dyn_ins 
+      write(lunout,*)' ok_dyn_ave = ', ok_dyn_ave 
       write(lunout,*)' use_filtre_fft = ', use_filtre_fft
       write(lunout,*)' use_mpi_alloc = ', use_mpi_alloc
Index: LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/control.h
===================================================================
--- LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/control.h	(revision 1346)
+++ 	(revision )
@@ -1,29 +1,0 @@
-!
-! $Header$
-!
-!
-!  ATTENTION!!!!: ce fichier include est compatible format fixe/format libre
-!                 veillez à n'utiliser que des ! pour les commentaires
-!                 et à bien positionner les & des lignes de continuation 
-!                 (les placer en colonne 6 et en colonne 73)
-!
-!-----------------------------------------------------------------------
-! INCLUDE 'control.h'
-
-      COMMON/control/nday,day_step,                                     &
-     &              iperiod,iapp_tracvl,iconser,iecri,idissip,iphysiq , &
-     &              periodav,iecrimoy,dayref,anneeref,                  &
-     &              raz_date,offline,ip_ebil_dyn,config_inca,           &
-     &              planet_type,output_grads_dyn,ok_dynzon
-
-      INTEGER   nday,day_step,iperiod,iapp_tracvl,iconser,iecri,        &
-     &          idissip,iphysiq,iecrimoy,dayref,anneeref, raz_date      &
-     &          ,ip_ebil_dyn
-      REAL periodav
-      logical offline
-      CHARACTER (len=4) :: config_inca
-      CHARACTER(len=10) :: planet_type ! planet type ('earth','mars',...)
-      LOGICAL :: output_grads_dyn ! output dynamics diagnostics in
-                                  ! binary grads file 'dyn.dat' (y/n)
-      LOGICAL :: ok_dynzon 
-!-----------------------------------------------------------------------
Index: LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/control_mod.F90
===================================================================
--- LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/control_mod.F90	(revision 1346)
+++ LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/control_mod.F90	(revision 1357)
@@ -1,4 +1,4 @@
 !
-! $Id$
+! $Id $
 !
 
@@ -14,7 +14,14 @@
   INTEGER :: iconser,iecri,idissip,iphysiq,iecrimoy
   INTEGER :: dayref,anneeref, raz_date, ip_ebil_dyn
-  LOGICAL :: offline, output_grads_dyn, ok_dynzon
+  LOGICAL :: offline
   CHARACTER (len=4)  :: config_inca
-  CHARACTER (len=10) :: planet_type
+  CHARACTER (len=10) :: planet_type ! planet type ('earth','mars',...)
+  LOGICAL output_grads_dyn ! output dynamics diagnostics in
+                           ! binary grads file 'dyn.dat' (y/n)
+  LOGICAL ok_dynzon  ! output zonal transports in dynzon.nc file
+  LOGICAL ok_dyn_ins ! output instantaneous values of fields
+                     ! in the dynamics in NetCDF files dyn_hist*nc
+  LOGICAL ok_dyn_ave ! output averaged values of fields in the dynamics
+                     ! in NetCDF files dyn_hist*ave.nc
 
 END MODULE
Index: LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/covnat.F
===================================================================
--- LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/covnat.F	(revision 1357)
+++ LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/covnat.F	(revision 1357)
@@ -0,0 +1,49 @@
+!
+! $Header$
+!
+      SUBROUTINE covnat (klevel,ucov, vcov, unat, vnat )
+      IMPLICIT NONE
+
+c=======================================================================
+c
+c   Auteur:  F Hourdin Phu LeVan
+c   -------
+c
+c   Objet:
+c   ------
+c
+c  *********************************************************************
+c    calcul des compos. naturelles a partir des comp.covariantes
+c  ********************************************************************
+c
+c=======================================================================
+
+#include "dimensions.h"
+#include "paramet.h"
+#include "comgeom.h"
+
+      INTEGER klevel
+      REAL ucov( ip1jmp1,klevel ),  vcov( ip1jm,klevel )
+      REAL unat( ip1jmp1,klevel ), vnat( ip1jm,klevel )
+      INTEGER   l,ij
+
+
+      DO l = 1,klevel
+         DO ij = 1, iip1
+            unat (ij,l) =0.
+         END DO
+
+         DO ij = iip2, ip1jm
+            unat( ij,l ) = ucov( ij,l ) / cu(ij)
+         ENDDO
+         DO ij = ip1jm+1, ip1jmp1  
+            unat (ij,l) =0.
+         END DO
+
+         DO ij = 1,ip1jm
+            vnat( ij,l ) = vcov( ij,l ) / cv(ij)
+         ENDDO
+
+      ENDDO
+      RETURN
+      END
Index: LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/gcm.F
===================================================================
--- LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/gcm.F	(revision 1346)
+++ LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/gcm.F	(revision 1357)
@@ -70,8 +70,11 @@
 #include "description.h"
 #include "serre.h"
-#include "com_io_dyn.h"
+!#include "com_io_dyn.h"
 #include "iniprint.h"
 #include "tracstoke.h"
+#ifdef INCA
+! Only INCA needs these informations (from the Earth's physics)
 #include "indicesol.h"
+#endif
 
       INTEGER         longcles
@@ -335,32 +338,32 @@
      .  ' restart ne correspondent pas a celles lues dans '
         write(lunout,*)' gcm.def'
-	write(lunout,*)' annee_ref=',annee_ref," anneeref=",anneeref
-	write(lunout,*)' day_ref=',day_ref," dayref=",dayref
-	write(lunout,*)' Pas de remise a zero'
+        write(lunout,*)' annee_ref=',annee_ref," anneeref=",anneeref
+        write(lunout,*)' day_ref=',day_ref," dayref=",dayref
+        write(lunout,*)' Pas de remise a zero'
       ENDIF
-c$$$      if (annee_ref .ne. anneeref .or. day_ref .ne. dayref) then
-c$$$        write(lunout,*)
-c$$$     .  'GCM: Attention les dates initiales lues dans le fichier'
-c$$$        write(lunout,*)
-c$$$     .  ' restart ne correspondent pas a celles lues dans '
-c$$$        write(lunout,*)' gcm.def'
-c$$$	write(lunout,*)' annee_ref=',annee_ref," anneeref=",anneeref
-c$$$	write(lunout,*)' day_ref=',day_ref," dayref=",dayref
-c$$$        if (raz_date .ne. 1) then
-c$$$          write(lunout,*)
-c$$$     .    'GCM: On garde les dates du fichier restart'
-c$$$        else
-c$$$          annee_ref = anneeref
-c$$$          day_ref = dayref
-c$$$          day_ini = dayref
-c$$$          itau_dyn = 0
-c$$$          itau_phy = 0
-c$$$          time_0 = 0.
-c$$$          write(lunout,*)
-c$$$     .   'GCM: On reinitialise a la date lue dans gcm.def'
-c$$$        endif
-c$$$      ELSE
-c$$$        raz_date = 0
-c$$$      endif
+c      if (annee_ref .ne. anneeref .or. day_ref .ne. dayref) then
+c        write(lunout,*)
+c     .  'GCM: Attention les dates initiales lues dans le fichier'
+c        write(lunout,*)
+c     .  ' restart ne correspondent pas a celles lues dans '
+c        write(lunout,*)' gcm.def'
+c        write(lunout,*)' annee_ref=',annee_ref," anneeref=",anneeref
+c        write(lunout,*)' day_ref=',day_ref," dayref=",dayref
+c        if (raz_date .ne. 1) then
+c          write(lunout,*)
+c     .    'GCM: On garde les dates du fichier restart'
+c        else
+c          annee_ref = anneeref
+c          day_ref = dayref
+c          day_ini = dayref
+c          itau_dyn = 0
+c          itau_phy = 0
+c          time_0 = 0.
+c          write(lunout,*)
+c     .   'GCM: On reinitialise a la date lue dans gcm.def'
+c        endif
+c      ELSE
+c        raz_date = 0
+c      endif
 
 #ifdef CPP_IOIPSL
@@ -486,21 +489,27 @@
 
 #ifdef CPP_IOIPSL
-      if ( 1.eq.1) then
       time_step = zdtvr
-      t_ops = iecri * daysec
-      t_wrt = iecri * daysec
+      if (ok_dyn_ins) then
+        ! initialize output file for instantaneous outputs
+        ! t_ops = iecri * daysec ! do operations every t_ops
+        t_ops =((1.0*iecri)/day_step) * daysec  
+        t_wrt = daysec ! iecri * daysec ! write output every t_wrt
+        t_wrt = daysec ! iecri * daysec ! write output every t_wrt
+        CALL inithist(day_ref,annee_ref,time_step,
+     &              t_ops,t_wrt)
 !      CALL inithist_p(dynhist_file,day_ref,annee_ref,time_step,
 !     .              t_ops, t_wrt, histid, histvid)
-
-      IF (ok_dynzon) THEN 
-         t_ops = iperiod * time_step
-         t_wrt = periodav * daysec
+      endif
+
+      IF (ok_dyn_ave) THEN 
+        ! initialize output file for averaged outputs
+        t_ops = iperiod * time_step ! do operations every t_ops
+        t_wrt = periodav * daysec   ! write output every t_wrt
+        CALL initdynav(day_ref,annee_ref,time_step,
+     &       t_ops,t_wrt)
 !         CALL initdynav_p(dynhistave_file,day_ref,annee_ref,time_step,
 !     .        t_ops, t_wrt, histaveid)
       END IF
       dtav = iperiod*dtvr/daysec
-      endif
-
-
 #endif
 ! #endif of #ifdef CPP_IOIPSL
Index: LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/leapfrog_p.F
===================================================================
--- LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/leapfrog_p.F	(revision 1346)
+++ LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3dpar/leapfrog_p.F	(revision 1357)
@@ -66,5 +66,5 @@
 #include "description.h"
 #include "serre.h"
-#include "com_io_dyn.h"
+!#include "com_io_dyn.h"
 #include "iniprint.h"
 #include "academic.h"
@@ -352,4 +352,5 @@
 c      idissip=1
       IF( purmats ) THEN
+      ! Purely Matsuno time stepping
          IF( MOD(itau,iconser) .EQ.0.AND.  forward    ) conser = .TRUE.
          IF( MOD(itau,idissip ).EQ.0.AND..NOT.forward ) apdiss = .TRUE.
@@ -357,4 +358,5 @@
      s          .and. iflag_phys.EQ.1                 ) apphys = .TRUE.
       ELSE
+      ! Leapfrog/Matsuno time stepping 
          IF( MOD(itau   ,iconser) .EQ. 0              ) conser = .TRUE.
          IF( MOD(itau+1,idissip)  .EQ. 0              ) apdiss = .TRUE.
@@ -362,8 +364,14 @@
       END IF
 
+! Ehouarn: for Shallow Water case (ie: 1 vertical layer),
+!          supress dissipation step
+      if (llm.eq.1) then
+        apdiss=.false.
+      endif
+
 cym    ---> Pour le moment      
 cym      apphys = .FALSE.
       statcl = .FALSE.
-      conser = .FALSE.
+      conser = .FALSE. ! ie: no output of control variables to stdout in //
       
       if (firstCaldyn) then
@@ -974,5 +982,5 @@
 c$OMP BARRIER
        call WaitRequest(Request_Physic)     
-
+c$OMP BARRIER
        call friction_p(ucov,vcov,iphysiq*dtvr)
       ENDIF ! of IF(iflag_phys.EQ.2)
@@ -1091,5 +1099,5 @@
             enddo
 c$OMP END DO NOWAIT            
-       endif
+       endif ! of if (dissip_conservative)
 
        ijb=ij_begin
@@ -1200,5 +1208,5 @@
 c$OMP END MASTER
 c$OMP BARRIER
-      END IF
+      END IF ! of IF(apdiss)
 
 cc$OMP END PARALLEL
@@ -1339,5 +1347,26 @@
               ENDIF !ok_dynzon
 #endif
-            ENDIF
+               IF (ok_dyn_ave) THEN
+!$OMP MASTER
+#ifdef CPP_IOIPSL
+! Ehouarn: Gather fields and make master send to output
+                call Gather_Field(vcov,ip1jm,llm,0)
+                call Gather_Field(ucov,ip1jmp1,llm,0)
+                call Gather_Field(teta,ip1jmp1,llm,0)
+		call Gather_Field(phi,ip1jmp1,llm,0)
+                do iq=1,nqtot
+                  call Gather_Field(q(1,1,iq),ip1jmp1,llm,0)
+                enddo
+		call Gather_Field(masse,ip1jmp1,llm,0)
+                call Gather_Field(ps,ip1jmp1,1,0)
+		call Gather_Field(phis,ip1jmp1,1,0)
+                if (mpi_rank==0) then
+                 CALL writedynav(itau,vcov,
+     &                 ucov,teta,pk,phi,q,masse,ps,phis)
+		endif
+#endif
+!$OMP END MASTER
+               ENDIF ! of IF (ok_dyn_ave)
+            ENDIF ! of IF((MOD(itau,iperiod).EQ.0).OR.(itau.EQ.itaufin))
 
 c-----------------------------------------------------------------------
@@ -1345,7 +1374,7 @@
 c   ------------------------------
 
-c      IF( MOD(itau,iecri         ).EQ.0) THEN
-
-            IF( MOD(itau,iecri*day_step).EQ.0) THEN
+            IF( MOD(itau,iecri).EQ.0) THEN
+             ! Ehouarn: output only during LF or Backward Matsuno
+	     if (leapf.or.(.not.leapf.and.(.not.forward))) then
 c$OMP BARRIER
 c$OMP MASTER
@@ -1381,8 +1410,27 @@
         
 #ifdef CPP_IOIPSL
- 
+              if (ok_dyn_ins) then
+! Ehouarn: Gather fields and make master write to output
+                call Gather_Field(vcov,ip1jm,llm,0)
+                call Gather_Field(ucov,ip1jmp1,llm,0)
+                call Gather_Field(teta,ip1jmp1,llm,0)
+		call Gather_Field(phi,ip1jmp1,llm,0)
+                do iq=1,nqtot
+                  call Gather_Field(q(1,1,iq),ip1jmp1,llm,0)
+                enddo
+		call Gather_Field(masse,ip1jmp1,llm,0)
+                call Gather_Field(ps,ip1jmp1,1,0)
+		call Gather_Field(phis,ip1jmp1,1,0)
+                if (mpi_rank==0) then
+	         CALL writehist(itau,vcov,ucov,teta,phi,q,masse,ps,phis)
+		endif
 !              CALL writehist_p(histid,histvid, itau,vcov, 
 !     &                         ucov,teta,phi,q,masse,ps,phis)
-
+! or use writefield_p
+!      call WriteField_p('ucov',reshape(ucov,(/iip1,jmp1,llm/)))
+!      call WriteField_p('vcov',reshape(vcov,(/iip1,jjm,llm/)))
+!      call WriteField_p('teta',reshape(teta,(/iip1,jmp1,llm/)))
+!      call WriteField_p('ps',reshape(ps,(/iip1,jmp1/)))
+              endif ! of if (ok_dyn_ins)
 #endif
 ! For some Grads outputs of fields
@@ -1401,4 +1449,5 @@
               endif ! of if (output_grads_dyn)
 c$OMP END MASTER
+             endif ! of if (leapf.or.(.not.leapf.and.(.not.forward)))
             ENDIF ! of IF(MOD(itau,iecri).EQ.0)
 
@@ -1479,5 +1528,5 @@
                GO TO 2
 
-            ELSE ! of IF(forward)
+            ELSE ! of IF(forward) i.e. backward step
 
               IF(MOD(itau,iperiod).EQ.0 .OR. itau.EQ.itaufin) THEN
@@ -1490,10 +1539,8 @@
                IF (ok_dynzon) THEN
 c$OMP BARRIER
-
                call Register_Hallo(vcov,ip1jm,llm,1,0,0,1,TestRequest)
                call SendRequest(TestRequest)
 c$OMP BARRIER
                call WaitRequest(TestRequest)
-
 c$OMP BARRIER
 c$OMP MASTER
@@ -1505,9 +1552,31 @@
                END IF !ok_dynzon
 #endif
+               IF (ok_dyn_ave) THEN
+!$OMP MASTER
+#ifdef CPP_IOIPSL
+! Ehouarn: Gather fields and make master send to output
+                call Gather_Field(vcov,ip1jm,llm,0)
+                call Gather_Field(ucov,ip1jmp1,llm,0)
+                call Gather_Field(teta,ip1jmp1,llm,0)
+		call Gather_Field(phi,ip1jmp1,llm,0)
+                do iq=1,nqtot
+                  call Gather_Field(q(1,1,iq),ip1jmp1,llm,0)
+                enddo
+		call Gather_Field(masse,ip1jmp1,llm,0)
+                call Gather_Field(ps,ip1jmp1,1,0)
+		call Gather_Field(phis,ip1jmp1,1,0)
+                if (mpi_rank==0) then
+                 CALL writedynav(itau,vcov,
+     &                 ucov,teta,pk,phi,q,masse,ps,phis)
+		endif
+#endif
+!$OMP END MASTER
+               ENDIF ! of IF (ok_dyn_ave)
+
               ENDIF ! of IF(MOD(itau,iperiod).EQ.0 .OR. itau.EQ.itaufin)
 
 
-c               IF(MOD(itau,iecri         ).EQ.0) THEN
-              IF(MOD(itau,iecri*day_step).EQ.0) THEN
+               IF(MOD(itau,iecri         ).EQ.0) THEN
+c              IF(MOD(itau,iecri*day_step).EQ.0) THEN
 c$OMP BARRIER
 c$OMP MASTER
@@ -1542,7 +1611,22 @@
 
 #ifdef CPP_IOIPSL
-
+              if (ok_dyn_ins) then
+! Ehouarn: Gather fields and make master send to output
+                call Gather_Field(vcov,ip1jm,llm,0)
+                call Gather_Field(ucov,ip1jmp1,llm,0)
+                call Gather_Field(teta,ip1jmp1,llm,0)
+		call Gather_Field(phi,ip1jmp1,llm,0)
+                do iq=1,nqtot
+                  call Gather_Field(q(1,1,iq),ip1jmp1,llm,0)
+                enddo
+		call Gather_Field(masse,ip1jmp1,llm,0)
+                call Gather_Field(ps,ip1jmp1,1,0)
+		call Gather_Field(phis,ip1jmp1,1,0)
+                if (mpi_rank==0) then
+	         CALL writehist(itau,vcov,ucov,teta,phi,q,masse,ps,phis)
+		endif
 !                CALL writehist_p(histid, histvid, itau,vcov , 
 !     &                           ucov,teta,phi,q,masse,ps,phis)
+              endif ! of if (ok_dyn_ins)
 #endif
 ! For some Grads output (but does it work?)
@@ -1562,5 +1646,5 @@
 
 c$OMP END MASTER
-              ENDIF ! of IF(MOD(itau,iecri*day_step).EQ.0)
+              ENDIF ! of IF(MOD(itau,iecri).EQ.0)
 
               IF(itau.EQ.itaufin) THEN
