#!/bin/bash

set -ex

export cdo=/usr/bin/cdo
export cdo=$(which cdo)

case $(hostname -s) in
   climcal?) datadir=/mnt/climstor/ERC_PALAEO/ModE-Sim ;;
   climstor) datadir=/tank/exports/ERC_PALAEO/ModE-Sim ;;
esac # hostname


tmpdir_base="/scratch3/rhand/spielwiese"

setlist="1420-1 1420-2 1420-3 1850-1 1850-2"
varlist="u v q geopoth omega"

setlist="1420-1 1420-2 1420-3 1850-1 1850-2 H-1400-1 H-1850-1 H-1870-1"
# varlist="srads srad0 sradsd srad0d aclcov"
varlist="geopoth"
levellist="50000"

setlist="1420-1 1420-2 1420-3 1850-1 1850-2 H-1400-1 H-1850-1 H-1870-1"
varlist="u v q geopoth omega"
levellist="10000 20000 30000 50000 70000 85000 100000"

# setlist="H-1850-1"
# varlist="temp2 totprec slp"

ts=mon

olddir=$(pwd)

for set in ${setlist}; do


   case ${set} in
      H-1400-1) 
              fyear=1400; lyear=1849
              ;;
      1420-?)
              fyear=1420; lyear=1849
              ;;
      H-1850-1|1850-?)
              fyear=1850; lyear=2009
              ;;
      H-1870-1)
              fyear=1870; lyear=1919
              ;;
   esac # set

   cd ${datadir}/outdata/set_${set}/abs

   for exp in $(ls -d m???); do
#   for exp in m001; do  
 
      cd ${datadir}/outdata/set_${set}/abs/${exp}

      for var in ${varlist}; do

         export var exp
         export varname=${var}

         case ${ts} in

            mon)
               case ${var} in
                  albedo|albedo_vis|slp|temp2|totprec|tsurf|srad*) file=mon.toasurf.nc; levellist="all" ;;
                  geopoth|u|v|q|omega) file=mon.stdlevs.nc; levellist="100000 85000 70000 50000 30000 20000 10000" ;;
               esac # var
            ;;

            6h)
               case ${var} in
                  geopoth) file=6h.special.grb ; levellist="50000" ;;
               esac # var
            ;;



         esac

         for level in ${levellist}; do
#         for level in 100000 70000; do

            export level
            export tmp=$(mktemp -d ${tmpdir_base}/tmp.create_timeseries.XXX)

            outdir=${datadir}/outdata/set_${set}/abs/${exp}/by_var/${ts}

            case ${level} in
               "all") 
                      export sellevel=""
                      outfile=${outdir}/ModE-Sim_set_${set}_${exp}_${var}_abs_${fyear}-${lyear}_${ts}.nc
                      ;;
               *)
                      export sellevel="-sellevel,${level}"
                      outfile=${outdir}/ModE-Sim_set_${set}_${exp}_${var}_${level}_abs_${fyear}-${lyear}_${ts}.nc
                      ;;
            esac # level

            pids=()
 
            eval ls by_year/{dummy_,}ModE-Sim_set_${set}_${exp}_{${fyear}..${lyear}}_${file} 2> /dev/null |
            xargs -n1 -P165 -I% bash -c 'year=$(${cdo} -s showyear % | sed -e "s/ //g") && ${cdo} -t echam6 -s -f nc ${sellevel} -selvar,${varname} % ${tmp}/${exp}.${var}.${level}.${year}.nc ; pids+=($!)'

            for pid in "${pids[@]}"; do
               wait "$pid"  # quit if any of the subprocesses failed
            done

            rm -f ${outfile} 
            if [ ! -d by_var/${ts} ]; then mkdir -p by_var/${ts}; fi
            eval ${cdo} --reduce_dim --no_history -O -t echam6 setvar,${var} [ -mergetime ${tmp}/${exp}.${var}.${level}.{${fyear}..${lyear}}.nc ] ${outfile} && rm -rf ${tmp}
    
         done # level

      done # var

   done # exp

done # set in setlist

exit 0
