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

#
# First version was by Y. Eidelman in the 1990s. This version is a major rewrite.
#

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.11 $ \$Author: borland $"

set IPLabelList [list IP1 IP2 IP3 IP4 IP5 IP6 IP7 IP8 IP9 CCG1 ID:CCG1 ID:IP1 C:IP1 C:IP2 C:IP3 C:IP4 C:IP5 RFC1 RFC2 RFC3 RFC4]
set IPList [list :IP1 :IP2 :IP3 :IP4 :IP5 :IP6 :IP7 :IP8 :IP9 :CCG1 ID:CCG1 ID:IP1 C:IP1 C:IP2 C:IP3 C:IP4 C:IP5 :RFC1 :RFC2 :RFC3 :RFC4]

set CPISectorList [list 36 37 38 39 40]
set CPIList(36) [list 1 2 3]
set CPIList(37) [list 1 2 3]
set CPIList(38) [list 1 2 3 4 5]
set CPIList(39) [list 1 2 3 4 5]
set CPIList(40) [list 1 2 3]
set missingIP [list]
for {set sector 1} {$sector<41} {incr sector} {
    if [lsearch $CPISectorList $sector]==-1 {
        for {set ip 1} {$ip<6} {incr ip} {
            lappend missingIP [format S%02dC:IP%d $sector $ip]
        }
    } else {
        for {set ip 1} {$ip<6} {incr ip} {
            if [lsearch $CPIList($sector) $ip]==-1 {
                lappend missingIP [format S%02dC:IP%d $sector $ip]
            }
        }
    }
}

set rfCavitySectorList [list 36 37 40]
for {set sector 1} {$sector<41} {incr sector} {
    if [lsearch $rfCavitySectorList $sector]==-1 {
        for {set ip 1} {$ip<5} {incr ip} {
            lappend missingIP [format S%02d:RFC%d $sector $ip]
        }
    }
}
    

set dataDir /home/helios/oagData/monitoring/SRvac
set knownColumnList [exec sdds2stream -column=ReadbackName $dataDir/SRvac.mon]
set fileListUpdateTime 0
set selectedColumns ""

APSApplication . -name SRVacuumTrending -overview \
    {This application allows for plotting of any selected group of ion pumps to show trends with time, lifetime, current, and dose.}

set w .userFrame

set ipConfigStatus ""
APSScrolledStatus .status -parent .userFrame -textVariable ipConfigStatus \
                          -width 100 -height 5
proc SetStatusText {text} {
   global ipConfigStatus     # variable to output status of anything
   set ipConfigStatus "$text"
   update
}


#======================================================================= 
# Process of the creating of window for to select ion pump's family
#======================================================================= 
APSSRSectorButtons .ipbuttons -parent .userFrame \
  -rootname ip -orientation horizontal -label "Ion Pump Selections" -description "Ion pump selection" \
  -itemList $IPList -packOption "-side top" -itemLabelList $IPLabelList -missingList $missingIP \
  -colorDesc 0 -sectorControl 1

#======================================================================= 
# Process of the creating special data (lists workingArray,  
# workingList and string averagingList) and execution of FindFiles process  
#=======================================================================   
proc ChoiceIP {args} {
    global IPList selectedList IPCount dataPlotting knownColumnList
    
    # list of the missing ion pumps:
    set absentIP [list ]
    
    # Set per-location lists to empty
    foreach ip $IPList {
        set selectedList($ip) [list ]
        set IPCount($ip) 0
    }
    # Set full-ring list to empty
    set selectedList(ring) [list ]
    set IPCount(ring) 0
    
    for {set sector 1} {$sector<=40} {incr sector} {
        set ssector [format S%02d $sector]
        set IPCount($ssector) 0
        set selectedList($ssector) [list ]
        foreach ip $IPList {
            # global variable for ion pump name:      
            set varName ip$ssector$ip 
            global $varName
            if ![info exists $varName] {
                puts stderr "Not found: $varName"
                continue
            }
            if [set $varName] {
                lappend selectedList($ssector) $ssector$ip:PressureM
                lappend selectedList($ip) $ssector$ip:PressureM
                lappend selectedList(ring) $ssector$ip:PressureM
                incr IPCount($ssector) 1
                incr IPCount(ring) 1
                incr IPCount($ip) 1
            }
        }
    }

    #--------------------------------------------------------- 
    # Exec FindFiles process:
    #  
    SetStatusText "Working..."
    if ![string length [FindFiles]] {
        SetStatusText "No data found."
        set dataPlotting 0
    } else {
        set dataPlotting 1
    }
}   

#======================================================================= 
# Process of the creating main window
#======================================================================= 
proc MakeMainWindow {widget args} {
    global statisticScope normalizationType xAxis statisticType fixedScale logScale groupingType includeOnPlot layoutSelection despikePasses medianFilterNeighbors colorSchemeSelection
    set parent ""
    APSStrictParseArguments {parent}

    APSFrame .tablo -parent $parent -label "Plotting and Exporting:" -packOption "-side left -anchor nw"

    set w $parent.tablo.frame

    set statisticType None
    APSRadioButtonFrame .rb1 -parent $w -label "Statistic: " -variable statisticType -orientation horizontal \
      -buttonList "None Average Maximum" -valueList "None Average Maximum" \
      -commandList [list "APSDisableWidget $w.rb15" "APSEnableWidget $w.rb15" "APSEnableWidget $w.rb15"]

    set statisticScope Ring
    APSRadioButtonFrame .rb15 -parent $w -label "Stat. scope: " -variable statisticScope -orientation horizontal \
        -buttonList "Sector Location Ring" -valueList "Sector Location Ring"
    APSDisableWidget $w.rb15

    set groupingType None
    APSRadioButtonFrame .rb16 -parent $w -label "Grouping: " -variable groupingType -orientation horizontal \
        -buttonList "None All Sector Location" -valueList "None All Sector Location"

    set normalizationType None
    APSRadioButtonFrame .rb2 -parent $w -label "Normalization: " -variable normalizationType -orientation horizontal \
                        -buttonList "None Current" -valueList "None Current" 

    set despikePasses 0
    APSRadioButtonFrame .rb4a -parent $w -label "Despike passes: " -variable despikePasses -orientation horizontal \
        -buttonList "0 1 2 3 4 5" -valueList "0 1 2 3 4 5"

    set medianFilterNeighbors 0
    APSRadioButtonFrame .rb4b -parent $w -label "Median filter neighbors: " -variable medianFilterNeighbors -orientation horizontal \
        -buttonList "0 2 4 6 8 10" -valueList "0 2 4 6 8 10"

    set xAxis Time
    APSRadioButtonFrame .rb3 -parent $w -label "x Axis: " -variable xAxis -orientation horizontal \
      -buttonList "Time Lifetime Current Dose" -valueList "Time S-INJ:MPS:LifetimeMinutesM S-DCCT:CurrentM S-DCCT:AmpHoursAM"  \
      -commandList {"set logScale y-auto" "set logScale y-auto" "set logScale y-auto" "set logScale both"}

    set fixedScale 0
    APSRadioButtonFrame .rb4 -parent $w -label "Fixed scale: " -variable fixedScale -orientation horizontal \
        -buttonList "Yes No" -valueList "1 0"

    set logScale y-auto
    APSRadioButtonFrame .rb5 -parent $w -label "Log scale: " -variable logScale -orientation horizontal \
        -buttonList "neither y-auto y both" -valueList "neither y-auto y both"

    set includeOnPlot Nothing
    APSRadioButtonFrame .rb6 -parent $w -label "Also plot: " -variable includeOnPlot -orientation horizontal \
        -buttonList "Nothing Current Dose Lifetime Bunches" -valueList "Nothing Current Dose Lifetime Bunches"

    set colorSchemeSelection -order=spect
    APSRadioButtonFrame .rb7 -parent $w -label "Color mode: " -variable colorSchemeSelection -orientation horizontal \
      -buttonList "Spectrum Normal" -valueList [list "-order=spect" ""]
     
    set layoutList [list ]
    for {set ix 1} {$ix < 9} {incr ix} {
        for {set iy 1} {$iy < 9} {incr iy} {
            lappend layoutList ${ix}x${iy}
        }
    }
    set layoutSelection 1x1
    APSComboboxFrame .cb1 -parent $w -label "Layout" -textVariable layoutSelection -itemList $layoutList

    APSButton .go -parent $w -text "Plot" -command  "PlotPressureVs" 
    APSButton .go2 -parent $w -text "Export" -command  "PlotPressureVs -export 1" 

} 

#======================================================================= 
# Process of the plotting selected data
#======================================================================= 
proc PlotPressureVs {args} {
    global statisticScope normalizationType xAxis statisticType fixedScale groupingType logScale knownColumnList layoutSelection despikePasses medianFilterNeighbors 
    global existingDataFile dataPlotting includeOnPlot colorSchemeSelection
    global selectedList
    global IPList IPLabelList
    global IPCount     
    global bucketsMin bucketsMax
    global currentMin currentMax
    global startTime endTime

    set export 0
    APSStrictParseArguments {export}

    set graphicType dot
    if [string compare $xAxis "Time"]==0 {
        set graphicType line
    }
    
    ChoiceIP

    if {$dataPlotting < 1} return

    if {$IPCount(ring) == 0} {
       SetStatusText "Choose something to display"
       return
    }
    global StartYear StartMonth StartDay EndYear EndMonth EndDay
    set startDate [exec timeconvert -breakDown=year=$StartYear,day=$StartDay,month=$StartMonth,hour=23.99 ]
    set endDate [exec timeconvert -breakDown=year=$EndYear,day=$EndDay,month=$EndMonth,hour=0 ]
    
    set tmpFile /tmp/[APSTmpString]
    APSAddToTempFileList $tmpFile

    set plotColumnsList [list ]
    global selectedColumns selectedPressureList
    
    set selectedColumns -retain=column,S-DCCT:*,S-INJ:MPS:*,Time,SRBucketsFilled
    set selectedPressureList [list ]
    foreach item $selectedList(ring) {
        if [lsearch -exact $knownColumnList $item]!=-1 {
            append selectedColumns ,$item
            lappend selectedPressureList $item
        }
    }
    
    set statResultList [list ]
    set rowstatsOptList [list ]
    set localGroupingType $groupingType
    if [string compare None $statisticType] {
        set statisticOperation(Maximum) maximum
        set statisticOperation(Average) mean
        set limitPressure toplimit=1e-3,bottomLimit=1e-13
        switch $statisticScope {
            Ring {
                set statInputList ""
                foreach item $selectedList(ring) {
                    lappend statInputList $item
                }
                lappend rowstatsOptList -$statisticOperation($statisticType)=${statisticType}RingPressure,[join $statInputList ,],$limitPressure
                lappend statResultList ${statisticType}RingPressure
                set localGroupingType None
            }
            Location {
                foreach ip $IPList iplabel $IPLabelList {
                    if $IPCount($ip)!=0 {
                        lappend rowstatsOptList -$statisticOperation($statisticType)=${statisticType}${iplabel}Pressure,[join $selectedList($ip) ,],$limitPressure
                        lappend statResultList ${statisticType}${iplabel}Pressure
                    }
                }
                if [lsearch -exact [list Sector Location] $localGroupingType]!=-1 {
                    set localGroupingType None
                }
            }
            Sector {
                for {set sector 1} {$sector<41} {incr sector} {
                    set ssector [format S%02d $sector]
                    if $IPCount($ssector)!=0 {
                        lappend rowstatsOptList -$statisticOperation($statisticType)=${statisticType}${ssector}Pressure,[join $selectedList($ssector) ,],$limitPressure
                        lappend statResultList ${statisticType}${ssector}Pressure
                    }
                }
                if [lsearch -exact [list Sector Location] $localGroupingType]!=-1 {
                    set localGroupingType None
                }
            }
        }
        set plotColumnsList $statResultList
    } else {
        set plotColumnsList $selectedPressureList
    }

    lappend defineList "-redefine=column,S-DCCT:CurrentM,S-DCCT:CurrentM abs 1e-6 +,units=mA"
    
    set xAxisMod $xAxis
#    if {(($xAxisMod == "S-INJ:MPS:LifetimeMinutesM") || ($includeOnPlot == "Lifetime")) && ($startDate < 1734501600) && ($endDate >= 1734501600)} {
#        SetStatusText "Time frame cannot span over 12/18/2024 00:00:00 due to lifetime PV change."
#        return
#    }
    if {($xAxisMod == "S-INJ:MPS:LifetimeMinutesM") && ($startDate < 1734501600) && ($endDate >= 1734501600)} {
        SetStatusText "Time frame cannot span over 12/18/2024 00:00:00 due to lifetime PV change."
        return
    }
    if {$endDate < 1734501600} {
        lappend defineList "-define=column,CurrentTimesLifetime,S-DCCT:CurrentM S-DCCT:LifetimeM *,units=mA*min"
        if {$xAxisMod == "S-INJ:MPS:LifetimeMinutesM"} {
            set xAxisMod "S-DCCT:LifetimeM"
        }
    } else {
        lappend defineList "-define=column,CurrentTimesLifetime,S-DCCT:CurrentM S-INJ:MPS:LifetimeMinutesM *,units=mA*min"
    }

    if [string compare Current $normalizationType]==0 {
        set yMaxLimit 1e6
        set yMinLimit 1e-4
        set yLabel "Normalized Pressure (nT/mA)"
        if [llength $statResultList] {
            foreach statResult $statResultList {
                lappend defineList "-redefine=column,${statResult},$statResult S-DCCT:CurrentM abs 1e-16 + / 1e9 *,units=nT/mA"
            }
        } else {
            foreach column $selectedPressureList {
                lappend defineList "-redefine=column,${column},$column S-DCCT:CurrentM abs 1e-16 + / 1e9 *,units=nT/mA"
            }
        }
    } else {
        set yLabel "Pressure (nT)"
        if [llength $statResultList] {
            foreach statResult $statResultList {
                lappend defineList "-redefine=column,${statResult},$statResult 1e9 *,units=nT"
            }
        } else {
            foreach column $selectedPressureList {
                lappend defineList "-redefine=column,${column},$column 1e9 *,units=nT"
            }
        }
        set yMaxLimit 1e5
        set yMinLimit 1e-3
    }

    if {$despikePasses>0 || $medianFilterNeighbors>0} {
        set smoothCommand "sddssmooth -pipe -passes=0 -column=*Pressure* "
        if $despikePasses>0 {
            append smoothCommand "-despike=passes=$despikePasses "
        }
        if $medianFilterNeighbors>0 {
            append smoothCommand "-medianFilter=windowSize=[expr $medianFilterNeighbors+1] "
        }
    } else {
        set smoothCommand cat
    }
    SetStatusText "Processing (statistics, smoothing, normalization, ...)"
    global selectedColumns 
    if [llength $rowstatsOptList] {
        if {![catch {eval exec sddsconvert $existingDataFile -pipe=out $selectedColumns \
                       | sddsrowstats -pipe $rowstatsOptList \
                       | $smoothCommand \
                       | sddsprocess -pipe=in $tmpFile $defineList} result]==0} {
            SetStatusText "Problem preparing data (1): $result"
            set dataPlotting 0                   
            return 
        } 
    } else {
        if {![catch {eval exec sddsconvert $existingDataFile -pipe=out $selectedColumns \
                       | $smoothCommand \
                       | sddsprocess -pipe=in $tmpFile $defineList} result]==0} {
            SetStatusText "Problem preparing data (2): $result"
            set dataPlotting 0                   
            return 
        }
    }

    if $export {
        set choose 1
        while {$choose} {
            set choose 0
            set filename [APSFileSelectDialog .fsd -width 40 -checkValidity 0 -selectDir 0 -reverseSort 0]
            if ![string length $filename] return
            if [file exists $filename] {
                switch [APSMultipleChoice .msd -question "$filename exists. What do you want to do?" \
                          -labelList {Overwrite "Specify another file" Abort} \
                          -returnList "Overwrite Repeat Abort"] {
                              Overwrite {
                                  set choose 0
                              }
                              Repeat {
                                  set choose 1
                              }
                              Abort {
                                  return
                              }
                          }
            }
        }
        file copy -force $tmpFile $filename
        SetStatusText "Exported data to $filename"
        return
    }

    set plotOptionList -graph=$graphicType
    switch $logScale {
        y-auto {
            if !$fixedScale {
                set mainMode -mode=y=autolog
            }  else {
                set mainMode -mode=y=log,y=special
            }
        }
        y {
            set mainMode -mode=y=log,y=special
        }
	both {
            set mainMode -mode=loglog
        }
        neither {
            set mainMode -mode=y=linear
        }
    }

    if $fixedScale {
        lappend plotOptionList -same=y,global
    }

    if [string compare $xAxisMod Time]==0 {
        lappend plotOptionList -ticks=xtime
    }

    if [string compare $includeOnPlot Nothing]==0 {
        set forceLegend 0
    } else {
        set forceLegend 1
        set colorSchemeSelection ""
    }
    set columnOptionList [list ]
    switch $localGroupingType {
        None {
            lappend plotOptionList -graph=$graphicType,vary,type=1
            foreach item $plotColumnsList {
                lappend columnOptionList -column=$xAxisMod,$item $tmpFile $mainMode -limit=ymax=$yMaxLimit,ymin=$yMinLimit
                if $forceLegend {
                    lappend columnOptionList -legend
                }
                lappend columnOptionList -end
            }
        }
        All {
            lappend plotOptionList "-ylabel=$yLabel"
            lappend plotOptionList -graph=$graphicType,vary,type=1
            if [string length $colorSchemeSelection] {
                lappend plotOptionList $colorSchemeSelection
            }
            lappend columnOptionList -column=$xAxisMod,([join $plotColumnsList ,]) $tmpFile $mainMode -legend=edit=%/:PressureM//%/Pressure// -limit=ymax=$yMaxLimit,ymin=$yMinLimit -end
        }
        Sector {
            lappend plotOptionList -graph=$graphicType,vary,type=1 
            set ssectorList [list ]
            foreach item $plotColumnsList {
                eval set ssector [os editstring S/:/100D $item]
                if [lsearch $ssectorList $ssector]==-1 {
                    lappend ssectorList $ssector
                }
                lappend ssectorItemList($ssector) $item
            }
            foreach ssector $ssectorList {
                lappend columnOptionList -column=$xAxisMod,([join $ssectorItemList($ssector) ,])  $tmpFile $mainMode -limit=ymax=$yMaxLimit,ymin=$yMinLimit
                lappend columnOptionList "-ylabel=$yLabel" -legend=edit=%/:PressureM//%/Pressure// -end
            }
        }
        Location {
            lappend plotOptionList -graph=$graphicType,vary,type=1
            if [string length $colorSchemeSelection] {
                lappend plotOptionList $colorSchemeSelection
            }
            set locationList [list ]
            foreach item $plotColumnsList {
                eval set location [os editstring 3dx/:/%/:PressureM// $item]
                if [lsearch $locationList $location]==-1 {
                    lappend locationList $location
                }
                lappend locationItemList($location) $item
            }
            foreach location $locationList {
                lappend columnOptionList -column=$xAxisMod,([join $locationItemList($location) ,])  $tmpFile "-ylabel=$yLabel" -limit=ymax=$yMaxLimit,ymin=$yMinLimit
                lappend columnOptionList $mainMode -legend=edit=%/:PressureM// -end
            }
        }
    }

    lappend filterList -filter=column,Time,$startTime,$endTime
    lappend filterList -filter=column,SRBucketsFilled,$bucketsMin,$bucketsMax
    lappend filterList -filter=column,S-DCCT:CurrentM,$currentMin,$currentMax
    
    switch $includeOnPlot {
        Nothing {
        } 
        Current {
            lappend plotOptionList -yscale=request
            eval lappend plotOptionList  -column=$xAxisMod,S-DCCT:CurrentM $tmpFile -omnipresent -graph=line,type=0 -legend=spec=Current $filterList
        }
        Dose {
            lappend plotOptionList -yscale=request
            eval lappend plotOptionList  -column=$xAxisMod,S-DCCT:AmpHoursAM $tmpFile -omnipresent -graph=line,type=0 -legend=spec=Dose $filterList
        }
        Lifetime {
            lappend plotOptionList -yscale=request
            if {$endDate < 1734501600} {
                eval lappend plotOptionList  -column=$xAxisMod,S-DCCT:LifetimeM $tmpFile -omnipresent -graph=line,type=0 -legend=spec=Lifetime $filterList
            } else {
                eval lappend plotOptionList  -column=$xAxisMod,S-INJ:MPS:LifetimeMinutesM $tmpFile -omnipresent -graph=line,type=0 -legend=spec=Lifetime $filterList
            }
        }
        Bunches {
            lappend plotOptionList -yscale=request
            eval lappend plotOptionList  -column=$xAxisMod,SRBucketsFilled $tmpFile -omnipresent -graph=line,type=0 -legend=spec=Bunches $filterList
        }
    }

    regexp {(.*)x(.*)} $layoutSelection all nx ny
    if {$ny!=1} {
        lappend plotOptionList -join=x
    }
    SetStatusText "Plotting"
    global sparsingInterval
    eval exec sddsplot -layout=$nx,$ny $filterList -sever=xgap=[expr $sparsingInterval*8] $plotOptionList $columnOptionList -offset=ychange=1e-12,ybeforelog &
}
         
#======================================================================= 
# Process of the creating DateTime window
#======================================================================= 
proc MakeDateTimeFrame {widget args} {

   set parent .
   set rootname ""
   set label "Date/Time Range of Interest:"

   APSStrictParseArguments {parent rootname}

   APSFrame $widget -parent $parent -label $label 

   set w $parent$widget.frame

   APSDateTimeAdjEntry .startDate -parent $w \
                                  -yearVariable ${rootname}StartYear \
                                  -monthVariable ${rootname}StartMonth \
                                  -dayVariable ${rootname}StartDay \
                                  -hourVariable ${rootname}StartHour \
                                  -label "Starting date/time (year, \
                                          month, day, hour): " \
                                  -defaultHour 0 \
                                  -command ResetDataFileList \
                                  -buttonSize small 

   APSDateTimeAdjEntry .endDate -parent $w \
                                -yearVariable ${rootname}EndYear \
                                -monthVariable ${rootname}EndMonth \
                                -dayVariable ${rootname}EndDay \
                                -hourVariable ${rootname}EndHour \
                                -label "Ending date/time (year, \
                                        month, day, hour):   " \
                                -defaultHour 24 \
                                -command ResetDataFileList \
                                -buttonSize small

#--------------------------------------------------------- 
# Exec SetDateTimeToToday processes: 
#   
   SetDateTimeToToday -rootname ${rootname}Start -hour 0
   SetDateTimeToToday -rootname ${rootname}End  -hour 24
}
      
#======================================================================= 
# Process of the creating Filters window
#======================================================================= 
proc MakeFiltersFrame {widget args} {
 
   global bucketsMin bucketsMax            # number of the filled buckets 
   global currentMin         # minimal used current
   global currentMax         # maximal used current
   
   set parent .
   set rootname ""
   APSStrictParseArguments {parent}

   set bucketsMin 0
   set bucketsMax 1296
   set currentMin 0.0
   set currentMax 205.0

   APSFrame $widget -parent $parent -label "Filters: " 
   set w $parent$widget.frame

   APSLabeledEntryFrame .buckets -parent $w -label "Buckets (min, max):" \
     -variableList {bucketsMin bucketsMax} -width 5 \
     -orientation horizontal \
     -contextHelp "Specify range of number of filled buckets to include."

   APSLabeledEntryFrame .maxCurrent -parent $w -label "Current (min, max; mA):" \
     -variableList {currentMin currentMax} -width 5 \
     -orientation horizontal \
     -contextHelp "Specify range of beam current to include."
}

proc MakePreprocessingFrame {widget args} {
   APSStrictParseArguments {parent}

   global sparsingInterval sparsingType
   set sparsingInterval 1
   set sparsingType ""
   
   APSFrame $widget -parent $parent -label "Preprocessing: "
   set w $parent$widget.frame
   
   APSLabeledEntry .sparsing -parent $w -label "Sparsing interval: " -textVariable sparsingInterval -width 4 \
                   -type integer -contextHelp "Interval with which to sparse (uniformly sample) the data."
   APSRadioButtonFrame  .sparsingType -parent $w -label "Sparsing type: " -variable sparsingType -orientation horizontal \
                        -buttonList "Sample Mean Median Min Max" -valueList {"" average median minimum maximum} \
                        -contextHelp "Select the method of sparsing."
}

#======================================================================= 
# Process
#======================================================================= 
proc SetDateTimeToToday {args} {

#   global dayVariable

   set rootname ""
   set hour 0
#--------------------------------------------------------- 
# Exec ResetDataFileList process:
#  
   ResetDataFileList

   APSStrictParseArguments {rootname hour}

#--------------------------------------------------------
# Attention: these descriptions cann't be placed before defintions 
# variables rootname and hour !!!
#
   global ${rootname}Month 
   global ${rootname}Year 
   global ${rootname}Day 
   global ${rootname}Hour

   APSDateBreakDown -dayVariable ${rootname}Day \
                    -yearVariable ${rootname}Year \
                    -monthVariable ${rootname}Month \
                    -twoDigitYear 0 \
                    -leadingZeros 0

   set ${rootname}Hour $hour
}

   set dataFileList ""
   set dataFileListIsOld 1
   set fileListUpdateTime 0

#======================================================================= 
# Erasing list of the selected files before new selection of them and
# reset flag dataFileListIsOld 
#======================================================================= 
proc ResetDataFileList {} {
   global dataFileList       # list of the selected files
   global dataFileListIsOld  # flag: =1 when list dataFileList is empty;
                             #       =0 when this list is ready 
   set dataFileList ""
   set dataFileListIsOld 1
}

set existingDataFile ""
set lastFileList [list ]
set fileListUpdateTime 0
    
set lastSparsingInterval 1
set lastSparsingMethod ""

proc FindFiles {args} {
    global StartMonth StartYear StartDay StartHour
    global EndMonth EndYear EndDay EndHour
    global startTime endTime 
    global existingDataFile    # merged data stored in this file
    global fileListUpdateTime  # time of the last update of current list 
                               # lastFileList with selected files
    global dataDir             # directory with main data files
    global dataFileListIsOld   # flag: =1 when list dataFileList is empty;
                               #       =0 when this list is ready 
    global lastFileList        # current list with selercted files
    global sparsingInterval sparsingType
    global selectedColumns selectedPressureList selectedList knownColumnList
    
    set Rootname SRvac 

    set startDate [APSFormatDate -year $StartYear -month $StartMonth -day $StartDay -dateFormat list]
    if [catch {APSConvertTimeToHours $StartHour} hour] {
        SetStatusText "Bad time value: $StartHour"
        return
    }
    set startTime [exec timeconvert -breakDown=year=$StartYear,day=$StartDay,month=$StartMonth,hour=$hour ]
    
    set endDate [APSFormatDate -year $EndYear -month $EndMonth -day $EndDay -dateFormat list]
    if [catch {APSConvertTimeToHours $EndHour} hour] {
        SetStatusText "Bad time value: $EndHour"
        return
    }
    set endTime [exec timeconvert -breakDown=year=$EndYear,day=$EndDay,month=$EndMonth,hour=$hour ]
   
    set dataFileList [APSFindFilesBetweenDates -rootname $Rootname- \
                        -directory $dataDir -startDateList $startDate -endDateList $endDate ]

    SetStatusText "[llength $dataFileList] files found."

    if [llength $dataFileList]==0 return

    set lastFileList $dataFileList
    set fileListUpdateTime [clock seconds]

    # Merge all the data files
    global env lastSelectedColumns

    set lastSelectedColumns $selectedColumns
    set selectedColumns -retain=column,S-DCCT:*,S-INJ:MPS:*,Time,SRBucketsFilled
    set selectedPressureList [list ]
    foreach item $selectedList(ring) {
        if [lsearch -exact $knownColumnList $item]!=-1 {
            append selectedColumns ,$item
            lappend selectedPressureList $item
        }
    }

    set outputFile /tmp/SRvac-$env(USER)-[expr int($startTime)]-[expr int($endTime)]-$sparsingInterval-$sparsingType.sdds
    if [string compare $selectedColumns $lastSelectedColumns] {
        set regenerate 1
    } else {
        if [file exists $outputFile] {
            set regenerate 0
            set lastUpdateTime [file mtime $outputFile]
            foreach dataFile $dataFileList {
                if [file mtime $dataFile]>$lastUpdateTime {
                    set regenerate 1
                    break
                }
            }
        } else {
            set regenerate 1
        }
    }
    set existingDataFile ""
    if !$regenerate {
        SetStatusText "Using cached file $outputFile"
        set existingDataFile $outputFile
        return $outputFile
    }
    
    APSAddToTempFileList $outputFile
    if {$sparsingInterval<=1} {
        SetStatusText "Merging data files..."
        if [catch {eval exec sddscombine -merge -overwrite $selectedColumns $dataFileList $outputFile} result] {
            SetStatusText "Merging problem with file $outputFile: $result"
            return ""
        }
    } else {
        SetStatusText "Merging and sparsing data files..."
        set sparsingOption -sparse=$sparsingInterval
        if [string length $sparsingType]!=0 {
            append sparsingOption ,$sparsingType
        }
        if [catch {eval exec sddscombine -merge -overwrite $selectedColumns $dataFileList $sparsingOption $outputFile} result] {
            SetStatusText "Merging problem with file $outputFile: $sparsingOption $result"
            return ""
        }
    }
    
    SetStatusText "Files merged"

    set existingDataFile $outputFile
    return $outputFile
}

#======================================================================= 
# Creating all menu in working window
#======================================================================= 

MakeMainWindow .plotting -parent .userFrame
MakeDateTimeFrame .datetime -parent .userFrame
MakePreprocessingFrame .preproc -parent .userFrame
MakeFiltersFrame .filters -parent .userFrame

update
foreach item {ID:IP1 :IP9} {
    set ipS$item 0
    APSSRSectorTogglePosition -item $item -rootname ip -toggleVar ipS$item -sectorCount 40
}

SetStatusText "Ready."
SetStatusText "ID:IP1 and IP9 considered unreliable and thus are deselected."
update
