set -evx
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This script interpolates and rotates the 10m zonal and meridional wind
# perturbations.
#
# History : Virginie Guemas - Initial version                         - 2012 
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#                                Arguments
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
year='2014'                 # Year for which to interpolate and rotate
dir='perturb1_era-dfs_mon'  # Directory from where to take the perturbations
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

for ((jmemb=1;jmemb<=4;jmemb++)) ; do

cat > scrip_use_in <<EOF
&remap_inputs
    remap_wgt   = '/cfu/pub/scripts/interpolation/weights/rmp_ERAint_to_ORCA1t_unmasked_bicubic.nc'
    infile      = '${dir}/u10_fc${jmemb}_${year}.nc'
    invertlat   = TRUE
    var         = 'u10'
    fromregular = TRUE
    outfile     = '${dir}/u10_fc${jmemb}_eraint_${year}_orca1t_unrotated.nc'
/
EOF
cp /cfu/pub/scripts/interpolation/scrip_use scrip_use
./scrip_use

cat > scrip_use_in <<EOF
&remap_inputs
    remap_wgt   = '/cfu/pub/scripts/interpolation/weights/rmp_ERAint_to_ORCA1t_unmasked_bicubic.nc'
    infile      = '${dir}/v10_fc${jmemb}_${year}.nc'
    invertlat   = TRUE
    var         = 'v10'
    fromregular = TRUE
    outfile     = '${dir}/v10_fc${jmemb}_eraint_${year}_orca1t_unrotated.nc'
/
EOF
./scrip_use

cat > namelist_rotateUVorca <<EOF
&nam_rotUV
  Ufilein   =  '${dir}/u10_fc${jmemb}_eraint_${year}_orca1t_unrotated.nc'
  Uvarin    =  'u10'
  Vfilein   =  '${dir}/v10_fc${jmemb}_eraint_${year}_orca1t_unrotated.nc'
  Vvarin    =  'v10'
  meshmask  =  '/cfu/autosubmit/con_files/mesh_mask_nemo.N3.2_O1L42.nc'
  Ufileout   =  '${dir}/u10_fc${jmemb}_eraint_${year}_orca1t.nc'
  Vfileout   =  '${dir}/v10_fc${jmemb}_eraint_${year}_orca1t.nc'
/
EOF
cp /cfu/pub/scripts/interpolation/rotateUVorca_orca1 rotateUVorca
./rotateUVorca

rm -f ${dir}/u10_fc${jmemb}_eraint_${year}_orca1t_unrotated.nc ${dir}/v10_fc${jmemb}_eraint_${year}_orca1t_unrotated.nc

done
rm -f scrip_use scrip_use_in rotateUVorca namelist_rotateUVorca
