#!/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 ] || [ $amip_mode = co2 ]; then
    ln -sf $datadir/forcing/siconcbcs*.nc  $rundir/.
    ln -sf $datadir/forcing/tosbcs*.nc  $rundir/.
    ln -sf $datadir/forcing/rmp_AMIP_to_${ifs_grid}_GAUSWGT.nc $rundir/.
    if [ $amip_mode = co2 ]; then
        ln -sf $datadir/co2/CO2-em-anthro_*.nc $rundir/.
        ln -sf $datadir/co2/rmp_CO2_to_${ifs_grid}_GAUSWGT*.nc $rundir/.
    fi
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 ] || [ $lang = pythoncompat ]; then
    cp -f $datadir/$amip_mode/masks.nc  $rundir/.
    cp -f $datadir/$amip_mode/grids.nc  $rundir/.
    cp -f $datadir/$amip_mode/areas.nc  $rundir/.
else
    cp -f $datadir/grids-noamip.nc  $rundir/grids.nc
    cp -f $datadir/masks-noamip.nc  $rundir/masks.nc
    cp -f $datadir/areas-noamip.nc  $rundir/areas.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 ] || [ $amip_mode = co2 ]; then
        sst_conf_var="Vars(1,:) = '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="Vars(2,:) = '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,"
        if [ $amip_mode = co2 ]; then
            co2_conf_var="Vars(3,:) = 'CO2_monthly', 'CO2', 'AMIP_emissions', 'CO2-em-anthro_input4MIPs_emissions_CMIP_CEDS-2017-05-18_gn_200001-201412.nc', 'CO2_em_anthro', 1750, 2018, 'monthly', ${amip_interpolate}, 1, 0, , ,"
        fi
    else
        sst_conf_var="Vars(1,:) = 'AMIP_sst_daily', 'PSST', 'AMIP_sst', 'HadISST2_prelim_0to360_alldays_sst_[year].nc', 'sst', 1850, 2016, 'daily', false, 1, 0, , ,"
        sic_conf_var="Vars(2,:) = '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
elif [ $amip_mode = co2 ]; then
    . ./namcouple_co2.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 

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