#!/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=/tank/exports/ERC_PALAEO/ModE-Sim/docu/ModE-Sim_overview.csv

indir_base=/tank/exports/ERC_PALAEO/ModE-Sim/forcings/orig_SST_forcings
outdir_base=/tank/exports/ERC_PALAEO/ModE-Sim

col_sst_forc_dir=$(head -1 ${overviewfile} | awk -F ";" -v b="sst_forc_dir" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
col_sst_forc_file=$(head -1 ${overviewfile} | awk -F ";" -v b="sst_forc_file" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
col_sic_forc_dir=$(head -1 ${overviewfile} | awk -F ";" -v b="sic_forc_dir" '{for (i=1;i<=NF;i++) { if ($i == b) { print i } }}')
col_sic_forc_file=$(head -1 ${overviewfile} | awk -F ";" -v b="sic_forc_file" '{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 H-1400-1 H-1850-1 H-1870-1; do
for final_set in H-1870-1; do

   case ${final_set} in

      1420-1|1420-2|H-1400-1|H-1850-1|H-1870-1)

         varlist="sst sic"

         ;;

      1420-3|1850-2)

         varlist="sst"

         ;;


   esac

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

      for var in ${varlist} ; do

         case ${var} in

            sst) 
                 forc_dir=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "{print \$$(echo ${col_sst_forc_dir})}" | sort -u)
		           forc_file=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "{print \$$(echo ${col_sst_forc_file})}" | sort -u)
                 long_name='Sea Surface Temperature'
                 standard_name="sea_surface_temperature"
                 unit="K"

                 case ${final_set} in 
                    
                    1420-1|1420-2|1420-3) 
                       
                       bc_type="SST reconstructions based on Samakinwa et al., 20210 (https://doi.org/10.1038/s41597-021-01043-1)"

                       ;;

                    1850-2) 

		                 m1=$(echo ${forc_file} | sed -E 's/.*_real([0-9]*)_real([0-9]*)_real([0-9]*)_.*/\1/')
                       m2=$(echo ${forc_file} | sed -E 's/.*_real([0-9]*)_real([0-9]*)_real([0-9]*)_.*/\2/')
                       m3=$(echo ${forc_file} | sed -E 's/.*_real([0-9]*)_real([0-9]*)_real([0-9]*)_.*/\3/')
		                 bc_type="Linear combination of HadISST members (member ${m1} * member ${m2} / member ${m3})"

                       ;;

	         esac

		 ;;

            sic)
	         forc_dir=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "{print \$$(echo ${col_sic_forc_dir})}" | sort -u)
	         forc_file=$(grep "${final_set};${final_member}" ${overviewfile} | awk -F ';' "{print \$$(echo ${col_sic_forc_file})}" | sort -u)
            long_name='Sea Ice Concentration'
	         standard_name="sea_ice_area_fraction"
            unit="%"

             case ${final_set} in

                1420-1|1420-2)

                   bc_type="Sea ice analogues based on Samakinwa et al., 2021 (https://doi.org/10.1038/s41597-021-01043-1)"

                   ;;

             esac

		   ;;

         esac # var

         fyear=$(echo ${forc_file} | sed -E 's/.*_([0-9]{4})-([0-9]{4}).nc/\1/')
         lyear=$(echo ${forc_file} | sed -E 's/.*_([0-9]{4})-([0-9]{4}).nc/\2/')

         outdir=${outdir_base}/forcings/set_${final_set}/abs/${final_member}/
         outfile=${outdir}/ModE-Sim_set_${final_set}_${final_member}_${var}_abs_${fyear}-${lyear}_mon.nc

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

         cdo -settunits,"seconds" -setreftime,1400-01-01,00:00:00 ${indir_base}/${forc_dir}/${forc_file} ${outfile}      
      
         ncatted -h \
           -a long_name,${var},o,c,"${long_name}" \
           -a standard_name,${var},o,c,"${standard_name}" \
           -a units,${var},o,c,"${unit}" \
           -a history,global,d,, \
           -a title,global,o,c,'ModE-Sim - A medium size AGCM ensemble to study climate variability during the past 600 years' \
           -a creator,global,o,c,'The PALAEO-RA project group' \
           -a institution,global,o,c,Oeschger\ Centre\ for\ Climate\ Change\ Research \
           -a contact,global,o,c,'Ralf Hand (https://orcid.org/0000-0003-4169-5016)' \
           -a model,global,o,c,ECHAM\ 6.3.5p2 \
           -a experiment,global,o,c,"ModE-Sim" \
           -a set,global,o,c,"${final_set}" \
           -a member,global,o,c,"${var} boundary condition for ${final_member}" \
           -a bc_type,global,o,c,"${bc_type}" \
           -a orig_name,global,o,c,"Original filename during runtime: ${forc_file}" \
	        -a creation_date,global,o,c,"$(date +"%Y-%m-%d %H:%M:%S %Z")" \
           ${outfile}

      done # var

   done # final_member

done # final_set

exit 0
