#!/bin/bash
#
# copies the ModE-Sim files from the old structure ("HIPPO_r???i????p?f????) to the new structure (ModE-Sim_m???)
#
# ralf.hand@giub.unibe.ch, 10.02.2023

set -ex

overviewfile=/tank/exports/ERC_PALAEO/ModE-Sim/docu/ModE-Sim_overview.csv

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

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 } }}')


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

   for final_member in $(cat ${overviewfile} | grep "^${final_set};m[0-9][0-9][0-9];" | awk -F ";" '{print $2}' | sort -u) ; do

      n_orig_sets=$(grep "${final_set};${final_member}" ${overviewfile} | wc -l)
   
      for i in $(seq 1 ${n_orig_sets}); do
   
         final_fyear=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==${i} {print \$$(echo ${col_final_fyear})}")
         final_lyear=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==${i} {print \$$(echo ${col_final_lyear})}")

         orig_sel_fyear=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==${i} {print \$$(echo ${col_orig_sel_fyear})}")
         orig_sel_lyear=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==${i} {print \$$(echo ${col_orig_sel_lyear})}")
         orig_set=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==${i} {print \$$(echo ${col_orig_set})}")
         orig_member=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "NR==${i} {print \$$(echo ${col_orig_member})}")   

         orig_dir=/tank/exports/ERC_PALAEO/${orig_set}/${orig_member}/rawdata

         outdir=${outdir_base}/set_${final_set}/abs/${final_member}/rawdata
         orig_file="${orig_dir}/${orig_member}_${year}.nc"

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

         for orig_file in $(ls ${orig_dir}/{dummy_,rerun_,}${orig_member}_??????.01_{accw,echam}) ; do

         outfile=$(basename ${orig_file} | sed -e "s/${orig_member}/ModE-Sim_set_${final_set}_${final_member}/g")
         echo "moving file ${orig_file} to ${outdir}/${outfile}"
         mv -n ${orig_file} ${outdir}/${outfile}
            
         done # orig_file

      done # i 

   done # final_member

done # final_set

exit 0
