#####################################################################
echo "-----------------------------------------------------"
echo " exgefs_post_pgrb2.sh.sms" 
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.
############################################################

SLEEP_LOOP_MAX=`expr $SLEEP_TIME / $SLEEP_INT`

############################################################
# Process Analysis Files before starting the Forecast Files
############################################################
export pgm="postcheck"
ic=1
while [ $ic -le $SLEEP_LOOP_MAX ]
do
   if test -f $COMIN/$cyc/pgrbb/${RUN}.${cycle}.pgrbbianl$cfsuffix -a ${SHOUR} -eq 0
   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 GRIB2 generation for fhr=$fhr"
postmsg "$jlogfile" "$msg"

FILEA=$COMIN/$cyc/pgrba/${RUN}.${cycle}.pgrbaanl$cfsuffix
FILEB=$COMIN/$cyc/pgrbb/${RUN}.${cycle}.pgrbbanl$cfsuffix

$EXECUTIL/cnvgrib -g12 -p40 $FILEA pgb2afile
$EXECUTIL/cnvgrib -g12 -p40 $FILEB pgb2bfile

if test "$SENDCOM" = 'YES'
then
   #
   # Save Pressure GRIB/Index files
   #
   cp pgb2afile $COMOUT/$cyc/pgrb2a/${RUN}.${cycle}.pgrb2aanl$cfsuffix
   cp pgb2bfile $COMOUT/$cyc/pgrb2b/${RUN}.${cycle}.pgrb2banl$cfsuffix

   if test "$SENDDBN" = 'YES'
   then
      if test "$NET" = 'gens'
      then
	 if test `echo $RUN | cut -c1-2` = "ge" -a ! -n "$cfsuffix"
	 then
	    MEMBER=`echo $RUN | cut -c3-5 | tr '[a-z]' '[A-Z]'`
	    $DBNROOT/bin/dbn_alert MODEL ENS_PGB2A_$MEMBER $job $COMOUT/$cyc/pgrb2a/${RUN}.${cycle}.pgrb2aanl$cfsuffix
	 fi

	 if test `echo $RUN | cut -c1-2` = "ge" -a ! -n "$cfsuffix"
	 then
	    MEMBER=`echo $RUN | cut -c3-5 | tr '[a-z]' '[A-Z]'`
	    $DBNROOT/bin/dbn_alert MODEL ENS_PGB2B_$MEMBER $job $COMOUT/$cyc/pgrb2b/${RUN}.${cycle}.pgrb2banl$cfsuffix
	 fi
         
         if test "$CREATE_TIGGE" = 'YES'
         then
            $EXECUTIL/wgrib $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbbanl$cfsuffix |grep -F -f ${FIXGLOBAL}/gefs_tigge_params | $EXECUTIL/wgrib -i -grib -o pgbcfile $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbbanl$cfsuffix
            $EXECUTIL/cnvgrib -g12 -p40 pgbcfile $COMOUT/$cyc/pgrb2c/${RUN}.${cycle}.pgrb2canl$cfsuffix  
            MEMBER=`echo $RUN | cut -c3-5 | tr '[a-z]' '[A-Z]'`
            $DBNROOT/bin/dbn_alert MODEL ENS_PGB2C_$MEMBER $job $COMOUT/$cyc/pgrb2c/${RUN}.${cycle}.pgrb2canl$cfsuffix
         fi
      fi 
   fi
fi

rm pgb2afile pgb2bfile pgbcfile

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

   FILEA=$COMIN/$cyc/pgrba/${RUN}.${cycle}.pgrbaf${fhr}$cfsuffix
   FILEB=$COMIN/$cyc/pgrbb/${RUN}.${cycle}.pgrbbf${fhr}$cfsuffix
   FILEALR=$COMIN/$cyc/pgrbalr/${RUN}.${cycle}.pgrbaf${fhr}.2$cfsuffix
   FILEBLR=$COMIN/$cyc/pgrbblr/${RUN}.${cycle}.pgrbbf${fhr}.2$cfsuffix

   $EXECUTIL/cnvgrib -g12 -p40 $FILEA pgb2afile
   $EXECUTIL/cnvgrib -g12 -p40 $FILEB pgb2bfile
   $EXECUTIL/cnvgrib -g12 -p40 $FILEALR pgb2afile.2
   $EXECUTIL/cnvgrib -g12 -p40 $FILEBLR pgb2bfile.2

   if test "$SENDCOM" = 'YES'
   then
      #
      # Save Pressure GRIB/Index files
      #
      cp pgb2afile $COMOUT/$cyc/pgrb2a/${RUN}.${cycle}.pgrb2af${fhr}$cfsuffix
      cp pgb2bfile $COMOUT/$cyc/pgrb2b/${RUN}.${cycle}.pgrb2bf${fhr}$cfsuffix
      cp pgb2afile.2 $COMOUT/$cyc/pgrb2alr/${RUN}.${cycle}.pgrb2af${fhr}.2$cfsuffix
      cp pgb2bfile.2 $COMOUT/$cyc/pgrb2blr/${RUN}.${cycle}.pgrb2bf${fhr}.2$cfsuffix

      if test "$SENDDBN" = 'YES'
      then
         if test "$NET" = 'gens'
         then
            if test `echo $RUN | cut -c1-2` = "ge" -a ! -n "$cfsuffix"
            then
               MEMBER=`echo $RUN | cut -c3-5 | tr '[a-z]' '[A-Z]'`
               $DBNROOT/bin/dbn_alert MODEL ENS_PGB2A_$MEMBER $job $COMOUT/$cyc/pgrb2a/${RUN}.${cycle}.pgrb2af${fhr}$cfsuffix
               $DBNROOT/bin/dbn_alert MODEL ENS_PGB2A2_$MEMBER $job $COMOUT/$cyc/pgrb2alr/${RUN}.${cycle}.pgrb2af${fhr}.2$cfsuffix
            fi

            if test `echo $RUN | cut -c1-2` = "ge" -a ! -n "$cfsuffix"
            then
               MEMBER=`echo $RUN | cut -c3-5 | tr '[a-z]' '[A-Z]'`
               $DBNROOT/bin/dbn_alert MODEL ENS_PGB2B_$MEMBER $job $COMOUT/$cyc/pgrb2b/${RUN}.${cycle}.pgrb2bf${fhr}$cfsuffix
               $DBNROOT/bin/dbn_alert MODEL ENS_PGB2B2_$MEMBER $job $COMOUT/$cyc/pgrb2blr/${RUN}.${cycle}.pgrb2bf${fhr}.2$cfsuffix
            fi
            
            if test "$CREATE_TIGGE" = 'YES'
            then
               $EXECUTIL/wgrib $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbbf${fhr}$cfsuffix |grep -F -f ${FIXGLOBAL}/gefs_tigge_params | $EXECUTIL/wgrib -i -grib -o pgbcfile $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbbf${fhr}$cfsuffix
               $EXECUTIL/cnvgrib -g12 -p40 pgbcfile $COMOUT/$cyc/pgrb2c/${RUN}.${cycle}.pgrb2cf${fhr}$cfsuffix
               MEMBER=`echo $RUN | cut -c3-5 | tr '[a-z]' '[A-Z]'`
               $DBNROOT/bin/dbn_alert MODEL ENS_PGB2C_$MEMBER $job $COMOUT/$cyc/pgrb2c/${RUN}.${cycle}.pgrb2cf${fhr}$cfsuffix
            fi
         fi
      fi
   fi
   rm pgb2afile pgb2bfile pgrb2afile.2 pgrb2bfile.2 pgbcfile

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