#!/bin/sh
#
#****s* fm/test_fm_2D.sh
#
# NAME
#    test_fm_2D.sh - Test the 2D ropp_fm program on ECMWF backgrounds.
#
# SYNOPSIS
#   test_fm_2D.sh
#
# DESCRIPTION
#   Runs the ropp_fm executable ../tools/ropp_fm_bg2ro_2d.
#   Uses input netCDF file ../data/ECMWF_10_OCCS.nc.
#   The FM computes bending angle from a ECMWF temperature,
#   water vapor, and pressure profile using a 2D operator.
#   IDL code plots the results, which can be compared to reference plots.
#   The output and reference fields are numerically compared with ropp_fm_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/002)
#
#****


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

for i_test in 1 2 ; do

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

  EXEC=../tools/ropp_fm_bg2ro_2d

  IFILE='../data/ECMWF_10_OCCS.nc'

  case $i_test in

    1)  # default options
      TEST="t_fm_2D_"${i_test}
      COMMENT="FM 2D; default options"
      EXTRA_CMD=""
      OFILE=ECMWF_10_OCCS_out.nc
      RFILE=../data/ECMWF_10_OCCS_out_reference.nc
      PFILE=ECMWF_10_OCCS_out.jpg
      RPFILE=../data/ECMWF_10_OCCS_out_reference.jpg
      ;;

    2)  # compressibility options
      TEST="t_fm_2D_"${i_test}
      COMMENT="FM 2D; compress factors"
      EXTRA_CMD=" -comp"
      OFILE=ECMWF_10_OCCS_out_comp.nc
      RFILE=../data/ECMWF_10_OCCS_out_comp_reference.nc
      PFILE=ECMWF_10_OCCS_out_comp.jpg
      RPFILE=../data/ECMWF_10_OCCS_out_comp_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_fm/tests" ] ; then
    echo "*** Not in ropp_fm*/tests subdirectory - test NOT PERFORMED"  >> $LOGFILE
    exit 1
  fi


#-------------------------------------------------------------------------------
# 2. Run the ropp_fm executable
#-------------------------------------------------------------------------------

  if [ -f $EXEC ]; then

    echo "./$EXEC  ${EXTRA_CMD}  $IFILE  -o $OFILE  -d"  >> $LOGFILE
          ./$EXEC  ${EXTRA_CMD}  $IFILE  -o $OFILE  -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
    plot_fm_twod, '${OFILE}', extra_command='${EXTRA_CMD}'
EOF

    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_fm_compare to compare results
# ------------------------------------------

  ./ropp_fm_compare $OFILE $RFILE $TEST $COMMENT


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

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

done  # loop over options

exit 0
