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

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
APSStandardSetup

set usage "usage: SRCorrectorPunchDown -buttonOrientation <value>"
set buttonOrientation horizontal
set args $argv
APSParseArguments {buttonOrientation}

set CVSRevisionAuthor "\$Revision: 1.19 $ \$Author: lemery $"

APSApplication . -name SRCorrectorPunchDown -version $CVSRevisionAuthor \
  -overview {This application punches down SR correctors to zero in steps. Before any changes are made a snapshot of current machine configuration is recorded in /home/helios/oagData/SCR/snapshots/SRCorSetpts directory. Restore feature available.}

set h_activeList ""
set v_activeList ""

proc ConfirmRestart {args} {
    set mode vertical
    APSStrictParseArguments {mode}
    if {[APSMultipleChoice [APSUniqueName .] -question \
           "Are you sure?  Exit and restart in $mode mode?"  \
           -returnList {1 0} -labelList {Yes No}]} { 
        exec SRCorrectorPunchDown -buttonOrientation $mode & 
        exit
    }
}

set sconfigConfigStatus ""
APSScrolledStatus .scrolled -parent .userFrame -textVariable sconfigConfigStatus \
  -width 85 -height 6 -lineLimit 1000 -label "" -withButtons 1

set sconfigConfigStatus "Ready..."


proc ChangeMarkedCorrSetup {args} {
    
    global stepNum pause snapshot sconfigConfigStatus abortPressed w1 controlVar hcorrItemList vcorrItemList
    global h_delta h_string hResult h_deltaFlag h_list
    global v_delta v_string esult v_deltaFlag v_list

    APSAlertBox .warning -name "Check Controllaw Limits Setting" \
      -errorMessage "Please check controllaw limits and make sure controllaw is running.\nThe controllaw limits for corrector punch down should be as following:\n\nturn off despike on both planes\n\nCorrect range error limit: 300 A \n\n Correcto step limit: 2 A\n\nbpm limit: 15 mm\n\nxray bpm limit: 50mm\n\n no gaps are closed\n\nPress the \"Full Start\" on the controllaw panel after these limits being changed\n\nPress following OK button to continue if the controllaw is running" -type warning
    
    WidgetMgr

    set sconfigConfigStatus "Working..."
    APSDisableButton $w1.run.button
    APSDisableButton $w1.check.button
    set abortPressed 0
    set controlVar 1
    setOpenCloseAbortBut 0
    setOpenCloseResetBut 0
    update

    if [catch {APSSaveMachine -machine SRCorSetpts \
                   -description "Routine save: save prior to punching down."} snapshot] {
        return -code error "SRCorrSetToZero: $snapshot"
    }

    if {$stepNum < 1} {
        set sconfigConfigStatus "No steps selection"
        APSEnableButton $w1.run.button
        setOpenCloseAbortBut 1
        return
    }

    set h_activeList ""
    set h_itemList  $hcorrItemList
    set h_list ""
    set v_list ""
    
    
    foreach h_item $h_itemList {
        for {set hn 1} {$hn <= 40} {incr hn} {
            set h_name hCorrS[format %02ld $hn]${h_item}
            set h_nameFlag ${h_name}
            global ${h_nameFlag}

            if [set ${h_nameFlag}] {
                lappend h_list S[format %02ld $hn]${h_item}
                lappend h_activeList "S[format %02ld $hn]${h_item}:PS:SetCurrentC"
            }
        }
    }
    
    set v_itemList $vcorrItemList
    set v_activeList ""
    
    foreach v_item $v_itemList {
        for {set vn 1} {$vn <= 40} {incr vn} {
            set v_name vCorrS[format %02ld $vn]${v_item}
            set v_nameFlag ${v_name}
            global ${v_nameFlag}
            if [set ${v_nameFlag}] {
                lappend v_list S[format %02ld $vn]${v_item}
                lappend v_activeList "S[format %02ld $vn]${v_item}:PS:SetCurrentC"
            }
        }
    }

    if {[llength $h_activeList] == 0 && [llength $v_activeList] == 0} {
        set sconfigConfigStatus "No corrector selected"
        APSEnableButton $w1.run.button
        setOpenCloseAbortBut 1
        return
    } 
    set h_deltaFlag 1
    if {[llength $h_activeList] == 0 && [llength $v_activeList] != 0} {
        if {$h_delta > 0} {
            set h_deltaFlag 0
        }
    }
    set v_deltaFlag 1
    if {[llength $h_activeList] != 0 && [llength $v_activeList] == 0} {
        if {$v_delta > 0} {
            set v_deltaFlag 0
        }
    }

    if {[llength $h_activeList] && ${h_delta}==0} {
        set sconfigConfigStatus "No delta selected for horizontal corrector"
        APSEnableButton $w1.run.button
        setOpenCloseAbortBut 1
        return
    }

    if {[llength $v_activeList] && ${v_delta}==0} {
        set sconfigConfigStatus "No delta selected for vertical corrector"
        APSEnableButton $w1.run.button
        setOpenCloseAbortBut 1
        return
    }

    set h_string [join $h_activeList ,]
   # puts "active: $h_activeList"
    set v_string [join $v_activeList ,]
    set hResult ""
    set vResult ""

    PunchDownProcessing
    
    WidgetMgr

    if $controlVar {
        ConfirmResults
    }

    setOpenCloseAbortBut 1
    setOpenCloseResetBut 1
    set sconfigConfigStatus "Done."
    bell
    APSEnableButton $w1.run.button
    APSEnableButton $w1.check.button
}

proc SetListsOfCorrectors {} {
    global h_activeList v_activeList h_deltaFlag v_deltaFlag hcorrItemList vcorrItemList
    global sconfigConfigStatus h_delta v_delta w1

    set h_activeList ""
    set h_itemList $hcorrItemList
    
    foreach h_item $h_itemList {
        for {set hn 1} {$hn <= 40} {incr hn} {
            set h_name hCorrS[format %02ld $hn]${h_item}
            set h_nameFlag ${h_name}
            global ${h_nameFlag}

            if [set ${h_nameFlag}] {
                lappend h_activeList "S[format %02ld $hn]${h_item}:PS:SetCurrentC"
            }
        }
    }
    
    set v_itemList $vcorrItemList
    set v_activeList ""
    
    foreach v_item $v_itemList {
        for {set vn 1} {$vn <= 40} {incr vn} {
            set v_name vCorrS[format %02ld $vn]${v_item}
            set v_nameFlag ${v_name}
            global ${v_nameFlag}
            if [set ${v_nameFlag}] {
                lappend v_activeList "S[format %02ld $vn]${v_item}:PS:SetCurrentC"
            }
        }
    }
    if {[llength $h_activeList] == 0 && [llength $v_activeList] == 0} {
        set sconfigConfigStatus "None of correctors is selected."
        APSEnableButton $w1.run.button
        setOpenCloseAbortBut 1
        return 0
    }
    set h_deltaFlag 1
    if {[llength $h_activeList] == 0 && [llength $v_activeList] != 0} {
        if {$h_delta > 0} {
            set h_deltaFlag 0
        }
    }
    set v_deltaFlag 1
    if {[llength $h_activeList] != 0 && [llength $v_activeList] == 0} {
        if {$v_delta > 0} {
            set v_deltaFlag 0
        }
    }
    return 1
}

proc ConfirmResults {} {
    global hResult h_string h_deltaFlag HsortList
    global vResult v_string v_deltaFlag VsortList sconfigConfigStatus

    WidgetMgr
    
    if [llength $hResult] {
        set outputHList ""
        set count 0
        array set harray $hResult
        foreach hel [array names harray] {
            lappend outputHList "$hel=$harray($hel)"
            incr count
        }
        set HsortList ""      
        SortList -sortList $outputHList -group H
        APSScrolledListWindow .noHzero -name "Horizontal Correctors" -itemList $HsortList \
          -closeButton 1 -clearButton 0 -acceptButton 0 \
          -contextHelp "Displays all of chosen horizontal correctors which current value is currently not zero"

    } else {
        if {[string length $h_string] && $h_deltaFlag} {
            set sconfigConfigStatus "All current values for the chosen horizontal correctors are zero"
        }
    }

    if [llength $vResult] {
        set outputVList ""
        set count 0
        array set varray $vResult
        foreach vel [array names varray] {
            lappend outputVList "$vel=$varray($vel)"
            incr count
        }
        set VsortList ""
        SortList -sortList $outputVList -group V
        APSScrolledListWindow .noVzero -name "Vertical Correctors" -itemList $VsortList \
          -closeButton 1 -clearButton 0 -acceptButton 0 \
          -contextHelp "Displays all of chosen vertical correctors which current value is currently not zero"
    } else {
        if {[string length $v_string] && $v_deltaFlag} {
            set sconfigConfigStatus "All current values for the chosen vertical correctors are zero"
        }
    }
}

proc SortList {args} {
    set sortList ""
    set group ""
    APSStrictParseArguments {sortList group}
    global HsortList VsortList
    set midstepList ""

    foreach name $sortList {
        set newName [string trimleft $name S]
        if {[string first A $newName] == 1 || [string first B $newName] == 1} {
            set newName 0$newName
        }
        lappend midstepList $newName
    }
    set midstepList [lsort -increasing $midstepList]
    foreach midName $midstepList {
        if {[string index $midName 0] == 0} {
            set midName [string trimleft $midName 0]
        }
        lappend ${group}sortList S$midName
    }
}

proc PunchDownProcessing {} { 
    global h_string h_delta hResult HreturnFlag stopH h_list 
    global v_string v_delta vResult VreturnFlag stopV v_list 
    global stepNum sconfigConfigStatus pause controlVar 
    global abortPressed w1 nonzeroCountH nonzeroCountV
    
    set h_step 0
    set v_step 0
    set booleanFlag 0
    set nochanged 0
    set HreturnFlag 0
    set VreturnFlag 0
    set stopH 0
    set stopV 0

  
    for {set step 1} {$step <= [expr $stepNum + 1]} {incr step} { 
        if {$step == $stepNum} {
            set booleanFlag 1
        }
        if {$step <= $stepNum} {
            set sconfigConfigStatus "Step $step is running"
            update
            bell
        }
        
        if {[string length $h_string]} {
            if [catch {eval exec cavget -list=$h_string -pend=30 \
                           -label {{-delim= }}} hValues] {
                set sconfigConfigStatus "Error from Hcavget: $hValues"
            }
	    if [string length $hValues] {
		if [ catch {SetNewValue -values $hValues -delta $h_delta \
				-flag $booleanFlag -group H} hResult] {
		    set sconfigConfigStatus "Error from Hsetnewvalues: $hResult"
		} else {
                    if {$step <= $stepNum} {
                        set h_count $nonzeroCountH
                        set sconfigConfigStatus "$h_count horizontal correctors are nonzero."
                        update
                    }
                }
            }
        }
        
	if {[string length $v_string]} {
            if [catch {eval exec cavget -list=$v_string -pend=30 \
                           -label {{-delim= }}} vValues] {
                set sconfigConfigStatus "Error from V-plane cavget: $vValues"
            }
            if [llength $vValues] {
                if [ catch {SetNewValue -values $vValues -delta $v_delta \
                                -flag $booleanFlag -group V} vResult] {
                    set sconfigConfigStatus "Error from V-plane setnewvalues: $vResult"
                } else {
                    if {$step <= $stepNum} {
                        set v_count $nonzeroCountV
                        set sconfigConfigStatus "$v_count vertical correctors are nonzero." 
                        update
                    }
                }
            }
        }
        if {[string length $h_string] && [string length $v_string]} {
            if {$h_step && $v_step} {
                set step [expr $stepNum + 1]
            }
           
        }
        if {![string length $h_string] && [string length $v_string]} {
            if $v_step {
                set step [expr $stepNum + 1]
            }
           
        }
        if {[string length $h_string] && ![string length $v_string]} {
            if $h_step {
                set step [expr $stepNum + 1]
            }
          
        }
        
        APSWaitWithUpdate -waitSeconds $pause -abortVariable abortPressed \
          -updateInterval 1
        if $abortPressed {
            set sconfigConfigStatus "Procedure aborted in $step step."
            setOpenCloseAbortBut 1  
            setOpenCloseResetBut 1
            set controlVar 0

            WidgetMgr
            APSEnableButton $w1.run.button
            return
        }
        if {$stopH && $stopV} {
            return
        }
    }
}


proc SetNewValue {args} {
    set values ""
    set delta 0
    set flag 0
    set group ""
    APSStrictParseArguments {values delta flag group}

    global sconfigConfigStatus HreturnFlag VreturnFlag stopH stopV nonzeroCount nonZeroCountH nonZeroCountV

    if {$delta <= 0} {
        set sconfigConfigStatus $delta
        return
    }
    set nonzeroCountH 0
    set nonzeroCountV 0
    array set valueNow $values

    set max 0
    set maxName ""
    foreach corr [array names valueNow] {
        if {$valueNow($corr) == 0} {
            unset valueNow($corr)
        } else {
            set magnitude $valueNow($corr)
            if {[expr abs($magnitude) > abs($max)]} {
                set max $magnitude
                set maxName $corr
            }
        }
    }
    if ![array size valueNow] {
        set stop$group 1
        return
    }

    if {abs($max) > 0 && ![set ${group}returnFlag]} {
        set sconfigConfigStatus "$maxName is the strongest $group corrector with the strength of $max."
    }

    set outputList ""
    set retList ""
    set nonzeroCount 0
    foreach elem [array names valueNow] {
        set value $valueNow($elem)
        if {$value>0} {
            set sign 1
        } elseif {$value<0} {
            set sign -1
        } else {
            continue
        } 
        if ![scan $value "%lf" result] {
            set sconfigConfigStatus "Return value from $elem is $value"
            return
        }
        if ![set ${group}returnFlag] {
            if {$value != 0} {
                if [expr abs($value)<=$delta] {
                    set newValue 0
                } else {
                    set newValue [expr $value-$sign*$delta]
                    incr nonzeroCount
                }
                lappend outputList "$elem=$newValue"
            }
        } else {
            lappend retList $elem 
            lappend retList $valueNow($elem)
        }
    }
    set nonzeroCount$group $nonzeroCount
    if {[llength $outputList] && ![set ${group}returnFlag]} {
        exec cavput -list=[join $outputList ,] -pend=30
    }

    if [set ${group}returnFlag] {
        return $retList
    }
    if $flag {
        set ${group}returnFlag 1
    }
}


proc MakeListOfCorrectors {corrState args} {
    set plane h
    APSParseArguments {plane}
    
    global v_activeList h_activeList vResult hResult v_string h_string 
    global sconfigConfigStatus
    set vResult ""
    set hResult ""
    set v_string ""
    set h_string ""
    set v_zero_result ""
    set h_zero_result ""

    if ![string compare $corrState nonzero] {
        set sconfigConfigStatus "Looking for nonzero correctors..."
        update

        WidgetMgr
    }
    if ![string compare $corrState zero] {
        set sconfigConfigStatus "Looking for zero correctors..."
        update
    } 

    catch {SetListsOfCorrectors} result
    if !$result {
        return
    }
    
    set h_0_Count 0
    if [llength $h_activeList] {
        set h_string [join $h_activeList ,]

        if [string length $h_string] {
            if [catch {eval exec cavget -list=$h_string -pend=30 \
                           -label \"-delim= \"} hValues] {
                set sconfigConfigStatus "Error from Hcavget: $hValues"
                return
            } else {
                set horCount 0
                array set hcorrValue $hValues
                foreach hcorr [array names hcorrValue] {
                    set hmag $hcorrValue($hcorr)
                    if [expr abs($hmag) > 0] {
                        lappend hResult $hcorr
                        lappend hResult $hmag
                        incr horCount
                    } else {
                        lappend h_zero_result $hcorr
                        incr h_0_Count
                    }
                }
                if ![string compare $corrState nonzero] {
                    set sconfigConfigStatus "Found $horCount nonzero horizontal correctors."
                } else {
                    set sconfigConfigStatus "Found $h_0_Count zero horizontal correctors."
                }
                update
            }
        }
    }
    
    set v_0_Count 0
    if [llength $v_activeList] {
        set v_string [join $v_activeList ,]

        if [string length $v_string] {
            if [catch {eval exec cavget -list=$v_string -pend=30 \
                         -label {{-delim= }}} vValues] {
                set sconfigConfigStatus "Error from Vcavget: $vValues"
                return
            } else {
                set verCount 0
                array set vcorrValue $vValues
                foreach vcorr [array names vcorrValue] {
                    set vmag $vcorrValue($vcorr)
                    if [expr abs($vmag) > 0] {
                        lappend vResult $vcorr
                        lappend vResult $vmag
                        incr verCount
                    } else {
                        lappend v_zero_result $vcorr
                        incr v_0_Count
                    }
                    
                }
                if ![string compare $corrState nonzero] {
                    set sconfigConfigStatus "Found $verCount nonzero vertical correctors."
                } else {
                    set sconfigConfigStatus "Found $v_0_Count zero vertical correctors."
                }
                update
            }
        }
    }
    
    if ![string compare $corrState nonzero] {
        ConfirmResults
    }
    if ![string compare $corrState zero] {
        if {$h_0_Count || $v_0_Count} {
            TurnOffZeroCorrectors $h_zero_result $v_zero_result
        } else {
            set sconfigConfigStatus "None of selected correctors has zero value."
            return
        }
    }

    foreach var [list vResult hResult v_string h_string h_activeList v_activeList \
                   v_zero_result h_zero_result] {
        set $var ""
    }
    foreach arr [list hcorrValue vcorrValue] {
        if [info exists $arr] {
            unset $arr
        }
    }
    set sconfigConfigStatus "Done."
    update
}

#done
proc TurnOffZeroCorrectors {H_list V_list} {
    global sconfigConfigStatus
    set hvOffList ""

    if [llength $H_list] {
        foreach hcorr $H_list {
            regexp {(S[0-9][0-9][AB]:F?H.)} $hcorr {} device
            lappend hvOffList $device
        }
    }
   
    if [llength $V_list] {
        foreach vcorr $V_list {
            regexp {(S[0-9][0-9][AB]:F?V.)} $vcorr {} device
            lappend hvOffList $device
        }
    }
    if [llength $hvOffList] {
        if [catch {eval exec cavput -pend=15 -blunderAhead=silently \
                     -list=[join $hvOffList ,] -list=:PS:SupplyOffC.PROC=1} result] {
            set sconfigConfigStatus "Error: $result"
            return
        }
    } 
    
    set sconfigConfigStatus "[llength $H_list] horizontal correctors are turned off."
    set sconfigConfigStatus "[llength $V_list] vertical correctors are turned off."
}

proc TurnOnAllOffListedCorrectors {} {
    global v_activeList h_activeList sconfigConfigStatus
    set lists ""
    set correctorList ""
    set H_count 0
    set V_count 0

    set sconfigConfigStatus "Looking for turned off correctors..."
    update

    catch {SetListsOfCorrectors} result
    if !$result {
        return
    }
    
    foreach corr $v_activeList {
        lappend correctorList [APSStringTrimRight $corr :PS:SetCurrentC]
    }
    foreach corr $h_activeList {
        lappend correctorList [APSStringTrimRight $corr :PS:SetCurrentC]
    }
    if [llength $correctorList] {
        lappend lists "-list=[join $correctorList ,]"
    } else {
        return
    }

    if [catch {eval exec cavget $lists -list=:PS:OnOffStatusM -pend=30 -label} result] {
        set sconfigConfigStatus "Error: $result"
        return
    }
    set statusList [APSSearchAssociateListForValue -list $result -notvalue 0]
    if ![llength $statusList] {
        return 
    }
    regsub -all {:PS:OnOffStatusM} $statusList {} statusList
    # the PROC part is necessary
    if [catch {eval exec cavput -blunder=silent -list=[join $statusList ,] -list=:PS:SupplyOnC.PROC=1} result] {
        set sconfigConfigStatus "Error: $result"
        return
    }

    foreach elem $statusList {
        if {[string first H $elem] >= 0} {
            incr H_count
        }
        if {[string first V $elem] >= 0} {
            incr V_count
        }
    }	
    
    set sconfigConfigStatus "$H_count horizontal correctors are commanded to turned on."
    set sconfigConfigStatus "$V_count vertical correctors are commanded to turned on."
    set sconfigConfigStatus "Done."     
}

proc RestoreSelectedCorrectors {} {
    global snapshot sconfigConfigStatus 

    set corrSnapDir /home/helios/oagData/SCR/snapshots/SRCorSetpts
    if [string length $snapshot] {
        set tempFile /tmp/[APSTmpString]
        if [catch {exec gzip -dcn $corrSnapDir/$snapshot > $tempFile
            APSRampToSnapshot -initDialog 1 -fileName $tempFile
        } result] {
            set sconfigConfigStatus "RestoreSelectedCorrectors: $result" 
        }
        catch {exec rm $tempFile}
    }

    WidgetMgr

    setOpenCloseResetBut 0
    setOpenCloseAbortBut 1
    set sconfigConfigStatus "All values are restored"
}

proc setOpenCloseResetBut {state} {
    global w1
    if $state {
        APSEnableButton $w1.reset.button
    } else {
        APSDisableButton $w1.reset.button
    } 
}

proc setOpenCloseAbortBut {state} {
    global w1
    if $state {
        APSDisableButton $w1.abort.button
    } else {
        APSEnableButton $w1.abort.button
    } 
}

proc setOpenCloseOnOffButs {state} {
    global w2
    if $state {
        APSEnableButton $w2.turnOff.button
        APSEnableButton $w2.turnOn.button
    } else {
        APSDisableButton $w2.turnOff.button
        APSDisableButton $w2.turnOn.button
    } 
}

proc WidgetMgr {} {
    set w_1 .noHzero
    if [winfo exists $w_1] {
        destroy $w_1
    }

    set w_2 .noVzero
    if [winfo exists $w_2] {
        destroy $w_2
    }
}

proc WriteFile {} {
    global outputFile writeDescription sconfigConfigStatus

    set fileDir [file dirname $outputFile]
    if ![file isdirectory $fileDir] {
        set sconfigConfigStatus "$fileDir is not a valid directory."
        return
    }

    if [file exists $outputFile] {
        set sconfigConfigStatus "File $outputFile alredy exists."
        return
    } 

    global hcorrItemList vcorrItemList
    set corCount(h) [APSCountSRConfig -rootname hCorr \
                       -itemList $hcorrItemList \
                       -missingList {}]
    set corCount(v) [APSCountSRConfig -rootname vCorr \
                       -itemList $vcorrItemList \
                       -missingList {}]

    set confirm 0
    set confirmQuery ""
    if {!$corCount(h) && !$corCount(v)} {
        set confirm 1
        append confirmQuery "\nNo correctors chosen."
    } 
   
    if {$confirm &&
        [APSMultipleChoice [APSUniqueName .] -question "Possible problem: $confirmQuery" \
           -labelList {Continue Cancel} -returnList {1 0}]==0} {
        return
    }
    set fileList ""
    if $corCount(h) {
	APSWriteSRConfig -rootname hCorr -filename $outputFile.h -updateDatabase 0\
	    -nameTypeList HCorrector -PVTypeList Regular -description $writeDescription \
	    -suffixLists [list $hcorrItemList]
	lappend fileList $outputFile.h
    }
    if $corCount(v) {
	APSWriteSRConfig -rootname vCorr -filename $outputFile.v \
	-nameTypeList VCorrector -PVTypeList Regular -description $writeDescription \
	    -suffixLists [list $vcorrItemList] -updateDatabase 0
	lappend fileList $outputFile.v
    }
    if {$corCount(h) && $coCount(v) } {
	if [catch {eval exec sddscombine $fileList $outputFile -over } result] {
	    return -code error "Error combine files: $result"
	} 
    } else {
	exec cp $fileList $outputFile
    }
    return
    
}

proc MakeReadConfigurationFrame {widget args} {
    set parent ""
    set plane h
    APSParseArguments {parent plane}

    set rootname ${plane}Corr
    global sconfigReadFile sconfigReadDescription sconfigFilterEnd1
    global ${rootname}Lattice ${rootname}MainDir 
    global ${rootname}Plane ${rootname}Readdir ${rootname}ReadDescription

    set ${rootname}Plane $plane
    set ${rootname}MainDir /home/helios/oagData/sr/orbitControllaw/lattices
    set ${rootname}Lattice default
    set ${rootname}Readdir ${plane}.default
    set ${rootname}ReadDescription ""
    set ${rootname}FilterEnd1 .*

    APSFrame $widget -parent $parent -label "Read Configuration" \
      -contextHelp "Use this frame to read corrector configurations."
    set w0 $parent$widget.frame
    
    APSFileSelectWidget .file -parent $w0 -variable ${rootname}Readdir -label "Config: " \
      -pathVariableList [list ${rootname}MainDir ${rootname}Lattice] \
      -mode directory -filterVariableList \
      [list ${rootname}Plane ${rootname}FilterEnd1] -width 60 \
      -incrementButtons 1 -contextHelp \
      "Enter the name of the configuration file to read."
    
    APSLabeledOutput .descrip -parent $w0 -label "Description: " -width 70 \
	-textVariable ${rootname}ReadDescription -contextHelp \
      "Shows the description of the last-read configuration file."
    
    
    APSButton .io0 -parent $w0 -text Read(replace) -size small -command \
      "APSReadOrbitCorrectionConfig -rootname $rootname -nameTypeList Corrector -filename \$${rootname}MainDir/\$${rootname}Lattice/\$${rootname}Readdir/config -logic replace" \
      -contextHelp "Press to read data to replace the existing configuration, using the named configuration file."
    
    APSButton .io1 -parent $w0 -text Read(or) -size small -command \
      "APSReadOrbitCorrectionConfig -rootname $rootname -nameTypeList Corrector -filename \$${rootname}MainDir/\$${rootname}Lattice/\$${rootname}Readdir/config -logic or" \
      -contextHelp "Press to read data to or with the existing configuration, using the named configuration file."
    
    APSButton .io2 -parent $w0 -text Read(and) -size small -command \
      "APSReadOrbitCorrectionConfig -rootname $rootname  -nameTypeList Corrector -filename \$${rootname}MainDir/\$${rootname}Lattice/\$${rootname}Readdir/config -logic and" \
      -contextHelp "Press to read data to and with the existing configuration, using the named configuration file."
    
    APSButton .io3 -parent $w0 -text Read(not) -size small -command \
      "APSReadOrbitCorrectionConfig -rootname $rootname -nameTypeList Corrector -filename \$${rootname}MainDir/\$${rootname}Lattice/\$${rootname}Readdir/config -logic not" \
      -contextHelp "Press to read data specifying elements to turn off, using the named configuration file."
    
    APSButton .io4 -parent $w0 -text Read(own) -size small -command PickOwnFile \
      -contextHelp "Press to read data from the own file."
    
}

set delta_limit 0.1
proc MakeProceedConfigurationFrame {widget args} {
    set parent ""
    APSParseArguments {parent}
    
    global w1 stepNum pause h_delta v_delta 
    APSFrame $widget -parent $parent -label "Punch Down Procedure" \
      -contextHelp "Use this frame to proceed with correctors setup"
    
    set w1 $parent$widget.frame
    APSFrameGrid .procGrid -parent $w1 -xList {x1 x2} -yList {y1 y2 y3} 
    set stepNum 100
    APSLabeledEntry .step -parent $w1.procGrid.x1.y1 -label "Number of Steps: " \
      -width 10 -textVariable stepNum \
      -contextHelp "Input for a number of steps the reduction algorytm should be ruapsn"
        $w1.procGrid.x1.y1 configure -relief groove
    
    set pause 5 
    APSLabeledEntry .pause -parent $w1.procGrid.x1.y2 -label "Time Interval: " \
	-width 10 -textVariable pause \
	-contextHelp "Time pause to wait for an user action to abort a procedure"
    $w1.procGrid.x1.y2 configure -relief groove
    global deltaLimit
    APSLabeledEntry .limit -parent $w1.procGrid.x1.y3 -label "Delta limit:" \
	-width 10 -textVariable deltaLimit \
	-contextHelp "The maximum delta in Amps allowed."
    
    set h_delta 1
    APSLabeledEntry .hdelta -parent $w1.procGrid.x2.y1 -label "    Horizontal Corrector Delta: " \
	-width 10 -textVariable h_delta \
	-contextHelp "Value of a delta to use for horizontal corrector current reduction"
    $w1.procGrid.x2.y1 configure -relief groove

    set v_delta 1
    APSLabeledEntry .vdelta -parent $w1.procGrid.x2.y2 -label "    Vertical Corrector Delta: " \
      -width 10 -textVariable v_delta \
      -contextHelp "Value of a delta to use for vertical corrector current reduction"
    $w1.procGrid.x2.y2 configure -relief groove

    APSButton .run -parent $w1 -text "Run" -command ChangeMarkedCorrSetup \
      -contextHelp "Button to invoke the reduction process" 
    APSButton .abort -parent $w1 -text "Abort" -command {set abortPressed 1} \
      -contextHelp "Button to stop the reduction process" 
    APSButton .reset -parent $w1 -text "Restore" -command RestoreSelectedCorrectors \
      -contextHelp "Button to restore initial corrector setpoints"
    APSButton .check -parent $w1 -text "Check" -command "MakeListOfCorrectors nonzero" \
	-contextHelp "Button to invoke procedure which makes a display of nonzero correctors."

    APSFrame .but -parent $w1 
    global w2
    set w2 $w1.but.frame
    APSButton .turnOff -parent $w2 -text "Turn Off" -command "MakeListOfCorrectors zero" \
      -contextHelp "Button to invoke procedure which is turning all marked zero correctors off."
    APSButton .turnOn -parent $w2 -text "Turn On" -command "TurnOnAllOffListedCorrectors" \
      -contextHelp "Button to invoke procedure which is turning all marked correctors on."
    APSButton .enable -parent $w2 -text "Enable" -command "setOpenCloseOnOffButs 1" \
      -contextHelp "Enables \"Turn On\" and \"Turn Off\" buttons."
    APSButton .disable -parent $w2 -text "Disable" -command "setOpenCloseOnOffButs 0" \
      -contextHelp "Disables \"Turn On\" and \"Turn Off\" buttons."
    update
    setOpenCloseResetBut 0
    setOpenCloseAbortBut 1
    setOpenCloseOnOffButs 0
}

proc MakeWriteConfigurationFrame {widget args} {
    set parent ""
    APSParseArguments {parent}
    
    global outputFile writeDescription
    APSFrame $widget -parent $parent -label "Write Configuration" \
      -contextHelp "Use this frame to write corrector configurations."
    
    set w2 $parent$widget.frame
    set outputFile [pwd]
    APSLabeledEntry .file -parent $w2 -label "Output File: " -width 76 \
      -textVariable outputFile -contextHelp \
      "Output file used for the configuration to be written into."
    
    APSLabeledEntry .descrip -parent $w2 -label "Description: " -width 76 \
      -textVariable writeDescription -contextHelp \
      "Use to enter a description for the configuration to be written."
    set writeDescription ""
    APSButton .io -parent $w2 -text Write -size small -command WriteFile \
      -contextHelp "Press to write data, using the named configuration file."
}

set hcorrItemList {A:FH1 A:H1 A:FH2 A:H7 B:H7 B:FH2 B:H1 B:FH1 A:Q4T B:Q4T A:Q5T B:Q5T A:M3T B:M3T A:Q8T B:Q8T A:M4T}
set vcorrItemList {A:FV1 A:V1 A:FV2 A:V7 A:V8 B:V8 B:V7 B:FV2 B:V1 B:FV1}

set deltaLimit 0.5
set cbOption "-side top"

set missingList ""
set missingListVar ""
set widgetList [APSTabFrame .but -parent .userFrame -label "Correction Selection" \
		    -labelList "Horizontal Vertical" -width 1050 -height 500 ]
set hparent [lindex $widgetList 0]
set vparent [lindex $widgetList 1]
APSSRCorrectorCheckButtons .shbut -parent $hparent -rootname hCorr -plane h \
  -sectorControl 1 \
  -orientation $buttonOrientation -packOption $cbOption
MakeReadConfigurationFrame .read -parent [lindex $widgetList 0] -plane h

APSSRCorrectorCheckButtons .svbut -parent $vparent -rootname vCorr -plane v \
  -sectorControl 1 \
  -orientation $buttonOrientation -packOption $cbOption
MakeReadConfigurationFrame .read -parent [lindex $widgetList 1] -plane v

set widgetList [APSTabFrame .action -parent .userFrame -label "Action" -labelList "Proceed Write" \
		       -width 1050 -height 200]

MakeProceedConfigurationFrame .proceed -parent [lindex $widgetList 0]
MakeWriteConfigurationFrame .write -parent [lindex $widgetList 1]


# necessary in case all correctors were zero when punch down started.
set nonzeroCount 0
set nonzeroCountH 0
set nonzeroCountV 0
