#  UTILITY SCRIPT NAME :  mkwfcgrd.sh
#               AUTHOR :  Mary Jacobs
#         DATE WRITTEN :  11/06/96
#
#  Abstract:  This utility script produces the WAFC GRID
#             bulletins, from the GFS analysis and forecast fields  
#             transmission to Australia.
#
#     Input:  1 argument are passed to this script.   
#             1st argument - Forecast Hour - format of 2I
#
echo "History: SEPT    1996 - First implementation of this utility script"
echo "History: AUG     1999 - Modified for IBM SP"
#

set +x
hour="$1"
num=$#

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

set +x
echo " ------------------------------------------"
echo " BEGIN MAKING ${NET} WAFC GRID PRODUCTS"  
echo " ------------------------------------------"
set -x

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


############################################
# Figure out INPUT/OUTPUT/PARM/EXEC Fields
############################################

if test ! -f pgrbf${hour}
then
   cp $COMIN/${RUN}.${cycle}.pgrbf${hour} pgrbf${hour}
fi

if test ! -f $COMIN/${RUN}.${cycle}.pgrbf${hour}
then
   #$EXECutil/grbindex pgrbf${hour} pgrbif${hour}
   /nwprod/util/exec/grbindex pgrbf${hour} pgrbif${hour}
else
   cp $COMIN/${RUN}.${cycle}.pgrbif${hour} pgrbif${hour}
fi

##############################
# Process WAFS
##############################

export pgm=wafcgrdc
. prep_step

case $hour in
   12)export XLFUNIT_11="pgrbf${hour}"
      export XLFUNIT_31="pgrbif${hour}"
      export XLFUNIT_20="$PARMutil/bulls_wafcdat1"
      ;;
   24)hour1=`expr $hour - 6`
      if test ! -f pgrbf${hour1}
      then
         cp $COMIN/${RUN}.${cycle}.pgrbf${hour1} pgrbf${hour1}
         if test ! -f $COMIN/${RUN}.${cycle}.pgrbf${hour}
         then
            $EXECutil/grbindex pgrbf${hour} pgrbif${hour}
         else
            cp $COMIN/${RUN}.${cycle}.pgrbif${hour1} pgrbif${hour1}
         fi
      fi
      export XLFUNIT_11="pgrbf${hour1}"
      export XLFUNIT_31="pgrbif${hour1}"
      export XLFUNIT_12="pgrbf${hour}"
      export XLFUNIT_32="pgrbif${hour}"
      export XLFUNIT_20="$PARMutil/bulls_wafcdat2"
      ;;
   36)export XLFUNIT_11="pgrbf${hour}"
      export XLFUNIT_31="pgrbif${hour}"
      export XLFUNIT_20="$PARMutil/bulls_wafcdat3"
      ;;
    *)echo "Execution only Expected for hours 12/24/36."
      ;;
esac

export XLFUNIT_88="grdbuls.wafcgr${hour}.tran"

$EXECutil/wafcgrdc >>$pgmout 2>errfile
export err=$?;err_chk

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

if test "$SENDCOM" = 'YES'
then
   cp grdbuls.wafcgr${hour}.tran $pcom/grdbuls.wafcgr${hour}.tran.$job
fi

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

if test "$SENDDBN" = 'YES'
then
    $USHutil/make_ntc_bull.pl  WMOBH NONE KWNO NONE    \
        grdbuls.wafcgr${hour}.tran    $pcom/grdbuls.wafcgr${hour}.tran.$job
else
  msg="File grdbuls.wafcgr${hour}.tran.$job not posted to db_net."
  postmsg "$jlogfile" "$msg"
fi

msg="WAFC Processing ${hour} hour  completed normally"
postmsg "$jlogfile" "$msg"

exit
