####  UNIX Script Documentation Block
#
# Script name:   ingest_script_mls.sh
#
# RFC contact:  Keyser      Org: NP22        Date: 2009-09-30
# Updated by :  Krasowski   Org: NP22        Date: 2011-11-02
#
# Abstract: This script reads Microwave Limb Sounder (MLS) Ozone Mixing Ratio
#   files (ML2O3) in HDF5 format) from the NASA Aura POES satellite, converts
#   them into NCEP BUFR format, and writes them into the /dcom tank database on
#   the NCEP CCS machines via tranjb.
#
# Script history log:
# 2009-01-16  Dennis Keyser -- Original version for implementation
#
# Usage: ingest_script_mls.sh  $1 $2
#
#   Script parameters: $1 - full path definition for BUFR mnemonic table
#                      $2 - file name (only) definition for input mls file
#
#   Modules and files referenced:
#                   $DATA/prep_step
#                   $DATA/postmsg
#                   $USHbufr/tranjb
#     executables : $EXECbufr/bufr_tranmls
#
# Remarks:
#
#   Invoked by the script ingest_translate_orbits.
#
#   Imported Variables that must be passed in:
#      DATA     - path to current working directory
#      USHbufr  - path to ush scripts
#      EXECbufr - path to executables
#      jlogfile - path to joblog file
#      TANKDIR  - path to output BUFR tank (e.g., /dcom/us007003)
#
#   Condition codes:
#     0 - no problem encountered
#   > 0 - some problem encountered
#     Specifically:   1 = bufr_tranmls: cannot open HDF5 interface
#                     2 = bufr_tranmls: cannot open HDF5 file (note: this
#                                       error will trigger one attempted re-run
#                                       of ingest processing for mls ozone
#                                       family)
#                     3 = bufr_tranmls: cannot open a groupname
#                     4 = bufr_tranmls: cannot open an attribute
#                     5 = bufr_tranmls: cannot read attribute-based variable
#                     6 = bufr_tranmls: invalid number of along-track scan
#                                       lines in file
#                     7 = bufr_tranmls: cannot close a groupname
#                     8 = bufr_tranmls: error returned from an HDF5 interface
#                                       routine
#                   103 - input mls data file not found
#                   253 - no mls reports processed by $EXECbufr/bufr_tranmls
#                   xxx - an error cmlsng out of program BUFR_TRANJB
#
# Attributes:
#   Language: aix unix script
#   Machine:  NCEP CCS
####

set -aux

cd $DATA
pwd


#  Set environment variables for processing
#  ----------------------------------------

table=$1
file=$DATA/$2

set +x
echo
echo "table = $table"
echo
echo "file = $DATA/$2"
echo
set -x


#  Check for existence of input mls data file
#  ------------------------------------------

if [ -s $file ] ; then
  set +x
  echo
  echo "Input mls data file $file exists"
  echo
  set -x
else
  set +x
  echo
  echo "Input mls data file $file NOT FOUND - ABORTING"
  echo
  set -x
  exit 103
fi


#  Execute the program
#  -------------------

pgm=bufr_tranmls
if [ -s $DATA/prep_step ]; then
  set +u
  . $DATA/prep_step
  set -u
else
  [ -f errfile ] && rm errfile
  export XLFUNITS=0
  unset `env | grep XLFUNIT | awk -F= '{print $1}'`

  set +u
  if [ -z "$XLFRTEOPTS" ]; then
    export XLFRTEOPTS="unit_vars=yes"
  else
    export XLFRTEOPTS="${XLFRTEOPTS}:unit_vars=yes"
  fi
  set -u
fi

msg="$pgm has BEGUN"
$DATA/postmsg "$jlogfile" "$msg"

export XLFUNIT_31=$table
export XLFUNIT_51=$DATA/mls_bufr$$
timex $EXECbufr/bufr_tranmls $file 2> errfile
rcsave=$?
####################cat $DATA/mls_output
cat errfile
if [ $rcsave -eq 0 ] ; then
  set +x
  echo
  echo "Program bufr_tranmls completed successfully"
  echo
  set -x
else
  set +x
  echo
  echo "PROBLEM IN PROGRAM bufr_tranmls - ABORT with return code  $rcsave"
  echo
  set -x
  if [ $rcsave -eq 2 ]; then

#  A return code of 2 from bufr_tranmls (meaning cannot open HDF5 file) will
#   trigger one attempted re-run of ingest processing for mls ozone family
#   (the executing script ingest_translate_orbits will not generate an errlog
#   message in this case UNLESS this is the second attempt and r.c.= 2 has
#   once again occurred)
#  --------------------------------------------------------------------------

     if [ -s $DATA/run_this_again ]; then
        rcsave=20  # change r.c. from 2 to 20 if this is second attempt to
                   # ingest mls data - this signals ingest_translate_orbits
                   # to go ahead and generate an errlog message in this case
     else
          # writing the string below into the file $DATA/run_this_again will
          # later signal the job script to make one more attmept to re-run
          # the ingest processing for the mls ozone family

        echo "run this again" > $DATA/run_this_again
     fi
  fi
  exit $rcsave
fi


#  Store the mls BUFR file into the database using TRANJB processing
#  -----------------------------------------------------------------

if [ -s $DATA/mls_bufr$$ ]; then
   give_rc="YES"
   cword="NO"
   sh $USHbufr/tranjb $TANKDIR $DATA/mls_bufr$$
   ier=$?
else
   set +x
   echo
   echo "NO mls REPORTS PROCESSED by bufr_tranmls - EXIT SCRIPT WITH return \
code 253"
   echo
   set -x
   exit 253
fi

#  End of script
#  -------------

exit $ier
