#!/bin/sh
#
#****s* fm/test_fm_iono.sh
#
# NAME
#    test_fm_iono.sh - Test the ropp_fm program on 1D ECMWF backgrounds (L1/2 bangles).
#
# SYNOPSIS
#   test_fm_iono.sh
#
# DESCRIPTION
#   Runs the ropp_fm executable  ../tools/ropp_fm_bg2ro_1d.
#   Uses input netCDF files      ../data/bgr*_YYYY.nc.
#   Produces output netCDF files bgr*_YYYY_iono.nc and bgr*_YYYY_neut.nc.
#   These are compared against ../data/bgr*_YYYY_iono_ref.nc.
#
#   Calls ropp_fm to compute L1 and L2 bending angle from
#   5 ECMWF temperature, water vapor, and pressure profiles.  These files
#   also contain (different) ionospheric parameters {Ne_max, H_peak, H_width}.
#
#   The bending angles are numerically compared against pre-calculated ones.
#
# NOTES
#   1) If $ROPP_VERBOSE is set, the log files wil be 'cat'ed.
#
# 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/bgr20090401_000329_M02_2030337800_N0007_YYYY.nc

  case $i_test in

    1)  # default modelling of neutral bangle
      TEST="t_fm_iono_"${i_test}
      COMMENT="FM iono; L_neutral"
      EXTRA_CMD=""
      OFILE=`basename $IFILE |sed -es/'.nc'/'_neut.nc'/`
      RFILE=`echo $IFILE |sed -es/'.nc'/'_iono_ref.nc'/` # Not a typo
      ;;

    2)  # direct modelling of L1 and L2
      TEST="t_fm_iono_"${i_test}
      COMMENT="FM iono; L1 and L2"
      EXTRA_CMD=" -direct_ion"
      OFILE=`basename $IFILE |sed -es/'.nc'/'_iono.nc'/`
      RFILE=`echo $IFILE |sed -es/'.nc'/'_iono_ref.nc'/`
      ;;

  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 Echo $LOGFILE if requested
# ------------------------------

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

# 3.3 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
