#!/bin/ksh
################################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exfirewx_post.sh
# Script description:  Run eta post jobs
#
# Author:        Eric Rogers       Org: NP22         Date: 1999-06-23
#
# Abstract: This script runs the NMM post jobs for the firewx forecast
#
# Script history log:
# 1999-06-23  Eric Rogers
# 1999-08-17  Brent Gordon  -- Modified for production.
# 2001-04-23  Eric Rogers modified Eta script for hiresw runs
# 2002-03-15  Eric Rogers modified 8km/4km hires window model
# 2003-03-11  Eric Rogers modified script for NMM firewx model
#

set -x
cd $DATA 

msg="$job HAS BEGUN FOR NEST=$NEST"
postmsg "$jlogfile" "$msg"

for fhr in $post_times
do
  icnt=1
  while [ $icnt -lt 1000 ]
  do
    if [ -s $FCSTDIR/fcstdone${fhr}.${tmmark} ]
    then
      break
    else
      icnt=$((icnt + 1))
      sleep 5
    fi
    if [ $icnt -ge 360 ]
    then
      msg="ABORTING after 30 minutes of waiting for FIREWX FCST F${fhr} to end."
      err_exit $msg
    fi
  done

  postmsg "$jlogfile" "Starting post for NEST=$NEST at F${fhr}"

  nposts=01
  incpst=01

  ln -s $FCSTDIR/restrt${fhr}.${tmmark} restrt${fhr}.${tmmark}

  export pgm=firewx_post
  . prep_step
  export XLFUNIT_11=$DATA/fcstdata.parm
  export XLFUNIT_12=$DATA/nhbmeso
  export XLFUNIT_13=$DATA/restrt${fhr}.${tmmark}
  export XLFUNIT_14=$PARMfirewx/firewx_cntrl.${NEST}.parm

  startmsg
  $EXECfirewx/firewx_post << ioEOF >> $pgmout 2>errfile
  $fhr $nposts $incpst
ioEOF
  export err=$?;err_chk

  mv EGRD3D${fhr}.tm00 $COMOUT/${RUN}.${cycle}.egrd3d${fhr}.tm00
  mv EGDAWP${fhr}.tm00 $COMOUT/${RUN}.${cycle}.egdawp${fhr}.tm00
  mv EGRDSF${fhr}.tm00 $COMOUT/${RUN}.${cycle}.egrdsf${fhr}.tm00

  if test $SENDDBN = 'YES'
  then
   echo "test"
###MAS    $DBNROOT/bin/dbn_alert MODEL NAM_${DBN_NEST}_EGB3D $job $COMOUT/${RUN}.${cycle}.egrd3d${fhr}.tm00
###MAS    $DBNROOT/bin/dbn_alert MODEL NAM_${DBN_NEST}_EGDSF $job $COMOUT/${RUN}.${cycle}.egrdsf${fhr}.tm00
  fi

  echo done > $FCSTDIR/postdone${fhr}.${tmmark}

  postmsg "$jlogfile" "FIREWX POST done for F${fhr}"

  #
  # Run the prdgen code for the firewx here, since both post and prdgen
  # run quickly for these smaller domains.
  #
  postmsg "$jlogfile" "Starting prdgen for NEST=$NEST at F${fhr}"
  $USHfirewx/firewx_prdgen.sh $fhr

done

echo EXITING $0
exit
#
