#!/bin/sh
#
#****s* fm/test_fm_1D.sh
#
# NAME
#    test_fm_1D.sh - Test the ropp_fm program on 1D ECMWF backgrounds.
#
# SYNOPSIS
#   test_fm_1D.sh
#
# DESCRIPTION
#   Runs the ropp_fm executable ../tools/ropp_fm_bg2ro_1d.
#   Uses input netCDF files ../data/bgr*_XXXX.nc.
#   The FM computes refractivity and bending angle from 5 ECMWF temperature,
#   water vapor, and pressure profiles. The test runs ropp_fm_bg2ro with a
#   set of single-profile input files.
#   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_1d

  IFILE='../data/bgr*_XXXX.nc'

  case $i_test in

    1)  # default options
      TEST="t_fm_1D_"${i_test}
      COMMENT="FM 1D; default options"
      EXTRA_CMD=""
      OFILE=ropp_fm_bgr20090401.nc
      RFILE=../data/ropp_fm_bgr20090401_reference.nc
      PFILE=ropp_fm_bgr20090401.jpg
      RPFILE=../data/ropp_fm_bgr20090401_reference.jpg
      ;;

    2)  # compressibility options
      TEST="t_fm_1D_"${i_test}
      COMMENT="FM 1D; compress factors"
      EXTRA_CMD=" -comp"
      OFILE=ropp_fm_bgr20090401_comp.nc
      RFILE=../data/ropp_fm_bgr20090401_comp_reference.nc
      PFILE=ropp_fm_bgr20090401_comp.jpg
      RPFILE=../data/ropp_fm_bgr20090401_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  -f  ${EXTRA_CMD}  $IFILE  -o $OFILE  -d"  >> $LOGFILE
          ./$EXEC  -f  ${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, '${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
