#!/bin/ksh
# 
# 
# ADAPS 4.4 VERSION
#
#       This is OSW Scripts page version.
#       Refer questions to "gs-w osw scripts"
#
#
# **** rtshiftplot Ver. 1.0 11/20/2004 - jpn ********
# 
# **** Utility to review shifted ratings for real-time sites ***************
# 
clear
echo ' '
echo '      **** rtshiftplot Ver 1.0 - 11/20/2004 ********'
echo ' '
echo '           **********************************'
echo ' '
echo 'This utility will plot v-diagrams and shifted ratings for'
echo 'all District real-time sites using gnuplot.'
echo ''
echo ''
echo ''
echo 'How many seconds would you like to display each plot? '"\c"

read pause

# Source the NWIS environment
. /usr/opt/etc/nwis.profile
. $NWISHOME/util/nwo_set_nwis_env

#clean up from last run if left ungracefully
rm -f $HOST.exsa_rat* ziplog rtshiftplot.station.list rts.file
rm -f gnufile.dat gnuplot.errors load outfile.rdb tempfile rts.errors

#get nwis host
HOST=`hostname | cut -d. -f1`   # extract host if fully-qualified name returned
inputfile=`ls /usr/opt/nwisweb/nwis_host/$HOST/data/cache/$HOST.exsa_rat* 2>rts.errors`
if [ -z "$inputfile" ]; then
  echo ""
  echo "Cannot access real-time ratings file."
  echo ""
 exit
fi

echo ''
echo 'Getting ratings...'
cp $inputfile .
zipfile=`ls $HOST.exsa_rat*.gz` 
gunzip $zipfile
tarfile=`ls $HOST.exsa_rat*.tar`
tar -xvf $tarfile >ziplog

echo ''
echo 'Plotting...'
echo ' '
echo 'Each plot will be displayed for '$pause' seconds.'
echo ' '

# read stations into station list
sed 's/x //' ziplog >rts.tempfile
sed 's/,//' rts.tempfile >rtshiftplot.station.list

while read file one two
do
  # get station info
  grep "STATION AGENCY" $file | cut -f4 -d'"' | read station
  grep "STATION NAME" $file | cut -f2 -d= | sed 's/"/ '$station' - /; s/"//' | read stationid
  echo $stationid 
  headchg -del <$file >outfile.rdb

  # write v-diagram info
  echo 'set data style lines' >> rts.file
  echo 'set grid' >> rts.file
  echo 'set xlabel "Shift"' >> rts.file
  echo 'set ylabel "Stage in feet"' >> rts.file
  echo 'set title ''"'$stationid'"' >> rts.file
  echo 'plot '"'outfile.rdb'"' using 2:1 title '"'$Typetext'"'' with lines >> rts.file
  echo 'pause '$pause >> rts.file

  # write rating info
  echo 'set data style lines' >> rts.file
  echo 'set grid' >> rts.file
  echo 'set xlabel "Discharge"' >> rts.file
  echo 'set ylabel "Stage in feet"' >> rts.file
  echo 'set title ''"'$stationid'"' >> rts.file
  echo 'set logscale' >> rts.file
  echo 'plot '"'outfile.rdb'"' using 3:1 title '"'$Typetext'"'' with lines >> rts.file
  echo 'pause '$pause >> rts.file

  # plot
  cp rts.file gnufile.dat
  gnuplot > load 'gnufile.dat' 2>gnuplot.errors

  # clean up
  rm -f gnufile.dat
  rm -f outfile.rdb
  rm -f rts.file
  rm -f load

done <rtshiftplot.station.list

# final clean up
echo ""
echo "Cleaning up..."
while read file one two
do
rm -f $file
done <rtshiftplot.station.list
rm -f $HOST.exsa_rat* ziplog rtshiftplot.station.list gnuplot.errors rts.errors
rm -f rts.tempfile
echo "Done."
echo ""
