#!/bin/sh
set -e
# add two files and multiply by 1.4 and select every 10th year

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

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

mtype=`uname -s|tr '[A-Z]' '[a-z]'`
case $mtype in
    aix) BINDIR=~ccc101/gcm2nc/cmip5_scripts/bin_aix ;;
  linux) BINDIR=~ccc101/gcm2nc/cmip5_scripts/bin_linux ;;
      *) echo "Unkown kernel type $mtype"
         exit 1 ;;
esac
PATH="${BINDIR}:$PATH"
export PATH

access inp1 ${pfx}_${inp1}
access inp2 ${pfx}_${inp2}
add inp1 inp2 inp12
echo "                 1.4" | ccc xlin inp12 inp12mlt

# select every 10th year: depends on RUNID
runid=`echo ${pfx} | cut -f2 -d'_'`
if [ "$runid" = "iga" ] ; then # control run
  # for the control run we select years ending on 1 since they correspond to the first years of historical runs
  echo "             -1" | ccc sel10th inp12mlt ${pfx}_${out}
elif [ `echo "$runid" | cut -c1-4` = "dhfp" ] ; then # decadal
  # for decadal runs we select the last year of each decade
  echo "             10" | ccc sel10th inp12mlt ${pfx}_${out}
elif [ `echo "$runid" | cut -c1-3` = "pdr" ] ; then # pdrmip runs. (add by ryj)
  # for pdrmip runs, no selection 
  mv inp1 ${pfx}_${out}
else
  # for all remaining runs we select years divisible by 10
  echo "              0" | ccc sel10th inp12mlt ${pfx}_${out}
fi

release inp1 inp2 inp12 inp12mlt
