#!/bin/bash
# 
# Goes throuch all (monthly?) timeseries in the new folder structure, checks which timeseries are in and then removes the existing ones from the old structure
# 
# ralf.hand@giub.unibe.ch, 15.03.23

# set -ex

basedir_new=/tank/exports/ERC_PALAEO/ModE-Sim/test
basedir_old=/tank/exports/ERC_PALAEO/

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

setlist="1420-1 1420-2 1420-3 1850-1 1850-2"

ts=mon

col_orig_member=$(head -1 ${overviewfile} | awk -F ";" -v b="orig_member" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
col_final_member=$(head -1 ${overviewfile} | awk -F ";" -v b="final_member" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')

for final_set in ${setlist}; do

   memberlist=$(grep "^${final_set};m" ${overviewfile} | awk -F ';' "{print \$$(echo ${col_final_member})}" | sort -u)

   for final_member in ${memberlist}; do

      for file in ${basedir_new}/set_${final_set}/abs/${final_member}/by_var/${ts}/ModE-Sim_set_${final_set}_${final_member}_*_abs_????-????_${ts}.nc; do

         orig_member_list=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "{print \$$(echo ${col_orig_member})}")
         var=$(basename ${file} | sed -r -e "s/ModE-Sim_set_${final_set}_${final_member}_(.*)_abs_([0-9]{4})-([0-9]{4})_mon.nc/\1/")

         for orig_member in ${orig_member_list}; do

            file=${basedir_old}/${orig_member}/timeseries/${ts}/${orig_member}_${var}_${ts}.nc 

            if [ -e ${file} ] ; then 
               echo "removing ${file}"
               rm ${file}
            fi # file exists

         done # orig_member

      done # file

   done # member

done # final_set

exit 0
