#!/bin/bash
#
# copies the ModE-Sim ocean boundary conditions from the old structure to the new structure (ModE-Sim_m???)
#
# ralf.hand@giub.unibe.ch, 01.03.2023

set -ex

overviewfile=ModE-Sim_overview.csv

indir_base=/tank/exports/ERC_PALAEO
outdir_base=/tank/exports/ERC_PALAEO/ModE-Sim/test

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 } }}')
col_volc_forc_dir=$(head -1 ${overviewfile} | awk -F ";" -v b="volc_forc_dir" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
col_volc_forc_file_struct_ir=$(head -1 ${overviewfile} | awk -F ";" -v b="volc_forc_file_ir" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
col_volc_forc_file_struct_sw=$(head -1 ${overviewfile} | awk -F ";" -v b="volc_forc_file_sw" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')

for final_set in 1420-2; do

   for final_member in $(cat ModE-Sim_overview.csv | grep "^${final_set};m" | awk -F ";" '{print $2}' | sort -u) ; do

      final_fyear=$(grep "^${final_set};${final_member}" ${overviewfile} | awk -F ';' "{print \$$(echo ${col_final_fyear})}" | sort -u )
      final_lyear=$(grep "^${final_set};${final_member}" ${overviewfile} | awk -F ';' "{print \$$(echo ${col_final_lyear})}" | sort -u )

      forc_dir=$(grep "^${final_set};${final_member}" ${overviewfile} | awk -F ';' '{print $'${col_volc_forc_dir}'}' | sort -u)
      echo ${final_member}

      for wl in ir sw; do

         case ${wl} in
            ir) forc_file_struct=$(grep "^${final_set};${final_member}" ${overviewfile} | awk -F ';' '{print $'${col_volc_forc_file_struct_ir}'}' | sed -e "s/YYYY/????/" | sort -u) ;;
            sw) forc_file_struct=$(grep "^${final_set};${final_member}" ${overviewfile} | awk -F ';' '{print $'${col_volc_forc_file_struct_sw}'}' | sed -e "s/YYYY/????/" | sort -u) ;;
         esac # wl

         volc_file_list=$(ls ${indir_base}/${forc_dir}/${forc_file_struct})

         outdir=${outdir_base}/set_${final_set}/abs/${final_member}/by_var/mon/forcings
         outfile=${outdir}/ModE-Sim_set_${final_set}_${final_member}_volc_forc_${wl}_abs_${final_fyear}-${final_lyear}_mon.nc

         if [ ! -d ${outdir} ]; then mkdir -p ${outdir};  fi # outdir exists?

         # cdo -selyear,${final_fyear}/${final_lyear} -settunits,"seconds" -setreftime,1400-01-01,00:00:00 -mergetime ${volc_file_list} ${outfile}      
      
         ncatted -h \
           -a history,global,d,, \
           -a title,global,c,c,'ModE-Sim - A medium size AGCM ensemble to study climate variability during the past 600 years' \
           -a creator,global,c,c,'The PALAEO-RA project group' \
           -a institution,global,m,c,Oeschger\ Centre\ for\ Climate\ Change\ Research \
           -a contact,global,c,c,'Eric Samakinwa (https://orcid.org/0000-0003-1907-060X)' \
           -a model,global,m,c,"EVA_v1.2" \
           -a experiment,global,c,c,"ModE-Sim" \
           -a set,global,c,c,"${final_set}" \
           -a member,global,c,c,"${var} volcano forcing for ${final_member}" \
           -a orig_name,global,c,c,"Original filename during runtime: ${forc_file_struct}" \
   	   -a creation_date,global,c,c,"$(date +"%Y-%m-%d %H:%M:%S %Z")" \
           ${outfile}

      done # wl

   done # final_member

done # final_set

exit 0
