####  UNIX Script Documentation Block
#
# Script name:   ingest_script_1b.sh
#
# JIF contact:  Keyser      Org: NP22        Date: 2006-05-12
#
# Abstract: This script reads TOVS HIRS-2 and MSU instrument 1B files (in
#   NESDIS orbit-by-orbit 1B format), converts them into both IEEE and
#   NCEP BUFR format, and writes them into the NCEP /dcom tank database on
#   the IBM CCS machines.
#
# Script history log:
# 1998-06-19  Bert Katz     original version for implementation
# 1999-09-23  Xiujuan Su    modified to seperate MSU and HIRS-2 decoding
#                           programs
# 2000-08-24  Larry Sager   modified to convert into NCEP BUFR (as well as
#                           IEEE) format and to write the BUFR files into
#                           /dcom using TRANJB
# 2001-03-15  Larry Sager   modified to add a step prior to TRANJB to execute
#                           BUFR_COMPRESS to compress BUFR messages
# 2004-01-28  Dennis Keyser added variable "compress" to namelist "input"
#                           in stdin parm herefile; removed step which executed
#                           obsolete program BUFR_COMPRESS; modified to look
#                           more like corresponding ush script
#                           ingest_script_atovs1b.sh (for eventual unification
#                           of the two scripts); improved comments and echoes
# 2006-05-12  Dennis Keyser added check for imported variable CLEAN, if YES
#                           (default) cleans out old IEEE files stored in non-
#                           production directory TANKDIR, otherwise does not
#                           clean out old files - before, always cleaned out
#                           old files (this change does not affect production
#                           where TANKDIR begins with "/dcom" - old files are
#                           never cleaned out); improved Docblock, comments
#
# Usage: ingest_script_1b.sh  $1 $2
#
#   Script parameters: $1 - full path definition for BUFR mnemonic table
#                      $2 - full path definition for TOVS 1B file
#
#   Modules and files referenced:
#     scripts     : $USHbufr/ingest_gross_window_orbits
#                   $DATA/prep_step
#                   $DATA/postmsg
#                   $USHbufr/tranjb
#     executables : $executable (either bufr_tranhirs2 or bufr_tranmsu)
#
# 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
#      FIXbufr  - path to fixed field files
#      EXECbufr - path to executables
#      TANKDIR  - path to output IEEE and BUFR tank (e.g., /dcom/us007003)
#      ndayarch - number of days that input files ftp'd from the remote machine
#                 will be kept in non-production directory TANKDIR
#                 (if CLEAN=YES)
#
#   Imported Variables that can be passed in:
#      COMPRESS - switch indicating whether or not messages in output
#                 BUFR file should be compresses (default = 'YES')
#      CLEAN    - switch to clean up "old" output IEEE files stored in non-
#                 production directory TANKDIR (default = 'YES')
#                 (does not affect production where TANKDIR begins with
#                 "/dcom")
#
#   Condition codes:
#     0 - no problem encountered
#   > 0 - some problem encountered
#     Specifically:   2 - $executable: unknown surface type or
#                                      scan position information outside range
#                     3 - $executable: unrecognized satellite identification or
#                                      unrecognized data type or
#                                      error reading header record of file
#                                      (all bufr_tranhirs2 only)
#                     4 - $executable: unrecognized satellite identification or
#                                      unrecognized data type or 
#                                      error reading header record of file
#                                      (all bufr_tranmsu only)
#                     5 - $executable: error opening data file or
#                                      invalid coefficient type specified by
#                                      user (latter bufr_tranhirs2 only)
#                     6 - $executable: unknown satellite id
#                     7 - $executable: unknown satellite instrument
#                   102 - file is outside gross window limits
#                   103 - input 1B data file not found
#                   253 - $executable: no records written, all subsequent
#                                      processing disabled
#
# Attributes:
#   Language: aix unix script
#   Machine:  NCEP CCS
####

set -aux

cd $DATA
pwd


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

table=$1
file=$DATA/$2
COMPRESS=${COMPRESS:-YES}
CLEAN=${CLEAN:-YES}

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


#  Check for existence of input 1B data file
#  -----------------------------------------

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


#  Ensure input file falls within gross time window
#  (script ingest_gross_window_orbits performs this check)
#  -------------------------------------------------------

YYMMDDGOOD=$($USHbufr/ingest_gross_window_orbits $2)
if [ $YYMMDDGOOD = NONO ] ; then
  set +x
  echo
  echo "Current time is `date -u`"
  echo "File $2 is outside gross time window limit -48 to +6 - ABORT"
  echo
  set -x
  exit 102
fi


#  Get instrument type and satellite id from file name, and do some
#  things specific to each intrument/satellite combination
#  ----------------------------------------------------------------

echo $2 | IFS="." read dum1 INSTRTYPE SATTYPE dum2

dum=$2
coefile="/dev/null"

if [ $SATTYPE = NH ]; then
  satnum=11
elif [ $SATTYPE = NI ]; then
  satnum=12
elif [ $SATTYPE = NJ ]; then
  satnum=14
else
  satnum=??
fi

if [[ $INSTRTYPE = HIRX ]] ; then
  if [ $satnum -le 14 ] ; then
    set +x
    echo
    echo
    banner "N-$satnum" "HIRS-2"
    echo
    echo
    set -x
    executable=bufr_tranhirs2
    dum=${dum1}.TOVX1B.${SATTYPE}.${dum2}
  fi

elif [[ $INSTRTYPE = HIRS ]] ; then  # Name change 10/10/2002
  if [ $satnum -le 14 ] ; then
    set +x
    echo
    echo
    banner "N-$satnum" "HIRS-2" "(STIP)"
    echo
    echo
    set -x
    executable=bufr_tranhirs2
    dum=${dum1}.TOVX1B.${SATTYPE}.${dum2}
  fi

elif [[ $INSTRTYPE = MSUX ]] ; then
  set +x
  echo
  echo
  banner "N-$satnum" "MSU 1B"
  echo
  echo
  set -x
  executable=bufr_tranmsu
  dum=${dum1}.UMSX1B.${SATTYPE}.${dum2}

elif [[ $INSTRTYPE = MSUS ]] ; then  # Name change 10/10/2002
  set +x
  echo
  echo
  banner "N-$satnum" "MSU 1B" "(STIP)"
  echo
  echo
  set -x
  executable=bufr_tranmsu
  dum=${dum1}.UMSX1B.${SATTYPE}.${dum2}

fi


#  Form parm file input to program
#  -------------------------------

cat <<EOF_parm > parm
 &input
  infile='$file',                              ! Path to input 1B data file
  outfile='$TANKDIR/$YYMMDDGOOD/mobsbfr/$dum', ! Path to output IEEE file
  compress='$COMPRESS',                        ! BUFR compression switch
  coefile='$coefile'                           ! Path to input coefficient file
 /
EOF_parm


#  Link to low topography and HIRS coefficient files
#  -------------------------------------------------

ln -sf $FIXbufr/bufr_hirsrtcf_ibm.dat hirsrtcf_ibm.dat 1>/dev/null 2>&1
ln -sf $FIXbufr/bufr_lowtopog.dat lowtopog.dat 1>/dev/null 2>&1


cd $TANKDIR
[ ! -d $YYMMDDGOOD/mobsbfr ] && mkdir -p $YYMMDDGOOD/mobsbfr
cd -
pwd


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

pgm=$executable
if [ -s prep_step ]; then
  set +u
  . 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"
postmsg "$jlogfile" "$msg"

export XLFUNIT_12=$table
export XLFUNIT_52=$DATA/bufr
timex $EXECbufr/$executable < parm 2> errfile
rcsave=$?
cat errfile
if [ $rcsave -eq 0 ] ; then
  set +x
  echo
  echo "Program $executable completed successfully"
  echo
  set -x
else
  set +x
  echo
  echo "PROBLEM IN PROGRAM $executable - ABORT with return code  $rcsave"
  echo
  set -x
  exit $rcsave
fi


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

if [ -s $DATA/bufr ]; then
  export cword=no
  $USHbufr/tranjb $TANKDIR $DATA/bufr
fi


if [ $CLEAN = YES ]; then

#  Clean up "old" output IEEE files stored in non-prod. TANKDIR directory
#  ----------------------------------------------------------------------

TANKDIRbeg=`echo $TANKDIR | cut -c1-5`
if [ $TANKDIRbeg != /dcom ] ; then
  numoldfile=$(find $TANKDIR -name NSS.$INSTRTYPE\* -mtime +$ndayarch \
                             -print | wc -l)
  if [ $numoldfile -gt 0 ] ; then
    set +x
    echo
    echo "The following files are older than $ndayarch days" \
         "and are being deleted from $TANKDIR :"
    echo
    set -x
    find $TANKDIR -name NSS.$INSTRTYPE\* -mtime +$ndayarch -print \
                  -exec rm {} \;
  fi
fi

fi


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

exit 0
