#  UTILITY SCRIPT NAME :  mkawpgphgbl.sh
#               AUTHOR :  Mary Jacobs
#         DATE WRITTEN :  11/06/96
#
#  Abstract:  This utility script produces the  AWIP Graphics
#             bulletins.  
#
#     Input:  1 argument is passed to this script.   
#             1st argument - Forecast Hour - format of 2I
#
#
echo "History: SEPT    1996 - First implementation of this utility script"
echo "         AUG     1999 - Modified for IBM SP"
#

set +x
hour=$1
hour_list=$2
num=$#

if test $num -eq 2
then
   echo " Appropriate number of arguments were passed"
   set -x
   export EXECutil=${EXECutil:-/nwprod/util/exec}
   export PARMutil=${PARMutil:-/nwprod/util/parm}
   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/${RUN}.$PDY}
   export pcom=${pcom:-/pcom/$NET}
   export pgmout=${pgmout:-OUTPUT.$$}
else
   echo ""
   echo "Usage: mkawpgphgbl.sh \$hour"
   echo ""
   exit 16
fi

set +x
echo " ----------------------------------------"
echo " BEGIN MAKING GFS AWIPS GRAPHICS PRODUCTS"  
echo " ----------------------------------------"
set -x

msg="Enter Make AWIP GRAPHICS utility."
postmsg "$jlogfile" "$msg"

##############################
# Copy Input Field to $DATA
##############################
export pgm=awipmakr
. prep_step

pgbunit=11
pgbiunit=31

for i in $hour_list
do
   if test ! -f pgrbf${i}
   then
       cp $COMIN/${RUN}.${cycle}.pgrbf${i} pgrbf${i}
   fi

   if test ! -f $COMIN/${RUN}.${cycle}.pgrbf${i}
   then
      $EXECutil/grbindex pgrbf${i} pgrbif${i}
   else
      cp $COMIN/${RUN}.${cycle}.pgrbif${i} pgrbif${i}
   fi

   eval export XLFUNIT_${pgbunit}="pgrbf${i}"
   eval export XLFUNIT_${pgbiunit}="pgrbif${i}"

   pgbunit=`expr $pgbunit + 1` 
   pgbiunit=`expr $pgbiunit + 1` 
done

cp $FIXgraph/graph_awpseed awpgph${hour}

##############################
# Create AWIP Graphics file
##############################
export XLFUNIT_51="awpgph${hour}"
export XLFUNIT_30="$PARMgraph/graph_awipmakr.${model}_t${cyc}z.${hour}"

startmsg
$EXECgraph/awipmakr >>$pgmout 2>errfile
export err=$?;err_chk
 
##############################
# Run AWIPSEND to format file for OSO transmission
##############################
export pgm=awipsend
. prep_step

export XLFUNIT_12="$FIXgraph/graph_awiplist"
export XLFUNIT_13="awpgph${hour}"
export XLFUNIT_15="$PARMgraph/graph_awpsndal"
export XLFUNIT_51="awpgph${hour}.send"

startmsg
$EXECgraph/awipsend >>$pgmout 2>errfile
export err=$?;err_chk

##############################
# Post Files to PCOM
##############################

NETA=gfs

if test "$SENDCOM" = 'YES'
then
   cp awpgph$hour $pcom/awp${NETA}gph${hour}.${job}
   cp awpgph$hour.send $pcom/awp${NETA}gph${hour}.send.${job}
fi

##############################
# Distribute Data
##############################

if test "$SENDDBN" = 'YES'
then
   $DBNROOT/bin/dbn_alert XTRN $NET $job $pcom/awp${NETA}gph${hour}.send.${job}
else
   msg="awp${NETA}gph${hour}.send.${job} file not posted to db_net."
   postmsg "$jlogfile" "$msg"
 fi
 
msg="Awip Processing ${hour} hour  completed normally"
postmsg "$jlogfile" "$msg"

exit
