Index: /trunk/LMDZ.COMMON/libf/evolution/changelog.txt
===================================================================
--- /trunk/LMDZ.COMMON/libf/evolution/changelog.txt	(revision 3295)
+++ /trunk/LMDZ.COMMON/libf/evolution/changelog.txt	(revision 3296)
@@ -262,2 +262,5 @@
 - Small correction to make the 3D PEM be able to compile.
 - Improvement of "launch_pem.sh": a file "kill_launch_pem.sh" is now automatically created which allows the user to kill the process of the launching script in case.
+
+== 08/04/2024 == JBC
+Correction of "launch_pem.sh" in the deftank: the number of years to be simulated was not respected because of extra PCM runs at the end of the simulation due to the order of PCM/PEM runs inside the loop.
Index: /trunk/LMDZ.COMMON/libf/evolution/deftank/launch_pem.sh
===================================================================
--- /trunk/LMDZ.COMMON/libf/evolution/deftank/launch_pem.sh	(revision 3295)
+++ /trunk/LMDZ.COMMON/libf/evolution/deftank/launch_pem.sh	(revision 3296)
@@ -139,5 +139,6 @@
 echo $i_myear $n_myear $convert_years > info_PEM.txt
 
-#-------------------------- Initial PCM runs ---------------------------
+#---------------------------- Initial runs -----------------------------
+#--- Initial PCM runs
 cp run_PCM.def run.def
 for ((i = 1; i <= $nPCM_ini; i++)); do
@@ -181,6 +182,88 @@
 sed -i "1s/.*/$i_myear $n_myear $convert_years/" info_PEM.txt
 
+#--- Reshaping PCM data with XIOS
+echo "Reshaping PCM data with XIOS..."
+./$exeReshape
+echo "Done!"
+
+#--- Running PEM
+echo "Run PEM $iPEM..."
+cp run_PEM.def run.def
+mv startfi.nc startfi_evol.nc
+if [ -f "start.nc" ]; then
+    mv start.nc start_evol.nc
+elif [ -f "start1D.txt" ]; then
+    mv start1D.txt start1D_evol.txt
+fi
+./$exePEM > out_runPEM${iPEM} 2>&1
+if [ ! -f "restartfi_evol.nc" ]; then # Check if run ended abnormally
+    echo "Error: the run PEM $iPEM crashed!"
+    exit 1
+fi
+# Copy data files and prepare the next run
+mv out_runPEM${iPEM} out_PEM/run${iPEM}
+mv diagpem.nc diags/diagpem${iPEM}.nc
+if [ -f "diagsoilpem.nc" ]; then
+    mv diagsoilpem.nc diags/diagsoilpem${iPEM}.nc
+fi
+cp restartpem.nc starts/startpem${iPEM}.nc
+mv restartpem.nc startpem.nc
+cp restartfi_evol.nc starts/startfi_postPEM${iPEM}.nc
+mv restartfi_evol.nc startfi.nc
+if [ -f "restart_evol.nc" ]; then
+    cp restart_evol.nc starts/restart_postPEM${iPEM}.nc
+    mv restart_evol.nc start.nc
+elif [ -f "restart1D_evol.txt" ]; then
+    cp restart1D_evol.txt starts/restart1D_postPEM${iPEM}.txt
+    mv restart1D_evol.txt start1D.txt
+fi
+((iPEM++))
+read i_myear n_myear convert_years < info_PEM.txt
+echo "Done!"
+
 #---------------------- Main loop to call PEM/PCM ----------------------
 while [ $i_myear -lt $n_myear ]; do
+    #--- Loop to run PCM year by year
+    cp run_PCM.def run.def
+    for ((i = 1; i <= $nPCM; i++)); do
+        echo "Run PCM $iPCM: call $i/$nPCM..."
+        sed -i "s/#SBATCH --job-name=runPCM.*/#SBATCH --job-name=runPCM${iPCM}/" exePCM.sh
+        sed -i "s/out_runPCM[0-9]\+/out_runPCM${iPCM}/" exePCM.sh
+        sbatch -W exePCM.sh
+        if [ ! -f "restartfi.nc" ]; then # Check if run ended abnormally
+            echo "Error: the run PCM $iPCM crashed!"
+            exit 1
+        fi
+        # Copy data files and prepare the next run
+        mv out_runPCM${iPCM} out_PCM/run${iPCM}
+        mv diagfi.nc diags/diagfi${iPCM}.nc
+        if [ -f "diagsoil.nc" ]; then
+            mv diagsoil.nc diags/diagsoil${iPCM}.nc
+        fi
+        if [ -f "stats.nc" ]; then
+            mv stats.nc diags/stats${iPCM}.nc
+        fi
+        k=$(echo "$i + 2 - $nPCM" | bc -l)
+        if [ $(echo "$k < 1" | bc -l) -eq 1 ]; then # Only the last 2 years are taken for the PEM
+            mv Xdiurnalave.nc diags/data2reshape${iPCM}.nc
+        else
+            cp Xdiurnalave.nc diags/data2reshape${iPCM}.nc
+            mv Xdiurnalave.nc data2reshape_Y${k}.nc
+        fi
+        cp restartfi.nc starts/startfi${iPCM}.nc
+        mv restartfi.nc startfi.nc
+        if [ -f "restart.nc" ]; then
+            cp restart.nc starts/restart${iPCM}.nc
+            mv restart.nc start.nc
+        elif [ -f "restart1D.txt" ]; then
+            cp restart1D.txt starts/restart1D${iPCM}.txt
+            mv restart1D.txt start1D.txt
+        fi
+        ((iPCM++))
+        ((i_myear++))
+        echo "Done!"
+    done
+    sed -i "1s/.*/$i_myear $n_myear $convert_years/" info_PEM.txt
+
     #--- Reshaping PCM data with XIOS
     echo "Reshaping PCM data with XIOS..."
@@ -222,46 +305,4 @@
     read i_myear n_myear convert_years < info_PEM.txt
     echo "Done!"
-
-    #--- Loop to run PCM year by year
-    cp run_PCM.def run.def
-    for ((i = 1; i <= $nPCM; i++)); do
-        echo "Run PCM $iPCM: call $i/$nPCM..."
-        sed -i "s/#SBATCH --job-name=runPCM.*/#SBATCH --job-name=runPCM${iPCM}/" exePCM.sh
-        sed -i "s/out_runPCM[0-9]\+/out_runPCM${iPCM}/" exePCM.sh
-        sbatch -W exePCM.sh
-        if [ ! -f "restartfi.nc" ]; then # Check if run ended abnormally
-            echo "Error: the run PCM $iPCM crashed!"
-            exit 1
-        fi
-        # Copy data files and prepare the next run
-        mv out_runPCM${iPCM} out_PCM/run${iPCM}
-        mv diagfi.nc diags/diagfi${iPCM}.nc
-        if [ -f "diagsoil.nc" ]; then
-            mv diagsoil.nc diags/diagsoil${iPCM}.nc
-        fi
-        if [ -f "stats.nc" ]; then
-            mv stats.nc diags/stats${iPCM}.nc
-        fi
-        k=$(echo "$i + 2 - $nPCM" | bc -l)
-        if [ $(echo "$k < 1" | bc -l) -eq 1 ]; then # Only the last 2 years are taken for the PEM
-            mv Xdiurnalave.nc diags/data2reshape${iPCM}.nc
-        else
-            cp Xdiurnalave.nc diags/data2reshape${iPCM}.nc
-            mv Xdiurnalave.nc data2reshape_Y${k}.nc
-        fi
-        cp restartfi.nc starts/startfi${iPCM}.nc
-        mv restartfi.nc startfi.nc
-        if [ -f "restart.nc" ]; then
-            cp restart.nc starts/restart${iPCM}.nc
-            mv restart.nc start.nc
-        elif [ -f "restart1D.txt" ]; then
-            cp restart1D.txt starts/restart1D${iPCM}.txt
-            mv restart1D.txt start1D.txt
-        fi
-        ((iPCM++))
-        ((i_myear++))
-        echo "Done!"
-    done
-    sed -i "1s/.*/$i_myear $n_myear $convert_years/" info_PEM.txt
 done
 
