#  UTILITY SCRIPT NAME :  mkfosspc
#               AUTHOR :  Micki Farley
#         DATE WRITTEN :  02/24/97
#
#  Abstract:  This utility script produces the GFS/MRF 2.5x5 and 5x5
#             degree GRIB bulletins (also known as FOS GRIB bulletins  
#             although these products AREN'T sent to FOS subscribers
#             anymore).
#
#     Input:  2 arguments are passed to this script.   
#             1st argument - Model - format of 3X in lower case
#             2nd argument - Forecast Hour - format of 2I
#
echo "History: MAR     1997 - First implementation of this utility script"
echo "History: AUG     1999 - Modified for implementation on IBM SP"
#

set +x
hour_list="$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: mkfosspc.sh \$hour"
   echo ""
   exit 16
fi

set +x
echo " ------------------------------------------"
echo " BEGIN MAKING GFS/MRF (FOS) GRIB PRODUCTS"  
echo " ------------------------------------------"
set -x

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

for hour in $hour_list
do

   ##############################
   # Copy Input Field to $DATA
   ##############################

   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}
   else
      cp $COMIN/${RUN}.${cycle}.pgrbif${hour} pgrbif${hour}
   fi

   ##############################
   # Process GRID
   ##############################

   export pgm=mkfosspc
   . prep_step
   export XLFUNIT_11="pgrbf${hour}"
   export XLFUNIT_31="pgrbif${hour}"
   export XLFUNIT_51="xtrn.fos${NET}${hour}"

   startmsg
   $EXECutil/mkfosspc < $PARMutil/grib_fos${NET}${hour} >> $pgmout 2>errfile
   export err=$?;err_chk

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

   if test "$SENDCOM" = 'YES'
   then
     cp xtrn.fos${NET}${hour} $pcom/xtrn.fos${NET}${hour}.$job
   fi

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

   if test "$SENDDBN" = 'YES'
   then
      $DBNROOT/bin/dbn_alert GRIB_LOW  $NET $job $pcom/xtrn.fos${NET}${hour}.$job
   else
      msg="File $pcom/xtrn.fos${NET}${hour}.$job not posted to db_net."
      postmsg "$jlogfile" "$msg"
   fi

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

done

exit
