#!/bin/sh
#
# $Id: t_eum2bufr_eccodes.sh 2905 2011-06-22 11:25:15Z idculv $
#
#****s* Tools/t_eum2bufr_eccodes *
#
# NAME
#    t_eum2bufr_eccodes - Test the eum2bufr_eccodes program.
#
# SYNOPSIS
#    t_eum2bufr_eccodes.sh
#
# DESCRIPTION
#    This shell script undertakes some tests of the eum2bufr program. 
#    It uses example eum format nc4 data located in the ../data directory, 
#    and creates "standard" ECMWF BUFR output.  The latter can then be compared 
#    with master versions of these files (originally also created by this 
#    script) using the standard unix utility cmp.
#
# NOTES
#   1) Assumes PWD = ropp_io/tests
#
# REFERENCES
#   See the ROPP IO User Guide (SAF/ROM/METO/UG/ROPP/002)
#
# AUTHOR
#   Met Office, Exeter, UK.
#   Any comments on this software should be given via the ROM SAF
#   Helpdesk at http://www.romsaf.org
#
# COPYRIGHT
#   (c) EUMETSAT. All rights reserved.
#   For further details please refer to the file COPYRIGHT
#   which you should have received as part of this distribution.
#
#****

if [ "$(echo $(basename $(dirname $PWD)) |cut -c1-7)"/"$(basename $PWD)" != "ropp_io/tests" ] ; then
  echo "*** Not in ropp_io*/tests subdirectory - test NOT PERFORMED"
  exit
fi

#-------------------------------------------------------------------------------

EXEC=../tools/eum2bufr_eccodes

#-------------------------------------------------------------------------------

#-------------------------------------------------------------------------------
# 1. Convert an EUM netCDF4 file to BUFR & compare with reference BUFR
#-------------------------------------------------------------------------------

echo
echo "=============================================================================="
echo "1. eum2bufr_eccodes: test eum2bufr_eccodes by comparing against reference file"
echo "=============================================================================="
echo

echo
echo "--------------------------------------------"
echo "1a. Converting EUM netCDF4 file -> BUFR file"
echo "--------------------------------------------"
echo

# Extraction of all Lev1a data
# ----------------------------

IFILE=../data/eum_test.n4

OFILE=eum_test_l.bfr
rm $OFILE > /dev/null 2>&1

echo "$EXEC  $IFILE  -o $OFILE"
      $EXEC  $IFILE  -o $OFILE

if [ ! -f $OFILE ]; then
  echo
  echo "*** Failed to generate a BUFR file: FAIL"
  exit
fi

echo
echo "-------------------------------------------------"
echo "1b. Comparing output BUFR file and reference file"
echo "-------------------------------------------------"
echo

RFILE=../data/${OFILE}_ref

echo "Using cmp to compare $OFILE and $RFILE"
cmp  $OFILE  $RFILE

if [ $? -eq 0 ]; then
  echo " "
  echo " *** No differences found in test BUFR files"
  echo " "
  echo "************************************"
  echo "*** eum2bufr_eccodes  test: PASS ***"
  echo "************************************"
else
  echo " "
  echo " *** Differences found in test BUFR files"
  echo " "
  echo "************************************"
  echo "*** eum2bufr_eccodes  test: FAIL ***"
  echo "************************************"
fi


#-----------------------------------------------------------------------------------
# 2. Check that eum2bufr_eccodes = eum2ropp_eccodes | ropp2bufr_eccodes, if possible
#-----------------------------------------------------------------------------------

if [ -f ../tools/eum2ropp_eccodes ] && [ -f ../tools/ropp2bufr_eccodes ] ; then

echo
echo "==============================================================================================="
echo "2. eum2bufrb: test eum2bufr_eccodes by comparing against eum2ropp followed by ropp2bufr_eccodes"
echo "==============================================================================================="
echo

echo
echo "----------------------------------------------------------------------------"
echo "2a. Converting EUM netCDF4 file -> BUFR file directly, with eum2bufr_eccodes"
echo "----------------------------------------------------------------------------"
echo

# Extraction of all Lev1b data
# ----------------------------

IFILE=../data/eum_test.n4

OFILE1=eum_test_b.bfr
rm $OFILE1 > /dev/null 2>&1

echo "$EXEC  $IFILE  -o $OFILE1"
      $EXEC  $IFILE  -o $OFILE1

if [ ! -f $OFILE1 ]; then
  echo
  echo "*** Failed to generate a BUFR file: FAIL"
  exit
fi

echo
echo "-----------------------------------------------------------"
echo "2b. Converting EUM netCDF4 file -> ROPP file, with eum2ropp"
echo "-----------------------------------------------------------"
echo

OFILE2=eum_test_b.nc
rm $OFILE2 > /dev/null 2>&1

echo "../tools/eum2ropp_eccodes  $IFILE  -o $OFILE2"
      ../tools/eum2ropp_eccodes  $IFILE  -o $OFILE2

if [ ! -f $OFILE2 ]; then
  echo
  echo "*** Failed to generate a ROPP file: FAIL"
  exit
fi

echo
echo "-------------------------------------------------------------"
echo "2c. Converting ROPP file to BUFR file, with ropp2bufr_eccodes"
echo "-------------------------------------------------------------"
echo

OFILE3=eum_test_b_viaropp.bfr
rm $OFILE3 > /dev/null 2>&1

echo "../tools/ropp2bufr_eccodes  $OFILE2  -o $OFILE3"
      ../tools/ropp2bufr_eccodes  $OFILE2  -o $OFILE3

if [ ! -f $OFILE3 ]; then
  echo
  echo "*** Failed to generate a ROPP file: FAIL"
  exit
fi


echo
echo "----------------------------------------------------------------------"
echo "2d. Comparing eum2bufr_eccodes to eum2ropp_eccodes | ropp2bufr_eccodes"
echo "----------------------------------------------------------------------"
echo

echo "Using cmp to compare $OFILE1 and $OFILE3"
cmp  $OFILE1  $OFILE3

if [ $? -eq 0 ]; then
  echo " "
  echo " *** No differences found in test BUFR files"
  echo " "
  echo "******************************"
  echo "*** eum2bufrb   test: PASS ***"
  echo "******************************"
else
  echo " "
  echo " *** Differences found in test BUFR files"
  echo " "
  echo "******************************"
  echo "*** eum2bufrb   test: FAIL ***"
  echo "******************************"
fi


fi
