Index: trunk/LMDZ.PLUTO/libf/phypluto/inifis_mod.F90
===================================================================
--- trunk/LMDZ.PLUTO/libf/phypluto/inifis_mod.F90	(revision 4026)
+++ trunk/LMDZ.PLUTO/libf/phypluto/inifis_mod.F90	(revision 4027)
@@ -18,5 +18,5 @@
   use comgeomfi_h, only: totarea, totarea_planet
   use comsoil_h, only: ini_comsoil_h, nsoilmx, lay1_soil, alpha_soil
-  use time_phylmdz_mod, only: diagfi_output_rate,startfi_output_rate, &
+  use time_phylmdz_mod, only: diagfi_output_rate,startfi_output_rate,slow_diagfi, &
                               init_time, daysec, dtphys
   use comcstfi_mod, only: rad, cpp, g, r, rcp, &
@@ -185,4 +185,13 @@
                                diagfi_output_rate
 
+     if ((is_master).and.(ngrid.eq.1)) write(*,*) trim(rname)//&
+       ": Open/close diagfi.nc at output rate (rather than once) ?"
+     slow_diagfi =.false. ! default value, open/close diagfi once (1D only)
+ 	   call getin_p("slow_diagfi",slow_diagfi) ! if true, open/close the diagfi.nc
+ 	                                           ! at output frequency instead of once
+ 	                                           ! (slower but required for debug)
+ 	   if ((is_master).and.(ngrid.eq.1)) write(*,*) trim(rname)//&
+ 	     ": slow_diagfi = ", slow_diagfi
+     
      if (is_master) write(*,*) trim(rname)//&
        ": Output rate for start/startfi.nc files (in physics steps) ?"
Index: trunk/LMDZ.PLUTO/libf/phypluto/time_phylmdz_mod.F90
===================================================================
--- trunk/LMDZ.PLUTO/libf/phypluto/time_phylmdz_mod.F90	(revision 4026)
+++ trunk/LMDZ.PLUTO/libf/phypluto/time_phylmdz_mod.F90	(revision 4027)
@@ -14,4 +14,8 @@
     INTEGER,SAVE :: startfi_output_rate ! mainly for debugging
 !$OMP THREADPRIVATE(diagfi_output_rate,startfi_output_rate)
+
+    LOGICAL,SAVE :: slow_diagfi ! to handle wether the netcdf file is
+                                ! opened/close once or at output frequency
+!$OMP THREADPRIVATE(slow_diagfi)
 
 CONTAINS
Index: trunk/LMDZ.PLUTO/libf/phypluto/writediagfi.F
===================================================================
--- trunk/LMDZ.PLUTO/libf/phypluto/writediagfi.F	(revision 4026)
+++ trunk/LMDZ.PLUTO/libf/phypluto/writediagfi.F	(revision 4027)
@@ -42,5 +42,5 @@
       use geometry_mod, only: cell_area
       use time_phylmdz_mod, only: diagfi_output_rate,dtphys,daysec
-      use time_phylmdz_mod, only: day_ini
+      use time_phylmdz_mod, only: day_ini, nday, slow_diagfi
       USE mod_phys_lmdz_para, only : is_parallel, is_mpi_root,
      &                               is_master, gather
@@ -79,6 +79,9 @@
 
       integer,save :: zitau=0
-      character(len=40),save :: firstnom='1234567890'
-!$OMP THREADPRIVATE(zitau,firstnom)
+      integer,save :: lastzitau=0
+      character(len=27),save :: firstnom='1234567890'
+      character(len=27),save :: prevnom='1234567890'
+      character(len=27),save :: lastnom='1234567890'
+!$OMP THREADPRIVATE(zitau,lastzitau,firstnom,prevnom,lastnom)
 
 ! Ajouts
@@ -86,5 +89,6 @@
 !$OMP THREADPRIVATE(ntime)
       integer :: idim,varid
-      integer :: nid
+      integer, save :: nid
+!$OMP THREADPRIVATE(nid)
       character(len=*),parameter :: fichnom="diagfi.nc"
       integer, dimension(4) :: id
@@ -132,4 +136,12 @@
       IF (firstcall) THEN
          firstcall=.false.
+
+         ! Compute the lastzitau (i.e. last timestep-1)
+	       lastzitau=nday*nint(daysec/dtphys)-1
+	       if (MOD(lastzitau+1,diagfi_output_rate).ne.0.) then
+	         ! If so, output rate is less than once per sol
+	         ! We must adjust zitau to
+	         lastzitau=lastzitau-MOD(lastzitau+1,diagfi_output_rate)
+	       endif 
 
 !$OMP MASTER
@@ -256,16 +268,19 @@
          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')
+
+      ! Find lastnom
+	    if (lastnom.eq.'1234567890') then
+	       if (nom.eq.firstnom) then
+	          if (prevnom.ne.'1234567890') then
+	             lastnom=prevnom
+	          endif
+	       endif
+	       prevnom=nom
+	    endif
 
 ! Increment time index 'zitau' if it is the "fist call" (at given time level)
@@ -288,5 +303,19 @@
 
         if (is_master) then
-           ! only the master is required to do this
+          ! only the master is required to do this
+
+	        ! 1D and slow_diagfi=.false. => open/close once
+	        if ((klon_glo.eq.1).and.(.not.slow_diagfi)) then
+	          ! if the very first time to write, open
+            if ((nom.eq.firstnom).and.
+     &         (((zitau+1)/diagfi_output_rate).eq.1)) then
+	            write(*,*) "Open NETCDF file for firstnom=", firstnom
+	            write(*,*) "zitau=", zitau
+	            ierr=NF_OPEN(fichnom,NF_WRITE,nid) ! open once in all simu
+	          endif
+	        else ! 3D or slow_diagfi=.true. => open/close at output frequency
+	          ierr=NF_OPEN(fichnom,NF_WRITE,nid)           
+	        endif
+
         if (nom.eq.firstnom) then
         ! We have identified a "first call" (at given date)
@@ -625,9 +654,20 @@
         endif ! of if (dim.eq.3) elseif(dim.eq.2)...
 
+        ! Only the master do it
+ 		    if (is_master) then
+ 		      ! 1D and slow_diagfi=.false. => open/close once
+ 		      if ((klon_glo.eq.1).and.(.not.slow_diagfi)) then
+ 		        ! if the very last time to write, close
+ 		        if ((nom.eq.lastnom).and.(zitau.eq.lastzitau)) then
+ 		          write(*,*) "Close NETCDF file for lastnom=",lastnom
+ 		          write(*,*) "zitau=",zitau
+ 		          ierr = NF_CLOSE(nid) ! close once in all simu
+ 		        endif
+ 		      else ! 3D or slow_diagfi=.true. => open/close at output frequency
+ 		        ierr = NF_CLOSE(nid)
+ 		      endif
+ 		    endif
+
       endif ! of if ( MOD(zitau+1,isample) .eq.0.)
 
-      if (is_master) then
-        ierr= NF_CLOSE(nid)
-      endif
-
       end
Index: trunk/LMDZ.PLUTO/libf/phypluto/writediagspecIR.F
===================================================================
--- trunk/LMDZ.PLUTO/libf/phypluto/writediagspecIR.F	(revision 4026)
+++ trunk/LMDZ.PLUTO/libf/phypluto/writediagspecIR.F	(revision 4027)
@@ -180,12 +180,9 @@
            call iniwrite_specIR(nid,day_ini,areafi_glo(1),1,1)
          ENDIF
+         ! Close the NetCDF file
+ 	       ierr= NF_CLOSE(nid)
          endif ! of if (is_master)
 
          zitau = -1 ! initialize zitau
-      else
-         if (is_master) then
-           ! Open the NetCDF file
-           ierr = NF_OPEN(fichnom,NF_WRITE,nid)
-         endif
       endif ! if (firstnom.eq.'1234567890')
 
@@ -202,4 +199,9 @@
 
       if ( MOD(zitau+1,isample) .eq.0.) then
+
+        if (is_master) then
+ 	        ! Open the NetCDF file
+ 	        ierr = NF_OPEN(fichnom,NF_WRITE,nid)
+ 	      endif
 
 ! Compute/write/extend 'Time' coordinate (date given in days)
@@ -334,10 +336,10 @@
         endif ! of if (dimpx.eq.3)
 
+        ! Close the NetCDF file
+ 	      if (is_master) then
+ 	        ierr= NF_CLOSE(nid)
+ 	      endif
+
       endif ! of if ( MOD(zitau+1,isample) .eq.0.)
 
-      ! Close the NetCDF file
-      if (is_master) then
-        ierr= NF_CLOSE(nid)
-      endif
-
       end
Index: trunk/LMDZ.PLUTO/libf/phypluto/writediagspecVI.F
===================================================================
--- trunk/LMDZ.PLUTO/libf/phypluto/writediagspecVI.F	(revision 4026)
+++ trunk/LMDZ.PLUTO/libf/phypluto/writediagspecVI.F	(revision 4027)
@@ -179,12 +179,9 @@
            call iniwrite_specVI(nid,day_ini,areafi_glo(1),1,1)
          ENDIF
+         ! Close the NetCDF file
+ 	       ierr= NF_CLOSE(nid)
          endif ! of if (is_master)
 
          zitau = -1 ! initialize zitau
-      else
-         if (is_master) then
-           ! Open the NetCDF file
-           ierr = NF_OPEN(fichnom,NF_WRITE,nid)
-         endif
       endif ! if (firstnom.eq.'1234567890')
 
@@ -207,4 +204,9 @@
 !       (like the 'histoire' outputs)
 !--------------------------------------------------------
+
+        if (is_master) then
+ 	        ! Open the NetCDF file
+ 	        ierr = NF_OPEN(fichnom,NF_WRITE,nid)
+ 	      endif
 
         if (nom.eq.firstnom) then
@@ -333,10 +335,10 @@
         endif ! of if (dimpx.eq.3)
 
+        ! Close the NetCDF file
+ 	      if (is_master) then
+ 	        ierr= NF_CLOSE(nid)
+ 	      endif
+
       endif ! of if ( MOD(zitau+1,irythme) .eq.0.)
 
-      ! Close the NetCDF file
-      if (is_master) then
-        ierr= NF_CLOSE(nid)
-      endif
-
       end
