#####################################################################
echo "-----------------------------------------------------"
echo " exglobal_pgrb_gfs_b.sh.sms" 
echo " Produces the half degree GRIB2 that when added to the" 
echo " PGRB files will make the old version master files."
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
############################################################
( cp $COMIN/${RUN}.t${cyc}z.master.grbanl masterfile_anl

  $EXECUTIL/wgrib masterfile_anl | grep -F -f $FIXGLOBAL/global_master-catchup_parmlist | $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}.pgrb2banl
    mv pgifile_anl $COMOUT/${RUN}.${cycle}.pgrb2banl.idx

    if test "$SENDDBN" = 'YES'
    then
      $DBNROOT/bin/dbn_alert MODEL GFS_PGB2B $job $COMOUT/${RUN}.${cycle}.pgrb2banl
      $DBNROOT/bin/dbn_alert MODEL GFS_PGB2B_WIDX $job $COMOUT/${RUN}.${cycle}.pgrb2banl.idx
    fi

  fi
  rm pgbfile_anl pgifile_anl tmpfile1_anl tmpfile2_anl ) &

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 for hours 0-180
# to create the 0.5 degree pgrbb 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

    $EXECUTIL/wgrib masterfile_$fhr | grep -F -f $FIXGLOBAL/global_master-catchup_parmlist | $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}.pgrb2bf${fhr}
       mv pgifile_$fhr $COMOUT/${RUN}.${cycle}.pgrb2bf${fhr}.idx

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

       if test $fhr -lt 100
       then
          pad="0"
       else
          pad=""
       fi
    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

############################################################
# Loop Through the Post Forecast Files a second time to
# create the full 1.0 degree master look alike files for 
# hours 192-384
############################################################
#BSM fhr=192
fhr=204
FHINC=12
FHOUR=$FHOUR2

cat $FIXGLOBAL/global_master-catchup_parmlist $FIXGLOBAL/global_1x1_paramlist > $DATA/parmlist

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


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

    $EXECUTIL/wgrib masterfile_$fhr | grep -F -f $DATA/parmlist | $EXECUTIL/wgrib -i -grib -o tmpfile1_$fhr masterfile_$fhr
    $EXECUTIL/copygb -g3 -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}.pgrb2bf${fhr}
       mv pgifile_$fhr $COMOUT/${RUN}.${cycle}.pgrb2bf${fhr}.idx

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

    fi
    rm pgbfile_$fhr pgifile_$fhr tmpfile1_$fhr tmpfile2_$fhr ) &

    export fhr=`expr $fhr + $FHINC`
done


wait
cat $pgmout

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

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

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