#####################################################################
echo "-----------------------------------------------------"
echo " exglobal_awc_halfdeg_gfs.sh.sms" 
echo " Nov 2003 - Gilbert - Produces Half degree GRIB files from master."
echo "                      for AWC."
echo " Apr 2005 - Cooke   - Now produces GRIB2 products instead of" 
echo "                      GRIB1"                    
echo " Sep 2007 - Cooke   - Uses copygb to get proper 0.5 data out of"
echo "                      new master gaussian grid."
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 pgrb 
#               generation.
############################################################


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 grib generation for fhr=$fhr"
    postmsg "$jlogfile" "$msg"

    ###############################
    # Put restart files into /nwges 
    # for backup to start Model Fcst
    ###############################

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

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

    ( $EXECUTIL/wgrib masterfile${fhr} | grep -F -f $paramlist | $EXECUTIL/wgrib -i -grib -o tmpfile${fhr} masterfile${fhr}
    $EXECUTIL/copygb -g4 -i0 -x tmpfile${fhr} pgbfile${fhr}
    $EXECUTIL/cnvgrib -p40 -g12 pgbfile${fhr} pgb2file${fhr}


    if test $SENDCOM = "YES"
    then
       cp pgb2file${fhr} $COMOUT/${RUN}.${cycle}.pgrb2f${fhr}.awc

       if test $SENDDBN = "YES"
       then
          $DBNROOT/bin/dbn_alert MODEL GFS_PGBAWC $job $COMOUT/${RUN}.${cycle}.pgrb2f$fhr.awc
       fi

       if test $fhr -lt 100
       then
          pad="0"
       else
          pad=""
       fi
       echo "$PDY$cyc$pad$fhr" > $COMOUT/${RUN}.t${cyc}z.control.awc.halfdeg
    fi
    rm tmpfile${fhr} pgbfile${fhr} pgb2file${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 #######################
