#!/bin/sh

sname=`basename $0`
echo `date` $sname $member $partltr $cfsuffix $fsuffix begin
#####################################################################
echo "-----------------------------------------------------"
echo " Script: gefs_post_intcnv.sh" 
echo " "
echo " Purpose - Perform interpolation and GRIB2 conversion"
echo "           on master GRIB files"
echo "           for one member and one time step."
echo "           Move posted files to /com"
echo "           Alert posted files to DBNet"
echo " "
echo " History - "
echo "    Wobus   - 8/28/07 - New "
echo "-----------------------------------------------------"
#####################################################################
export PS4='+ $SECONDS $sname $member $ffhr $LINENO: '
 
date
echo
echo exported input variables
echo
echo anlflag=$anlflag
echo ffhr=$ffhr
echo fhr=$fhr
echo
echo RUN=$RUN
echo member=$member
echo
echo PDY=$PDY
echo cyc=$cyc
echo cycle=$cycle
echo cyc_fcst=$cyc_fcst
echo cfsuffix=$cfsuffix
echo cycsuffix=$cycsuffix
echo
echo addgrb1id=$addgrb1id
echo makegrb1i=$makegrb1i
echo
echo DATA=$DATA
echo COMOUT=$COMOUT
echo SENDCOM=$SENDCOM
echo SENDDBN=$SENDDBN
echo

set -xa

cd $DATA

###testb
if [[ $envir != prod ]]; then
echo
echo files at beginning of pgrb ush
pwd
ls -al
date
fi
###teste

WGRIB=/nwprod/util/exec/wgrib
echo GRBINDEX=$GRBINDEX
export GRBIDX=/nwprod/util/exec/grbindex
ENSADD=$USHGLOBAL/global_ensadd.sh

R1=`echo $RUN|cut -c1-3`
R2=`echo $RUN|cut -c4-5`
case $R1 in
  (gec) if (( R2 == 0 )); then 
           (( e1 = 1 ))
	   (( e2 = 2 ))
	fi;;
  (gen) (( e1 = 2 ))
	(( e2 = R2 ));;
  (gep) (( e1 = 3 ))
	(( e2 = R2 ));;
  (*)   (( e1 = 0 ))
	(( e2 = 0 ))
	echo unrecognized RUN=$RUN R1=$R1 R2=$R2 ;;
esac

COPYGB=$EXECUTIL/copygb
###testb
if [[ $envir != prod ]]; then
  # set PARMGLOBAL for test runs
  if [[ $envir = para ]] || \
    [[ $envir = test ]]; then
    PARMGLOBALSAVE=$PARMGLOBAL
    PARMGLOBAL=/nw$envir/parm
  else
    PARMGLOBALSAVE=$PARMGLOBAL
    PARMGLOBAL=$basesource/nw$envir/parm
    COPYGB=/global/save/wx20mi/copygb.problems/copygb.fd/copygb
  fi
fi
###teste

msg="Starting post for member=$member ffhr=$ffhr"
postmsg "$jlogfile" "$msg"

####################################
# create 1x1 pgrba
####################################

if [[ -f $COMOUT/$cyc/pgrba/${RUN}.${cycle}.pgrba$ffhr$cfsuffix ]] && \
   [[ -f $COMOUT/$cyc/pgrba/${RUN}.${cycle}.pgrbai$ffhr$cfsuffix ]] && \
   [[ $overwrite = no ]]; then
   echo `date` 1x1 pgrba processing skipped for $RUN $ffhr
else
   echo `date` pgrba 1x1 started
   $COPYGB -g3 -i0 -x pgbafile.master.$ffhr$cfsuffix pgbafile.$ffhr$cfsuffix
   echo `date` pgrba 1x1 copygb $ffhr completed
   $ENSADD $e1 $e2 pgbafile.$ffhr$cfsuffix pgbaifile.$ffhr$cfsuffix epgbafile.$ffhr$cfsuffix
   echo `date` pgrba 1x1 ensadd $ffhr completed
   if [[ "$addgrb1id" = "yes" ]]; then
     mv epgbafile.$ffhr$cfsuffix pgbafile.$ffhr$cfsuffix
     if [[ "$makegrb1i" = "yes" ]]; then
       $GRBINDEX pgbafile.$ffhr$cfsuffix pgbaifile.$ffhr$cfsuffix
     fi
   fi
   echo `date` pgrba 1x1 grbindex $ffhr completed
   if test "$SENDCOM" = 'YES'
   then
      #
      # Save Pressure GRIB/Index files
      #
      mv pgbafile.$ffhr$cfsuffix $COMOUT/$cyc/pgrba/${RUN}.${cycle}.pgrba$ffhr$cfsuffix
      if [[ "$makegrb1i" = "yes" ]]; then
	mv pgbaifile.$ffhr$cfsuffix $COMOUT/$cyc/pgrba/${RUN}.${cycle}.pgrbai$ffhr$cfsuffix
      fi

      # Send DBNet alerts for PGBA and PGBA2 at 6 hour increments for all forecast hours
      # Do for 00, 06, 12, and 18Z cycles.

      if test "$SENDDBN" = 'YES' -a "$NET" = 'gens' -a ` expr $cyc % 6 ` -eq 0
      then
	if test `echo $RUN | cut -c1-2` = "ge"
	then
	  MEMBER=`echo $RUN | cut -c3-5 | tr '[a-z]' '[A-Z]'`
	  if [[ $fhr -ge 0 && $fhr -le $fhmax && ` expr $fhr % 6 ` -eq 0 && ! -n "$cfsuffix" ]]
	  then
	    ($DBNROOT/bin/dbn_alert MODEL ENS_PGBA_$MEMBER $job $COMOUT/$cyc/pgrba/${RUN}.${cycle}.pgrba$ffhr$cfsuffix)&
	  fi
	fi
      fi

   fi
   echo `date` pgrba 1x1 sendcom $ffhr completed
fi

if [[ -f $COMOUT/$cyc/pgrb2a/${RUN}.${cycle}.pgrb2a$ffhr$cfsuffix ]] && \
   [[ -f $COMOUT/$cyc/pgrb2a/${RUN}.${cycle}.pgrb2ai$ffhr$cfsuffix ]] && \
   [[ $overwrite = no ]]; then
   echo `date` 1x1 pgrb2a processing skipped for $RUN $ffhr
else

   FILEA=$COMIN/$cyc/pgrba/${RUN}.${cycle}.pgrba$ffhr$cfsuffix

   $EXECUTIL/cnvgrib -g12 -p40 $FILEA pgb2afile.$ffhr$cfsuffix
   $EXECUTIL/wgrib2 -s pgb2afile.$ffhr$cfsuffix > pgb2afile.${ffhr}${cfsuffix}.idx

   if test "$SENDCOM" = 'YES'
   then
      #
      # Save Pressure GRIB/Index files
      #
      mv pgb2afile.$ffhr$cfsuffix $COMOUT/$cyc/pgrb2a/${RUN}.${cycle}.pgrb2a$ffhr$cfsuffix
      mv pgb2afile.${ffhr}${cfsuffix}.idx $COMOUT/$cyc/pgrb2a/${RUN}.${cycle}.pgrb2a${ffhr}${cfsuffix}.idx

      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}.pgrb2a$ffhr$cfsuffix
               $DBNROOT/bin/dbn_alert MODEL ENS_PGB2A_${MEMBER}_WIDX $job \
                $COMOUT/$cyc/pgrb2a/${RUN}.${cycle}.pgrb2a${ffhr}${cfsuffix}.idx
            fi

         fi
      fi
   fi

fi

####################################
# create 1x1 pgrbb
####################################

if [[ -f $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbb$ffhr$cfsuffix ]] && \
   [[ -f $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbbi$ffhr$cfsuffix ]] && \
   [[ $overwrite = no ]]; then
   echo `date` 1x1 pgrbb processing skipped for $RUN $ffhr
else

   echo `date` pgrbb 1x1 started
   $COPYGB -g3 -i0 -x pgbbfile.master.$ffhr$cfsuffix pgbbfile.$ffhr$cfsuffix
   echo `date` pgrbb 1x1 copygb $ffhr completed
   $ENSADD $e1 $e2 pgbbfile.$ffhr$cfsuffix pgbbifile.$ffhr$cfsuffix epgbbfile.$ffhr$cfsuffix
   echo `date` pgrbb 1x1 ensadd $ffhr completed
   if [[ "$addgrb1id" = "yes" ]]; then
     mv epgbbfile.$ffhr$cfsuffix pgbbfile.$ffhr$cfsuffix
     if [[ "$makegrb1i" = "yes" ]]; then
       $GRBINDEX pgbbfile.$ffhr$cfsuffix pgbbifile.$ffhr$cfsuffix
     fi
   fi
   echo `date` pgrbb 1x1 grbindex $ffhr completed
   if test "$SENDCOM" = 'YES'
   then
      #
      # Save Pressure GRIB/Index files
      #
      mv pgbbfile.$ffhr$cfsuffix $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbb$ffhr$cfsuffix
      if [[ "$makegrb1i" = "yes" ]]; then
	mv pgbbifile.$ffhr$cfsuffix $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbbi$ffhr$cfsuffix
      fi

      # Send DBNet alerts for PGBB at 6 hour increments for forecast hours 0 - 84 and
      # for PGBB2 at 6 hour increments for forecast hours 90 through 384.
      # Do for 00 and 12Z cycles.

      if test "$SENDDBN" = 'YES' -a "$NET" = 'gens' -a ` expr $cyc % 12 ` -eq 0
      then
	if test `echo $RUN | cut -c1-2` = "ge"
	then
	  MEMBER=`echo $RUN | cut -c3-5 | tr '[a-z]' '[A-Z]'`
	  if [[ $fhr -ge 0 && $fhr -le 84 && ` expr $fhr % 6 ` -eq 0 && ! -n "$cfsuffix" ]]
	  then
	    $DBNROOT/bin/dbn_alert MODEL ENS_PGBB_$MEMBER $job $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbb$ffhr$cfsuffix
	  fi
	fi
      fi

   fi
   echo `date` pgrbb 1x1 sendcom $ffhr completed
fi

if [[ -f $COMOUT/$cyc/pgrb2b/${RUN}.${cycle}.pgrb2b$ffhr$cfsuffix ]] && \
   [[ -f $COMOUT/$cyc/pgrb2b/${RUN}.${cycle}.pgrb2bi$ffhr$cfsuffix ]] && \
   [[ $overwrite = no ]]; then
   echo `date` 1x1 pgrb2b processing skipped for $RUN $ffhr
else

   FILEB=$COMIN/$cyc/pgrbb/${RUN}.${cycle}.pgrbb$ffhr$cfsuffix

   $EXECUTIL/cnvgrib -g12 -p40 $FILEB pgb2bfile.$ffhr$cfsuffix
   $EXECUTIL/wgrib2 -s pgb2bfile.$ffhr$cfsuffix > pgb2bfile.${ffhr}${cfsuffix}.idx

   if test "$SENDCOM" = 'YES'
   then
      #
      # Save Pressure GRIB/Index files
      #
      mv pgb2bfile.$ffhr$cfsuffix $COMOUT/$cyc/pgrb2b/${RUN}.${cycle}.pgrb2b$ffhr$cfsuffix
      mv pgb2bfile.${ffhr}${cfsuffix}.idx $COMOUT/$cyc/pgrb2b/${RUN}.${cycle}.pgrb2b${ffhr}${cfsuffix}.idx

      if test "$CREATE_TIGGE" = 'YES'
      then
	 if (( fhr == 0 )); then
	   parmlist=${PARMGLOBAL}/gefs_pgrbc_f00.parm
	 else
	   parmlist=${PARMGLOBAL}/gefs_pgrbc_fhh.parm
	 fi
	 $EXECUTIL/wgrib $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbb$ffhr$cfsuffix | \
	   grep -F -f $parmlist | \
	   $EXECUTIL/wgrib -i -grib -o pgbcfile.$ffhr$cfsuffix $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbb$ffhr$cfsuffix
	 $EXECUTIL/cnvgrib -g12 -p40 pgbcfile.$ffhr$cfsuffix $COMOUT/$cyc/pgrb2c/${RUN}.${cycle}.pgrb2c$ffhr$cfsuffix

	 rm pgbcfile.$ffhr$cfsuffix
      fi

      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_PGB2B_$MEMBER $job $COMOUT/$cyc/pgrb2b/${RUN}.${cycle}.pgrb2b$ffhr$cfsuffix
               $DBNROOT/bin/dbn_alert MODEL ENS_PGB2B_${MEMBER}_WIDX $job \
                $COMOUT/$cyc/pgrb2b/${RUN}.${cycle}.pgrb2b${ffhr}${cfsuffix}.idx
            fi
            
            if test "$CREATE_TIGGE" = 'YES'
            then
               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}.pgrb2c$ffhr$cfsuffix
            fi
         fi
      fi
   fi

fi

####################################
# Create Low Res Post File
####################################
if [[ "$DO_LOW_RES" = 'YES' ]] && \
   [ `expr $fhr % 6 ` -eq 0 ] && \
   [[ "$anlflag" != "yes" ]]; then

  if [[ -f $COMOUT/$cyc/pgrbalr/${RUN}.${cycle}.pgrba$ffhr.2$cfsuffix ]] && \
     [[ -f $COMOUT/$cyc/pgrbalr/${RUN}.${cycle}.pgrbai$ffhr.2$cfsuffix ]] && \
     [[ $overwrite = no ]]; then
    echo `date` 2.5x2.5 pgrba processing skipped for $RUN $ffhr
  else

    ####################################
    # create 2.5 x 2.5 pgrba
    ####################################

    echo `date` pgrba 2.5x2.5 background started
    $COPYGB -g2 -i0 -x pgbafile.master.$ffhr$cfsuffix pgbafile.$ffhr.2$cfsuffix
    echo `date` pgrba 2.5x2.5 copygb $ffhr completed
    $ENSADD $e1 $e2 pgbafile.$ffhr.2$cfsuffix pgbaifile.$ffhr.2$cfsuffix epgbafile.$ffhr.2$cfsuffix
    echo `date` pgrba 2.5x2.5 ensadd $ffhr completed
    if [[ "$addgrb1id" = "yes" ]]; then
      mv epgbafile.$ffhr.2$cfsuffix pgbafile.$ffhr.2$cfsuffix
      if [[ "$makegrb1i" = "yes" ]]; then
        $GRBINDEX pgbafile.$ffhr.2$cfsuffix pgbaifile.$ffhr.2$cfsuffix
      fi
    fi
    echo `date` pgrba 2.5x2.5 grbindex $ffhr completed
    if test "$SENDCOM" = 'YES'
    then
      #
      # Save Pressure GRIB/Index files
      #
      mv pgbafile.$ffhr.2$cfsuffix $COMOUT/$cyc/pgrbalr/${RUN}.${cycle}.pgrba$ffhr.2$cfsuffix
      if [[ "$makegrb1i" = "yes" ]]; then
	mv pgbaifile.$ffhr.2$cfsuffix $COMOUT/$cyc/pgrbalr/${RUN}.${cycle}.pgrbai$ffhr.2$cfsuffix
      fi

      # Send DBNet alerts for PGBA and PGBA2 at 6 hour increments for all forecast hours
      # Do for 00, 06, 12, and 18Z cycles.

      if test "$SENDDBN" = 'YES' -a "$NET" = 'gens' -a ` expr $cyc % 6 ` -eq 0
      then
	if test `echo $RUN | cut -c1-2` = "ge"
	then
	  MEMBER=`echo $RUN | cut -c3-5 | tr '[a-z]' '[A-Z]'`
	  if test "$DO_LOW_RES" = 'YES' -a ` expr $fhr % 6 ` -eq 0 -a ! -n "$cfsuffix"
	  then
	    ($DBNROOT/bin/dbn_alert MODEL ENS_PGBA2_$MEMBER $job $COMOUT/$cyc/pgrbalr/${RUN}.${cycle}.pgrba$ffhr.2$cfsuffix)&
	  fi
	fi
      fi
    fi
    echo `date` pgrba 2.5x2.5 sendcom $ffhr completed
  fi

  if [[ -f $COMOUT/$cyc/pgrb2alr/${RUN}.${cycle}.pgrb2a$ffhr.2$cfsuffix ]] && \
     [[ -f $COMOUT/$cyc/pgrb2alr/${RUN}.${cycle}.pgrb2ai$ffhr.2$cfsuffix ]] && \
     [[ $overwrite = no ]]; then
    echo `date` 2.5x2.5 pgrb2a processing skipped for $RUN $ffhr
  else

    FILEALR=$COMIN/$cyc/pgrbalr/${RUN}.${cycle}.pgrba$ffhr.2$cfsuffix

    $EXECUTIL/cnvgrib -g12 -p40 $FILEALR pgb2a.$ffhr.2$cfsuffix
    $EXECUTIL/wgrib2 -s pgb2a.$ffhr.2$cfsuffix > pgb2a.$ffhr.2${cfsuffix}.idx

    if test "$SENDCOM" = 'YES'
    then
      #
      # Save Pressure GRIB/Index files
      #
      mv pgb2a.$ffhr.2$cfsuffix $COMOUT/$cyc/pgrb2alr/${RUN}.${cycle}.pgrb2a$ffhr.2$cfsuffix
      mv pgb2a.$ffhr.2${cfsuffix}.idx $COMOUT/$cyc/pgrb2alr/${RUN}.${cycle}.pgrb2a$ffhr.2${cfsuffix}.idx

      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_PGB2A2_$MEMBER $job $COMOUT/$cyc/pgrb2alr/${RUN}.${cycle}.pgrb2a$ffhr.2$cfsuffix
            $DBNROOT/bin/dbn_alert MODEL ENS_PGB2A2_${MEMBER}_WIDX $job \
             $COMOUT/$cyc/pgrb2alr/${RUN}.${cycle}.pgrb2a$ffhr.2${cfsuffix}.idx
	  fi

	fi
      fi
    fi
  fi



  if [[ -f $COMOUT/$cyc/pgrbblr/${RUN}.${cycle}.pgrbb$ffhr.2$cfsuffix ]] && \
     [[ -f $COMOUT/$cyc/pgrbblr/${RUN}.${cycle}.pgrbbi$ffhr.2$cfsuffix ]] && \
     [[ $overwrite = no ]]; then
    echo `date` 2.5x2.5 pgrbb processing skipped for $RUN $ffhr
  else

    ####################################
    # create 2.5 x 2.5 pgrbb
    ####################################

    echo `date` pgrbb 2.5x2.5 background started
    $COPYGB -g2 -i0 -x pgbbfile.master.$ffhr$cfsuffix pgbbfile.$ffhr.2$cfsuffix
    echo `date` pgrbb 2.5x2.5 copygb $ffhr completed
    $ENSADD $e1 $e2 pgbbfile.$ffhr.2$cfsuffix pgbbifile.$ffhr.2$cfsuffix epgbbfile.$ffhr.2$cfsuffix
    echo `date` pgrbb 2.5x2.5 ensadd $ffhr completed
    if [[ "$addgrb1id" = "yes" ]]; then
      mv epgbbfile.$ffhr.2$cfsuffix pgbbfile.$ffhr.2$cfsuffix
      if [[ "$makegrb1i" = "yes" ]]; then
	$GRBINDEX pgbbfile.$ffhr.2$cfsuffix pgbbifile.$ffhr.2$cfsuffix
      fi
    fi
    echo `date` pgrbb 2.5x2.5 grbindex $ffhr completed
    if test "$SENDCOM" = 'YES'
    then
      #
      # Save Pressure GRIB/Index files
      #
      mv pgbbfile.$ffhr.2$cfsuffix $COMOUT/$cyc/pgrbblr/${RUN}.${cycle}.pgrbb$ffhr.2$cfsuffix
      if [[ "$makegrb1i" = "yes" ]]; then
	mv pgbbifile.$ffhr.2$cfsuffix $COMOUT/$cyc/pgrbblr/${RUN}.${cycle}.pgrbbi$ffhr.2$cfsuffix
      fi

      # Send DBNet alerts for PGBB at 6 hour increments for forecast hours 0 - 84 and
      # for PGBB2 at 6 hour increments for forecast hours 90 through 384.
      # Do for 00 and 12Z cycles.

      if test "$SENDDBN" = 'YES' -a "$NET" = 'gens' -a ` expr $cyc % 12 ` -eq 0
      then
	if test `echo $RUN | cut -c1-2` = "ge"
	then
	  MEMBER=`echo $RUN | cut -c3-5 | tr '[a-z]' '[A-Z]'`
	  if test "$DO_LOW_RES" = 'YES' -a ` expr $fhr % 6 ` -eq 0 -a $fhr -ge 90 -a ! -n "$cfsuffix"
	  then
	    ($DBNROOT/bin/dbn_alert MODEL ENS_PGBB2_$MEMBER $job $COMOUT/$cyc/pgrbblr/${RUN}.${cycle}.pgrbb$ffhr.2$cfsuffix)&
	  fi
	fi
      fi
    fi
    echo `date` pgrbb 2.5x2.5 sendcom $ffhr completed
  fi

  if [[ -f $COMOUT/$cyc/pgrb2blr/${RUN}.${cycle}.pgrb2b$ffhr.2$cfsuffix ]] && \
     [[ -f $COMOUT/$cyc/pgrb2blr/${RUN}.${cycle}.pgrb2bi$ffhr.2$cfsuffix ]] && \
     [[ $overwrite = no ]]; then
    echo `date` 2.5x2.5 pgrb2b processing skipped for $RUN $ffhr
  else

    FILEBLR=$COMIN/$cyc/pgrbblr/${RUN}.${cycle}.pgrbb$ffhr.2$cfsuffix

    $EXECUTIL/cnvgrib -g12 -p40 $FILEBLR pgb2bfile.$ffhr.2$cfsuffix

    if test "$SENDCOM" = 'YES'
    then
      #
      # Save Pressure GRIB/Index files
      #
      mv pgb2bfile.$ffhr.2$cfsuffix $COMOUT/$cyc/pgrb2blr/${RUN}.${cycle}.pgrb2b$ffhr.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_PGB2B2_$MEMBER $job $COMOUT/$cyc/pgrb2blr/${RUN}.${cycle}.pgrb2b$ffhr.2$cfsuffix
	  fi
	fi
      fi
    fi
  fi

fi

####################################
# create 1x1 pgrbd
####################################

if [[ -f $COMOUT/$cyc/pgrbd/${RUN}.${cycle}.pgrbd$ffhr$cfsuffix ]] && \
   [[ -f $COMOUT/$cyc/pgrbd/${RUN}.${cycle}.pgrbdi$ffhr$cfsuffix ]] && \
   [[ $overwrite = no ]]; then
   echo `date` 1x1 pgrbd processing skipped for $RUN $ffhr
else

   echo `date` pgrbd 1x1 processing $ffhr begin
   $COPYGB -g3 -i0 -x pgbdfile.master.$ffhr$cfsuffix pgbdfile.$ffhr$cfsuffix
   echo `date` pgrbd 1x1 copygb $ffhr completed
   $ENSADD $e1 $e2 pgbdfile.$ffhr$cfsuffix pgbdifile.$ffhr$cfsuffix epgbdfile.$ffhr$cfsuffix
   echo `date` pgrbd 1x1 ensadd $ffhr completed
   if [[ "$addgrb1id" = "yes" ]]; then
     mv epgbdfile.$ffhr$cfsuffix pgbdfile.$ffhr$cfsuffix
     if [[ "$makegrb1i" = "yes" ]]; then
       $GRBINDEX pgbdfile.$ffhr$cfsuffix pgbdifile.$ffhr$cfsuffix
     fi
   fi
   echo `date` pgrbd 1x1 grbindex $ffhr completed
   if test "$SENDCOM" = 'YES'
   then
      #
      # Save Pressure GRIB/Index files
      #
      mv pgbdfile.$ffhr$cfsuffix $COMOUT/$cyc/pgrbd/${RUN}.${cycle}.pgrbd$ffhr$cfsuffix
      if [[ "$makegrb1i" = "yes" ]]; then
	mv pgbdifile.$ffhr$cfsuffix $COMOUT/$cyc/pgrbd/${RUN}.${cycle}.pgrbdi$ffhr$cfsuffix
      fi
   fi
   pwd
   ls -al
   echo `date` pgrbd 1x1 sendcom $ffhr completed

fi

if [[ -f $COMOUT/$cyc/pgrb2d/${RUN}.${cycle}.pgrb2d$ffhr$cfsuffix ]] && \
   [[ -f $COMOUT/$cyc/pgrb2d/${RUN}.${cycle}.pgrb2di$ffhr$cfsuffix ]] && \
   [[ $overwrite = no ]]; then
   echo `date` 1x1 pgrb2d processing skipped for $RUN $ffhr
else

   FILED=$COMIN/$cyc/pgrbd/${RUN}.${cycle}.pgrbd$ffhr$cfsuffix

   $EXECUTIL/cnvgrib -g12 -p40 $FILED pgb2dfile.$ffhr$cfsuffix

   if test "$SENDCOM" = 'YES'
   then
      #
      # Save Pressure GRIB/Index files
      #
      mv pgb2dfile.$ffhr$cfsuffix $COMOUT/$cyc/pgrb2d/${RUN}.${cycle}.pgrb2d$ffhr$cfsuffix

   fi

fi


####################################
# save forecasts to /nwges
####################################
if test "$SAVEGES" = "YES" -a $fhr -le 15
then
   cp $COMOUT/$cyc/sfcsig/${RUN}.t${cyc}z.s$ffhr$cfsuffix $GESdir/${RUN}.${cycle}.s$ffhr$cfsuffix
   cp $COMOUT/$cyc/sfcsig/${RUN}.t${cyc}z.b$ffhr$cfsuffix $GESdir/${RUN}.${cycle}.b$ffhr$cfsuffix
   msg="Guess files for fcst hour $fhr copied to $GESdir"
   postmsg "$jlogfile" "$msg"
fi
echo `date` sf and bf copied to nwges $ffhr completed


###testb
# create sigstats in non-nco run
if [[ $envir != prod ]]; then
   if [[ $envir != para ]]; then
     if [[ $envir != test ]]; then
       sigfilename=${RUN}.t${cyc}z.s$ffhr$cfsuffix
       sigstat $COMIN/$cyc/sfcsig/$sigfilename >$COMIN/$cyc/stats/sigstat.$sigfilename
     fi
   fi
fi
###teste
    
####################################
# send control files to misc
####################################
if test $SENDCOM = "YES"
then
   if test $fhr -lt 100
   then
      pad="0"
   else
      pad=""
   fi
   echo "$PDY$cyc$pad$fhr" > $COMOUT/$cyc/misc/${RUN}.t${cyc}z.intcnv.control$cfsuffix
fi

###testb
if [[ $envir != prod ]]; then
  #restore PARMGLOBAL to default
  if [[ $envir != para ]]; then
    if [[ $envir != test ]]; then
      PARMGLOBAL=$PARMGLOBALSAVE
    fi
  fi
fi
###teste

cat $pgmout

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

echo
echo files at end of pgrb ush
pwd
ls -al
echo `date` $sname $member $partltr $cfsuffix $fsuffix end on machine=`uname -n`
msg='ENDED NORMALLY.'
postmsg "$jlogfile" "$msg"

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