# UTILITY SCRIPT NAME : mkgrdbull.sh
#              AUTHOR : David Michaud
# 
# ABSTRACT : This utility script generates GRID Bulletins from analysis
#            and forecast files.
#
# INPUT: 4 arguments are input to this script.
#         1st argument - hour list   - Fcst hour list
#         2nd argument - bullparm    - parameter input.
#         3rd argument - out         - bulletin file
#
echo "History: JAN     2000 - Modified for IBM SP"
#

set +x
hour_list=$1
bullparm=$2
out=$3
num=$#

if test $num -eq 3
then
   echo " Appropriate number of arguments were passed"
   set -x
   export EXECbulls=${EXECbulls:-/nwprod/util/exec}
   export PARMbulls=${PARMbulls:-/nwprod/util/parm}
   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.$$}
else
   echo ""
   echo "Usage: mkgrdbull.sh "hour_list" bullparm outputname"
   echo ""
   exit 16
fi

set +x
echo "########################################################"
echo "#  Begin making the VARIAN FAX CHART $out maps"
echo "########################################################"
set -x

msg="Enter Make mkgrdbull utility."
postmsg "$jlogfile" "$msg"

export pgm=gridbull
. prep_step

##############################
# Copy Input Field to $DATA
##############################
pgbunit=11
pgbiunit=31

for i in $hour_list
do
   if test "$i" = "anl"
   then
      pgrbfile="pgrbanl"
      pgrbifile="pgrbianl"
   elif test "$i" = "sstgrb"
   then
      pgrbfile="sstgrb"
      pgrbifile="sstgrb.index"
   else
      pgrbfile="pgrbf$i"
      pgrbifile="pgrbif$i"
   fi
   if test ! -f $pgrbfile
   then
      cp $COMIN/${RUN}.${cycle}.$pgrbfile $pgrbfile
   fi

   if test ! -f $pgrbifile
   then
      $EXECutil/grbindex $pgrbfile $pgrbifile
   fi

   eval export XLFUNIT_${pgbunit}="$pgrbfile"
   eval export XLFUNIT_${pgbiunit}="$pgrbifile"

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

export XLFUNIT_20="$PARMbulls/$bullparm"
export XLFUNIT_88="$out"
 
startmsg
$EXECgraph/gridbull >> $pgmout 2> errfile
export err=$?;err_chk

##############################
# Post Files to PCOM
##############################
if test "$SENDCOM" = 'YES'
then
   cp $out $pcom/$out
   ##############################
   # Distribute Data
   ##############################
   if test "$SENDDBN" = 'YES'
   then
     $DBNROOT/bin/dbn_alert TRAN $NET $job $pcom/$out
   else
      msg="$out file not posted to db_net."
      postmsg "$jlogfile" "$msg"
   fi
fi

msg="mkgrdbull completed normally"
postmsg "$jlogfile" "$msg"

exit
