#!/bin/sh  
# \
exec oagwish "$0" "$@"

#
# $Log: not supported by cvs2svn $
# Revision 1.5  1996/11/04 16:59:42  borland
# Renamed APSMakeDateEntry etc. to APSDateEntry etc.
#
# Revision 1.4  1996/09/19 21:51:00  borland
# Changed from using "exec wish" to "exec oagwish".
#
# Revision 1.3  1996/08/14 18:26:44  borland
# Added sort of filenames to get data in the right order on the plot when there
# are multiple files.
#
# Revision 1.2  1996/03/28  23:01:32  borland
# Changed to use -presparse option on sddsplot.
#
# Revision 1.1  1996/03/13  00:37:02  borland
# First version.
#
#

set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]
APSDebugPath

set CVSRevisionAuthor "\$Revision: 1.6 $ \$Author: borland $"

set dataDir /home/helios/oagData/monitoring/processWater

proc ProcessWaterReviewOverview {} {
    return "This is a utility for simple processing and display of data from the process water system."
        }

APSApplication . -name ProcessWaterReview -version $CVSRevisionAuthor \
        -overview [ProcessWaterReviewOverview]
set statusText "Ready."
APSScrolledStatus .status -parent .userFrame -textVariable statusText

proc SetStatusText {text} {
    global statusText 
    set statusText  "$text"
    update
}

proc MakeDateWidget {widget args} {
    global day month year
    set parent ""
    APSStrictParseArguments {parent}
    APSDateTimeEntry ${widget} -parent $parent \
      -dayVariable day -monthVariable month \
      -yearVariable year -label "Date (year, month, day): "
    APSDateBreakDown -dayVariable day -yearVariable year \
      -monthVariable month -twoDigitYear 0 -leadingZeros 0
}


proc MakeSmoothWidget {widget args} {
    global doSmooth smoothPasses
    set parent ""
    APSStrictParseArguments {parent}

    APSFrame ${widget} -parent $parent -label "Smoothing" \
          -contextHelp "Controls for smoothing of data before display" 
    APSFrameGrid .fg -parent $parent${widget}.frame -xList {f1 f2}
    set w $parent${widget}.frame.fg
    APSRadioButtonFrame .onoff -parent $w.f1 -label Smooth \
          -variable doSmooth -buttonList {on off} -valueList {1 0} \
          -contextHelp "Turns smoothing on and off." -orientation horizontal
    APSLabeledEntry .passes -parent $w.f2 -label Passes: -width 4 \
          -textVariable smoothPasses -packOption "-expand 1 -side top" \
          -contextHelp "Enter the number of times the smoother will pass over the data."

}

proc MakeButtonRow {widget args} {
    set parent ""
    APSStrictParseArguments {parent}

    APSFrame $widget -parent $parent -label ""
    APSButton .plotday -parent $parent$widget.frame \
      -text "PLOT DAY" -command PlotDailyData \
      -contextHelp "Plots data for the day specified."
    APSButton .plothour -parent $parent$widget.frame  \
      -text "PLOT LAST HOUR" -command PlotLastHoursData \
      -contextHelp "Plots data for the the last hour for today."
}


proc FindDataFile {args} {
    set dataDir ""
    set rootname ""
    set year 0
    set day 0
    set month 0
    APSStrictParseArguments {year day month dataDir rootname}
    set year [APSMakeFourDigitYear $year]
    scan month "%ld" $month
    scan day "%ld" $day
    set month [format %02ld $month]
    set day [format %02ld $day]
    set fileList [lsort [glob -nocomplain $dataDir/${rootname}-$year-???-${month}${day}.gz]]
    set dataFile /tmp/[APSTmpString]
    if ![llength $fileList] {
        set fileList \
          [lsort [glob -nocomplain \
                    $dataDir/${rootname}-$year-???-${month}${day}.\[0-9\]\[0-9\]\[0-9\]\[0-9\]]]
        if ![llength $fileList] {
            return ""
        }
    }
    if [llength $fileList]>1 {
        set dataFile /tmp/[APSTmpString]
        eval exec sddscombine $fileList $dataFile -merge 
    } else {
        set fileName [lindex [lsort $fileList] end]
        if [string compare [file extension $fileName] .gz]==0 {
            set dataFile $dataFile.gz
        }
        eval exec ln -s $fileName $dataFile
    }
    # always return either a link to the datafile or to a copy of the data
    # other procedures will delete or replace this file
    return $dataFile
}

proc PlotDailyData {} {
    global doSmooth smoothPasses year month day dataDir
    set file [FindDataFile -dataDir $dataDir -year $year -month $month \
                -day $day -rootname processWater]
    if [string length $file]==0 {
        SetStatusText "No data found."
        return
    }
    set data [ComputeNetFlow -fileName $file]
    if $doSmooth {
        if ![SmoothNetFlowData -fileName $data -passes $smoothPasses] {
            return
        }
        PlotNetFlowData -fileName $data -title "Data smoothed $smoothPasses times"
    } else {
        PlotNetFlowData -fileName $data
    }
    after 60000 "exec rm $data"
}

proc PlotLastHoursData {} {
    global doSmooth smoothPasses year month day dataDir
    set file [FindDataFile -dataDir $dataDir -year $year -month $month \
                -day $day -rootname processWater]
    if [string length $file]==0 {
        SetStatusText "No data found."
        return
    }
    set timeList [split [exec date +%H:%M:%S] :]
    scan [lindex $timeList 0] "%ld" hour
    scan [lindex $timeList 1] "%ld" min
    scan [lindex $timeList 2] "%ld" sec
    set time1 [expr ($sec/60.0+$min)/60.0+$hour]
    set time0 [expr $time1-1.0]
    if $time0<0 {
        set time0 0
    }
    set data [ComputeNetFlow -fileName $file -lowerTime $time0 -upperTime $time1]
    if $doSmooth {
        if ![SmoothNetFlowData -fileName $data -passes $smoothPasses] {
            return
        }
        PlotNetFlowData -fileName $data -title "Data smoothed $smoothPasses times"
    } else {
        PlotNetFlowData -fileName $data
    }

    after 60000 "exec rm $data"
}

proc PlotNetFlowData {args} {
    global sparsingInterval plotDevice sameScaleY layoutChoice printerName labelSizeOption env
    set fileName ""
    set title ""
    APSStrictParseArguments {fileName title}
    if ![file exists $fileName] {
        return
    }

    lappend extraOptions "-title="
    lappend extraOptions "-topline=$title"
    lappend extraOptions $layoutChoice 
    lappend extraOptions -presparse=$sparsingInterval
    lappend extraOptions -device=$plotDevice
    lappend extraOptions $labelSizeOption
    if $sameScaleY {
        lappend extraOptions -same=x,y,global
    } else {
        lappend extraOptions -same=x,global
    }
    if [string compare $plotDevice motif] {
        eval exec sddsplot -device=$plotDevice -separate $extraOptions $fileName -ticks=xtime \
          -column=Time,*NetFlow | lpr -P$printerName &
    } else {
        eval exec sddsplot -separate $extraOptions $fileName -ticks=xtime -column=Time,*NetFlow &
    }
    SetStatusText "Plots launched."
}

proc SmoothNetFlowData {args} {
    set fileName ""
    set passes 1
    APSStrictParseArguments {fileName passes}
    if ![file exists $fileName] {
        return 0
    }
    set tmpfile /tmp/[APSTmpString]
    SetStatusText "Smoothing in progress..."
    if [catch \
          {exec sddssmooth $fileName $tmpfile -columns=*NetFlow -passes=$passes} result] {
        SetStatusText "Error smoothing data: $result"
        return 0
    }
    if [catch {exec mv $tmpfile $fileName} result] {
        SetStatusText "Error: $result"
        return 0
    }
    return 1
}

proc ComputeNetFlow {args} {
    set fileName ""
    set lowerTime 0
    set upperTime 24
    APSStrictParseArguments {fileName lowerTime upperTime}
    if ![file exists $fileName] {
        return ""
    }
    set dataOutput /tmp/[APSTmpString]
    SetStatusText "Computing net flows..."
    if [catch \
          {exec sddsprocess $fileName $dataOutput \
             -filter=column,TimeOfDay,$lowerTime,$upperTime \
             "-define=column,%sNetFlow,%sSupplyFlow %sReturnFlow -,select=*SupplyFlow,edit=%/SupplyFlow//,units=GPM"} result] {
        SetStatusText "Error: $result"
        return ""
    }
    return $dataOutput
}

proc MakePlotControls {widget args} {
    global sparsingInterval plotDevice sameScaleY layoutChoice printerName labelSizeOption env
    set sparsingInterval 1
    set plotDevice motif
    set sameScaleY 0
    set layoutChoice -layout=1,1
    set labelSizeOption -labelsize=0.02
    if [info exists env(PRINTER)] {
        set printerName $env(PRINTER)
    } else {
        set printerName mcr1
    }

    set parent "" 
    APSStrictParseArguments {parent}

    APSFrame $widget -parent $parent -label "Plot controls"
    set parent $parent$widget.frame

    APSFrameGrid .grid -parent $parent \
      -yList {y1 y2} -width 10 \
      -xList {x1 x2 x3 x4}
        
    APSRadioButtonFrame .layout -parent ${parent}.grid.x1.y1 \
      -label Layout -orientation vertical \
      -variable layoutChoice \
      -buttonList {1x1 1x2 2x1 2x2 3x3 4x4 5x5} \
      -valueList {-layout=1,1 -layout=1,2 -layout=2,1 -layout=2,2 -layout=3,3 -layout=4,4 -layout=5,5} \
      -contextHelp {Choose the number of plot panels horizontally and vertical on each page.}
    APSCheckButtonFrame .misc  -parent ${parent}.grid.x2.y1 \
      -label Misc. -orientation vertical \
      -variableList {sameScaleY} -buttonList {"Same y scales."} 
    APSRadioButtonFrame .sparse  -parent ${parent}.grid.x3.y1 \
      -label Sparsing -orientation vertical \
      -variable sparsingInterval \
      -buttonList {None 2 4 8 16 32 64 128} \
      -valueList {1 2 4 8 16 32 64 128} \
      -contextHelp "For large amounts of data, a larger sparsing interval will result in\
faster display, but at the expense of only seeing a sample of the points."
    APSRadioButtonFrame .device  -parent ${parent}.grid.x4.y1 \
      -label Device -orientation vertical \
      -variable plotDevice \
      -buttonList {X-windows "B&W Postscript" "Color Postscript"} \
      -valueList {motif postscript cpostscript} \
      -contextHelp "Choose the plotting device.  Postscript is for delivery to a printer only."
    APSLabeledEntry .printer -parent ${parent}.grid.x4.y2 \
        -label "Printer: " -textVariable printerName -width 10
    APSRadioButtonFrame .labelsize -parent ${parent}.grid.x2.y2 \
        -label "Label size" -orientation vertical -variable labelSizeOption \
        -buttonList {normal +15% +30% +45% +60%} \
        -valueList {-labelsize=0.02 -labelsize=0.023 -labelsize=0.026 -labelsize=0.029 -labelsize=0.032} \
        -contextHelp "For layouts with many panels, the labels may be hard to read.  You can increase the size of the labels using these options."
}

set doSmooth 1
set smoothPasses 1
set day 0
set month 0
set year 0

MakeDateWidget .date -parent .userFrame
MakeSmoothWidget .smooth -parent .userFrame
MakePlotControls .plotcontrol -parent .userFrame 
MakeButtonRow .ops -parent .userFrame
