#!/bin/bash
## Usage
if [ $# -ne 8 ]; then
    echo 'Invalid args.'
    echo 'Usage ./run_example.sh <lang> <amip_mode> <ifs_nproc> <leg_start_date> <leg_end_date> <ifs_cmip_fixyear> <ifs_grid> <amip_interpolate>'
    echo 'Try ./run_example.sh python forcing 4 1990-01-01 1991-01-01 0 L128 true'
    exit 1
fi

lang=$1
amip_mode=$2 # forcing or primavera
# - Define number of processes to run each executable
nproc_exe1=$3
nproc_exe2=1 # AMIP is always one process

leg_start_date=$4
leg_end_date=$5
ifs_cmip_fixyear=$6
ifs_grid=$7
amip_interpolate=$8

host=`uname -n`
user=`whoami`

## - Define paths
srcdir=`pwd`
datadir=$srcdir/data


exe1='python3 IFS_mock.py'
source_exe1=$srcdir/IFS_mock.py

if [ $lang = fortran ]; then
    exe2='./amip-forcing'
    if [ $amip_mode = forcing ]; then
        source_exe2=$srcdir/../sources/amip-forcing/bin/amip-forcing
    else
        source_exe2=$srcdir/../sources/amip-primavera/bin/amip-forcing
    fi
elif [ $lang = python ] || [ $lang = pythoncompat ]; then
    exe2='python3 amip_forcing.py'
    # exe2='coverage run -p --branch --include=*/amip-forcing/src/python/* amip_forcing.py'
    # exe2='python3 -m cProfile -s cumtime amip_forcing.py'
    source_exe2=$srcdir/../sources/amip-forcing/src/python/*.py
else
    echo 'Invalid args. You must specify a language for the AMIP reader: <fortran | python> | <pythoncompat>'
    exit 1
fi

# - Define rundir
rundir=${srcdir}/work_${lang}_${amip_mode}_${nproc_exe1}_${leg_start_date}_${leg_end_date}_${ifs_cmip_fixyear}_${ifs_grid}_${amip_interpolate}
# export COVERAGE_FILE=${rundir}/../.coverage

echo '*****************************************************************'
echo '*** '$casename' : '$run
echo ''
echo 'Rundir       :' $rundir
echo 'Architecture :' $arch
echo 'Host         : '$host
echo 'User         : '$user
echo ''
echo $exe1' runs on '$nproc_exe1 'processes'
echo $exe2' runs on '$nproc_exe2 'processes'
echo ''
######################################################################
### Create rundir and copy everything needed

\rm -fr $rundir
mkdir -p $rundir
ln -sf $datadir/ICMGGa22e+000000  $rundir/.
ln -sf $datadir/*.txt  $rundir/.
# Get SST and SIC
if [ $amip_mode = forcing ]; then
    ln -sf $datadir/$amip_mode/siconcbcs*.nc  $rundir/.
    ln -sf $datadir/$amip_mode/tosbcs*.nc  $rundir/.
    ln -sf $datadir/$amip_mode/rmp_AMIP_to_${ifs_grid}_GAUSWGT.nc $rundir/.
else
    ln -sf $datadir/$amip_mode/HadI*.nc  $rundir/.
    ln -sf $datadir/$amip_mode/rmp_PSIC_to_${ifs_grid}_GAUSWGT.nc $rundir/.
    ln -sf $datadir/$amip_mode/rmp_PSST_to_${ifs_grid}_GAUSWGT.nc $rundir/.
fi
# Get grids and masks
if [ $lang = fortran ]; then
    ln -sf $datadir/$amip_mode/masks.nc  $rundir/.
    ln -sf $datadir/$amip_mode/grids.nc  $rundir/.
else
    ln -sf $datadir/amip-oasis-data.nc  $rundir/.
    cp -f $datadir/grids-noamip.nc  $rundir/grids.nc
    cp -f $datadir/masks-noamip.nc  $rundir/masks.nc
fi
cp -f $datadir/*.sh  $rundir/.
cp -f $source_exe1 $rundir/.
ln -sf $source_exe2 $rundir/.
cd $rundir

### Generate namelist
leg_length_sec=$(( $(date -u -d "${leg_end_date}" +%s) - $(date -u -d "${leg_start_date}" +%s) ))
cpl_freq_amip_sec=86400 # Always
leg_start_date_yyyymmdd=$(date -u -d "${leg_start_date}" +%Y%m%d)

if [ $lang = fortran ] || [ $lang = pythoncompat ]; then
    if [ $amip_mode = forcing ]; then
        . ./namelist.amip.sh > ./namelist.amip
    else
        . ./namelist_primavera.amip.sh > ./namelist.amip
    fi
else
    if [ $amip_mode = forcing ]; then
        sst_conf_var="'AMIP_sst_monthly', 'AMIP', 'AMIP_sst', 'tosbcs_input4MIPs_SSTsAndSeaIce_CMIP_PCMDI-AMIP-1-1-3_gn_187001-201706.nc', 'tosbcs', 1870, 2016, 'monthly', ${amip_interpolate}, 1, 273.15, 271.38, ,"
        sic_conf_var="'AMIP_sic_monthly', 'AMIP', 'AMIP_sic', 'siconcbcs_input4MIPs_SSTsAndSeaIce_CMIP_PCMDI-AMIP-1-1-3_gn_187001-201706.nc', 'siconcbcs', 1870, 2016, 'monthly', ${amip_interpolate}, 0.01, 0, 0, 1,"
    else
        sst_conf_var="'AMIP_sst_daily', 'PSST', 'AMIP_sst', 'HadISST2_prelim_0to360_alldays_sst_[year].nc', 'sst', 1850, 2016, 'daily', false, 1, 0, , ,"
        sic_conf_var="'AMIP_sic_daily', 'PSIC', 'AMIP_sic', 'HadISST2_prelim_0to360_alldays_sic_[year].nc', 'sic', 1850, 2016, 'daily', false, 1, 0, , ,"
    fi
    . ./namelist_python.amip.sh > ./namelist.amip
fi
if [ $amip_mode = forcing ]; then
    . ./namcouple.sh > ./namcouple
else
    . ./namcouple_primavera.sh > ./namcouple
fi

######################################################################
### Definition of mpirun command and batch script

echo 'Executing the model using mpirun'
mpirun -np $nproc_exe1 $exe1 : -np $nproc_exe2 $exe2

echo $casename 'is executed or submitted to queue.'
echo 'Results are found in rundir : '$rundir 

######################################################################
