#!/bin/sh
#
#****s* pp/test_pp_occ_gnos.sh
#
# NAME
#    test_pp_occ_gnos.sh
#
# SYNOPSIS
#   test_pp_occ_gnos.sh
#
# DESCRIPTION
#   Tests the ropp_pp executable ../tools/ropp_pp_occ_tool.
#   Uses input netCDF file ../data/ropp_pp_test_gnos.nc.
#   Generates bending angle, refractivity and dry temperature from a
#   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
#-------------------------------------------------------------------------------

  EXEC=../tools/ropp_pp_occ_tool

  IFILE=../data/ropp_pp_test_gnos.nc

  CFILE=../config/cosmic_pp.cf

  MFILE=../data/MSIS_coeff.nc

  MOPT=MSIS

  case $i_test in

    1)  # default options
      TEST="t_pp_occ_gnos_"${i_test}
      COMMENT="PP occ; GNOS data"
      EXTRA_CMD="-ellipsoid"
      OFILE=ropp_pp_test_gnos.nc
      RFILE=../data/ropp_pp_test_gnos_reference.nc
      PFILE=ropp_pp_test_gnos.jpg
      RPFILE=../data/ropp_pp_test_gnos_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 $EXEC ]; then

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

  else

    echo "*** $EXEC 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="$IFILE"
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
