#!/bin/sh
######################################################################
#  UTILITY SCRIPT NAME :  exgfs_grib_wafs.sh.sm
#         DATE WRITTEN :  10/04/2004
#
#  Abstract:  This utility script produces the  WAFS GRIB
#
#     Input:  1 arguments are passed to this script.
#             1st argument - Forecast Hour - format of 2I
#
#     Logic:   If we are processing fcsthrss 12-30, we have the
#              added variable of the a or b in the process accordingly.
#              The other fcsthrss, the a or b  is dropped.
#     Modified:  3/13/13 - C. Caruso Magee - comment out call to
#                          mkwafcgrd.sh (no longer needed).
#
#####################################################################
echo "------------------------------------------------"
echo "JWAFS_00/06/12/18 GFS postprocessing"
echo "------------------------------------------------"
echo "History: OCT 2004 - First implementation of this new script."
echo " "
#####################################################################
set +x
fcsthrs_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 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_wafs.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 WAFS PRODUCTS"
echo " ------------------------------------------"

####################################################
#
#    GFS WAFS PRODUCTS MUST RUN IN CERTAIN ORDER
#    BY REQUIREMENT FROM FAA.
#    PLEASE DO NOT ALTER ORDER OF PROCESSING WAFS
#    PRODUCTS CONSULTING WITH MR. BRENT GORDON.
#
####################################################

set +x
echo " "
echo "#####################################"
echo " Process GRIB WAFS PRODUCTS (mkwafs)"
echo " FORECAST HOURS 00 - 72."
echo "#####################################"
echo " "
set -x

if test $fcsthrs -eq 0
then
    echo "  "
fi

#    If we are processing fcsthrss 12-30, we have the
#    added variable of the a  or b in the process.
#    The other fcsthrss, the a or b  is dropped.

if test $fcsthrs -ge 12 -a $fcsthrs -le 24
then
    sh $USHutil/mkwfsgbl.sh ${fcsthrs} a
fi

if test $fcsthrs -eq 30
then
    sh $USHutil/mkwfsgbl.sh ${fcsthrs} a
    for fcsthrs in 12 18 24 30
    do
       sh $USHutil/mkwfsgbl.sh ${fcsthrs} b
    done
    sh $USHutil/mkwfsgbl.sh 00 x
    sh $USHutil/mkwfsgbl.sh 06 x
fi

if test $fcsthrs -gt 30 -a $fcsthrs -le 48
then
    sh $USHutil/mkwfsgbl.sh ${fcsthrs} x
fi

if test $fcsthrs -eq 60 -o $fcsthrs -eq 72
then
    sh $USHutil/mkwfsgbl.sh ${fcsthrs} x
fi

if test $fcsthrs -le 120
then
   set +x
   echo " "
   echo "####################################################"
   echo " Process GFS WAFS GRIB PRODUCTS FOR EIGHT OCTANTS  "
   echo " FOR FORECAST HOURS 00 - 120 AT 00Z, 06Z, 12Z AND 18Z."
   echo " THESE GRID FILES SEND TO TOC."
   echo "####################################################"
   echo " "
   set -x
   export parmlist=$PARMutil/grib_wafsgfs_intdsk
   if test $fcsthrs -eq 00
   then
      export parmlist=$PARMutil/grib_wafsgfs_intdskf00
      sh $USHutil/wafs_intdsk.sh  "00"
   elif test $fcsthrs -eq 36
   then
      sh $USHutil/wafs_intdsk.sh  "36"
      export parmlist=$PARMutil/grib_wafsgfs_intdskf00
      sh $USHutil/wafs_intdsk.sh  "03 09 15 21 27 33"
   elif test $fcsthrs -eq 120
   then 
      sh $USHutil/wafs_intdsk.sh  "54 66 78 90 102 114 120"
   else
      sh $USHutil/wafs_intdsk.sh  ${fcsthrs}
   fi
fi

#if test ${cycle} = 't00z' -o ${cycle} = 't12z'
#then
#   if test ${fcsthrs} -eq 12 -o ${fcsthrs} -eq 24 -o ${fcsthrs} -eq 36
#   then
#      set +x
#      echo " "
#      echo "######################################"
#      echo " Process WAFC GRID products."
#      echo "######################################"
#      echo " "
#      set -x
#      sh $USHutil/mkwafcgrd.sh ${fcsthrs}
#   fi
#fi

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

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

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