#!/bin/bash

# Running on chrysalis

#SBATCH  --job-name=climo_atm_monthly_diurnal_8xdaily_180x360_aave_0011-0020
#SBATCH  --account=e3sm
#SBATCH  --nodes=4
#SBATCH  --output=test_bash_generation_output/post/scripts/climo_atm_monthly_diurnal_8xdaily_180x360_aave_0011-0020.o%j
#SBATCH  --exclusive
#SBATCH  --time=02:00:00


#SBATCH  --partition=haswell


...

# Turn on debug output if needed
debug=False
if [[ "${debug,,}" == "true" ]]; then
  set -x
fi

# Script dir
cd test_bash_generation_output/post/scripts

# Get jobid
id=${SLURM_JOBID}

# Update status file
STARTTIME=$(date +%s)
echo "RUNNING ${id}" > climo_atm_monthly_diurnal_8xdaily_180x360_aave_0011-0020.status

# Create temporary workdir
workdir=`mktemp -d tmp.${id}.XXXX`
cd ${workdir}



# --- Diurnal cycle climatologies ---

# Create symbolic links to input files
input=./archive/atm/hist
for (( year=11; year<=20; year++ ))
do
  YYYY=`printf "%04d" ${year}`
  for file in ${input}/case_name.eam.h4.${YYYY}-*.nc
  do
    ln -s ${file} .
  done
done

# Add the last file of the previous year
year=10
YYYY=`printf "%04d" ${year}`
mapfile -t files < <( ls ${input}/case_name.eam.h4.${YYYY}-*.nc 2> /dev/null )
if [ ${#files[@]} -ne 0 ]
then
  ln -s ${files[-1]} .
fi
# as well as first file of next year to ensure that first and last years are complete
year=21
YYYY=`printf "%04d" ${year}`
mapfile -t files < <( ls ${input}/case_name.eam.h4.${YYYY}-*.nc 2> /dev/null )
if [ ${#files[@]} -ne 0 ]
then
  ln -s ${files[0]} .
fi

ls case_name.eam.h4.????-*.nc > input.txt
if grep -q "*" input.txt; then
  cd test_bash_generation_output/post/scripts
  echo 'Missing input files'
  echo 'ERROR (1)' > climo_atm_monthly_diurnal_8xdaily_180x360_aave_0011-0020.status
  exit 1
fi
# Now, call ncclimo
cat input.txt | ncclimo \
--case=case_name.eam.h4 \
--jobs=${SLURM_NNODES} \
--thr=1 \
--vars=PRECT \
--parallel=mpi \
--yr_srt=11 \
--yr_end=20 \
--map=/home/ac.zender/data/maps/map_ne30pg2_to_cmip6_180x360_aave.20200201.nc \
--output=trash \
--regrid=output \
--climatology_mode=hfc



if [ $? != 0 ]; then
  cd test_bash_generation_output/post/scripts
  echo 'ERROR (3)' > climo_atm_monthly_diurnal_8xdaily_180x360_aave_0011-0020.status
  exit 3
fi

# Move regridded climo files to final destination
subdir=""

{
 subdir=clim_diurnal_8xdaily
}
dest=test_bash_generation_output/post/atm/180x360_aave/${subdir}/10yr
mkdir -p ${dest}
mv output/*.nc ${dest}

if [ $? != 0 ]; then
  cd test_bash_generation_output/post/scripts
  echo 'ERROR (4)' > climo_atm_monthly_diurnal_8xdaily_180x360_aave_0011-0020.status
  exit 4
fi

# Delete temporary workdir
cd ..
if [[ "${debug,,}" != "true" ]]; then
  rm -rf ${workdir}
fi

# Update status file and exit

ENDTIME=$(date +%s)
ELAPSEDTIME=$(($ENDTIME - $STARTTIME))

echo ==============================================
echo "Elapsed time: $ELAPSEDTIME seconds"
echo ==============================================
rm -f climo_atm_monthly_diurnal_8xdaily_180x360_aave_0011-0020.status
echo 'OK' > climo_atm_monthly_diurnal_8xdaily_180x360_aave_0011-0020.status
exit 0