#/!bin/bash

set -ex

outfile=ModE-Sim_overview.csv

echo "final_set;final_member;res;final_fyear;final_lyear;orig_set;orig_member;epoch;orig_sel_fyear;orig_sel_lyear;sst_forc_type;sst_forc_dir;sst_forc_file;sic_forc_type;sic_forc_dir;sic_forc_file;volc_forc_type;volc_forc_dir;volc_forc_file_ir;volc_forc_file_sw" >> ${outfile}

for final_set in 1420-1 1420-2 1420-3 1850-1 1850-2; do

   case ${final_set} in
      1420-[123]|1850-[12]) res=T63 ;;
   esac # final_set
   
   case ${final_set} in

      1420-?) 
              epoch_list="1a 1b"
              final_fyear=1420
              final_lyear=1849
              ;;
      1850-?) 
              epoch_list="2"
              final_fyear=1850
              final_lyear=2009
              ;;

   esac # final_set

   case ${final_set} in

      1420-1) 
              final_member_first=1
              final_member_last=20
              sst_forc_type=reconst
              sst_forc_dir=PALAEO-RA_SST
              sic_forc_type=analogues
              sic_forc_dir=PALAEO-RA_SST
              volc_forc_type=PMIP4
              volc_forc_dir="ECHAM pool"
              ;;


      1420-2)
              final_member_first=21
              final_member_last=40
              sst_forc_type=reconst
              sst_forc_dir=PALAEO-RA_SST
              sic_forc_type=analogues
              sic_forc_dir=PALAEO-RA_SST
              volc_forc_type=EVA
              volc_forc_dir="inputfiles/PALAEO-RA/forcings/PALAEO-RA_volcano"
              ;;


      1420-3)
              final_member_first=41
              final_member_last=60
              sst_forc_type=reconst
              sst_forc_dir=PALAEO-RA_SST/Mod-ERA_SST_SIC
              sic_forc_type=climatology
              sic_forc_dir=PALAEO-RA_SST/Mod-ERA_SST_SIC
              volc_forc_type=PMIP4
              volc_forc_dir="ECHAM pool"
              ;;

      1850-1)
              final_member_first=1
              final_member_last=20
              sst_forc_type=HadISST
              sst_forc_dir=HadISST_2.1.0.0_for_echam6/T63
              sic_forc_type="Had ice"
              sic_forc_dir=HadISST_2.1.0.0_for_echam6/T63
              volc_forc_type=PMIP4
              volc_forc_dir="ECHAM pool"
              ;;


      1850-2)
              final_member_first=21
              final_member_last=36
              sst_forc_type="LC HadISST"
              sst_forc_dir=HadISST_2.1.0.0_for_echam6/T63/combined
              sic_forc_type="Had ice"
              sic_forc_dir=HadISST_2.1.0.0_for_echam6/T63
              volc_forc_type=PMIP4
              volc_forc_dir="ECHAM pool"
              ;;

   esac # 

   for member in $(seq -w ${final_member_first} ${final_member_last}) ensmean ensstd ensmin ensmax; do

      for epoch in ${epoch_list}; do

         case ${final_set} in
   
            1420-1)
               case ${member} in
                  0[1-5])
                      sst_forc_file=PALAEO-RA_T63_R0${member}_H69_sst_cor_trans_1420-1900.nc
                      ;;
                  0[6-9]|10)
                      sst_forc_file=PALAEO-RA_T63_R0${member}_H115_sst_cor_trans_1420-1900.nc
                      ;;
                  1[1-5])
                      sst_forc_file=PALAEO-RA_T63_R0${member}_H137_sst_cor_trans_1420-1900.nc
                      ;;
                  1[6-9]|20)
                      sst_forc_file=PALAEO-RA_T63_R0${member}_H396_sst_cor_trans_1420-1900.nc
                      ;;
   
               esac # member
   
               sic_forc_file=PALAEO-RA_T63_R0${member}_sic_cor_trans_1420-1900.nc
               volc_forc_file_ir=eva_holo2.2_forcing_echam_T63_ir_PMIPYYYY.nc
               volc_forc_file_sw=eva_holo2.2_forcing_echam_T63_sw_PMIPYYYY.nc

               case ${epoch} in
   
                  1a)
                      orig_set=HIPPO_set_1420_1
                      orig_member=HIPPO_r0${member}i1420p1f11${member}
                      ;;
   
                   1b)
                      orig_set=HIPPO_set_1780_1
                      orig_member=HIPPO_r0${member}i1780p1f14${member}
                      ;;
   
               esac
   
               ;;
   
             1420-2)
   
               case ${member} in
                 
                  2[1-5])
                      sst_forc_file=PALAEO-RA_T63_R0${member}_H400_sst_cor_trans_1420-1900.nc
                      ;;
                  2[6-9]|30)
                      sst_forc_file=PALAEO-RA_T63_R0${member}_H1059_sst_cor_trans_1420-1900.nc
                      ;;
                  3[1-5])
                      sst_forc_file=PALAEO-RA_T63_R0${member}_H1195_sst_cor_trans_1420-1900.nc
                      ;;
                  3[6-9]|40)
                      sst_forc_file=PALAEO-RA_T63_R0${member}_H1169_sst_cor_trans_1420-1900.nc
                      ;;
               esac # member
   
               sic_forc_file=PALAEO-RA_T63_R0${member}_sic_cor_trans_1420-1900.nc
               
	       case ${member} in
		  [0-9]*)
                     volc_forc_dir=$(echo ${volc_forc_dir} | sed -e "s&/forcing_out_[0-9][0-9][0-9]&&")/forcing_out_$(printf %03d $((${member} - 20 )))
	             volc_forc_file_ir=paleao_ra_T63_ir_$(printf %03d $((${member} - 20)))_YYYY.nc
                     volc_forc_file_sw=paleao_ra_T63_sw_$(printf %03d $((${member} - 20)))_YYYY.nc
                     ;;
		  *)
                     volc_forc_dir="$(echo ${volc_forc_dir} | sed -e "s&/forcing_out_[0-9][0-9][0-9]&&")"
		     volc_forc_file_ir=""
		     volc_forc_file_sw=""
	             ;;
               esac

               case ${epoch} in
   
                  1a)
                      orig_set=HIPPO_set_1420_2
                      orig_member=HIPPO_r0${member}i1420p1f13${member}
                      ;;
   
                  1b)
                      orig_set=HIPPO_set_1780_2
                      orig_member=HIPPO_r0${member}i1780p1f15${member}
                      ;;
   
               esac
   
               ;;
   
            1420-3)
               sst_forc_file=Mod-ERA_T63_R0$(printf %02d $((${member}-40)))_sst_assim_uncor_1380-1849.nc
               sic_forc_file=Mod-ERA_T63_sic_cor_trans_1380-1870.nc
               orig_member=HIPPO_r0$(printf %02d $((${member}-40)))i1420p1f18$(printf %02d $((${member}-40)))
               volc_forc_file_ir=eva_holo2.2_forcing_echam_T63_ir_PMIPYYYY.nc
               volc_forc_file_sw=eva_holo2.2_forcing_echam_T63_sw_PMIPYYYY.nc

               case ${epoch} in
   
                  1a)
                      orig_set=HIPPO_set_1420_3
                      orig_member=HIPPO_r0$(printf %02d $((${member}-40)))i1400p1f18$(printf %02d $((${member}-40)))
                      ;;
   
                   1b)
                      orig_set=HIPPO_set_1780_3
                      orig_member=HIPPO_r0$(printf %02d $((${member}-40)))i1780p1f18$(printf %02d $((${member}-40)))
                      ;;
   
               esac
   
               ;;


            1850-1)

               case ${member} in
                  01|11) 
                     sst_forc_file=HadISST.2.1.0.0_T63_real69_cor_trans_sst_1850-2010.nc
                     sic_forc_file=HadISST.2.1.0.0_T63_real69_cor_trans_sic_1850-2010.nc
                     ;;
                  02|12)
                     sst_forc_file=HadISST.2.1.0.0_T63_real115_cor_trans_sst_1850-2010.nc
                     sic_forc_file=HadISST.2.1.0.0_T63_real115_cor_trans_sic_1850-2010.nc
                     ;;
                  03|13)
                     sst_forc_file=HadISST.2.1.0.0_T63_real137_cor_trans_sst_1850-2010.nc
                     sic_forc_file=HadISST.2.1.0.0_T63_real137_cor_trans_sic_1850-2010.nc
                     ;;
                  04|14)
                     sst_forc_file=HadISST.2.1.0.0_T63_real396_cor_trans_sst_1850-2010.nc
                     sic_forc_file=HadISST.2.1.0.0_T63_real396_cor_trans_sic_1850-2010.nc
                     ;;
                  05|15)
                     sst_forc_file=HadISST.2.1.0.0_T63_real400_cor_trans_sst_1850-2010.nc
                     sic_forc_file=HadISST.2.1.0.0_T63_real400_cor_trans_sic_1850-2010.nc
                     ;;
                  06|16)
                     sst_forc_file=HadISST.2.1.0.0_T63_real1059_cor_trans_sst_1850-2010.nc
                     sic_forc_file=HadISST.2.1.0.0_T63_real1059_cor_trans_sic_1850-2010.nc
                     ;;
                  07|17)
                     sst_forc_file=HadISST.2.1.0.0_T63_real1169_cor_trans_sst_1850-2010.nc
                     sic_forc_file=HadISST.2.1.0.0_T63_real1169_cor_trans_sic_1850-2010.nc
                     ;;
                  08|18)
                     sst_forc_file=HadISST.2.1.0.0_T63_real1194_cor_trans_sst_1850-2010.nc
                     sic_forc_file=HadISST.2.1.0.0_T63_real1194_cor_trans_sic_1850-2010.nc
                     ;;
                  09|19)
                     sst_forc_file=HadISST.2.1.0.0_T63_real1346_cor_trans_sst_1850-2010.nc
                     sic_forc_file=HadISST.2.1.0.0_T63_real1346_cor_trans_sic_1850-2010.nc
                     ;;
                  10|20)
                     sst_forc_file=HadISST.2.1.0.0_T63_real1466_cor_trans_sst_1850-2010.nc
                     sic_forc_file=HadISST.2.1.0.0_T63_real1466_cor_trans_sic_1850-2010.nc
                     ;;
               esac # member

               case ${member} in
                   0?|10) 
                       orig_set=HIPPO_set_1850_1
                       orig_member=HIPPO_r0$(printf %02d ${member#0})i1850p1f12$(printf %02d $((${member#0}-1)))
                       ;;
                   1[1-9]|20) 
                       orig_set=HIPPO_set_1850_2
                       orig_member=HIPPO_r0$(printf %02d ${member})i1850p1f12$(printf %02d $((${member}-11)))
                       ;;

               esac # member

               volc_forc_file_ir=eva_holo2.2_forcing_echam_T63_ir_PMIPYYYY.nc
               volc_forc_file_sw=eva_holo2.2_forcing_echam_T63_sw_PMIPYYYY.nc

               ;;

            1850-2)

               reals=(69 115 137 396 400 1059 1169 1194 1346 1466)

               case ${member} in

                   2[1-8])
                       orig_set=HIPPO_set_1850_3
                       r1=0
                       r2=3
                       r3=(1 2 4 5 6 7 8 9)
                       orig_init=(2 3 5 6 7 8 9 10)
                       orig_member=$(echo HIPPO_r0$(printf %02d ${orig_init[$((${member} - 21))]})i1850p1f12${r1}${r2}${r3[$((${member} - 21))]})
                       sst_forc_file=$(echo HadISST.2.1.0.0_T63_LC_real${reals[${r1}]}_real${reals[${r2}]}_real${reals[${r3[$((${member}-21))]}]}_cor_trans_sst_1850-2010.nc)
                       sic_forc_file=$(echo HadISST.2.1.0.0_T63_real${reals[${r3[$((${member}-21))]}]}_cor_trans_sic_1850-2010.nc)
                       ;;

                   29|3[0-6])
                       orig_set=HIPPO_set_1850_4
                       r1=4
                       r2=2
                       r3=(0 1 3 5 6 7 8 9)
                       orig_init=(1 2 4 6 7 8 9 10)
                       orig_member=$(echo HIPPO_r0$(printf %02d ${orig_init[$((${member} - 29))]})i1850p1f12${r1}${r2}${r3[$((${member} - 29))]})
                       sst_forc_file=$(echo HadISST.2.1.0.0_T63_LC_real${reals[${r1}]}_real${reals[${r2}]}_real${reals[${r3[$((${member}-29))]}]}_cor_trans_sst_1850-2010.nc)
                       sic_forc_file=$(echo HadISST.2.1.0.0_T63_real${reals[${r3[$((${member}-29))]}]}_cor_trans_sic_1850-2010.nc)
                       ;;

               esac # member

               volc_forc_file_ir=eva_holo2.2_forcing_echam_T63_ir_PMIPYYYY.nc
               volc_forc_file_sw=eva_holo2.2_forcing_echam_T63_sw_PMIPYYYY.nc

               ;;

         esac # final_set

         case ${epoch} in 

            1a)  orig_sel_fyear=1420; orig_sel_lyear=1779 ;;
            1b)  orig_sel_fyear=1780; orig_sel_lyear=1849 ;;
            2)   orig_sel_fyear=1850; orig_sel_lyear=2009 ;;

         esac # epoch

         case ${member} in

            ens*)
                  orig_member=ensstat
                  member_out=${member}
                  sst_forc_file=""
                  sic_forc_file=""
                  volc_forc_file_ir=""
		  volc_forc_file_sw=""
                  ;;

            *)
                  orig_member=${orig_member}
                  member_out="m0${member}"
                  ;;

         esac # member


         echo "${final_set};${member_out};${res};${final_fyear};${final_lyear};${orig_set};${orig_member};${epoch};${orig_sel_fyear};${orig_sel_lyear};${sst_forc_type};${sst_forc_dir};${sst_forc_file};${sic_forc_type};${sic_forc_dir};${sic_forc_file};${volc_forc_type};${volc_forc_dir};${volc_forc_file_ir};${volc_forc_file_sw}" >> ${outfile}

      done # epoch

   done # member

   echo ";" >> ${outfile}

done # final_set


exit 0
