#!/bin/bash
# Partition to run on:
#MSUB -q rome
# project to run on 
#MSUB -A  gen10391
# disks to use
#MSUB -m  scratch,work,store
# Job name
#MSUB -r run_PEM
# Job standard output:
#MSUB -o run_PEM.%I
# Job standard error:
#MSUB -e run_PEM.%I
# number of OpenMP threads c
#MSUB -c 5
# number of MPI tasks n
#MSUB -n 24
# number of nodes to use N
#MSUB -N 1
# max job run time T (in seconds)
#MSUB -T 86000
# request exclusive use of the node (128 cores)
#MSUB -x

# 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=5
export OMP_STACKSIZE=400M

set -xv
ls -al

#------------------Modify here the number of PEM call----------------------

## set starting PEM first and last number call: 
num_PEM_now=1
num_PEM_end=10
(( num_PEM_previous = $num_PEM_now - 1 ))

#-----------------Modify here the number of GCM call between each PEM ---------

## set number of GCM call between each PEM call: 
nb_GCM_call=2

#----------------Here we launch GCM call year by year ---------

(( num_GCM_now = ($num_PEM_now - 1)* $nb_GCM_call + 1 ))
(( num_GCM_previous = $num_GCM_now - 1 ))
(( num_GCM_end = $num_GCM_previous + $nb_GCM_call ))
echo "$num_GCM_previous" > num_run

while [ $num_GCM_now -le $num_GCM_end ]
do
  sed s/9999/669/ run.def.ref > run.def
  ./run0 >> error 
  ((num_GCM_now++))
  (( num_GCM_previous = $num_GCM_now - 1 ))
  echo "$num_GCM_previous" > num_run
done

#--------------- We reshape the GCM data from XIOS-------------------

(( num_GCM_previous_previous = $num_GCM_previous - 1 ))

cp Xdiurnalave${num_GCM_previous_previous}.nc data2reshape1.nc
echo "y"
cp Xdiurnalave${num_GCM_previous}.nc data2reshape2.nc
echo "y"

rm -rf datareshaped1.nc
rm -rf datareshaped2.nc

./reshape_XIOS_output_64x48x54_phymars_seq.e

#---------------- We rename files for the PEM call----------------

cp startfi${num_GCM_previous}.nc startfi_evol.nc
echo "y"
cp startfi${num_GCM_previous}.nc startfi${num_GCM_previous}_GCM.nc

cp start${num_GCM_previous}.nc start_evol.nc
echo "y"
cp start${num_GCM_previous}.nc start${num_GCM_previous}_GCM.nc

mv datareshaped1.nc data_GCM_Y1.nc
echo "y"
mv datareshaped2.nc data_GCM_Y2.nc
echo "y"

#--------------- We run the PEM ------------------

if [[ -r run.def_PEM ]] ; then
  cp run.def_PEM run.def
  echo "y"
fi
if [[ -r diagfi.def_PEM ]] ; then
  cp diagfi.def_PEM diagfi.def
  echo "y"
fi

./pem_64x48x54_phymars_seq.e > PEMrun${num_PEM_now} 2>&1

if [[ -r run.def_GCM ]] ; then
  cp run.def_GCM run.def
  echo "y"
fi
if [[ -r diagfi.def_GCM ]] ; then
  cp diagfi.def_GCM diagfi.def
  echo "y"
fi

#--------------- We rename PEM output ------------------

cp restartfi_evol.nc startfi${num_GCM_previous}_post_PEM.nc
cp restartfi_evol.nc startfi${num_GCM_previous}.nc
echo "y"

cp restart_evol.nc start${num_GCM_previous}_post_PEM.nc
cp restart_evol.nc start${num_GCM_previous}.nc
echo "y"

(( num_PEM_next = $num_PEM_now + 1 ))

cp restartfi_PEM.nc startfi_PEM${num_PEM_now}.nc
cp restartfi_PEM.nc startfi_PEM.nc
echo "y"

if [[ -r diagfi.nc ]] ; then
  \mv -f diagfi.nc diagfi${num_PEM_now}_PEM.nc
fi

#------------------- Launch the next PEM call------------------

# launch job for next month
if (( $num_PEM_next <= $num_PEM_end )) ; then
  cp -f run_pem$num_PEM_now tmp
  sed -e "s@run_pem${num_PEM_now}@run_pem${num_PEM_next}@" \
      -e "s@num_PEM_now=${num_PEM_now}@num_PEM_now=${num_PEM_next}@" tmp > run_pem$num_PEM_next
  rm tmp
  
  ccc_msub run_pem$num_PEM_next
fi
