#!/bin/bash
#
#  ll ensemble members for the HIPPO_r0XXiXXXXpXfXXXX ensemble
# 
# ralf.hand@giub.unibe.ch 2021-03-03

set -ex 


# # normal time series
# 
# dir=/mnt/climstor/ERC_PALAEO
# setlist="HIPPO_set_1420_all_prelim"
# varlist="hadc_nh_loc hadc_sh_loc hadc_nh_str hadc_sh_str"
# level="50000"
# tslist="year djf jja"
# infile_struct=${dir}/SET/RUN/timeseries/TS/RUN_VAR_TS.nc
# outfile_struct=${dir}/SET/ensstat/timeseries/TS/RUN_VAR_ENSSTAT_TS.nc

# region means

dir=/scratch3/ralf/experiments/PALAEO-RA
setlist="HIPPO_set_1780_1 HIPPO_set_1780_2"
tslist="year djf"
level=surf
varlist="slp"
# infile_struct=${dir}/SET/RUN/indices/region_fldmean/TS/RUN_temp2_*_fldmean_RU_land.nc
# outfile_struct=${dir}/SET/ensstat/indices/region_fldmean/TS/SET_temp2_djf_fldmean_RU_land.png
infile_struct=${dir}/SET/RUN/indices/region_fldmean/TS/RUN_slp_*_fldmean_ICE.nc
outfile_struct=${dir}/SET/ensstat/indices/region_fldmean/TS/SET_slp_djf_fldmean_ICE.png

oldpwd=$(pwd)

for set in ${setlist}; do
   for var in ${varlist}; do
      for ts in ${tslist}; do
   
         tmp=$(mktemp -d ${TMP_BASEDIR}/tmp.ensstat.XXX)
         cd ${tmp}
         
         set +e; filelist=$(ls $(echo ${infile_struct} | sed -e "s&RUN&HIPPO_r\*i\*p\*f\*&g" -e "s&SET&${set}&g" -e "s&TS&${ts}&g" -e "s&VAR&${var}&g") 2>/dev/null); set -e
      
         echo -e "ensemble members to be included: \n ${filelist}"
      
         nfiles=$(echo ${filelist} | wc -w)
         echo "number of ensemble members to be included: ${nfiles}"

         outfile=$(eval echo ${outfile_struct} | sed -e "s&SET&${set}&g" -e "s&TS&${ts}&g" -e "s&VAR&${var}&g")
         echo "outfile will be written to: ${outfile}"

         for file in ${filelist}; do

            real=$(echo $(basename ${file}) | sed -e "s/HIPPO_\(r.*i\).*/\1/" -e "s/i//")
            echo ${real} 

            case ${level} in
               surf) sellevel="";;
               *)    sellevel="-sellevel,${level}";;
            esac # level

            cdo copy ${sellevel} ${file} infile.${real}.nc

            cat << __EOF__ | sed -e "s/^ *//" >> plot_load.py
               ds_${real}=xr.open_dataset('infile.${real}.nc')
               data_${real}=ds_${real}.${var}.isel(lon=0,lat=0)
               time_${real}=ds_${real}.time
               plt.plot(time_${real},data_${real},linewidth=2)

__EOF__

         done # file

         cat << __EOF__ | sed -e "s/^ *//" > plot_header.py
            import xarray as xr
            import numpy as np
            import matplotlib.pyplot as plt

            dpi = 100
            fig = plt.figure(figsize=(1100/dpi, 1100/dpi), dpi=dpi)
            ax  = fig.add_axes([0.1,0.1,0.8,0.9])

__EOF__


         cat << __EOF__ | sed -e "s/^ *//" > plot_footer.py
            plt.xlabel('time', fontsize=30)
            plt.ylabel('${var}', fontsize=30)
       
            # locs, labels=plt.xticks()
            # print(locs)
       
            ax.tick_params(axis='both', labelsize=20)
      
            plt.savefig('${outfile}', bbox_inches='tight', dpi=dpi)
__EOF__

         cat plot_header.py plot_load.py plot_footer.py > plot.py

         python3 plot.py
         echo "outfile written to ${outfile}"
       
         cd ${oldpwd}
         rm -rf ${tmp}

      done # ts
   done # var
done # set

exit 0
