Index: /trunk/LMDZ.GENERIC/changelog.txt
===================================================================
--- /trunk/LMDZ.GENERIC/changelog.txt	(revision 3927)
+++ /trunk/LMDZ.GENERIC/changelog.txt	(revision 3928)
@@ -2103,2 +2103,8 @@
 option "Lmodif=2" when calling tabi from newstart to skip some checks
 which only make sense when called during a regular GCM run.
+
+== 13/10/2025 == JM
+Adding a slow_diagfi flag to the run.def/rcm1d.def file for 1D models only. When False, the netcdf
+file is opened/closed once, thus saving significant computing time. When true,
+the opening frequency is at output frequency (recommended in debug mode). Also
+fixing a redundant loop on tracers when writing outputs in physiq_mod.
Index: /trunk/LMDZ.GENERIC/libf/phystd/inifis_mod.F90
===================================================================
--- /trunk/LMDZ.GENERIC/libf/phystd/inifis_mod.F90	(revision 3927)
+++ /trunk/LMDZ.GENERIC/libf/phystd/inifis_mod.F90	(revision 3928)
@@ -17,5 +17,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, &
+  use time_phylmdz_mod, only: diagfi_output_rate, slow_diagfi, &
                               init_time, daysec, dtphys
   use comcstfi_mod, only: rad, cpp, g, r, rcp, &
@@ -155,4 +155,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)//&
        ": Run with or without atm mass update "//&
Index: /trunk/LMDZ.GENERIC/libf/phystd/physiq_mod.F90
===================================================================
--- /trunk/LMDZ.GENERIC/libf/phystd/physiq_mod.F90	(revision 3927)
+++ /trunk/LMDZ.GENERIC/libf/phystd/physiq_mod.F90	(revision 3928)
@@ -2705,44 +2705,44 @@
             ! call write_output(trim(noms(iq))//'_surf',trim(noms(iq))//'_surf',  &
             !              'kg m^-2',qsurf(:,iq) )
-
-            if(watercond.or.CLFvarying)then
-               call write_output("rneb_man","H2O cloud fraction (conv)"," ",rneb_man)
-               call write_output("rneb_lsc","H2O cloud fraction (large scale)"," ",rneb_lsc)
-               call write_output("CLF","H2O cloud fraction"," ",cloudfrac)
-               call write_output("CLFt","H2O column cloud fraction"," ",totcloudfrac)
-               call write_output("RH","relative humidity"," ",RH)
-               call write_output("vteta","virtual potential temperature","K",zh * (1.+(1./epsi-1.) * &
-                       zq(1:ngrid,1:nlayer,1)))
-            endif
-
-            if(waterrain)then
-               call write_output("rain","rainfall","kg m-2 s-1",zdqsrain)
-               call write_output("snow","snowfall","kg m-2 s-1",zdqssnow)
-               call write_output("reevap","reevaporation of precipitation","kg m-2 s-1",reevap_precip)
-            endif
-
-            if(generic_condensation)then
-               call write_output("CLF","GCS cloud fraction"," ",cloudfrac)
-               !AF24: TODO fix rneb_generic and RH_generic failure with write_output()
-               ! call write_output("rneb_generic","GCS cloud fraction (generic condensation)"," ",rneb_generic)
-               ! call write_output("RH_generic","GCS relative humidity"," ",RH_generic)
-               call writediagfi(ngrid,"rneb_generic","GCS cloud fraction (generic condensation)"," ",3,rneb_generic)
-               call writediagfi(ngrid,"RH_generic","GCS relative humidity"," ",3,RH_generic)
-               call write_output("vteta","virtual potential temperature","K",zh * (1.+(1./epsi_generic-1.) * &
-                       zq(1:ngrid,1:nlayer,1) ))
-            endif
-
-            if(generic_rain)then
-               call write_output("rain","generic rainfall","kg m-2 s-1",zdqsrain_generic)
-               call write_output("snow","generic snowfall","kg m-2 s-1",zdqssnow_generic)
-               call write_output("reevap","generic reevaporation of precipitation","kg m-2 s-1",reevap_precip_generic)
-            endif
-
-            if((hydrology).and.(.not.ok_slab_ocean))then
-               call write_output("hice","oceanic ice height","m",hice)
-            endif
-
-            call write_output("tau_col","Total aerosol optical depth","[]",tau_col)
          enddo ! end of 'nq' loop
+
+         if(watercond.or.CLFvarying)then
+            call write_output("rneb_man","H2O cloud fraction (conv)"," ",rneb_man)
+            call write_output("rneb_lsc","H2O cloud fraction (large scale)"," ",rneb_lsc)
+            call write_output("CLF","H2O cloud fraction"," ",cloudfrac)
+            call write_output("CLFt","H2O column cloud fraction"," ",totcloudfrac)
+            call write_output("RH","relative humidity"," ",RH)
+            call write_output("vteta","virtual potential temperature","K",zh * (1.+(1./epsi-1.) * &
+                     zq(1:ngrid,1:nlayer,1)))
+         endif
+
+         if(waterrain)then
+            call write_output("rain","rainfall","kg m-2 s-1",zdqsrain)
+            call write_output("snow","snowfall","kg m-2 s-1",zdqssnow)
+            call write_output("reevap","reevaporation of precipitation","kg m-2 s-1",reevap_precip)
+         endif
+
+         if(generic_condensation)then
+            call write_output("CLF","GCS cloud fraction"," ",cloudfrac)
+            !AF24: TODO fix rneb_generic and RH_generic failure with write_output()
+            ! call write_output("rneb_generic","GCS cloud fraction (generic condensation)"," ",rneb_generic)
+            ! call write_output("RH_generic","GCS relative humidity"," ",RH_generic)
+            call writediagfi(ngrid,"rneb_generic","GCS cloud fraction (generic condensation)"," ",3,rneb_generic)
+            call writediagfi(ngrid,"RH_generic","GCS relative humidity"," ",3,RH_generic)
+            call write_output("vteta","virtual potential temperature","K",zh * (1.+(1./epsi_generic-1.) * &
+                     zq(1:ngrid,1:nlayer,1) ))
+         endif
+
+         if(generic_rain)then
+            call write_output("rain","generic rainfall","kg m-2 s-1",zdqsrain_generic)
+            call write_output("snow","generic snowfall","kg m-2 s-1",zdqssnow_generic)
+            call write_output("reevap","generic reevaporation of precipitation","kg m-2 s-1",reevap_precip_generic)
+         endif
+
+         if((hydrology).and.(.not.ok_slab_ocean))then
+            call write_output("hice","oceanic ice height","m",hice)
+         endif
+
+         call write_output("tau_col","Total aerosol optical depth","[]",tau_col)
        endif ! end of 'tracer'
 
Index: /trunk/LMDZ.GENERIC/libf/phystd/time_phylmdz_mod.F90
===================================================================
--- /trunk/LMDZ.GENERIC/libf/phystd/time_phylmdz_mod.F90	(revision 3927)
+++ /trunk/LMDZ.GENERIC/libf/phystd/time_phylmdz_mod.F90	(revision 3928)
@@ -15,4 +15,8 @@
                                     ! (set via inifis)
 !$OMP THREADPRIVATE(diagfi_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.GENERIC/libf/phystd/writediagfi.F
===================================================================
--- /trunk/LMDZ.GENERIC/libf/phystd/writediagfi.F	(revision 3927)
+++ /trunk/LMDZ.GENERIC/libf/phystd/writediagfi.F	(revision 3928)
@@ -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
@@ -78,6 +78,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
@@ -85,5 +88,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
@@ -127,4 +131,11 @@
          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
   !      Open diagfi.def definition file if there is one:
@@ -245,16 +256,18 @@
          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)
@@ -277,5 +290,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)
@@ -614,9 +641,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,diagfi_output_rate) .eq.0.)
 
-      if (is_master) then
-        ierr= NF_CLOSE(nid)
-      endif
-
       end
Index: /trunk/LMDZ.GENERIC/libf/phystd/writediagspecIR.F
===================================================================
--- /trunk/LMDZ.GENERIC/libf/phystd/writediagspecIR.F	(revision 3927)
+++ /trunk/LMDZ.GENERIC/libf/phystd/writediagspecIR.F	(revision 3928)
@@ -181,12 +181,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')
 
@@ -203,4 +200,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)
@@ -336,10 +338,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.GENERIC/libf/phystd/writediagspecVI.F
===================================================================
--- /trunk/LMDZ.GENERIC/libf/phystd/writediagspecVI.F	(revision 3927)
+++ /trunk/LMDZ.GENERIC/libf/phystd/writediagspecVI.F	(revision 3928)
@@ -180,12 +180,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')
 
@@ -208,4 +205,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
@@ -335,10 +337,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
