#!/bin/ksh
echo 
echo 
# * get_rat4_2 10/14/02 - lgs  **************************************
# *************      Retrieve ADAPS data in rdb format  *************
# *************      and create a Tkg2 rating plot      *************
#              ******************************************
#

# Use current directory to store template by default
# Change this directory if you want to keep the templates in 
# a common storage location, but then also remove the comments
# from the end of this script to keep the working directory clean.

tempdir=$PWD



outdir=$PWD  # Use current directory for output

rundir=$PWD  # run directory (don't change this one!)

#
#
echo  "get_rat for NWIS 4_2"
echo  
echo 'This script will create a Tkg2 rating plot using the Kalispell template'
echo  
echo '            The script needs to be run from your NWIS machine'
echo  
echo 'ENTER STATION NUMBER: '"\c"
read station
echo  
echo 'SELECT DD FOR RATING TO PLOT (Remember to use the OUTPUT DD!)'
pick_dd -n$station | read dd
nwrt2rdb -orating.rdb -e -r -n$station -d$dd
grep "STATION NAME" rating.rdb | cut -f2 -d= | sed 's/"/ '$station' - /; s/"//' > stationid
echo 
echo 'DO YOU WANT TO PLOT A SECOND (previous) RATING? (y/n): '"\c"
read second
if test $second = "y"
then
echo
echo "SELECT SECOND RATING"
echo 
nwrt2rdb -orating2.rdb -e -r -n$station -d$dd
echo 
fi 
echo  
echo 'DO YOU WANT A SHIFT CURVE? (y/n): '"\c"
read shift
if test $shift = "y"
then
echo  
echo 'ENTER DATE OF EFFECTIVE SHIFT (yyyymmdd): '"\c"
read shiftdate
shiftime=120000
echo 
echo 'SELECT THE RATING TO SHIFT'
echo 
nwrt2rdb -oshift.rdb -n$station -d$dd -e -r -b$shiftdate$shiftime
fi
echo 
echo 'ENTER STARTING DATE FOR MEASUREMENT DISPLAY (MM DD YYYY): '"\c"
read startqdate
echo 'ENTER ENDING DATE FOR MEASUREMENT DISPLAY (MM DD YYYY): '"\c"
read endqdate
echo 

echo
rm -f O.SHIFT_ANLY*

#  OLD CODE USING ALTERNATE WAY TO RUN SHIFT ANALYSIS
#
# echo  
# echo 'PARSEING MEASUREMENTS TO POSITIVE AND NEGATIVE'
# echo  
# echo '    ********IMPORTANT - Please read the following!! ********'
# echo 
# echo '  THE FOLLOWING WILL RUN THE ADAPS SHIFT ANALYSIS PROGRAM'
# echo   
# echo '      - PICK THE CURRENT RATING'
# echo '      - MAKE SURE PA IS SET TO CURRENT DIRECTORY,  use *'
# echo '      - MAKE SURE OT IS SET TO FILE'
# echo '      - ACCEPT THE DEFAULT FILE NAME'
# echo  
# echo '      -ANSWER NO (n) TO THE FOLLOWING QUESTIONS: '
# echo '         Do Plot? n'
# echo '         Do another analysis? n'
# echo 
# echo ' wait.......'
# sleep 10     
# nwis shift_anly

echo 'GETTING SHIFT ANALYSIS, PLEASE BE PATIENT AND IGNORE ALL ADAPS PROMPTS...'
sleep 3

{
  echo "PA"         # set path to run directory
  echo "$rundir" 
  echo "ot"         # output to file
  echo "1"
  echo "ST"         # set group
  echo "$station"
  echo "$dd"
  echo "dt"         # set dates
  echo "$startqdate"
  echo "$endqdate"
  echo ""           # run program
  echo ""           # default to choosing rating by effective dates
  echo ""           # default to measurement Q2 if available else Q1
  echo "n"          # don't do a plot
  echo "n"          # don't do another analysis
} | nwis shift_anly


mv O.SHIFT_ANLY* shift_anly
echo 
sed -n '14,100p' shift_anly > shift1
grep "   " shift1 > shift2
rm shift1
sed -e "s/      / /g" shift2 > shift3
rm shift2
sed -e "s/     / /g" shift3 > shift4
rm shift3
sed -e "s/    / /g" shift4 > shift5
rm shift4
sed -e "s/   / /g" shift5 > shift6
rm shift5
sed -e "s/  / /g" shift6 > shift7
rm shift6
sed -e "s/ /	/g" shift7 > shift8
rm shift7
print "BLANK\tNUMBER\tDATE\tTIME\tSTAGE\tDISCHARGE\tRTD\tPCT\tSHIFT" > shift9
print "10S\t4N\t10D\t4N\t6N\t10N\t1S\t6N\t6N" >> shift9
cat shift8 >> shift9
rm shift8
column NUMBER DATE STAGE DISCHARGE SHIFT < shift9 > shift10
rm shift9
echo 'REMOVE ICE MEASUREMENTS? (y/n): '"\c"
read remove
no=1
rm shift_anly
mv shift10 shift_anly.rdb
cp shift_anly.rdb file$no
while test $remove = "y"
do
echo ' '
cat file$no
echo ' '
echo 'ENTER MEASUREMENT NUMBER TO REMOVE: '"\c"
read delete
row NUMBER ne "$delete" < file$no >file$no_out
rm file$no
mv file$no_out file$no
echo 'MORE (y/n): '"\c"
read remove
done
mv file$no shift_anly_out.rdb
rm shift_anly.rdb
row SHIFT gt -.001 < shift_anly_out.rdb > shiftpos.rdb
row SHIFT lt 0 < shift_anly_out.rdb > shiftminus.rdb
echo 
cat shift_anly_out.rdb
rm shift_anly_out.rdb
echo  
echo 'ENTER FIRST MEASUREMENT NUMBER FOR CURRENT YEAR: '"\c"
read number
row NUMBER ge $number <shiftpos.rdb >shiftpos1.rdb
row NUMBER lt $number <shiftpos.rdb >shiftpos2.rdb
row NUMBER ge $number <shiftminus.rdb >shiftminus1.rdb
row NUMBER lt $number <shiftminus.rdb >shiftminus2.rdb
rm shiftpos.rdb
rm shiftminus.rdb
cat stationid | read stationid
if test $shift != "y" && test $second != "y"
then
#cp $tempdir/rating1NS.tkg2 .
sed -e "s/xxxx/$stationid/" rating1NS.tkg2 > rating_out1.tkg2
elif test $shift = "y" && test $second != "y"
then
#cp $tempdir/rating1.tkg2 .
sed -e "s/xxxx/$stationid/" rating1.tkg2 > rating_out1.tkg2
elif test $shift != "y" && test $second = "y"
then
#cp $tempdir/rating2NS.tkg2 .
sed -e "s/xxxx/$stationid/" rating2NS.tkg2 > rating_out1.tkg2
elif test $shift = "y" && test $second = "y"
then
#cp $tempdir/rating2.tkg2 .
sed -e "s/xxxx/$stationid/" rating2.tkg2 > rating_out1.tkg2
fi
rm stationid
echo 
#   
#  Set axis max and min for both graphs and set offset
#  
echo 'LOG AXIS DEFAULTS:'
echo '     Min Stage = 1'
echo '     Max Stage = 10'
echo  
echo '     Rating Offset = 0'
echo  
echo '     Min Q = 10'
echo '     Max Q = 1000'
echo  
echo 'CHANGE LOG AXIS LIMITS? (y/n): '"\c"  
read yaxis
echo  
if test $yaxis = "y"
then
print 'Plot2D / Log :' > axis.inst
echo 'ENTER MINIMUM STAGE: '"\c"
read ymin
print 'y min == '$ymin >> axis.inst
echo 'ENTER MAXIMUM STAGE: '"\c"
read ymax
print 'y max == '$ymax >> axis.inst
echo
echo 'ENTER RATING OFFSET: '"\c"
read offset
print 'y logoffset == '$offset >> axis.inst
echo  
echo 'ENTER MINIMUM DISCHARGE: '"\c"
read xmin
print 'x min == '$xmin >> axis.inst
echo 'ENTER MAXIMUM DISCHARGE: '"\c"
read xmax
print 'x max == '$xmax >> axis.inst
else 
print 'Plot2D / Log :' > axis.inst
print 'y min == 1' >> axis.inst
print 'y max == 10' >> axis.inst
print 'y logoffset == 0' >> axis.inst
print 'x min == 10' >> axis.inst
print 'x max == 1000' >> axis.inst
fi
echo  
echo 'LINEAR AXIIS DEFAULTS:'
echo '     Min Stage = 1'
echo '     Max Stage = 4'
echo '     Min Q = 10'
echo '     Max Q = 1000'
echo  
echo 'CHANGE LINEAR AXIS LIMITS? (y/n): '"\c"
read xaxis
if test $xaxis = "y"
then
echo 'ENTER MINIMUM STAGE: '"\c"
read ymin2
print 'Plot2D / Linear :' >> axis.inst
print 'y min == '$ymin2 >> axis.inst
echo 'ENTER MAXIMUM STAGE: '"\c"
read ymax2
print 'y max == '$ymax2 >> axis.inst
echo 'ENTER MINIMUM DISCHARGE: '"\c"
read xmin2
print 'x min == '$xmin2 >> axis.inst
echo 'ENTER MAXIMUM DISCHARGE: '"\c"
read xmax2
print 'x max == '$xmax2 >> axis.inst
else
print 'Plot2D / Linear :' >> axis.inst
print 'y min == 1' >> axis.inst
print 'y max == 4' >> axis.inst
print 'x min == 10' >> axis.inst
print 'x max == 500' >> axis.inst
fi
#  
#  Using a Text file for Rating Annotation
#
echo  
echo 'DO YOU WANT TO USE A RATING DESCRIPTION TEXT FILE? (y/n): '"\c"
read text
if test $text = "y"
then
# Add Text file Annotation
# 
echo  
echo 'TYPE IN THE PATH AND NAME OF THE TEXT FILE TO USE'
echo 'Example: /kalispell/stations/RATING/TEXT/12370000.txt'
echo  
echo 'ENTER PATH AND FILE: '"\c"
read textpath
cat $textpath > textfile
print 'AnnoText / STATION TEXT :' >> axis.inst
print 'doit == 1' >> axis.inst
print 'yorigin == 296' >> axis.inst
print 'xorigin == 418' >> axis.inst
else
#  Hide Text file Annotation
#  
print 'AnnoText / STATION TEXT :' >> axis.inst
print 'doit == 0' >> axis.inst 
fi
echo  
echo 'PLOT SIZE DEFAULT = 11x8.5'
echo 'Change Size? (y/n)'"\c"
read scaling
if test $scaling = "y"
then
echo 
echo 'SET WIDTH IN INCHES; ENTER WIDTH: '"\c"
read width
echo 'SET HEIGHT IN INCHES; ENTER HEIGHT: '"\c"
read height
else
height=8.5
width=11
fi
echo  
echo  
echo 'Thank you:'  
echo 'Compiling...............  '$stationid '   '
tkg2 -withdraw -importdata -inst=axis.inst -stdout -autoexit rating_out1.tkg2 > rating_out.tkg2
rm axis.inst
rm rating.rdb
rm rating_out1.tkg2
if test $shift = "y"
then
rm shift.rdb
fi
rm shiftminus1.rdb
rm shiftminus2.rdb
rm shiftpos1.rdb
rm shiftpos2.rdb

#  REMOVE THESE COMMENTS IF YOU DON"T STORE THE
#  TEMPLATES IN THE RUN DIRECTORY BUT COPY THEM IN
#  FROM A COMMON STORAGE SPOT

# if test $shift != "y" && test $second != "y"
# then
# rm rating1NS.tkg2
# fi
# if test $shift = "y" && test $second != "y"
# then
# rm rating1.tkg2
# fi
# if test $shift != "y" && test $second = "y"
# then
# rm rating2NS.tkg2
# fi
# if test $shift = "y" && test $second = "y"
# then
# rm rating2.tkg2
# fi
# if test $second = "y"
# then
# rm rating2.rdb
# fi
# if test $text = "y"
# then
# rm textfile
# fi

echo 
echo 'Starting Tkg2........  Output is: rating_out.tkg2'
tkg2 -height=$height -width=$width rating_out.tkg2





