#!/bin/sh
#
#****s* 1d_var/test_1dvar_COSMIC_refrac.sh
#
# NAME
#    test_1dvar_COSMIC_refrac.sh - Test the ropp_1dvar program.
#
# SYNOPSIS
#   test_1dvar_COSMIC_refrac.sh
#
# DESCRIPTION
#   Runs the ropp_1dvar executable ropp_1dvar_refrac.
#   The 1D-VAR tool retrieves temperature, water vapor, and pressure from
#   5 COSMIC refractivity profiles and co-located Met Office
#   background fields.
#   The IDL code checks the differences between input and output
#   refractivities and plots the differences.
#   These are compared with the reference results.
#
# 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 FILE_ID in 04_refrac 04comp_refrac 04newop_refrac ; do

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

  EXEC=../tools/ropp_1dvar_refrac

  OBS=../data/IT-1DVAR-04_y.nc            # observations created using the background in this test

  BG_COV=../data/IT-1DVAR-04_c.nc         # correlations for bg errors

  OB_COV=../data/IT-1DVAR-04_o.nc         # correlations for ob errors

  BACK=../data/IT-1DVAR-04_b.nc           # background data

  CONFIG=../config/metoffice_refrac_1dvar.cf  # configuration file (obs_covar_method = VSFC, bg_covar_method = VSFC)

  OFILE=IT-1DVAR-${FILE_ID}.1.nc                      # output solution

  RFILE=../data/IT-1DVAR-${FILE_ID}.1_reference.nc    # reference solution

  PFILE=ropp_1dvar_${FILE_ID}.jpg                            # output picture

  RPFILE=../data/ropp_1dvar_${FILE_ID}_reference.jpg  # reference picture

  case $FILE_ID in

    04_refrac)  # default options
      TEST_NAME=t_1dvar_COSMIC_${FILE_ID}
      COMMENT="1DVAR COSMIC; default"
      EXTRA_CMD=""
      ;;

    04comp_refrac)  # compressibility factors
      TEST_NAME=t_1dvar_COSMIC_${FILE_ID}
      COMMENT="1DVAR COSMIC; comp factors"
      EXTRA_CMD=" -comp"
      ;;

    04newop_refrac)  # new interpolation scheme
      TEST_NAME=t_1dvar_COSMIC_${FILE_ID}
      COMMENT="1DVAR COSMIC; new interp"
      EXTRA_CMD=" -new_op"
      ;;

  esac

  LOGFILE=${TEST_NAME}.log

  echo " "
  echo "Running $TEST_NAME ($COMMENT) ..."

  echo "*** Results log of $TEST_NAME ($COMMENT) ***" > $LOGFILE

  if [ "$(echo $(basename $(dirname $PWD)) |cut -c1-10)"/"$(basename $PWD)" != "ropp_1dvar/tests" ] ; then
    echo "*** Not in ropp_1dvar*/tests subdirectory - test NOT PERFORMED"  >> $LOGFILE
    exit 1
  fi


#-------------------------------------------------------------------------------
# 2. Run the ropp_1dvar executable
#-------------------------------------------------------------------------------

  if [ -f $EXEC ]; then

    echo "./$EXEC  -y $OBS  -b $BACK  --bg-corr $BG_COV  --obs-corr $OB_COV  -c $CONFIG  -o $OFILE ${EXTRA_CMD}  -d"  >> $LOGFILE
          ./$EXEC  -y $OBS  -b $BACK  --bg-corr $BG_COV  --obs-corr $OB_COV  -c $CONFIG  -o $OFILE ${EXTRA_CMD}  -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_1dvar, '${FILE_ID}', extra_command='${EXTRA_CMD}'
EOF

    echo "*** Finished IDL"  >> $LOGFILE
    PFILE1=`echo $PFILE |sed -es/'.jpg'/'_refrac.jpg'/`
    if [ -f $PFILE1 ] ; then
      mv $PFILE1 $PFILE
    fi
    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_1dvar_compare to compare results
# ------------------------------------------

  ./ropp_1dvar_compare $OFILE $RFILE $TEST_NAME $COMMENT # Compare refrac and retrieved T, p, q, Z

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

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


done  # loop over options

exit 0
