####  UNIX Script Documentation Block
#
# Script name:   ingest_cloud_NASA.sh
#
# JIF contact:  J. Ator     Org: NCO         Date: 2010-06-12
#
# Abstract: This script reads a file of NASA Langley cloud pixel data,
#   converts it to NCEP BUFR format, and writes it to the observational
#   database.
#
# Script history log:
# 2010-06-12  Jeff Ator     Original version for implementation
#
# Usage: ingest_cloud_NASA.sh  $1 $2
#
#   Script parameters: $1 - full path definition for BUFR mnemonic table
#                      $2 - full path definition for NASA Langley data file
#
#   Modules and files referenced:
#     scripts     : $DATA/prep_step
#                   $DATA/postmsg
#                   $USHbufr/tranjb
#     executables : $EXECbufr/decod_dcncld
#
# 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
#      TANKDIR  - path to output BUFR database (e.g., /dcom/us007003)
#
#   Condition codes:
#     0 - no problem encountered
#   > 0 - some problem encountered
#     Specifically: 100 - input NASA Langley cloud pixel data file not found
#                   110 - unable to create BUFR output; see decoder log
#                         $TANKDIR/decoder_logs/decod_dcncld.log for details
#                   120 - unable to ingest BUFR output in database; see
#                         $TANKDIR/errlog.out for details
#
# Attributes:
#   Language: UNIX script
#   Machine:  NCEP CCS
####

set -aux

cd $DATA


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

table=$1
file=$DATA/$2

set +x
echo
echo "table = $table"
echo
echo "file = $file"
echo
set -x


#  Check for existence of input 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 100
fi


TANKDIRbeg=`echo $TANKDIR | cut -c1-5`
if [ $TANKDIRbeg = /dcom ] ; then

#  Save the original NetCDF file in /dcom or /dcomdev
#   (only invoked for production or parallel-production jobs)
#  ----------------------------------------------------------

   savefile=$(basename $file)

   msg="copying $savefile to $TANKDIR/langley_cloud"
   $DATA/postmsg "$jlogfile" "$msg"

   if [ ! -e $TANKDIR/langley_cloud ] ; then
      mkdir -p $TANKDIR/langley_cloud
   fi

   cp $savefile $TANKDIR/langley_cloud

   retc=$?
   if [ $retc -ne 0 ] ; then
      set +x
      echo
      echo "FAILURE IN COPY OF $savefile to $TANKDIR/langley_cloud"
      echo
      set -x
   fi

#  Scrub any files here older than 2 days (based on modifcation time)
#  ------------------------------------------------------------------

  numoldfile=$(find $TANKDIR/langley_cloud -name G\* -mtime +2 \
                             -print | wc -l)
  if [ $numoldfile -gt 0 ] ; then
    set +x
    echo
    echo "The following files are older than 2 days" \
         "and are being deleted from $TANKDIR/langley_cloud :"
    echo
    set -x
    find $TANKDIR/langley_cloud -name G\* -mtime +2 -print \
                  -exec rm {} \;
  fi

fi


#  Unzip input data file and store the output in the current working directory
#  ---------------------------------------------------------------------------

infile=`basename $file .gz`

gzip -d $file -c > $infile


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

GEMERR=`grep ^export /nwprod/gempak/.gempak | cut -f2 -d=`/gempak/error

outfile=$file.BUFR.out.$$

pgm=$EXECbufr/decod_dcncld

if [ -s prep_step ]; then
  set +u
  . prep_step
  set -u
fi

msg="decod_dcncld has BEGUN"
postmsg "$jlogfile" "$msg"

$pgm -v 2 -d $TANKDIR/decoder_logs/decod_dcncld.log $infile $table $outfile
if [ $? -eq 0 ] ; then
  set +x
  echo
  echo "Program decod_dcncld completed successfully"
  echo
  set -x
else
  set +x
  echo
  echo "PROBLEM IN PROGRAM decod_dcncld - ABORT with return code 110"
  echo
  set -x
  exit 110
fi


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

export cword=no
$USHbufr/tranjb $TANKDIR $outfile
if [ $? -eq 0 ] ; then
  set +x
  echo
  echo "Program tranjb completed successfully"
  echo
  set -x
else
  set +x
  echo
  echo "PROBLEM IN PROGRAM tranjb - ABORT with return code 120"
  echo
  exit 120
fi

#  Clean up
#  --------

rm -f $infile $outfile


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

exit 0
