#####################################################################
echo "-----------------------------------------------------"
echo " exglobal_post.sh.sms" 
echo " Apr 99 - Michaud - Generated to post global forecast"
echo " Mar 03 - Zhu - Add post for 0.5x0.5 degree"
echo "-----------------------------------------------------"
#####################################################################

set -x

cd $DATA

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

export MP_LABELIO=YES
#poe hostname

############################################################
#  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.sanl -a ${RUN} != "drfmr" -a ${SHOUR} -eq 0
then
   cp $COMIN/${RUN}.t${cyc}z.sanl sigfile

   export SIGINP=sigfile
   export FLXINP=/dev/null
   export FLXIOUT=/dev/null
   export PGBOUT=pgbfile
   export PGIOUT=pgifile
   export POSTGPVARS=$POSTGPVARS_HIGH
   export POSTGPLIST=$FIXGLOBAL/global_kplist.1d.txt
   export IGEN=$IGEN_ANL
    
   $USHGLOBAL/global_postgp.sh

   if test "$SENDCOM" = 'YES'
   then
      #
      # Save Pressure GRIB/Index files
      #
      cp pgbfile $COMOUT/${RUN}.${cycle}.pgrbanl
      cp pgifile $COMOUT/${RUN}.${cycle}.pgrbianl
      if [ "$CHGRES" = "YES" ]
      then
        #
        # Save Pressure GRIB/Index files to 126 directory also
        #  (For FTP back to Crays)
        #
        cp pgbfile $COMOUT126/${RUN}.${cycle}.pgrbanl
        cp pgifile $COMOUT126/${RUN}.${cycle}.pgrbianl
        cp $COMIN/${RUN}.t${cyc}z.prepbufr $COMOUT126
        cp $COMIN/${RUN}.t${cyc}z.prepbufr.unblok $COMOUT126
        cp $COMIN/${RUN}.t${cyc}z.abias $COMOUT126
      fi
      # convert gdas pgrbanl file to grib2
      if test "$RUN" = 'gdas1'
      then
         /nwprod/util/exec/cnvgrib -g12 -p40 $COMOUT/${RUN}.${cycle}.pgrbanl \
         $COMOUT/${RUN}.${cycle}.pgrbanl.grib2
         /nwprod/util/exec/wgrib2 $COMOUT/${RUN}.${cycle}.pgrbanl.grib2 -s >\
         $COMOUT/${RUN}.${cycle}.pgrbanl.grib2.idx 
      fi

      if test "$SENDDBN" = 'YES'
      then
         if test "$RUN" = 'gdas1'
         then
            run=`echo $RUN | tr '[a-z]' '[A-Z]'`
            $DBNROOT/bin/dbn_alert MODEL ${run}_MSC_sfcanl $job $COMOUT/${RUN}.${cycle}.sfcanl
            $DBNROOT/bin/dbn_alert MODEL ${run}_SA $job $COMIN/${RUN}.t${cyc}z.sanl
            $DBNROOT/bin/dbn_alert MODEL ${run}_PGA $job $COMIN/${RUN}.t${cyc}z.pgrbanl
            $DBNROOT/bin/dbn_alert MODEL ${run}_PGA_GB2 $job $COMIN/${RUN}.t${cyc}z.pgrbanl.grib2
            $DBNROOT/bin/dbn_alert MODEL ${run}_PGA_GB2_WIDX $job $COMIN/${RUN}.t${cyc}z.pgrbanl.grib2.idx
            $DBNROOT/bin/dbn_alert MODEL ${run}_PGAI $job $COMIN/${RUN}.t${cyc}z.pgrbianl
         fi
         if test "$RUN" = 'gfs'
         then
            $DBNROOT/bin/dbn_alert MODEL GFS_MSC_sfcanl $job $COMOUT/${RUN}.${cycle}.sfcanl
            $DBNROOT/bin/dbn_alert MODEL GFS_SA $job $COMIN/${RUN}.t${cyc}z.sanl
            $DBNROOT/bin/dbn_alert MODEL GFS_PGB $job $COMOUT/${RUN}.${cycle}.pgrbanl
            $DBNROOT/bin/dbn_alert MODEL GFS_PGBI $job $COMOUT/${RUN}.${cycle}.pgrbianl
         fi
         if test "$RUN" = 'mrf'
         then
            if test `echo $RUN | cut -c1-3` = "ens"
            then
               MEMBER=`echo $RUN | cut -c4-5 | tr '[a-z]' '[A-Z]'`
               $DBNROOT/bin/dbn_alert MODEL ENS_PGB_$MEMBER $job $COMOUT/${RUN}.${cycle}.pgrbanl
               $DBNROOT/bin/dbn_alert MODEL ENS_PGBI_$MEMBER $job $COMOUT/${RUN}.${cycle}.pgrbianl
            fi
         fi
      fi
   fi
   rm pgbfile pgifile

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
    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 post for fhr=$fhr"
    postmsg "$jlogfile" "$msg"

    ###############################
    # Put restart files into /nwges 
    # for backup to start Model Fcst
    ###############################
    cp $COMIN/${RUN}.t${cyc}z.sf$fhr sigfile
    cp $COMIN/${RUN}.t${cyc}z.sfluxgrbf$fhr flxfile

    if test $fhr -gt 0
    then
       export IGEN=$IGEN_FCST
    else
       export IGEN=$IGEN_ANL
    fi
    export SIGINP=sigfile
    export FLXINP=flxfile
    export FLXIOUT=flxifile
    export PGBOUT=pgbfile
    export PGIOUT=pgifile
    export POSTGPLIST=$FIXGLOBAL/global_kplist.1d.txt
    export POSTGPVARS=$POSTGPVARS_HIGH
    export ANOMCATSH=${USHGLOBAL}/global_anomcat.sh
    
    $USHGLOBAL/global_postgp.sh

    if test "$fhr" = '00' -a "$RUN" = 'drfmr'
    then
      rm $PGIOUT
      mv $PGBOUT pgbfile1
      $EXECGLOBAL/global_overgen_grib pgbfile1 $PGBOUT 
      /nwprod/util/exec/grbindex $PGBOUT $PGIOUT
    fi

#   if test "$DO_LOW_RES" = 'YES' -a `expr $fhr % 12 ` -eq 0
    if test "$DO_LOW_RES" = 'YES' -a `expr $fhr % 6 ` -eq 0
    then
       export SIGINP=sigfile
       export FLXINP=flxfile
       export FLXIOUT=flxifile
       export PGBOUT=pgbfile.2
       export PGIOUT=pgifile.2
       export POSTGPLIST=$FIXGLOBAL/global_kplist.1d.txt
       export POSTGPVARS=$POSTGPVARS_LOW
    
       $USHGLOBAL/global_postgp.sh
    fi
    
### Add process for 0.5x0.5 PGRB files

    if test "$RUN" = "gfs" -a "$DO_HD_PGRB" = 'YES' -a $fhr -le $HDMAX
    then
       export SIGINP=sigfile
       export FLXINP=flxfile
       export FLXIOUT=flxifile
       export PGBOUT=pgbfile.hd
       export PGIOUT=pgifile.hd
       export IO=720
       export JO=361
       export POSTGPLIST=$FIXGLOBAL/global_kplist.hd.txt
       export POSTGPVARS=$POSTGPVARS_HD
       export ANOMCATSH='/dev/null'
       $USHGLOBAL/global_postgp.sh
    fi

### End 0.5x0.5 degree PGRB files

    $USHGLOBAL/global_transfer.sh

    if test $SENDCOM = "YES"
    then
       if test $fhr -lt 100
       then
          pad="0"
       else
          pad=""
       fi
       echo "$PDY$cyc$pad$fhr" > $COMOUT/${RUN}.t${cyc}z.control
    fi
    rm flxfile flxifile pgbfile pgifile pgbfile.grib2 pgbfile.grib2.idx \
       flxfile.grib2 flxfile.grib2.idx

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