#!/bin/bash
# unpacks the files that were retrieved back from tape and converts them to the new structure.
# Ralf, 30.10.23

final_setlist="1850-1 1850_2"

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 ${final_setlist}; 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})}")

         tar_dir=/tank/exports/ERC_PALAEO/${orig_set}/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?

         echo ${i} ${orig_set} ${orig_member} ${final_set} ${final_member}

      done # i

   done # final_member

done # setlist

exit 0
