#!/bin/sh
set -e
# Select 1st level of tmask, and then change the unit to % -- DY, Feb/2019.

if [ $# -ne 3 ] ; then
  echo "Error in $0: invalid number of arguments $#"
  exit 1
fi

pfx=$1
inp=$2
out=$3

# Get CDO / TEMPORARY!
export PATH=/fs/ssm/hpco/exp/mib002/anaconda/anaconda-4.4.0/anaconda_4.4.0_ubuntu-14.04-amd64-64/envs/cdo-1.9.0/bin:$PATH

release inp nemo_mesh_mask
rm -f tmp*

# access inp & nemo_mesh_mask.nc
access inp ${pfx}_${inp}.nc
access nemo_mesh_mask nemo_mesh_mask_rc3.nc

# Select lst level of tmask
ncks -F -v tmask -d z,1,1 nemo_mesh_mask tmp1

# Change unit from 1 to %
cdo mulc,1e+2 tmp1 tmp2

# Remove deptht
ncwa -O -a z tmp2 tmp3

# Rename t to time_counter, and tmask to areacello
ncrename -d t,time_counter -v tmask,areacello tmp3 ${pfx}_${out}
chmod ug+w ${pfx}_${out}

# Select nav_lon & nav_lat
ncks -O -C -a -v nav_lon,nav_lat inp tmp4

# Append nav_lat & nav_lon
ncks -A tmp4 ${pfx}_${out}

release inp nemo_mesh_mask
rm -f tmp*
