#!/bin/bash
# 
# Extracts 6-hourly u-wind, v-wind and potential temperature from raw data
# 
# ralf.hand@unibe.ch, 2024-06-24

set -ex

case $(hostname -s) in
   climstor) export ModE_dir=/tank33/ERC_PALAEO_ARCHIVE
             export ModE_rawdir=/tank33/ERC_PALAEO_ARCHIVE/ModE-Sim/outdata
             export workdir=/tank33/ERC_PALAEO_ARCHIVE/outdata ;;
   climcal4) export ModE_dir=/mnt/climstor/ERC_PALAEO
             export ModE_rawdir=/mnt/climstor/ERC_PALAEO
             # export workdir=/scratch3/rhand/test-EP ;;
             export workdir=/mnt/climstor/ERC_PALAEO_ARCHIVE/outdata ;;
esac # hostname

export overviewfile=${ModE_dir}/ModE-Sim/docu/ModE-Sim_overview.csv
export outdir_base=${workdir}

# export outdir_base=/tank/exports/ERC_PALAEO/ModE-Sim/outdata/

col_orig_set=$(head -1 ${overviewfile} | awk -F ";" -v b="orig_set" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
col_orig_member=$(head -1 ${overviewfile} | awk -F ";" -v b="orig_member" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
col_orig_sel_fyear=$(head -1 ${overviewfile} | awk -F ";" -v b="orig_sel_fyear" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
col_orig_sel_lyear=$(head -1 ${overviewfile} | awk -F ";" -v b="orig_sel_lyear" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
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 } }}')


###################
# define function #
###################

function extract_ep {

   # set -ex

   module load cdo/1.9.10
   cdo=cdo
   
   block=$1
   # cd ${workdir}/${orig_member}/rawdata

   case ${final_set} in

      1850-1)  

               cd ${ModE_rawdir}/set_${final_set}/rawdata/${orig_member}
               infile_base=${orig_member}_${block}
               ;;

		1850-2)
               cd ${ModE_rawdir}/set_${final_set}/rawdata
               infile_base=${orig_member}_${block}
               ;;

      H-1850-1|H-1870-1) 
               
               cd ${ModE_rawdir}/set_${final_set}/abs/${final_member}/rawdata
               pwd
               infile_base=ModE-Sim_set_${final_set}_${final_member}_${block}
               ;;
   
   esac # final_set

   for year in $(seq 0 9); do 

      $cdo after ${infile_base}${year}??.01_echam ${outdir}/ModE-Sim_set_${final_set}_${final_member}_${block}${year}_6h.alllevs.grb << __EOF__
      &select
        code = 130, 131, 132, 239 
        level = -1 
        type = 70
        format = 1
        mean = 0
      /
__EOF__
  
   done # year

   cd ${workdir}

}

export -f extract_ep


##############
# do the job #
##############

# for final_set in 1850-1 1850-2 H-1400-1 H-1850-1 H-1870-1; do
# for final_set in H-1870-1; do
for final_set in 1850-2; do

   export final_set

   for final_member in $(cat ${overviewfile} | grep "^${final_set};m[0-9][0-9][0-9];" | awk -F ";" '{print $2}' | sort -u) ; do
   # for final_member in m004 m005 m006 m007 m008 m009 m010 m011 m012 m013 m014 m015 m016 m017 m018 m019 m020 m021 m022 m023; do

      export final_member

      n_orig_sets=$(grep "^${final_set};${final_member}" ${overviewfile} | wc -l)

      for i in $(seq 1 ${n_orig_sets}); do

         export final_fyear=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==${i} {print \$$(echo ${col_final_fyear})}")
         export final_lyear=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==${i} {print \$$(echo ${col_final_lyear})}")
   
         export orig_sel_fyear=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==${i} {print \$$(echo ${col_orig_sel_fyear})}")
         # export orig_sel_fyear=1630
         export orig_sel_lyear=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==${i} {print \$$(echo ${col_orig_sel_lyear})}")
         export orig_set=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==${i} {print \$$(echo ${col_orig_set})}")
         export orig_member=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==${i} {print \$$(echo ${col_orig_member})}")      
   
         echo "final fyear: ${final_fyear}"
         echo "final lyear: ${final_lyear}"
   
         echo "orig_sel_fyear: ${orig_sel_fyear}"
         echo "orig_sel_lyear: ${orig_sel_lyear}"
         echo "orig_set: ${orig_set}"
         echo "orig_member: ${orig_member}"

         export archive_dir=${ModE_rawdir}/${orig_set}/${orig_set}
         # export archive_dir=${ModE_rawdir}/ModE-Sim/outdata/set_${final_set}/rawdata

         export outdir=${outdir_base}/set_${final_set}/abs/${final_member}/by_year

         if [ ! -e ${outdir} ]; then mkdir -p ${outdir}; fi

         fblock=$(echo ${orig_sel_fyear} | cut -c 1-3)
         lblock=$(echo ${orig_sel_lyear} | cut -c 1-3)

         case ${final_set} in
            H-1870-1) maxproc=5;;
            *) maxproc=8;;
         esac # final_set

         seq ${fblock} ${lblock} | xargs -n1 -P${maxproc} -I% bash -c "extract_ep %"

      done # i

   done # final_member   

done # final_set

exit 0
