#!/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 1
# number of MPI tasks n
#MSUB -n 1
# number of nodes to use N
#MSUB -N 1
# max job run time T (in seconds)
#MSUB -T 860
#MSUB -Q test
# 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=1
export OMP_STACKSIZE=400M

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

## set starting PEM first and last number call: 
num_PEM_now=1
num_PEM_end=20
(( 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_now $num_GCM_now"
echo "num_GCM_previous $num_GCM_previous"
echo "num_GCM_end $num_GCM_end"

echo "$num_GCM_previous" > num_run

  \cp -f startfi$num_GCM_previous.nc startfi.nc
  ./testphys1d_29_phymars_seq.e > lrun$num_GCM_now 2>&1
  \cp -f diagfi.nc diagfi$num_GCM_now.nc
  \cp -f diagfi.nc data2reshape1.nc
  \cp -f restartfi.nc startfi${num_GCM_now}.nc
  \cp -f restartfi.nc startfi.nc

  ./testphys1d_29_phymars_seq.e > lrun$num_GCM_end 2>&1
  \cp -f diagfi.nc diagfi$num_GCM_end.nc
  \cp -f diagfi.nc data2reshape2.nc
  \cp -f restartfi.nc startfi${num_GCM_end}_GCM.nc
  \cp -f restartfi.nc startfi_evol.nc

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

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

./reshape_XIOS_output_32x24x27_phymars_seq.e

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

cp datareshaped1.nc data_GCM_Y1.nc
echo "y"
cp 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_29_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_end}_post_PEM.nc
echo "y"
cp restartfi_evol.nc startfi${num_GCM_end}.nc
echo "y"

(( num_PEM_next = $num_PEM_now + 1 ))

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_pem1d_$num_PEM_now tmp
  sed -e "s@run_pem1d_${num_PEM_now}@run_pem1d_${num_PEM_next}@" \
      -e "s@num_PEM_now=${num_PEM_now}@num_PEM_now=${num_PEM_next}@" tmp > run_pem1d_$num_PEM_next
  rm tmp
  
  ccc_msub run_pem1d_$num_PEM_next
fi
