#####################################################################
echo "-----------------------------------------------------"
echo " exglobal_1dgrib_special.sh.sms" 
echo " Jan 2008 - Chuang - Produces 1x1 degree special Grib 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.
############################################################

############################################################
# NO processing Analysis special Files 
############################################################

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

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

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

    $EXECUTIL/copygb -g3 -i1,1 -x masterfile pgbfile
# creating higher resolution goes files for US centers    
    $EXECUTIL/copygb -g221 -i1,1 -x masterfile pgbfile2 
    $EXECUTIL/grbindex pgbfile pgifile
    $EXECUTIL/grbindex pgbfile2 pgifile2
    $EXECUTIL/cnvgrib -g12 -p40 pgbfile pgb2file
    $EXECUTIL/cnvgrib -g12 -p40 pgbfile2 pgb2file2
    $EXECUTIL/wgrib2 pgb2file -s > pgb2ifile


    if test $SENDCOM = "YES"
    then
       cp pgbfile $COMOUT/${RUN}.${cycle}.goessimpgrbf${fhr}
       cp pgifile $COMOUT/${RUN}.${cycle}.goessimpgrbif${fhr}
       cp pgb2file $COMOUT/${RUN}.${cycle}.goessimpgrb2f${fhr}.1p0deg
       cp pgb2ifile $COMOUT/${RUN}.${cycle}.goessimpgrb2f${fhr}.1p0deg.idx
       cp pgbfile2 $COMOUT/${RUN}.${cycle}.goessimpgrb221f${fhr}
       cp pgifile2 $COMOUT/${RUN}.${cycle}.goessimpgrb221if${fhr}
       cp pgb2file2 $COMOUT/${RUN}.${cycle}.goessimpgrb2f${fhr}.grd221

       if test $SENDDBN = "YES"
       then
          $DBNROOT/bin/dbn_alert MODEL GFS_GOESSIMPGB $job $COMOUT/${RUN}.${cycle}.goessimpgrbf$fhr
          $DBNROOT/bin/dbn_alert MODEL GFS_GOESSIMPGBI $job $COMOUT/${RUN}.${cycle}.goessimpgrbif$fhr
          $DBNROOT/bin/dbn_alert MODEL GFS_GOESSIMPGB2_1P0 $job $COMOUT/${RUN}.${cycle}.goessimpgrb2f${fhr}.1p0deg
          $DBNROOT/bin/dbn_alert MODEL GFS_GOESSIMPGB2_1P0_WIDX $job $COMOUT/${RUN}.${cycle}.goessimpgrb2f${fhr}.1p0deg.idx
          $DBNROOT/bin/dbn_alert MODEL GFS_GOESSIM221_PGB2 $job $COMOUT/${RUN}.${cycle}.goessimpgrb221f$fhr
          $DBNROOT/bin/dbn_alert MODEL GFS_GOESSIM221I_PGB2 $job $COMOUT/${RUN}.${cycle}.goessimpgrb221if$fhr
          $DBNROOT/bin/dbn_alert MODEL GFS_GOESSIMGRD221_PGB2 $job $COMOUT/${RUN}.${cycle}.goessimpgrb2f${fhr}.grd221
       fi

       if test $fhr -lt 100
       then
          pad="0"
       else
          pad=""
       fi
       echo "$PDY$cyc$pad$fhr" > $COMOUT/${RUN}.t${cyc}z.control.goessimpgrb
    fi
    rm pgbfile pgifile pgb2file pgbfile2 pgifile2 pgb2file2

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

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