###########################################################################
echo "--------------------------------------------------------------------"
echo "exradwnd2p5.sh.sms - Runs periodically to ingest superobed Level 2.5"
echo "                     NEXRAD radial wind data into BUFR tank database"
echo "--------------------------------------------------------------------"
echo "History: Jun 09 2004 - Original script - V. Krishna Kumar."
echo "         Jun 29 2004 - Modifications to prepare for implementation"
echo "                                       - D. Keyser"
###########################################################################
echo
echo

set -xau

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

msg="LEVEL 2.5 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 ingest any level 2.5 data"
   echo $msg
   postmsg "$jlogfile" "$msg"
   exit $errsc
fi

cat break > $pgmout

INDIR=$INDIR/radial_wind_2p5

ls $INDIR > current_files

cd $DATA

new_files_found=YES

if [ -s $USERDIR/radwnd_2p5.history ] ; then

#  Sort both the list of files in history file (files that have already been
#    processed) and the current list of raw data files (just read in) - the 
#    latter must be sorted because even though the raw files are naturally
#    sorted by time (to the nearest second), subsequent qualifiers in the raw
#    filenames are not sorted in "dictionary" order.  The sorting of both file
#    listings here will ensure that the comparison of filenames in the two
#    listings is done correctly.
#  ---------------------------------------------------------------------------

   sort -d -o radwnd_2p5.history_tempsort $USERDIR/radwnd_2p5.history
   sort -d -o current_files_tempsort current_files
   if [ $? -eq 0 ] ; then
      awk ' { print $1 } ' radwnd_2p5.history_tempsort > \
                           $USERDIR/radwnd_2p5.history
   else
      msg="**FAILURE IN SORT OF FILES IN $USERDIR/radwnd_2p5.history - \
NO SUPEROBED 2.5 RADIAL WIND DATA PROCESSED --> non-fatal"
      postmsg "$jlogfile" "$msg"
      set +x
      echo
      echo $msg
      echo
      set -x
      exit 99
    fi

#  Determine if there are any new files to process, and update history file
#  ------------------------------------------------------------------------

   comm -13 $USERDIR/radwnd_2p5.history current_files_tempsort > new_files
   comm -23 $USERDIR/radwnd_2p5.history current_files_tempsort > old_files
   if [ -s old_files ] ; then
      set +x
      echo
      echo
      echo "NCO has deleted the following files in $INDIR :"
      cat old_files
      echo
      echo
      set -x
      comm -12 $USERDIR/radwnd_2p5.history current_files_tempsort > \
       remaining_files
      mv remaining_files $USERDIR/radwnd_2p5.history
      nfiles=`wc -l old_files | awk '{ print $1 }'`
      msg="$nfiles old files removed from $INDIR since last time this job ran"
      postmsg "$jlogfile" "$msg"
   fi

   if [ ! -s new_files ] ; then
      msg=" No new files to process in $INDIR"
      set +x
      echo
      echo
      echo $msg
      echo
      echo
      set -x
      postmsg "$jlogfile" "$msg"
      new_files_found=NO
   fi
else
    sort -d -o current_files_tempsort current_files
####mv current_files_tempsort new_files
    cp current_files_tempsort new_files
fi

if [ $new_files_found = YES ]; then

set +x
echo
echo
echo "The following new files in $INDIR are available for processing :"
cat new_files
echo
echo
set -x
nfiles=`wc -l new_files | awk '{ print $1 }'`
msg="$nfiles new files in $INDIR are available for processing"
postmsg "$jlogfile" "$msg"


> radwnd_2p5.history_temp

icount_in=0
icount_good=0
jcount0=0
jcount4=0
jcount5=0
jcount6=0
jcount7=0

for filename in `cat new_files`; do

   cp $INDIR/$filename $filename
   icount_in=`expr $icount_in + 1`

# Uncompress the superob2.5 data for all the individual stations

   > $filename.uncom 
   export pgm=radar_uncomp_sup2p5
   set +u
   . prep_step
   set -u

   $EXECbufr/radar_uncomp_sup2p5 $filename $filename.uncompress.out
   err=$?

   if [ $err -eq 0 ]
   then
      icount_good=`expr $icount_good + 1`
      set +x
      echo "$pgm completed NORMALLY for station in $filename"
      set -x
   else
      msg="**ERROR: $pgm FAILED  with r.c. $err for station in $filename - \
DATA IN THIS FILE NOT PROCESSED, MOVE ON TO NEXT FILE --> non-fatal"
      postmsg "$jlogfile" "$msg"
      echo $filename > radwnd_2p5.history_temp
      cat radwnd_2p5.history_temp >> $USERDIR/radwnd_2p5.history
      continue
   fi

   filename_uncom=$filename.uncom 
   isize=`ls -l $filename_uncom  | awk '{ print $5 }'`
   set +x
   echo
   echo "filesize=$isize"
   echo
   set -x
   echo "$filename_uncom" > parmfile

   cat <<nidsparmsEOF >> parmfile
 &namnids nbytes=$isize
 /
nidsparmsEOF


# Convert the uncompressed superob 2.5 individual station data into BUFR
#  and cat onto BUFR file containing all station data

   export pgm=bufr_tranrad2p5
   set +u
   . prep_step
   set -u

   export XLFUNIT_40=$FIXbufr/bufrtab.006
   export XLFUNIT_45=$HOMEbufr/dictionaries/nexrad.tbl
   export XLFUNIT_71=bufrout.$filename
   $EXECbufr/bufr_tranrad2p5 < parmfile >> $pgmout
   err=$? 

   if [ $err -lt 8 ]
   then
      cat bufrout.$filename >> bufrout.total
      errcat=$? 
      if [ $err -eq 0 -a $errcat -eq 0 ]
      then
         set +x
         echo "$pgm completed NORMALLY for station in $filename"
         set -x
      elif [ $err -eq 0 -a $errcat -gt 0 ]
      then
         msg="#####$pgm DID NOT GENERATE OUTPUT BUFR FILE for station in \
$filename, reason unknown --> non-fatal"
         set +x
         echo
         echo
         echo $msg
         echo
         echo
         set -x
#########postmsg "$jlogfile" "$msg"
         jcount0=`expr $jcount0 + 1`
      elif [ $err -eq 4 -a $errcat -gt 0 ]
      then
         msg="#####$pgm DID NOT GENERATE OUTPUT BUFR FILE for station in \
$filename, input file empty --> non-fatal"
         set +x
         echo
         echo
         echo $msg
         echo
         echo
         set -x
#########postmsg "$jlogfile" "$msg"
         jcount4=`expr $jcount4 + 1`
      elif [ $err -eq 5 -a $errcat -gt 0 ]
      then
         msg="#####$pgm DID NOT GENERATE OUTPUT BUFR FILE for station in \
$filename, mismatch between internal center hours IHR and JHR --> non-fatal"
         set +x
         echo
         echo
         echo $msg
         echo
         echo
         set -x
#########postmsg "$jlogfile" "$msg"
         jcount5=`expr $jcount5 + 1`
      elif [ $err -eq 6 -a $errcat -gt 0 ]
      then
         msg="#####$pgm DID NOT GENERATE OUTPUT BUFR FILE for station in \
$filename, MSGCODE is not 136 --> non-fatal"
         set +x
         echo
         echo
         echo $msg
         echo
         echo
         set -x
         postmsg "$jlogfile" "$msg"
         jcount6=`expr $jcount6 + 1`
      elif [ $err -eq 7 -a $errcat -gt 0 ]
      then
         msg="#####$pgm DID NOT GENERATE OUTPUT BUFR FILE for station in \
$filename, packet code is not 27 --> non-fatal"
         set +x
         echo
         echo
         echo $msg
         echo
         echo
         set -x
         postmsg "$jlogfile" "$msg"
         jcount7=`expr $jcount7 + 1`
      fi
      echo $filename >> radwnd_2p5.history_temp
   else
      msg="**ERROR: $pgm FAILED  with r.c. $err for station in $filename - \
NO SUPEROBED 2.5 RADIAL WIND DATA PROCESSED --> non-fatal"
      postmsg "$jlogfile" "$msg"
      echo $filename > radwnd_2p5.history_temp
      cat radwnd_2p5.history_temp >> $USERDIR/radwnd_2p5.history
      exit 99
   fi
done

msg="a total of $icount_in stations were read in"
postmsg "$jlogfile" "$msg"
msg="radar_uncomp_sup2p5 completed NORMALLY for $icount_good stations"
postmsg "$jlogfile" "$msg"
msg="bufr_tranrad2p5 completed NORMALLY for $icount_good stations"
postmsg "$jlogfile" "$msg"
jcount=`expr $jcount0 + $jcount4 + $jcount5 + $jcount6 + $jcount7`
if [ `expr $jcount` -gt 0 ]
then
   msg="bufr_tranrad2p5 did NOT generate an output BUFR file for $jcount \
stations"
   postmsg "$jlogfile" "$msg"
   if [ `expr $jcount0` -gt 0 ]
   then
      msg=" ... $jcount0 stations - reason unknown"
      postmsg "$jlogfile" "$msg"
   fi
   if [ `expr $jcount4` -gt 0 ]
   then
      msg=" ... $jcount4 stations - input file empty"
      postmsg "$jlogfile" "$msg"
   fi
   if [ `expr $jcount5` -gt 0 ]
   then
      msg=" ... $jcount5 stations - mismatch between internal center hours \
IHR and JHR"
      postmsg "$jlogfile" "$msg"
   fi
   if [ `expr $jcount6` -gt 0 ]
   then
      msg=" ... $jcount6 stations - MSGCODE is not 136"
      postmsg "$jlogfile" "$msg"
   fi
   if [ `expr $jcount7` -gt 0 ]
   then
      msg=" ... $jcount7 stations - packet code is not 27"
      postmsg "$jlogfile" "$msg"
   fi
fi

if [ ! -s bufrout.total ]
then

   msg="***WARNING: COMBINED STATION BUFR FILE NOT FOUND, TRANJB DID NOT \
RUN - NO SUPEROBED 2.5 RADIAL WIND DATA PROCESSED"
   postmsg "$jlogfile" "$msg"

else

   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

   set +x
   echo
   echo "run tranjb"
   echo
   set -x

   sh /nwprod/ush/tranjb $TANKDIR $DATA/bufrout.total
   err=$?
   if [ $err -eq 0 ]
   then
      msg="TRANJB completed normally"
      postmsg "$jlogfile" "$msg"
      nfiles=`wc -l radwnd_2p5.history_temp | awk '{ print $1 }'`
      msg="$nfiles new files added to $USERDIR/radwnd_2p5.history"
      postmsg "$jlogfile" "$msg"
      cat radwnd_2p5.history_temp >> $USERDIR/radwnd_2p5.history
   else
      msg="**ERROR: TRANJB FAILED with r.c. $err - NO SUPEROBED 2.5 RADIAL \
WIND DATA PROCESSED --> non-fatal"
      postmsg "$jlogfile" "$msg"
      exit 99
   fi
fi

fi # new_files_found=YES

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


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

sleep 10

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


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

exit 0
