Index: trunk/LMDZ.COMMON/libf/evolution/changelog.txt
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/changelog.txt	(revision 3427)
+++ trunk/LMDZ.COMMON/libf/evolution/changelog.txt	(revision 3428)
@@ -419,2 +419,5 @@
 == 06/09/2024 == JBC
 Modification in the way of updating the soil temperatures to improve the computation time  + Small correction in the calculation of 'timestep'.
+
+== 16/09/2024 == JBC
+Corrections and improvements for the launching script.
Index: trunk/LMDZ.COMMON/libf/evolution/deftank/clean.sh
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/clean.sh	(revision 3427)
+++ trunk/LMDZ.COMMON/libf/evolution/deftank/clean.sh	(revision 3428)
@@ -33,4 +33,7 @@
     cp starts/start1D.txt .
 fi
+if [ -f "starts/startpem.nc" ]; then
+    cp starts/startpem.nc .
+fi
 # Cleaning of files in the sub-folders
 rm -f diags/*
Index: trunk/LMDZ.COMMON/libf/evolution/deftank/lib_launchPEM.sh
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/lib_launchPEM.sh	(revision 3427)
+++ trunk/LMDZ.COMMON/libf/evolution/deftank/lib_launchPEM.sh	(revision 3428)
@@ -151,4 +151,7 @@
     elif [ -f "star1D.nc" ]; then
         cp star1D.txt starts/
+    fi
+    if [ -f "startpem.nc" ]; then
+        cp startpem.nc starts/
     fi
 
@@ -317,6 +320,6 @@
     else
         # PCM relaunch during a cycle
-        iPEM=$((($irelaunch - $nPCM_ini)/$nPCM + 1))
-        il=$(echo "($irelaunch - $nPCM_ini)%$nPCM" | bc -l)
+        iPEM=$((($irelaunch - $nPCM_ini - 1)/$nPCM + 1))
+        il=$(echo "($irelaunch - $nPCM_ini - 1)%$nPCM + 1" | bc -l)
         cleanfiles diags/diagpem .nc $iPEM
         cleanfiles "out_PEM/run" "" $iPEM
@@ -326,15 +329,15 @@
         cleanfiles starts/restartpem .nc $iPEM
         cp starts/restartpem${iPEM}.nc startpem.nc
-        if [ $il -eq $(($nPCM - 1)) ]; then
+        if [ $il -eq $(($nPCM - 1)) ]; then # Second to last PCM run
             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
             cyclelaunch $1 $nPCM $il
-        elif [ $il -eq 0 ]; then
+        elif [ $il -eq $nPCM ]; then # Last PCM run so the next job is a PEM run
             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
-            submitPEM $1 # The next job is a PEM run
+            submitPEM $1
         else
             i_myear=$(($(awk "NR==$iPEM {print \$1}" "info_PEM.txt") + $il))
@@ -349,5 +352,5 @@
 relaunchPEM() {
     iPEM=$irelaunch
-    iPCM=$(($nPCM_ini + ($nPCM - 1)*$irelaunch + 1))
+    iPCM=$(($nPCM_ini + $nPCM*($irelaunch - 1) + 1))
     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
