#####################################################################
echo "-----------------------------------------------------"
echo " exglobal_post_gfs.sh.sms" 
echo " Feb 06 - Wobus - new script to convert gfs to pgrba"
echo "-----------------------------------------------------"
#####################################################################
echo `date` $0 begin

set -x

cd $DATA

msg="HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

export MP_LABELIO=YES
#poe hostname

echo cyc=$cyc
echo cycle=$cycle

export WGRIB=${EXECUTIL}/wgrib
export COPYGB=$EXECUTIL/copygb
export CNVGRIB=${EXECUTIL}/cnvgrib
export GRBIDX=${EXECUTIL}/grbindex
export GRB2INDEX=${EXECUTIL}/grb2index
export ENSADD=$USHGLOBAL/global_ensadd.sh
echo WGRIB=$WGRIB
echo COPYGB=$COPYGB
echo CNVGRIB=$CNVGRIB
echo GRBINDEX=$GRBINDEX
echo GRBIDX=$GRBIDX
echo GRB2INDEX=$GRB2INDEX
echo ENSADD=$ENSADD

# Turn off the generation of the grib2 index files for this job:
export makegrb2i=no

# set variables for ensemble PDS header
(( e1 = 1 ))
(( e2 = 1 ))

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

############################################################
# Post Analysis Files before starting the Forecast Post
############################################################
if test -f $COMIN/${RUN}.t${cyc}z.master.grbanl -a ${SHOUR} -eq 0
then
   parmlist=$PARMgefs/gefs_pgrba_f00.parm
   cp $COMIN/${RUN}.t${cyc}z.master.grbanl master_grbfile
   $WGRIB -s master_grbfile |grep -F -f $parmlist |$WGRIB -i -grib -o tmpfile master_grbfile
   $COPYGB -g3 -i0 -x tmpfile pgbafile
   $GRBINDEX pgbafile pgbaifile
   $ENSADD $e1 $e2 pgbafile pgbaifile epgbafile
   if [[ "$makepgrb2a" = "yes" ]]; then
     $CNVGRIB -g12 -p40 epgbafile pgb2afile
   fi
   if [[ "$makegrb2i" = "yes" ]]; then
     if [[ "$makepgrb2a" = "yes" ]]; then
       $GRB2INDEX pgb2afile pgb2aifile
     fi
   fi
   #
   # control the inclusion of perturbation identifiers
   # in the GRIB1 ensemble PDS header extension
   #
   if [[ "$addgrb1id" = "yes" ]]; then
     mv epgbafile pgbafile
     if [[ "$makegrb1i" = "yes" ]]; then
       $GRBINDEX pgbafile pgbaifile
     fi
   fi

   if test "$SENDCOM" = 'YES'
   then
      #
      # Save Pressure GRIB/Index files
      #
      mv pgbafile $COMOUT/$cyc/pgrba/ge${RUN}.${cycle}.pgrbaanl
      if [[ "$makegrb1i" = "yes" ]]; then
	mv pgbaifile $COMOUT/$cyc/pgrba/ge${RUN}.${cycle}.pgrbaianl
      fi
      if [[ "$makepgrb2a" = "yes" ]]; then
	mv pgb2afile $COMOUT/$cyc/pgrb2a/ge${RUN}.${cycle}.pgrb2aanl
      fi
      if [[ "$makegrb2i" = "yes" ]]; then
	if [[ "$makepgrb2a" = "yes" ]]; then
	  cp pgb2aifile $COMOUT/$cyc/pgrb2a/ge${RUN}.${cycle}.pgrb2aianl
	fi
      fi

#################################### CHECK DBN ALERTS ###########
#################################### CHECK DBN ALERTS ###########
#################################### CHECK DBN ALERTS ###########
      if test "$SENDDBN" = 'YES'
      then
         if test "$NET" = 'gens'
         then
	   MEMBER=GFS
	   $DBNROOT/bin/dbn_alert MODEL ENS_PGBA_$MEMBER $job $COMOUT/$cyc/pgrba/ge${RUN}.${cycle}.pgrbaanl
	   if [[ "$makegrb1i" = "yes" ]]; then
	     $DBNROOT/bin/dbn_alert MODEL ENS_PGBAI_$MEMBER $job $COMOUT/$cyc/pgrba/ge${RUN}.${cycle}.pgrbaianl
	   fi
	   if [[ "$makepgrb2a" = "yes" ]]; then
	     $DBNROOT/bin/dbn_alert MODEL ENS_PGB2A_$MEMBER $job $COMOUT/$cyc/pgrb2a/ge${RUN}.${cycle}.pgrb2aanl
	   fi
	   if [[ "$makegrb2i" = "yes" ]]; then
	     if [[ "$makepgrb2a" = "yes" ]]; then
	       $DBNROOT/bin/dbn_alert MODEL ENS_PGB2AI_$MEMBER $job $COMOUT/$cyc/pgrb2a/ge${RUN}.${cycle}.pgrb2aianl
	     fi
	   fi
         fi
      fi
   fi
   rm pgbfile
   rm pgbafile pgbaifile
   rm pgb2afile pgb2aifile

fi

SLEEP_LOOP_MAX=`expr $SLEEP_TIME / $SLEEP_INT`

(( fhr=SHOUR))
if (( fhr == 0 )); then
  fhr=00
fi
export fhr

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
    set -x
    export pgm="postcheck"
    ic=1

    while [ $ic -le $SLEEP_LOOP_MAX ]
    do
       if [[ -s $restart_file_a$fhr ]] && [[ -s $restart_file_b$fhr ]]; then
	  found=yes
          break
       else
          ic=`expr $ic + 1`
          sleep $SLEEP_INT
       fi
       if (( fhr > fh_gfs_hires )) && (( fhr % 12 == 6 )); then
	 if (( ic > 2 )) && (( ic < SLEEP_LOOP_MAX )); then
	   echo fhr=$fhr not expected
	   found=no
	   break
	 fi
       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

  if [[ "$found" = yes ]]; then

    msg="Starting post for fhr=$fhr"
    postmsg "$jlogfile" "$msg"

    if [ $fhr -eq 0 ]
    then
      parmlist=$PARMgefs/gefs_pgrba_f00.parm
    else
      parmlist=$PARMgefs/gefs_pgrba_fhh.parm
    fi

    cp $COMIN/${RUN}.t${cyc}z.master.grbf${fhr} master_grbfile
    $WGRIB -s master_grbfile |grep -F -f $parmlist |$WGRIB -i -grib -o tmpfile master_grbfile
    $COPYGB -g3 -i0 -x tmpfile pgbafile
    $GRBINDEX pgbafile pgbaifile
    $ENSADD $e1 $e2 pgbafile pgbaifile epgbafile
    if [[ "$makepgrb2a" = "yes" ]]; then
      $CNVGRIB -g12 -p40 epgbafile pgb2afile
    fi
    if [[ "$makegrb2i" = "yes" ]]; then
      if [[ "$makepgrb2a" = "yes" ]]; then
	$GRB2INDEX pgb2afile pgb2aifile
      fi
    fi

    if [[ "$addgrb1id" = "yes" ]]; then
      mv epgbafile pgbafile
      if [[ "$makegrb1i" = "yes" ]]; then
	$GRBINDEX pgbafile pgbaifile
      fi
    fi

    if test "$DO_LOW_RES" = 'YES' -a `expr $fhr % 6 ` -eq 0
    then
      $WGRIB -s master_grbfile |grep -F -f $parmlist |$WGRIB -i -grib -o tmpfile.2 master_grbfile
      $COPYGB -g2 -i0 -x tmpfile.2 pgbafile.2
      $GRBINDEX pgbafile.2 pgbaifile.2
      $ENSADD $e1 $e2 pgbafile.2 pgbaifile.2 epgbafile.2
      if [[ "$makepgrb2a" = "yes" ]]; then
	 $CNVGRIB -g12 -p40 epgbafile.2 pgb2afile.2
      fi
      if [[ "$makegrb2i" = "yes" ]]; then
	 if [[ "$makepgrb2a" = "yes" ]]; then
	   $GRB2INDEX pgb2afile.2 pgb2aifile.2
	 fi
      fi

      if [[ "$addgrb1id" = "yes" ]]; then
	 mv epgbafile.2 pgbafile.2
	 if [[ "$makegrb1i" = "yes" ]]; then
	   $GRBINDEX pgbafile.2 pgbaifile.2
	 fi
      fi

    fi
    
    $USHGLOBAL/gefs_transfer_gfs.sh

    if test $SENDCOM = "YES"
    then
       if test $fhr -lt 100
       then
          pad="0"
       else
          pad=""
       fi
       echo "$PDY$cyc$pad$fhr" > $COMOUT/$cyc/misc/ge${RUN}.t${cyc}z.control
    fi
    rm pgbfile
   rm pgbafile pgbaifile
   rm pgb2afile pgb2aifile

    fi
# if not found, come here to increment

    export fhr=`expr $fhr + $FHINC`
    if test $fhr -lt 10
    then
       export fhr="0$fhr"
    fi
done

cat $pgmout

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

echo `date` $0 end
msg='ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

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