#--- Default values of variables used in GUI ----------------------------------------------------------------------------

proc GetUsualDirName {dirname} {
    #--- This procedure removes numbers from the server name: for example, makes "helios" from "helios06"
    foreach word [split $dirname /] {
	if {[string range $word 0 5] == "helios"} {
	    lappend wordList helios
	} elseif {[string range $word 0 5] == "oxygen"} {
	    lappend wordList oxygen
	} elseif {[string range $word 0 6] == "phoebus"} {
	    lappend wordList phoebus
	} else {
	    lappend wordList $word
	}
    }
    set dirname [join $wordList /]
    return $dirname
}
proc AddEndSymbol {args} {
    APSParseArguments {elementList symbol}
    set newList ""
    if [llength $elementList] {foreach element $elementList {lappend newList ${element}$symbol}}
    return $newList
}

set showMatchingMachineConfigOnly 1
set recalculateDerivative 0
set filterDerivative 0
set recombineDerivative 0
set recalculateInverse 0
set recalculateSVD 0
set continuePrevious 0
set twoRuns 0
set SVnumber 0
set corWeight 0.5
set iterations 100
set bigIterNumber 3
set splitTasks 20
set useBpmWeight 0
set averLevel 0
set nonlinLevel 0
set badPointsLevel 0
set bpmTiltLevel 0
set useMeasuredDisp 0
set analMode 2
set configComment ""
set beamlineName RING
set lteFile ""
set optimFile ""
set latticeParamFile ""
if ![info exists workDir] {set workDir [GetUsualDirName [exec pwd]]}
set orbitDiffCoeff 1.0
set sortRms 1
set useDoubleOrbits 0
set removeDispersionSepErrors 1
set resultsFile $workDir/results.loco

set seed 123456789
set removeAverageBpmError 1
set correctOrbit 0
set correctTunes 0
set quadError 0
set quadTilt 0
set corCalib 0
set corTilt 0
set bpmGain 0
set bpmTilt 0
set bpmCoef 0
set sextDispX 0
set sextDispY 0
set bpmNoise 0
set bpmAver 0
set knobFile ""
set rfBeamlineName ""
set correctBetas 1
set errorRootname ""
set multipleErrorRootname ""
set nSeeds 0
set elementPattern *
set bins 40
set betaLimits0 "0 1"
set betaLimits1 "0 0.2"
set etaLimits0 "0 0.2"
set etaLimits1 "0 0.1"
set transform2measuredFormat 0
set useAdditionalErrorSources 0
set additionalErrorSourcesSeed 1357
set additionalErrorSources ""
set useAverageOrbits 0
set averageOrbits 0
set treatNonBTSdiff 0
set nonBTSfile ""

#--- Lists describing machine elements ----------------------------------------------------------------------------------

#--- By default sectorList is one for all types of elements. If that is not enough, a new list can be used for 
#--- any type of elements with name sectorList<ElementType>, where <ElementType> is Quad, HCorr, etc.
#--- (see preffixArray array definition below for all element types). Example is in the Recycler section.

array set preffixArray [list Quad "" Sext "" HCorr "" VCorr "" HBPM "" VBPM "" Energy "" Skew "" HCorrTilt "" \
			    VCorrTilt "" BPMTilt "" BPMCoef "" QuadNoise "" SkewNoise ""]

set height3 260
switch -exact -- $machineName {
    APS {
	set sextOffsetAnalysis 0
	set scu0Analysis 0
        #--- GUI geometry ---
	# Index 1 below is for main widget; 2 - for RM or VAR; 3 - for the bottom widget
        set width1  1000
        set width2  1000
        set width3  930

	set acceleratorCode elegant
	set sectorButtonOrientation horizontal
        set sectorList {S1 S2 S3 S4 S5 S6 S7 S8 S9 S10 S11 S12 S13 S14 S15 S16 S17 S18 S19 S20 S21 S22 S23 S24 \
                            S25 S26 S27 S28 S29 S30 S31 S32 S33 S34 S35 S36 S37 S38 S39 S40}
        #--- Element suffixes
        set QuadSufList  {A:Q1 A:Q2 A:Q3 A:Q4 A:Q5 B:Q5 B:Q4 B:Q3 B:Q2 B:Q1 A:QS A:QS4 B:QS}
        set SextSufList  {A:S1 A:S2 A:S3 A:S4 B:S3 B:S2 B:S1}
        set HCorrSufList {A:H1 A:H2 A:H3 A:H4 B:H4 B:H3 B:H2 B:H1}
        set VCorrSufList {A:V1 A:V2 A:V3 A:V4 B:V4 B:V3 B:V2 B:V1}
	set BPMSufList   {A:P0 A:P1 A:P2 A:P3 A:P4 B:P5 B:P4 B:P3 B:P2 B:P1 B:P0 C:P0}
	set HBPMSufList  $BPMSufList
	set VBPMSufList  $BPMSufList
        set EnergySufList $HCorrSufList
	set SkewSufList {A:Q1 A:Q2 A:Q3 A:Q4 A:Q5 B:Q5 B:Q4 B:Q3 B:Q2 B:Q1}
	if $sextOffsetAnalysis {
	    set height1 [expr $height1 + 60]
	    set QuadSufList [concat $QuadSufList A:S2 A:S3 B:S3 B:S2]
	    set SkewSufList [concat $SkewSufList A:S2 A:S3 B:S3 B:S2]
	}
        set HCorrTiltSufList $HCorrSufList
        set VCorrTiltSufList $VCorrSufList
        set BPMTiltSufList  $BPMSufList
        set BPMCoefSufList  $BPMSufList
        #--- Missing element lists
	set QuadsMissing  {S1A:QS S1B:QS S2A:QS S2B:QS S3B:QS S4A:QS S4B:QS S5A:QS \
			       S6A:QS S6B:QS S7B:QS S8A:QS S8B:QS S9A:QS S10A:QS S10B:QS \
			       S11B:QS S12A:QS S12B:QS S13A:QS S14A:QS S14B:QS S15B:QS \
			       S16A:QS S16B:QS S17A:QS S18A:QS S18B:QS S19B:QS S20A:QS S20B:QS \
			       S21A:QS S22A:QS S22B:QS S23B:QS S24A:QS S24B:QS S25A:QS \
			       S26A:QS S26B:QS S27B:QS S28A:QS S28B:QS S29A:QS S30A:QS S30B:QS \
			       S31B:QS S32A:QS S32B:QS S33A:QS S34A:QS S34B:QS S35B:QS \
			       S36A:QS S36B:QS S37A:QS S38A:QS S38B:QS S39B:QS S40A:QS S40B:QS}
	set SkewsMissing {}
	set SextsMissing {}
        set HCorrsMissing {}
        set VCorrsMissing {}
        set HCorrTiltsMissing $HCorrsMissing
        set VCorrTiltsMissing $VCorrsMissing
	set EnergysMissing {}
	set BPMsMissing {S1A:P0 S36B:P0 S37A:P0 S37B:P0 S38A:P0 S38B:P0 S39A:P0 S40A:P0 S40B:P0 \
			     S2C:P0 S3C:P0 S4C:P0 S5C:P0 S8C:P0 S9C:P0 S10C:P0 \
			     S11C:P0 S14C:P0 S15C:P0 S17C:P0 S18C:P0 S19C:P0 S20C:P0 \
			     S22C:P0 S26C:P0 S27C:P0 S28C:P0 S29C:P0 S30C:P0 \
			     S31C:P0 S32C:P0 S33C:P0 S35C:P0 S36C:P0 S37C:P0 S38C:P0 S39C:P0 S40C:P0}
	set HBPMsMissing $BPMsMissing
	set VBPMsMissing $BPMsMissing
        set BPMTiltsMissing $BPMsMissing
        set BPMCoefsMissing $BPMsMissing
	set QuadNoiseSufList $QuadSufList
	set QuadNoisesMissing $QuadsMissing
	set SkewNoiseSufList $SkewSufList
	set SkewNoisesMissing $SkewsMissing

	if $scu0Analysis {
	    set QuadSufList [concat $quadSufList B:Q0 B:QS0]
	    for {set i 1} {$i < 41} {incr i} {
		if {$i != 6} {lappend QuadsMissing S${i}B:Q0 S${i}B:QS0}
	    }
	}
	#------ +20 is for "Read RM Config" button; +40 is for additional "Read Quads" button; Noise frames do not have these buttons
	set buttonSize 21
        set height2 [expr max([llength $QuadSufList]*$buttonSize+20,[llength $SkewSufList]*$buttonSize+50,[llength $HBPMSufList]*$buttonSize+20,\
				  [llength $VBPMSufList]*$buttonSize+20,[llength $HCorrSufList]*$buttonSize+20,[llength $VCorrSufList]*$buttonSize+20, \
				  [llength $QuadNoiseSufList]*$buttonSize,[llength $SkewNoiseSufList]*$buttonSize)]
        set height1 [expr $height2 + 50]
    }
    APSBooster {
	set acceleratorCode elegant
	set sectorButtonOrientation vertical
        set sectorList {B1 B2 B3 B4}
        #--- Element suffixes
        set QuadSufList  {C0QF C0QD C1QF C1QD C2QF C2QD C3QF C3QD C4QF C4QD C5QF C5QD C6QF C6QD C7QF C7QD C8QF C8QD \
                              C9QF C9QD }
        set SextSufList  {C0SF C0SD C1SF C1SD C2SF C2SD C3SF C3SD C4SF C4SD C5SF C5SD C6SF C6SD C7SF C7SD C8SF C8SD \
                              C9SF C9SD }
        set HCorrSufList {C0H C1H C2H C3H C4H C5H C6H C7H C8H C9H}
        set VCorrSufList {C0V C1V C2V C3V C4V C5V C6V C7V C8V C9V}
	set BPMSufList   {C0P1 C0P2 C1P1 C1P2 C2P1 C2P2 C3P1 C3P2 C4P1 C4P2 C5P1 C5P2 C6P1 C6P2 \
			      C7P1 C7P2 C8P1 C8P2 C9P1 C9P2}
	set HBPMSufList $BPMSufList
	set VBPMSufList $BPMSufList

        set EnergySufList $HCorrSufList
        set SkewSufList $QuadSufList
        set HCorrTiltSufList $HCorrSufList
        set VCorrTiltSufList $VCorrSufList
        set BPMTiltSufList  $BPMSufList
        set BPMCoefSufList  $BPMSufList
        #--- Missing element lists
        set QuadsMissing {}
        set SextsMissing {}
        set HCorrsMissing {}
        set VCorrsMissing {}
	set BPMsMissing {}
	set HBPMsMissing $BPMsMissing
	set VBPMsMissing $BPMsMissing
        set BPMTiltsMissing $BPMsMissing
        set BPMCoefsMissing $BPMsMissing
        set EnergysMissing $HCorrsMissing
        set SkewsMissing $QuadsMissing
        set HCorrTiltsMissing $HCorrsMissing
        set VCorrTiltsMissing $VCorrsMissing
	set QuadNoiseSufList $QuadSufList
	set QuadNoisesMissing $QuadsMissing
	set SkewNoiseSufList $SkewSufList
	set SkewNoisesMissing $SkewsMissing
        #--- GUI geometry ---
	# Index 1 below is for main widget; 2 - for RM or VAR; 3 - for the bottom widget
        set width1  1000
        set height1 270
        set width2  1000
        set height2 160
        set width3  930
    }
    PAR {
	set acceleratorCode elegant
	set sectorButtonOrientation horizontal
        set sectorList {P1 P2 P3 P4}
        #--- Element suffixes
        set QuadSufList  {Q1 Q2 Q3 Q4}
        set SextSufList  {S1 SD SF}
        set HCorrSufList {H1 H2 H3}
        set VCorrSufList {V1 V2 V3}
	set BPMSufList  {P1 P2 P3 P4}
	set HBPMSufList $BPMSufList
	set VBPMSufList $BPMSufList
        set EnergySufList $HCorrSufList
        set SkewSufList $QuadSufList
        set HCorrTiltSufList $HCorrSufList
        set VCorrTiltSufList $VCorrSufList
        set BPMTiltSufList  $BPMSufList
        set BPMCoefSufList  $BPMSufList
        #--- Missing element lists
        set QuadsMissing {}
        set SextsMissing {P3SF P4SF}
        set HCorrsMissing {P3H3 P4H3}
        set VCorrsMissing {P3V3 P4V3}
	set BPMsMissing {}
	set HBPMsMissing $BPMsMissing
	set VBPMsMissing $BPMsMissing
        set EnergysMissing $HCorrsMissing
        set SkewsMissing $QuadsMissing
        set HCorrTiltsMissing $HCorrsMissing
        set VCorrTiltsMissing $VCorrsMissing
        set BPMTiltsMissing $BPMsMissing
        set BPMCoefsMissing $BPMsMissing
	set QuadNoiseSufList $QuadSufList
	set QuadNoisesMissing $QuadsMissing
	set SkewNoiseSufList $SkewSufList
	set SkewNoisesMissing $SkewsMissing
        #--- GUI geometry ---
	# Index 1 below is for main widget; 2 - for RM or VAR; 3 - for the bottom widget
        set width1  1070
        set height1 250
        set width2  1000
        set height2 160
        set width3  930
    }
    NSLS2 {
	set sextOffsetAnalysis 0
	set scu0Analysis 0
        #--- GUI geometry ---
	# Index 1 below is for main widget; 2 - for RM or VAR; 3 - for the bottom widget
        set width1  1120
        set height1 430
        set width2  1000
        set height2 360
        set width3  930

	set acceleratorCode elegant
	set sectorButtonOrientation horizontal
        set sectorList {C01 C02 C03 C04 C05 C06 C07 C08 C09 C10 C11 C12 C13 C14 C15 C16 C17 C18 C19 C20 C21 C22 C23 C24 \
                            C25 C26 C27 C28 C29 C30}
        #--- Element suffixes
        set QuadSufList  {A:G2:QH1 A:G2:QH2 A:G2:QH3 A:G4:QM1 A:G4:QM2 B:G4:QM2 B:G4:QM1 B:G6:QL3 B:G6:QL2 B:G6:QL1 A:G2:SQ1H B:G3:SQ2H}
        set SextSufList  {A:G2:SH1 A:G2:SH2 A:G2:SH3 A:G2:SH4 A:G4:SM1 B:G4:SM2 B:G4:SM1 B:G6:SL3 B:G6:SL2 B:G6:SL1}
        set HCorrSufList {A:G2:CH1X A:G2:CH2X A:G3:CM1X B:G4:CM1X B:G6:CL2X B:G6:CL1X}
        set VCorrSufList {A:G2:CH1Y A:G2:CH2Y A:G3:CM1Y B:G4:CM1Y B:G6:CL2Y B:G6:CL1Y}
	set HBPMSufList  {A:G2:PH1 A:G2:PH2 A:G4:PM1 B:G4:PM1 B:G6:PL2 B:G6:PL1}
	set VBPMSufList  {A:G2:PH1 A:G2:PH2 A:G4:PM1 B:G4:PM1 B:G6:PL2 B:G6:PL1}
        set EnergySufList $HCorrSufList
	set SkewSufList {A:G2:QH1 A:G2:QH2 A:G2:QH3 A:G4:QM1 A:G4:QM2 B:G4:QM2 B:G4:QM1 B:G6:QL3 B:G6:QL2 B:G6:QL1}
        set HCorrTiltSufList $HCorrSufList
        set VCorrTiltSufList $VCorrSufList
        set HBPMTiltSufList  $HBPMSufList
        set VBPMTiltSufList  $VBPMSufList
        #--- Missing element lists
	set QuadsMissing  {C01A:G2:SQ1H C02B:G3:SQ2H C03A:G2:SQ1H C04B:G3:SQ2H C05A:G2:SQ1H \
			       C06B:G3:SQ2H C07A:G2:SQ1H C08B:G3:SQ2H C09A:G2:SQ1H C10B:G3:SQ2H \
			       C11A:G2:SQ1H C12B:G3:SQ2H C13A:G2:SQ1H C14B:G3:SQ2H C15A:G2:SQ1H \
			       C16B:G3:SQ2H C17A:G2:SQ1H C18B:G3:SQ2H C19A:G2:SQ1H C20B:G3:SQ2H \
			       C21A:G2:SQ1H C22B:G3:SQ2H C23A:G2:SQ1H C24B:G3:SQ2H C25A:G2:SQ1H \
			       C26B:G3:SQ2H C27A:G2:SQ1H C28B:G3:SQ2H C29A:G2:SQ1H C30B:G3:SQ2H}
	set SkewsMissing {}
	set SextsMissing {}
        set HCorrsMissing {}
        set VCorrsMissing {}
        set HCorrTiltsMissing $HCorrsMissing
        set VCorrTiltsMissing $VCorrsMissing
	set EnergysMissing {}
	set HBPMsMissing {}
	set VBPMsMissing $HBPMsMissing
        set HBPMTiltsMissing $HBPMsMissing
        set VBPMTiltsMissing $VBPMsMissing
	if $makeBpmNamesUnique {
	    set HBPMSufList [AddEndSymbol -elementList $HBPMSufList -symbol X]
	    set VBPMSufList [AddEndSymbol -elementList $VBPMSufList -symbol Y]
	    set HBPMTiltSufList [AddEndSymbol -elementList $HBPMTiltSufList -symbol X]
	    set VBPMTiltSufList [AddEndSymbol -elementList $VBPMTiltSufList -symbol Y]
	    set HBPMsMissing [AddEndSymbol -elementList $HBPMsMissing -symbol X]
	    set VBPMsMissing [AddEndSymbol -elementList $VBPMsMissing -symbol Y]
	    set HBPMTiltsMissing [AddEndSymbol -elementList $HBPMTiltsMissing -symbol X]
	    set VBPMTiltsMissing [AddEndSymbol -elementList $VBPMTiltsMissing -symbol Y]
	}
	if $scu0Analysis {
	    set QuadSufList [concat $quadSufList B:Q0 B:QS0]
	    for {set i 1} {$i < 41} {incr i} {
		if {$i != 6} {lappend QuadsMissing S${i}B:Q0 S${i}B:QS0}
	    }
	}
    }
    APSU {
        #--- GUI geometry ---
	# Index 1 below is for top widget; 2 - for RM or VAR; 3 - for the bottom widget
        set width1  1000
        set width2  1000
        set width3  930

	set calculateEmittance 1
	set beamlineEmit RINGORBRF
	set rfVoltageMV 5.0
	set rfH 1296
	set beamEnergyEmitGeV 6.0
	
	set acceleratorCode elegant
	set sectorButtonOrientation horizontal
        set sectorList {S01 S02 S03 S04 S05 S06 S07 S08 S09 S10 S11 S12 S13 S14 S15 S16 S17 S18 S19 S20 S21 S22 S23 S24 \
                            S25 S26 S27 S28 S29 S30 S31 S32 S33 S34 S35 S36 S37 S38 S39 S40}
        #--- Element suffixes
        set QuadSufList  {A:Q1 A:Q2 A:Q3 A:Q4 A:Q5 A:Q6 A:Q7 A:Q8 B:Q8 B:Q7 B:Q6 B:Q5 B:Q4 B:Q3 B:Q2 B:Q1 A:M3 A:M4 B:M3 A:Q4S A:Q5S A:Q8S B:Q8S B:Q5S B:Q4S}
        set HCorrSufList {A:FH1 A:H1 A:FH2 A:H7 A:H8 B:H8 B:H7 B:FH2 B:H1 B:FH1}
        set VCorrSufList {A:FV1 A:V1 A:FV2 A:V7 A:V8 B:V8 B:V7 B:FV2 B:V1 B:FV1}
	set BPMSufList   {A:P0 A:P1 A:P2 A:P3 A:P4 A:P5 A:P6 B:P6 B:P5 B:P4 B:P3 B:P2 B:P1 B:P0}
	set HBPMSufList $BPMSufList
	set VBPMSufList $BPMSufList
        set EnergySufList $HCorrSufList
	set SkewSufList  {A:Q1 A:Q2 A:Q3 A:Q6 A:Q7 B:Q7 B:Q6 B:Q3 B:Q2 B:Q1}
        set HCorrTiltSufList $HCorrSufList
        set VCorrTiltSufList $VCorrSufList
        set BPMTiltSufList   $BPMSufList
	set BPMCoefSufList   $BPMSufList
	#--- Noise suffixes:
        set QuadNoiseSufList {A:Q1 A:Q2 A:Q3 A:Q4 A:Q5 A:Q6 A:Q7 A:Q8 B:Q8 B:Q7 B:Q6 B:Q5 B:Q4 B:Q3 B:Q2 B:Q1 A:M3 A:M4 B:M3 A:Q4S A:Q5S A:Q8S B:Q8S B:Q5S B:Q4S}
	set SkewNoiseSufList {A:Q1 A:Q2 A:Q3 A:Q6 A:Q7 B:Q7 B:Q6 B:Q3 B:Q2 B:Q1}
        set SextNoiseSufList {A:S1 A:S2 A:S3 B:S3 B:S2 B:S1}
	set HBPMNoiseSufList $BPMSufList
	set VBPMNoiseSufList $BPMSufList
	set HCorrNoiseSufList $HCorrSufList
	set VCorrNoiseSufList $VCorrSufList
        #--- Missing element lists
	set QuadsMissing  {}
	set SkewsMissing {}
        set HCorrsMissing {}
        set VCorrsMissing {}
        set HCorrTiltsMissing $HCorrsMissing
        set VCorrTiltsMissing $VCorrsMissing
	set EnergysMissing {}
	set BPMsMissing {S01A:P0 S36B:P0 S37B:P0 S39B:P1}
	set HBPMsMissing $BPMsMissing
	set VBPMsMissing $BPMsMissing
        set BPMTiltsMissing $BPMsMissing
        set BPMCoefsMissing $BPMsMissing
	#--- Missing noise lists:
	set QuadsNoiseMissing  {}
	set SkewsNoiseMissing {}
	set SextsNoiseMissing {}
	set HBPMsNoiseMissing $BPMsMissing
	set VBPMsNoiseMissing $BPMsMissing
        set HCorrsNoiseMissing $HCorrsMissing
        set VCorrsNoiseMissing $VCorrsMissing
	

	#------ +20 is for "Read RM Config" button; +40 is for additional "Read Quads" button; Noise frames do not have these buttons
	set buttonSize 19
        set height2 [expr max([llength $QuadSufList]*$buttonSize+20,[llength $SkewSufList]*$buttonSize+50,[llength $HBPMSufList]*$buttonSize+20,[llength $VBPMSufList]*$buttonSize+20, \
				  [llength $HCorrSufList]*$buttonSize+20,[llength $VCorrSufList]*$buttonSize+20, \
				  [llength $QuadNoiseSufList]*$buttonSize,[llength $SkewNoiseSufList]*$buttonSize)]
        set height1 [expr $height2 + 50]
    }
    generic {
	if ![info exists guiListFile] {
	    puts stderr "Error: If you use \"generic\" machine name, you must supply guiListFile with definition of GUI lists."
	    exit
	}
        #--- GUI geometry ---
	# Index 1 below is for main widget; 2 - for RM or VAR; 3 - for the bottom widget
        set width1  1120
        set height1 490
        set width2  1000
        set height2 360
        set width3  930

	set acceleratorCode elegant
	set sectorButtonOrientation horizontal
	source $guiListFile
	set height1 [expr 420 + ([llength $QuadSufList] - 10) * 11]
        #--- Element suffixes
        set EnergySufList $HCorrSufList
        set HCorrTiltSufList $HCorrSufList
        set VCorrTiltSufList $VCorrSufList
        set BPMTiltSufList  $BPMSufList
        set BPMCoefSufList  $BPMSufList
        #--- Missing element lists
        set HCorrTiltsMissing $HCorrsMissing
        set VCorrTiltsMissing $VCorrsMissing
	set EnergysMissing $HCorrsMissing
        set BPMTiltsMissing $BPMsMissing
        set BPMCoefsMissing $BPMsMissing
    }
    APSBTS {
        #--- GUI geometry ---
	# Index 1 below is for main widget; 2 - for RM or VAR; 3 - for the bottom widget
        set width1  1000
        set width2  1000
        set width3  930
	set height3 300

	set acceleratorCode elegant
	set sectorButtonOrientation vertical
        set sectorList {BOO: BTS: S40: S1: S2: S3: S4: S5:}
        #--- Element suffixes
        set QuadSufList  {Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13}
        set SextSufList  {A:S1 A:S2 A:S3 A:S4 B:S3 B:S2 B:S1}
        set HCorrSufList {H1 H2 H3 H4 H5 H6 H7 H8}
        set VCorrSufList {V1 V2 V3 V4 V5 V6 V7}
	set BPMSufList   {P0 P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12}
	set HBPMSufList  $BPMSufList
	set VBPMSufList  $BPMSufList
        set EnergySufList $HCorrSufList
	set SkewSufList $QuadSufList
        set HCorrTiltSufList $HCorrSufList
        set VCorrTiltSufList $VCorrSufList
        set BPMTiltSufList  $BPMSufList
        set BPMCoefSufList  $BPMSufList
        #--- Missing element lists
	set QuadsMissing  {BOO:Q3 BOO:Q4 BOO:Q5 BOO:Q6 BOO:Q7 BOO:Q8 BOO:Q9 BOO:Q10 BOO:Q11 BOO:Q12 BOO:Q13 \
			       BTS:Q13 S40:Q11 S40:Q13 S1:Q11 S1:Q13 S2:Q11 S2:Q13 S3:Q13 S4:Q11 S4:Q13 S5:Q11}
	set SkewsMissing $QuadsMissing
	set SextsMissing {}
        set HCorrsMissing {BOO:H2 BOO:H3 BOO:H4 BOO:H5 BOO:H6 BOO:H7 BOO:H8}
        set VCorrsMissing {BOO:V2 BOO:V3 BOO:V4 BOO:V5 BOO:V6 BOO:V7 BTS:V6 BTS:V7}
        set HCorrTiltsMissing $HCorrsMissing
        set VCorrTiltsMissing $VCorrsMissing
	set EnergysMissing {}
	set BPMsMissing {BOO:P2 BOO:P3 BOO:P4 BOO:P5 BOO:P6 BOO:P7 BOO:P8 BOO:P9 BOO:P10 BOO:P11 BOO:P12 \
			     S40:P0 S40:P1 S40:P9 S40:P10 S40:P11 S40:P12 \
			     S1:P0 S1:P1 S1:P9 S1:P10 S1:P11 S1:P12 \
			     S2:P0 S2:P1 S2:P9 S2:P10 S2:P11 S2:P12 \
			     S3:P0 S3:P1 S3:P9 S3:P10 S3:P11 S3:P12 \
			     S4:P0 S4:P1 S4:P9 S4:P10 S4:P11 S4:P12 \
			     S5:P0 S5:P1 S5:P9 S5:P10 S5:P11 S5:P12}
	set HBPMsMissing $BPMsMissing
	set VBPMsMissing $BPMsMissing
        set BPMTiltsMissing $BPMsMissing
        set BPMCoefsMissing $BPMsMissing
	set QuadNoiseSufList $QuadSufList
	set QuadNoisesMissing $QuadsMissing
	set SkewNoiseSufList $SkewSufList
	set SkewNoisesMissing $SkewsMissing
	
	#------ +20 is for "Read RM Config" button; +40 is for additional "Read Quads" button; Noise frames do not have these buttons
	set buttonSize 21
        set height2 [expr max([llength $QuadSufList]*$buttonSize+20,[llength $SkewSufList]*$buttonSize+50,[llength $HBPMSufList]*$buttonSize+20,\
				  [llength $VBPMSufList]*$buttonSize+20,[llength $HCorrSufList]*$buttonSize+20,[llength $VCorrSufList]*$buttonSize+20, \
				  [llength $QuadNoiseSufList]*$buttonSize,[llength $SkewNoiseSufList]*$buttonSize)]
        set height1 [expr $height2 + 50]

	#------ Correspondence lists between widget names ($sector:Q1,$sector:Q2, etc) and real names (BTS:AQ1, BTS:BQ1, etc):
	set quadCorrespondList [list BOO:Q1 B2C8QD BOO:Q2 B2C9QF \
				    BTS:Q1 BTS:AQ1 BTS:Q2 BTS:AQ2 BTS:Q3 BTS:AQ3 BTS:Q4 BTS:AQ4 BTS:Q5 BTS:AQ5 BTS:Q6 BTS:BQ1 BTS:Q7 BTS:BQ2 BTS:Q8 BTS:BQ3 \
				    BTS:Q9 BTS:BQ4  BTS:Q10 BTS:CQ1 BTS:Q11 BTS:CQ2 BTS:Q12 BTS:CQ3]
	set hcorCorrespondList [list BOO:H1 B2C9H BTS:H1 B2C9ES1C BTS:H2 B2C9ES2C BTS:H3 BTS:AH1 BTS:H4 BTS:AH2 BTS:H5 BTS:AH3 BTS:H6 BTS:BH1 \
				    BTS:H7 BTS:BH2 BTS:H8 BTS:CH1]
	set vcorCorrespondList [list BOO:V1 B2C8V BTS:V1 BTS:AV1 BTS:V2 BTS:AV2 BTS:V3 BTS:AV3 BTS:V4 BTS:BV1 BTS:V5 BTS:CV1]
	set bpmCorrespondList [list BOO:P1 B2C8P2 BOO:P2 B2C9P1 \
				   BTS:P0 BTS:APH1 BTS:P1 BTS:APV1 BTS:P2 BTS:APH2 BTS:P3 BTS:APV2 BTS:P4 BTS:APH3 BTS:P5 BTS:APV3 BTS:P6 BTS:BPH1 BTS:P7 BTS:BPV1 \
				   BTS:P8 BTS:BPH2 BTS:P9 BTS:CPV1 BTS:P10 BTS:CPH1 BTS:P11 BTS:CPH2 BTS:P12 BTS:CPV2]
	foreach sector [list S40 S1 S2 S3 S4 S5] {
	    for {set i 1} {$i < 6} {incr i} {lappend quadCorrespondList $sector:Q$i ${sector}A:Q$i $sector:Q[expr $i+5] ${sector}B:Q[expr 6-$i]}
	    lappend quadCorrespondList $sector:Q11 ${sector}A:QS $sector:Q12 ${sector}A:QS4 $sector:Q13 ${sector}B:QS
	    for {set i 1} {$i < 5} {incr i} {lappend hcorCorrespondList $sector:H$i ${sector}A:H$i $sector:H[expr $i+4] ${sector}B:H[expr 5-$i]}
	    for {set i 1} {$i < 4} {incr i} {lappend vcorCorrespondList $sector:V$i ${sector}A:V$i $sector:V[expr $i+4] ${sector}B:V[expr 4-$i]}
	    lappend vCorrespondcorList $sector:V4 ${sector}B:V4
	    for {set i 0} {$i < 5} {incr i} {lappend bpmCorrespondList $sector:P$i ${sector}A:P$i $sector:P[expr $i+6] ${sector}B:P[expr 4-$i]}
	    lappend bpmCorrespondList $sector:P5 ${sector}B:P5 $sector:P11 ${sector}C:P0
	}
	#-------------------------------------------------
	#--- Elements in widgets are named as Q1, Q2, Q3, etc. Procedure below converts them into real names like BTS:AQ1. BTS:AQ2, etc.
	proc ConvertWidgetNames2ElementNames_APSBTS {args} {
	    global quadCorrespondList hcorCorrespondList vcorCorrespondList bpmCorrespondList
	    APSParseArguments {elementList varRoot}
	    set returnList ""
	    array set quadArr $quadCorrespondList
	    array set hcorArr $hcorCorrespondList
	    array set vcorArr $vcorCorrespondList
	    array set xbpmArr $bpmCorrespondList
	    array set ybpmArr $bpmCorrespondList
	    foreach element $elementList {
		switch -exact $varRoot {
		    Quad {lappend returnList $quadArr($element)#1}
		    Skew {lappend returnList $quadArr($element)#1}
		    HCorr {lappend returnList $hcorArr($element)#1}
		    VCorr {lappend returnList $vcorArr($element)#1}
		    HBPM {lappend returnList $xbpmArr($element)#X#1}
		    VBPM {lappend returnList $ybpmArr($element)#Y#1}
		    HCorrTilt {lappend returnList $hcorArr($element)#1}
		    VCorrTilt {lappend returnList $vcorArr($element)#1}
		    BPMTilt {lappend returnList $xbpmArr($element)#1}
		    BPMCoef {lappend returnList $ybpmArr($element)#1}
		}
	    }
	    return $returnList
	}
	#-------------------------------------------------
	proc ConvertElementNames2WidgetNames_APSBTS {args} {
	    global quadCorrespondList hcorCorrespondList vcorCorrespondList bpmCorrespondList
	    APSParseArguments {elementList varRoot}
	    set returnList ""
	    array set quadArr [ReverseArrayList -inputList $quadCorrespondList]
	    array set hcorArr [ReverseArrayList -inputList $hcorCorrespondList]
	    array set vcorArr [ReverseArrayList -inputList $vcorCorrespondList]
	    array set xbpmArr [ReverseArrayList -inputList $bpmCorrespondList]
	    array set ybpmArr [ReverseArrayList -inputList $bpmCorrespondList]
	    foreach element $elementList {
		set elemName [string range $element 0 [expr [string first "#" $element] - 1]]
		switch -exact $varRoot {
		    Quad {lappend returnList $quadArr($elemName)}
		    Skew {lappend returnList $quadArr($elemName)}
		    HCorr {lappend returnList $hcorArr($elemName)}
		    VCorr {lappend returnList $vcorArr($elemName)}
		    HBPM {lappend returnList $xbpmArr($elemName)}
		    VBPM {lappend returnList $ybpmArr($elemName)}
		    HCorrTilt {lappend returnList $hcorArr($elemName)}
		    VCorrTilt {lappend returnList $vcorArr($elemName)}
		    BPMTilt {lappend returnList $xbpmArr($elemName)}
		    BPMCoef {lappend returnList $ybpmArr($elemName)}
		}
	    }
	    return $returnList
	}
    }
    default {
        return -code error "Machine name $machineName is not known."
    }
}

proc ReverseArrayList {args} {
    APSParseArguments {inputList}
    set outputList ""
    set N [expr [llength $inputList] / 2]
    for {set i 0} {$i < $N} {incr i} {lappend outputList [lindex $inputList [expr $i*2+1]] [lindex $inputList [expr $i*2]]}
    return $outputList
}
