#!/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 "\$Author: soliday $"
set apsttk 1

ttk::style configure Tight.TButton -padding 2 -shiftrelief 0 -focusthickness 0 -width 0
ttk::style configure Tall.TSpinbox -arrowsize 12

APSApplication . -name SRVacuumGlitchReview \
  -overview "Allows reviewing glitch data for storage ring vacuum"

set status "Ready..."
APSScrolledStatus .status -parent .userFrame -textVariable status -height 10 -width 45 -packOption "-fill x"

proc setStatus {text} {
    APSSetVarAndUpdate status $text
}


proc MakeDateTimeFrame {frame} {
    global StartTextDay StartDay StartTextMonth StartYear 
    global StartHour StartMinute StartSecond StartTime
    global EndTextDay EndDay EndTextMonth EndYear 
    global EndHour EndMinute EndSecond EndTime

    set s [clock seconds]

    ttk::frame $frame -padding 2
    pack $frame

    ttk::frame $frame.from
    pack $frame.from 
    set f $frame.from

    ttk::label $f.label -text "From:"
    ttk::spinbox $f.textday -values "Mon Tue Wed Thu Fri Sat Sun" \
      -width 3 -wrap true -textvariable StartTextDay \
      -style Tall.TSpinbox
    bindtags $f.textday "TSpinbox . all .userFrame.fg.bottom.time.frame.from.textday "
    bind $f.textday <<Increment>> "TimeAdjusted Start %W up"
    bind $f.textday <<Decrement>> "TimeAdjusted Start %W down"
    ttk::spinbox $f.day -from 1 -to 31 -width 2 -wrap true \
      -textvariable StartDay \
      -validate key -validatecommand {string is integer %P} \
      -style Tall.TSpinbox
    bindtags $f.day "TSpinbox . all .userFrame.fg.bottom.time.frame.from.day "
    bind $f.day <<Increment>> "TimeAdjusted Start %W up"
    bind $f.day <<Decrement>> "TimeAdjusted Start %W down"
    ttk::spinbox $f.month \
      -values "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec" \
      -width 3 -wrap true -textvariable StartTextMonth \
      -style Tall.TSpinbox
    bindtags $f.month "TSpinbox . all .userFrame.fg.bottom.time.frame.from.month "
    bind $f.month <<Increment>> "TimeAdjusted Start %W up"
    bind $f.month <<Decrement>> "TimeAdjusted Start %W down"
    ttk::spinbox $f.year -from 1900 -to 2100 -width 4 -wrap true \
      -textvariable StartYear \
      -validate key -validatecommand {string is integer %P} \
      -style Tall.TSpinbox
    bindtags $f.year "TSpinbox . all .userFrame.fg.bottom.time.frame.from.year "
    bind $f.year <<Increment>> "TimeAdjusted Start %W up"
    bind $f.year <<Decrement>> "TimeAdjusted Start %W down"
    ttk::label $f.space -text " "
    ttk::spinbox $f.hour -from 0 -to 23 -width 2 -wrap true \
      -format %02.0f -textvariable StartHour \
      -validate key -validatecommand {string is integer %P} \
      -style Tall.TSpinbox
    bindtags $f.hour "TSpinbox . all .userFrame.fg.bottom.time.frame.from.hour "
    bind $f.hour <<Increment>> "TimeAdjusted Start %W up"
    bind $f.hour <<Decrement>> "TimeAdjusted Start %W down"
    ttk::label $f.colon1 -text ":"
    ttk::spinbox $f.minute -from 0 -to 59 -width 2 -wrap true \
      -format %02.0f -textvariable StartMinute \
      -validate key -validatecommand {string is integer %P} \
      -style Tall.TSpinbox
    bindtags $f.minute "TSpinbox . all .userFrame.fg.bottom.time.frame.from.minute "
    bind $f.minute <<Increment>> "TimeAdjusted Start %W up"
    bind $f.minute <<Decrement>> "TimeAdjusted Start %W down"
    ttk::label $f.colon2 -text ":"
    ttk::spinbox $f.second -from 0 -to 59 -width 2 -wrap true \
      -format %02.0f -textvariable StartSecond \
      -validate key -validatecommand {string is integer %P} \
      -style Tall.TSpinbox
    bindtags $f.second "TSpinbox . all .userFrame.fg.bottom.time.frame.from.second "
    bind $f.second <<Increment>> "TimeAdjusted Start %W up"
    bind $f.second <<Decrement>> "TimeAdjusted Start %W down"

    bind $f.textday <Return> "TimeAdjustedManually"
    bind $f.day <Return> "TimeAdjustedManually"
    bind $f.month <Return> "TimeAdjustedManually"
    bind $f.year <Return> "TimeAdjustedManually"
    bind $f.hour <Return> "TimeAdjustedManually"
    bind $f.minute <Return> "TimeAdjustedManually"
    bind $f.second <Return> "TimeAdjustedManually"

    set StartTextDay [clock format $s -format %a]
    set StartDay [clock format $s -format %d]
    set StartTextMonth [clock format $s -format %b]
    set StartYear [clock format $s -format %Y]
    set StartHour 00
    set StartMinute 00
    set StartSecond 00

    set StartTime [clock scan "$StartTextDay $StartTextMonth $StartDay ${StartHour}:${StartMinute}:${StartSecond} $StartYear"]

   ttk::button $f.reset -text "Reset" -command "set StartTextDay $StartTextDay ; set StartDay $StartDay ; set StartTextMonth $StartTextMonth ; set StartYear $StartYear ; set StartHour $StartHour ; set StartMinute $StartMinute ; set StartSecond $StartSecond ; set StartTime $StartTime" -style Tight.TButton

    pack $f.label $f.textday $f.day $f.month \
      $f.year $f.space $f.hour $f.colon1 \
      $f.minute $f.colon2 $f.second $f.reset -side left

    ttk::button $f.15min -text "Last 15min" -command "TimePreset -minutes 15" -style Tight.TButton
    pack $f.15min -side left
    ttk::button $f.30min -text "Last 30min" -command "TimePreset -minutes 30" -style Tight.TButton
    pack $f.30min -side left


    ttk::frame $frame.to
    pack $frame.to -anchor e
    set f $frame.to

    ttk::label $f.label -text "To:"
    ttk::spinbox $f.textday -values "Mon Tue Wed Thu Fri Sat Sun" \
      -width 3 -wrap true -textvariable EndTextDay \
      -style Tall.TSpinbox
    bindtags $f.textday "TSpinbox . all .userFrame.fg.bottom.time.frame.to.textday "
    bind $f.textday <<Increment>> "TimeAdjusted End %W up"
    bind $f.textday <<Decrement>> "TimeAdjusted End %W down"
    ttk::spinbox $f.day -from 1 -to 31 -width 2 -wrap true \
      -textvariable EndDay \
      -validate key -validatecommand {string is integer %P} \
      -style Tall.TSpinbox
    bindtags $f.day "TSpinbox . all .userFrame.fg.bottom.time.frame.to.day "
    bind $f.day <<Increment>> "TimeAdjusted End %W up"
    bind $f.day <<Decrement>> "TimeAdjusted End %W down"
    ttk::spinbox $f.month \
      -values "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec" \
      -width 3 -wrap true -textvariable EndTextMonth \
      -style Tall.TSpinbox
    bindtags $f.month "TSpinbox . all .userFrame.fg.bottom.time.frame.to.month "
    bind $f.month <<Increment>> "TimeAdjusted End %W up"
    bind $f.month <<Decrement>> "TimeAdjusted End %W down"
    ttk::spinbox $f.year -from 1900 -to 2100 -width 4 -wrap true \
      -textvariable EndYear \
      -validate key -validatecommand {string is integer %P} \
      -style Tall.TSpinbox
    bindtags $f.year "TSpinbox . all .userFrame.fg.bottom.time.frame.to.year "
    bind $f.year <<Increment>> "TimeAdjusted End %W up"
    bind $f.year <<Decrement>> "TimeAdjusted End %W down"
    ttk::label $f.space -text " "
    ttk::spinbox $f.hour -from 0 -to 23 -width 2 -wrap true \
      -format %02.0f -textvariable EndHour \
      -validate key -validatecommand {string is integer %P} \
      -style Tall.TSpinbox
    bindtags $f.hour "TSpinbox . all .userFrame.fg.bottom.time.frame.to.hour "
    bind $f.hour <<Increment>> "TimeAdjusted End %W up"
    bind $f.hour <<Decrement>> "TimeAdjusted End %W down"
    ttk::label $f.colon1 -text ":"
    ttk::spinbox $f.minute -from 0 -to 59 -width 2 -wrap true \
      -format %02.0f -textvariable EndMinute \
      -validate key -validatecommand {string is integer %P} \
      -style Tall.TSpinbox
    bindtags $f.minute "TSpinbox . all .userFrame.fg.bottom.time.frame.to.minute "
    bind $f.minute <<Increment>> "TimeAdjusted End %W up"
    bind $f.minute <<Decrement>> "TimeAdjusted End %W down"
    ttk::label $f.colon2 -text ":"
    ttk::spinbox $f.second -from 0 -to 59 -width 2 -wrap true \
      -format %02.0f -textvariable EndSecond \
      -validate key -validatecommand {string is integer %P} \
      -style Tall.TSpinbox
    bindtags $f.second "TSpinbox . all .userFrame.fg.bottom.time.frame.to.second "
    bind $f.second <<Increment>> "TimeAdjusted End %W up"
    bind $f.second <<Decrement>> "TimeAdjusted End %W down"

    bind $f.textday <Return> "TimeAdjustedManually"
    bind $f.day <Return> "TimeAdjustedManually"
    bind $f.month <Return> "TimeAdjustedManually"
    bind $f.year <Return> "TimeAdjustedManually"
    bind $f.hour <Return> "TimeAdjustedManually"
    bind $f.minute <Return> "TimeAdjustedManually"
    bind $f.second <Return> "TimeAdjustedManually"

    set EndTextDay [clock format $s -format %a]
    set EndDay [clock format $s -format %d]
    set EndTextMonth [clock format $s -format %b]
    set EndYear [clock format $s -format %Y]
    set EndHour 23
    set EndMinute 59
    set EndSecond 59

    set EndTime [clock scan "$EndTextDay $EndTextMonth $EndDay ${EndHour}:${EndMinute}:${EndSecond} $EndYear"]

    ttk::button $f.reset -text "Reset" -command "set EndTextDay $EndTextDay ; set EndDay $EndDay ; set EndTextMonth $EndTextMonth ; set EndYear $EndYear ; set EndHour $EndHour ; set EndMinute $EndMinute ; set EndSecond $EndSecond ; set EndTime $EndTime" -style Tight.TButton

    pack $f.label $f.textday $f.day $f.month \
      $f.year $f.space $f.hour $f.colon1 \
      $f.minute $f.colon2 $f.second $f.reset -side left

    ttk::button $f.45min -text "Last 45min" -command "TimePreset -minutes 45" -style Tight.TButton
    pack $f.45min -side left
    ttk::button $f.60min -text "Last 60min" -command "TimePreset -minutes 60" -style Tight.TButton
    pack $f.60min -side left

}

proc TimePreset {args} {
    APSStrictParseArguments {minutes}
    foreach var "Start End" {
        global ${var}TextDay ${var}Day ${var}TextMonth ${var}Year 
        global ${var}Hour ${var}Minute ${var}Second ${var}Time
    }
    set s [clock seconds]
    
    set EndTextDay [clock format $s -format %a]
    set EndDay [clock format $s -format %d]
    set EndTextMonth [clock format $s -format %b]
    set EndYear [clock format $s -format %Y]
    set EndHour 23
    set EndMinute 59
    set EndSecond 59
    set EndTime [clock scan "$EndTextDay $EndTextMonth $EndDay ${EndHour}:${EndMinute}:${EndSecond} $EndYear"]

    set s [expr $s - ($minutes * 60)]
    set StartTextDay [clock format $s -format %a]
    set StartDay [clock format $s -format %d]
    set StartTextMonth [clock format $s -format %b]
    set StartYear [clock format $s -format %Y]
    set StartHour [clock format $s -format %H]
    set StartMinute [clock format $s -format %M]
    set StartSecond [clock format $s -format %S]
    set StartTime [clock scan "$StartTextDay $StartTextMonth $StartDay ${StartHour}:${StartMinute}:${StartSecond} $StartYear"]


}

proc TimeAdjustedManually {args} {
    foreach var "Start End" {
        global ${var}TextDay ${var}Day ${var}TextMonth ${var}Year 
        global ${var}Hour ${var}Minute ${var}Second ${var}Time
        
        set ${var}Time [clock scan "[set ${var}TextMonth] [set ${var}Day] [set ${var}Hour]:[set ${var}Minute]:[set ${var}Second] [set ${var}Year]"]
        set ${var}Minute [clock format [set ${var}Time] -format %M]
        set ${var}Hour [clock format [set ${var}Time] -format %H]
        set ${var}TextDay [clock format [set ${var}Time] -format %a]
        set ${var}Day [clock format [set ${var}Time] -format %d]
        set ${var}TextMonth [clock format [set ${var}Time] -format %b]
        set ${var}Year [clock format [set ${var}Time] -format %Y]
    }
}

proc TimeAdjusted {var widget direction} {
    global ${var}TextDay ${var}Day ${var}TextMonth ${var}Year 
    global ${var}Hour ${var}Minute ${var}Second ${var}Time

    if {[lindex [split $widget .] end] == "second"} {
        if {$direction == "up"} {
            set direction "+"
        } else {
            set direction "-"
        }
        set ${var}Time [expr "[set ${var}Time] $direction (1)"]
        set ${var}Second [clock format [set ${var}Time] -format %S]
        set ${var}Minute [clock format [set ${var}Time] -format %M]
        set ${var}Hour [clock format [set ${var}Time] -format %H]
        set ${var}TextDay [clock format [set ${var}Time] -format %a]
        set ${var}Day [clock format [set ${var}Time] -format %d]
        set ${var}TextMonth [clock format [set ${var}Time] -format %b]
        set ${var}Year [clock format [set ${var}Time] -format %Y]
    } elseif {[lindex [split $widget .] end] == "minute"} {
        if {$direction == "up"} {
            set direction "+"
        } else {
            set direction "-"
        }
        set ${var}Time [expr "[set ${var}Time] $direction (60)"]
        set ${var}Minute [clock format [set ${var}Time] -format %M]
        set ${var}Hour [clock format [set ${var}Time] -format %H]
        set ${var}TextDay [clock format [set ${var}Time] -format %a]
        set ${var}Day [clock format [set ${var}Time] -format %d]
        set ${var}TextMonth [clock format [set ${var}Time] -format %b]
        set ${var}Year [clock format [set ${var}Time] -format %Y]
    } elseif {[lindex [split $widget .] end] == "hour"} {
        if {$direction == "up"} {
            set direction "+"
        } else {
            set direction "-"
        }
        set ${var}Time [expr "[set ${var}Time] $direction (60 * 60)"]
        set ${var}Hour [clock format [set ${var}Time] -format %H]
        set ${var}TextDay [clock format [set ${var}Time] -format %a]
        set ${var}Day [clock format [set ${var}Time] -format %d]
        set ${var}TextMonth [clock format [set ${var}Time] -format %b]
        set ${var}Year [clock format [set ${var}Time] -format %Y]
    } elseif {[lindex [split $widget .] end] == "textday"} {
        if {$direction == "up"} {
            set direction "+"
        } else {
            set direction "-"
        }
        set ${var}Time [expr "[set ${var}Time] $direction (24 * 60 * 60)"]
        set initTextDay [set ${var}TextDay]
        set ${var}TextDay [clock format [set ${var}Time] -format %a]
        if {$initTextDay != [set ${var}TextDay]} {
            #This is needed for the transition to daylight savings time.
            set ${var}Time [expr "[set ${var}Time] $direction (1 * 60 * 60)"]
            set ${var}TextDay [clock format [set ${var}Time] -format %a]
        }
        set ${var}Day [clock format [set ${var}Time] -format %d]
        set ${var}TextMonth [clock format [set ${var}Time] -format %b]
        set ${var}Year [clock format [set ${var}Time] -format %Y]
    } elseif {[lindex [split $widget .] end] == "day"} {
        if {$direction == "up"} {
            set direction "+"
        } else {
            set direction "-"
        }
        set ${var}Time [expr "[set ${var}Time] $direction (24 * 60 * 60)"]
        set initTextDay [set ${var}TextDay]
        set ${var}TextDay [clock format [set ${var}Time] -format %a]
        if {$initTextDay == [set ${var}TextDay]} {
            #This is needed for the transition to daylight savings time.
            set ${var}Time [expr "[set ${var}Time] $direction (1 * 60 * 60)"]
            set ${var}TextDay [clock format [set ${var}Time] -format %a]
        }
        set ${var}Day [clock format [set ${var}Time] -format %d]
        set ${var}TextMonth [clock format [set ${var}Time] -format %b]
        set ${var}Year [clock format [set ${var}Time] -format %Y]
    } elseif {[lindex [split $widget .] end] == "month"} {
        set month [clock format [set ${var}Time] -format %b]
        set year [clock format [set ${var}Time] -format %Y]
        set s1 [clock scan "$month 01 00:00:00 $year"]
        if {$direction == "up"} {
            if {$month == "Dec"} {
                incr year
            }
        } else {
            if {$month == "Jan"} {
                incr year -1
            }
        }
        set s2 [clock scan "[set ${var}TextMonth] 01 00:00:00 $year"]
        set s [expr {$s2 - $s1 + (60 * 60)}]
        set ${var}Time [expr "[set ${var}Time] + $s"]
        while {[set ${var}TextMonth] != [clock format [set ${var}Time] -format %b]} {
            if {$month == [clock format [set ${var}Time] -format %b]} {
                if {$direction == "up"} {
                    set ${var}Time [expr "[set ${var}Time] + (12 * 60 * 60)"]
                } else {
                    set ${var}Time [expr "[set ${var}Time] - (12 * 60 * 60)"]
                }
            } else {
                if {$direction == "up"} {
                    set ${var}Time [expr "[set ${var}Time] - (12 * 60 * 60)"]
                } else {
                    set ${var}Time [expr "[set ${var}Time] + (12 * 60 * 60)"]
                }
            }
        }
        set ${var}TextDay [clock format [set ${var}Time] -format %a]
        set ${var}Day [clock format [set ${var}Time] -format %d]
        set ${var}TextMonth [clock format [set ${var}Time] -format %b]
        set ${var}Year [clock format [set ${var}Time] -format %Y]
    } elseif {[lindex [split $widget .] end] == "year"} {
        if {([set ${var}Day] == "29") && ([set ${var}TextMonth] == "Feb")} {
            set ${var}Day 28
        }
        set ${var}Time [clock scan "[set ${var}TextMonth] [set ${var}Day] [set ${var}Hour]:[set ${var}Minute]:[set ${var}Second] [set ${var}Year]"]
        set ${var}TextDay [clock format [set ${var}Time] -format %a]
    }
    set ${var}Time [clock scan "[set ${var}TextMonth] [set ${var}Day] [set ${var}Hour]:[set ${var}Minute]:[set ${var}Second] [set ${var}Year]"]

}

proc CalcTime {} {
    foreach var "Start End" {
        global ${var}TextDay ${var}Day ${var}TextMonth ${var}Year 
        global ${var}Hour ${var}Minute ${var}Second ${var}Time
        global dateEntryStyle ${var}Month
        set ${var}Time [clock scan "[set ${var}TextMonth] [set ${var}Day] [set ${var}Hour]:[set ${var}Minute]:[set ${var}Second] [set ${var}Year]"]
    }
}








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

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

set vacFileList ""
set vacTimeList ""
set vacEventList ""
APSScrolledList .fileList -parent .userFrame.fg.top \
  -height 15 -listvar vacEventList -callback FileChoiceCallback \
  -selectMode single -packOption "-side left" 
#set fileListWidget .userFrame.fg.top.fileList.listbox
.userFrame.fg.top.fileList.listbox configure -width 70

set filterString *
APSLabeledEntry .include \
  -parent .userFrame.fg.bottom \
  -label "Include:" \
  -packOption "-expand true -fill x" \
  -textVariable filterString
bind .userFrame.fg.bottom.include.entry <Return> "Search"
set excludeFilterString ""
APSLabeledEntry .exclude \
  -parent .userFrame.fg.bottom \
  -label "Exclude:" \
  -packOption "-expand true -fill x" \
  -textVariable excludeFilterString
bind .userFrame.fg.bottom.exclude.entry <Return> "Search"

frame .userFrame.fg.bottom.time
pack .userFrame.fg.bottom.time
MakeDateTimeFrame .userFrame.fg.bottom.time.frame

APSButton .search -parent .userFrame.fg.bottom -text "Search Time Frame" -command Search -width ""
APSButton .plotData -parent .userFrame.fg.bottom -text "Plot Whole Day" -command PlotData -width ""
APSButton .plotData2 -parent .userFrame.fg.bottom -text "Plot Single Event" -command PlotData2 -width ""


set selectionIndex -1

proc FileChoiceCallback {item doubleClick} {
    global vacEventList selectionIndex
    set selectionIndex [lsearch -exact $vacEventList $item]
    if {$selectionIndex > -1} {
        if {$doubleClick} {
            PlotData
        }
    }
}

proc Search {} {
    global vacList StartTime EndTime dataDir vacFileList vacTimeList vacEventList filterString excludeFilterString selectionIndex
    CalcTime
    if {$StartTime >= $EndTime} {
        setStatus "The date/time range is invalid"
        return
    }
    setStatus "Searching for glitch events..."
    set vacFileList ""
    set vacTimeList ""
    set vacEventList ""
    set selectionIndex -1


    #Locate files in the time range
    set filterList ""
    set excludeFilterString [string trim $excludeFilterString]
    set efs [string length $excludeFilterString]
    foreach vac $vacList {
        if {[string match $filterString $vac]} {
            if {$efs && [string match "*${excludeFilterString}*" $vac]} {
                continue
            }
            set sec $StartTime
            while {$sec < $EndTime} {
                lappend filterList $dataDir/${vac}/${vac}-[clock format $sec -format "%Y-%j-%m%d.????"]
                incr sec [expr 60 * 60 * 24]
            }
        }
    }

    set fileList [lsort [eval glob -nocomplain $filterList]]
    if {[llength $fileList] == 0} {
        setStatus "Found 0 PVs with glitch events."
        return
    }
    if {[catch {eval exec sdds2stream $fileList -parameter=TriggerTime -rows=total} data]} {
        setStatus "Problem reading SDDS files: $data"
        return
    }
    set lines [split $data \n]
    set i 0
    set info ""
    foreach line $lines {
        if {[lindex $line 1] == "rows"} {
            set rows [lindex $line 0]
            incr i
        } elseif {$line > 0} {
            set file [lindex $fileList $i]
            set vac [join [lrange [split [file tail [lindex $fileList $i]] "-"] 0 end-3] -]
            set trigTime $line
            if {($trigTime >= $StartTime) && ($trigTime <= $EndTime)} {
                lappend info "$trigTime $vac $file"
            }
        }
    }
    if {[llength $info] == 0} {
        setStatus "Found 0 PVs with glitch events."
    }
    set info [lsort -index {0} $info]
    foreach ele $info {
        set sec [expr int([lindex $ele 0])]
        set point [expr [lindex $ele 0] - $sec]
        lappend vacEventList "[clock format $sec -format "%a %b %d %H:%M:%S"][string trimleft [format %.3f $point] 0][clock format $sec -format " %Z %Y"]  [lindex $ele 1]"
        lappend vacFileList [lindex $ele 2]
        lappend vacTimeList $sec
    }
    set vacEventList [lreverse $vacEventList]
    set vacFileList [lreverse $vacFileList]
    set vacTimeList [lreverse $vacTimeList]
    setStatus "Found [llength $vacEventList] glitch events."
    
}

proc PlotData {} {
    global selectionIndex vacEventList vacFileList
    
    if {$selectionIndex == -1} {
        setStatus "Choose glitch event"
        return
    }
    set name [lindex [lindex $vacEventList $selectionIndex] end]
    set file [lindex $vacFileList $selectionIndex]
    
    set columns [exec sddsquery $file -col]
    if {[lsearch -exact $columns ${name}:PressureM.CRNT] != -1} {
        exec sddsplot "-device=motif,(-newzoom true)" $file \
          -sever=xgap=5 \
          -separate=1 -layout=1,3 -join=x \
          -topline=@PageTimeStamp -ticks=xtime \
          -graph=line,vary -legend \
          "-ylabel=$name" \
          "-column=Time,(${name}:PressureM,${name}:PressureM.CRNT,${name}:PressureM.VOLT)" &
    } else {
        exec sddsplot "-device=motif,(-newzoom true)" $file \
          -sever=xgap=5 \
          -separate=1 -layout=1,3 -join=x \
          -topline=@PageTimeStamp -ticks=xtime \
          -graph=line,vary -legend \
          "-ylabel=$name (T)" \
          "-column=Time,(${name}:PressureM)" &
    }
    setStatus "Plot launched..."
}

proc PlotData2 {} {
    global selectionIndex vacEventList vacFileList vacTimeList
    
    if {$selectionIndex == -1} {
        setStatus "Choose glitch event"
        return
    }
    set time [lindex $vacTimeList $selectionIndex]
    set name [lindex [lindex $vacEventList $selectionIndex] end]
    set file [lindex $vacFileList $selectionIndex]
    
    set columns [exec sddsquery $file -col]
    
    if {[lsearch -exact $columns ${name}:PressureM.CRNT] != -1} {
        exec sddsplot "-device=motif,(-newzoom true)" $file \
          -filter=parameter,TriggerTime,[expr $time - 1],[expr $time + 1] \
          -sever=xgap=5 \
          -separate=1 -layout=1,3 -join=x \
          -topline=@PageTimeStamp -ticks=xtime \
          -graph=line,vary -legend \
          "-ylabel=$name" \
          "-column=Time,(${name}:PressureM,${name}:PressureM.CRNT,${name}:PressureM.VOLT)" &
    } else {
        exec sddsplot "-device=motif,(-newzoom true)" $file \
          -filter=parameter,TriggerTime,[expr $time - 1],[expr $time + 1] \
          -sever=xgap=5 \
          -separate=1 -layout=1,3 -join=x \
          -topline=@PageTimeStamp -ticks=xtime \
          -graph=line,vary -legend \
          "-ylabel=$name (T)" \
          "-column=Time,(${name}:PressureM)" &
    }
    setStatus "Plot launched..."
}
