###########################################################################
echo "--------------------------------------------------------------------"
echo "exradwnd3p0.sh.sms - Runs periodically to ingest superobed Level 3.0"
echo "                     NEXRAD radial wind data into BUFR tank database"
echo "--------------------------------------------------------------------"
echo "History: Mar 07 2000 - Original script."
echo "         Last modification date: 11/26/2003"
###########################################################################

set -x

# Make sure we are in the $DATA directory
cd $DATA

msg="LEVEL 3.0 RADIAL WIND SUPEROB PROCESSING HAS BEGUN on `hostname`"
postmsg "$jlogfile" "$msg"

utilscript=${utilscript:-/nwprod/util/ush}
$utilscript/ingest_qmgr.sh
errsc=$?

if [ $errsc -eq 99 ]; then
   msg="Another job with this name is in the system, this ingest job will \
continue but not attempt to ingest any lvl 3 data"
   echo $msg
   postmsg "$jlogfile" "$msg"
   exit $errsc
fi

cat break > $pgmout

MPMD=$utilscript/mpmd.sh

# Go back 23-hrs from latest cycle and attempt to ingest any missing cycles
#  up through and including latest cycle time

ifound=NO
tmhr=24
while [ $tmhr -ge 1 ];
do
   tmhr=`expr $tmhr - 1`
   cdate10=`/nwprod/util/exec/ndate -$tmhr $PDY$cyc`
   grep $cdate10 $USERDIR/radwnd_3p0.history
   err=$?
   if [ $err -ne 0 ]; then
      msg="Radial wind not yet ingested for $cdate10 - check for a raw \
radial wind file"
      postmsg "$jlogfile" "$msg"
      cycr=`echo $cdate10|cut -c9-10`

      radfile="$INDIR/radial_wind_3p0/tar.radwnd.${cycr}"
      if [ -s $radfile ]; then
         cp $radfile .
         tar -xvf tar.radwnd.${cycr}
         YMDH=`cat lasthour `
         if [ "${YMDH}" = "$cdate10" ]; then
            msg="Raw radial wind data for $cdate10 found in $radfile"
            postmsg "$jlogfile" "$msg"
         else
            set +x
            echo
            ls -l $INDIR/radial_wind_3p0/*
            echo
            set -x
            msg="#####Raw radial wind file $radfile has incorrect date ($YMDH)"
            postmsg "$jlogfile" "$msg"
            msg="### Level 3.0 processing UNSUCCESSFUL for $cdate10  \
--> non-fatal"
            postmsg "$jlogfile" "$msg"
            continue
         fi
      else
         set +x
         echo
         ls -l $INDIR/radial_wind_3p0/*
         echo
         set -x
         msg="#####Raw radial wind file $radfile does not exist"
         postmsg "$jlogfile" "$msg"
         msg="### Level 3.0 processing UNSUCCESSFUL for $cdate10  --> non-fatal"
         postmsg "$jlogfile" "$msg"
         continue
      fi

      ifound=YES

      cat<<EOF>poe.cmd
$USHbufr/bufr_tranrad3.sh 1 $cdate10
$USHbufr/bufr_tranrad3.sh 2 $cdate10
$USHbufr/bufr_tranrad3.sh 3 $cdate10
$USHbufr/bufr_tranrad3.sh 4 $cdate10
$USHbufr/bufr_tranrad3.sh 5 $cdate10
$USHbufr/bufr_tranrad3.sh 6 $cdate10
$USHbufr/bufr_tranrad3.sh 7 $cdate10
$USHbufr/bufr_tranrad3.sh 8 $cdate10
EOF

      POE_OPTS="-ilevel 3 -labelio yes -stdoutmode ordered"
# Effective with transition to frost/snow, DO NOT execute a time command
#  with poe command!!!
#########TIMEIT=/usr/bin/timex

      set +x
      echo
      echo --------------------------------------------------------------------
      echo "START POE TO RUN BUFR_TRANRAD3 THREADS TO PUT DATA INTO BUFR"
      echo --------------------------------------------------------------------
      echo
      set -x

      sh $DATA/poe.cmd 2>&1

      set +x
      echo
      echo --------------------------------------------------------------------
      echo "START TRANJB TO PUT BUFR DATA INTO TANKS IN $TANKDIR"
      echo --------------------------------------------------------------------
      echo
      set -x

      export cword=no
      export SCREEN=on
      export RC_FLAG=YES

      totfiles=8
      numfile=0
      num=0

      while [ $numfile -lt $totfiles ]; do
         numfile=` expr $numfile + 1 `
         file=$DATA/bufrout.$cdate10.$numfile
         echo "run tranjb for $file"
         if [ -s $file ];then
            msg="Starting tranjb for task $numfile."
            postmsg "$jlogfile" "$msg"
            sh /nwprod/ush/tranjb $TANKDIR $file
            err=$?
            if [ $err -eq 0 ]; then
               msg="TRANJB completed normally for task $numfile"
               postmsg "$jlogfile" "$msg"
               num=` expr $num + 1 `
            else
               msg="#####PROBLEM - TRANJB FAILED for task $numfile."
               postmsg "$jlogfile" "$msg"
            fi
         else
            msg="#####PROBLEM - task $numfile failed" 
            postmsg "$jlogfile" "$msg"
         fi
      done

      if [ $num -eq $totfiles ]; then
         msg="ALL $num tasks for $cdate10 successfully completed"
         postmsg "$jlogfile" "$msg"
         echo "TRANJB successful for all tasks for $cdate10" >> \
          $USERDIR/radwnd_3p0.history
         sort -d -o radwnd_3p0.history_tempsort $USERDIR/radwnd_3p0.history
         tail -n 48 radwnd_3p0.history_tempsort > $USERDIR/radwnd_3p0.history
      elif [ $num -gt 0 ]; then
         msg="###PROBLEM: only $num tasks out of $totfiles successfully \
completed for $cdate10 --> non-fatal"
         postmsg "$jlogfile" "$msg"
      else
         msg="###PROBLEM: ZERO TASKS OUT OF $totfiles successfully \
completed for $cdate10 - no radial wind data processed --> non-fatal"
         postmsg "$jlogfile" "$msg"
      fi
   else
      set +x
      echo
      echo "Radial wind ALREADY ingested for $cdate10 - move on to next hour"
      echo
      set -x
   fi
done

if [ $ifound = NO ]; then
   msg="No new level 3.0 raw radial wind files found"
   postmsg "$jlogfile" "$msg"
fi

set +x
echo
echo "The past 23 cycles examined - all done"
echo
set -x

# GOOD RUN
set +x
echo " "
echo " ****** LEVEL 3.0 PROCESSING COMPLETED NORMALLY"
echo " ****** LEVEL 3.0 PROCESSING COMPLETED NORMALLY"
echo " ****** LEVEL 3.0 PROCESSING COMPLETED NORMALLY"
echo " ****** LEVEL 3.0 PROCESSING COMPLETED NORMALLY"
echo " "
set -x


# save standard output
cat  break $pgmout break > allout
cat allout
# rm allout

sleep 10

msg="LEVEL 3.0 SUPEROBED RADIAL WIND PROCESSING HAS COMPLETED NORMALLY!"
postmsg "$jlogfile" "$msg"


################## END OF SCRIPT #######################

exit 0
