#!/bin/sh
set -e

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

pfx=$1
inp1=$2
inp2=$3
inp3=$4
inp4=$5
inp5=$6
inp6=$7
out=$8

# 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

rm -f tmp*

access  phyfe ${pfx}_${inp1}.nc
access  phy2fe ${pfx}_${inp2}.nc
# sum two phytoplankton fractions and convert to mol m^-3
cdo add phyfe phy2fe phy0
cdo mulc,1e-9 phy0 phy
release phyfe phy2fe

access  zoo ${pfx}_${inp3}.nc
access  zoo2 ${pfx}_${inp4}.nc
cdo add zoo zoo2 z
release zoo zoo2

access  poc ${pfx}_${inp5}.nc
access  goc ${pfx}_${inp6}.nc
cdo add poc goc d
release poc goc

# combine ZOO* and POC/GOC (pools with fixed Fe/C)
cdo add z d tmp1
# calculate Fe content (in the model code, rr_c2n=6.625 and rr_fe2n=33. so rr_fe2c=4.9811320)
cdo mulc,4.981132e-9 tmp1 tmp2
# add phytoplankton Fe
cdo add phy tmp2 ${pfx}_${out}

rm -f tmp* d z phy phy0

