#! @SHELL@
#
AFTERTESTFILE=ECHAM5_T42L19.grb
if [ -f "$CDOTESTDATA/$AFTERTESTFILE" -a "@ENABLE_CGRIBEX@" = yes ]; then
  echo 1..4 # Number of tests to be executed.
else
  echo 1..1 # Number of tests to be executed.
fi
#
test -n "$CDO"      || CDO=cdo
test -n "$DATAPATH" || DATAPATH=./data
#
ABSLIMMAX=0.0001
CDOOUT=cout
CDOERR=cerr
FORMAT=""
NTEST=1
######################################################
IFILE=$DATAPATH/gp2sp_ref
RFILE=$DATAPATH/sp2gp_ref
OFILE=sp2gp_res
#
RSTAT=0
SELECT="TYPE=20 FORMAT=1"
CDOTEST="$SELECT"
CDOCOMMAND="$CDO $FORMAT after $IFILE $OFILE"
echo "$CDOCOMMAND"
echo $SELECT | $CDOCOMMAND
test $? -eq 0 || let RSTAT+=1
#
$CDO diff,$ABSLIMMAX $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"
test $RSTAT -eq 0 || echo "not ok $NTEST - $CDOTEST"
#
let NTEST+=1
rm -f $OFILE
rm -f $CDOOUT $CDOERR
######################################################
if [ -f "$CDOTESTDATA/$AFTERTESTFILE" -a "@ENABLE_CGRIBEX@" = yes ]; then
######################################################
SELECT=bot
IFILE=$CDOTESTDATA/$AFTERTESTFILE
RFILE=$CDOTESTDATA/after_${SELECT}_ref
OFILE=after_${SELECT}_res
#
RSTAT=0
CDOTEST="$SELECT"
CDOCOMMAND="$CDO $FORMAT after $IFILE $OFILE"
echo "$CDOCOMMAND"
$CDOCOMMAND << EOR
  CODE=91,92,93,94,95,96,97,102,103,104,105,106,107,108,109,110,111,112,113,
       114,115,116,117,119,120,121,122,123,124,125,126,
       129,134,139,140,141,142,143,144,145,146,147,150,151,160,161,164,165,166,
       167,168,169,171,175,176,177,178,179,180,181,182,184,
       185,186,187,188,193,197,203,204,205,206,207,208,209,
       210,211,213,214,216,218,221,222,230,231,233,260
  TYPE=20
  FORMAT=1
  MEAN=1
EOR
test $? -eq 0 || let RSTAT+=1
#
$CDO diff,$ABSLIMMAX $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"
test $RSTAT -eq 0 || echo "not ok $NTEST - $CDOTEST"
#
let NTEST+=1
rm -f $OFILE
rm -f $CDOOUT $CDOERR
######################################################
SELECT=atm
IFILE=$CDOTESTDATA/$AFTERTESTFILE
RFILE=$CDOTESTDATA/after_${SELECT}_ref
OFILE=after_${SELECT}_res
#
RSTAT=0
CDOTEST="$SELECT"
CDOCOMMAND="$CDO $FORMAT after $IFILE $OFILE"
echo "$CDOCOMMAND"
$CDOCOMMAND << EOR
  CODE=130,131,132,133,135,153,154,156,157,223
  LEVEL=100000,92500,85000,77500,70000,60000,50000,40000,30000,25000,
         20000,15000,10000,7000,5000,3000,2000,1000,700,500,300,200,100,50,20,10
  TYPE=30
  FORMAT=1
  MEAN=1
EOR
test $? -eq 0 || let RSTAT+=1
#
$CDO diff,$ABSLIMMAX $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"
test $RSTAT -eq 0 || echo "not ok $NTEST - $CDOTEST"
#
let NTEST+=1
rm -f $OFILE
rm -f $CDOOUT $CDOERR
######################################################
SELECT=atm2
IFILE=$CDOTESTDATA/$AFTERTESTFILE
RFILE=$CDOTESTDATA/after_${SELECT}_ref
OFILE=after_${SELECT}_res
#
RSTAT=0
CDOTEST="$SELECT"
CDOCOMMAND="$CDO $FORMAT after $IFILE $OFILE"
echo "$CDOCOMMAND"
$CDOCOMMAND << EOR
  CODE=138,148,149,155
  LEVEL=100000,92500,85000,77500,70000,60000,50000,40000,30000,25000,
         20000,15000,10000,7000,5000,3000,2000,1000,700,500,300,200,100,50,20,10
  TYPE=70
  FORMAT=1
  MEAN=1
EOR
test $? -eq 0 || let RSTAT+=1
#
$CDO diff,$ABSLIMMAX $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"
test $RSTAT -eq 0 || echo "not ok $NTEST - $CDOTEST"
#
let NTEST+=1
rm -f $OFILE
rm -f $CDOOUT $CDOERR
######################################################
fi
#
exit 0
