#!/bin/bash

# Running on chrysalis

#SBATCH  --job-name=global_time_series_0001-0020
#SBATCH  --account=e3sm
#SBATCH  --nodes=1
#SBATCH  --output=test_bash_generation_output/post/scripts/global_time_series_0001-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

# Get jobid
id=${SLURM_JOBID}

# Update status file
STARTTIME=$(date +%s)
cd test_bash_generation_output/post/scripts
echo "RUNNING ${id}" > global_time_series_0001-0020.status

# Generate global time series plots

# Names
moc_file=mocTimeSeries_0001-0020.nc
experiment_name=case_name
figstr=global_ts
case=case_name

# Years
start_yr=1
end_yr=20
ts_num_years=10

# Paths
www=www/path
case_dir=test_bash_generation_output
global_ts_dir=test_bash_generation_output/post/scripts/global_time_series_0001-0020_dir

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

export CDMS_NO_MPI=true

use_atm=True
if [[ ${use_atm,,} == "true" ]]; then
    echo 'Create xml files for atm'
    cd ${case_dir}/post/atm/glb/ts/monthly/${ts_num_years}yr
    cdscan -x glb.xml *.nc
    if [ $? != 0 ]; then
      cd test_bash_generation_output/post/scripts
      echo 'ERROR (1)' > global_time_series_0001-0020.status
      exit 1
    fi
fi

use_lnd=False
if [[ ${use_lnd,,} == "true" ]]; then
    echo 'Create xml files for lnd'
    cd ${case_dir}/post/lnd/glb/ts/monthly/${ts_num_years}yr
    cdscan -x glb.xml *.nc
    if [ $? != 0 ]; then
      cd test_bash_generation_output/post/scripts
      echo 'ERROR (2)' > global_time_series_0001-0020.status
      exit 2
    fi
fi

use_ocn=True
if [[ ${use_ocn,,} == "true" ]]; then
    echo 'Create ocean time series'
    cd ${global_ts_dir}
    mkdir -p ${case_dir}/post/ocn/glb/ts/monthly/${ts_num_years}yr
    input=./archive/ocn/hist
    python ocean_month.py ${input} ${case_dir} ${start_yr} ${end_yr} ${ts_num_years}
    if [ $? != 0 ]; then
      cd test_bash_generation_output/post/scripts
      echo 'ERROR (3)' > global_time_series_0001-0020.status
      exit 3
    fi

    echo 'Create xml for for ocn'
    export CDMS_NO_MPI=true
    cd ${case_dir}/post/ocn/glb/ts/monthly/${ts_num_years}yr
    cdscan -x glb.xml mpaso.glb*.nc
    if [ $? != 0 ]; then
      cd test_bash_generation_output/post/scripts
      echo 'ERROR (4)' > global_time_series_0001-0020.status
      exit 4
    fi

    echo 'Copy moc file'
    cd ${case_dir}/post/analysis/mpas_analysis/cache/timeseries/moc
    cp ${moc_file} ../../../../../ocn/glb/ts/monthly/${ts_num_years}yr/
    if [ $? != 0 ]; then
      cd test_bash_generation_output/post/scripts
      echo 'ERROR (5)' > global_time_series_0001-0020.status
      exit 5
    fi

fi

echo 'Update time series figures'
cd ${global_ts_dir}
atmosphere_only=False
python coupled_global.py ${case_dir} ${experiment_name} ${figstr} ${start_yr} ${end_yr} Blue ${ts_num_years} net_toa_flux_restom,global_surface_air_temperature,toa_radiation,net_atm_energy_imbalance,change_ohc,max_moc,change_sea_level,net_atm_water_imbalance ${atmosphere_only,,} None None None None glb,n,s
if [ $? != 0 ]; then
  cd test_bash_generation_output/post/scripts
  echo 'ERROR (6)' > global_time_series_0001-0020.status
  exit 6
fi


echo 'Copy images to directory'
results_dir=global_time_series_0001-0020_results
results_dir_absolute_path=test_bash_generation_output/post/scripts/${results_dir}
mkdir -p ${results_dir_absolute_path}
cp *.pdf ${results_dir_absolute_path}
cp *.png ${results_dir_absolute_path}

################################################################################
# Copy output to web server
echo
echo ===== COPY FILES TO WEB SERVER =====
echo

# Create top-level directory
f=${www}/${case}/global_time_series
mkdir -p ${f}
if [ $? != 0 ]; then
  cd test_bash_generation_output/post/scripts
  echo 'ERROR (7)' > global_time_series_0001-0020.status
  exit 7
fi



# Copy files
rsync -a --delete ${results_dir_absolute_path} ${www}/${case}/global_time_series
if [ $? != 0 ]; then
  cd test_bash_generation_output/post/scripts
  echo 'ERROR (8)' > global_time_series_0001-0020.status
  exit 8
fi




# For LCRC, change permissions of new files
pushd ${www}/${case}/global_time_series
chmod -R go+rX,go-w ${results_dir}
popd


################################################################################
# Update status file and exit

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

echo ==============================================
echo "Elapsed time: $ELAPSEDTIME seconds"
echo ==============================================
cd test_bash_generation_output/post/scripts
rm -f global_time_series_0001-0020.status
echo 'OK' > global_time_series_0001-0020.status
exit 0