#!/bin/sh
#
#****s* pp/test_pp_rs.sh
#
# NAME
#    test_pp_rs.sh
#
# SYNOPSIS
#   test_pp_rs.sh
#
# DESCRIPTION
#   Tests the ropp_pp executable ../tools/ropp_pp_grasrs2ropp.
#   Uses input netCDF file ../data/ropp_pp_grasrs_test.nc.
#   ../tools/ropp_pp_occ_tool is then used to generate refractivity from 
#   the bending angle profile.
#   IDL code plots the results, which can be compared to reference plots.
#   The output and reference fields are numerically compared with ropp_pp_compare.
#
# NOTES
#   1) If $ROPP_VERBOSE is set, the log files wil be 'cat'ed.
#   2) Requires IDL to generate plots (not essential).
#
# REFERENCES
#   See the ROPP User Guide (SAF/ROM/METO/UG/ROPP/003)
#
#****


#-------------------------------------------------------------------------------
# 0. Loop over tests
#-------------------------------------------------------------------------------

for i_test in 1 ; do

#-------------------------------------------------------------------------------
# 1. Define variables
#-------------------------------------------------------------------------------

  EXEC1=../tools/ropp_pp_grasrs2ropp

  EXEC2=../tools/ropp_pp_occ_tool

  IFILE=../data/ropp_pp_grasrs_test.nc

  TFILE=ropp_pp_test_gras.nc # intermediate file

  CFILE=../config/gras_pp.cf

  MOPT=GMSIS

  case $i_test in

    1)  # default options
      TEST="t_pp_rs_"${i_test}
      COMMENT="PP raw sampling; default options"
      EXTRA_CMD=""
      OFILE=ropp_pp_test_3m.nc
      RFILE=../data/ropp_pp_test_3m_reference.nc
      PFILE=ropp_pp_test_3m.jpg
      RPFILE=../data/ropp_pp_test_3m_reference.jpg
      ;;

  esac

  LOGFILE=${TEST}.log

  echo " "
  echo "Running $TEST ($COMMENT) ..."

  echo "*** Results log of $TEST ($COMMENT) ***" > $LOGFILE

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


#-------------------------------------------------------------------------------
# 2. Run the ropp_pp executable
#-------------------------------------------------------------------------------

  if [ -f $EXEC1 -a -f $EXEC2 ]; then

    echo "./$EXEC1  ${EXTRA_CMD}  $IFILE  -o $TFILE  -d"  >> $LOGFILE
          ./$EXEC1  ${EXTRA_CMD}  $IFILE  -o $TFILE  -d  >> $LOGFILE

    echo "./$EXEC2  ${EXTRA_CMD}  $TFILE  -o $OFILE  -c $CFILE  -m $MOPT  -d"  >> $LOGFILE
          ./$EXEC2  ${EXTRA_CMD}  $TFILE  -o $OFILE  -c $CFILE  -m $MOPT  -d  >> $LOGFILE

  else

    echo "*** $EXEC1 or $EXEC2 not found - test FAILED"  >> $LOGFILE
    exit 1

  fi


#-------------------------------------------------------------------------------
# 3. Compare the results
#-------------------------------------------------------------------------------

# 3.1 Check output file is produced
# ---------------------------------

  if [ ! -f $OFILE ]; then
    echo "*** Output file $OFILE was not created - test FAILED"  >> $LOGFILE
    exit 1
  fi

# 3.2 Run idl to plot the results, if possible
# --------------------------------------------

  if which idl >/dev/null 2>&1; then

    echo "*** Running IDL to plot results"  >> $LOGFILE

    idl 1>> $LOGFILE 2>&1 << EOF
    it_pp_01, "$OFILE", ofile="$RFILE"
EOF
    if [ ! -f ropp_pp_comp.jpg ] ; then
      echo "*** ropp_pp_comp.jpg was not created by IDL - cannot compare"  >> $LOGFILE
    else
      mv ropp_pp_comp.jpg $PFILE
    fi

    echo "*** Finished IDL"  >> $LOGFILE
    echo "*** $PFILE can be compared against $RPFILE" >> $LOGFILE

  else

    echo "*** IDL not found in path: cannot plot results" >> $LOGFILE

  fi

# 3.3 Echo $LOGFILE if requested
# ------------------------------

  if [ -n ${ROPP_VERBOSE:-''} ] ; then
    cat $LOGFILE
  fi

# 3.4 Run ropp_pp_compare to compare results
# ------------------------------------------

  ./ropp_pp_compare $OFILE $RFILE $TEST $COMMENT


#-------------------------------------------------------------------------------
# 4. Finish
#-------------------------------------------------------------------------------

  echo "... examine $LOGFILE for details"
  echo " "


done  # loop over options

exit 0
