#####################################################################
echo "-----------------------------------------------------"
echo " exglobal_pgrb_gfs.sh.sms" 
echo " Apr 2005 - Gilbert - Produces 1x1 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.
############################################################

############################################################
# 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 tmpfile_anl masterfile_anl
   #$EXECUTIL/copygb -g3 -i1,1 -x tmpfile_anl pgbfile_anl
   $EXECUTIL/copygb -g3 -i0 -x tmpfile_anl pgbfile_anl
   $EXECUTIL/grbindex pgbfile_anl pgifile_anl
   $EXECUTIL/cnvgrib -g12 -p40 pgbfile_anl pgb2file_anl
   $EXECUTIL/wgrib2 pgb2file_anl -s > pgbi2file_anl

   if test "$SENDCOM" = 'YES'
   then
      #
      # Save Pressure GRIB/Index files
      #
      cp pgbfile_anl $COMOUT/${RUN}.${cycle}.pgrbanl
      cp pgifile_anl $COMOUT/${RUN}.${cycle}.pgrbianl
      cp pgb2file_anl $COMOUT/${RUN}.${cycle}.pgrb2anl.1p0deg
      cp pgbi2file_anl $COMOUT/${RUN}.${cycle}.pgrb2anl.1p0deg.idx

      if test "$SENDDBN" = 'YES'
      then
         $DBNROOT/bin/dbn_alert MODEL GFS_PGB $job $COMOUT/${RUN}.${cycle}.pgrbanl
         $DBNROOT/bin/dbn_alert MODEL GFS_PGBI $job $COMOUT/${RUN}.${cycle}.pgrbianl
         $DBNROOT/bin/dbn_alert MODEL GFS_PGB2_1P0 $job $COMOUT/${RUN}.${cycle}.pgrb2anl.1p0deg
         $DBNROOT/bin/dbn_alert MODEL GFS_PGB2_1P0_WIDX $job $COMOUT/${RUN}.${cycle}.pgrb2anl.1p0deg.idx
      fi
   fi
   rm pgbfile_anl pgifile_anl tmpfile_anl pgb2file_anl
   
   if test "$SENDSMS" = 'YES'
   then
      $SMSBIN/setev pgrb_anl_ready
   fi ) &
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 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_$fhr

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

#SEC added by wx11sc for P6 testing, to see if copy was successful
    comfilesize=`ls -lrt $COMIN/${RUN}.t${cyc}z.master.grbf$fhr | awk '{print $5}'`
    dirfilesize=`ls -lrt masterfile_$fhr | awk '{print $5}'`
    if [ $comfilesize -ne $dirfilesize ]; then
       err_exit "Input file size does not match copy in working directory"
    fi


    $EXECUTIL/wgrib masterfile_$fhr | grep -F -f $paramlist | $EXECUTIL/wgrib -i -grib -o tmpfile_$fhr masterfile_$fhr
    #$EXECUTIL/copygb -g3 -i1,1 -x tmpfile_$fhr pgbfile_$fhr
    $EXECUTIL/copygb -g3 -i0 -x tmpfile_$fhr pgbfile_$fhr
    $EXECUTIL/grbindex pgbfile_$fhr pgifile_$fhr
    $EXECUTIL/cnvgrib -g12 -p40 pgbfile_$fhr pgb2file_$fhr
    $EXECUTIL/wgrib2 pgb2file_$fhr -s > pgbi2file_$fhr


    if test $SENDCOM = "YES"
    then
       cp pgbfile_$fhr $COMOUT/${RUN}.${cycle}.pgrbf${fhr}
       cp pgifile_$fhr $COMOUT/${RUN}.${cycle}.pgrbif${fhr}
       cp pgb2file_$fhr $COMOUT/${RUN}.${cycle}.pgrb2f${fhr}.1p0deg
       cp pgbi2file_$fhr $COMOUT/${RUN}.${cycle}.pgrb2f${fhr}.1p0deg.idx

       if test $SENDDBN = "YES"
       then
          $DBNROOT/bin/dbn_alert MODEL GFS_PGB $job $COMOUT/${RUN}.${cycle}.pgrbf$fhr
          $DBNROOT/bin/dbn_alert MODEL GFS_PGBI $job $COMOUT/${RUN}.${cycle}.pgrbif$fhr
          $DBNROOT/bin/dbn_alert MODEL GFS_PGB2_1P0 $job $COMOUT/${RUN}.${cycle}.pgrb2f${fhr}.1p0deg
          $DBNROOT/bin/dbn_alert MODEL GFS_PGB2_1P0_WIDX $job $COMOUT/${RUN}.${cycle}.pgrb2f${fhr}.1p0deg.idx
       fi

       if test $fhr -lt 100
       then
          pad="0"
       else
          pad=""
       fi
       echo "$PDY$cyc$pad$fhr" > $COMOUT/${RUN}.t${cyc}z.control.pgrb
    fi
    rm pgbfile_$fhr pgifile_$fhr tmpfile_$fhr pgb2file_$fhr

    if test "$SENDSMS" = 'YES'
    then
       export fhour=`expr ${fhr} % 6 `
       if test ${fhour} -eq 0
       then
          $SMSBIN/setev pgrb_${fhr}_ready
       fi
    fi ) &

    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 #######################
