#####################################################################
echo "-----------------------------------------------------"
echo " exglobal_pgrb2_gfs.sh.sms" 
echo " Nov 2003 - Gilbert - Produces half degree GRIB2 files from master."
echo "-----------------------------------------------------"
#####################################################################

set -x

cd $DATA

msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

############################################################
#  Define Variables:
#  -----------------
#  SHOUR        is the starting forecast hour. normally 0 except for restarts.
#  FHOUR        is the ending forecast hour.
#  FHINC        is the increment hour for each forecast steps.
#  FH           is the current forecast hour.
#  SLEEP_TIME   is the number of seconds to sleep before exiting with error.
#  SLEEP_INT    is the number of seconds to sleep between restrt file checks.
#  restart_file is the name of the file to key off of to kick off pgrb2
#               generation.
############################################################

############################################################
# Process Analysis Files before starting the Forecast Files
############################################################
if test -f $COMIN/${RUN}.t${cyc}z.master.grbianl -a ${SHOUR} -eq 0
then
   ( cp $COMIN/${RUN}.t${cyc}z.master.grbanl masterfile_anl

   $EXECUTIL/wgrib masterfile_anl | grep -F -f $FIXGLOBAL/global_1x1_paramlist.anl | $EXECUTIL/wgrib -i -grib -o tmpfile1_anl masterfile_anl
   $EXECUTIL/copygb -g4 -i0 -x tmpfile1_anl tmpfile2_anl
   $EXECUTIL/cnvgrib -g12 -p40 tmpfile2_anl pgbfile_anl
   $EXECUTIL/wgrib2 -s pgbfile_anl > pgifile_anl

   if test "$SENDCOM" = 'YES'
   then
      #
      # Save Pressure GRIB/Index files
      #
      mv pgbfile_anl $COMOUT/${RUN}.${cycle}.pgrb2anl
      mv pgifile_anl $COMOUT/${RUN}.${cycle}.pgrb2anl.idx

      if test "$SENDDBN" = 'YES'
      then
         $DBNROOT/bin/dbn_alert MODEL GFS_PGB2 $job $COMOUT/${RUN}.${cycle}.pgrb2anl
	 $DBNROOT/bin/dbn_alert MODEL GFS_PGB2_WIDX $job $COMOUT/${RUN}.${cycle}.pgrb2anl.idx
      fi

      GRIB_LIST="UGRD:10.m.a|VGRD:10.m.a"
      $EXECUTIL/wgrib -s tmpfile2_anl | egrep "$GRIB_LIST" | $EXECUTIL/wgrib tmpfile2_anl -s -i -grib -o $COMOUT/${RUN}.${cycle}.master.grbanl.10m.uv
      $EXECUTIL/cnvgrib -g12 -p40 $COMOUT/${RUN}.${cycle}.master.grbanl.10m.uv \
          $COMOUT/${RUN}.${cycle}.master.grbanl.10m.uv.grib2
      $EXECUTIL/wgrib2 $COMOUT/${RUN}.${cycle}.master.grbanl.10m.uv.grib2 -s > \
          $COMOUT/${RUN}.${cycle}.master.grbanl.10m.uv.grib2.idx

      if test "$SENDDBN" = 'YES'
      then
         $DBNROOT/bin/dbn_alert MODEL GFS_PGB_USCG $job $COMOUT/${RUN}.${cycle}.master.grbanl.10m.uv
         $DBNROOT/bin/dbn_alert MODEL GFS_PGB_USCG_GB2 $job $COMOUT/${RUN}.${cycle}.master.grbanl.10m.uv.grib2
         $DBNROOT/bin/dbn_alert MODEL GFS_PGB_USCG_GB2_IDX $job $COMOUT/${RUN}.${cycle}.master.grbanl.10m.uv.grib2.idx
      fi
   fi
   rm pgbfile_anl pgifile_anl tmpfile1_anl tmpfile2_anl ) &

fi


SLEEP_LOOP_MAX=`expr $SLEEP_TIME / $SLEEP_INT`

export fhr=$SHOUR
if test $fhr -lt 10 -a $fhr -gt 0
then
   export fhr="0$fhr"
fi
############################################################
# Loop Through the Post Forecast Files 
############################################################
while test $fhr -le $FHOUR
do

    ###############################
    # Start Looping for the 
    # existence of the restart files
    ###############################
    set +x
    export pgm="postcheck"
    ic=1
    while [ $ic -le $SLEEP_LOOP_MAX ]
    do
       if test -f $restart_file$fhr
       then
          break
       else
          ic=`expr $ic + 1`
          sleep $SLEEP_INT
       fi
       ###############################
       # If we reach this point assume
       # fcst job never reached restart 
       # period and error exit
       ###############################
       if [ $ic -eq $SLEEP_LOOP_MAX ]
       then
          export err=9
          err_chk
       fi
    done
    set -x

    msg="Starting half degree GRIB2 generation for fhr=$fhr"
    postmsg "$jlogfile" "$msg"


    ( cp $COMIN/${RUN}.t${cyc}z.master.grbf$fhr masterfile_$fhr

    if test $fhr -eq 0
    then
       paramlist=$FIXGLOBAL/global_1x1_paramlist.f00
    else
       paramlist=$FIXGLOBAL/global_1x1_paramlist
    fi

   
    $EXECUTIL/wgrib masterfile_$fhr | grep -F -f $paramlist | $EXECUTIL/wgrib -i -grib -o tmpfile1_$fhr masterfile_$fhr
    $EXECUTIL/copygb -g4 -i0 -x tmpfile1_$fhr tmpfile2_$fhr
    $EXECUTIL/cnvgrib -g12 -p40 tmpfile2_$fhr pgbfile_$fhr
    $EXECUTIL/wgrib2 -s pgbfile_$fhr > pgifile_$fhr


    if test $SENDCOM = "YES"
    then
       mv pgbfile_$fhr $COMOUT/${RUN}.${cycle}.pgrb2f$fhr
       mv pgifile_$fhr $COMOUT/${RUN}.${cycle}.pgrb2f$fhr.idx

       if test "$SENDDBN" = 'YES'
       then
          $DBNROOT/bin/dbn_alert MODEL GFS_PGB2 $job $COMOUT/${RUN}.${cycle}.pgrb2f${fhr}
          $DBNROOT/bin/dbn_alert MODEL GFS_PGB2_WIDX $job $COMOUT/${RUN}.${cycle}.pgrb2f${fhr}.idx
       fi

      GRIB_LIST="UGRD:10.m.a|VGRD:10.m.a"
      $EXECUTIL/wgrib -s tmpfile2_$fhr | egrep "$GRIB_LIST" | $EXECUTIL/wgrib tmpfile2_$fhr -s -i -grib -o $COMOUT/${RUN}.${cycle}.master.grbf${fhr}.10m.uv
      $EXECUTIL/cnvgrib -g12 -p40 $COMOUT/${RUN}.${cycle}.master.grbf${fhr}.10m.uv \
          $COMOUT/${RUN}.${cycle}.master.grbf${fhr}.10m.uv.grib2
      $EXECUTIL/wgrib2 $COMOUT/${RUN}.${cycle}.master.grbf${fhr}.10m.uv.grib2 -s > \
          $COMOUT/${RUN}.${cycle}.master.grbf${fhr}.10m.uv.grib2.idx

      if test "$SENDDBN" = 'YES'
      then
         $DBNROOT/bin/dbn_alert MODEL GFS_PGB_USCG $job $COMOUT/${RUN}.${cycle}.master.grbf${fhr}.10m.uv
         $DBNROOT/bin/dbn_alert MODEL GFS_PGB_USCG_GB2 $job $COMOUT/${RUN}.${cycle}.master.grbf${fhr}.10m.uv.grib2
         $DBNROOT/bin/dbn_alert MODEL GFS_PGB_USCG_GB2_IDX $job $COMOUT/${RUN}.${cycle}.master.grbf${fhr}.10m.uv.grib2.idx
      fi
       if [ $fhr -eq 96 ]; then
         if [ $SENDSMS = YES ]; then
           $SMSBIN/setev pgrb2_${fhr}_ready
         fi
       fi

       if test $fhr -lt 100
       then
          pad="0"
       else
          pad=""
       fi
       echo "$PDY$cyc$pad$fhr" > $COMOUT/${RUN}.t${cyc}z.control.pgrb2
    fi
    rm pgbfile_$fhr pgifile_$fhr tmpfile1_$fhr tmpfile2_$fhr ) &

    export fhr=`expr $fhr + $FHINC`
    if test $fhr -lt 10
    then
       export fhr="0$fhr"
    fi
done

wait
cat $pgmout

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

msg='ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

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