#!/bin/ksh
version="5.0"

#******************************************************************************
# Office of Surface Water
#******************************************************************************
#
#       This is OSW Scripts page version.
#       Refer questions to "gs-w osw scripts"
#
#
#----------------------------------------------------------------------------------
# DISTRICT DEFAULT VARIABLES SET BELOW

# printer default
printer=$LPDEST

# Set tempdir to the name of directory containing templates if running from central location.
# Default is to use the users working directory ($PWD).
#tempdir=/home/dbawork/SCRIPTS
tempdir=$PWD

# Outpt plots sent to the (s)creen, (p)rinter, or (f)iles
output='s'

# Default height and width of plots
height=8.5
width=11

# END DISTRICT DEFAULT VARIABLES
#---------------------------------------------------------------------------------

#
#
#
# program: loadplot 
# purpose: Review yearly accumulated load vs. accumulated discharge plots to see yearly changes in sediment
#          transport and detect possible analyst bias or other problems in worked sediment-discharge records
#
#
# history:                               version 0.50beta
#          02/15/05 Original coding - WJW
#          03/17/05 Fixed bugs when non-primary DDs selected. Removed unneeded time zone option 
#                   and Q processor check -JPN
#          08/10/05 Minor annotation tweaks - JPN
#
#    --------------------- v. 1.0 release --------------------------------------
#          03/27/07 updated for NWIS parameter_cd/parm_cd change - WJW
#
#    --------------------- v. 1.1 release --------------------------------------
#          03/29/10 fixed bug causing 0 loads to be interpreted as missing values - was causing Q totals to come out wrong. -WJW
#
#    --------------------- v. 1.2 release --------------------------------------
#          02/20/13 Finished updating for NWIS 5.0/Oracle, added some paths, and showing load/Q totals on stdout. - WJW
#
#
#            No command line options with original coding
	
#variable to clean up/not clean up files - set to n for testing purposes
deletevar=y

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

# Get table names for later sql statements
db_no=01
sitetbl=sitefile`/usr/local/bin/tsql $NWISDB -Gnwis_select "select suffix_tx from master_table where ddl_nm='SITEFILE_##' and db_no='${db_no}'"`
loctbl=loc`/usr/local/bin/tsql $NWISDB -Gnwis_select "select suffix_tx from master_table where ddl_nm='LOC_##' and db_no='${db_no}'"`
ddtbl=dd`/usr/local/bin/tsql $NWISDB -Gnwis_select "select suffix_tx from master_table where ddl_nm='DD_##' and db_no='${db_no}'"`
tstbl=ts_comp_inst`/usr/local/bin/tsql $NWISDB -Gnwis_select "select suffix_tx from master_table where ddl_nm='TS_COMP_INST_##' and db_no='${db_no}'"`
ddtstbl=dd_ts_comp_inst`/usr/local/bin/tsql $NWISDB -Gnwis_select "select suffix_tx from master_table where ddl_nm='DD_TS_COMP_INST_##' and db_no='${db_no}'"`

#trap used to verify exit from script and to clean up files when interrupt key used
trap '
print "Do you really want to quit loadplot? (CR=y) \c"
read quitans
case $quitans in
 n | N | no | NO | No | nO)
  quitans=
  print "Continuing loadplot";;
 *)
  print "Cleaning up files...exiting loadplot"
   if test $deletevar = "y"
   then
   rm -f discharge.rdb discharge1.rdb discharge2.rdb discharge3.rdb
   rm -f load.rdb load1.rdb load2.rdb load3.rdb loadinv loadplot.errors
   rm -f masterdata.rdb stationid year*.rdb year*.tmp.rdb 
   rm -f year*.tmp1.rdb

   print "Bye"
   exit
   else
   print "Bye"
   exit
   fi
esac
  ' INT

# print 'tempdir '$tempdir
# check for correct template location

if test -r $tempdir/loadplot.tkg2
 then
 print 'Tkg2 template found.'
 else
 print ''
 print 'Tkg2 template not found. Template (loadplot.tkg2) must be in the directory'
 print 'defined by variable "tempdir" on about line 20 of the loadplot script.'
 print 'The script default is for the template to be in the same directory as' 
 print 'loadplot, assuming the script is in the users current working directory.'
 print ''
 exit
 fi


# ----------------------------- cordialities ----------------------------------
clear
print "Version $version"
print "######################################################################"
print "#      loadplot -  Create yearly accumulated load vs Q plots.        #"
print "#                                                                    #"
print "######################################################################"
print
print


# ----------------------------- get station -----------------------------------
station=$1
if [ -z "$station" ]; then
 until [ $station ]
 do
  print "\nEnter station number (q=quit): \c"
  read station
  case $station in
   q | qu | quit | ex | Q | QU | QUIT | EX )
    exit;;
  *) ;;
  esac

  statest=`/usr/local/bin/tsql $NWISDB -Gnwis_select  "select s.station_nm from sitefile_01 s 
        where s.agency_cd='USGS' and s.site_no='$station'"`

  case $statest in
  '' ) 
    print $station ' does not exist in the site file. Please try again or enter q to quit.'
    print
    station='';;
  *) ;;
  esac
 done
fi





# ---------------------------- get dvinv table of available load data for site ------------------------------

dvinv -m80155 -aUSGS -z1 $station > loadinv
print 'The folowing load data is in the database for this site:'
cat loadinv
echo
# ----------------------------- use primary load DD's? -------------------------------------------------

print "\nUse primary DDs for data retrieval (Y) or manually pick DDs (N)? (CR=Y): \c"
read primary
case $primary in
    q | qu | quit | ex | Q | QU | QUIT | EX )
     exit;;
    n | N | no | No | nO | NO )
     primary=n;;
    * )
     print 'Using primary DDs for data retrievals.'
     sleep 3
     primary=y;;
esac



# ----------------------------- if not primary than get load and discharge DD's ----------------------------------------

if test $primary = "n"
then

  print "\nSelect load DD..."
  pick_dd -n$station | read ddload

until [ $goodq ]
do
  print "\nSelect discharge DD..."
  pick_dd -n$station | read ddq
# check for discharge
qtest=`/usr/local/bin/tsql $NWISDB -Gnwis_select  "select d.parm_cd from sitefile_01 s, loc_01 l, dd_01 d
        where s.agency_cd='USGS' and s.site_no='$station' and
        l.site_id=s.site_id and d.loc_id=l.loc_id and
        d.dd_nu='$ddq'"`

case $qtest in
  00060 | 00061 | 30208 | 50042 | 99060 | 99061 )
  goodq='y'
  ;;
  * )
  print ''
  print 'This is not a recognized discharge DD. Please try again.'
  print ''
  sleep 3 
  clear
  goodq=''
  ;;
esac

done
fi
clear
print 'The folowing load data is in the database for this site:'
cat loadinv
#-------------------------get most recent water year and period to pull data from----------
echo

    until [ "$begm" ]
    do
 print 'Enter most recent water year (yyyy) with load data: '"\c"
 read wateryear
echo
echo
 print "\nEnter number of previous years to retrieve data for (1-20): \c"
 read period

   print
    begm='10'
    begd='01'
    begya=`expr $wateryear - $period`
    begy=`expr $begya - 1`
    endm='09'
    endd='30'
    endy=$wateryear
    begtime=000000
    endtime=235959

    print 'Data will be pulled from ' $begm $begd $begy 'to ' $endm $endd $endy
    print 'Is this correct? (CR=y)'"\c"
    read question
    case $question in
    n) begm='';;
    N) begm=;;
    esac
    done
    begtime=000000
    endtime=235959


# ----------------------------- get other options -----------------------
# 
#check=$1
#if [ -z "$check" ]; 
#then
until [ $leave ]
do
until [ $loop  ]
do
clear
print  'USING:'
print  'Station ........ '$station ' '$statest
if test $primary = "n"
then
print  'Discharge DD.. '$ddq
print  'Load DD....... '$ddload
else
print  'Discharge DD.. Primary'
print  'Load DD....... Primary'
fi
print  ''
print  'Load data available in the database for this site:'
cat loadinv 
print  ''
print  'CURRENT PROGRAM OPTIONS:'
print  ''
echo   '--------------  Plot Options --------------------------------'
print  '1. Station ................................................. '$station
print  '2. Period .................................................. '$begm $begd $begy 'to '$endm $endd $endy
print  '3. Plot height ............................................. '$height
print  '   Plot width .............................................. '$width
print  '4. Plots sent to the (s)creen, (p)rinter, or (f)iles ....... '$output
 if test $output = "p"
 then
print  '5. Default printer.......................................... '$printer
print  ''
print  'Enter 1-5 or <CR> to plot: '"\c"
read choice
 else
print  'Enter 1-4 or <CR> to plot: '"\c"
read choice
 fi

case $choice in
"") loop='n'
    choice='n';;
q | qu | quit | ex | Q | QU | QUIT | EX )
    exit;;
*)  loop=''
esac

#-------------------------------------change station--------------------------------------
if test $choice = "1"
then
 station=""
 until [ $station ]
 do
  print "\nEnter new station number (q=quit): \c"
  read station
  case $station in
   q | qu | quit | ex | Q | QU | QUIT | EX )
    exit;;
  *) ;;
  esac

  statest=`/usr/local/bin/tsql $NWISDB -Gnwis_select  "select s.station_nm from sitefile_01 s 
        where s.agency_cd='USGS' and s.site_no='$station'"`

  case $statest in
  '' ) 
    print $station ' does not exist in the site file. Please try again or enter q to quit.'
    print
    station='';;
  *) ;;
  esac
 done
dvinv -m80155 -aUSGS -z1 $station > loadinv
print 'The folowing load data are in the database for this site:'
cat loadinv

# ----------------------------- use primary load DD's for new site?

print "\nUse primary DDs for data retrieval(Y) or manually pick DDs (N)? (CR=Y): \c"
read primary
case $primary in
    q | qu | quit | ex | Q | QU | QUIT | EX )
     exit;;
    n | N | no | No | nO | NO )
     primary=n;;
    * )
     print 'Using primary DDs for data retrievals.'
     primary=y;;
esac

if test $primary = "n"
then
  print "\nSelect load DD..."
  pick_dd -n$station | read ddload

goodq=""
until [ $goodq ]
do
  print "\nSelect discharge DD..."
  pick_dd -n$station | read ddq
# check for discharge
qtest=`/usr/local/bin/tsql $NWISDB -Gnwis_select  "select d.parm_cd from sitefile_01 s, loc_01 l, dd_01 d
        where s.agency_cd='USGS' and s.site_no='$station' and
	l.site_id=s.site_id and d.loc_id=l.loc_id and
	d.dd_nu='$ddq'"`

case $qtest in
  00060 | 00061 | 30208 | 50042 | 99060 | 99061 )
  goodq='y'
  ;;
  * )
  print ''
  print 'This is not a recognized discharge DD. Please try again.'
  print ''
  sleep 3 
  clear
  goodq=''
  ;;
esac

done
fi

fi

# ----------------------------- get water year -----------------------
if test $choice = "2"
then
echo
echo
echo
echo
echo
echo
begm=''
    until [ "$begm" ]
    do
print "Enter most recent water year (yyyy) with load data: \c"
 read wateryear
echo
echo
 print "\nEnter number of previous years to retrieve data for (1-20): \c"
 read period
    print
    begm='10'
    begd='01'
    begya=`expr $wateryear - $period`
    begy=`expr $begya - 1`
    endm='09'
    endd='30'
    endy=$wateryear
    begtime=000000
    endtime=235959

    print 'Data will be pulled from ' $begm $begd $begy 'to ' $endm $endd $endy
    print 'Is this correct? (CR=y)'"\c"
    read question
    case $question in
    n) begm='';;
    N) begm=;;
    esac
    done
    begtime=000000
    endtime=235959

fi

#----------------------------------change output option-----------------------------
if test $choice = "4"
then
  print
  print '  Do you want plots sent to the (s)creen, (p)rinter, or (f)iles (CR=s)? \c'
  read output
  case $output in
    q | qu | quit | ex | Q | QU | QUIT | EX )
      exit;;
    p | P )
    output="p";;
    f | F )
      output="f";;
    s | S | * )
      output="s";;
    esac
fi

#--------------------------------change printer if output=printer-------------------
if test $choice = "5"
then
     print 'Enter new printer for plots: (CR='$printer') '"\c"
      read newprinter
      if [ "$newprinter" ]
      then
      printer=$newprinter 
      fi
fi


#------------------------------------------------change plot dimensions----------------------
if test $choice = "3"
then
  print
  print 'Enter plot height: '"\c"
  read height
  print 'Enter plot width: '"\c"
  read width
fi



#fi # end of if-loop for changing default options-only if command line instituted


done # end of menu loop




#-------------------------- getting NWIS data -------------------
print 'Station '$station
print ''
print 'Retrieving data...'



if test $primary = "y"
then
print '... discharge data'
/usr/local/bin/nwts2rdb -odischarge.rdb -tdv -s00003 -p00060 -b$begy$begm$begd -e$endy$endm$endd -n$station 
print '... load data'
/usr/local/bin/nwts2rdb -oload.rdb -tdv -s00003 -p80155 -b$begy$begm$begd -e$endy$endm$endd -n$station 
else
print '... discharge data'
/usr/local/bin/nwts2rdb -odischarge.rdb -tdv -s00003 -b$begy$begm$begd -e$endy$endm$endd -n$station -d$ddq
print '... load data'
/usr/local/bin/nwts2rdb -oload.rdb -tdv -s00003 -b$begy$begm$begd -e$endy$endm$endd -n$station -d$ddload
fi

#---------------------------------------make dummy yearly files for 21 years' worth of data-------------------
print "DATE\ttotal_DISCHARGE\ttotal_LOAD" > year0.rdb
print "8D\t100N\t100N" >> year0.rdb
cp year0.rdb year1.rdb
cp year1.rdb year2.rdb
cp year1.rdb year3.rdb
cp year1.rdb year4.rdb
cp year1.rdb year5.rdb
cp year1.rdb year6.rdb
cp year1.rdb year7.rdb
cp year1.rdb year8.rdb
cp year1.rdb year9.rdb
cp year1.rdb year10.rdb
cp year1.rdb year11.rdb
cp year1.rdb year12.rdb
cp year1.rdb year13.rdb
cp year1.rdb year14.rdb
cp year1.rdb year15.rdb
cp year1.rdb year16.rdb
cp year1.rdb year17.rdb
cp year1.rdb year18.rdb
cp year1.rdb year19.rdb
cp year1.rdb year20.rdb

#-----------------------------------------combine and reduce data files to useful columns-------------------------
print 'Formatting data'
/usr/local/bin/column DATE VALUE < discharge.rdb > discharge1.rdb
/usr/local/bin/headchg -del < discharge1.rdb > discharge2.rdb
print "DATE\tDISCHARGE" > discharge3.rdb
print "8D\t100N" >> discharge3.rdb
cat discharge2.rdb >> discharge3.rdb

/usr/local/bin/column DATE VALUE < load.rdb > load1.rdb
/usr/local/bin/headchg -del < load1.rdb > load2.rdb
print "DATE\tLOAD" > load3.rdb
print "8D\t100S" >> load3.rdb
cat load2.rdb >> load3.rdb


/usr/local/bin/jointbl DATE discharge3.rdb < load3.rdb > masterdata.rdb

#-----------------------------------------split up data into water years ------------------------------------
count=$period
cp  $tempdir/loadplot.tkg2 temp.tkg2
while ((count >= 0))
do
edate=`expr $wateryear - $count`
sdate=`expr $edate - 1`
/usr/local/bin/row DATE ge $sdate$begm$begd and DATE le $edate$endm$endd < masterdata.rdb > year$count.tmp.rdb
/usr/local/bin/row LOAD ne null < year$count.tmp.rdb > year$count.tmp11.rdb
/usr/xpg4/bin/sed 's/100S/100N/g' <year$count.tmp11.rdb > year$count.tmp1.rdb
print "Generating totals for $edate WY"
/usr/local/bin/reducetbl < year$count.tmp1.rdb -s total > year$count.rdb 2>loadplot.errors
if test -s year$count.rdb
then
sed -e "s/No year $count data/$edate WY/" < temp.tkg2 > temp1.tkg2
/usr/xpg4/bin/grep -ve "^#" -ve "^100n" < year$count.rdb
echo
mv temp1.tkg2 temp.tkg2
else
echo "No data detected for year..."
echo
fi
((count=$count-1))
done

#----------------------------------------------------creat text annotation ---------------------------------
grep "STATION NAME" discharge.rdb | cut -f2 -d= | sed 's/"/ '$station' - /; s/"//' > stationid
print "" >> stationid
print "Yearly load vs discharge for period $begm-$begd-$begy to $endm-$endd-$endy" >> stationid

#---------------------------------------------------create plot--------------------------------------------
if test $output = "p"
then
 print ''
 print 'You have chosen to autoprint.'
 print 'Plot will appear and print automatically.'
 print ''
fi
print 'Starting tkg2 plotting.'
echo
echo

print 'Generating file: loadplot.'$wateryear'.'$station'.tkg2'
 
/usr/local/bin/tkg2 -stdout -autoexit -importdata -height=$height -width=$width -optimize -withdraw temp.tkg2 > loadplot.$wateryear.$station.tkg2 2>loadplot.errors
rm -f temp.tkg2
echo
echo

# then print if desired
if test $output = "p"
then
print 'Printing plot at printer '$printer'.'
#print $output 'should be p'
 /usr/local/bin/tkg2 -batch -importdata -destination=$printer -height=$height -width=$width loadplot.$wateryear.$station.tkg2 2>loadplot.errors
fi


chmod 755 *.tkg2

# Show plot if "s" option used
if test $output = "s"
then
print
print "Launching tkg2..."
/usr/local/bin/tkg2 -height=$height -width=$width loadplot.$wateryear.$station.tkg2
fi


#-------------------------- done, clean up -------------------------------------


if test $deletevar = "y" 
then
   rm -f discharge.rdb discharge1.rdb discharge2.rdb discharge3.rdb
   rm -f load.rdb load1.rdb load2.rdb load3.rdb loadplot.errors
   rm -f masterdata.rdb stationid year*.rdb year*.tmp.rdb 
   rm -f year*.tmp1.rdb
fi


# keep plot files if "f" (file) option used
if test $output = "f"
    then
    print
    print "The tkg2 file can be found in " $DIR
else
    clear
    print "\nDo you want to delete the loadplot file loadplot.$wateryear.$station.tkg2? (CR=y) \c"
     read clean
   case $clean in
    N | n | no | NO | No | nO)
    print
    print "The tkgs2 files can be found in " $PWD
     ;;
     * )
     rm -f loadplot.$wateryear.$station.tkg2
     ;;
     esac
fi
print "Plot any more data? (CR=n)? \c"
read cont
case $cont in
 y | Y | yes | YES)
    loop=''
    leave='';;
 *)leave=y;;
esac

done # end of script loop
print "Done"
rm -f loadinv
exit
