
set bpmList {A:P2 A:P3 A:P4 B:P5 B:P4 B:P3 B:P2}
set bpmVarList {AP2 AP3 AP4 BP5 BP4 BP3 BP2}
set MSavg 2
set gain 0
set multiplier 16
set number [expr int(pow(2,$MSavg) * $multiplier)]
set abortFlag 0
set dataFlag 1
set histFlag 0
set averageFlag 0

set ControlStatus "Ready."

#Make application and status window widgets.  Returns 1 when completed.

proc MakeSRBPMCheckoutStatusWidget {widget args} {

    global ControlStatus
    set parent ""
    set system SR
    APSStrictParseArguments {parent system}
    APSApplication . -name "$system BPM Self Test" \
      -overview "SR BPM self test procedure interface." -version "Version: 1.1   Author: N. Sereno"

    set w $parent$widget
    APSScrolledStatus .status -parent $w -textVariable ControlStatus -width 100 -packOption {-side top}
    return 1
}

#Make BPM self test and file selection widgets.  Returns 1 when completed.

proc MakeSRBPMSelfTestWidget {widget args} {
    global ControlStatus SectorSelection gain SelfTestDataFileName \
      contentsString abortFlag processFlag

    set parent ""
    set system SR
    APSStrictParseArguments {parent system}

    APSFrame $widget -parent $parent -label "" \
      -height 30 \
      -packOption {-side top -expand 1}

    set w $parent$widget.frame
    if {$system=="SR" || $system=="Booster"} {
	set btext1 "Perform Self Test"
	set btext2  "Abort Self Test"
    } else {
	set btext1 "Perform Test"
	set btext2 "Abort Test"
    }
    APSButton .selfTestButton -parent $w \
        -text $btext1 \
        -command "PerformBPMSelfTest -statusCallback SetStatus" \
        -packOption {-side left -fill x} \
        -contextHelp "This button invokes a procedure that performs a $system BPM self test on either a single sectors worth of $system BPMs or on all $system BPMs.  Lists of BPMs that failed the self test are produced as well as plots."  
    
    APSButton .abortSelfTestFrame -parent $w \
      -text $btext2 \
      -command {SetFlagsOnAbort} \
      -packOption {-side left -fill x} \
      -contextHelp "This button aborts the self test procedure when it is running."

    APSButton .cleanupSelectionFrame -parent $w \
      -text "Clean Up" \
      -command {CleanupProcessedBPMDataFiles -statusCallback SetStatus} \
      -packOption {-side left -fill x} \
      -contextHelp "This button removes *Reg* and *Hist* processed data files from the /tmp directory."

    APSButton .updateContentsFrame -parent $w \
      -text "Update Contents Description" \
      -command {UpdateDataFileContents -statusCallback SetStatus} \
      -packOption {-side left -fill x} \
      -contextHelp "This button enables the user to change the data file contents description parameter."
    
    return 1
}


# Makes labelled entry widgets for the memory scanner boxcar average,
# BPM gain mode, number of BPM Self Tests, and self test data file.
# The data file is automatically generated based on the most recent
# version.
proc UpdateSelfTestNumber {args} {
    global number MSavg multiplier
    set number [expr int(pow(2,$MSavg) * $multiplier)]
}

proc MakeLabelledEntryWidgets {widget args} {
    global MSavg SelfTestDataFileName SelfTestDataFileRootName gain number multiplier MostRecentFileName 
    global dataFlag histFlag averageFlag directory CompareDataFileName 
    set parent ""
    set fileWidth 80
    set system SR
    APSStrictParseArguments {parent fileWidth system}
    
    set year ""
    set month ""
    set day ""
    APSDateBreakDown -monthVariable month -yearVariable year -twoDigitYear 0 -dayVariable day        

    set SelfTestDataFileRootName ${directory}/SelfTestData-$year-$month-${day}
    set fileIndex [DetermineMostRecentFileIndex $SelfTestDataFileRootName]
    set SelfTestDataFileName ${SelfTestDataFileRootName}.${fileIndex}.sdds

    APSFrame $widget -parent $parent \
      -height 30 \
      -packOption {-side top -expand 1}

    set w $parent$widget.frame

    if {$system=="SR" || $system=="Booster"} {
	set text1 "Self Test Number (= M x 2^N):  "
	set text2 "Self Test Multiplier (M):  "
    } else {
	set text1 "Test Number (= M x 2^N):  "
	set text2 "Test Multiplier (M):  "
    }
    APSLabeledOutput .testNumberFrame -parent $w -width 3 \
      -textVariable number \
      -packOption {-side top -fill x} \
		-label $text1 \
      -contextHelp "Number of Self Tests to perform.  This number is equal to the multiplier number times \
                              2 raised to the power of memory scanner boxcar average number."
    
    APSLabeledEntry .multAvgFrame -parent $w -width 3 \
      -textVariable multiplier \
      -packOption {-side top -fill x} \
		-label $text2 \
      -contextHelp "Memory scanner average for all bpms.  This number is a power of 2 so for example, if one \
                              wants 32 averages this factor would be 5."  
    bind $w.multAvgFrame.entry <Return> "UpdateSelfTestNumber"
    APSLabeledEntry .msAvgFrame -parent $w -width 3 \
      -textVariable MSavg \
      -packOption {-side top -fill x} \
		-label "Memory Scanner Average (N):  " \
      -contextHelp "Memory scanner average for all bpms.  This number is a power of 2 so for example, if one wants 32 averages this factor would be 5."  
    bind $w.msAvgFrame.entry <Return> "UpdateSelfTestNumber"
    APSRadioButtonFrame .gainFrame -parent $w -label "BPM Gain Mode" \
      -packOption {-side top} \
      -variable gain \
      -buttonList {Low High} \
      -valueList {0 1} \
      -orientation horizontal \
      -contextHelp "This button selects the BPM gain mode.  In High gain mode smaller position changes  can be detected than in Low gain mode."
    
    APSLabeledOutput .data1 -parent $parent -width $fileWidth \
        -textVariable CompareDataFileName \
        -packOption {-side top -fill x} \
		-label "Comparing Data Filename:" \
        -contextHelp "Filename for comparing with following file in the plot."
    APSButton .sel -parent $parent.data1 -text "Select" -size small \
        -packOption "-side right" \
        -command "GetSelectedDataFile -compareData 1"
    APSButton .proc -parent $parent.data1 -text "Process" -size small \
        -packOption "-side right" \
        -command "RegroupAndHistogramSelfTestData -compareData 1"
    APSLabeledEntry .dataContentsFrame1 -parent $parent -width $fileWidth \
      -textVariable contentsString1 \
      -packOption {-side top -fill x} \
		-label "Compare Data Description:  " \
      -contextHelp "Filename contents containing a description of the self-test data present in the compare data file."


    APSLabeledOutput .dataFileFrame -parent $parent -width $fileWidth \
        -textVariable SelfTestDataFileName \
        -packOption {-side top -fill x} \
		-label "Self Test Data Filename:  " \
        -contextHelp "Filename containing the SCDU, MS, and BPLD self test data."

    APSButton .sel1 -parent $parent.dataFileFrame -text "Select" -size small \
        -packOption "-side right" \
        -command "GetSelectedDataFile -compareData 0"
    APSButton .proc1 -parent $parent.dataFileFrame -text "Process" -size small \
        -packOption "-side right" \
        -command "RegroupAndHistogramSelfTestData -compareData 0"

    APSLabeledEntry .dataContentsFrame -parent $parent -width $fileWidth \
      -textVariable contentsString \
      -packOption {-side top -fill x} \
		-label "Self Test Data Description:  " \
      -contextHelp "Filename contents containing a description of the self-test data present in the file."

   
    APSFrame .plotFrame -parent $parent \
        -height 30 \
        -packOption {-side top -expand 1}
    APSButton .compare -parent $parent.plotFrame.frame \
        -text "Compare" \
        -command {CompareBPMSelfTestData -statusCallback SetStatus} \
        -packOption {-side left -fill x} \
        -contextHelp "This button compares the self test data file with compare data file with plots."
    APSButton .plotSelectionFrame -parent $parent.plotFrame.frame \
      -text "Plot" \
      -command {PlotBPMSelfTestData -statusCallback SetStatus} \
      -packOption {-side left -fill x} \
      -contextHelp "This button plots the data acquired for the selected data file."

    APSCheckButtonFrame .srBPMPlottingCheckButtonFrame -parent $parent.plotFrame.frame -label "" \
      -packOption {-side right} \
      -buttonList {Data Histogram Average} -variableList "dataFlag histFlag averageFlag" \
      -allNone 1 \
      -orientation horizontal \
      -contextHelp "These checkbuttons are used to select self-test data plotting options."
}
