#####################################################################
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 " Jul 05 - Wobus - 6-hour breeding, ensemble only"
echo "-----------------------------------------------------"
#####################################################################

set -x

cd $DATA

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

export MP_LABELIO=YES
#poe hostname

if [[ "$cycsuffix" = "true" ]]; then
  export cfsuffix=".cycfs$cyc_fcst"
else
  export cfsuffix=""
fi
echo cyc=$cyc cyc_fcst=$cyc_fcst
echo cycle=$cycle cycle_fcst=$cycle_fcst
echo fcstlong=$fcstlong
echo cycsuffix=$cycsuffix
echo cfsuffix=$cfsuffix

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

WGRIB_LIST=":HGT:1000.mb|:HGT:925.mb|:HGT:850.mb|:HGT:700.mb|:HGT:500.mb|:HGT:250.mb|:HGT:200.mb|:UGRD:10.m.abo|:UGRD:1000.mb|:UGRD:925.mb|:UGRD:850.mb|:UGRD:700.mb|:UGRD:500.mb|:UGRD:250.mb|:UGRD:200.mb|:VGRD:10.m.abo|:VGRD:1000.mb|:VGRD:925.mb|:VGRD:850.mb|:VGRD:700.mb|:VGRD:500.mb|:VGRD:250.mb|:VGRD:200.mb|:TMP:2.m.|:TMP:1000.mb|:TMP:925.mb|:TMP:850.mb|:TMP:700.mb|:TMP:500.mb|:TMP:250.mb|:TMP:200.mb|:RH:2.m.|:RH:1000.mb|:RH:925.mb|:RH:850.mb|:RH:700.mb|:RH:500.mb|:RH:250.mb|:RH:200.mb|:APCP|:PRMSL:MSL|:PRES:sfc|:HGT:sfc:anl|:PWAT:atmos.col|:TCDC:atmos.col|:CAPE:180-0.mb|:TMAX:2.m.|:TMIN:2.m.|:CRAIN:sfc|:CFRZR:sfc|:CICEP:sfc|:CSNOW:sfc"

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

############################################################
#  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/$cyc/init/${RUN}.t${cyc}z.sanl$cfsuffix -a ${SHOUR} -eq 0
then
   rm -rf sigfile
   ln -s $COMIN/$cyc/init/${RUN}.t${cyc}z.sanl$cfsuffix sigfile

   ####################################
   # Create High Res Post File 
   ####################################
   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
   $WGRIB -s pgbfile > pgbfile.inv

   ####################################
   # Break Out Post Files
   ####################################
   #
   # control the inclusion of perturbation identifiers
   # in the GRIB1 ensemble PDS header extension
   #
   addgrb1id=yes
   export makegrb1i=yes

   (egrep $WGRIB_LIST pgbfile.inv | $WGRIB pgbfile -s -grib -i -o pgbafile
   $GRBINDEX pgbafile pgbaifile
   $ENSADD $e1 $e2 pgbafile pgbaifile epgbafile
   if [[ "$addgrb1id" = "yes" ]]; then
     mv epgbafile pgbafile
     if [[ "$makegrb1i" = "yes" ]]; then
       $GRBINDEX pgbafile pgbaifile
     fi
   fi)&

   (egrep -v $WGRIB_LIST pgbfile.inv | $WGRIB pgbfile -s -grib -i -o pgbbfile
   $GRBINDEX pgbbfile pgbbifile
   $ENSADD $e1 $e2 pgbbfile pgbbifile epgbbfile
   if [[ "$addgrb1id" = "yes" ]]; then
     mv epgbbfile pgbbfile
     if [[ "$makegrb1i" = "yes" ]]; then
       $GRBINDEX pgbbfile pgbbifile
     fi
   fi)&

   wait

   if test "$SENDCOM" = 'YES'
   then
      #
      # Save Pressure GRIB/Index files
      #
      mv pgbafile $COMOUT/$cyc/pgrba/${RUN}.${cycle}.pgrbaanl$cfsuffix
      mv pgbbfile $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbbanl$cfsuffix
      if [[ "$makegrb1i" = "yes" ]]; then
	mv pgbaifile $COMOUT/$cyc/pgrba/${RUN}.${cycle}.pgrbaianl$cfsuffix
	mv pgbbifile $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbbianl$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_PGBA_$MEMBER $job $COMOUT/$cyc/pgrba/${RUN}.${cycle}.pgrbaanl$cfsuffix)&
            fi
            
            if test `echo $RUN | cut -c1-2` = "ge" -a ! -n "$cfsuffix" -a ` expr $cyc % 12 ` -eq 0
            then
               MEMBER=`echo $RUN | cut -c3-5 | tr '[a-z]' '[A-Z]'`
               ($DBNROOT/bin/dbn_alert MODEL ENS_PGBB_$MEMBER $job $COMOUT/$cyc/pgrbb/${RUN}.${cycle}.pgrbbanl$cfsuffix)&
            fi
            wait
         fi
      fi
   fi
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 test -f $restart_file$fhr$cfsuffix -a -f $COMIN/$cyc/sfcsig/${RUN}.t${cyc}z.sf$fhr$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 post for fhr=$fhr"
    postmsg "$jlogfile" "$msg"

    ###############################
    # Put restart files into /nwges 
    # for backup to start Model Fcst
    ###############################
    rm -rf sigfile flxfile
    ln -s $COMIN/$cyc/sfcsig/${RUN}.t${cyc}z.sf$fhr$cfsuffix sigfile
    ln -s $COMIN/$cyc/sflux/${RUN}.t${cyc}z.sfluxgrbf$fhr$cfsuffix flxfile


    ###################################3
    # Create High Res Post File
    ###################################3
    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

    ###################################3
    # Create Low Res Post File
    ###################################3
    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
        $EXECUTIL/copygb -g2 -i1,1 -x pgbfile pgbfile.2
    fi

    ###################################3
    # Break Out Post Files
    ###################################3
    ($WGRIB -s pgbfile > pgbfile.inv)&
    $WGRIB -s pgbfile.2 > pgbfile.2.inv

    wait

    (egrep    $WGRIB_LIST pgbfile.inv | $WGRIB pgbfile -s -grib -i -o pgbafile
    $GRBINDEX pgbafile pgbaifile
    $ENSADD $e1 $e2 pgbafile pgbaifile epgbafile
    if [[ "$addgrb1id" = "yes" ]]; then
       mv epgbafile pgbafile
       if [[ "$makegrb1i" = "yes" ]]; then
         $GRBINDEX pgbafile pgbaifile
       fi
    fi)&

    (egrep -v $WGRIB_LIST pgbfile.inv | $WGRIB pgbfile -s -grib -i -o pgbbfile
    $GRBINDEX pgbbfile pgbbifile
    $ENSADD $e1 $e2 pgbbfile pgbbifile epgbbfile
    if [[ "$addgrb1id" = "yes" ]]; then
       mv epgbbfile pgbbfile
       if [[ "$makegrb1i" = "yes" ]]; then
         $GRBINDEX pgbbfile pgbbifile
       fi
    fi)&

    if test "$DO_LOW_RES" = 'YES' -a `expr $fhr % 6 ` -eq 0
    then
       (egrep    $WGRIB_LIST pgbfile.2.inv | $WGRIB pgbfile.2 -s -grib -i -o pgbafile.2
       $GRBINDEX pgbafile.2 pgbaifile.2
       $ENSADD $e1 $e2 pgbafile.2 pgbaifile.2 epgbafile.2
       if [[ "$addgrb1id" = "yes" ]]; then
	 mv epgbafile.2 pgbafile.2
	 if [[ "$makegrb1i" = "yes" ]]; then
	   $GRBINDEX pgbafile.2 pgbaifile.2
	 fi
       fi)&

       (egrep -v $WGRIB_LIST pgbfile.2.inv | $WGRIB pgbfile.2 -s -grib -i -o pgbbfile.2
       $GRBINDEX pgbbfile.2 pgbbifile.2
       $ENSADD $e1 $e2 pgbbfile.2 pgbbifile.2 epgbbfile.2

       if [[ "$addgrb1id" = "yes" ]]; then
	 mv epgbbfile.2 pgbbfile.2
	 if [[ "$makegrb1i" = "yes" ]]; then
	   $GRBINDEX pgbbfile.2 pgbbifile.2
	 fi
       fi)&

    fi

    wait
    
    $USHGLOBAL/gefs_transfer.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/${RUN}.t${cyc}z.control$cfsuffix
    fi
    rm flxfile flxifile pgbfile pgifile

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