#  UTILITY SCRIPT NAME :  mkpost65.sh
#               AUTHOR :  David Michaud
#         DATE WRITTEN :  8/5/99
#
#  Abstract:  This utility script produces the 65x65 grids for MOS input. 
#
#     Input:  1 argument is passed to this script.   
#             1st argument - Forecast Hour - format of 2I
#
echo "History: AUG     1999 - Created for implementation on IBM SP"
echo "                      - Allows users to run interactively" 
#

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

if test $num -ge 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 job=${job:-interactive} 
   export pgmout=${pgmout:-OUTPUT.$$}
else
   echo ""
   echo "Usage: mkpost65.sh \$hour"
   echo ""
   exit 16
fi

set +x
echo " ------------------------------------------"
echo " BEGIN MAKING ${NET} 65x65 GRIDS"  
echo " ------------------------------------------"
set -x

msg="Enter Make POST65 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=post65
   . prep_step

   export XLFUNIT_11="pgrbf${hour}"
   export XLFUNIT_31="pgrbif${hour}"
   export XLFUNIT_51="${RUN}.${cycle}.grb65f${hour}"
   export XLFUNIT_52="sp1"

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

   $EXECutil/grbindex ${RUN}.${cycle}.grb65f${hour} ${RUN}.${cycle}.grb65if${hour}

   ##############################
   # Post Files to COM 
   ##############################

   if test "$SENDCOM" = 'YES'
   then
      cp ${RUN}.${cycle}.grb65f${hour} $COMOUT/${RUN}.${cycle}.grb65f${hour}
      cp ${RUN}.${cycle}.grb65if${hour} $COMOUT/${RUN}.${cycle}.grb65if${hour}
   fi

   msg="Post65 Processing $hour hour completed normally"
   postmsg "$jlogfile" "$msg"

done

exit
