#####################################################################
echo "-----------------------------------------------------"
echo " exglobal_postg2.sh.sms" 
echo " Apr 99 - Michaud - Generated to post global forecast"
echo " Mar 03 - Zhu - Add post for 0.5x0.5 degree"
echo " Aug 03 - Gilbert - modified from exglobal_post.sh.sms"
echo "          to create 0.5x0.5 degree fields in GRIB2 format."
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 IO=720
   export JO=361
#   export POSTGPVARS=$POSTGPVARS
   export POSTGPLIST=$FIXGLOBAL/global_kplist.1d.txt
   export IGEN=$IGEN_ANL
    
   $USHGLOBAL/global_postgp.sh

#     Convert to GRIB2
   $EXECUTIL/cnvgrib -g12 -p40000 pgbfile pgb2file

   if test "$SENDCOM" = 'YES'
   then
      #
      # Save Pressure GRIB2 files
      #
      mv pgb2file $COMOUT/${RUN}.${cycle}.pgrb2anl

      if test "$SENDDBN" = 'YES'
      then
        if test "$RUN" = 'gfs'
        then
            $DBNROOT/bin/dbn_alert MODEL GFS_PGB2 $job $COMOUT/${RUN}.${cycle}.pgrb2anl
        fi
      fi
   fi
   rm pgbfile

fi

SLEEP_LOOP_MAX=`expr $SLEEP_TIME / $SLEEP_INT`

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
       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 IO=720
    export JO=361
#    export POSTGPVARS=$POSTGPVARS_HIGH
    export POSTGPLIST=$FIXGLOBAL/global_kplist.1d.txt
    export ANOMCATSH=${USHGLOBAL}/global_anomcat.sh
    
    $USHGLOBAL/global_postgp.sh

#     Convert to GRIB2
    $EXECUTIL/cnvgrib -g12 -p40000 pgbfile pgb2file

    if test $SENDCOM = "YES"
    then
       mv pgb2file $COMOUT/${RUN}.${cycle}.pgrb2f${fhr}

       if test "$SENDDBN" = 'YES'
       then
         if test "$RUN" = 'gfs'
         then
            $DBNROOT/bin/dbn_alert MODEL GFS_PGB2 $job $COMOUT/${RUN}.${cycle}.pgrb2f${fhr}
         fi
       fi
       if test $fhr -lt 100
       then
          pad="0"
       else
          pad=""
       fi
       echo "$PDY$cyc$pad$fhr" > $COMOUT/${RUN}.t${cyc}z.control2
    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 #######################
