#/bin/bash
#
# computes the enstemble statistics for the by_var files
# 
# Ralf Hand, 27.07.2023

set -ex

# dataset="ModE-Sim"
# 
# setlist="set_1420-1 set_1420-2 set_1420-3 set_1420-1_to_3 set_1850-1 set_1850-2 set_1850-1_to_2"
# varlist="u v q geopoth omega"
# levellist="10000 20000 30000 50000 70000 85000 100000"
# statlist="ensmean ensstd ensmin ensmax enspctl125 enspctl875"
# statlist="ensmean ensstd ensmin ensmax"
# 
# 
# # setlist="H-1400-1 H-1850-1"
# varlist="temp2 totprec slp"
# levellist="toasurf"
# statlist="ensmean ensstd ensmin ensmax enspctl125 enspctl875"
# tslist="mon year mam jja son djf"

# dataset="ModE-RA"
# setlist="lowres_20mem_Set_1420-3_1850-1 hires_5mem"

# dataset="ModE-Sim"
# setlist="set_1420-1 set_1420-2 set_1420-3 set_1420-1_to_3 set_1850-1 set_1850-2 set_1850-1_to_2 set_H-1400-1 set_H-1850-1 set_H-1870-1"

# varlist="geopoth omega u v q"
# levellist="10000 20000 30000 50000 70000 85000 100000"
# statlist="ensmean ensstd ensmin ensmax enspctl125 enspctl875"
# tslist="mon year mam jja son djf"


dataset="ModE-RAclim"
setlist="ModE-RAclim_anom_after_assim_71yr_highpassfiltered_observations"
varlist="temp2 precip slp geopoth_50000"
statlist="ensmean ensstd ensmin ensmax enspctl125 enspctl875"
tslist="mon year mam jja son djf"



base_dir=/tank/exports/ERC_PALAEO

for ts in ${tslist}; do
   for var in ${varlist}; do
      for level in ${levellist}; do
   
         case ${level} in
            toasurf) varname=${var} ;;
            *)       varname=${var}_${level} ;;
         esac
   
         for set in ${setlist}; do
   
            outdir=/${base_dir}/${dataset}/outdata/${set}/abs/ensstat/by_var/${ts}
            if [ ! -d ${outdir} ] ; then mkdir -p ${outdir}; fi
   
            case ${set} in
               set_H-1400-1)                   fyear=1400; lyear=1849 ;;
               set_1420-*)                     fyear=1420; lyear=1849 ;;
               set_H-1850-1|set_1850-*)        fyear=1850; lyear=2009 ;;
               set_H-1870-1)                   fyear=1870; lyear=1919 ;;
               lowres_20mem_Set_1420-3_1850-1) fyear=1420; lyear=2009 ;;
	            hires_5mem)                     fyear=1421; lyear=2008 ;;
            esac
   
            case ${set} in
               set_1420-?|set_1850-?|set_H-1400-1|set_H-1850-1|set_H-1870-1)
                  infiles=$(ls ${base_dir}/${dataset}/outdata/${set}/abs/m???/by_var/${ts}/${dataset}_${set}_m???_${varname}_abs_${fyear}-${lyear}_${ts}.nc )
                  ;;
               set_1420-1_to_3)
                  infiles=$(ls ${base_dir}/ModE-Sim/outdata/set_1420-{1,2,3}/abs/m???/by_var/${ts}/ModE-Sim_set_1420-[123]_m???_${varname}_abs_${fyear}-${lyear}_${ts}.nc)
                  ;;
               set_1850-1_to_2)
                  infiles=$(ls ${base_dir}/ModE-Sim/outdata/set_1850-{1,2}/abs/m???/by_var/${ts}/ModE-Sim_set_1850-[12]_m???_${varname}_abs_${fyear}-${lyear}_${ts}.nc)
                  ;;
               lowres_20mem_Set_1420-3_1850-1|hires_5mem)
                  infiles=$(ls ${base_dir}/ModE-RA/outdata/${set}/abs/m???/by_var/${ts}/ModE-RA_${set}_m???_${varname}_abs_${fyear}-${lyear}_${ts}.nc)
                  ;;
               ModE-RAclim_anom_after_assim_71yr_highpassfiltered_observations
                  infiles=$(ls ${base_dir}/ModE-RAclim/outdata/${set}//m???/by_var/${ts}/ModE-RA_${set}_m???_${varname}_abs_${fyear}-${lyear}_${ts}.nc)


            esac  # set
            
            for stat in ${statlist}; do
   
               case ${stat} in
                  ensmean|ensstd|ensmin|ensmax) enscmd=${stat} ;;
                  enspctl125) enscmd="enspctl,12.5" ;;
                  enspctl875) enscmd="enspctl,87.5" ;;
               esac # stat
   
               outfile=${outdir}/${dataset}_${set}_${stat}_${varname}_abs_${fyear}-${lyear}_${ts}.nc
   
               cdo -O --no_history -setreftime,1400-01-01,00:00:00 -${enscmd} ${infiles} ${outfile}
   
               case ${stat} in
                  ensmean) member_cmd="ensemble mean of set $(echo ${set} | sed -e "s/set_//" | sed -e "s/_/ /g")" ;;
                  ensstd) member_cmd="ensemble standard deviation of set $(echo ${set} | sed -e "s/set_//" | sed -e "s/_/ /g")" ;;
                  ensmin) member_cmd="ensemble minimum of set $(echo ${set} | sed -e "s/set_//" | sed -e "s/_/ /g")" ;;
                  ensmax) member_cmd="ensemble maximum of set $(echo ${set} | sed -e "s/set_//" | sed -e "s/_/ /g")" ;;
                  enspctl125)  member_cmd="12.5th ensemble percentile of set $(echo ${set} | sed -e "s/set_//" | sed -e "s/_/ /g")" ;;
                  enspctl875)  member_cmd="87.5th ensemble percentile of set $(echo ${set} | sed -e "s/set_//" | sed -e "s/_/ /g")" ;;
               esac # member
   
               ncatted -h \
                  -a set,global,m,c,"${set}" \
                  -a member,global,m,c,"${member_cmd}" \
                  -a creation_date,global,m,c,"$(date +"%Y-%m-%d %H:%M:%S %Z")" \
                 ${outfile}
   
            done # stat         
         done # set
      done # level
   done # var
done # ts

exit 0
