# load modules
module load intel/20.0.4-lednsve
module load intel-parallel-studio/cluster.2020.2-xz35pbn
module load anaconda3/2020.07
module load gcc/9.2.0-pkmzczt
module load netcdf-fortran

# define filenames
#
# file that E3SM will use to start mpas-o, either IC or restart file
set runInitFile = mpaso.RRSwISC6to18E3r4.rstFromG-chrysalis.20240123.nc

# suite of files that get created
set grid = RRSwISC6to18E3r4
set gridDate = 20240105
set todayDate = 20240227
set fullICfile = mpaso.$grid.$gridDate.nc
set IC_TSfile = TS.$fullICfile
set restoringTSfile = 'restoring'$IC_TSfile
set rateRossYear = 'restoringRatesRoss1250km_k21-80_1year_'$grid.$gridDate.nc
set rateRossMonth = 'restoringRatesRoss1250km_k21-80_1month_'$grid.$gridDate.nc
set rateWeddellYear = 'restoringRatesWeddell1250km_k21-80_1year_'$grid.$gridDate.nc
set rateWeddellMonth = 'restoringRatesWeddell1250km_k21-80_1month_'$grid.$gridDate.nc
set rateBoth = 'restoringTS+restoringRatesRossWeddell1250km_k21-80_1month.'$grid.$gridDate.nc
set rateTSboth = 'restoringTS+restoringRatesRoss+Weddell1250km_k21-80_1month.'$grid.$gridDate.nc
set finalInitFile = $runInitFile:r.restoringRossWeddell1250km_k21-80_1month.$todayDate.nc

# extract IC T and S to use as restoring target fields
ncks -v temperature,salinity $fullICfile $IC_TSfile

# rename above to what mpas expects
ncrename --bfr_sz=10000000 -v temperature,temperatureInteriorRestoringValue -v salinity,salinityInteriorRestoringValue $IC_TSfile $restoringTSfile

# run createRestoringRateCircular3D_MPAS to create Ross and Weddell fields 
# with 1 year timescale
sed -e 's/'GRIDFILE'/'$fullICfile'/g' in_Weddell_template > junk
sed -e 's/'RATE'/'$rateWeddellYear'/g' junk > in
~/bin/createRestoringRateCircular3D_MPAS < in

sed -e 's/'GRIDFILE'/'$fullICfile'/g' in_Ross_template > junk
sed -e 's/'RATE'/'$rateRossYear'/g' junk > in
~/bin/createRestoringRateCircular3D_MPAS < in

# create 1 month timescale from the 1 year fields for both Ross and Weddell
ncap2 --bfr_sz=10000000 -s 'temperatureInteriorRestoringRate=temperatureInteriorRestoringRate*365./12.;salinityInteriorRestoringRate=salinityInteriorRestoringRate*365./12.' $rateRossYear $rateRossMonth

ncap2 --bfr_sz=10000000 -s 'temperatureInteriorRestoringRate=temperatureInteriorRestoringRate*365./12.;salinityInteriorRestoringRate=salinityInteriorRestoringRate*365./12.' $rateWeddellYear $rateWeddellMonth

# rename Weddell fields so we can add them to the Ross fields
ncrename --bfr_sz=10000000 -v temperatureInteriorRestoringRate,temperatureInteriorRestoringRateWeddell -v salinityInteriorRestoringRate,salinityInteriorRestoringRateWeddell $rateRossMonth

# copy target TS file to new file which we will append Ross and Weddell, 
# then add them together
/bin/cp $restoringTSfile $rateBoth

ncks --bfr_sz=10000000 -A $rateWeddellMonth $rateBoth
ncks --bfr_sz=10000000 -A $rateRossMonth $rateBoth

# add Weddell to Ross fields
ncap2 --bfr_sz=10000000 -s 'temperatureInteriorRestoringRate=temperatureInteriorRestoringRate+temperatureInteriorRestoringRateWeddell;salinityInteriorRestoringRate=salinityInteriorRestoringRate+salinityInteriorRestoringRateWeddell' $rateBoth $rateTSboth

# copy standard IC file to the new one that will have the restoring fields appended
cp $runInitFile $finalInitFile

# append restoring fields to IC
ncks --bfr_sz=10000000 -A $rateTSboth $finalInitFile


