#!/bin/bash
#
# computes annual and seasonal means for the HIPPO_r0xxi1850p1f12xx ensemble
# 
# ralf.hand@giub.unibe.ch 2020-03-25

set -ex 

dir=/mnt/climstor/ERC_PALAEO
# dir=/tank/exports/ERC_PALAEO

# setlist="HIPPO_set_1780_1 HIPPO_set_1780_2"
setlist="HIPPO_set_1850_1 HIPPO_set_1850_2 HIPPO_set_1850_3 HIPPO_set_1850_4"

# varlist="hadc_nh_loc hadc_sh_loc hadc_nh_str hadc_sh_str"
varlist="slp temp2 albedo albedo_vis"
# varlist="albedo albedo_vis"

oldpwd=$(pwd)

for set in ${setlist}; do
   for var in ${varlist}; do

      set +e; runlist=$(ls -d ${dir}/${set}/HIPPO_r*i*p*f* 2>/dev/null | sed -e "s&.*/&&"); set -e
   
      for run in ${runlist}; do

         mondir=${dir}/${run}/timeseries/mon

         if [ ! -d ${dir}/${run}/timeseries/coldseas ]; then mkdir -p ${dir}/${run}/timeseries/coldseas ; fi
         # cdo setmon,01 -yearmean -shifttime,+3mon -selseas,ondjfmam ${mondir}/${run}_${var}_mon.nc ${dir}/${run}/timeseries/coldseas/${run}_${var}_coldseas.nc         

         if [ ! -d ${dir}/${run}/timeseries/ondj ]; then mkdir -p ${dir}/${run}/timeseries/ondj ; fi
         cdo setmon,01 -yearmean -shifttime,+3mon -selseas,ondj ${mondir}/${run}_${var}_mon.nc ${dir}/${run}/timeseries/ondj/${run}_${var}_ondj.nc

         if [ ! -d ${dir}/${run}/timeseries/fmam ]; then mkdir -p ${dir}/${run}/timeseries/fmam ; fi
         cdo yearmean -selseas,fmam ${mondir}/${run}_${var}_mon.nc ${dir}/${run}/timeseries/fmam/${run}_${var}_fmam.nc

      done # var
 
      cd ${oldpwd}
   done # var
done # set

exit 0
