#!/bin/sh
#
#****s* pp/test_pp_wopt_2d.sh
#
# NAME
#   test_pp_wopt_2d.sh
#
# SYNOPSIS
#   test_pp_wopt_2d.sh
#
# DESCRIPTION
#   Tests the ropp_pp executable ../tools/ropp_pp_wopt_2d_tool,
#   which generates excess phases from a 2D refractivity slice.
#   Uses input netCDF file ../data/CASE_12_ref2d.nc, one of the
#   'standard' 55 refractivity test cases.
#   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) Uses the low resolution configuration file quick_opt.nml,
#      so scientific integrity of test results is very low.
#   2) If $ROPP_VERBOSE is set, the log files wil be 'cat'ed.
#   3) Requires IDL to generate plots (not essential).
#
# REFERENCES
#   Description of wave optics modelling in ROPP-9 and suggested improvements for ROPP-9.1 
#   (SAF/ROM/ECMWF/REP/RSR/028)
#
#   ROPP Input/Output Module User Guide
#   (SAF/ROM/METO/UG/ROPP/002)
#
#   ROPP Pre-processor Module User Guide
#   (SAF/ROM/METO/UG/ROPP/004)
#
#****

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

for i_test in 1 ; do

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

  EXEC=../tools/ropp_pp_wopt_2D_tool

  IFILE='../data/CASE_12_ref2d.nc'

  CFILE=../config/quick_wopt.nml

  case $i_test in

    1)  # 'quick' options
      TEST="t_pp_wopt_2d_"${i_test}
      COMMENT="PP 2D WOPT; quick options"
      EXTRA_CMD=""
      OFILE=$(basename $IFILE |sed -es/".nc"/"_out_${i_test}.nc"/)
      RFILE=../data/$(basename $OFILE |sed -es/".nc"/"_reference.nc"/)
      PFILE=$(basename $IFILE |sed -es/".nc"/"_out_${i_test}.jpg"/)
      RPFILE=../data/$(basename $PFILE |sed -es/".jpg"/"_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  -c $CFILE  -o $OFILE  "  >> $LOGFILE
          ./$EXEC  ${EXTRA_CMD}  $IFILE  -c $CFILE  -o $OFILE     >> $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_wopt_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
