#!/bin/sh
######################################################################
#  UTILITY SCRIPT NAME :  exgfs_grib_fos.sh.sm
#         DATE WRITTEN :  10/04/2004
#
#  Abstract:  This utility script produces the  FOS GRIB
#
#     Input:  1 arguments are passed to this script.
#             1st argument - Forecast Hour - format of 2I
#
#####################################################################
echo "------------------------------------------------"
echo "JFOS_00/06/12/18 GFS postprocessing"
echo "------------------------------------------------"
echo "History: OCT 2004 - First implementation of this new script."
echo " "
#####################################################################
set +x
fcsthrs="$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 DBNALERT_TYPE=${DBNALERT_TYPE:-GRIB}
   export NET=${NET:-gfs}
   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: exgfs_grib_fos.sh.sms  \$fcsthrs "
   echo ""
   exit 16
fi

cd $DATA

set -x
###############################################
# Wait for the availability of the pgrib file
###############################################
icnt=1
while [ $icnt -lt 1000 ]
do
  if [ -s $COMIN/${RUN}.${cycle}.pgrbf$fcsthrs ]
  then
     break
  fi

  sleep 10
  icnt=$((icnt + 1))
  if [ $icnt -ge 180 ]
  then
     msg="ABORTING after 30 min of waiting for the pgrib filei!"
     err_exit $msg
  fi
done

########################################
msg="HAS BEGUN!"
postmsg "$jlogfile" "$msg"
########################################

echo " ------------------------------------------"
echo " BEGIN MAKING GFS FOS PRODUCTS"
echo " ------------------------------------------"

if test ${cycle} = 't00z' -o ${cycle} = 't12z'
then
   if test $fcsthrs -le 72 -a fcsthrs%6 -eq 0
   then
      set +x
      echo " "
      echo "####################################################"
      echo " Process GFS International GRIB PRODUCTS "
      echo "####################################################"
      echo " "
      set -x
      sh $USHutil/mkfosspc.sh ${fcsthrs}
   fi
fi

##############################################################################
# GOOD RUN
set +x
echo "**************JOB EXGFS_GRIB_FOS.SH.SMS COMPLETED NORMALLY ON THE IBM"
echo "**************JOB EXGFS_GRIB_FOS.SH.SMS COMPLETED NORMALLY ON THE IBM"
echo "**************JOB EXGFS_GRIB_FOS.SH.SMS COMPLETED NORMALLY ON THE IBM"
set -x
##############################################################################

msg="HAS COMPLETED NORMALLY!"
postmsg "$jlogfile" "$msg"

############## END OF SCRIPT #######################
