#!/bin/bash

set -ex

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

tmpdir_base="/scratch3/ralf/spielwiese"

setlist="HIPPO_set_1850_1 HIPPO_set_1850_2 HIPPO_set_1850_3 HIPPO_set_1850_4 HIPPO_set_1420_1 HIPPO_set_1420_2 HIPPO_set_1420_3 HIPPO_set_1780_1 HIPPO_set_1780_2 HIPPO_set_1780_3"
# setlist="HIPPO_set_1420_3"
varlist="temp2 slp totprec tsurf geopoth u v omega"
# varlist="mastrfu albedo albedo_vis"
# ts="djf"
ts=mon
# ts=6h

olddir=$(pwd)

for set in ${setlist}; do

   cd ${set}

   for exp in $(ls -d HIPPO_r0*i*p*f*); do
#    for exp in $(ls -d HIPPO_r0{21..30}i????p1f????); do
 
      cd ${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) file=mon.toasurf.nc ;;
                  geopoth|u|v|omega) file=mon.stdlevs.nc ;;
                  mastrfu) file=mon.meridional.nc ;;
               esac # var
            ;;

            6h)
               case ${var} in
                  geopoth500) file=6h.special.grb; varname=geopoth;;
               esac
            ;;

         esac

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

         N=30
         pids=()

#          (
#          for file in $(ls means/{dummy_,}${exp}_????_${file}); do
# 
#             ((i=i%N)); ((i++==0)) && wait
# 
#             year=$(${cdo} showyear ${file} | sed -e "s/ //g") && ${cdo} -f nc -t echam -selvar,${varname} ${file} ${tmp}/${exp}.${var}.${year}.nc &
#             pids+=($!)
# 
#          done # file
#          )


         pwd
         ls means/{dummy_,}${exp}_????_${file} |
         xargs -n1 -P165 -I% bash -c 'year=$(${cdo} -s showyear % | sed -e "s/ //g") && readlink -f  % && set -ex ; ${cdo} showvar % &&  ${cdo} -f nc -selvar,${varname} % ${tmp}/${exp}.${var}.${year}.nc ; pids+=($!)'

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

         if [ ! -d timeseries/${ts} ]; then mkdir -p timeseries/${ts}; fi
         ${cdo} -O mergetime ${tmp}/${exp}.${var}.????.nc timeseries/${ts}/${exp}_${var}_${ts}.nc && rm -rf ${tmp}
 
      done # var


      cd ..

   done # exp

   cd ..

done # set in setlist

exit 0
