echo "----------------------------------------------------------------"
echo "Executing exhrly_realtime.sh.sms XXXX"
echo "This script reads the hourly or ruc dump counts" 
echo "and compares to the monthly averages. The outputs" 
echo "are used for making houlry time series as well as"
echo "hourly/ruc dump summary for WEB applications with" 
echo "alerts to SMS for critical data shortages"   
echo "Authors: Bradley Ballish & Krishna Kumar" 
echo "NCO/Data Management & Quality Assessment Branch Sep 04"
echo "NCO/Systems Integration Branch August 2008 - V. Krishna Kumar"
echo "Modified to automate GEMPAK plotting of all data types"
echo "in the bufr dump list file"
echo "Modified to extend time series for Hourly/RUC to 3 days May 2010"
echo "Modified to replace RUC with Rapid Refresh (rap) Nov 2011"
echo "----------------------------------------------------------------"

##### CHANGE LOG ####################################################
#
#  9 Sep 2009 - Jeff Stoudt added Perl script call that dynamically
#               produces the bufr_dumplist.html (Explanation of
#               Data Types) product from the bufr_dumplist file.
#               Therefore, bufr_dumplist.html is no longer a fix file,
#               and manual intervention is no longer needed to update
#               this web page whenever bufr_dumplist is modified.
#
# 12 Jan 2010 - Jeff Stoudt added copies of new fix web files, 
#               body.regexprmeta.html and DataInventory.xls which
#               are local links from the Data Deficiency Trouble-
#               shooting Guide to reference UNIX regular expresion
#               metacharacters and data type inventory specifications.
#
#    May 2010 - Kelly Malone modified to extend the time series for
#               both hourly and RUC to 3 days.  Also, added a sort 
#               to the realtime_hrlymonx output files in order to
#               alphabetize the data types for ease of reading on web.
#
#    Nov 2011 - Kelly Kempisty modified to replace the legacy RUC
#		with the new Rapid Refresh (RAP).
#
#####################################################################

cd $DATA

########################################
set -x
msg="HAS BEGUN!"
postmsg "$jlogfile" "$msg"
########################################

cp $FIXPREP/bufr_dumplist bufr_dumplist_temp
grep "#>" bufr_dumplist_temp > realtime_bufr_dumplist.txt

### Added Perl script call: Jeff Stoudt 9 Sep 2009 ###
$USHPREP/bufrdumplist.pl $DATA

fday1=${PDY}${cyc}

fday2=`$utilexec/ndate -1 $fday1`
dd2=`echo $fday2 | cut -c1-8`
hr2=`echo $fday2 | cut -c9-10`
 
hrz2=t${hr2}z
if [ $NET = "hourly" ]; then
   if test -s $COMDAY/$BASERUN.$PDY/$BASERUN.$cycle.status.tm00.bufr_d
   then
      grep REPORTS$ $COMDAY/$BASERUN.$PDY/$BASERUN.$cycle.status.tm00.bufr_d > bufrhr1
   else
      msg="current hourly bufr-d status file not available"
      echo $msg
      postmsg "$jlogfile" "$msg"
      exit
   fi
else
   if [ "$DATESTAMP" -eq $PDY ]; then
      RAPCOM1=$COMDAY/$BASERUN.$PDY/$BASERUN.$cycle.status.tm00.bufr_d
   else
      RAPCOM1=/com/${ARCH}/${envir}/${DLY}/${BASERUN}/${BASERUN}.${PDY}/$BASERUN.$cycle.status.tm00.bufr_d
   fi
   if test -s $RAPCOM1
      then
      grep REPORTS$ $RAPCOM1 > bufrhr1
   else
      msg="current hourly bufr-d status file not available"
      echo $msg
      postmsg "$jlogfile" "$msg"
      exit
   fi
fi

if [ $NET = "hourly" ]; then
   grep REPORTS$ $COMDAY/$BASERUN.$dd2/$BASERUN.$hrz2.status.tm00.bufr_d > bufrhr2
else
   if [ "$DATESTAMP" -eq $PDY ]; then
      RAPCOM2=$COMDAY/$BASERUN.$dd2/$BASERUN.$hrz2.status.tm00.bufr_d
   else
      RAPCOM2=/com/${ARCH}/${envir}/${DLY}/${BASERUN}/${BASERUN}.${dd2}/$BASERUN.$hrz2.status.tm00.bufr_d
   fi
   grep REPORTS$ $RAPCOM2 > bufrhr2
fi

if test -s $COMMON/obcount_30davg.$BASERUN.current
then
   grep "^#" $COMMON/obcount_30davg.$BASERUN.current > monthly
else
   msg="obcount_30davg.$BASERUN.current file not available"
   echo $msg
   postmsg "$jlogfile" "$msg"
   exit
fi

iread1=0

if test -s bufrhr2 ; then iread1=1
fi
echo $iread1

cat <<EOH > cardec
${fday1} ${fday2} ${iread1} ${NET}
EOH

msg="Begin program REALTIME_HRLYMON."
postmsg "$jlogfile" "$msg"

export pgm=realtime_hrlymon
. prep_step
startmsg

export XLFRTEOPTS="unit_vars=yes"     # Allow overriding default names.
export XLFUNIT_21="monthly"
export XLFUNIT_22="bufrhr1"
export XLFUNIT_23="bufrhr2"
export XLFUNIT_24="realtime_bufr_dumplist.txt"
export XLFUNIT_28="$FIXPREP/realtime_hrlymon_acardist.txt"
export XLFUNIT_29="$FIXPREP/realtime_hrlymon_amdrdist.txt"
export XLFUNIT_50="output"
export XLFUNIT_53="testg"
export XLFUNIT_55="gemout"
export XLFUNIT_56="sumtable"
export XLFUNIT_57="datatypes_plotnumbs"
export XLFUNIT_75="smslist"

$EXECPREP/realtime_hrlymonx 1>hrlymon.out < cardec 2>hrlymon.err
errhrlymon=$?

cat output > hourtest${cycle}
cat gemout | sort +1 -2 > hrly_alldatatype_${cycle}
if [ $NET = "hourly" ]; then
   cat sumtable | sort +0 -1 > hly_sumtable
   cat datatypes_plotnumbs | sort +0 -1 > hly_dataplotlist_${cycle}
else
   cat sumtable | sort +0 -1 > ${NET}_sumtable
   cat datatypes_plotnumbs | sort +0 -1 > ${NET}_dataplotlist_${cycle}
fi

if test "$SENDCOM" = 'YES'
then
   cp hrly_alldatatype_${cycle} $COMOUTRT/.
   cp $FIXWEB/index.tsg.shtml $COM1OUT/.
   cp $FIXWEB/index.dec.shtml $COM1OUT/.
   cp $FIXWEB/index.sat.shtml $COM1OUT/.
   cp $FIXWEB/body.tsg.html $COM1OUT/.
   cp $FIXWEB/body.decoders.html $COM1OUT/.
   cp $FIXWEB/body.satlutil.html $COM1OUT/.
   cp $FIXWEB/index.bufrdump.shtml $COM1OUT/.

### Modified by Jeff Stoudt 9 Sep 2009 ###
   cp $DATA/bufr_dumplist.html   $COM1OUT/.

### Modified by Jeff Stoudt 12 Jan 2010 ###
   cp $FIXWEB/body.regexprmeta.html $COM1OUT/.
   cp $FIXWEB/DataInventory.xls $COM1OUT/.

   if [ $NET = "hourly" ]; then
      cp hly_sumtable $COM1OUT/.
      cp hly_sumtable $COMTABLE/.
      cp hly_sumtable $COMOUTRT/hly_sumtable_$cycle
      cp hly_dataplotlist_${cycle} $COMOUTRT/.
      echo $PDY$cyc > $COMOUTRT/hly_$cycle
      echo $PDY$cyc > $COM1OUT/hly_latest
   else
      cp rap_sumtable $COM1OUT/.
      cp rap_sumtable $COMTABLE/.
      cp rap_sumtable $COMOUTRT/rap_sumtable_$cycle
      cp rap_dataplotlist_${cycle} $COMOUTRT/.
      echo $PDY$cyc > $COMOUTRT/rap_$cycle
      echo $PDY$cyc > $COM1OUT/rap_latest
   fi
fi

### Make a message file whenever there is a critical shortage
### as well as excess data and both occur and post it to the jlog file

if test $errhrlymon -eq 33
then
   grep ZZ smslist | cut -c1-6 >shortage_list
   msg=" Error Return in $job is $errhrlymon - a Critical Hourly Data Shortage"
   msgs=`echo |cat shortage_list`
   postmsg "$jlogfile" "$msg"
   postmsg "$jlogfile" "$msgs"
   ##################################################################
   $SMSBIN/smsmsg "*** $msg ***"
   ##############################################################
fi

if test $errhrlymon -eq 34
then
   grep XX smslist | cut -c1-6 >excess_list
   msg=" Error Return in $job is $errhrlymon - a Critical Hourly Data Excess"
   msgx=`echo |cat excess_list`
   postmsg "$jlogfile" "$msg"
   postmsg "$jlogfile" "$msgx"
   ##################################################################
   $SMSBIN/smsmsg "*** $msg ***"
   ##############################################################
fi

if test $errhrlymon -eq 35
then
   grep ZZ smslist | cut -c1-6 >shortage_list
   grep XX smslist | cut -c1-6 >excess_list
   msgs=" Error Return in $job is $errhrlymon - a Critical Hourly Data Shortage"
   msgss=`echo |cat shortage_list`
   msgx=" Error Return in $job is $errhrlymon - a Critical Hourly Data Excess"
   msgxx=`echo |cat excess_list`
   postmsg "$jlogfile" "$msgs"
   postmsg "$jlogfile" "$msgss"
   postmsg "$jlogfile" "$msgx"
   postmsg "$jlogfile" "$msgxx"
   ##################################################################
   $SMSBIN/smsmsg "*** $msg ***"
   ##############################################################
fi

#####################################################################
# GOOD RUN
echo "************** $job COMPLETED NORMALLY ON THE IBM"
echo "************** $job COMPLETED NORMALLY ON THE IBM"
echo "************** $job COMPLETED NORMALLY ON THE IBM"
set -x
#####################################################################

msg="HAS COMPLETED NORMALLY!"
echo $msg
postmsg "$jlogfile" "$msg"

############## END OF SCRIPT #######################
