Index: trunk/LMDZ.MARS/libf/phymars/writediagfi.F
===================================================================
--- trunk/LMDZ.MARS/libf/phymars/writediagfi.F	(revision 3855)
+++ trunk/LMDZ.MARS/libf/phymars/writediagfi.F	(revision 3870)
@@ -74,5 +74,5 @@
       REAL area((nbp_lon+1),nbp_lat)
 
-      integer isample
+      integer, save :: isample
       integer ierr,ierr2
       integer i,j,l, ig0
@@ -122,9 +122,4 @@
 
 !***************************************************************
-! Compute the output rate
-
-      isample=steps_per_sol/outputs_per_sol
-
-!***************************************************************
 
 ! At very first call, check if there is a "diagfi.def" to use and read it
@@ -132,4 +127,7 @@
       IF (firstcall) THEN
          firstcall=.false.
+
+         ! Compute the output rate
+         isample=steps_per_sol/outputs_per_sol
 
 !$OMP MASTER
@@ -251,15 +249,7 @@
          ENDIF
 
+         ierr= NF_CLOSE(nid) ! Close the NETCDF file once initialized
+
          endif ! of if (is_master)
-
-      else
-
-         if (is_master) then
-           ! only the master is required to do this
-
-           ! Open the NetCDF file
-           ierr = NF_OPEN(fichnom,NF_WRITE,nid)
-         endif ! of if (is_master)
-
       endif ! if (firstnom.eq.'1234567890')
 
@@ -276,35 +266,37 @@
 
       if ( MOD(zitau+1,isample) .eq.0.) then
-
+      
 ! Compute/write/extend 'Time' coordinate (date given in days)
 ! (done every "first call" (at given time level) to writediagfi)
 ! Note: date is incremented as 1 step ahead of physics time
 !--------------------------------------------------------
-
         if (is_master) then
-           ! only the master is required to do this
-        if (nom.eq.firstnom) then
-        ! We have identified a "first call" (at given date)
-           ntime=ntime+1 ! increment # of stored time steps
-           ! compute corresponding date (in days and fractions thereof)
-           date=(zitau +1.)/steps_per_sol
-           ! Get NetCDF ID of 'Time' variable
-           ierr= NF_INQ_VARID(nid,"Time",varid)
-           ! Write (append) the new date to the 'Time' array
-!#ifdef NC_DOUBLE
-!           ierr= NF_PUT_VARA_DOUBLE(nid,varid,[ntime],[1],[date])
+          ! only the master is required to do this
+
+          ! Time to write data, open NETCDF file
+          ierr=NF_OPEN(fichnom,NF_WRITE,nid)
+
+          if (nom.eq.firstnom) then
+            ! We have identified a "first call" (at given date)
+            ntime=ntime+1 ! increment # of stored time steps
+            ! compute corresponding date (in days and fractions thereof)
+            date=(zitau +1.)/steps_per_sol
+            ! Get NetCDF ID of 'Time' variable
+            ierr= NF_INQ_VARID(nid,"Time",varid)
+            ! Write (append) the new date to the 'Time' array
+      !#ifdef NC_DOUBLE
+            ! ierr= NF_PUT_VARA_DOUBLE(nid,varid,[ntime],[1],[date])
 !#else
-           ierr= NF_PUT_VARA_REAL(nid,varid,[ntime],[1],[date])
+            ierr= NF_PUT_VARA_REAL(nid,varid,[ntime],[1],[date])
 !#endif
-           if (ierr.ne.NF_NOERR) then
-              write(*,*) "***** PUT_VAR matter in writediagfi_nc"
-              write(*,*) "***** with time"
-              write(*,*) 'ierr=', ierr,": ",NF_STRERROR(ierr)  
-c             call abort
-           endif
-
-           write(6,*)'WRITEDIAGFI: date= ', date
-        end if ! of if (nom.eq.firstnom)
-
+            if (ierr.ne.NF_NOERR) then
+                write(*,*) "***** PUT_VAR matter in writediagfi_nc"
+                write(*,*) "***** with time"
+                write(*,*) 'ierr=', ierr,": ",NF_STRERROR(ierr)  
+c               call abort
+            endif
+
+            write(6,*)'WRITEDIAGFI: date= ', date
+          end if ! of if (nom.eq.firstnom)
         endif ! of if (is_master)
 
@@ -621,9 +613,10 @@
         endif ! of if (dim.eq.3) elseif(dim.eq.2)...
 
+        if (is_master) then
+          ! Close until next variable to dump or next dump iteration
+          ierr= NF_CLOSE(nid)
+        endif
+
       endif ! of if ( MOD(zitau+1,isample) .eq.0.)
 
-      if (is_master) then
-        ierr= NF_CLOSE(nid)
-      endif
-
       end
Index: trunk/LMDZ.MARS/libf/phymars/writediagmicrofi.F
===================================================================
--- trunk/LMDZ.MARS/libf/phymars/writediagmicrofi.F	(revision 3855)
+++ trunk/LMDZ.MARS/libf/phymars/writediagmicrofi.F	(revision 3870)
@@ -81,5 +81,5 @@
       REAL area((nbp_lon+1),nbp_lat)
 
-      integer isample
+      integer, save :: isample
       integer ierr,ierr2
       integer i,j,l, ig0
@@ -124,15 +124,11 @@
 #endif
 
-!***************************************************************
-! Compute the output rate
-
-      isample = steps_per_sol/outputs_per_sol
-
-!***************************************************************
-
 ! At very first call, check if there is a "diagfi.def" to use and read it
 ! -----------------------------------------------------------------------
       IF (firstcall) THEN
          firstcall=.false.
+         
+         ! Compute the output rate
+         isample = steps_per_sol/outputs_per_sol
 
 !$OMP MASTER
@@ -264,15 +260,7 @@
          ENDIF
 
+         ierr= NF_CLOSE(nid) ! Close the NETCDF file once initialized
+
          endif ! of if (is_master)
-
-      else
-
-         if (is_master) then
-           ! only the master is required to do this
-
-           ! Open the NetCDF file
-           ierr = NF_OPEN(fichnom,NF_WRITE,nid)
-         endif ! of if (is_master)
-
       endif ! if (firstnom.eq.'1234567890')
 
@@ -297,4 +285,8 @@
         if (is_master) then
            ! only the master is required to do this
+
+           ! Time to write data, open NETCDF file
+           ierr=NF_OPEN(fichnom,NF_WRITE,nid)
+
         if ((nom.eq.firstnom) .and. (microstep.eq.1)) then
         ! We have identified a "first call" (at given date)
@@ -669,9 +661,10 @@
         endif ! of if (dim.eq.3) elseif(dim.eq.2)...
 
+        if (is_master) then
+          ! Close until next variable to dump or next dump iteration
+          ierr= NF_CLOSE(nid)
+        endif
+
       endif ! of if ( MOD(zitau+1,isample) .eq.0.)
-
-      if (is_master) then
-        ierr= NF_CLOSE(nid)
-      endif
-
+      
       end
Index: trunk/LMDZ.MARS/libf/phymars/writediagsoil.F90
===================================================================
--- trunk/LMDZ.MARS/libf/phymars/writediagsoil.F90	(revision 3855)
+++ trunk/LMDZ.MARS/libf/phymars/writediagsoil.F90	(revision 3870)
@@ -159,4 +159,6 @@
      call iniwritesoil(nid,ngrid,inertiafi_glo(1,:),areafi_glo(1),1,1,nsoilmx,mlayer)
    endif
+   
+   ierr= NF_CLOSE(nid) ! Close the NETCDF file once initialized
 
   endif ! of if (is_master)
@@ -164,10 +166,4 @@
   ! set zitau to -1 to be compatible with zitau incrementation step below
   zitau=-1
-  
-else
-  ! If not an initialization call, simply open the NetCDF file
-  if (is_master) then
-   ierr=NF_OPEN(filename,NF_WRITE,nid)
-  endif
 endif ! of if (firstname.eq."1234567890")
 
@@ -180,18 +176,21 @@
 ! 3. Write data, if the time index matches the sample rate
 if (mod(zitau+1,isample).eq.0) then
+  if (is_master) then
+    ! only the master is required to do this
+
+    ! Time to write data, open NETCDF file
+    ierr=NF_OPEN(filename,NF_WRITE,nid)
 
 ! 3.1 If first call at this date, update 'time' variable
-  if (name.eq.firstname) then
-    ntime=ntime+1
-    date=float(zitau+1)/float(steps_per_sol)
-    
-    if (is_master) then
-     ! Get NetCDF ID for "time"
-     ierr=NF_INQ_VARID(nid,"time",varid)
-     ! Add the current value of date to the "time" array
+    if (name.eq.firstname) then
+      ntime=ntime+1
+      date=float(zitau+1)/float(steps_per_sol)
+      ! Get NetCDF ID for "time"
+      ierr=NF_INQ_VARID(nid,"time",varid)
+      ! Add the current value of date to the "time" array
 !#ifdef NC_DOUBLE
-!     ierr=NF_PUT_VARA_DOUBLE(nid,varid,ntime,1,date)
+      ! ierr=NF_PUT_VARA_DOUBLE(nid,varid,ntime,1,date)
 !#else
-     ierr=NF_PUT_VARA_REAL(nid,varid,[ntime],[1],[date])
+      ierr=NF_PUT_VARA_REAL(nid,varid,[ntime],[1],[date])
 !#endif
      if (ierr.ne.NF_NOERR) then
@@ -199,6 +198,6 @@
       call abort_physic("writediagsoil","failed writing time",1) 
      endif
-    endif ! of if (is_master)
-  endif ! of if (name.eq.firstname)
+    endif ! of if (name.eq.firstname)
+  endif ! of if (is_master)
 
 ! 3.2 Write the variable to the NetCDF file
@@ -409,11 +408,13 @@
 
 endif ! of if (dimpx.eq.3) elseif (dimpx.eq.2) ...
+
+! 4. Close the NetCDF file
+  if (is_master) then
+    ! Close until next variable to dump or next dump iteration
+    ierr= NF_CLOSE(nid)
+  endif
+
 endif ! of if (mod(zitau+1,isample).eq.0)
 
-! 4. Close the NetCDF file
-if (is_master) then
-  ierr=NF_CLOSE(nid)
-endif
-
 end subroutine writediagsoil
 
