Index: trunk/LMDZ.COMMON/libf/evolution/changelog.txt
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/changelog.txt	(revision 3665)
+++ trunk/LMDZ.COMMON/libf/evolution/changelog.txt	(revision 3666)
@@ -609,2 +609,6 @@
 == 26/02/2025 == JBC
 Correction and simplification of the numbering of jobs/runs in the launching script.
+
+== 28/02/2025 == JBC
+- Adjusments of few scripts to handle more situations.
+- Bug correction related to deallocation in case of "soil_pem = .false." or "layering_algo = .true.".
Index: trunk/LMDZ.COMMON/libf/evolution/compute_soiltemp_mod.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/compute_soiltemp_mod.F90	(revision 3665)
+++ trunk/LMDZ.COMMON/libf/evolution/compute_soiltemp_mod.F90	(revision 3666)
@@ -246,5 +246,5 @@
 real, dimension(ngrid,nsoil,nslope) :: tsoil_old
 
-write(*,*)"Shifting soil temperature to surface"
+write(*,*) "> Shifting soil temperature profile to match surface evolution"
 tsoil_old = tsoil
 
Index: trunk/LMDZ.COMMON/libf/evolution/deftank/inipem_orbit.sh
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/inipem_orbit.sh	(revision 3665)
+++ trunk/LMDZ.COMMON/libf/evolution/deftank/inipem_orbit.sh	(revision 3666)
@@ -15,4 +15,7 @@
 # Name of the file containing the orbital data
 orb_data="obl_ecc_lsp.asc"
+
+# Name of the file containing the starting date (Earth years)
+date_file="run_PEM.def"
 ######################################################################
 
@@ -23,16 +26,17 @@
     exit 1
 fi
-if [ ! -f "startfi.nc" ]; then
+if [ ! -f "$name_file" ]; then
     echo "Error: file \"$name_file\" not found!"
     exit 1
 fi
-if [ ! -f "run_PEM.def" ]; then
-    echo "Error: file \"run_PEM.def\" not found!"
+if [ ! -f "$date_file" ]; then
+    echo "Error: file \"$date_file\" not found!"
     exit 1
 fi
 
-# Get the number of Earth years before present to start the PEM run from "run_PEM.def"
-eyears_bp_ini=$(grep 'year_earth_bp_ini=' run_PEM.def | cut -d '=' -f 2 | tr -d '[:space:]')
-echo "The starting date is $eyears_bp_ini Earth years according to \"run_PEM.def\"."
+# Get the number of Earth years before present to start the PEM run
+eyears_bp_ini=$(grep 'year_earth_bp_ini=' $date_file | cut -d '=' -f 2 | tr -d '[:space:]')
+echo "In \"$date_file\":"
+echo "The starting date is $eyears_bp_ini Earth years."
 
 # Get the new values for the orbital parameters
@@ -58,8 +62,8 @@
         fi
         new_Lsp=$(echo "($lsp2 - $lsp1)/($y2 - $y1)*($yearlask - $y1) + $lsp1" | bc -l)
-        echo "New obliquity    = $new_obl"
-        echo "New eccentricity = $new_ecc"
-        echo "New Lsp          = $new_Lsp"
-        echo "Done!"
+        echo "In \"$orb_data\":"
+        echo "Found obliquity    = $new_obl"
+        echo "Found eccentricity = $new_ecc"
+        echo "Found Lsp          = $new_Lsp"
         break
     fi
@@ -95,16 +99,4 @@
 peri_day=$(echo "$year_day*(1. - ($zx0 - $new_ecc*s($zx0))/(2.*$pi))" | bc -l)
 
-ztheta=$(echo "($new_iniLs - $new_Lsp)/$degrad" | bc -l)
-tan=$(echo "s(0.5*$ztheta)/c(0.5*$ztheta)" | bc -l)
-zx0=$(echo "2.*a($tan*sqrt((1. - $new_ecc)/(1. + $new_ecc)))" | bc -l)
-xref=$(echo "$zx0 - $new_ecc*s($zx0)" | bc -l)
-new_inisol=$(echo "$peri_day + $xref*$year_day/(2.*$pi)" | bc -l)
-if [ $(echo "$new_inisol < 0." | bc -l) -eq 1 ]; then
-    new_inisol=$(echo "$new_inisol + $year_day" | bc -l)
-fi
-if [ $(echo "$new_inisol >= $year_day" | bc -l) -eq 1 ]; then
-    new_inisol=$(echo "$new_inisol - $year_day" | bc -l)
-fi
-
 # Update the netCDF file
 # controle(15) = periheli ! min. Sun-Mars distance (Mkm)  ̃206.66
@@ -117,5 +109,4 @@
          -s "controle(15)=$aphelie" \
          -s "controle(16)=$peri_day" \
-         -s "controle(2)=$new_inisol" \
          $name_file $name_file
 
@@ -123,6 +114,5 @@
 echo "New obliquit     = $new_obl"
 echo "New eccentricity = $new_ecc -> new periheli = $periheli"
-echo "                              -> new aphelie  = $aphelie"
+echo "                                         -> new aphelie  = $aphelie"
 echo "New Lsp          = $new_Lsp -> new peri_day = $peri_day"
-echo "New initial Ls   = $new_iniLs -> New initial sol = $new_inisol"
 echo "Done!"
Index: trunk/LMDZ.COMMON/libf/evolution/deftank/lib_launchPEM.sh
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/lib_launchPEM.sh	(revision 3665)
+++ trunk/LMDZ.COMMON/libf/evolution/deftank/lib_launchPEM.sh	(revision 3666)
@@ -181,5 +181,5 @@
         else # Mode: submitting jobs
             cp PCMrun.job PCMrun${iPCM}.job
-            sed -i -E "s/($name_job[^0-9]*)[0-9]+([^0-9]*)$/\1${iPCM}\2/" PCMrun${iPCM}.job
+            sed -i -E "/^$name_job/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${iPCM}\3/" PCMrun${iPCM}.job
             sed -i "s/^k=[0-9]\+$/k=$(echo "$ii - $3 + 2" | bc)/" PCMrun${iPCM}.job
             jobID=$(eval "$submit_job PCMrun${iPCM}.job")
@@ -208,5 +208,5 @@
             else # Mode: submitting jobs
                 cp PCMrun.job PCMrun${iPCM}.job
-                sed -i -E "s/($name_job[^0-9]*)[0-9]+([^0-9]*)$/\1${iPCM}\2/" PCMrun${iPCM}.job
+                sed -i -E "/^$name_job/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${iPCM}\3/" PCMrun${iPCM}.job
                 sed -i "s/^k=[0-9]\+$/k=$(echo "$i - $3 + 2" | bc)/" PCMrun${iPCM}.job
                 jobID=$(eval "$submit_dependjob=afterok:${jobID} PCMrun${iPCM}.job")
@@ -234,5 +234,5 @@
             fi
         else # Mode: submitting jobs
-            sed -i -E "s/($name_job[^0-9]*)[0-9]+([^0-9]*)$/\1${iPEM}\2/" PEMrun.job
+            sed -i -E "/^$name_job/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${iPEM}\3/" PEMrun.job
             jobID=$(eval "$submit_job PEMrun.job")
             # Create a file to cancel the dependent jobs of the cycle
@@ -264,5 +264,5 @@
             fi
         else # Mode: submitting jobs
-            sed -i -E "s/($name_job[^0-9]*)[0-9]+([^0-9]*)$/\1${iPEM}\2/" PEMrun.job
+            sed -i -E "/^$name_job/s/(.*[^0-9])([0-9]+)(_[^0-9]*)?$/\1${iPEM}\3/" PEMrun.job
             jobID=$(eval "$submit_dependjob=afterok:${jobID} PEMrun.job")
             echo $kill_job $jobID >> kill_launchPEM.sh
Index: trunk/LMDZ.COMMON/libf/evolution/deftank/visu_evol_layering.py
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/visu_evol_layering.py	(revision 3665)
+++ trunk/LMDZ.COMMON/libf/evolution/deftank/visu_evol_layering.py	(revision 3666)
@@ -151,4 +151,7 @@
 ### Processing
 stratif_data, max_top_elevation, longitude, latitude = process_files(folder_path,base_name)
+if dz > max_top_elevation:
+    print('The discretization step is higher than the maximum top elevation: please provide a correct value!')
+    exit()
 ref_grid, gridded_stratif_data = interpolate_data(stratif_data,max_top_elevation,dz)
 date_time = read_infofile(infofile)
Index: trunk/LMDZ.COMMON/libf/evolution/layering_mod.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/layering_mod.F90	(revision 3665)
+++ trunk/LMDZ.COMMON/libf/evolution/layering_mod.F90	(revision 3666)
@@ -125,6 +125,8 @@
 
 ! Verification of volume fraction
-if (abs(1. - (str%co2ice_volfrac + str%h2oice_volfrac + str%dust_volfrac + str%air_volfrac)) > tol) &
-     error stop 'add_stratum: properties for the new stratum are not possible (sum of volumetric fraction /= 1)!'
+if (abs(1. - (str%co2ice_volfrac + str%h2oice_volfrac + str%dust_volfrac + str%air_volfrac)) > tol) then
+    call print_stratum(str)
+    error stop 'add_stratum: properties for the new stratum are not possible (sum of volumetric fraction /= 1)!'
+endif
 
 ! Increment the number of strata
@@ -178,6 +180,8 @@
 
     ! Verification of volume fraction
-    if (abs(1. - (str%co2ice_volfrac + str%h2oice_volfrac + str%dust_volfrac + str%air_volfrac)) > tol) &
-         error stop 'insert_stratum: properties for the new stratum are not possible (sum of volumetric fraction /= 1)!'
+    if (abs(1. - (str%co2ice_volfrac + str%h2oice_volfrac + str%dust_volfrac + str%air_volfrac)) > tol) then
+        call print_stratum(str)
+        error stop 'add_stratum: properties for the new stratum are not possible (sum of volumetric fraction /= 1)!'
+    endif
 
     ! Increment the number of strata
Index: trunk/LMDZ.COMMON/libf/evolution/pem.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/pem.F90	(revision 3665)
+++ trunk/LMDZ.COMMON/libf/evolution/pem.F90	(revision 3666)
@@ -893,7 +893,5 @@
     if (soil_pem) then
 ! II_d.2 Shifting soil temperature to surface
-        write(*,*) "> Shifting soil temperature profile to match surface evolution"
         call shift_tsoil2surf(ngrid,nsoilmx_PEM,nslope,zshift_surf,zlag,tsurf_avg,tsoil_PEM)
-        deallocate(zshift_surf,zlag)
 
 ! II_d.3 Update soil temperature
@@ -977,4 +975,5 @@
         endif
     endif !soil_pem
+    deallocate(zshift_surf,zlag)
 
 !------------------------
@@ -1022,5 +1021,6 @@
     !    enddo
     !enddo
-    deallocate(vmr_co2_PEM_phys,icetable_depth_old,tsoil_PEM_timeseries_old)
+    if (soil_pem) deallocate(icetable_depth_old,tsoil_PEM_timeseries_old)
+    deallocate(vmr_co2_PEM_phys)
 
 !------------------------
@@ -1071,12 +1071,5 @@
 deallocate(d_co2ice,d_co2ice_ini,d_h2oice)
 deallocate(is_co2ice_ini,is_co2ice_sublim_ini,is_h2oice_sublim_ini)
-if (layering_algo) then
-    do islope = 1,nslope
-        do i = 1,ngrid
-            call del_layering(stratif(i,islope))
-        enddo
-    enddo
-    deallocate(new_str,new_lag1,new_lag2,current1,current2)
-endif
+if (layering_algo) deallocate(new_str,new_lag1,new_lag2,current1,current2)
 !------------------------------ END RUN --------------------------------
 
Index: trunk/LMDZ.COMMON/libf/evolution/pemetat0.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/pemetat0.F90	(revision 3665)
+++ trunk/LMDZ.COMMON/libf/evolution/pemetat0.F90	(revision 3666)
@@ -139,5 +139,5 @@
         if (.not. found) then
             write(*,*) 'Pemetat0: failed loading <nb_str_max>!'
-            write(*,*) '''nb_str_max'' is set to 3!'
+            write(*,*) '''nb_str_max'' is set to 1!'
         else
             nb_str_max = int(inquire_dimension_length('nb_str_max'))
