#!/bin/ksh
######################################################################
####  UNIX Script Documentation Block
#                      .                                             .
# Script name:         exfirewx_post0.sh
# Script description:  Trigger NMM sounding post job
#
# Author:        Eric Rogers       Org: NP22         Date: 1999-06-23
#
# Abstract: This script triggers the NMM sounding post job, which
#           creates a piece of the model sounding profile whose
#           time interval is determined by the input forecast hours.
#
# Script history log:
# 2000-05-16  Eric Rogers
#
set -x

cd $DATA

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

export tmmark=tm00
. $COMIN/${RUN}.t${cyc}z.envir.sh

cp $FIXfirewx/firewx_profdat.${NEST} $DATA/firewx_profdat
cp $FIXhiresw/hiresw_deta_ldt1.60.25mb deta.0
cp deta.0 deta.01
cp deta.01 deta.02
cp deta.01 deta.03
cp deta.01 deta.04
cp deta.01 deta.05
cp $PARMfirewx/firewx_bcexdata.parm.${tmmark} bcexdata.parm

export fhr=00
typeset -Z2 fhr

endfhr=48

while [ $fhr -le $endfhr ]
do
   ic=1
   while [ $ic -lt 1000 ]
   do
      if [ -s $DATA/fcstdone${fhr}.${tmmark} ]
      then
         break
      else
         let "ic=ic+1"
         sleep 10
      fi

      if [ $ic -ge 180 ]
      then
         err_exit "COULD NOT LOCATE: $DATA/fcstdone${fhr}.${tmmark}"
      fi
   done

   if [ $fhr -eq 00 ]
   then
      rm $DATA/sndpdone
   fi

   nposts=01
   incpst=01

   export RSTFNL=$DATA/ 

   export pgm=firewx_post0
   . prep_step
   export XLFUNIT_12=$DATA/nhbmeso
   export XLFUNIT_17=$DATA/bcexdata.parm
   export XLFUNIT_19=$DATA/firewx_profdat
   export XLFUNIT_76=$DATA/profilm.c1.${tmmark}

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


   if [ $fhr -ge 02 ]
   then
      let "fhrm=fhr-2"
      typeset -Z2 fhrm
      y=`expr $fhrm % 3`

      if [ $y -ne 0 ]
      then
      echo "do nothing"
###MAS         rm $DATA/restrt${fhrm}.tm00
      else
         ic=1
         while [ $ic -lt 1000 ]
         do
            if [ -s $DATA/postdone${fhrm}.${tmmark} ]
            then
###MAS               rm $DATA/restrt${fhrm}.tm00
               break
            else
               let "ic=ic+1"
               sleep 10
            fi

            if [ $ic -ge 180 ]
            then
              err_exit "COULD NOT LOCATE: $DATA/postdone${fhrm}.${tmmark}"
            fi
         done
      fi
   fi

   let "fhr=fhr+1"
   typeset -Z2 fhr
done

# Done with post0 so run sounding post if fcsthr=48

if [ $fhr -gt $endfhr ]
then

   $USHhiresw/hiresw_sndp.sh

   mv class1.bufr.tm00 $COMOUT/${RUN}.${cycle}.class1.bufr.tm00

   echo $NEST > NAME
   dd if=NAME conv=ucase > name
   unest=`cut -c 1-10 name`

   if [ $SENDDBN = "YES" ]
   then
      $DBNROOT/bin/dbn_alert MODEL ${unest}_BUFR_class1 $job $COMOUT/${RUN}.${cycle}.class1.bufr.tm00
      $DBNROOT/bin/dbn_alert MODEL ${unest}_BUFR_SND $job $COMOUT/bufr.${cycle}.${RUN}/${RUN}.bufrloc
   fi
fi

msg="JOB $job FOR NEST=$NEST HAS COMPLETED NORMALLY."
postmsg "$jlogfile" "$msg"

echo EXITING $0 with return code $err
exit $err
