####  UNIX Script Documentation Block
#
# Script name: ingest_cloud_NH.sh
#
# JIF contact:  O'Reilly        org: NP11         date: 2006-08-01
#
# Abstract: This script will extract a date from AFWA cloud file and create
#           a yyyymmddhh label to use to store the file in the correct
#           place in /dcom.
#
# Script history log:
# 2006-08-23  Patrick O'Reilly  original version for implementation
#
# Usage: ingest_cloud_NH.sh $1 $2
#
#   Script parameters: $1 - full path definition for BUFR mnemonic table
#                      $2 - full path definition for NPR.CLO[NS].* file
#
#   Modules and files referenced:
#     scripts     : None
#     executables : $pgm (cld_getdate)
#
# Remarks:
#
#   Invoked by the script ingest_translate_orbits.
#
#   Imported Variables that must be passed in:
#      DATA     - path to current working directory
#      EXECbufr - path to executables
#      TANKDIR  - path to output IEEE and BUFR tank (e.g., /dcom/us007003)
#      TANKFILE - the name of the tank file to be created/appended.

#   Condition codes:
#     0     - no problem encountered
#     > 0 - some problem encountered
#       Specifically: 90 - Program cld_getdate failure
#
# Attributes:
#
#   Language: /bin/sh script
#   Machine:  IBM SP
####

set -x

cd $DATA

pgm=cld_getdate
export pgm
cwd=`pwd`
cd $DATA
. prep_step
cd $cwd

export XLFUNIT_20="$2"
export XLFUNIT_67="date_nh.grib"

$EXECbufr/cld_getdate
err=$?
if [ $err -eq 0 ]; then
echo " --------------------------------------------- "
echo " ********** COMPLETED PROGRAM   $pgm **********"
echo " --------------------------------------------- "
                  msg="PROGRAM $pgm completed normally"
                  postmsg "$jlogfile" "$msg"

else

echo "*******************************************************"
echo "********  ERROR PROGRAM $pgm  RETURN CODE $err ********"
echo "*******************************************************"
                  msg="ERROR PROGRAM $pgm RETURN CODE $err"
                  postmsg "$jlogfile" "$msg"
                  exit 90
fi

grep yyyymmddhh date_nh.grib | awk '{print $2}'
fdate=`grep yyyymmddhh date_nh.grib | awk '{print $2}'`
echo "Filedate is: $fdate."
filedate=`echo $fdate | cut -c 1-8`

if [ ! -d $TANKDIR/$filedate/$TANKFILE/cloud ] ; then
  mkdir -p $TANKDIR/$filedate/$TANKFILE/cloud
fi

if [ $SENDCOM = "YES" ] ; then
   cp $2 $TANKDIR/$filedate/$TANKFILE/cloud/$2
fi

exit 0
