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

#
# $Log: not supported by cvs2svn $
# Revision 1.25  2009/01/21 17:22:46  shang
# removed waveform files from the snapshot list
#
# Revision 1.24  2008/05/19 18:55:28  shang
# added option of including SCR attic
#
# Revision 1.23  2007/03/29 20:48:00  shang
# modified the preset button to compare PVs that are in baseline file instead of fixed number of PVs.
#
# Revision 1.22  2005/12/02 22:55:24  emery
# Changes to SR BPM history processing. Removed contraint of requiring
# 720 bpms setpoint PVs. Added the P0 bpms. Changed calculation of
# Sector column. Replace exec rm with file delete commands.
# There is still the problem of files that may have different
# number of setpoint PVs that has to be solved.
#
# Revision 1.21  2000/04/03 21:17:59  emery
# Made the wildcard string for selecting 720 bpms of the SCR
# file more specific in order to guarantee 720 rows.
#
# Revision 1.20  2000/04/03 20:17:25  emery
# Added number of rows test before using APSGetSDDSColumn
# on $pvListFile to prevent a tcl error in case there are no rows.
#
# Revision 1.19  2000/03/22 19:54:46  emery
# Added -noWarning option to sddsxref commands.
#
# Revision 1.18  1999/05/18 15:23:26  borland
# Fixed bugs in SR special processing for orbit setpoints.
#
# Revision 1.17  1998/07/07 19:20:03  borland
# Fixed problem with sextupole wildcard string in SRConverterSPlots.
#
# Revision 1.16  1998/01/04 22:48:48  borland
# Modified to use new column deletion feature of sddscombine.
#
# Revision 1.15  1997/10/24 16:01:33  borland
# Modified to permit viewing string readbacks as well as numerical readbacks,
# but had to comment it out because sddstranspose won't transpose string data.
#
# Revision 1.14  1997/05/02 17:04:25  borland
# Added commandline option to allow retargeting to use a different SCR
# directory.
#
# Revision 1.13  1997/04/23 23:24:07  borland
# Added more features for SR special comparisons.  Now allows selecting
# comparisons of earlier, later, or all snapshots (relative to baseline).
#
# Revision 1.12  1997/04/23 22:13:40  borland
# Improved SR special processing.  Now has more types of analysis and simpler
# code.  Also allows data exporting.
#
# Revision 1.11  1997/04/21 22:15:40  borland
# Removed APSChooseItemFromList and put in a library.
#
# Revision 1.10  1997/04/19 14:04:00  borland
# Fixed bug in last change.
#
# Revision 1.9  1997/04/19 14:02:37  borland
# Provided choice of CurrentAI or CurrentAO for corrector analysis.
#
# Revision 1.8  1997/04/14 22:14:53  borland
# Improved SR special processing.  Includes ability to pick the reference
# snapshot.
#
# Revision 1.7  1997/04/10 19:55:09  borland
# Added special processing buttons for SR.
#
# Revision 1.6  1996/09/19 21:51:31  borland
# Changed from using "exec wish" to "exec oagwish".
#
# Revision 1.5  1996/08/29 20:28:08  borland
# Switched to new variable names for SCR procedures.  Removed
# APSSCRSystemChoiceWidget and APSSCRDateTimeEntryWidget; now in
# SCRprocedures.tcl
#
# Revision 1.4  1996/08/16 15:49:45  borland
# Added "Export and plot..." button, giving option to
# export data and launch quickSDDSplot.  Added sorting
# of data by Time column.
#
# Revision 1.3  1996/07/18 19:49:25  borland
# Really switched to right snapshot directory.
#
# Revision 1.2  1996/07/18 16:31:54  borland
# Added context help for Plot and Export buttons on data selection dialog.
#
# Revision 1.1  1996/07/17 23:13:29  borland
# First version of utility to view/export SCR data histories.
#
#

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.26 $ \$Author: shang $"

set topDirectory ""
set args $argv
APSStrictParseArguments {topDirectory}
APSSCRDefineVariables -topDirectory $topDirectory

set SRSpecialProcessingWidgetAvailable 1
proc SRSpecialProcessingWidget {widget args} {
    set parent ""
    APSStrictParseArguments {parent}
    
    global SRSpecialProcessingExportQuery SRSpecialProcessingCompareDirection
    set SRSpecialProcessingExportQuery 0
    set SRSpecialProcessingCompareDirection all

    APSFrame $widget -parent $parent -label "SR Special Processing"  \
      -contextHelp "Allows application of processing scripts unique to the SR."
    set w $parent$widget.frame

    APSFrameGrid .fg -parent $w -yList {zero one two three}

    APSRadioButtonFrame .rb0 -parent $w.fg.zero \
      -label "Export results: " -buttonList {Yes No} \
      -valueList {1 0} -variable SRSpecialProcessingExportQuery \
      -contextHelp "Choose whether this application will bring up a dialog to permit exporting the results of special processing." \
      -packOption "-side left"
    APSRadioButtonFrame .rb1 -parent $w.fg.zero \
      -label "Compare to baseline: " -buttonList {Earlier Later All} \
      -valueList {earlier later all} -variable SRSpecialProcessingCompareDirection \
      -contextHelp "Choose which snapshots to compare to the baseline snapshot (which you choose).  You can compare only snapshots earlier or later than the baseline, or you can compare all snapshots for the days you've chosen." \
      -packOption "-side left"

    APSButton .spOrbit -parent $w.fg.one -text "Orbit setpoints" \
      -command {SRSetpointSPlots -exportQuery $SRSpecialProcessingExportQuery \
            -compareDirection $SRSpecialProcessingCompareDirection} \
      -contextHelp "Analyzes and plots changes in BPM setpoints."

    APSButton .offOrbit -parent $w.fg.one -text "Orbit offsets" \
      -command {SRSetpointSPlots -exportQuery $SRSpecialProcessingExportQuery \
            -quantity offset \
            -compareDirection $SRSpecialProcessingCompareDirection} \
      -contextHelp "Analyzes and plots changes in BPM offsets."

    APSButton .wHOrbitAO -parent $w.fg.two -text "H setpoints" \
      -command {SRConverterSPlots -group H -readback 0 \
                  -compareDirection $SRSpecialProcessingCompareDirection \
                  -exportQuery $SRSpecialProcessingExportQuery} \
      -contextHelp "Analyzes and plots changes in H corrector setpoints"
    APSButton .wVOrbitAO -parent $w.fg.two -text "V setpoints" \
      -command {SRConverterSPlots -group V -readback 0 \
                  -compareDirection $SRSpecialProcessingCompareDirection \
                  -exportQuery $SRSpecialProcessingExportQuery} \
      -contextHelp "Analyzes and plots changes in V corrector setpoints"
    APSButton .wQOrbitAO -parent $w.fg.two -text "Quad setpoints" \
      -command {SRConverterSPlots -group Q -readback 0 \
                  -compareDirection $SRSpecialProcessingCompareDirection \
                  -exportQuery $SRSpecialProcessingExportQuery} \
      -contextHelp "Analyzes and plots changes in quadrupole setpoints"
    APSButton .wSOrbitAO -parent $w.fg.two -text "Sext setpoints" \
      -command {SRConverterSPlots -group S -readback 0 \
                  -compareDirection $SRSpecialProcessingCompareDirection \
                  -exportQuery $SRSpecialProcessingExportQuery} \
      -contextHelp "Analyzes and plots changes in sextupole setpoints"

    APSButton .wHOrbitAI -parent $w.fg.three -text "H readbacks" \
      -command {SRConverterSPlots -group H -readback 1 \
                  -compareDirection $SRSpecialProcessingCompareDirection \
                  -exportQuery $SRSpecialProcessingExportQuery} \
      -contextHelp "Analyzes and plots changes in H corrector readbacks"
    APSButton .wVOrbitAI -parent $w.fg.three -text "V readbacks" \
      -command {SRConverterSPlots -group V -readback 1 \
                  -compareDirection $SRSpecialProcessingCompareDirection \
                  -exportQuery $SRSpecialProcessingExportQuery} \
      -contextHelp "Analyzes and plots changes in V corrector readbacks"
    APSButton .wQOrbitAI -parent $w.fg.three -text "Quad readbacks" \
      -command {SRConverterSPlots -group Q -readback 1 \
                  -compareDirection $SRSpecialProcessingCompareDirection \
                  -exportQuery $SRSpecialProcessingExportQuery} \
      -contextHelp "Analyzes and plots changes in quadrupole readbacks"
    APSButton .wSOrbitAI -parent $w.fg.three -text "Sext readbacks" \
      -command {SRConverterSPlots -group S -readback 1 \
                  -compareDirection $SRSpecialProcessingCompareDirection \
                  -exportQuery $SRSpecialProcessingExportQuery} \
      -contextHelp "Analyzes and plots changes in sextupole readbacks"
}

proc SRSetpointSPlots {args} {
    set exportQuery 0
    set compareDirection all
# original procedure was written for septoints.  offsets would run the same
# way, so I replaced various coding of "Setpoint" using variables which
# could be setpoint or offset.
    set quantity setpoint
    set quantityLabel Setpoint
    set quantityPV SetpointAO
    APSStrictParseArguments {exportQuery compareDirection quantity}
    switch $quantity {
	setpoint {
	    set quantityPV SetpointAO
	    set quantityLabel Setpoint
	}
	offset {
	    set quantityPV OffsetAO
	    set quantityLabel Offset
	}
	default {
	    set quantityPV SetpointAO
	    set quantityLabel Setpoint
	    }
    }
    global fileList shortNames
    
    set refFileIndex [APSChooseItemFromList -itemList $shortNames \
                        -name "Choose your baseline configuration."]
    if $refFileIndex<0 {
        SetStatusText "Cancelled."
        return
    }
    set refFile [lindex $fileList $refFileIndex]
    set refTmpFile /tmp/[APSTmpString]
    APSAddToTempFileList $refTmpFile
    SetStatusText "[file rootname [file tail $refFile]] is baseline."

    switch $compareDirection {
        earlier {
            set localFileList [lrange $fileList 0 $refFileIndex]
        }
        later {
            set localFileList [lrange $fileList $refFileIndex end]
        }
        default {
            set localFileList $fileList
        }
    }
    
    SetStatusText "Processing..."
    # process the reference file
    if [catch {exec sddsprocess $refFile -pipe=out \
		   -match=column,ControlName=S*\[ABC\]:P\[0-5\]:ms:?:[set quantityPV],ValueString=_NoConnection_,!,& \
		   | sddsprocess -pipe \
		   -scan=column,[set quantityLabel]Value,ValueString,%le,units=mm \
		   -edit=column,Plane,ControlName,3Z:1fD \
		   -edit=column,BPMName,ControlName,%/:ms://%/x://%/y://%/[set quantityPV]// \
		   | sddsxref -pipe /home/helios/oagData/SCR/miscInputs/BPMPositions.xref \
		   -match=BPMName -take=s -reuse=page,row -noWarning \
		   | sddssort -pipe -column=Plane,incr -column=s,incr \
		   | tee $refTmpFile \
		   | sdds2stream -rows -pipe | token -n=1} result] {
        SetStatusText "SRSetpointSPlots(1): $result"
        return
    }
#    if {$result!=720} {
#        SetStatusText "The baseline file you choose is incomplete---please select another."
#        return
#    }

    set tmpFile /tmp/[APSTmpString]
    APSAddToTempFileList $tmpFile $tmpFile.x $tmpFile.xy
    if [catch {eval exec sddscombine $localFileList -pipe=out \
		   -retain=column,ValueString,ControlName \
		   | sddsselect -pipe $refTmpFile \
		   -reuse=page,row -match=ControlName -nowarning \
		   | sddsprocess -pipe -match=column,ValueString=_NoConnection_,! \
		   | sddsprocess -pipe \
		   -scan=column,[set quantityLabel]Value,ValueString,%le,units=mm \
		   -edit=column,Plane,ControlName,3Z:1fD \
		   -edit=column,BPMName,ControlName,%/:ms://%/x://%/y://%/[set quantityPV]// \
		   | sddsxref -pipe /home/helios/oagData/SCR/miscInputs/BPMPositions.xref \
		   -match=BPMName -take=s -reuse=page,row -noWarning \
		   | sddssort -pipe -column=Plane,incr -column=s,incr \
		   | sddschanges -pipe=in $tmpFile -copy=s,Plane,BPMName -changesIn=[set quantityLabel]Value \
		   -baseline=$refTmpFile} result] {
        SetStatusText "SRSetpointSPlots(2): $result"
        return
    }
    if [catch {exec sddsprocess $tmpFile -pipe=out -match=column,Plane=x \
                 | sddsconvert -pipe=in $tmpFile.x \
                 -rename=column,ChangeIn[set quantityLabel]Value=x[set quantityLabel]Delta} result] {
        SetStatusText "SRSetpointSPlots(3): $result"
        return
    }
    if [catch {exec sddsprocess $tmpFile -pipe=out -match=column,Plane=y \
                 "-def=col,Sector,s 27.6 / 1 +" \
                 | sddsxref -pipe $tmpFile.x -match=BPMName -noWarning \
                 | sddsconvert -pipe=in $tmpFile.xy \
                 -rename=column,ChangeIn[set quantityLabel]Value=y[set quantityLabel]Delta} result] {
        SetStatusText "SRSetpointSPlots(4): $result"
        return
    }
    if $exportQuery {
        exec sddsExportData -dataFileList $tmpFile.xy \
          -message "Export interface for orbit $quantity analysis from ViewSCRHistory" &
    }
    exec sddsplot -column=Sector,x[set quantityLabel]Delta \
      "-topline=x $quantity changes from [file tail [file rootname $refFile]]" \
      -graph=impulse \
      $tmpFile.xy -split=page -sep=page -title=@TimeStamp -limit=ymax=0.5,ymin=-0.5,autoscaling &
    exec sddsplot -column=Sector,y[set quantityLabel]Delta \
      "-topline=y $quantity changes from [file tail [file rootname $refFile]]" \
      -graph=impulse \
      $tmpFile.xy -split=page -sep=page -title=@TimeStamp -limit=ymax=0.5,ymin=-0.5,autoscaling &
}

proc SRConverterSPlots {args} {
    global fileList shortNames
    set group ""
    set readback 0
    set exportQuery 0
    set compareDirection all
    APSStrictParseArguments {group readback exportQuery compareDirection}

    set groupTagList {H V Q S}
    set groupNameList {"H corrector" "V corrector" "Quadrupole" "Sextupole"}
    set groupMatchStringList {S*:H[1-4]: S*:V[1-4]: S*:Q[1-5]: S*:S[1-4]:}
    set groupPVCountList {320 320 400 280}
    set groupIndex [lsearch $groupTagList $group]
    if $groupIndex==-1 {
        return -code error "APSSCRSRCompareConvertors: group $group is unknown."
    }
    set groupName [lindex $groupNameList $groupIndex]
    set pvCount [lindex $groupPVCountList $groupIndex]
    set matchString [lindex $groupMatchStringList $groupIndex]
    
    set refFileIndex [APSChooseItemFromList -itemList $shortNames \
                        -name "Choose your baseline configuration."]
    if $refFileIndex<0 {
        SetStatusText "Cancelled."
        return
    }
    set refFile [lindex $fileList $refFileIndex]

    switch $compareDirection {
        earlier {
            set localFileList [lrange $fileList 0 $refFileIndex]
        }
        later {
            set localFileList [lrange $fileList $refFileIndex end]
        }
        default {
            set localFileList $fileList
        }
    }

    if $readback {
        set title "$groupName readback changes from [file tail [file rootname $refFile]]"
        set currentType CurrentAI
    } else {
        set title "$groupName setpoint changes from [file tail [file rootname $refFile]]"
        set currentType CurrentAO
    }

    SetStatusText "[file rootname [file tail $refFile]] is baseline."
    SetStatusText "Processing..."
    set refTmpFile /tmp/[APSTmpString]
    APSAddToTempFileList $refTmpFile
    if [catch {exec sddsprocess $refFile -pipe=out -nowarnings \
                 -match=column,ControlName=$matchString${currentType},ValueString=_NoConnection_,!,&  \
                 -scan=column,${currentType}ValueRef,ValueString,%le,units=mm \
                 -edit=column,ElementName,ControlName,%/:${currentType}// \
                 | sddsxref /home/helios/oagData/SCR/miscInputs/ConverterMagnetPositions.xref \
                 -pipe -match=ElementName -take=s -reuse=page,row -noWarning \
                 | sddssort -pipe=in $refTmpFile -column=s,incr} result] {
        SetStatusText "SRConverterSPlots(1): $result"
        return
    }
    set tmpFile /tmp/[APSTmpString]
    APSAddToTempFileList $tmpFile
    if [catch {eval exec sddscombine $localFileList -pipe=out \
                   -retain=column,ControlName,ValueString \
                   | sddsprocess -pipe -nowarnings \
                   -match=column,ControlName=\$matchString\$currentType,ValueString=_NoConnection_,!,&  \
                   -scan=column,${currentType}Value,ValueString,%le,units=A \
                   -edit=column,ElementName,ControlName,%/:${currentType}// \
                   | sddsxref /home/helios/oagData/SCR/miscInputs/ConverterMagnetPositions.xref \
                   -pipe -match=ElementName -take=s -reuse=page,row -noWarning \
                   | sddssort -pipe -column=s,incr \
                   | sddsxref -pipe $refTmpFile -match=ElementName -nowarnings -take=${currentType}ValueRef \
                   | sddsprocess -pipe=in $tmpFile -nowarnings\
                   \"-define=col,ChangeIn${currentType}Value,${currentType}Value ${currentType}ValueRef -\" \
               } result1] {
        SetStatusText "SRConverterSPlots(2): $result1"
        return 
    }
    if $exportQuery {
        exec sddsExportData -dataFileList $tmpFile \
            -message "Export interface for $group $currentType analysis from ViewSCRHistory" &
    }
    exec sddsplot -column=s,ChangeIn${currentType}Value \
        "-topline=$title" \
        -graph=impulse -ticks=xspacing=110.4 -subticks=xdiv=4 \
        $tmpFile -split=page -sep=page -title=@TimeStamp -limit=ymax=150,ymin=-150,autoscaling &
}


# list of files selected for viewing
set fileList ""
# name of widget for category checkbutton lists
set categoryFrameWidget ""
# filter for PV names to go along with category selections
set pvNameFilter *
set useNumerical y

proc SetStatusText {text} {
    global statusText
    set statusText "$text ([exec date +%H:%M:%S])"
    update
}

proc ResetViewingChoices {} {
    global fileList categoryFrameWidget pvNameFilter
    set fileList ""
    set pvNameFilter *
    if [winfo exists $categoryFrameWidget] {
        destroy $categoryFrameWidget
    }
}

proc SearchForSnapshots {widget args} {
    global fileList apsSCRSnapDir apsSCRSystem apsSCRAtticDir shortNames
    global apsSCRConfigStartYear apsSCRConfigStartMonth apsSCRConfigStartDay apsSCRConfigStartHour
    global apsSCRConfigEndYear apsSCRConfigEndMonth apsSCRConfigEndDay apsSCRConfigEndHour
    global categoryFrameWidget pvNameFilter includeAttic
    global ${apsSCRSystem}SpecialProcessingWidgetAvailable
    set parent .
    APSStrictParseArguments {parent}

    ResetViewingChoices 
    SetStatusText "Searching..."
    set snapDir $apsSCRSnapDir/$apsSCRSystem
    set fileList \
      [APSFindFilesBetweenDates -directory $snapDir -rootname $apsSCRSystem \
         -startDateList \
         [APSFormatDate -year $apsSCRConfigStartYear -month $apsSCRConfigStartMonth \
            -day $apsSCRConfigStartDay -twoDigitYear 0 -dateFormat list] \
         -endDateList \
         [APSFormatDate -year $apsSCRConfigEndYear -month $apsSCRConfigEndMonth \
            -day $apsSCRConfigEndDay -twoDigitYear 0 -dateFormat list] \
         -extensionList {".gz"} -excludeList "waveform"
      ]
    set lastIndex [expr [llength $fileList] -1]
    set pathname [file dirname [lindex $fileList 0]]
    regsub -all $pathname/ $fileList {} shortNames0
    if $includeAttic {
        set snapDir1 $apsSCRAtticDir/$apsSCRSystem
        set fileList1 \
            [APSFindFilesBetweenDates -directory $snapDir1 -rootname $apsSCRSystem \
                 -startDateList \
                 [APSFormatDate -year $apsSCRConfigStartYear -month $apsSCRConfigStartMonth \
                      -day $apsSCRConfigStartDay -twoDigitYear 0 -dateFormat list] \
                 -endDateList \
                 [APSFormatDate -year $apsSCRConfigEndYear -month $apsSCRConfigEndMonth \
                      -day $apsSCRConfigEndDay -twoDigitYear 0 -dateFormat list] \
                 -extensionList {".gz"}
            ]
        set fileList [concat $fileList $fileList1]
        if [llength $fileList1] {
            foreach file $fileList1 {
                lappend shortNames0 attic/[file tail $file]
            }
        }
    }
    regsub -all .gz $shortNames0 {} shortNames
    if [llength $fileList]<2 {
        bell
        SetStatusText "Too few snapshots found (only [llength $fileList])."
        return
    }
    SetStatusText "[llength $fileList] snapshots found"
    SetStatusText "Making category buttons"
    if $lastIndex>=0 {
        set lastFile [lindex $fileList $lastIndex]
    } else {
        set lastFile [lindex $fileList end]
    }
    if [regexp {attic} $lastFile] {
        set attic 1
    } else {
        set attic 0
    }
    set rootname [file rootname [file tail $lastFile]]
    if $attic {
        if {[file exists $apsSCRAtticDir/$apsSCRSystem/categories]} {
            set categories $apsSCRAtticDir/$apsSCRSystem/categories/categ$rootname
        } else {
            set categories $apsSCRAtticDir/$apsSCRSystem/categ$rootname
        }
        if {[file exists $apsSCRAtticDir/$apsSCRSystem/beamlines]} {
            set beamlines $apsSCRAtticDir/$apsSCRSystem/beamlines/beam$rootname
        } else {
            set beamlines $apsSCRAtticDir/$apsSCRSystem/beam$rootname
        }
    } else {
        if {[file exists $apsSCRSnapDir/$apsSCRSystem/categories]} {
            set categories $apsSCRSnapDir/$apsSCRSystem/categories/categ$rootname
        } else {
            set categories $apsSCRSnapDir/$apsSCRSystem/categ$rootname
        }
        if {[file exists $apsSCRSnapDir/$apsSCRSystem/beamlines]} {
            set beamlines $apsSCRSnapDir/$apsSCRSystem/beamlines/beam$rootname
        } else {
            set beamlines $apsSCRSnapDir/$apsSCRSystem/beam$rootname
        }
    }
    if ![file exists $categories] {
        SetStatusText "Generating missing system category file"
        exec sddssort $lastFile $categories -unique -column=Category
    }
    
    if ![file exists $beamlines] {
        SetStatusText "Generating missing beamline category file"
        exec sddssort $lastFile $beamlines -unique -column=Beamline
    }

    set categoryFrameWidget $parent$widget
    if [winfo exists $parent$widget] {
        destroy $parent$widget
        update
    }
    if [info exists ${apsSCRSystem}SpecialProcessingWidgetAvailable] {
        APSFrameGrid $widget -parent $parent -yList {top bottom} 
        APSFrame .categ -parent $parent$widget.top -label "Select Categories and Filter PV Names"
        set w $parent$widget.top.categ.frame
        set w2 $parent$widget.bottom
    } else {
        APSFrame $widget -parent $parent -label "Select Categories and Filter PV Names"
        set w $parent$widget.frame
    }
    APSLabeledEntry .pvname -parent $w -textVariable pvNameFilter -width 20  \
      -label "PV name filter: " -packOption "-side top" \
      -contextHelp  \
      "Enter a string to use in filtering process variable names prior to review/restore.
 The character * matches zero or more arbitrary characters, while the character ? matches any single character.  So S*A:Q1* would match all the A:Q1 quadrupole process variables for the SR."

    
    # remove this, as the implied feature won't work until sddstranspose is
    # upgraded to transpose string columns
    #    APSRadioButtonFrame .numnonum -parent $w -variable useNumerical -buttonList \
      {Yes No} -valueList {y n} -orientation horizontal -label "Numerical PVs:" \
      -contextHelp "Use to select processing either of numerical or nonnumerical PVs."
    
    APSSDDSCheckButtons .list1 -parent $w -fileName $categories -column Category \
      -arrayName apsSCRFilterCategory -page 0 -limit 7 -duplicate 0 -modal 0 \
      -noOk 1 -noCancel 1
    APSSDDSCheckButtons .list2 -parent $w -fileName $beamlines -column Beamline \
      -arrayName apsSCRFilterBeamline -page 0 -limit 7 -duplicate 0 -modal 0 \
      -noOk 1 -noCancel 1
    APSButton .scan -parent $w -text "Scan" \
      -contextHelp \
      "Scans snapshots to make list from which items may be selected for viewing." \
      -command ScanSnapshots

    if [info exists ${apsSCRSystem}SpecialProcessingWidgetAvailable] {
        ${apsSCRSystem}SpecialProcessingWidget .special -parent $w2 
    }

}

proc ScanSnapshots {} {
    global fileList 
    global apsSCRFilterCategory apsSCRFilterBeamline pvNameFilter useNumerical
    if [llength $fileList]==0 {
        SetStatusText "No files selected."
        return
    }
    SetStatusText "Scanning..."
    set lastFile [lindex $fileList end]
    set rootname [file tail [file rootname $lastFile]]
    set matchOpt [APSMakeSCRMatchOptions $lastFile \
                    -includeReadOnlys 1 -protectionLock 0 \
                    -pvNameFilter $pvNameFilter]
    set pvListFile /tmp/[APSTmpString]
    
    if [catch {eval exec sddsprocess $lastFile $pvListFile \
                 $matchOpt -match=col,IsNumerical=$useNumerical -nowarning} result] {
        SetStatusText $result
        return
    }
    if [catch {APSGetSDDSRowCount -fileName $pvListFile} rows] {
        SetStatusText "$rows"
        return
    }
    if {!$rows} {
        SetStatusText "No matching numerical data"
        return
    }
    if [catch {APSGetSDDSColumn -fileName $pvListFile \
                 -column ControlName} pvNameList ] {
        SetStatusText "$pvNameList"
        return
    }
    if [string length $pvNameList]==0 {
        SetStatusText "No matching numerical data"
        return
    }
    SetStatusText "[llength [split $pvNameList]] matching data elements"
    file delete $pvListFile

    set widget [APSUniqueName .]
    APSScrolledListWindow $widget -name "ViewSCRHistory PV Selection" \
      -label "Select PV Names for Viewing" \
      -itemList $pvNameList -selectionVar pvNameSelectionList
    set basicCommand [$widget.buttonRow.accept.button cget -command]
    pack forget $widget.buttonRow.close.button
    $widget.buttonRow.accept.button configure -text Plot \
      -command "$basicCommand; MakeSCRHistoryPlots"
    APSSetContextHelp $widget.buttonRow.accept.button \
      -contextHelp "Make plots of the selected quantities vs. time."
    APSButton .export -parent $widget.buttonRow -text "Export..." \
      -command "$basicCommand; ExportSCRHistoryData -plot 0" \
      -contextHelp "Export data for the selected quantities to an SDDS file."
    APSButton .exportqpl -parent $widget.buttonRow -text "Export and plot..." \
      -command "$basicCommand; ExportSCRHistoryData -plot 1" \
      -contextHelp "Export data for the selected quantities to an SDDS file, then launch quickSDDSplot to plot the data."
    APSButton .done -parent $widget.buttonRow -text "Done" \
      -command "destroy $widget" -contextHelp \
      "Closes the selection window without taking any other action."
}

proc ProcessSCRHistoryData {args} {
    set dataFile ""
    APSStrictParseArguments {dataFile}

    global fileList apsSCRSystem pvNameSelectionList useNumerical
    set filterFile /tmp/[APSTmpString]
    set ffID [open $filterFile w]
    puts $ffID "SDDS1\n&column name=ControlName type=string &end"
    puts $ffID "&data mode=ascii &end\n[llength $pvNameSelectionList]"
    foreach elem $pvNameSelectionList {
        puts $ffID $elem
    }
    close $ffID
    set expectedCount [llength $pvNameSelectionList]
    if [string length $dataFile]==0 {
        set dataFile /tmp/[APSTmpString]
    }
    SetStatusText "Processing data files..."

    if [string compare $useNumerical y]==0 {
        set scanOption -scan=column,Value,ValueString,%le 
    } else {
        set scanOption -print=column,Value,%s,ValueString
    }
    
    eval exec sddscombine $fileList -pipe=out \
	-retain=col,ControlName,ValueString \
	| sddsselect -pipe -reuse=page $filterFile -match=ControlName \
	| sddsprocess -pipe -match=column,ValueString=_NoConnection_,! -nowarnings \
	| sddsprocess -pipe -nowarning {"-test=param,n_rows $expectedCount =="} \
	$scanOption \
	| sddsconvert -pipe -retain=col,ControlName,Value \
	| sddssort -pipe -column=ControlName \
	| sddstranspose -pipe \
	| sddsprocess -pipe -process=*,first,%s \
	| sddscollapse -pipe \
	| sddssort -pipe=in $dataFile -column=Time

    file delete $filterFile
    return $dataFile
}

proc MakeSCRHistoryPlots {} {
    global apsSCRSystem pvNameSelectionList
    set dataFile [ProcessSCRHistoryData]
    exec sddsplot -column=Time,([join $pvNameSelectionList ,]) \
      -graph=sym -separate $dataFile \
      -ticks=xtime \
      "-title=SCR History data for system $apsSCRSystem" &
    SetStatusText "Plot launched."
#    after 6000 "file delete $dataFile"
}

proc ExportSCRHistoryData {args} {
    global apsSCRSystem pvNameSelectionList
    set plot 0
    APSStrictParseArguments {plot}
    set dataFile [APSInfoDialog .[APSTmpString] \
                    -name "SCR History Export File Query" \
                    -infoMessage "Enter the name of the file to copy the data to." \
                    -width 80]
    if [string length $dataFile]==0 return
    ProcessSCRHistoryData -dataFile $dataFile
    SetStatusText "File $dataFile created."
    if $plot {
        exec quickSDDSplot -dataFileList $dataFile -autoTimeMode 1 &
    }
}

set apsSCRAtticDir /home/helios/oagData/SCR/attic
APSApplication . -name ViewSCRHistory -version $CVSRevisionAuthor \
  -overview "Plots and prints data from save/compare/restore system snapshot files as a function of time."
set statusText Ready.
APSScrolledStatus .status -parent .userFrame -textVariable statusText \
  -width 80

APSSCRSystemChoiceWidget .choice -parent .userFrame -variable apsSCRSystem \
  -command ResetViewingChoices 
set apsSCRSystem SR
APSSCRDateTimeEntryWidget .date -parent .userFrame \
  -command ResetViewingChoices
set includeAttic 0
APSRadioButtonFrame .attic -parent .userFrame.date.frame -label "Include SCR attic?" -buttonList {Yes No} -valueList {1 0} \
    -variable includeAttic -contextHelp "if Yes is selected, the files will also include those in SCR attic." -orientation horizontal
APSButton .search -parent .userFrame.date.frame -text "Search" \
  -contextHelp \
  "Searches for snapshots for the system and date/time range of interest." \
  -command "SearchForSnapshots .categ -parent .userFrame"
