#!/bin/sh
######################################################################
#  UTILITY SCRIPT NAME :  exgfs_bull.sh.sm
#         DATE WRITTEN :  10/04/2004
#
#  Abstract:  This utility script produces the AWIPS BULLETINS
#
#     Input:  1 arguments are passed to this script.
#             1st argument - Forecast Hour - format of 2I
#
#####################################################################
echo "------------------------------------------------"
echo "JGFS_BULL 00/12 GFS postprocessing"
echo "------------------------------------------------"
echo "History: OCT 2004 - First implementation of this new script."
echo "History: OCT 2005 - Send bulletins to TOC via NTC.          "
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_bull.sh.sms  \$fcsthrs "
   echo ""
   exit 16
fi

cd $DATA
###############################################
# 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

if test $fcsthrs -eq 180
then

   set +x
   echo " "
   echo " ###############################################"
   echo "    Execute tracker to generate objective track"
   echo "    of tropical cyclones within GFS GRIB output"
   echo " ###############################################"
   echo " "
   set -x
   export cmodel=gfs
   export trkrscript=/nwprod/util/ush
   sh $trkrscript/extrkr.sh

fi

if test ${cycle} = 't00z' -o ${cycle} = 't12z'
then

   if test $fcsthrs -le 72 -a fcsthrs%6 -eq 0
   then
      set +x
      echo " "
      echo "############################################################"
      echo "           Create 65x65 grids and make GRIB index."
      echo "############################################################"
      echo " "
      set -x
      sh $USHutil/mkpost65.sh ${fcsthrs}
   fi

   if test ${cycle} = "t12z" -a $fcsthrs -eq 12
   then
      set +x
      echo " "
      echo "####################################################"
      echo " Process wmo 5-day mean bulletin at 12Z"
      echo "####################################################"
      echo " "
      set -x

      sh $USHutil/wmo5dayx.sh
   fi

   if test $fcsthrs -eq 72
   then
      set +x
      echo " "
      echo "############################################################"
      echo "   Making fsnt05tx and dfwindsx bulletin."
      echo "   This program is dependent on the 65x65 grid."
      echo "############################################################"
      echo " "
      set -x
      sh $USHutil/fsnt05tx.sh

      sh $USHutil/dfwindsx.sh

      set +x
      echo " "
      echo "#################################################"
      echo " fxus bulletins"
      echo "#################################################"
      echo " "
      set -x
      sh $USHutil/wrcorcx.sh

   fi

   if test $fcsthrs -eq 24
   then
      set +x
      echo " "
      echo "#################################################"
      echo " Process 06, 12, 18 and 24 fcsthrs WINTEM Bulletins. "
      echo "#################################################"
      echo " "
      set -x
      sh $utilities/mkwintem.sh
   fi

   case  $fcsthrs in
     24) hours="anl 12 24 00"
         bullparm=bulls_grdbgfs1.$cycle
         out=grdbgfs1.${job}
         ;;
     48) hours="36 48"
         bullparm=bull_grdbgfs2.d$cyc
         out=grdbuls.gfs48.${job}
         ;; 
     72) hours="36 48 72"
         bullparm=bull_grdbgfs2.d$cyc
         out=grdbuls.gfs48.${job}
         ;;
   esac
   
   if test $fcsthrs -eq 24
   then
       set +x
       echo " "
       echo "######################################"
       echo " Execute MKBULL   "
       echo "######################################"
       echo " "
       set -x
       sh $USHutil/mkbull_ntc.sh "$hours" $bullparm $out
   fi

   if test $fcsthrs -eq 48 -a "$cycle" = "t00z"
   then
      set +x
      echo " "
      echo "######################################"
      echo " Execute MKBULL   "
      echo "######################################"
      echo " "
      set -x

      sh $USHutil/mkbull_ntc.sh "$hours" $bullparm $out
   fi


   if test $fcsthrs -eq 72 -a "$cycle" = "t12z"
   then
      set +x
      echo " "
      echo "######################################"
      echo " Execute MKBULL   "
      echo "######################################"
      echo " "
      set -x

      sh $USHutil/mkbull_ntc.sh "$hours" $bullparm $out
   fi
fi

####################################################

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

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