#!/bin/bash
### Partition to use
#SBATCH --account=cin0391
#SBATCH --constraint=GENOA
### Number of nodes/cores to use
#SBATCH --nodes=1
#SBATCH --ntasks-per-node=1
#SBATCH --cpus-per-task=1
#SBATCH --threads-per-core=1 # --hint=nomultithread
### Job information
#SBATCH --job-name=jobPEM1
#SBATCH --output=jobPEM_%j.out
#SBATCH --time=24:00:00
#SBATCH --mem=64G

########################################################################
# Modify here the parameters depending on your setup
####################################################
# Path to the arch.env to source:
source ../trunk/LMDZ.COMMON/arch.env

# Name of executable for the PEM:
pem_exe="pem_64x48x32_phymars_seq.e"

# Argument for the PEM execution ("--auto-exit" for SLURM and PBS/TORQUE | "" when the script is not run as a job):
pem_arg="--auto-exit"
########################################################################


ulimit -s unlimited

# Running the PEM
read n_yr_sim ntot_yr_sim r_plnt2earth_yr i_pcm_run i_pem_run n_pcm_runs n_pcm_runs_ini < pem_workflow.sts
echo "Run \"PEM $i_pem_run\" is starting."
cp run_pem.def run.def
eval "./$pem_exe $pem_arg > run.log 2>&1"
if [ ! -f "restartfi.nc" ] || ! (tail -n 100 run.log | grep -iq "so far, so good!"); then # Check if it ended abnormally
    echo "Error: the run \"PEM $i_pem_run\" crashed!"
    exit 1
fi

# Copy data files and prepare the next run
mv run.log logs/run_pem${i_pem_run}.log
if [ -f "diagevol.nc" ]; then
    mv diagevol.nc diags/diagevol${i_pem_run}.nc
fi
if [ -f "diagevol_soil.nc" ]; then
    mv diagevol_soil.nc diags/diagevol_soil${i_pem_run}.nc
fi
cp restartevol.nc starts/restartevol${i_pem_run}.nc
mv restartevol.nc startevol.nc
cp restartfi.nc starts/restartfi_postpem${i_pem_run}.nc
mv restartfi.nc startfi.nc
if [ -f "restart.nc" ]; then
    cp restart.nc starts/restart_postpem${i_pem_run}.nc
    mv restart.nc start.nc
elif [ -f "restart1D.txt" ]; then
    cp restart1D.txt starts/restart1D_postpem${i_pem_run}.txt
    mv restart1D.txt start1D.txt
fi

# Submit the next cycle
#if [ "$(awk 'END{print $NF}' pem_workflow.sts)" -eq 7 ]; then
#    read n_yr_sim ntot_yr_sim r_plnt2earth_yr i_pcm_run i_pem_run n_pcm_runs n_pcm_runs_ini < pem_workflow.sts
#    ./pem_workflow.sh cont # Continue the PEM run if it stopped because of job time limit
#else
    ./pem_workflow.sh new
#fi
