#!/bin/bash
#SBATCH --job-name=jobPEM1
#SBATCH --account=cin0391
### GENOA nodes accommodate 96 cores
#SBATCH --constraint=GENOA
### Number of Nodes to use
#SBATCH --nodes=1
#SBATCH --ntasks=1
#SBATCH --cpus-per-task=1
###SBATCH --exclusive
#SBATCH --output=jobPEM_%j.out
#SBATCH --time=24:00:00
#SBATCH --mem=29000M

# A few parameters that might need to be changed depending on your setup
# Path to the arch.env to source:
source ../trunk/LMDZ.COMMON/arch.env

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

# Name of executable for reshaping PCM data with XIOS:
exeReshape="reshape_XIOS_output_64x48x32_phymars_seq.e"

# Argument for the PEM execution (for SLURM: "$SLURM_JOB_ID" | for PBD/TORQUE: "$PBS_JOBID" | "" when the script is not run as a job):
arg_pem="$SLURM_JOB_ID"
########################################################################


ulimit -s unlimited

# Reshaping PCM data with XIOS
echo "Reshaping PCM data with XIOS is starting."
./$exeReshape
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

# Running the PEM
read i_myear n_myear convert_years iPCM iPEM nPCM nPCM_ini < info_PEM.txt
echo "Run PEM $iPEM is starting."
cp run_PEM.def run.def
eval "./$exePEM $arg_pem > out_runPEM${iPEM} 2>&1"
if [ ! -f "restartfi.nc" ] || ! (tail -n 100 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}
if [ -f "diagpem.nc" ]; then
    mv diagpem.nc diags/diagpem${iPEM}.nc
fi
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

# Launch the next cycle
#if [ "$(awk 'END{print $NF}' info_PEM.txt)" -eq 7 ]; then
#    read i_myear n_myear convert_years iPCM iPEM nPCM nPCM_ini < info_PEM.txt
#    ./launchPEM.sh cont # Continue the PEM run if it stopped because of job time limit
#else
    ./launchPEM.sh new
#fi
