#!/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)]
APSDebugPath
set CVSRevisionAuthor "\$Author: borland $"

APSApplication . -name SRConverterGlitchReview \
  -overview "Allows reviewing glitch data for storage ring converters"

set status ""
APSScrolledStatus .status -parent .userFrame -textVariable status -height 10 -width 45

proc setStatus {text} {
    APSSetVarAndUpdate status $text
}

set dataDir /home/helios/oagData/glitchLogs/SRConverters
set converterList0 [lsort [glob -nocomplain $dataDir/S*:*]]
set converterList ""
foreach item $converterList0 {
    if [file isdirectory $item] {
        lappend converterList [string trim [file tail $item]]
    }
}

APSFrameGrid .fg -parent .userFrame -yList {top bottom}

APSScrolledList .converterList -parent .userFrame.fg.top \
  -height 15 -itemList $converterList \
  -callback ConverterChoiceCallback \
  -selectMode single -packOption "-side left"
.userFrame.fg.top.converterList.listbox configure -width 10

APSScrolledList .fileList -parent .userFrame.fg.top \
  -height 15 -itemList <None> -callback FileChoiceCallback \
  -selectMode single -packOption "-side left" 
set fileListWidget .userFrame.fg.top.fileList.listbox
.userFrame.fg.top.fileList.listbox configure -width 30

APSButton .showFiles -parent .userFrame.fg.bottom -text "Show Files" \
    -command ShowFiles
APSButton .showRecords -parent .userFrame.fg.bottom -text "Show Records" \
    -command ShowRecords
APSButton .plotData -parent .userFrame.fg.bottom -text "Plot Data" \
    -command PlotData

set converterChoice ""
set fileChoice ""
proc ConverterChoiceCallback {item doubleClick} {
    global converterChoice 
    set converterChoice $item
    if $doubleClick {
        ShowFiles
    }
}

proc FileChoiceCallback {item doubleClick} {
    global converterChoice dataDir fileChoice
    set fileChoice $dataDir/$converterChoice/[lindex [split $item] 0]
    if $doubleClick ShowRecords
}

proc ShowFiles {} {
    global converterChoice dataDir fileListWidget
    setStatus "Searching for $converterChoice ..."
    set fileList [lsort [glob -nocomplain $dataDir/$converterChoice/$converterChoice-*]]
    if ![llength $fileList] {
        setStatus "No files found"
        return
    }
    set fileList [eval os editstring 100Z/ $fileList]
    set fileListP ""
    foreach item $fileList {
        if [catch {exec sdds2stream -npages=bare $dataDir/$converterChoice/$item} rows] {
            set rows 0
        }
        lappend fileListP "$item $rows"
    }
    $fileListWidget delete 0 end
    eval {$fileListWidget insert end} $fileListP
    setStatus "[llength $fileList] files found"
}

proc ShowRecords {} {
    global fileChoice
    setStatus "Examining [file tail $fileChoice]"
    if [catch {exec sddscollapse $fileChoice -pipe=out \
                 | sddsprocess -pipe \
                 -process=PageTimeStamp,first,PageTimeStamp0 \
                 -process=PageTimeStamp,last,PageTimeStamp1 \
                 | sdds2stream -pipe -rows=bare -param=PageTimeStamp0,PageTimeStamp1} dataList] {
        setStatus "$dataList"
        return
    }
    set rows [lindex $dataList 0]
    if !$rows {
        setStatus "No data recorded in this file."
    } else {
        setStatus "$rows glitch records between [lindex $dataList 1] and [lindex $dataList 2]"
    }
}

proc PlotData {} {
    global fileChoice converterChoice
    if ![string length $fileChoice] {
        setStatus "Choose a file"
        return
    }
    exec sddsplot $fileChoice \
        -split=page -separate=page -groupby=page -topline=@PageTimeStamp -ticks=xtime \
        -graph=line,vary -legend=edit=%/$converterChoice:// \
        "-ylabel=$converterChoice (Amperes)" \
        -column=Time,${converterChoice}:CurrentAO \
        -column=Time,${converterChoice}:CurrentAI \
        -column=Time,${converterChoice}:DacAI &
    setStatus "Plot launched..."
}
