Index: trunk/LMDZ.COMMON/libf/evolution/deftank/README
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/README	(revision 3339)
+++ trunk/LMDZ.COMMON/libf/evolution/deftank/README	(revision 3349)
@@ -1,15 +1,24 @@
 This folder contains all the files needed to run the Planetary Evolution Model (PEM).
 
-# launch_pem.sh:
+# launchPEM.sh:
   Bash script file to launch the chained simulation of PEM and PCM runs.
   The user has to specify:
       (i)   n_mars_years, n_earth_years -> the number of Mars/Earth years to be simulated in total;
-      (ii)  nPCM -> the number of PCM calls between PEM runs (usually nPCM=2);
-      (iii) exePEM -> the name of executable file for the PEM run;
-      (iv)  exeReshape -> the name of executable file for the data reshaping task with XIOS.
+      (ii)  nPCM_ini -> the number of initial PCM runs (at least 2);
+      (iii) nPCM -> the number of PCM runs between each PEM run (usually 2).
+  The script can take an argument:
+      - If there is no argument, then the script initiates a PEM simulation from scratch.
+      - If the argument is 're', then the script relaunches an existing PEM simulation. It will ask for parameters to know the starting point that you want to. 
+
+# liblaunchPEM.sh:
+  Library of bash functions used by the PEM launching script "launchPEM.sh".
+
+# jobPCM.slurm:
+  Bash script file to run the PCM with a SLURM job. The name of the PCM executable file should be adapted. The header corresponds to the ADASTRA supercomputer and should be changed for other machines.
   The path to source the arch file should be adapted to the machine.
 
-# exePCM.sh:
-  Bash script file to run the PCM with a SLURM job. The name of the PCM executable file should be adapted. The header corresponds to the ADASTRA supercomputer and should be changed for other machines.
+# jobPEM.slurm:
+  Bash script file to run the PEM with a SLURM job. The name of the PEM executable file and Reshaping executable file should be adapted. The header corresponds to the ADASTRA supercomputer and should be changed for other machines.
+  The path to source the arch file should be adapted to the machine.
 
 # run_PEM.def
@@ -23,6 +32,15 @@
   Bash script file to modify orbital parameters in the file "startfi.nc" (obliquity, eccentricity, Ls perihelion and possibly initial Ls). They should be consistent with Laskar's data in "obl_ecc_lsp.asc" to start a chained simulation at a specific date.
 
+# modify_startfi_var.sh:
+  Bash script file to modify the value of a variable in a file "startfi.nc".
+
+# inipem_orbit.sh:
+  Bash script file to modify the orbital parameters of a file "startfi.nc" according to the date set in the file "run_PEM.def".
+
 # concat_diagpem.sh:
   Bash script file to concatenate along the variable 'Time' all the "diagpem.nc" files of the PEM into one NetCDF file. 'Time' is re-indexed with the numbering of Martian years simulated by the PEM run.
+
+# output_layering.py:
+  Python script file to output the stratification data from the "startpem.nc" files.
 
 Note:
Index: trunk/LMDZ.COMMON/libf/evolution/deftank/exePCM.sh
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/exePCM.sh	(revision 3339)
+++ 	(revision )
@@ -1,17 +1,0 @@
-#!/bin/bash
-#SBATCH --account=cin0391
-#SBATCH --job-name=runPCM1
-#SBATCH --constraint=GENOA
-### GENOA nodes accommodate 96 cores
-### Number of Nodes to use
-#SBATCH --nodes=1
-### Number of MPI tasks per node
-#SBATCH --ntasks-per-node=1
-### Number of OpenMP threads per MPI task
-#SBATCH --cpus-per-task=1
-#SBATCH --threads-per-core=1
-###SBATCH --exclusive
-#SBATCH --output=run_PCM_%A.out
-#SBATCH --time=10:00:00
-
-srun testphys1d_29_phymars_para.e > out_runPCM1 2>&1
Index: trunk/LMDZ.COMMON/libf/evolution/deftank/jobPCM.slurm
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/jobPCM.slurm	(revision 3349)
+++ trunk/LMDZ.COMMON/libf/evolution/deftank/jobPCM.slurm	(revision 3349)
@@ -0,0 +1,63 @@
+#!/bin/bash
+#SBATCH --job-name=jobPCM0
+#SBATCH --account=cin0391
+### GENOA nodes accommodate 96 cores
+#SBATCH --constraint=GENOA
+### Number of Nodes to use
+#SBATCH --nodes=1
+#SBATCH --ntasks-per-node=12
+#SBATCH --cpus-per-task=4
+#SBATCH --threads-per-core=1 # --hint=nomultithread
+###SBATCH --exclusive
+#SBATCH --output=jobPCM_%A.out
+#SBATCH --time=10:00:00
+
+# A few parameters that might need be changed depending on your setup:
+# Path to the arch.env to source
+source ../trunk/LMDZ.COMMON/arch.env
+
+# Number of threads to use (must be the same as '#MSUB -c' above)
+export OMP_NUM_THREADS=4
+export OMP_STACKSIZE=400M
+
+read i_myear n_myear convert_years iPCM iPEM nPCM nPCM_ini < info_PEM.txt
+cp run_PCM.def run.def
+
+echo "Run PCM $iPCM is starting."
+########################################################################
+srun --cpu-bind=threads --label -c${OMP_NUM_THREADS:=1} gcm_32x24x26_phymars_para.e > out_runPCM${iPCM} 2>&1
+########################################################################
+
+if [ ! -f "restartfi.nc" ] || [ ! tail -n 1 out_runPCM${iPCM} | grep -iq "everything is cool" ]; then # Check if it ended abnormally
+    echo "Error: the run PCM $iPCM crashed!"
+    echo "Be careful: there may be dependent jobs remaining in the SLURM queue with status 'DependencyNeverSatisfied'! You can cancel them by executing the script \"kill_launchPEM.sh\"."
+    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=0
+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/restartfi${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++))
+sed -i "1s/.*/$i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini/" info_PEM.txt
Index: trunk/LMDZ.COMMON/libf/evolution/deftank/jobPEM.slurm
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/jobPEM.slurm	(revision 3349)
+++ trunk/LMDZ.COMMON/libf/evolution/deftank/jobPEM.slurm	(revision 3349)
@@ -0,0 +1,58 @@
+#!/bin/bash
+#SBATCH --job-name=jobPEM2
+#SBATCH --account=cin0391
+### GENOA nodes accommodate 96 cores
+#SBATCH --constraint=GENOA
+### Number of Nodes to use
+#SBATCH --nodes=1
+#SBATCH --ntasks-per-node=1
+#SBATCH --cpus-per-task=1
+#SBATCH --threads-per-core=1 # --hint=nomultithread
+###SBATCH --exclusive
+#SBATCH --output=jobPEM_%A.out
+#SBATCH --time=1:00:00
+
+# A few parameters that might need be changed depending on your setup:
+# Path to the arch.env to source
+source ../trunk/LMDZ.COMMON/arch.env
+
+read i_myear n_myear convert_years iPCM iPEM nPCM nPCM_ini < info_PEM.txt
+cp run_PEM.def run.def
+
+echo "Reshaping PCM data with XIOS is starting."
+########################################################################
+./reshape_XIOS_output_32x24x26_phymars_seq.e
+########################################################################
+
+if [ ! -f "data_PCM_Y1.nc" ] || [ ! -f "data_PCM_Y2.nc" ]; then # Check if it ended abnormally
+    echo "Error: the reshaping executable crashed!"
+    exit 1
+fi
+echo "Run PEM $iPEM is starting."
+########################################################################
+./pem_32x24x26_phymars_seq.e > out_runPEM${iPEM} 2>&1
+########################################################################
+
+if [ ! -f "restartfi.nc" ] || [ ! tail -n 1 out_runPEM${iPEM} | grep -iq "so far, so good" ]; then # Check if it 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/restartpem${iPEM}.nc
+mv restartpem.nc startpem.nc
+cp restartfi.nc starts/restartfi_postPEM${iPEM}.nc
+mv restartfi.nc startfi.nc
+if [ -f "restart.nc" ]; then
+    cp restart.nc starts/restart_postPEM${iPEM}.nc
+    mv restart.nc start.nc
+elif [ -f "restart1D.txt" ]; then
+    cp restart1D.txt starts/restart1D_postPEM${iPEM}.txt
+    mv restart1D.txt start1D.txt
+fi
+
+./launchPEM.sh new
Index: trunk/LMDZ.COMMON/libf/evolution/deftank/launchPEM.sh
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/launchPEM.sh	(revision 3349)
+++ trunk/LMDZ.COMMON/libf/evolution/deftank/launchPEM.sh	(revision 3349)
@@ -0,0 +1,61 @@
+#!/bin/bash
+########################################################################
+#### Launching script for a chained simulation of PEM and PCM runs  ####
+########################################################################
+# This script can take an argument:
+#   - If there is no argument, then the script initiates a PEM simulation from scratch.
+#   - If the argument is 're', then the script relaunches an existing PEM simulation.
+#     It will ask for parameters to know the starting point that you want to. 
+########################################################################
+
+
+########################################################################
+# Modify here the parameters for the simulation
+###############################################
+# Set the number of years to be simulated, either Martian or Earth years:
+n_mars_years=100
+#n_earth_years=300
+
+# Set the number of initial PCM runs:
+nPCM_ini=3
+
+# Set the number of PCM runs between each PEM run:
+nPCM=2
+########################################################################
+
+
+dir=`pwd`
+machine=`hostname`
+user=`whoami`
+if [ ! -f "lib_launchPEM.sh" ]; then
+    echo "Error: file \"lib_launchPEM.sh\" does not exist in $dir!"
+    echo "It can be found in the PEM deftank."
+    exit 1
+fi
+
+source lib_launchPEM.sh
+
+if [ $# -eq 0 ]; then # From scratch
+    echo "The launching script is starting!"
+    echo "The output file is \"log_launchPEM.txt\"."
+    exec > log_launchPEM.txt 2>&1
+    echo "Beginning of the launching script for the PEM simulation."
+    date
+    checklaunch
+    initlaunch
+    cyclelaunch $nPCM_ini
+else
+    if [ $1 = "new" ]; then # New cycle
+        exec >> log_launchPEM.txt 2>&1
+        echo "This is a new cycle for the PEM simulation."
+        date
+        read i_myear n_myear convert_years iPCM iPEM nPCM nPCM_ini < info_PEM.txt
+        cyclelaunch $nPCM
+    elif [ $1 = "re" ]; then # Relaunch
+        echo "Case not coded yet!"
+        errlaunch
+    else
+        echo "Error: given argument '$1' for the launching script is unknown!"
+        errlaunch
+    fi
+fi
Index: trunk/LMDZ.COMMON/libf/evolution/deftank/launch_pem.sh
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/launch_pem.sh	(revision 3339)
+++ 	(revision )
@@ -1,301 +1,0 @@
-#!/bin/bash
-#####################################################################
-### Launching script for a chained simulation of PEM and PCM runs ###
-#####################################################################
-
-echo "The launching script is starting!"
-echo "The output file is \"loglaunch.txt\"."
-
-# Re-spawn the script as a background process and create a bash file to kill it in case
-if [ "$1" = "bg" ]; then 
-    date
-    echo '#!/bin/bash' > kill_launch_pem.sh
-    echo 'kill' $$ >> kill_launch_pem.sh
-    chmod +x kill_launch_pem.sh
-else
-    nohup "$0" bg > loglaunch.txt 2>&1 &
-    exit 1
-fi
-
-
-#####################################################################
-# A few parameters that might need be changed depending on your setup:
-# Path to the arch.env to source:
-source ../trunk/LMDZ.COMMON/arch.env
-
-#---------- Modify here the number of years to be simulated ------------
-## set the number of years, either Martian or Earth years:
-n_mars_years=1000
-#n_earth_years=300
-
-#------------- Modify here the number of initial PCM calls -------------
-## set the number of initial PCM calls between each PEM call:
-nPCM_ini=4
-
-#---------------- Modify here the number of PCM calls ------------------
-## set the number of PCM calls between each PEM call:
-nPCM=2
-
-#------------------ Modify here the name of PEM exe --------------------
-## fill in the name of executable for PEM:
-exePEM="pem_29_phymars_seq.e"
-
-#-------------- Modify here the name of reshape XIOS exe ---------------
-## fill in the name of executable for reshape XIOS:
-exeReshape="reshape_XIOS_output_64x48x29_phymars_seq.e"
-#####################################################################
-
-
-# Save the current value of LC_NUMERIC and set it to a locale that uses a dot as the decimal separator
-OLD_LC_NUMERIC=$LC_NUMERIC
-LC_NUMERIC=en_US.UTF-8
-
-#------ Check if files/directories necessary for the script exist ------
-dir=`pwd`
-machine=`hostname`
-address=`whoami`
-if [ ! -f "$exePEM" ]; then
-    echo "Error: file \"$exePEM\" does not exist in $dir!"
-    exit 1
-fi
-if [ ! -f "$exeReshape" ]; then
-    echo "Error: file \"$exeReshape\" does not exist in $dir!"
-    exit 1
-fi
-if [ ! -f "exePCM.sh" ]; then
-    echo "Error: file \"exePCM.sh\" does not exist in $dir!"
-    exit 1
-fi
-if [ ! -f "run_PEM.def" ]; then
-    echo "Error: file \"run_PEM.def\" does not exist in $dir!"
-    exit 1
-fi
-if [ ! -f "run_PCM.def" ]; then
-    echo "Error: file \"run_PCM.def\" does not exist in $dir!"
-    exit 1
-fi
-if [ ! -f "context_lmdz_physics.xml" ]; then
-    echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!"
-    exit 1
-fi
-if [ ! -f "field_def_physics_mars.xml" ]; then
-    echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!"
-    exit 1
-fi
-if [ ! -f "file_def_physics_mars.xml" ]; then
-    echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!"
-    exit 1
-fi
-if [ ! -f "iodef.xml" ]; then
-    echo "Error: file \"iodef.xml\" does not exist in $dir!"
-    exit 1
-fi
-if [ ! -d "out_PCM" ]; then
-    mkdir out_PCM
-fi
-if [ ! -d "out_PEM" ]; then
-    mkdir out_PEM
-fi
-if [ ! -d "starts" ]; then
-    mkdir starts
-fi
-if [ ! -d "diags" ]; then
-    mkdir diags
-fi
-
-#---------------------------- Initialization ---------------------------
-dir=`pwd`
-machine=`hostname`
-address=`whoami`
-echo "This is a chained simulation for PEM and PCM runs in $dir on $machine."
-myear=686.9725      # Number of Earth days in Martian year
-eyear=365.256363004 # Number of days in Earth year
-convert_years=$(echo "$myear/$eyear" | bc -l)
-convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year
-if [ -v n_mars_years ] && [ ! -z "$n_mars_years" ]; then
-    n_myear=$n_mars_years
-    echo "Number of years to be simulated: $n_myear Martian years."
-elif [ -v n_earth_years ] && [ ! -z "$n_earth_years" ]; then
-    n_myear=$(echo "($n_earth_years/$convert_years + 0.999999)/1" | bc) # Ceiling of n_earth_years/convert_years
-    echo "Number of years to be simulated: $n_earth_years Earth years = $n_myear Martian years."
-else
-    echo "No number of years to be simulated has been set!"
-    exit 1
-fi
-i_myear=0
-iPEM=1
-((iPCM = ($iPEM - 1)*$nPCM + 1))
-cp startfi.nc starts/
-if [ -f "start.nc" ]; then
-    cp start.nc starts/
-elif [ -f "star1D.nc" ]; then
-    cp star1D.txt starts/
-fi
-
-# Create a file to manage years of the chained simulation and store some info from the PEM runs
-if [ -f "info_PEM.txt" ]; then
-    rm info_PEM.txt
-fi
-echo $i_myear $n_myear $convert_years > info_PEM.txt
-
-#---------------------------- Initial runs -----------------------------
-#--- Initial PCM runs
-cp run_PCM.def run.def
-for ((i = 1; i <= $nPCM_ini; i++)); do
-    echo "Run PCM $iPCM (initial): call $i/$nPCM_ini..."
-    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_ini" | 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/restartfi${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..."
-./$exeReshape
-echo "Done!"
-
-#--- Running PEM
-echo "Run PEM $iPEM..."
-cp run_PEM.def run.def
-./$exePEM > out_runPEM${iPEM} 2>&1
-if [ ! -f "restartfi.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/restartpem${iPEM}.nc
-mv restartpem.nc startpem.nc
-cp restartfi.nc starts/restartfi_postPEM${iPEM}.nc
-mv restartfi.nc startfi.nc
-if [ -f "restart.nc" ]; then
-    cp restart.nc starts/restart_postPEM${iPEM}.nc
-    mv restart.nc start.nc
-elif [ -f "restart1D.txt" ]; then
-    cp restart1D.txt starts/restart1D_postPEM${iPEM}.txt
-    mv restart1D.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/restartfi${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..."
-    ./$exeReshape
-    echo "Done!"
-
-    #--- Running PEM
-    echo "Run PEM $iPEM..."
-    cp run_PEM.def run.def
-    ./$exePEM > out_runPEM${iPEM} 2>&1
-    if [ ! -f "restartfi.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/restartpem${iPEM}.nc
-    mv restartpem.nc startpem.nc
-    cp restartfi.nc starts/restartfi_postPEM${iPEM}.nc
-    mv restartfi.nc startfi.nc
-    if [ -f "restart.nc" ]; then
-        cp restart.nc starts/restart_postPEM${iPEM}.nc
-        mv restart.nc start.nc
-    elif [ -f "restart1D.txt" ]; then
-        cp restart1D.txt starts/restart1D_postPEM${iPEM}.txt
-        mv restart1D.txt start1D.txt
-    fi
-    ((iPEM++))
-    read i_myear n_myear convert_years < info_PEM.txt
-    echo "Done!"
-done
-
-# Restore the previous value of LC_NUMERIC
-LC_NUMERIC=$OLD_LC_NUMERIC
-
-date
-echo "The launching script ended."
Index: trunk/LMDZ.COMMON/libf/evolution/deftank/lib_launchPEM.sh
===================================================================
--- trunk/LMDZ.COMMON/libf/evolution/deftank/lib_launchPEM.sh	(revision 3349)
+++ trunk/LMDZ.COMMON/libf/evolution/deftank/lib_launchPEM.sh	(revision 3349)
@@ -0,0 +1,189 @@
+#!/bin/bash
+########################################################################
+######## Library of bash functions for the PEM launching script ########
+########################################################################
+
+# To end the launching script
+endlaunch() {
+    # Restore the previous value of LC_NUMERIC
+    LC_NUMERIC=$OLD_LC_NUMERIC
+
+    date
+    echo "Successful end of the launching script for the PEM simulation."
+    exit 0
+}
+
+# To end the launching script with error
+errlaunch() {
+    # Restore the previous value of LC_NUMERIC
+    LC_NUMERIC=$OLD_LC_NUMERIC
+
+    date
+    echo "End with error of the launching script for the PEM."
+    exit 1
+}
+
+# To check if SLURM is the job scheduler
+function is_slurm() {
+    if [ ! -x $(command -v squeue) ]; then
+        echo "Error: the job scheduler is not SLURM on $machine!"
+        echo "You need to adapt the script to your case."
+        errlaunch
+    fi
+}
+
+# To check if everything necessary for the launching script is ok
+checklaunch() {
+    # Save the current value of LC_NUMERIC and set it to a locale that uses a dot as the decimal separator
+    OLD_LC_NUMERIC=$LC_NUMERIC
+    LC_NUMERIC=en_US.UTF-8
+
+    is_slurm
+    if [ -v n_mars_years ] && [ ! -z "$n_mars_years" ]; then
+        if [ $n_mars_years -lt 1 ]; then
+            echo "Error: the value of 'n_mars_years' must be >0!"
+            errlaunch
+        fi
+    elif [ -v n_earth_years ] && [ ! -z "$n_earth_years" ]; then
+        if [ $n_earth_years -lt 1 ]; then
+            echo "Error: the value of 'n_earth_years' must be >0!"
+            errlaunch
+        fi
+    else
+        echo "Error: no number of years to be simulated has been set!"
+        errlaunch
+    fi
+    if [ $nPCM_ini -lt 2 ] || [ -z "$nPCM_ini" ]; then
+        echo "Error: the value of 'nPCM_ini' must be >1!"
+        errlaunch
+    fi
+    if [ $nPCM -lt 2 ] || [ -z "$nPCM" ]; then
+        echo "Error: the value of 'nPCM' must be >1!"
+        errlaunch
+    fi
+    if [ ! -f "jobPCM.slurm" ]; then
+        echo "Error: file \"jobPCM.slurm\" does not exist in $dir!"
+        errlaunch
+    fi
+    if [ ! -f "jobPEM.slurm" ]; then
+        echo "Error: file \"$jobPEM.slurm\" does not exist in $dir!"
+        errlaunch
+    fi
+    if [ ! -f "run_PCM.def" ]; then
+        echo "Error: file \"run_PCM.def\" does not exist in $dir!"
+        errlaunch
+    fi
+    if [ ! -f "run_PEM.def" ]; then
+        echo "Error: file \"run_PEM.def\" does not exist in $dir!"
+        errlaunch
+    fi
+    if [ ! -f "context_lmdz_physics.xml" ]; then
+        echo "Error: file \"context_lmdz_physics.xml\" does not exist in $dir!"
+        errlaunch
+    fi
+    if [ ! -f "field_def_physics_mars.xml" ]; then
+        echo "Error: file \"field_def_physics_mars.xml\" does not exist in $dir!"
+        errlaunch
+    fi
+    if [ ! -f "file_def_physics_mars.xml" ]; then
+        echo "Error: file \"file_def_physics_mars.xml\" does not exist in $dir!"
+        errlaunch
+    fi
+    if [ ! -f "iodef.xml" ]; then
+        echo "Error: file \"iodef.xml\" does not exist in $dir!"
+        errlaunch
+    fi
+    if [ ! -d "out_PCM" ]; then
+        mkdir out_PCM
+    fi
+    if [ ! -d "out_PEM" ]; then
+        mkdir out_PEM
+    fi
+    if [ ! -d "starts" ]; then
+        mkdir starts
+    fi
+    if [ ! -d "diags" ]; then
+        mkdir diags
+    fi
+}
+
+# To initialize the launching script
+initlaunch() {
+    echo "This is a chained simulation for PEM and PCM runs in $dir on $machine by $user."
+    myear=686.9725      # Number of Earth days in Martian year
+    eyear=365.256363004 # Number of days in Earth year
+    convert_years=$(echo "$myear/$eyear" | bc -l)
+    convert_years=$(printf "%.4f" $convert_years) # Rounding to the 4th decimal to respect the precision of Martian year
+    if [ -v n_mars_years ]; then
+        n_myear=$n_mars_years
+        echo "Number of years to be simulated: $n_myear Martian years."
+    elif [ -v n_earth_years ]; then
+        n_myear=$(echo "($n_earth_years/$convert_years + 0.999999)/1" | bc) # Ceiling of n_earth_years/convert_years
+        echo "Number of years to be simulated: $n_earth_years Earth years = $n_myear Martian years."
+    fi
+    i_myear=0
+    iPEM=1
+    ((iPCM = ($iPEM - 1)*$nPCM + 1))
+    cp startfi.nc starts/
+    if [ -f "start.nc" ]; then
+        cp start.nc starts/
+    elif [ -f "star1D.nc" ]; then
+        cp star1D.txt starts/
+    fi
+
+    # Create a file to manage years of the chained simulation and store some info from the PEM runs
+    echo $i_myear $n_myear $convert_years $iPCM $iPEM $nPCM $nPCM_ini > info_PEM.txt
+}
+
+# To submit the PCM runs
+submitPCM() {
+    find . -type f -name "jobPCM*.slurm" ! -name "jobPCM.slurm" -exec rm {} +
+    if [ $i_myear -lt $n_myear ]; then
+        echo "Run PCM $iPCM: call 1/$1..."
+        cp jobPCM.slurm jobPCM${iPCM}.slurm
+        sed -i "s/#SBATCH --job-name=jobPCM.*/#SBATCH --job-name=jobPCM${iPCM}/" jobPCM${iPCM}.slurm
+        sed -i "s/^k=[0-9]\+$/k=$(echo "3 - $nPCM_ini" | bc -l)/" jobPCM${iPCM}.slurm
+        jobID=$(sbatch --parsable jobPCM${iPCM}.slurm)
+        echo "#!/bin/bash" > kill_launchPEM.sh
+        chmod +x kill_launchPEM.sh
+        echo "scancel" $jobID >> kill_launchPEM.sh
+        ((iPCM++))
+        ((i_myear++))
+    else
+        endlaunch
+    fi
+    for ((i = 2; i <= $1; i++)); do
+        if [ $i_myear -lt $n_myear ]; then
+            echo "Run PCM $iPCM: call $i/$1..."
+            cp jobPCM.slurm jobPCM${iPCM}.slurm
+            sed -i "s/#SBATCH --job-name=jobPCM.*/#SBATCH --job-name=jobPCM${iPCM}/" jobPCM${iPCM}.slurm
+            sed -i "s/^k=[0-9]\+$/k=$(echo "$i + 2 - $nPCM_ini" | bc -l)/" jobPCM${iPCM}.slurm
+            jobID=$(sbatch --parsable --dependency=afterok:${jobID} jobPCM${iPCM}.slurm)
+            echo "scancel" $jobID >> kill_launchPEM.sh
+            ((iPCM++))
+            ((i_myear++))
+        else
+            endlaunch
+        fi
+    done
+}
+
+# To submit the PEM run
+submitPEM() {
+    if [ $i_myear -lt $n_myear ]; then
+        sed -i "s/#SBATCH --job-name=jobPEM.*/#SBATCH --job-name=jobPEM${iPEM}/" jobPEM.slurm
+        jobID=$(sbatch --parsable --dependency=afterok:${jobID} jobPEM.slurm)
+        echo "scancel" $jobID >> kill_launchPEM.sh
+    else
+        endlaunch
+    fi
+}
+
+# To make one cycle of PCM and PEM runs
+cyclelaunch() {
+    # PCM runs
+    submitPCM $1
+
+    # PEM run
+    submitPEM
+}
