#! @SHELL@
echo 1..2 # Number of tests to be executed.
#
test -n "$CDO"      || CDO=cdo
test -n "$DATAPATH" || DATAPATH=./data
#
NTEST=1
#
for OPERATOR in reducegrid; do
  for grid  in r18x9 icon_cell; do
    RSTAT=0

    REFGRID="${DATAPATH}/${grid}_grid"
    REF="$DATAPATH/griddes.${grid}"

    CDOTEST="$OPERATOR on grid ${grid}"

    if [ "@ENABLE_NETCDF@" = yes ] ; then

      $CDO -f nc -temp,$REFGRID data_${grid}.nc
      $CDO -f nc -gtc,273.15 -temp,$REFGRID mask_${grid}.nc

      echo "Running test: $NTEST"
      CDOCOMMAND="$CDO -f nc reducegrid,mask_${grid}.nc data_${grid}.nc reduced_${grid}.nc"
      echo "$CDOCOMMAND"

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

      $CDO griddes reduced_${grid}.nc > griddes.${grid}
      cmp griddes.${grid} $REF
      test $? -eq 0 || let RSTAT+=1

      rm reduced_${grid}.nc
      rm griddes.${grid}

      test $RSTAT -eq 0 && echo "ok $NTEST - $CDOTEST"
      test $RSTAT -eq 0 || echo "not ok $NTEST - $CDOTEST"
    else
      test $RSTAT -eq 0 && echo "ok $NTEST - $CDOTEST # SKIP netCDF not enabled"
    fi

    rm data_${grid}.nc
    rm mask_${grid}.nc

    let NTEST+=1
  done
done
#
exit 0
