Index: LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3d/leapfrog.F
===================================================================
--- LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3d/leapfrog.F	(revision 1299)
+++ LMDZ4/branches/LMDZ4V5.0-dev/libf/dyn3d/leapfrog.F	(revision 1357)
@@ -60,5 +60,5 @@
 #include "description.h"
 #include "serre.h"
-#include "com_io_dyn.h"
+!#include "com_io_dyn.h"
 #include "iniprint.h"
 #include "academic.h"
@@ -197,10 +197,10 @@
 
       itau = 0
-c$$$      iday = day_ini+itau/day_step
-c$$$      time = REAL(itau-(iday-day_ini)*day_step)/day_step+time_0
-c$$$         IF(time.GT.1.) THEN
-c$$$          time = time-1.
-c$$$          iday = iday+1
-c$$$         ENDIF
+c      iday = day_ini+itau/day_step
+c      time = REAL(itau-(iday-day_ini)*day_step)/day_step+time_0
+c         IF(time.GT.1.) THEN
+c          time = time-1.
+c          iday = iday+1
+c         ENDIF
 
 
@@ -276,4 +276,5 @@
 
       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.
@@ -281,8 +282,15 @@
      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.
          IF( MOD(itau+1,iphysiq).EQ.0.AND.iflag_phys.EQ.1) apphys=.TRUE.
       END IF
+
+! Ehouarn: for Shallow Water case (ie: 1 vertical layer),
+!          supress dissipation step
+      if (llm.eq.1) then
+        apdiss=.false.
+      endif
 
 c-----------------------------------------------------------------------
@@ -522,10 +530,10 @@
             IF(forward. OR. leapf) THEN
               itau= itau + 1
-c$$$              iday= day_ini+itau/day_step
-c$$$              time= REAL(itau-(iday-day_ini)*day_step)/day_step+time_0
-c$$$                IF(time.GT.1.) THEN
-c$$$                  time = time-1.
-c$$$                  iday = iday+1
-c$$$                ENDIF
+c              iday= day_ini+itau/day_step
+c              time= REAL(itau-(iday-day_ini)*day_step)/day_step+time_0
+c                IF(time.GT.1.) THEN
+c                  time = time-1.
+c                  iday = iday+1
+c                ENDIF
             ENDIF
 
@@ -559,12 +567,16 @@
                IF (ok_dynzon) THEN
 #ifdef CPP_IOIPSL
-!                  CALL writedynav(histaveid, itau,vcov ,
-!     ,                 ucov,teta,pk,phi,q,masse,ps,phis)
-                  CALL bilan_dyn (2,dtvr*iperiod,dtvr*day_step*periodav,
-     ,                 ps,masse,pk,pbaru,pbarv,teta,phi,ucov,vcov,q)
+                 CALL bilan_dyn(2,dtvr*iperiod,dtvr*day_step*periodav,
+     &                 ps,masse,pk,pbaru,pbarv,teta,phi,ucov,vcov,q)
 #endif
                END IF
-
-            ENDIF
+               IF (ok_dyn_ave) THEN
+#ifdef CPP_IOIPSL
+                 CALL writedynav(itau,vcov,
+     &                 ucov,teta,pk,phi,q,masse,ps,phis)
+#endif
+               ENDIF
+
+            ENDIF ! of IF((MOD(itau,iperiod).EQ.0).OR.(itau.EQ.itaufin))
 
 c-----------------------------------------------------------------------
@@ -572,7 +584,7 @@
 c   ------------------------------
 
-            IF( MOD(itau,iecri         ).EQ.0) THEN
-c           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
               nbetat = nbetatdem
               CALL geopot(ip1jmp1,teta,pk,pks,phis,phi)
@@ -583,12 +595,19 @@
               enddo
 #ifdef CPP_IOIPSL
-c             CALL writehist(histid,histvid,itau,vcov, 
-c     &                      ucov,teta,phi,q,masse,ps,phis)
+              if (ok_dyn_ins) then
+!               write(lunout,*) "leapfrog: call writehist, itau=",itau
+	       CALL writehist(itau,vcov,ucov,teta,phi,q,masse,ps,phis)
+!               call WriteField('ucov',reshape(ucov,(/iip1,jmp1,llm/)))
+!               call WriteField('vcov',reshape(vcov,(/iip1,jjm,llm/)))
+!              call WriteField('teta',reshape(teta,(/iip1,jmp1,llm/)))
+!               call WriteField('ps',reshape(ps,(/iip1,jmp1/)))
+!               call WriteField('masse',reshape(masse,(/iip1,jmp1,llm/)))
+              endif ! of if (ok_dyn_ins)
 #endif
 ! For some Grads outputs of fields
-             if (output_grads_dyn) then
+              if (output_grads_dyn) then
 #include "write_grads_dyn.h"
-             endif
-
+              endif
+             endif ! of if (leapf.or.(.not.leapf.and.(.not.forward)))
             ENDIF ! of IF(MOD(itau,iecri).EQ.0)
 
@@ -645,11 +664,11 @@
 
              itau =  itau + 1
-c$$$             iday = day_ini+itau/day_step
-c$$$             time = REAL(itau-(iday-day_ini)*day_step)/day_step+time_0
-c$$$
-c$$$                  IF(time.GT.1.) THEN
-c$$$                   time = time-1.
-c$$$                   iday = iday+1
-c$$$                  ENDIF
+c             iday = day_ini+itau/day_step
+c             time = REAL(itau-(iday-day_ini)*day_step)/day_step+time_0
+c
+c                  IF(time.GT.1.) THEN
+c                   time = time-1.
+c                   iday = iday+1
+c                  ENDIF
 
                forward =  .FALSE.
@@ -660,5 +679,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
@@ -671,10 +690,14 @@
                IF (ok_dynzon) THEN 
 #ifdef CPP_IOIPSL
-!                  CALL writedynav(histaveid, itau,vcov ,
-!     ,                 ucov,teta,pk,phi,q,masse,ps,phis)
-                  CALL bilan_dyn (2,dtvr*iperiod,dtvr*day_step*periodav,
-     ,                 ps,masse,pk,pbaru,pbarv,teta,phi,ucov,vcov,q)
-#endif
-               END IF
+                 CALL bilan_dyn(2,dtvr*iperiod,dtvr*day_step*periodav,
+     &                 ps,masse,pk,pbaru,pbarv,teta,phi,ucov,vcov,q)
+#endif
+               ENDIF
+               IF (ok_dyn_ave) THEN
+#ifdef CPP_IOIPSL
+                 CALL writedynav(itau,vcov,
+     &                 ucov,teta,pk,phi,q,masse,ps,phis)
+#endif
+               ENDIF
 
               ENDIF ! of IF(MOD(itau,iperiod).EQ.0 .OR. itau.EQ.itaufin)
@@ -690,6 +713,9 @@
                 enddo
 #ifdef CPP_IOIPSL
-c               CALL writehist( histid, histvid, itau,vcov , 
-c    &                           ucov,teta,phi,q,masse,ps,phis)
+              if (ok_dyn_ins) then
+!                write(lunout,*) "leapfrog: call writehist (b)",
+!     &                        itau,iecri
+		CALL writehist(itau,vcov,ucov,teta,phi,q,masse,ps,phis)
+              endif ! of if (ok_dyn_ins)
 #endif
 ! For some Grads outputs
