Index: LMDZ4/trunk/libf/dyn3dpar/leapfrog_p.F
===================================================================
--- LMDZ4/trunk/libf/dyn3dpar/leapfrog_p.F	(revision 1286)
+++ LMDZ4/trunk/libf/dyn3dpar/leapfrog_p.F	(revision 1403)
@@ -20,4 +20,5 @@
        USE guide_p_mod, ONLY : guide_main
        USE getparam
+       USE control_mod
 
       IMPLICIT NONE
@@ -62,9 +63,8 @@
 #include "logic.h"
 #include "temps.h"
-#include "control.h"
 #include "ener.h"
 #include "description.h"
 #include "serre.h"
-#include "com_io_dyn.h"
+!#include "com_io_dyn.h"
 #include "iniprint.h"
 #include "academic.h"
@@ -212,5 +212,5 @@
       itau = 0
 !      iday = day_ini+itau/day_step
-!      time = FLOAT(itau-(iday-day_ini)*day_step)/day_step+time_0
+!      time = REAL(itau-(iday-day_ini)*day_step)/day_step+time_0
 !         IF(time.GT.1.) THEN
 !          time = time-1.
@@ -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
@@ -677,6 +685,8 @@
          call suspend_timer(timer_caldyn)
 
+        if (prt_level >= 10) then
          write(lunout,*)
      &   'leapfrog_p: Entree dans la physique : Iteration No ',true_itau
+        endif
 c$OMP END MASTER
 
@@ -964,6 +974,10 @@
        ijb=ij_begin
        ije=ij_end
-       teta(ijb:ije,:)=teta(ijb:ije,:)
-     s  -iphysiq*dtvr*(teta(ijb:ije,:)-tetarappel(ijb:ije,:))/taurappel
+!$OMP DO SCHEDULE(STATIC,OMP_CHUNK) 
+       do l=1,llm
+       teta(ijb:ije,l)=teta(ijb:ije,l)
+     &  -iphysiq*dtvr*(teta(ijb:ije,l)-tetarappel(ijb:ije,l))/taurappel
+       enddo
+!$OMP END DO
 
        call Register_Hallo(ucov,ip1jmp1,llm,0,1,1,0,Request_Physic)
@@ -972,6 +986,9 @@
 c$OMP BARRIER
        call WaitRequest(Request_Physic)     
-
+c$OMP BARRIER
+!$OMP MASTER
        call friction_p(ucov,vcov,iphysiq*dtvr)
+!$OMP END MASTER
+!$OMP BARRIER
       ENDIF ! of IF(iflag_phys.EQ.2)
 
@@ -1089,5 +1106,5 @@
             enddo
 c$OMP END DO NOWAIT            
-       endif
+       endif ! of if (dissip_conservative)
 
        ijb=ij_begin
@@ -1198,5 +1215,5 @@
 c$OMP END MASTER
 c$OMP BARRIER
-      END IF
+      END IF ! of IF(apdiss)
 
 cc$OMP END PARALLEL
@@ -1280,5 +1297,5 @@
               itau= itau + 1
 !              iday= day_ini+itau/day_step
-!              time= FLOAT(itau-(iday-day_ini)*day_step)/day_step+time_0
+!              time= REAL(itau-(iday-day_ini)*day_step)/day_step+time_0
 !                IF(time.GT.1.) THEN
 !                  time = time-1.
@@ -1337,5 +1354,27 @@
               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(pk,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-----------------------------------------------------------------------
@@ -1343,7 +1382,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
@@ -1379,8 +1418,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
@@ -1399,4 +1457,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)
 
@@ -1458,5 +1517,5 @@
              itau =  itau + 1
 !             iday = day_ini+itau/day_step
-!             time = FLOAT(itau-(iday-day_ini)*day_step)/day_step+time_0
+!             time = REAL(itau-(iday-day_ini)*day_step)/day_step+time_0
 !
 !                  IF(time.GT.1.) THEN
@@ -1477,5 +1536,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
@@ -1488,10 +1547,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
@@ -1503,9 +1560,32 @@
                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(pk,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
@@ -1540,7 +1620,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?)
@@ -1560,5 +1655,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
