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

#$Log: not supported by cvs2svn $
#Revision 1.4  2011/02/16 08:26:30  sajaev
#Changed directory where to look for LOCO configurations from my local to SR.
#
#Revision 1.3  2006/02/08 15:28:31  shang
#fixed a procedure call and added "&" to sddsplot command.
#
#Revision 1.2  2006/02/02 22:07:06  shang
#wrote by Vadim Sajaev.
#

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.5 $ \$Author: sajaev $"

APSApplication . -name "SRAdjustBPMGains" -version $CVSRevisionAuthor \
  -overview "Adjusts bpm gains and changes offsets and setpoints accordingly."

set status "Ready."
APSScrolledStatus .status -parent .userFrame -width 100 \
  -textVariable status 

if [info exists env(LOCO_CONFDIR)] {
    set configDirectory $env(LOCO_CONFDIR)
} else {
    APSSetVarAndUpdate status "Warning: No environment variable LOCO_CONFDIR. Will use /home/oxygen/SR/LOCO/CONFIG/..."
    set configDirectory /home/oxygen/SR/LOCO/CONFIG/
}

#-----------------------------------------------------------------------------------------------------------------------

proc findFiles {args} {
    set fileVar ""
    APSParseArguments {fileVar}
    global $fileVar
    set file [set $fileVar] 
    if ![string length $file] {
        set dir /home/helios/oagData/SCR/snapshots/
    } else {
        set dir [file dir $file]
    }
    set choosedfile [APSFileSelectDialog .chooseInputFile -listDir $dir -pattern *]
    if [string length $choosedfile] {
        set $fileVar $choosedfile
    }
} 

#-----------------------------------------------------------------------------------------------------------------------
#--- This procedure is to pick LOCO configurations

proc ChooseLOCOConfiguration { args } {

    set configDirectory ""
    APSParseArguments {configDirectory}

    set dirList [glob -nocomplain -- $configDirectory/??? ]

    set commentList ""
    set datasetList ""
    foreach directory $dirList {
	if [file exists $directory/comment] {
	    set comment "[file tail $directory]: "
	    if [catch {open $directory/comment r} fid] {
		return -code error "$fid"
	    } else {
		lappend commentList [append comment [gets $fid]]
		close $fid
		lappend datasetList $directory
	    }
	}
    }
    set sortedCommentList [lsort -command CommentCompare $commentList]
    set sortedDatasetList ""
    foreach comment $sortedCommentList {
	set i [lsearch -exact $commentList $comment]
	if {$i >= 0} {
	    lappend sortedDatasetList [lindex $datasetList $i]
	} else {
	    return -code error "Error in the comment lists!"
	}
    }
    
    set chosenList [APSChooseItemFromList \
			-name "Data Set Selection" \
			-itemList $sortedCommentList \
			-returnList $sortedDatasetList \
			-returnIndices 0 \
			-multiItem 1 \
			-contextHelp "Select saveset for data reprocessing."]

    return $chosenList
}

#-----------------------------------------------------------------------------------------------------------------------

proc CommentCompare { a b } {
    set adate [string range $a 5 17]
    set bdate [string range $b 5 17]
    set res [string compare $adate $bdate]
    if {$res != 0} {
	return $res
    } else {
	return [string compare $a $b]
    }
}

#-----------------------------------------------------------------------------------------------------------------------
#--- Creates temp files with new PVs ready for altering
proc MakeNewVariables { args } {

    global tmpDir
    APSParseArguments { gainAdjustmentFile referenceOffsetFile newOffsetFile referenceSetpointFile \
			    newSetpointFile referenceGainFile newGainFile }

    set tmpRoot $tmpDir/[APSTmpString]-makeNewVar
    if [catch {exec sddsprocess $referenceGainFile -pipe=out -match=col,ControlName=*GainAO \
		   -scan=col,Gain,ValueString,%lf -edit=col,BPMName,ControlName,%/:GainAO// "-filter=col,Gain,0,0,!" \
		   | sddsxref -pipe $gainAdjustmentFile -match=BPMName -take=GainAdjustment -noWarnings \
		   | sddsprocess -pipe "-redefine=col,Value,Gain GainAdjustment *" -reprint=col,ValueString,%le,Value \
		   | sddsconvert -pipe=in $tmpRoot.gain -del=col,BPMName,Gain,Value,GainAdjustment } result] {
        return -code error "MakeNewVariables (gain): $result"
    }
    file copy -force $tmpRoot.gain $newGainFile
    
    if [catch {exec sddsprocess $referenceOffsetFile -pipe=out -match=col,ControlName=*OffsetAO \
		   -scan=col,Offset,ValueString,%lf -edit=col,BPMName,ControlName,%/:OffsetAO// \
		   | sddsxref -pipe $gainAdjustmentFile -match=BPMName -take=GainAdjustment -noWarning \
		   | sddsprocess -pipe "-define=col,Value,Offset GainAdjustment *" -reprint=col,ValueString,%le,Value \
		   | sddsconvert -pipe=in $tmpRoot.offset -del=col,BPMName,Offset,Value,GainAdjustment} result] {
	return -code error "MakeNewVariables (offset): $result"
    }
    file copy -force $tmpRoot.offset $newOffsetFile
    
    if [catch {exec sddsprocess $referenceSetpointFile -pipe=out \
		   -match=col,ControlName=*SetpointAO,ControlName=*FTSetpointAO,!,& \
		   -scan=col,Setpoint,ValueString,%lf -edit=col,BPMName,ControlName,%/:SetpointAO// \
		   | sddsxref -pipe $gainAdjustmentFile -match=BPMName -take=GainAdjustment -noWarning \
		   | sddsprocess -pipe "-define=col,Value,Setpoint GainAdjustment *" -reprint=col,ValueString,%le,Value \
		   | sddsconvert -pipe=in $tmpRoot.setpoint -del=col,BPMName,Setpoint,Value,GainAdjustment} result] {
	return -code error "MakeNewVariables (setpoint): $result"
    }
    file copy -force $tmpRoot.setpoint $newSetpointFile
    
}

#-----------------------------------------------------------------------------------------------------------------
#--- Tranform elegant-style gains from LOCO file into real gains, subtracts non-zero average (based on P1s),
#--- and filters out bpms with low weight in loco fitting.

proc MakeNewGains { args } {

    global tmpDir 

    set deleteFiles ""
    set dir ""
    set outputFile ""
    set bpmStatusFile ""
    set plot 0
    set xbpmActionList ""
    set ybpmActionList ""
    APSParseArguments {dir outputFile plot xbpmActionList ybpmActionList}

    if ![string length $dir] { return -code error "Error: LOCO configuration choice is empty." }
    set locoVarFile $dir/variables.sdds
    if ![file exists $locoVarFile] { return -code error "LOCO variables file $locoVarFile does not exist." }
    set locoResultsFile $dir/[exec sddsprocess $locoVarFile -pipe=out -match=col,VariableName=resultsFile \
				  | sdds2stream -pipe=in -col=VariableValue]

    set tmpRoot $tmpDir/[APSTmpString]-makeNewGains
    #--- Transform from elegant-style gains to real gains...
    if [catch {exec sddsprocess $locoResultsFile -pipe=out -match=para,PageID=xBpm -nowarning \
		   | sddsprocess -pipe "-def=col,GainAdjustment,1 1 ParameterValue + /" \
		   "-reedit=col,ElementName,%/X//" \
		   | tee $tmpRoot.x \
		   | sddsprocess -pipe -match=col,ElementName=*P1* \
		   | sddsprocess -pipe -process=GainAdjustment,average,Aver \
		   | sdds2stream -pipe=in -para=Aver} averageGainX] {
	return -code error "Error calculating gains X: $averageGainX"
    }
    exec sddsprocess $tmpRoot.x -nowarning "-redef=col,GainAdjustment,GainAdjustment $averageGainX 1 - -"
    if [catch {exec sddsprocess $locoResultsFile -pipe=out -match=para,PageID=yBpm -nowarning \
		   | sddsprocess -pipe "-def=col,GainAdjustment,1 1 ParameterValue + /" \
		   "-reedit=col,ElementName,%/Y//" \
		   | tee $tmpRoot.y \
		   | sddsprocess -pipe -match=col,ElementName=*P1* \
		   | sddsprocess -pipe -process=GainAdjustment,average,Aver \
		   | sdds2stream -pipe=in -para=Aver} averageGainY] {
	return -code error "Error calculating gains Y: $averageGainY"
    }
    exec sddsprocess $tmpRoot.y -nowarning "-redef=col,GainAdjustment,GainAdjustment $averageGainY 1 - -"
    lappend deleteFiles $tmpRoot.x $tmpRoot.y $tmpRoot.x~ $tmpRoot.y~
    APSSetVarAndUpdate status "The following average gains will be subtracted from LOCO solution:"
    APSSetVarAndUpdate status "   X: [format %8.3f $averageGainX];   Y: [format %8.3f $averageGainY]."

    #--- Leave only bpms in action lists:
    if [llength $xbpmActionList] {
	exec sddsmakedataset $tmpRoot.xactionList \
	    -col=ElementName,type=string -data=[join $xbpmActionList ,]
	exec sddsselect $tmpRoot.x $tmpRoot.xactionList -pipe=out -match=ElementName \
	    | sddsconvert -pipe -retain=col,ElementName,GainAdjustment -rename=col,ElementName=BPMName \
	    | sddsprocess -pipe=in $tmpRoot.gainx "-reedit=col,BPMName,e i%:ms:x%"
    } else {
	exec sddsconvert $tmpRoot.x -pipe=out -retain=col,ElementName,GainAdjustment -rename=col,ElementName=BPMName \
	    | sddsprocess -pipe=in $tmpRoot.gainx "-reedit=col,BPMName,e i%:ms:x%"
    }
    if [llength $ybpmActionList] {
	exec sddsmakedataset $tmpRoot.yactionList \
	    -col=ElementName,type=string -data=[join $ybpmActionList ,]
	exec sddsselect $tmpRoot.y $tmpRoot.yactionList -pipe=out -match=ElementName \
	    | sddsconvert -pipe -retain=col,ElementName,GainAdjustment -rename=col,ElementName=BPMName \
	    | sddsprocess -pipe=in $tmpRoot.gainy "-reedit=col,BPMName,e i%:ms:y%"
    } else {
	exec sddsconvert $tmpRoot.y -pipe=out -retain=col,ElementName,GainAdjustment -rename=col,ElementName=BPMName \
	    | sddsprocess -pipe=in $tmpRoot.gainy "-reedit=col,BPMName,e i%:ms:y%"
    }
    lappend deleteFiles $tmpRoot.xactionList $tmpRoot.gainx $tmpRoot.yactionList $tmpRoot.gainy 

    exec sddscombine $tmpRoot.gainx $tmpRoot.gainy $outputFile -overWrite -merge
    eval file delete $deleteFiles
    if $plot {
	exec sddsplot -graph=symbol,connect,scale=1.5 -sep -layout=1,2 \
	    -col=BPMName,GainAdjustment -match=col,BPMName=*P0:ms:x -topLine=X:P0 $outputFile \
	    -col=BPMName,GainAdjustment -match=col,BPMName=*P0:ms:y -topLine=Y:P0 $outputFile -end \
	    -col=BPMName,GainAdjustment -match=col,BPMName=*P1:ms:x -topLine=X:P1 $outputFile \
	    -col=BPMName,GainAdjustment -match=col,BPMName=*P1:ms:y -topLine=Y:P1 $outputFile -end \
	    -col=BPMName,GainAdjustment -match=col,BPMName=*P2:ms:x -topLine=X:P2 $outputFile \
	    -col=BPMName,GainAdjustment -match=col,BPMName=*P2:ms:y -topLine=Y:P2 $outputFile -end \
	    -col=BPMName,GainAdjustment -match=col,BPMName=*P3:ms:x -topLine=X:P3 $outputFile \
	    -col=BPMName,GainAdjustment -match=col,BPMName=*P3:ms:y -topLine=Y:P3 $outputFile -end \
	    -col=BPMName,GainAdjustment -match=col,BPMName=*P4:ms:x -topLine=X:P4 $outputFile \
	    -col=BPMName,GainAdjustment -match=col,BPMName=*P4:ms:y -topLine=Y:P4 $outputFile -end \
	    -col=BPMName,GainAdjustment -match=col,BPMName=*P5:ms:x -topLine=X:P5 $outputFile \
	    -col=BPMName,GainAdjustment -match=col,BPMName=*P5:ms:y -topLine=Y:P5 $outputFile -end &
    }
}

#-----------------------------------------------------------------------------------------------------------------

proc AlterSnapshotFiles { args } {

    global tmpDir
    APSParseArguments {newGainFile newOffsetFile newSetpointFile srFile srFileDescription sbpmFile sbpmFileDescription}

    set tmpRoot $tmpDir/[APSTmpString]-alter
    set srSnapshotDir /home/helios/oagData/SCR/snapshots/SR

    #--- Altering setpoints and offsets...
    set machineName SR
    set replacementFile $tmpRoot.offsetp
    exec sddscombine $newOffsetFile $newSetpointFile $tmpRoot.comb -merge 
    exec sddsconvert $srFile -pipe=out -del=col,ValueString \
	| sddsxref -pipe=in $tmpRoot.comb $replacementFile -take=ValueString -match=ControlName -nowarning
    if {[string compare [file type $srFile] link] == 0} {
	set sourceFile [file readlink $srFile]
	if {[string compare [file dirname $sourceFile] .] == 0} {set sourceFile $srSnapshotDir/$sourceFile}
	#--- Could be double layer link:
	if {[string compare [file type $sourceFile] link] == 0} {
	    set sourceFile [file readlink $sourceFile]
	    if {[string compare [file dirname $sourceFile] .] == 0} {set sourceFile $srSnapshotDir/$sourceFile}
	}
    } else {
	set sourceFile $srFile
    }
    if [catch {APSSCRAlterSnapshot \
		   -sourceSnapshot  $sourceFile \
		   -description $srFileDescription \
		   -replacementData $replacementFile -machine $machineName} result] { \
        return -code error "APSSCRAlterSnapshot(SR: $sourceFile; $replacementFile): $result" \
    }

    #--- Altering gains...
    set machineName SBPMs
    set replacementFile $tmpRoot.gain
    exec sddsconvert $sbpmFile -pipe=out -del=col,ValueString \
	| sddsxref -pipe=in $newGainFile $replacementFile -take=ValueString -match=ControlName -nowarning
    if {[string compare [file type $sbpmFile] link] == 0} {
	set sourceFile [file readlink $sbpmFile]
    } else {
	set sourceFile $sbpmFile
    }
    if [catch {APSSCRAlterSnapshot \
		   -sourceSnapshot  $sourceFile \
		   -description $sbpmFileDescription \
		   -replacementData $replacementFile -machine $machineName} result] { \
        return -code error "APSSCRAlterSnapshot(SBPMs: $sourceFile; $replacementFile): $result" \
    }
    
    file delete $tmpRoot.comb $tmpRoot.gain $tmpRoot.offsetp
}

#-----------------------------------------------------------------------------------------------------------------

proc DoEverything { args } {

    global tmpDir
    APSParseArguments {referenceOffsetFile referenceSetpointFile referenceGainFile locoDirectory \
			   sbpmFile sbpmFileDescription srFile srFileDescription xbpmActionList ybpmActionList} 

    APSSetVarAndUpdate status "Calculating gain adjustments..."
    set gainAdjustmentFile $tmpDir/gainAdjustment.sdds
    if [catch {MakeNewGains -dir $locoDirectory -outputFile $gainAdjustmentFile \
		   -xbpmActionList $xbpmActionList -ybpmActionList $ybpmActionList} result] {
	APSSetVarAndUpdate status "MakeNewGains: $result"
	return
    }
    
    APSSetVarAndUpdate status "Calculating new gains, setpoints, and offsets..."
    set newOffsetFile $tmpDir/newOffsets.tmp
    set newSetpointFile $tmpDir/newSetpoints.tmp
    set newGainFile $tmpDir/newGains.tmp
    if [catch {MakeNewVariables -gainAdjustmentFile $gainAdjustmentFile \
		   -referenceOffsetFile $referenceOffsetFile -newOffsetFile $newOffsetFile \
		   -referenceSetpointFile $referenceSetpointFile -newSetpointFile $newSetpointFile \
		   -referenceGainFile $referenceGainFile -newGainFile $newGainFile} result] {
	APSSetVarAndUpdate status "MakeNewVariables $result"
	return
    }

    APSSetVarAndUpdate status "Altering files..."
    if [catch {AlterSnapshotFiles \
		   -newOffsetFile $newOffsetFile -newSetpointFile $newSetpointFile -newGainFile $newGainFile \
		   -sbpmFile $sbpmFile -sbpmFileDescription $sbpmFileDescription \
		   -srFile $srFile -srFileDescription $srFileDescription } result] {
	return -code error "AlterSnapshotFiles: $result"
    }
    file delete $newGainFile $newOffsetFile $newSetpointFile $gainAdjustmentFile
    APSSetVarAndUpdate status "Done."
}

#------------------------------------------------------------------------------------------------------------------------
proc UpdateButtons {args} {
    global bpmSufList sectorList
    APSParseArguments {locoDir}
    set locoVarFile $locoDir/variables.sdds
    set locoResultsFile $locoDir/[exec sddsprocess $locoVarFile -pipe=out -match=col,VariableName=resultsFile \
				      | sdds2stream -pipe=in -col=VariableValue]
    set weightFile $locoDir/[exec sddsprocess $locoVarFile -pipe=out -match=col,VariableName=manualWeightFile \
				 | sdds2stream -pipe=in -col=VariableValue]

    set hbpmListLOCO [join [exec sddsprocess $locoResultsFile -pipe=out -match=para,PageID=xBpm -nowarning \
				| sdds2stream -pipe=in -col=ElementName] ]
    set vbpmListLOCO [join [exec sddsprocess $locoResultsFile -pipe=out -match=para,PageID=yBpm -nowarning \
				| sdds2stream -pipe=in -col=ElementName] ]
    set badBpmList [join [exec sddsprocess $weightFile -pipe=out -filter=col,Weight,-1e-10,0.1 -nowarning \
			      "-edit=col,BPMName,TagName,S@#@6d" \
			      | sdds2stream -pipe=in -col=BPMName] ]

    if [llength $badBpmList] {
	foreach badBpm $badBpmList {
	    set indexH [lsearch -exact $hbpmListLOCO $badBpm]
	    if {$indexH != -1} {
		set hbpmListLOCO [lreplace $hbpmListLOCO $indexH $indexH]
	    }
	    set indexV [lsearch -exact $vbpmListLOCO $badBpm]
	    if {$indexV != -1} {
		set vbpmListLOCO [lreplace $vbpmListLOCO $indexV $indexV]
	    }
	}
    }
    foreach rootname [list xBpms yBpms] elementList [list $hbpmListLOCO $vbpmListLOCO] {
	ToggleAllButtons -mode all-off -rootname $rootname -itemList $bpmSufList -sectorCount [llength $sectorList] \
	    -sectorList $sectorList
	ToggleAllButtons -mode data -rootname $rootname -itemList $bpmSufList -sectorCount [llength $sectorList] \
	    -sectorList $sectorList -elementList $elementList
    }
}
#------------------------------------------------------------------------------------------------------------------------
proc ToggleAllButtons {args} {
    set missingList ""
    set missingListVar ""
    set elementList ""
    APSStrictParseArguments {mode rootname sectorCount itemList missingList missingListVar sectorList elementList}

    if [string length $missingListVar] {
        global $missingListVar
        set missingList [set $missingListVar]
    }
    switch $mode {
        all-on  {set value 1; set useData 0}
        all-off {set value 0; set useData 0}
	data {set useData 1}
        default {return}
    }
    global ${rootname}CBWidget
    if $useData {
	foreach element $elementList {
	    set nameFlag $rootname$element
	    global $nameFlag
	    if {[info exists $nameFlag]} {
		if {[set $nameFlag] != 1} {
		    [set ${rootname}CBWidget($element)] invoke
		}
	    }
	}
    } else {
	foreach item $itemList {
	    set toggleVar ${rootname}${item}
	    global $toggleVar
	    set $toggleVar $value
	    foreach sector $sectorList {
		set name ${sector}${item}
		set nameFlag ${rootname}${name}
		global $nameFlag
		if {[info exists $nameFlag]} {
		    if {[set $nameFlag] != [set $toggleVar]} {
			[set ${rootname}CBWidget($name)] invoke
		    }
		}
	    }
	}
    }
    APSSRSectorUnsetMissing -rootname $rootname -missingList $missingList -sectorList $sectorList
}
#------------------------------------------------------------------------------------------------------------------------
proc ReadButtons {args} {
    global bpmSufList sectorList
    APSParseArguments {rootname}
    set elementList ""
    foreach bpmSuf $bpmSufList {
	foreach sector $sectorList {
	    set name ${sector}${bpmSuf}
	    set nameFlag ${rootname}${name}
	    global $nameFlag
	    if [set $nameFlag] {
		lappend elementList $name
	    }
	}
    }
    return $elementList
}
#-----------------------------------------------------------------------------------------------------------------------

set referenceSetpointFile /home/helios/oagData/SCR/snapshots/SR/SR-UserBeamPreferred.gz
set referenceGainFile /home/helios/oagData/SCR/snapshots/SBPMs/SBPMs-Preferred.gz
set referenceOffsetFile /home/helios/oagData/SCR/snapshots/SR/SR-BPMOffsetReference.gz
set sbpmFile2Alter /home/helios/oagData/SCR/snapshots/SBPMs/SBPMs-Preferred.gz
set srFile2Alter /home/helios/oagData/SCR/snapshots/SR/SR-UserBeamPreferred.gz
#set sbpmFile2AlterDescription "Altered: [exec sdds2stream $sbpmFile2Alter -para=SnapshotDescription]"
#set srFile2AlterDescription "Altered: [exec sdds2stream $srFile2Alter -para=SnapshotDescription]"
set sbpmFile2AlterDescription ""
set srFile2AlterDescription ""
set tmpDir /tmp

#--- BPM frame

set tabFrameWidth 1100
set tabFrameHeight 360
set bpmConfigFile /home/helios/oagData/sr/BPMStatus/config.sdds
set deviceWidgetList [APSTabFrame .tabsMain -parent .userFrame -width $tabFrameWidth -height $tabFrameHeight \
    -labelList "\"Hor. BPMs\" \"Ver. BPMs\"" -label ""]
set bpmSufList [list 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 sectorList [list 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]
set missingBPMsH [join [exec sddsprocess $bpmConfigFile -pipe=out -match=col,DeviceName=*\[ABC\]:* \
			    -filter=col,NonexistentH,1,1 \
			    | sdds2stream -pipe=in -col=DeviceName] ]
set missingBPMsV [join [exec sddsprocess $bpmConfigFile -pipe=out -match=col,DeviceName=*\[ABC\]:* \
			    -filter=col,NonexistentV,1,1 \
			    | sdds2stream -pipe=in -col=DeviceName] ]

APSSRSectorButtons .hbpmButtons -parent [lindex $deviceWidgetList 0] -rootname xBpms \
    -orientation horizontal -sectorList $sectorList \
    -label "H BPMs" \
    -description "Choose horizontal BPMs for which the gain correction will be done." \
    -itemList $bpmSufList -packOption "-side top" \
    -itemLabelList $bpmSufList -sectorControl 1 -missingList $missingBPMsH
APSSRSectorButtons .vbpmButtons -parent [lindex $deviceWidgetList 1] -rootname yBpms \
    -orientation horizontal -sectorList $sectorList \
    -label "V BPMs" \
    -description "Choose vertical BPMs for which the gain correction will be done." \
    -itemList $bpmSufList -packOption "-side top" \
    -itemLabelList $bpmSufList -sectorControl 1 -missingList $missingBPMsH

#--- Reference files frame -----------------------------------------------------------------------------------------

APSFrame .reference -parent .userFrame -label "Reference files" \
    -contextHelp "Frame to choose reference files for setpoints , offsets and gains."
    
set w0 .userFrame.reference.frame
APSFrameGrid .grid -parent $w0 -yList {y1 y2 y3}

set w $w0.grid.y1
APSLabeledEntry .offsetentry -parent $w -label "Offset:  " \
    -textVariable referenceOffsetFile -width 80 -contextHelp \
    "Offset reference file - the file with present values to be corrected."
APSButton .dir -parent $w.offsetentry \
    -text F -packOption "-side right" \
    -command "findFiles -fileVar referenceOffsetFile" \
    -contextHelp "Bring up a file selection dialog box to choose the offset reference file."

set w $w0.grid.y2
APSLabeledEntry .setpointentry -parent $w -label "Setpoint: " \
    -textVariable  referenceSetpointFile -width 80 -contextHelp \
    "Setpoint reference file - the file with present values to be corrected."
APSButton .dir -parent $w.setpointentry \
    -text F -packOption "-side right" \
    -command "findFiles -fileVar referenceSetpointFile" \
    -contextHelp "Bring up a file selection dialog box to choose the setpoint reference file."

set w $w0.grid.y3
APSLabeledEntry .gainentry -parent $w -label "Gain:   " \
    -textVariable  referenceGainFile -width 80 -contextHelp \
    "Gain reference file - the file with present values to be corrected."
APSButton .dir -parent $w.gainentry \
    -text F -packOption "-side right" \
    -command "findFiles -fileVar referenceGainFile" \
    -contextHelp "Bring up a file selection dialog box to choose the gain reference file."

#--- LOCO configuration frame --------------------------------------------------------------------------------------

APSFrame .locoConfig -parent .userFrame -label "Choose LOCO configuration"
set w .userFrame.locoConfig.frame
    
APSLabeledOutput .configName -parent $w -textVariable configComment -label "" -width 63

set pickDir $w.pickDir.button
APSButton .pickDir -parent $w -text "Choose LOCO dataset" -command {
    APSDisableButton $pickDir
    set locoDirectory [ChooseLOCOConfiguration -configDirectory $configDirectory]
    if {[llength $locoDirectory] != 1} {
	APSAlertBox .noChosen -errorMessage "Error: Number of savesets chosen must be 1."
    } else {
	set configComment [exec cat $locoDirectory/comment]
	APSSetVarAndUpdate status "Configuration choice is done."
	set locoNumber [file tail $locoDirectory]
	set sbpmFile2AlterDescription \
	    "Altered: LOCO $locoNumber: [exec sdds2stream $sbpmFile2Alter -para=SnapshotDescription]"
	set srFile2AlterDescription \
	    "Altered: LOCO $locoNumber: [exec sdds2stream $srFile2Alter -para=SnapshotDescription]"
	if [catch {UpdateButtons -locoDir $locoDirectory} result] {
	    return -code error "UpdateButtons: $result"
	}
    }
    update idletasks
    APSEnableButton $pickDir
} -contextHelp "Chooses the results of LOCO fit from the list of existing ones."

APSButton .calcAdjustments -parent $w \
    -text "Convert Gains and Plot..." \
    -command {
	set gainAdjustmentFile $tmpDir/gainAdjustment.sdds
	if [catch {MakeNewGains -dir $locoDirectory -outputFile $gainAdjustmentFile -plot 1} result] {
	    APSSetVarAndUpdate status "MakeNewGains: $result"
	    return
	}
    } -contextHelp "Computes new calibration coefficients (gains) based on LOCO fit. The definition of gain errors in LOCO fit is different from the bpm system. This button creates file with gains in bpm system definition."

#--- Files to alter frame -------------------------------------------------------------------------------

APSFrame .scr -parent .userFrame -label "Files to alter" \
    -contextHelp "Frame to choose reference files for setpoints , offsets and gains."

set w0 .userFrame.scr.frame
APSFrameGrid .grid -parent $w0 -yList {y1 y2 y3 y4}

set w $w0.grid.y1
APSLabeledEntry .srFile -parent $w -label "SR file to alter:  " \
    -textVariable srFile2Alter -width 80 -contextHelp \
    "Name of SR file to alter."
APSButton .dir -parent $w.srFile \
    -text F -packOption "-side right" \
    -command {
	findFiles -fileVar srFile2Alter
	set srFile2AlterDescription "Altered: [exec sdds2stream $srFile2Alter -para=SnapshotDescription]"
    } \
    -contextHelp "Bring up a file selection dialog box to choose the offset reference file."

set w $w0.grid.y2
APSLabeledEntry .srFileDescription -parent $w -label "Description:  " \
    -textVariable srFile2AlterDescription -width 80 -contextHelp \
    "Name description for SR file."

set w $w0.grid.y3
APSLabeledEntry .sbpmFile -parent $w -label "SBPM file to alter:" \
    -textVariable  sbpmFile2Alter -width 80 -contextHelp \
    "name of SBPM file to alter."
APSButton .dir -parent $w.sbpmFile \
    -text F -packOption "-side right" \
    -command {
	findFiles -fileVar sbpmFile2Alter
	set sbpmFile2AlterDescription "Altered: [exec sdds2stream $sbpmFile2Alter -para=SnapshotDescription]"
    } \
    -contextHelp "Bring up a file selection dialog box to choose the gain reference file."

set w $w0.grid.y4
APSLabeledEntry .sbpmFileDescription -parent $w -label "Description:  " \
    -textVariable sbpmFile2AlterDescription -width 80 -contextHelp \
    "Name description for SBPM file."

#--- ActionButton --------------------------------------------------------------------------------------------------

APSButton .adjustGains -parent .userFrame \
    -text "Multiply offsets and setpoints by gain, adjust gains, and alter files" \
    -command {
	set xbpmActionList [ReadButtons -rootname xBpms]
	set ybpmActionList [ReadButtons -rootname yBpms]
	if [catch {DoEverything \
		       -xbpmActionList $xbpmActionList \
		       -ybpmActionList $ybpmActionList \
		       -referenceOffsetFile $referenceOffsetFile \
		       -referenceSetpointFile $referenceSetpointFile \
		       -referenceGainFile $referenceGainFile \
		       -locoDirectory $locoDirectory \
		       -sbpmFile $sbpmFile2Alter -sbpmFileDescription $sbpmFile2AlterDescription \
		       -srFile $srFile2Alter -srFileDescription $srFile2AlterDescription} result] {
	    APSSetVarAndUpdate status "DoEverything: $result"
	}
    } -contextHelp "Computes new offsets, setpoints, and gains from reference files by multiplying them with values from gain adjustment file. Then alters above snapshot files and saves them with comments entered above." \
    -packOption "-side top"



set locoDirectory ""
set configComment ""
set bpmStatusFile /home/helios/oagData/sr/BPMStatus/config.sdds
