#!/bin/bash

# slurm specific options for Marenostrum4
#SBATCH -p interactive
#SBATCH -N 1
#SBATCH -n 3
#SBATCH -t 0:10:00
##SBATCH --exclusive

#load python and netcdf modules
export PYTHONPATH=""
module load python/3.6.1
module load netcdf/4.2

set -xuve

# setup pyOasis environment
CURRENT_PATH=$(pwd)
ROOTDIR=${CURRENT_PATH}/../../
export PYOASIS_ROOT=${ROOTDIR}/sources/oasis3-mct/generated
export LD_LIBRARY_PATH=${PYOASIS_ROOT}/lib:${LD_LIBRARY_PATH}
export PYTHONPATH=${PYOASIS_ROOT}/python:${PYTHONPATH}
export MPIRUN4PY="mpirun"

# prepare rundir
rundir=run/
srcdir=`pwd`
datadir=/gpfs/scratch/bsc32/bsc32442/python-amip-reader/data/ece3-toy-model

rm -rf $rundir
mkdir -p $rundir

for f in masks.nc grids.nc areas.nc; do
    cp -f $datadir/$f $rundir
done
cp -f $datadir/rmp* $rundir
cp -f $srcdir/*.py $rundir
cp -f $srcdir/namcouple $rundir
cp -f $srcdir/*.yaml $rundir

# Copy the restart files
cp -f $srcdir/rstas.nc $rundir
cp -f $srcdir/rstos.nc $rundir

cd $rundir

# run IFS+NEMO+runoff toy model
ifs_nproc=3
nemo_nproc=2
rnf_neproc=1

toy_model_script=$srcdir/toy_model.py

mpirun -np $ifs_nproc python3 $toy_model_script --yaml_file_path ifs_toy_model.yaml : -np $nemo_nproc python3 $toy_model_script --yaml_file_path nemo_toy_model.yaml : -np $rnf_neproc python3 $toy_model_script --yaml_file_path runoff_toy_model.yaml
