#####################################################################
echo "-----------------------------------------------------"
echo " exglobal_sgrb2_gfs.sh.sms" 
echo " Jan 2005 - Gilbert - Produces half degree GRIB2 fields on sigma "
echo "                      levels on a gaussian grid from sigma files "
echo "                      for AFWA."
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 
#               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 Model Sigma 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 Sigma GRIB2 generation for fhr=$fhr"
    postmsg "$jlogfile" "$msg"


    cp $COMIN/${RUN}.t${cyc}z.sf$fhr sigma

   
    $EXECUTIL/ss2g2 sigma sgrb2file 4 768 384
    #$EXECUTIL/grb2index sgrb2file sgrb2ifile


    if test $SENDCOM = "YES"
    then
       cp sgrb2file $COMOUT/${RUN}.${cycle}.sgrb2f$fhr
       #cp sgrb2ifile $COMOUT/${RUN}.${cycle}.sgrb2if$fhr

       if test "$SENDDBN" = 'YES'
       then
          $DBNROOT/bin/dbn_alert MODEL GFS_SGB2 $job $COMOUT/${RUN}.${cycle}.sgrb2f${fhr}
       fi

       if test $fhr -lt 100
       then
          pad="0"
       else
          pad=""
       fi
       echo "$PDY$cyc$pad$fhr" > $COMOUT/${RUN}.t${cyc}z.control.sgrb2
    fi
    rm sigma sgrb2file

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