#!/bin/bash

#PBS -N atm_averages
#PBS -q regular
#PBS -l select=4:ncpus=18:mpiprocs=18
#PBS -l walltime=06:00:00
#PBS -A P19010000

source /etc/profile.d/modules.sh

export MPI_UNBUFFERED_STDIO=true
export TMPDIR=$TMPDIR


##########
##
## See https://github.com/NCAR/CESM_postprocessing/wiki for details
## regarding settings for optimal performance for CESM postprocessing tools.
##
##########

if [ ! -e /glade/p/cesm/postprocessing/cesm-env2/bin ]; then
    echo "*************************************************************************************"
    echo "CESM atm_averages exiting due to non-existant python virtual environment in"
    echo "    /glade/p/cesm/postprocessing/cesm-env2/bin"
    echo "You must first run:"
    echo "$POSTPROCESS_PATH/create_python_env -machine [machine]"
    echo "*************************************************************************************"
    exit
fi


module purge




## activate the virtualenv that contains all the non-bootstrapped dependencies

cd /glade/p/cesm/postprocessing/cesm-env2/bin
echo "Running from virtualenv directory:"
pwd
. activate

## load the boot-strap modules 


module load gnu/8.3.0

module load ncarenv

module load ncarcompilers

module load mpt/2.19

module load netcdf/4.6.3

module load nco/4.7.9

module load ncl/6.6.2





today="$(date '+%Y%m%d-%H%M%S')"
log_filename=/glade/p/cesm/chwg_dev/tilmes/cases/cesm2_ch/fire/b.e21.B1850.1750landuse.f09_g17.CMIP6-piControl.cesm2_ctsm5.1_fire6/postprocess/logs/atm_averages.log.$today


mpiexec_mpt dplace -s 1 ./atm_avg_generator.py --debug 0  --caseroot /glade/p/cesm/chwg_dev/tilmes/cases/cesm2_ch/fire/b.e21.B1850.1750landuse.f09_g17.CMIP6-piControl.cesm2_ctsm5.1_fire6/postprocess >> ${log_filename} 2>&1

