#!/bin/bash

#PBS -N {{ job_name }}
#PBS -A {{ account }}
#PBS -q {{ queue }}
#PBS -l job_priority={{ job_priority }}
#PBS -l select=8:ncpus=128:mpiprocs=2:ompthreads=64+3:ncpus=128:mpiprocs=1:ompthreads=128
#PBS -l walltime={{ walltime }}
#PBS -j oe
#PBS -m abe

echo "Job $PBS_JOBID started at `date` on `hostname` in directory `pwd`."

echo 'Loading modules.'
module --force purge
{%- for module in modules %}
module load {{ module }}
{%- endfor %}
echo 'The currently loaded modules are:'
module list

echo 'Loading python environment.'
mage_test_root=$HOME
if [ -d "${mage_test_root}/miniconda3" ]; then
    echo 'Loading local miniconda3'
    mage_miniconda3="${mage_test_root}/miniconda3"
    mage_conda="${mage_miniconda3}/bin/conda"
    __conda_setup="$($mage_conda 'shell.bash' 'hook' 2> /dev/null)"
    if [ $? -eq 0 ]; then
        eval "$__conda_setup"
    else
        if [ -f "$mage_miniconda3/etc/profile.d/conda.sh" ]; then
            . "$mage_miniconda3/etc/profile.d/conda.sh"
        else
            export PATH="$mage_miniconda3/bin:$PATH"
        fi
    fi
    unset __conda_setup
else
    echo 'Loading conda module'
    module load conda
fi
conda activate {{ conda_environment }}
echo "The current conda environment is ${CONDA_PREFIX}."

echo 'Setting up MAGE environment.'
source {{ kaijuhome }}/scripts/setupEnvironment.sh
echo "The kaiju software is located at ${KAIJUHOME}."

echo 'Setting environment variables.'
export TMPDIR={{ tmpdir }}
export SLACK_BOT_TOKEN={{ slack_bot_token }}
export OMP_NUM_THREADS=128
export MPI_TYPE_DEPTH=32
export KMP_STACKSIZE=128M
export MAGE_TEST_ROOT="{{ mage_test_root }}"
export MAGE_TEST_SET_ROOT={{ mage_test_set_root }}
export BRANCH_OR_COMMIT={{ branch_or_commit }}

echo 'The active environment variables are:'
printenv

# Generate the LFM grid file.
echo 'Generating LFM grid file.'
{{ genLFM_cmd }}
echo "The LFM grid file is `ls lfm?.h5`."

# Generate the solar wind boundary condition file.
echo 'Generating the solar wind boundary condition file.'
{{ cda2wind_cmd }}
echo "The solar wind boundary condition file is `ls bcwind.h5`."

# Generate the Raiju configuration file.
echo 'Generating the Raiju configuration file.'
{{ genRaiju_cmd }}
echo "The Raiju configuration file is `ls raijuconfig.h5`."

# Run the model.
MPICOMMAND="{{ mpiexec_cmd }}"
cmd="$MPICOMMAND {{ voltron_cmd }} >& weeklyDashGo.out"
echo "Model run command is:"
echo $cmd
eval $cmd

echo "Job $PBS_JOBID ended at `date` on `hostname` in directory `pwd`."
