#!/bin/sh
set -e
# sum of two input files, then multiply by 1e-9 -- JRC, 2019-08-15

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

pfx=$1
inp1=$2
inp2=$3
out=$4

# 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 inp1 inp2
rm -f tmp1 tmp2 tmp3

access inp1 ${pfx}_${inp1}.nc
access inp2 ${pfx}_${inp2}.nc

# Sum two variables
cdo add inp1 inp2 tmp1

# Get the 1st level 
ncks -F -d deptht,1,1 tmp1 tmp2

# Remove deptht
ncwa -O -a deptht tmp2 tmp3

# Multiple 0.000001
cdo mulc,0.000000001 tmp3 ${pfx}_${out}

release inp1 inp2
rm -f tmp1 tmp2 tmp3

