#!/bin/bash
#
# computes annual and seasonal means for the ModE-Sim ensemble
# 
# ralf.hand@giub.unibe.ch 2020-03-25, adapted to ModE-Sim, 2023-04-04

set -ex 

export overviewfile=/tank/exports/ERC_PALAEO/ModE-Sim/docu/ModE-Sim_overview.csv

export dataset=ModE-Sim
# export setlist="set_1420-1 set_1420-2 set_1420-3 set_1850-1 set_1850-2"
# export varlist="temp2 totprec slp geopoth_50000"
# export tslist="mam son"
export setlist="set_1420-1 set_1420-2 set_1420-3 set_1850-1 set_1850-2 set_H-1400-1 set_H-1850-1 set_H-1870-1"
export setlist="set_H-1400-1 set_H-1850-1 set_H-1870-1"
export setlist="set_H-1870-1"
export varlist="u v q geopoth omega"
export levellist="10000 20000 30000 50000 70000 85000 100000"
export tslist="djf jja son mam year"

# export dataset=ModE-RA
# export setlist="lowres_20mem_Set_1420-3_1850-1 hires_5mem"
# export varlist="geopoth_50000"
# export tslist="djf jja son mam year"

col_final_fyear=$(head -1 ${overviewfile} | awk -F ";" -v b="final_fyear" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
col_final_lyear=$(head -1 ${overviewfile} | awk -F ";" -v b="final_lyear" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')

export dir=/tank/exports/ERC_PALAEO/${dataset}/outdata
export oldpwd=$(pwd)

for set in ${setlist}; do

   export set

   set +e; export memberlist=$(ls -d ${dir}/${set}/abs/m[0-9][0-9][0-9] 2>/dev/null | sed -e "s&.*/&&"); set -e
   # export memberlist="m004 m005"

   for member in ${memberlist}; do

      export member

      case ${dataset} in
         ModE-Sim)
                   fyear=$(grep "${set/set_/};${member}" ${overviewfile} | awk -F ';' "NR==1 {print \$$(echo ${col_final_fyear})}")
                   lyear=$(grep "${set/set_/};${member}" ${overviewfile} | awk -F ';' "NR==1 {print \$$(echo ${col_final_lyear})}")
                   ;;
         ModE-RA)
                    case ${set} in
                       lowres_20mem_Set_1420-3_1850-1) fyear=1420; lyear=2009 ;;
                       hires_5mem)                     fyear=1421; lyear=2008 ;;
                    esac # set
                    ;;
         ModE-RAclim)
                    fyear=1421
                    lyear=2008
                    ;;
      esac # dataset

      export fyear
      export lyear

      export mondir=${dir}/${set}/abs/${member}/by_var/mon
      if [ ! -d ${mondir} ]; then exit 1 ; fi

      for ts in ${tslist}; do

         export ts
         export tsdir=${dir}/${set}/abs/${member}/by_var/${ts}

         if [ ! -d ${tsdir} ]; then mkdir -p ${tsdir} ; fi

         unset var_levlist

         case ${levellist} in

            "")
                var_levlist=${varlist}
                ;;

            *)
                for var in ${varlist}; do

                   for level in ${levellist}; do

                      var_levlist="${var_levlist}\n${var}_${level}\n"

                   done # levellist

                done # var
                ;;

         esac # levellist (only one level?)

         function compute_timmeans {

            var_lev=${1}

            echo "computing timmeans for ${set} ${member} ${ts} ${var_lev}..."

            case ${ts} in
               mam|jja|son) 
                  cdo seasmean -selseas,${ts} ${mondir}/${dataset}_${set}_${member}_${var_lev}_abs_${fyear}-${lyear}_mon.nc ${tsdir}/${dataset}_${set}_${member}_${var_lev}_abs_${fyear}-${lyear}_${ts}.nc
                  ;;
               djf)
                  cdo seasmean -selseas,${ts} -seldate,${fyear}-03-01,${lyear}-11-30 ${mondir}/${dataset}_${set}_${member}_${var_lev}_abs_${fyear}-${lyear}_mon.nc ${tsdir}/${dataset}_${set}_${member}_${var_lev}_abs_${fyear}-${lyear}_${ts}.nc
                  ;; 
               year)
                  cdo yearmean ${mondir}/${dataset}_${set}_${member}_${var_lev}_abs_${fyear}-${lyear}_mon.nc ${tsdir}/${dataset}_${set}_${member}_${var_lev}_abs_${fyear}-${lyear}_${ts}.nc
                  ;;
            esac

         }
 
         export -f compute_timmeans

         echo -e ${var_levlist} | xargs -I {} -n 1 -P 12 bash -c "compute_timmeans {}"
  
      done # ts
 
   done # member
 
   cd ${oldpwd}

done # set

exit 0
