#####################################################################
echo "-----------------------------------------------------"
echo " exglobal_pgrb_2p5.sh.sms" 
echo " Apr 2005 - Gilbert - Produces 2.5x2.5 degree pres GRIB 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 pgrb 
#               generation.
############################################################

export ENS_COM=/com/mrf/${envir}/ens.$PDY

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 1x1 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

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

    $EXECUTIL/wgrib masterfile | grep -F -f $paramlist | $EXECUTIL/wgrib -i -grib -o tmpfile masterfile
    #$EXECUTIL/copygb -g2 -i1,1 -x tmpfile pgbfile
    $EXECUTIL/copygb -g2 -i0 -x tmpfile pgbfile
    $EXECUTIL/grbindex pgbfile pgifile
    $EXECUTIL/cnvgrib -g12 -p31 pgbfile pgb2file
    $EXECUTIL/wgrib2 pgb2file -s > pgbi2file


    if test $SENDCOM = "YES"
    then
       if test $fhr -le 192
       then
          cp pgbfile $COMOUT/${RUN}.${cycle}.pgrbf${fhr}.2p5deg
          cp pgifile $COMOUT/${RUN}.${cycle}.pgrbif${fhr}.2p5deg

          if test `expr $fhr % 6` -eq 0
          then
          #############################
          # Convert to grib2 format
          #############################
          $EXECUTIL/cnvgrib -g12 -p40 pgbfile pgbfile.grib2
          $EXECUTIL/wgrib2 pgbfile.grib2 -s >pgbfile.grib2.idx

          cp pgbfile $ENS_COM/${RUN}.${cycle}.pgrbf${fhr}.2
          cp pgifile $ENS_COM/${RUN}.${cycle}.pgrbif${fhr}.2
          cp pgbfile.grib2 $ENS_COM/${RUN}.${cycle}.pgrbf${fhr}.2.grib2
          cp pgbfile.grib2.idx $ENS_COM/${RUN}.${cycle}.pgrbf${fhr}.2.grib2.idx
          fi

          if test $SENDDBN = "YES" -a ` expr $fhr % 12 ` -eq 0 -a $cyc -eq 12
          then
             $DBNROOT/bin/dbn_alert MODEL ENS_PGB_GFS $job $ENS_COM/${RUN}.${cycle}.pgrbf$fhr.2
             $DBNROOT/bin/dbn_alert MODEL ENS_PGBI_GFS $job  $ENS_COM/${RUN}.${cycle}.pgrbif$fhr.2

             if [ $SENDDBN_GB2 = YES ]
             then

             $DBNROOT/bin/dbn_alert MODEL ENS_PGB_GFS_GB2 $job $ENS_COM/${RUN}.${cycle}.pgrbf$fhr.2.grib2
             $DBNROOT/bin/dbn_alert MODEL ENS_PGB_GFS_GB2_WIDX $job $ENS_COM/${RUN}.${cycle}.pgrbf$fhr.2.grib2.idx
           
             fi

          fi
       else
          cp pgbfile $COMOUT/${RUN}.${cycle}.pgrbf${fhr}
          cp pgifile $COMOUT/${RUN}.${cycle}.pgrbif${fhr}
          if test $SENDDBN = "YES"
          then
             $DBNROOT/bin/dbn_alert MODEL GFS_PGB2P5 $job $COMOUT/${RUN}.${cycle}.pgrbf$fhr
             $DBNROOT/bin/dbn_alert MODEL GFS_PGBI2P5 $job $COMOUT/${RUN}.${cycle}.pgrbif$fhr
          fi
       fi

       cp pgb2file $COMOUT/${RUN}.${cycle}.pgrb2f${fhr}.2p5deg
       cp pgbi2file $COMOUT/${RUN}.${cycle}.pgrb2f${fhr}.2p5deg.idx
       
       if test $SENDDBN = "YES"
       then
          $DBNROOT/bin/dbn_alert MODEL GFS_PGB2_2P5 $job $COMOUT/${RUN}.${cycle}.pgrb2f${fhr}.2p5deg
          $DBNROOT/bin/dbn_alert MODEL GFS_PGB2_2P5_WIDX $job $COMOUT/${RUN}.${cycle}.pgrb2f${fhr}.2p5deg.idx
       fi

       # add for the event release for preprep job of HR-QPF
       if [ $fhr -eq 216 ]; then
         if [ $SENDSMS = YES ]; then
           $SMSBIN/setev pgrb_${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.pgrb.2p5deg
    fi
    rm pgbfile pgifile tmpfile pgb2file pgbfile.grib2 pgbfile.grib2.idx

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

#cat $pgmout

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

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

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