#!/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

ts="mon" #  son mam djf jja"
varlist="temp2 tsurf totprec slp geopoth geopoth_50000 omega omega_50000 u u10 u_85000 v v10 v_85000 hadc_nh_str hadc_sh_str hadc_nh_loc hadc_sh_loc"
varlist="hadc_nh_str hadc_sh_str hadc_nh_loc hadc_sh_loc"
varlist="temp2 totprec slp"

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 H-1400-1; do
for final_set in 1420-3-ext; do

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

      for var in ${varlist}; do

         case ${var} in

            geopoth_50000) 
                         orig_var="geopoth"
                         sellev_cmd="-sellevel,50000"
                         ;;

            u_85000)
                         orig_var="u"
                         sellev_cmd="-sellevel,85000"
                         ;;

            v_85000)
                         orig_var="v"
                         sellev_cmd="-sellevel,85000"
                         ;;

            omega_50000)
                         orig_var="omega"
                         sellev_cmd="-sellevel,50000"
                         ;;

             *)
                         orig_var="${var}"
                         sellev_cmd=""
                         ;;

         esac # var

         n_orig_files=$(grep "${final_set};${final_member}" ${overviewfile} | wc -l)
   
         concat_cmd="-mergetime"

         for i in $(seq 1 ${n_orig_files}); 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}

            case ${final_member} in
               m0??) 
                     outdir=${outdir_base}/set_${final_set}/abs/${final_member}/by_var/${ts}
                     orig_file="${orig_dir}/timeseries/${ts}/${orig_member}_${orig_var}_${ts}.nc"
                     ;;

               ensmean|ensstd|ensmin|ensmax|enspctl125|enspctl875) 
                     outdir=${outdir_base}/set_${final_set}/abs/ensstat/by_var/${ts}
                     orig_file="${orig_dir}/timeseries/${ts}/${orig_set}_${orig_var}_${final_member}_${ts}.nc"
                     ;;

            esac # final_member

            concat_cmd="${concat_cmd} -selyear,${orig_sel_fyear}/${orig_sel_lyear} ${orig_file}"
   
         done # i

         outfile=${outdir}/ModE-Sim_set_${final_set}_${final_member}_${var}_abs_${final_fyear}-${final_lyear}_${ts}.nc

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

         case ${var} in
      
            temp2)
                   long_name='2m air temperature'
                   standard_name="air_temperature"
                   ;;
      
            slp)   
                   long_name='mean sea level pressure'
                   standard_name="air_pressure_at_mean_sea_level"
                   ;;
      
            totprec)
                   long_name='total precipitation'
                   standard_name="precipitation_flux"
                   ;;

            geopoth)
                   long_name='geopotential height'
                   standard_name="geopotential_height"
		   ;;

	    geopoth_50000)
                   long_name='500 hPa geopotential height'
                   standard_name="geopotential_height"
                   ;;
      
            u)
                   long_name='u-wind'
                   standard_name="eastward_wind"
                   ;;


            u10)   long_name='10m u-velocity'
		   standard_name='eastward_wind'
                   ;;

            u_85000)
                   long_name='850 hPa u-wind'
                   standard_name="eastward_wind"
                   ;;
      
            v)
                   long_name='v-wind'
                   standard_name="northward_wind"
                   ;;

            v10)
                   long_name='10m v-velocity'
                   standard_name='northward_wind'
                   ;;

            v_85000)
                   long_name='850 hPa v-wind'
                   standard_name="northward_wind"
                   ;;

            omega)
                   long_name='vertical velocity'
                   standard_name="lagrangian_tendency_of_air_pressure"
                   ;;

            omega_50000)
                   long_name='500 hPa vertical velocity'
                   standard_name="lagrangian_tendency_of_air_pressure"
                   ;;

            hadc_nh_str)
	           long_name='NH hadley cell strength (max(merid. massstrf 20S:40N))'
                   ;;

            hadc_nh_loc)
                   long_name='NH hadley cell lat (lat(max(merid. massstrf 20S:40N)))'
		   ;;

	    hadc_sh_str)
                   long_name='SH hadley cell strength (min(merid. massstrf 40S:20N))'
                   ;;

            hadc_sh_loc)
                   long_name='SH hadley cell lat (lat(min(merid. massstrf 40S:20N)))'
                   ;;

         esac   # var

         case ${final_member} in
             m0??) member_cmd="${final_member/m/}" ;;
             ensmean) member_cmd="ensemble mean of set ${final_set}\"" ;;
             ensstd) member_cmd="ensemble standard deviation of set ${final_set}" ;;
             ensmin) member_cmd="ensemble minimum of set ${final_set}" ;;
             ensmax) member_cmd="ensemble maximum of set ${final_set}" ;;
             enspctl125)  member_cmd="12.5th ensemble percentile of set ${final_set}" ;;
             enspctl875)  member_cmd="87.5th ensemble percentile of set ${final_set}" ;;
         esac # member

         cdo -setreftime,1400-01-01,00:00:00 ${sellev_cmd} ${concat_cmd} ${outfile}      
      
         ncatted -h \
           -a long_name,${orig_var},m,c,"${long_name}" \
           -a standard_name,${orig_var},c,c,"${standard_name}" \
           -a institution,${orig_var},d,, \
           -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,'Ralf Hand (https://orcid.org/0000-0003-4169-5016)' \
           -a model,global,c,c,ECHAM\ 6.3.5p2 \
           -a experiment,global,c,c,"ModE-Sim" \
           -a set,global,c,c,"${final_set}" \
           -a member,global,c,c,"${member_cmd}" \
	   -a creation_date,global,c,c,"$(date +"%Y-%m-%d %H:%M:%S %Z")" \
           ${outfile}

      done # var

   done # final_member

done # final_set

exit 0
