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



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)]

set CVSRevisionAuthor "\$Revision: 1.11 $ \$Author: soliday $"

set env(EPICS_CA_MAX_ARRAY_BYTES) 100000

proc PlotTDS {args} {
    set timestamp ""
    set directory /home/helios/oagData/mpsDumps/
    APSStrictParseArguments {directory}
    APSSetVarAndUpdate status "Plotting"
    if {$directory != "/tmp"} {
	set selection [lindex [.userFrame.top.right.time.listbox curselection] 0]
	if {![llength $selection]} { 
	    APSSetVarAndUpdate status "No timestamp selected"
	    return 
	}
	set timestamp [clock format [clock scan [.userFrame.top.right.time.listbox get $selection]] -format "%Y-%j-%m%d/%H%M%S"]
    }
    set type(1) 4
    set type(2) 2
    set type(3) 9
    set type(4) 3
    set options ""

    if {[file exists [file join $directory $timestamp tds3000_rf.sdds.gz]]} {
        for {set rf 1} {$rf <= 4} {incr rf} {
            global rf${rf}hv
            for {set channel 1} {$channel <= 4} {incr channel} {
                if {[set rf${rf}hv($channel)]} {
                    append options "-col=RF${rf}:HV:scaledTimeAxisWF,RF${rf}:HV:chan${channel}ScaledWaveWF [file join $directory $timestamp tds3000_rf.sdds.gz] \"-topline=RF${rf}:HV Channel \#${channel}\" -graph=line,type=$type($channel) -end "
                }
            }
        }
        for {set rf 1} {$rf <= 4} {incr rf} {
            global rf${rf}ll
            for {set channel 1} {$channel <= 4} {incr channel} {
                if {[set rf${rf}ll($channel)]} {
                    append options "-col=RF${rf}:LL:scaledTimeAxisWF,RF${rf}:LL:chan${channel}ScaledWaveWF [file join $directory $timestamp tds3000_rf.sdds.gz] \"-topline=RF${rf}:LL Channel \#${channel}\" -graph=line,type=$type($channel) -end "
                }
            }
        }
    }
    if {![llength $options]} { 
	APSSetVarAndUpdate status "No RF Scope channels selected"
	return 
    }

    global layout
    eval exec sddsplot "-title=" -layout=$layout -axes=x -ylabel=Division -xlabel=Nanoseconds  $options &

    APSSetVarAndUpdate status "Done"
}

proc PlotCurrent {args} {
    set fileName /tmp/[APSTmpString]
    set outputdir /tmp/
    APSSetVarAndUpdate status "Collecting Data"
    for {set n 1} {$n <= 4} {incr n} {
	append PVs2 "RF${n}:HV:scaledTimeAxisWF RF${n}:HV:chan1ScaledWaveWF RF${n}:HV:chan2ScaledWaveWF RF${n}:HV:chan3ScaledWaveWF RF${n}:HV:chan4ScaledWaveWF "
    }
    for {set n 1} {$n <= 4} {incr n} {
	append PVs2 "RF${n}:LL:scaledTimeAxisWF RF${n}:LL:chan1ScaledWaveWF RF${n}:LL:chan2ScaledWaveWF RF${n}:LL:chan3ScaledWaveWF RF${n}:LL:chan4ScaledWaveWF "
    }
    set PVs2 [join $PVs2 ","]
    APSAddToTempFileList [file join $outputdir tds3000_rf.sdds.gz]
    exec sddswmonitor -erase [file join $outputdir tds3000_rf.sdds.gz] -pv=$PVs2 -steps=1

    PlotTDS -directory /tmp
}

proc FindFiles {args} {
    global StartDay StartMonth StartYear
    global EndDay EndMonth EndYear
    global timeStampsAll timeStamps
    set start [clock scan [format %02d $StartMonth]/[format %02d $StartDay]/[format %4d $StartYear]]
    set end [expr {86400 + [clock scan [format %02d $EndMonth]/[format %02d $EndDay]/[format %4d $EndYear]]}]
    set ts ""
    foreach t $timeStampsAll {
	if {($t >= $start) && ($t <= $end)} {
	    lappend ts [clock format $t -format "%m/%d/%Y %H:%M:%S"]
	}
    }
    set timeStamps $ts
}

APSApplication . -name "TDS 3054 Scope Tool" -version $CVSRevisionAuthor
set status ""
APSScrolledStatus .status -parent .userFrame -textVariable status \
    -packOption "-fill x" -width 20
pack configure .userFrame.status.frame -fill x

pack [frame .userFrame.top] -expand true -fill both
pack [frame .userFrame.top.left] -side left -fill y
pack [frame .userFrame.top.left.top] -anchor n
pack [frame .userFrame.top.left.bottom]
pack [frame .userFrame.top.right] -side right -expand true -fill both

for {set n 1} {$n <= 4} {incr n} {
    set rf1hv($n) 1
    set rf2hv($n) 1
    set rf3hv($n) 1
    set rf4hv($n) 1
    set rf1ll($n) 1
    set rf2ll($n) 1
    set rf3ll($n) 1
    set rf4ll($n) 1
}

APSCheckButtonFrame .rf1hv -parent .userFrame.top.left.top -label "RF1:HV" -buttonList "Channel1 Channel2 Channel3 Channel4" -variableList "rf1hv(1) rf1hv(2) rf1hv(3) rf1hv(4)" -packOption "-side left" -allNone 1
APSCheckButtonFrame .rf2hv -parent .userFrame.top.left.top -label "RF2:HV" -buttonList "Channel1 Channel2 Channel3 Channel4" -variableList "rf2hv(1) rf2hv(2) rf2hv(3) rf2hv(4)" -packOption "-side left" -allNone 1
APSCheckButtonFrame .rf3hv -parent .userFrame.top.left.bottom -label "RF3:HV" -buttonList "Channel1 Channel2 Channel3 Channel4" -variableList "rf3hv(1) rf3hv(2) rf3hv(3) rf3hv(4)" -packOption "-side left" -allNone 1
APSCheckButtonFrame .rf4hv -parent .userFrame.top.left.bottom -label "RF4:HV" -buttonList "Channel1 Channel2 Channel3 Channel4" -variableList "rf4hv(1) rf4hv(2) rf4hv(3) rf4hv(4)" -packOption "-side left" -allNone 1
APSCheckButtonFrame .rf1ll -parent .userFrame.top.left.top -label "RF1:LL" -buttonList "Channel1 Channel2 Channel3 Channel4" -variableList "rf1ll(1) rf1ll(2) rf1ll(3) rf1ll(4)" -packOption "-side left" -allNone 1
APSCheckButtonFrame .rf2ll -parent .userFrame.top.left.bottom -label "RF2:LL" -buttonList "Channel1 Channel2 Channel3 Channel4" -variableList "rf2ll(1) rf2ll(2) rf2ll(3) rf2ll(4)" -packOption "-side left" -allNone 1
APSCheckButtonFrame .rf3ll -parent .userFrame.top.left.top -label "RF3:LL" -buttonList "Channel1 Channel2 Channel3 Channel4" -variableList "rf3ll(1) rf3ll(2) rf3ll(3) rf3ll(4)" -packOption "-side left" -allNone 1
APSCheckButtonFrame .rf4ll -parent .userFrame.top.left.bottom -label "RF4:LL" -buttonList "Channel1 Channel2 Channel3 Channel4" -variableList "rf4ll(1) rf4ll(2) rf4ll(3) rf4ll(4)" -packOption "-side left" -allNone 1

pack [label .userFrame.top.right.label -text "MPS Dump Time Stamps"]
APSScrolledList .time -parent .userFrame.top.right -listvar timeStamps -selectMode single

APSDateTimeAdjEntry .start -parent .userFrame -label "Start Date" -dayVariable StartDay -monthVariable StartMonth -yearVariable StartYear
APSDateTimeAdjEntry .end -parent .userFrame -label "  End Date" -dayVariable EndDay -monthVariable EndMonth -yearVariable EndYear
APSIncrementDateVariables  -dayVariable StartDay -monthVariable StartMonth -yearVariable StartYear  -offset -7 -unit day

set layout "1,4"
APSRadioButtonFrame .layout -parent .userFrame -label "Layout" -buttonList "1x1 1x4 2x2" -valueList "1,1 1,4 2,2" -variable layout -orientation horizontal

APSButton .plot -parent .userFrame -text "Plot" -command "PlotTDS"
APSButton .refresh -parent .userFrame -text "Refresh" -command "FindFiles"
APSButton .plotcurrent -parent .userFrame -text "Plot Present Data" -command "PlotCurrent"

update
APSSetVarAndUpdate status "Searching for files..."
set ts ""
set files [glob -nocomplain -tails -directory /home/helios/oagData/mpsDumps ????-???-????]
foreach f $files {
    set f3 [split $f -]
    if {[string range $f 0 3] < "2003"} { continue }
    set files2 [glob -nocomplain -tails -directory /home/helios/oagData/mpsDumps/$f ??????]
    foreach f2 $files2 {
	if {[file exists /home/helios/oagData/mpsDumps/$f/$f2/tds3000_rf.sdds.gz]} {
	    lappend ts [clock scan "[string range [lindex $f3 2] 0 1]/[string range [lindex $f3 2] 2 3]/[lindex $f3 0] [string range $f2 0 1]:[string range $f2 2 3]:[string range $f2 4 5]"]
	}
    }
}
set timeStampsAll [lsort $ts]

FindFiles
APSSetVarAndUpdate status "Ready"
