#!/bin/bash

set -ex

fyear=1420
lyear=1900

# for file in /scratch3/esamakin/SSTs/Palaeo-SST_1000-1900_R0{01..02}_H*_mod.nc; do
# for file in /scratch3/esamakin/bc_vf/SIC/SIC_1000-1900_R01?.nc /scratch3/esamakin/bc_vf/SIC/SIC_1000-1900_R020.nc ; do
for file in /scratch3/esamakin/bc_vf/SIC/SIC_1000-1900_R0{36..50}.nc; do
# for file in /scratch3/esamakin/bc_vf/SIC/SIC_1000-1900_R*.nc /scratch3/esamakin/SSTs/Palaeo-SST_1000-1900_R0{03..50}_H*.nc; do

  var=$(cdo showvar ${file} | sed -e "s/ //g")

  if [ "${var}" == "sic" ]; then mulc="-mulc,100"; else mulc=""; fi   

  case ${var} in
     sst) real=$(basename ${file} | sed -e "s/_mod//" -e "s/.*_\(R[0-9]*_H[0-9]*\).nc/\1/g") ;;
     sic) real=R$(printf %03d $(basename ${file} | sed -e "s/.*_\(R[0-9]*\).nc/\1/g" | sed -e "s/R0*//")) ;;
  esac

  outfile=PALAEO-RA_T63_${real}_${var}_uncor_${fyear}-${lyear}.nc

  cdo -f nc2 setmisstonn -remapdis,PALAEO-RA_T63_real000_sic_cor_trans_1420-1849.nc ${mulc} -selvar,${var} -selyear,${fyear}/${lyear} -setcalendar,standard ${file} ${outfile} 

done # file

exit 0
