#!/bin/sh
#
#  UTILITY SCRIPT NAME :  wrcorcx.sh
#               AUTHOR :  Hua-Lu pan
#         DATE WRITTEN :  03/12/97
#
#  Abstract:  This utility script produces the auus03
#             bulletins, from the GFS forecast fields
#
#     Input:  none
#             This job requires the presence of the 65x65 GRIB file
#               for hours 00, 12, 24,...,72
#
echo "History: SEPT    1996 - First implementation of this utility script"
echo "History: SEPT    1999 - IBM SP port of this utility script"
echo "History: MARCH   2004 - Modified for new header FXUS3 -> AUUS03"
echo "History: OCT     2005 - Modified to send products to TOC via NTC"
#

set -x

#############################
# Set Vars for Interactive Run
#############################
export EXECutil=${EXECutil:-/nwprod/util/exec}
export PARMutil=${PARMutil:-/nwprod/util/parm}
export USHutil=${USHutil:-/nwprod/util/ush}
export EXECgraph=${EXECgraph:-/nwprod/util/exec}
export PARMgraph=${PARMgraph:-/nwprod/util/parm}
export FIXgraph=${FIXgraph:-/nwprod/util/fix}
export envir=${envir:-prod}
export jlogfile=${jlogfile:-jlogfile}
export NET=${NET:-gfs}
export RUN=${RUN:-gfs}
export cyc=${cyc:-00}
export cycle=${cycle:-t${cyc}z}
export SENDCOM=${SENDCOM:-NO}
export SENDDBN=${SENDDBN:-NO}
if [ -z "$DATA" ]
then
   export DATA=`pwd`
   cd $DATA
   /nwprod/util/ush/setup.sh
   /nwprod/util/ush/setpdy.sh
   . PDY
fi
export COMIN=${COMIN:-/com/$NET/$envir/$NET.$PDY}
export pcom=${pcom:-/pcom/$NET}
export pgmout=${pgmout:-OUTPUT.$$}

cd $DATA

#############################
# Copy Files into Tmp Working Area
#############################
for i in 00 12 24 36 48 72
do
   if test -f $COMIN/gfs.${cycle}.grb65f$i
   then
      cp $COMIN/gfs.${cycle}.grb65f$i grb65f$i
      cp $COMIN/gfs.${cycle}.grb65if$i grb65if$i
   else
      sh $USHutil/mkpost65.sh $i
      mv gfs.${cycle}.grb65f$i grb65f$i
      mv gfs.${cycle}.grb65if$i grb65if$i
   fi
done

cp $PARMutil/bulls_wrcord parmcorv

#############################
# Begin Processing Files
#############################
export pgm=wrcorcx
. prep_step

export XLFUNIT_10="parmcorv"
export XLFUNIT_11="grb65f00"
export XLFUNIT_12="grb65f12"
export XLFUNIT_13="grb65f24"
export XLFUNIT_14="grb65f36"
export XLFUNIT_15="grb65f48"
export XLFUNIT_16="grb65f72"
export XLFUNIT_31="grb65if00"
export XLFUNIT_32="grb65if12"
export XLFUNIT_33="grb65if24"
export XLFUNIT_34="grb65if36"
export XLFUNIT_35="grb65if48"
export XLFUNIT_36="grb65if72"
export XLFUNIT_88="auus03.bul"

startmsg
$EXECutil/wrcorcx
export err=$?; err_chk

if test "$err" = 0
then
  msg="wrcorc successfully made"
  postmsg "$jlogfile" "$msg"
else
  msg="wrcorc failed "
  postmsg "$jlogfile" "$msg"
fi

############################################
#  Send bulletin through dbNet
############################################

if test "$SENDCOM" = 'YES'
then
  cp auus03.bul $pcom/auus03.bul.$job

  if test "$SENDDBN" = 'YES'
  then
    $USHutil/make_ntc_bull.pl  WMOBH NONE KWNO NONE    \ 
         $DATA/auus03.bul    $pcom/auus03.bul.$job
    export err=$?;

    if test "$err" = 0
    then
      msg="auus03 dbnet alert successfully done"
      postmsg "$jlogfile" "$msg"
    else
      msg="auus03 dbnet alert failed "
      postmsg "$jlogfile" "$msg"
    fi
  fi
fi

exit
