#! @SHELL@
if [ "$USER" = "m214003" ]; then
echo 1..32 # Number of tests to be executed.
else
echo 1..20 # Number of tests to be executed.
fi
#
test -n "$CDO"      || CDO=cdo
test -n "$DATAPATH" || DATAPATH=./data
#
ABSLIMMAX=0.001
ABSLIMYCON=0.25
CDOOUT=cout
CDOERR=cerr
FORMAT="-f srv -b 32"
GRIDS="n16 n32"
#
IFILE=$DATAPATH/bathy4.grb
NTEST=1
#
for GRIDTYPE in "regular" "curvilinear" "unstructured"; do
  SETGRID=""
  if [ "$GRIDTYPE" != "regular" ]; then SETGRID="-setgridtype,$GRIDTYPE"; fi
  for GRID in $GRIDS; do
# remaplaf: sort could give different results"
    RMODS="bil bic con ycon"
    if [ "$GRIDTYPE" = "unstructured" ]; then RMODS="con ycon"; fi
    if [ "$USER" = "m214003" ]; then RMODS="dis nn $RMODS"; fi
    for RMOD in $RMODS; do
      ABSLIM=$ABSLIMMAX
      if [ $RMOD = ycon ]; then ABSLIM=$ABSLIMYCON; fi
      RSTAT=0
      OFILE=${GRID}_${RMOD}
      RFILE=$DATAPATH/${OFILE}_ref

      CDOTEST="gen${RMOD} $GRID $GRIDTYPE"
      CDOCOMMAND="$CDO $FORMAT gen${RMOD},$GRID $SETGRID $IFILE remapweights"

      if [ "@ENABLE_NETCDF@" = yes ] ; then
        echo "Running test: $NTEST"
        echo "$CDOCOMMAND"

        $CDOCOMMAND
        test $? -eq 0 || let RSTAT+=1

        $CDO $FORMAT remap,$GRID,remapweights $SETGRID $IFILE ${OFILE}
        test $? -eq 0 || let RSTAT+=1

        $CDO diff,$ABSLIM $OFILE $RFILE > $CDOOUT 2> $CDOERR
        test $? -eq 0 || let RSTAT+=1
        test -s $CDOOUT && let RSTAT+=1
        cat $CDOOUT $CDOERR

        test $RSTAT -eq 0 && echo "ok $NTEST - $CDOTEST"
        if [ $RMOD = dis ]; then
          test $RSTAT -eq 0 || echo "not ok $NTEST - $CDOTEST # TODO check depends on optimization"
        else
          test $RSTAT -eq 0 || echo "not ok $NTEST - $CDOTEST"
        fi
      else
        test $RSTAT -eq 0 && echo "ok $NTEST - $CDOTEST # SKIP netCDF not enabled"
      fi

      let NTEST+=1
      rm -f $OFILE remapweights
    done
  done
done
#
rm -f $CDOOUT $CDOERR
#
exit 0
