Index: trunk/LMDZ.COMMON/libf/evolution/changelog.txt
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/changelog.txt	(revision 3364)
+++ trunk/LMDZ.COMMON/libf/evolution/changelog.txt	(revision 3365)
@@ -353,2 +353,5 @@
 - Update of "jobPEM.slurm" in the deftank to guarantee enough memory space to run the job.
 - Few minor cleanings.
+
+== 10/06/2024 == JBC
+Correction of an error in "lib_launchPEM.sh" due to a miscalculated condition + Improvement of the relaunch which now cleans the unnecessary files.
Index: trunk/LMDZ.COMMON/libf/evolution/deftank/launchPEM.sh
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/launchPEM.sh	(revision 3364)
+++ trunk/LMDZ.COMMON/libf/evolution/deftank/launchPEM.sh	(revision 3365)
@@ -75,5 +75,4 @@
         done
         read i_myear n_myear convert_years iPCM iPEM nPCM nPCM_ini < info_PEM.txt
-        echo $i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini
         while true; do
             if [ $relaunch = "PCM" ]; then
Index: trunk/LMDZ.COMMON/libf/evolution/deftank/lib_launchPEM.sh
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/lib_launchPEM.sh	(revision 3364)
+++ trunk/LMDZ.COMMON/libf/evolution/deftank/lib_launchPEM.sh	(revision 3365)
@@ -206,7 +206,34 @@
 }
 
+# To clean files after the starting run of the relaunch
+cleanfiles() {
+    prefix=$1
+    extension=$2
+    if [ -z "$extension" ]; then
+        for file in ${prefix}*; do
+            num=${file#$prefix}
+            if [[ $num =~ ^[0-9]+$ ]] && [ $num -gt $3 ]; then
+                rm $file
+            fi
+        done
+    else
+        for file in ${prefix}*${extension}; do
+            num=${file#$prefix}
+            num=${num%$extension}
+            if [[ $num =~ ^[0-9]+$ ]] && [ $num -gt $3 ]; then
+                rm $file
+            fi
+        done
+    fi
+}
+
 # To relaunch from PCM run
 relaunchPCM() {
     iPCM=$(($irelaunch + 1))
+    cleanfiles diags/diagfi .nc $irelaunch
+    cleanfiles "out_PCM/run" "" $irelaunch
+    cleanfiles starts/restart1D .txt $irelaunch
+    cleanfiles starts/restart .nc $irelaunch
+    cleanfiles starts/restartfi .nc $irelaunch
     cp starts/restartfi${irelaunch}.nc startfi.nc
     if [ -f "starts/restart${irelaunch}.nc" ]; then
@@ -218,31 +245,53 @@
         # PCM relaunch during the initialization cycle
         iPEM=1
+        cleanfiles diags/diagpem .nc $iPEM
+        cleanfiles "out_PEM/run" "" $iPEM
+        cleanfiles starts/restart1D_postPEM .txt $iPEM
+        cleanfiles starts/restart_postPEM .nc $iPEM
+        cleanfiles starts/restartfi_postPEM .nc $iPEM
+        cleanfiles starts/restartpem .nc $iPEM
         i_myear=$irelaunch
         sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
         if [ $irelaunch -eq $(($nPCM_ini - 1)) ]; then
             cp diags/data2reshape${irelaunch}.nc data2reshape_Y1.nc
+            cleanfiles diags/data2reshape .nc $irelaunch
             cyclelaunch $nPCM_ini $irelaunch
-        elif [ $irelaunch -eq $nPCM_ini ]; then
+        elif [ $irelaunch -eq 0 ]; then
             cp diags/data2reshape$(($irelaunch - 1)).nc data2reshape_Y1.nc
             cp diags/data2reshape${irelaunch}.nc data2reshape_Y2.nc
+            cleanfiles diags/data2reshape .nc $(($irelaunch - 1))
             submitPEM # The next job is a PEM run
         else
+            cleanfiles diags/data2reshape .nc 0
             cyclelaunch $nPCM_ini $iPCM
         fi
     else
         # PCM relaunch during a cycle
-        iPEM=$(($irelaunch/$nPCM + 1))
+        iPEM=$((($irelaunch - $nPCM_ini)/$nPCM + 1))
         il=$(echo "($irelaunch - $nPCM_ini)%$nPCM" | bc -l)
-        i_myear=$(($(awk "NR==$(($iPEM + 1)) {print \$1}" "info_PEM.txt") + $il))
-        sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
+        cleanfiles diags/diagpem .nc $iPEM
+        cleanfiles "out_PEM/run" "" $iPEM
+        cleanfiles starts/restart1D_postPEM .txt $iPEM
+        cleanfiles starts/restart_postPEM .nc $iPEM
+        cleanfiles starts/restartfi_postPEM .nc $iPEM
+        cleanfiles starts/restartpem .nc $iPEM
         cp starts/restartpem${iPEM}.nc startpem.nc
         if [ $il -eq $(($nPCM - 1)) ]; then
+            i_myear=$(($(awk "NR==$iPEM {print \$1}" "info_PEM.txt") + $il))
+            sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
             cp diags/data2reshape${irelaunch}.nc data2reshape_Y1.nc
+            cleanfiles diags/data2reshape .nc $irelaunch
             cyclelaunch $nPCM $il
-        elif [ $il -eq $nPCM ]; then
+        elif [ $il -eq 0 ]; then
+            i_myear=$(($(awk "NR==$iPEM {print \$1}" "info_PEM.txt") + $nPCM))
+            sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
             cp diags/data2reshape$(($irelaunch - 1)).nc data2reshape_Y1.nc
             cp diags/data2reshape${irelaunch}.nc data2reshape_Y2.nc
+            cleanfiles diags/data2reshape .nc $(($irelaunch - 1))
             submitPEM # The next job is a PEM run
         else
+            i_myear=$(($(awk "NR==$iPEM {print \$1}" "info_PEM.txt") + $il))
+            sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
+            cleanfiles diags/data2reshape .nc $irelaunch
             cyclelaunch $nPCM $il
         fi
@@ -256,4 +305,16 @@
     i_myear=$(awk "NR==$(($iPEM + 1)) {print \$1}" "info_PEM.txt")
     sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
+    cleanfiles diags/diagfi .nc $(($iPCM - 1))
+    cleanfiles "out_PCM/run" "" $(($iPCM - 1))
+    cleanfiles starts/restart1D .txt $(($iPCM - 1))
+    cleanfiles starts/restart .nc $(($iPCM - 1))
+    cleanfiles starts/restartfi .nc $(($iPCM - 1))
+    cleanfiles diags/data2reshape .nc $(($iPCM - 1))
+    cleanfiles diags/diagpem .nc $irelaunch
+    cleanfiles "out_PEM/run" "" $irelaunch
+    cleanfiles starts/restart1D_postPEM .txt $irelaunch
+    cleanfiles starts/restart_postPEM .nc $irelaunch
+    cleanfiles starts/restartfi_postPEM .nc $irelaunch
+    cleanfiles starts/restartpem .nc $irelaunch
     cp starts/restartpem${irelaunch}.nc startpem.nc
     cp starts/restartfi_postPEM${irelaunch}.nc startfi.nc
Index: trunk/LMDZ.COMMON/libf/evolution/pem.F90
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/pem.F90	(revision 3364)
+++ trunk/LMDZ.COMMON/libf/evolution/pem.F90	(revision 3365)
@@ -239,5 +239,5 @@
 character(100)  :: chtimelimit     ! Time limit for the PEM job outputted by the SLURM command
 real            :: timelimit       ! Time limit for the PEM job in seconds
-real, parameter :: antetime = 1200 ! Anticipation time to prevent reaching the time time limit: 1200 s = 20 min by default
+real, parameter :: antetime = 1200 ! Anticipation time to prevent reaching the time limit: 1200 s = 20 min by default
 integer         :: cstat, days, hours, minutes, seconds
 character(1)    :: sep
@@ -596,4 +596,5 @@
 call compute_tend(ngrid,nslope,min_h2o_ice,tend_h2o_ice)
 tend_co2_ice_ini = tend_co2_ice
+deallocate(min_co2_ice,min_h2o_ice)
 
 !------------------------
@@ -602,5 +603,4 @@
 !------------------------
 allocate(co2_ice(ngrid,nslope),h2o_ice(ngrid,nslope))
-deallocate(min_co2_ice,min_h2o_ice)
 
 allocate(stratif(ngrid,nslope))
