#!/bin/bash
set -evx 
# 
# This script interpolates vertically ocean monthly mean temperature and 
# salinity, extrapolates them horizontally, fills up empty seas with 
# climatologies, and extrapolate vertically. These monthly mean temperature and
# salinity are to be used as reference files for ocean nudging. 
#
# History : Virginie Guemas - Initial version                           2012 
#           Virginie Guemas - ORAS4 + vertical extrapolation + 
#                             commenting and clarifying         -  July 2014
# 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
monthly_in=$1 # Monthly mean netcdf file from the reanalysis to be interpolated
              # GLORYS2v1, GLOSEA5 or ORAS4
varin=$2      # Input variable : 'votemper' or 'vosaline' - if this script
              # is extended to be used for other variables, a few lines need 
              # to be changed below in the case varin loop.
meshmaskin='/cfu/autosubmit/con_files/mesh_mask_nemo.nemovar_O1L42.nc'
              # Meshmask of the input reanalysis. Example:
              #'/cfu/autosubmit/con_files/mesh_mask_nemo.glorys2v1_O25L75.nc'
confout='ORCA1L46' # output configuration
vertgridout='/cfu/autosubmit/con_files/mesh_mask_nemo.Ec3.0_O1L46.nc'
              # Meshmask of the output grid, i.e. model and grid to be used in
              # the nudged simulation
Ptoextrap='/cfu/pub/scripts/prep_restarts/auxfiles/masks/Tofill_ecearth.v3.0.ORCA1L46.extrap.oras4.outputs.nc'
  # Locations of the points where to extrapolate horizontally the outputs after
  # interpolation, = 1 in the netcdf file, 0 everywhere else. Locations 
  # available for the tmask grid. If you don't know what is tmask,
  # read the NEMO documentation about the grid.
  # To obtain the Ptoextrap netcdf file, it is necessary to run once this script
  # without extrapolation and filling of the empty seas and compare the output
  # with the meshmask of the output grid. To do so, set 0 everywhere in 
  # Ptoextrap and use build_Tofill.bash afterwards.
Ptofillclim='/cfu/pub/scripts/prep_restarts/auxfiles/masks/Tofill_ecearth.v3.0.ORCA1L46.clim.nc'
  # Locations of the empty seas that need to be filled up with a climatology 
  # after interpolation = 1 in the netcdf file, 0 everywhere else. Locations 
  # available for the tmask grid. If you don't know what is tmask,
  # read the NEMO documentation about the grid.
  # To obtain the Ptofillclim netcdf file, it is necessary to run once this 
  # script without extrapolation and filling of the empty seas and compare the
  # output with the meshmask of the output grid. To do so, set 0 everywhere in 
  # Ptofillclim and use build_Tofill.bash afterwards.
mon=$4 # Month of the restart on 2 digits MM
monthly_out=$3 # Output netcdf file name
cfutools='/home/vguemas/CFU_tools_new'
  # Location of the cfutools repository
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
source ${cfutools}/prep_restarts/library/library.bash

cdo vertsum -selvar,tmask $meshmaskin mask2din.nc
mask='tmask' ; varlon='glamt' ; varlat='gphit' ;  fill='tmask'

# The vertical interpolation is performed below
python ${cfutools}/interpolation/interp_vert.py $monthly_in $varin $meshmaskin e3t_0 mask2din.nc $mask $vertgridout e3t_0 gdept_0 int_${monthly_in}

# The function extrap extrapolates horizontally and fill in the empty seas with a climatology
extrap int_${monthly_in} ${varin} ${meshmaskin} ${varlon} ${varlat} ${Ptoextrap} ${Ptofillclim} ${fill} int2_${monthly_in} 3d $cfutools $confout $mon

# The vertical extrapolation to empty levels is performed below
python ${cfutools}/interpolation/vertextrap.py int2_${monthly_in} ${varin} $vertgridout nav_lev int3_${monthly_in}

# Apply the mask
applymask $vertgridout $mask int3_${monthly_in} $varin $monthly_out

# Add vertical levels
ncrename -v z,nav_lev $monthly_out
ncks -A -v gdepw_0 $vertgridout $monthly_out

# Clean
rm -f  int_${monthly_in} int2_${monthly_in} int3_${monthly_in} mask2din.nc

