Index: trunk/LMDZ.GENERIC/changelog.txt
===================================================================
--- trunk/LMDZ.GENERIC/changelog.txt	(revision 4127)
+++ trunk/LMDZ.GENERIC/changelog.txt	(revision 4130)
@@ -2208,2 +2208,7 @@
 == 12/03/2026 == CM
 Added subsurface outputs for XIOS, and outputting tsoil for XIOS.
+
+== 16/03/2026 == EM
+Follow-up of previous commit, cleanup output of soil variables in diagsoil.nc
+when not using XIOS. Added logical flag "diagsoil" (default .false.) to trigger
+outputs in diagsoil.nc
Index: trunk/LMDZ.GENERIC/libf/phygeneric/inifis_mod.F90
===================================================================
--- trunk/LMDZ.GENERIC/libf/phygeneric/inifis_mod.F90	(revision 4127)
+++ trunk/LMDZ.GENERIC/libf/phygeneric/inifis_mod.F90	(revision 4130)
@@ -25,4 +25,5 @@
   use callkeys_mod
   use wstats_mod, only: callstats
+  use writediagsoil_mod, only: diagsoil
   use ioipsl_getin_p_mod, only : getin_p
   use mod_phys_lmdz_para, only : is_parallel, is_master, bcast
@@ -259,4 +260,10 @@
      call getin_p("callstats",callstats)
      if (is_master) write(*,*) trim(rname)//": callstats = ",callstats
+
+     if (is_master) write(*,*) trim(rname)//&
+       ": Write sub-surface fields in file diagsoil.nc ?"
+     diagsoil=.false. ! default value
+     call getin_p("diagsoil",diagsoil)
+     if (is_master) write(*,*) trim(rname)//": diagsoil = ",diagsoil
 
      if (is_master) write(*,*) trim(rname)//&
Index: trunk/LMDZ.GENERIC/libf/phygeneric/iniwritesoil.F90
===================================================================
--- trunk/LMDZ.GENERIC/libf/phygeneric/iniwritesoil.F90	(revision 4127)
+++ trunk/LMDZ.GENERIC/libf/phygeneric/iniwritesoil.F90	(revision 4130)
@@ -1,2 +1,8 @@
+module iniwritesoil_mod
+
+implicit none
+
+contains
+
 subroutine iniwritesoil(nid,ngrid,inertia,area,nbplon,nbplat)
 
@@ -271,2 +277,4 @@
 
 end subroutine iniwritesoil
+
+end module iniwritesoil_mod
Index: trunk/LMDZ.GENERIC/libf/phygeneric/writediagsoil.F90
===================================================================
--- trunk/LMDZ.GENERIC/libf/phygeneric/writediagsoil.F90	(revision 4127)
+++ trunk/LMDZ.GENERIC/libf/phygeneric/writediagsoil.F90	(revision 4130)
@@ -2,4 +2,8 @@
 
 implicit none
+
+logical,save :: diagsoil ! global flag to trigger generating a diagsoil.nc
+                         ! file or not. Initialized in inifis()
+!$OMP THREADPRIVATE(diagsoil)
 
 contains
@@ -24,4 +28,6 @@
 use mod_grid_phy_lmdz, only : klon_glo, Grid1Dto2D_glo, &
                               nbp_lon, nbp_lat
+use mod_grid_phy_lmdz, only: grid_type, unstructured
+use iniwritesoil_mod, only: iniwritesoil
 
 implicit none
@@ -79,4 +85,15 @@
 #endif
 
+! 0. Do we ouput a diagsoil.nc file? If not just bail out now.
+if(.not.diagsoil) return
+
+! additional check: one can only output diagsoil.nc files
+! in lon-lat case (or 1D)
+if (grid_type==unstructured) then
+  write(*,*) "writediagsoil: Error !!!"
+  write(*,*) "diagsoil.nc outputs not possible on unstructured grids!!"
+  call abort_physic("writediagsoil","impossible on unstructured grid",1)
+endif
+
 ! 1. Initialization step
 if (firstname.eq."1234567890") then
@@ -100,4 +117,5 @@
     call abort_physic("writediagsoil","Unable to crzate output file",1)
    endif
+  endif ! of if ((is_master)
 
 #ifdef CPP_PARA
@@ -111,4 +129,5 @@
 #endif
 
+  if (is_master) then
    ! build inertia() and area()
    if (klon_glo>1) then
@@ -139,4 +158,6 @@
    endif
 
+   ierr= NF_CLOSE(nid) ! Close the NETCDF file once initialized
+
   endif ! of if (is_master)
 
@@ -144,9 +165,4 @@
   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")
 
@@ -159,4 +175,9 @@
 ! 3. Write data, if the time index matches the sample rate
 if (mod(zitau+1,diagfi_output_rate).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
@@ -164,7 +185,4 @@
     ntime=ntime+1
     date=(zitau+1)*(dtphys/daysec)
-    ! Note: day_step is known from control.h
-
-    if (is_master) then
      ! Get NetCDF ID for "time"
      ierr=NF_INQ_VARID(nid,"time",varid)
@@ -173,15 +191,17 @@
 !     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
       call abort_physic("writediagsoil", "Failed writing date to time variable",1)
+      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
 if (dimpx.eq.3) then ! Case of a 3D variable
   ! A. Recast data along 'dynamics' grid
+  if (klon_glo>1) then ! General case
 #ifdef CPP_PARA
   ! gather field on a "global" (without redundant longitude) array
@@ -197,5 +217,4 @@
 !$OMP BARRIER
 #else
-  if (klon_glo>1) then ! General case
    do l=1,nsoilmx
     ! handle the poles
@@ -213,8 +232,8 @@
     enddo
    enddo
+#endif
   else ! 1D model case
    data3_1d(1,1:nsoilmx)=px(1,1:nsoilmx)
   endif
-#endif
 
   ! B. Write (append) the variable to the NetCDF file
@@ -270,4 +289,5 @@
 
   ! A. Recast data along 'dynamics' grid
+  if (klon_glo>1) then ! General case
 #ifdef CPP_PARA
   ! gather field on a "global" (without redundant longitude) array
@@ -284,5 +304,4 @@
 !$OMP BARRIER
 #else
-  if (klon_glo>1) then ! general case
     ! handle the poles
     do i=1,nbp_lon+1
@@ -298,8 +317,8 @@
       data2(nbp_lon+1,j)=data2(1,j) ! extra (modulo) longitude
     enddo
+#endif
   else ! 1D model case
     data2_1d=px(1,1)
   endif
-#endif
 
   ! B. Write (append) the variable to the NetCDF file
@@ -340,5 +359,5 @@
     ierr=NF_PUT_VARA_REAL(nid,varid,corners,edges,data2)
   else
-    ierr=NF_PUT_VARA_REAL(nid,varid,corners,edges,data2_1d)
+    ierr=NF_PUT_VARA_REAL(nid,varid,corners,edges,[data2_1d])
   endif
 !#endif
@@ -380,5 +399,5 @@
 !  ierr=NF_PUT_VARA_DOUBLE(nid,varid,corners,edges,data0)
 !#else
-  ierr=NF_PUT_VARA_REAL(nid,varid,corners,edges,data0)
+  ierr=NF_PUT_VARA_REAL(nid,varid,corners,edges,[data0])
 !#endif
   if (ierr.ne.NF_NOERR) then
@@ -388,11 +407,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,diagfi_output_rate).eq.0)
 
-! 4. Close the NetCDF file
-if (is_master) then
-  ierr=NF_CLOSE(nid)
-endif
-
 end subroutine writediagsoil
 
