
proc APSMpBTSTurnOffPSInfo {} {
    return "For information about common safeguards and check please refer to OPSDoc_Boo_PEM006 under required reading.

This machine procedure is used to turn off selected BTS power supplies.  The operator must select the desired supplies to turn off.
"
}


proc APSMpBTSTurnOffPS {args} {
    set arrayName APSMpBTSControlPS
    set configured 0
    set turnOffList ""
    set turnOffListDetailed ""
    
    APSStrictParseArguments {arrayName configured turnOffList turnOffListDetailed}
    global status mpInterface execMode $arrayName
    if {![info exists $arrayName] || !$configured} {
        APSMpGenericPSArraySetup -arrayName $arrayName \
          -turnOffList $turnOffList \
          -turnOffListDetailed $turnOffListDetailed \
          -system booster
    }
    if {[llength $turnOffList] == 0} {
        APSMpStep init -promptDialog \
          "APSMpGenericPSDialog -packProc APSMpBTSTurnOffPSPackProc -turnOff 1 -arrayName $arrayName -system Booster -frame "
        set turnOffList [APSMpGenericPSArrayQuery -arrayName $arrayName -operation TurnOff]
    }
    
    set paramString ""
    foreach param {turnOffList} {
        append paramString "$param=\"[set $param]\" "
    }

    catch {APSLogScriptAction \
             -procedure APSMpBTSTurnOffPS \
             -action initialize \
             -state ok \
             -parameters $paramString}

    set foundallbooster 1
    foreach sector "BTS:AH BTS:AV BTS:AQ" {
        if {[lsearch $turnOffList $sector] == -1} {
            set foundallbooster 0
            break
        }
        set detailedDeviceList [APSMpConvertDeviceNames \
                                  -operation TurnOff \
                                  -deviceList $sector \
                                  -system booster]
        foreach detailedDevice $detailedDeviceList {
            if {[set ${arrayName}(TurnOffDetailed,$detailedDevice)] == 0} {
                set foundallbooster 0
                break
            }
        }
    }

    set foundallsr 1
    foreach sector "BTS:BH BTS:BV BTS:BQ BTS:BSQ BTS:CQ BTS:DQ" {
        if {[lsearch $turnOffList $sector] == -1} {
            set foundallsr 0
            break
        }
        set detailedDeviceList [APSMpConvertDeviceNames \
                                  -operation TurnOff \
                                  -deviceList $sector \
                                  -system booster]
        foreach detailedDevice $detailedDeviceList {
            if {[set ${arrayName}(TurnOffDetailed,$detailedDevice)] == 0} {
                set foundallsr 0
                break
            }
        }
    }

    APSMpStep "Set power supplies to 0 amps"

    set deviceList [APSMpGenericPSArrayQuery -arrayName $arrayName -operation TurnOff]
    
    set deviceList [APSMpConvertDeviceNames -operation TurnOff -deviceList $deviceList -system booster]

    set deviceList [APSMpConvertDetailedDeviceNames -operation TurnOff -deviceList $deviceList -arrayName $arrayName]
    
    set ab 0
    set bx 0
    set deviceListModified $deviceList
    APSMpStep "Checking if BTS:AB of BTS:BX are in the process of doing a decay conditioning"
    set i [lsearch -exact $deviceListModified "BTS:AB"]
    if {$i != -1} {
        if [catch {exec cavget -list=BTS:AB:PS:StandardizeDecayC -num -printErrors} state] {
            APSMpReturn error "APSMpBTSTurnOffPS: Error reading BTS:AB:PS:StandardizeDecayC $state"
        }
        if {$state} {
            APSMpReturn error "APSMpBTSTurnOffPS: Previous BTS:AB conditioning is still ongoing"
        }
        if [catch {exec cavget -list=BTS:DipoleSwitchingC -num -printErrors} state] {
            APSMpReturn error "APSMpBTSTurnOffPS: Error reading BTS:DipoleSwitchingC $state"
        }
        if {$state} {
            APSMpReturn error "APSMpBTSTurnOffPS: Dipole switching is still ongoing"
        }
        set deviceListModified [lreplace $deviceListModified $i $i]
        set ab 1
    }
    set i [lsearch -exact $deviceListModified "BTS:BX"]
    if {$i != -1} {
        if [catch {exec cavget -list=BTS:BX:PS:StandardizeDecayC -num -printErrors} state] {
            APSMpReturn error "APSMpBTSTurnOffPS: Error reading BTS:BX:PS:StandardizeDecayC $state"
        }
        if {$state} {
            APSMpReturn error "APSMpBTSTurnOffPS: Previous BTS:BX conditioning is still ongoing"
        }
        if [catch {exec cavget -list=BTS:DipoleSwitchingC -num -printErrors} state] {
            APSMpReturn error "APSMpBTSTurnOffPS: Error reading BTS:DipoleSwitchingC $state"
        }
        if {$state} {
            APSMpReturn error "APSMpBTSTurnOffPS: Dipole switching is still ongoing"
        }
        set deviceListModified [lreplace $deviceListModified $i $i]
        set bx 1
    }
    if [catch {APSDevSend -releaseLinks 1 \
                 -group PSOps \
                 -operation SetCurrent \
                 -deviceList $deviceListModified \
                 -value 0} result] {
        APSMpReturn error "APSMpBTSTurnOffPS: $result"
    }

    global abStatus bxStatus
    set abStatus ""
    set bxStatus ""

    if {$ab} {
        package require BLT
        if {[catch {exec cavget -list=BTS:AB:StandardizeC -list=.B,.C} ABValues]} {
            APSMpReturn error "APSMpBTSTurnOffPS: $ABValues"
        }
        if {[catch {exec cavput -list=BTS:AB:PS:StandardizeDecayC=1} results]} {
            APSMpReturn error "APSMpBTSTurnOffPS: $results"
        }
        blt::bgexec abStatus \
          standardizePVwithDecayCL -pv "BTS:AB:PS:SetCurrentC" \
          -lowerLimit [lindex $ABValues 0] -upperLimit [lindex $ABValues 1]  \
          -numSteps 240 -stepInterval .5 -numCycles 0 \
          -finalSetpoint 0 &
    }
    if {$bx} {
        package require BLT
        if {[catch {exec cavget -list=BTS:BX:StandardizeC -list=.B,.C} BXValues]} {
            APSMpReturn error "APSMpBTSTurnOffPS: $BXValues"
        }
        if {[catch {exec cavput -list=BTS:BX:PS:StandardizeDecayC=1} results]} {
            APSMpReturn error "APSMpBTSTurnOffPS: $results"
        }
        blt::bgexec bxStatus \
          standardizePVwithDecayCL -pv "BTS:BX:PS:SetCurrentC" \
          -lowerLimit [lindex $BXValues 0] -upperLimit [lindex $BXValues 1]  \
          -numSteps 240 -stepInterval .5 -numCycles 0 \
          -finalSetpoint 0 &
    }

    set tries 10
    APSMpStep "Waiting for currents to get below 1amp"
    while {$tries} {
        APSWaitWithUpdate -waitSeconds 3
        if [catch {APSDevSend -releaseLinks 1 \
                     -group PSOps \
                     -operation ReadCurrent \
                     -deviceList $deviceList} result] {
            APSMpReturn error "APSMpBTSTurnOffPS: $result"
        }
        set pass 1
        foreach value $result {
            if {$value > 1} {
                set pass 0
                break
            }
        }
        if {$pass} {
            set tries 0
        } else {
            incr tries -1
        }
    }


    if {$ab} { 
        if {$abStatus == ""} {
            tkwait variable abStatus
        }
    }
    if {$bx} { 
        if {$bxStatus == ""} {
            tkwait variable bxStatus
        }
    }
    if {$ab} { 
        if {[catch {exec cavput -list=BTS:AB:PS:StandardizeDecayC=0} results]} {
            APSMpReturn error "APSMpBTSTurnOffPS: $results"
        }
    }
    if {$bx} { 
        if {[catch {exec cavput -list=BTS:BX:PS:StandardizeDecayC=0} results]} {
            APSMpReturn error "APSMpBTSTurnOffPS: $results"
        }
    }

    APSMpStep "Turn off power supplies"

    set turnOffList [APSMpGenericPSArrayQuery -arrayName $arrayName -operation TurnOff]
    
    set turnOffList [APSMpConvertDeviceNames \
                      -operation TurnOff \
                      -deviceList $turnOffList \
                      -system booster]
    set turnOffList [APSMpConvertDetailedDeviceNames \
                      -operation TurnOff \
                      -deviceList $turnOffList \
                      -arrayName $arrayName]
    APSMpStep "turnOffList : $turnOffList"
    
    foreach device $turnOffList {
        if [catch {APSDevSend -releaseLinks 1 \
                     -timeout 10 \
                     -group PSOps \
                     -operation TurnOff \
                     -deviceList $device} result] {
            APSMpReturn error "APSMpBTSTurnOffPS: $result"
        }
    }
    APSWaitWithUpdate -waitSeconds 10

    APSMpStep "Turn off raw power supplies"
    if {$foundallbooster} {
        if [catch {APSDevSend -releaseLinks 1 \
                     -group PSOps \
                     -operation TurnOff \
                     -deviceList BTS:RBO} result] {
            APSMpReturn error "APSMpBTSTurnOffPS: $result"
        }
    }
    if {$foundallsr} {
        if [catch {APSDevSend -releaseLinks 1 \
                     -group PSOps \
                     -operation TurnOff \
                     -deviceList BTS:RSR} result] {
            APSMpReturn error "APSMpBTSTurnOffPS: $result"
        }
        if [catch {APSDevSend -releaseLinks 1 \
                     -group PSOps \
                     -operation TurnOff \
                     -deviceList BTS:DC1} result] {
            APSMpReturn error "APSMpBTSTurnOffPS: $result"
        }
        if [catch {APSDevSend -releaseLinks 1 \
                     -group PSOps \
                     -operation TurnOff \
                     -deviceList BTS:DC2} result] {
            APSMpReturn error "APSMpBTSTurnOffPS: $result"
        }
    }

    APSMpReturn ok
}
proc APSMpBTSTurnOffPSPackProc {frame} {
    global APSMpBTSControlPS
    button $frame.booster -text "Booster Side Only" -command "set APSMpBTSControlPS(TurnOff,BTS:AH) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AH1) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AH2) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AH3) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:AV) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AV1) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AV2) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AV3) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:AQ) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AQ1) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AQ2) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AQ3) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AQ4) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AQ5) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:AB) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AB) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BX) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BX) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BB1) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BB1) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:CB1) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CB1) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BH) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BH1) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BH2) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BV) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BV1) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BV2) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BV3) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BQ) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BQ1) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BQ2) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BQ3) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BQ4) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BQ5) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BSQ) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BSQH) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BSQV) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ1) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ2) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ3) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ4) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ5) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ6) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ1H) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ2V) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ5H) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ6V) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:CQ) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:CQH) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:CQV) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CQ1) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CQ2) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CQ3) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CQ1H) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CQ2V) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CQ3H) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:DQ) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:DQH) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:DQV) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:DQ1) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:DQ2) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:DQ1H) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:DQ2V) 0"

    pack $frame.booster
    button $frame.sr -text "  SR Side Only   " -command "set APSMpBTSControlPS(TurnOff,BTS:AH) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AH1) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AH2) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AH3) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:AV) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AV1) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AV2) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AV3) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:AQ) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AQ1) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AQ2) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AQ3) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AQ4) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AQ5) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:AB) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:AB) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BX) 0 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BX) 0 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BB1) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BB1) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:CB1) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CB1) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BH) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BH1) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BH2) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BV) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BV1) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BV2) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BV3) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BQ) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BQ1) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BQ2) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BQ3) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BQ4) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BQ5) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BSQ) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BSQH) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:BSQV) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ1) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ2) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ3) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ4) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ5) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ6) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ1H) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ2V) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ5H) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:BSQ6V) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:CQ) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:CQH) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:CQV) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CQ1) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CQ2) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CQ3) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CQ1H) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CQ2V) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:CQ3H) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:DQ) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:CQH) 1 ; \
      set APSMpBTSControlPS(TurnOff,BTS:CQV) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:DQ1) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:DQ2) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:DQ1H) 1 ; \
      set APSMpBTSControlPS(TurnOffDetailed,BTS:DQ2V) 1"
    pack $frame.sr
}

proc APSMpBTSTurnOnPSInfo {} {
    return "For information about common safeguards and check please refer to OPSDoc_Boo_PEM006 under required reading.

This machine procedure is used to turn on selected BTS power supplies.  The operator must select the desired supplies to turn on.
"
}

proc APSMpBTSTurnOnPS {args} {
    set arrayName APSMpBTSControlPS
    set configured 0
    set turnOnList ""
    set turnOnListDetailed ""
    APSStrictParseArguments {arrayName configured turnOnList turnOnListDetailed}

    global status mpInterface execMode $arrayName
    if {![info exists $arrayName] || !$configured} {
        APSMpGenericPSArraySetup -arrayName $arrayName \
          -turnOnList $turnOnList \
          -turnOnListDetailed $turnOnListDetailed \
          -system booster
    }
    if {[llength $turnOnList] == 0} {
        APSMpStep init -promptDialog \
          "APSMpGenericPSDialog -packProc APSMpBTSTurnOnPSPackProc -turnOn 1 -arrayName $arrayName -system Booster -frame "
        set turnOnList [APSMpGenericPSArrayQuery -arrayName $arrayName -operation TurnOn]
    }
    
    set paramString ""
    foreach param {turnOnList} {
        append paramString "$param=\"[set $param]\" "
    }
    catch {APSLogScriptAction \
             -procedure APSMpBTSTurnOnPS \
             -action initialize \
             -state ok \
             -parameters $paramString}

    foreach sector "BTS:AH BTS:AV BTS:AQ BTS:BH BTS:BV BTS:BQ BTS:BSQ BTS:CQ BTS:DQ" {
        if {([lsearch $turnOnList $sector] != -1) || $configured} {
            APSMpStep "Turn on raw power supplies"

            if [catch {APSDevSend -releaseLinks 1 \
                         -group PSOps \
                         -operation QueryOn \
                         -deviceList BTS:RSR} result] {
                APSMpReturn error "APSMpBTSTurnOnPS: $result"
            }
            if {!$result} {
                if [catch {APSDevSend -releaseLinks 1 \
                             -group PSOps \
                             -operation SetVoltage \
                             -value 0.0 \
                             -deviceList BTS:RSR} result] {
                    APSMpReturn error "APSMpBTSTurnOnPS: $result"
                }
                if [catch {APSDevSend -releaseLinks 1 \
                             -group PSOps \
                             -operation TurnOn \
                             -deviceList BTS:RSR} result] {
                    APSMpReturn error "APSMpBTSTurnOnPS: $result"
                }
            }
            if [catch {APSDevSend -releaseLinks 1 \
                         -group PSOps \
                         -operation QueryOn \
                         -deviceList BTS:RBO} result] {
                APSMpReturn error "APSMpBTSTurnOnPS: $result"
            }
            if {!$result} {
                if [catch {APSDevSend -releaseLinks 1 \
                             -group PSOps \
                             -operation SetVoltage \
                             -value 0.0 \
                             -deviceList BTS:RBO} result] {
                    APSMpReturn error "APSMpBTSTurnOnPS: $result"
                }
                if [catch {APSDevSend -releaseLinks 1 \
                             -group PSOps \
                             -operation TurnOn \
                             -deviceList BTS:RBO} result] {
                    APSMpReturn error "APSMpBTSTurnOnPS: $result"
                }
            }
            while {1} {
                if [catch {APSDevSend -releaseLinks 1 \
                             -group PSOps \
                             -operation QueryOn \
                             -deviceList BTS:DC1} result] {
                    APSMpReturn error "APSMpBTSTurnOnPS: $result"
                }
                if {!$result} {
                    if [catch {APSDevSend -releaseLinks 1 \
                                 -group PSOps \
                                 -operation TurnOn \
                                 -deviceList BTS:DC1} result] {
                        APSMpReturn error "APSMpBTSTurnOnPS: $result"
                    }
                }
                if [catch {APSDevSend -releaseLinks 1 \
                             -group PSOps \
                             -operation QueryOn \
                             -deviceList BTS:DC2} result] {
                    APSMpReturn error "APSMpBTSTurnOnPS: $result"
                }
                if {!$result} {
                    if [catch {APSDevSend -releaseLinks 1 \
                                 -group PSOps \
                                 -operation TurnOn \
                                 -deviceList BTS:DC2} result] {
                        APSMpReturn error "APSMpBTSTurnOnPS: $result"
                    }
                }
                APSWaitWithUpdate -waitSeconds 3
                
                if [catch {APSDevSend -releaseLinks 1 \
                             -group PSOps \
                             -operation QueryOn \
                             -deviceList "BTS:DC1 BTS:DC2"} result] {
                    APSMpReturn error "APSMpBTSTurnOnPS: $result"
                }
                if {![lindex $result 0] || ![lindex $result 1]} {
                    set answer [APSPEMTKDialog .warningDialog -text "APSMpBTSTurnOnPS: One or more of the BTS Raw supplies failed to turn on" -strings {Continue Retry "Abort Procedure"}]
                    if {$answer == 0} {
                        break
                    } elseif {$answer == 2} {
                        APSMpReturn error "APSMpBTSTurnOnPS: One or more of the BTS Raw supplies failed to turn on"
                    }
                } else {
                    break
                }
            }

            
            if [catch {APSDevSend -releaseLinks 1 \
                         -group PSOps \
                         -operation SetVoltage \
                         -value 65.0 \
                         -deviceList BTS:RSR} result] {
                APSMpReturn error "APSMpBTSTurnOnPS: $result"
            }
            if [catch {APSDevSend -releaseLinks 1 \
                         -group PSOps \
                         -operation SetVoltage \
                         -value 65.0 \
                         -deviceList BTS:RBO} result] {
                APSMpReturn error "APSMpBTSTurnOnPS: $result"
            }
            while {1} {
                if {[catch {exec cavput -list=BTS:DC1:PS:CVModeC.PROC=1,BTS:DC2:PS:CVModeC.PROC=1} result]} {
                    APSMpReturn error "APSMpBTSTurnOnPS: $result"
                }
                APSWaitWithUpdate -waitSeconds 1
                if [catch {APSDevSend -releaseLinks 1 \
                             -group PSOps \
                             -operation SetVoltage \
                             -value 55.0 \
                             -deviceList BTS:DC1} result] {
                    APSMpReturn error "APSMpBTSTurnOnPS: $result"
                }
                if [catch {APSDevSend -releaseLinks 1 \
                             -group PSOps \
                             -operation SetVoltage \
                             -value 55.0 \
                             -deviceList BTS:DC2} result] {
                    APSMpReturn error "APSMpBTSTurnOnPS: $result"
                }
                APSWaitWithUpdate -waitSeconds 1
                if {[catch {exec cavget -list=BTS:DC1:PS:CVModeM,BTS:DC2:PS:CVModeM -printErrors} result]} {
                    APSMpReturn error "APSMpBTSTurnOnPS: $result"
                }
                if {([lindex $result 0] != "On") || ([lindex $result 1] != "On")} {
                    set answer [APSPEMTKDialog .warningDialog -text "APSMpBTSTurnOnPS: One or more of the BTS Raw supplies is not in Constant Voltage Mode" -strings {Continue Retry "Abort Procedure"}]
                    if {$answer == 1} {
                        continue
                    } elseif {$answer == 2} {
                        APSMpReturn error "APSMpBTSTurnOnPS: One or more of the BTS Raw supplies is not in Constant Voltage Mode"
                    }
                }
                if [catch {APSDevSend -releaseLinks 1 \
                             -group PSOps \
                             -operation ReadVoltage \
                             -deviceList "BTS:DC1 BTS:DC2"} result] {
                    APSMpReturn error "APSMpBTSTurnOnPS: $result"
                }
                if {([expr abs([lindex $result 0] - 55.0)] > .05) || ([expr abs([lindex $result 1] - 55.0)] > .05)} {
                    set answer [APSPEMTKDialog .warningDialog -text "APSMpBTSTurnOnPS: One or more of the BTS Raw supplies readback voltage does not match the setpoint within .05" -strings {Continue Retry "Abort Procedure"}]
                    if {$answer == 1} {
                        continue
                    } elseif {$answer == 2} {
                        APSMpReturn error "APSMpBTSTurnOnPS: One or more of the BTS Raw supplies readback voltage does not match the setpoint within .05"
                    }
                }
                break
            }
            
            break
        }
    }
    APSMpStep "Turn on power supplies"

    set turnOnList [APSMpGenericPSArrayQuery -arrayName $arrayName -operation TurnOn]
    
    set turnOnList [APSMpConvertDeviceNames \
                      -operation TurnOn \
                      -deviceList $turnOnList \
                      -system booster]
    set turnOnList [APSMpConvertDetailedDeviceNames \
                      -operation TurnOn \
                      -deviceList $turnOnList \
                      -arrayName $arrayName]
    APSMpStep "turnOnList : $turnOnList"
    
    if [catch {APSDevSend -releaseLinks 1 \
                 -timeout 10 \
                 -group PSOps \
                 -operation QueryOn \
                 -deviceList $turnOnList} result] {
        APSMpReturn error "APSMpBTSTurnOnPS: $result"
    }
    
    set tol ""
    foreach item $result device $turnOnList {
        if {!$item} {
            lappend tol $device
        }
    }
    if {[llength $tol]} {
        #If this fails, maybe we need to add a reset step

        foreach device $tol {
            if {($device == "BTS:AQ1") || \
                  ($device == "BTS:AQ2") || \
                  ($device == "BTS:AQ3") || \
                  ($device == "BTS:AQ4") || \
                  ($device == "BTS:AQ5") || \
                  ($device == "BTS:BQ1") || \
                  ($device == "BTS:BQ2") || \
                  ($device == "BTS:BQ3") || \
                  ($device == "BTS:BQ4") || \
                  ($device == "BTS:BQ5") || \
                  ($device == "BTS:CQ1") || \
                  ($device == "BTS:CQ2") || \
                  ($device == "BTS:CQ3") || \
                  ($device == "BTS:DQ1") || \
                  ($device == "BTS:DQ2") || \
                  ($device == "BTS:BSQ1") || \
                  ($device == "BTS:BSQ2") || \
                  ($device == "BTS:BSQ3") || \
                  ($device == "BTS:BSQ4") || \
                  ($device == "BTS:BSQ5") || \
                  ($device == "BTS:BSQ6") || \
                  ($device == "BTS:BSQ1H") || \
                  ($device == "BTS:BSQ2V") || \
                  ($device == "BTS:BSQ5H") || \
                  ($device == "BTS:BSQ6V") || \
                  ($device == "BTS:CQ1H") || \
                  ($device == "BTS:CQ2V") || \
                  ($device == "BTS:CQ3H") || \
                  ($device == "BTS:DQ1H") || \
                  ($device == "BTS:DQ2V") || \
                  ($device == "BTS:BB1") || \
                  ($device == "BTS:CB1")} {
                catch {APScavput -list=${device}:PS:ResetSupplyC.PROC=1 -pend=2}
            }
        }
        after 1000
       foreach device $tol {
            if [catch {APSDevSend -releaseLinks 1 \
                         -timeout 10 \
                         -group PSOps \
                         -operation TurnOn \
                         -deviceList $device} result] {
                APSMpReturn error "APSMpBTSTurnOnPS: $result"
            }
            #Pause .01 seconds inbetween magnets
            after 10
        }
        APSWaitWithUpdate -waitSeconds 10
        if [catch {APSDevSend -releaseLinks 1 \
                     -timeout 10 \
                     -group PSOps \
                     -operation SetCurrent \
                     -forceRead 1 \
                     -deviceList $tol} results] {
            APSMpReturn error "APSMpBTSTurnOnPS: $results"
        }
        if [catch {APSDevSend -releaseLinks 1 \
                     -timeout 10 \
                     -group PSOps \
                     -operation SetCurrent \
                     -valueList $results \
                     -deviceList $tol} result] {
            APSMpReturn error "APSMpBTSTurnOnPS: $result"
        }
    }
    APSMpReturn ok
}

proc APSMpBTSTurnOnPSPackProc {frame} {
    global APSMpBTSControlPS
    button $frame.booster -text "Booster Side Only" -command "set APSMpBTSControlPS(TurnOn,BTS:AH) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AH1) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AH2) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AH3) 1 ; \
      set APSMpBTSControlPS(TurnOn,BTS:AV) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AV1) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AV2) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AV3) 1 ; \
      set APSMpBTSControlPS(TurnOn,BTS:AQ) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AQ1) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AQ2) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AQ3) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AQ4) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AQ5) 1 ; \
      set APSMpBTSControlPS(TurnOn,BTS:AB) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AB) 1 ; \
      set APSMpBTSControlPS(TurnOn,BTS:BX) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BX) 1 ; \
      set APSMpBTSControlPS(TurnOn,BTS:BB1) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BB1) 0 ; \
      set APSMpBTSControlPS(TurnOn,BTS:CB1) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CB1) 0 ; \
      set APSMpBTSControlPS(TurnOn,BTS:BH) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BH1) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BH2) 0 ; \
      set APSMpBTSControlPS(TurnOn,BTS:BV) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BV1) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BV2) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BV3) 0 ; \
      set APSMpBTSControlPS(TurnOn,BTS:BQ) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BQ1) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BQ2) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BQ3) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BQ4) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BQ5) 0 ; \
      set APSMpBTSControlPS(TurnOn,BTS:BSQ) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ1) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ2) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ3) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ4) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ5) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ6) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ1H) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ2V) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ5H) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ6V) 0 ; \
      set APSMpBTSControlPS(TurnOn,BTS:CQ) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CQ1) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CQ2) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CQ3) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CQ1H) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CQ2V) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CQ3H) 0 ; \
      set APSMpBTSControlPS(TurnOn,BTS:DQ) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:DQ1) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:DQ2) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:DQ1H) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:DQ2V) 0"

    pack $frame.booster
    button $frame.sr -text "  SR Side Only   " -command "set APSMpBTSControlPS(TurnOn,BTS:AH) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AH1) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AH2) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AH3) 0 ; \
      set APSMpBTSControlPS(TurnOn,BTS:AV) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AV1) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AV2) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AV3) 0 ; \
      set APSMpBTSControlPS(TurnOn,BTS:AQ) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AQ1) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AQ2) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AQ3) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AQ4) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AQ5) 0 ; \
      set APSMpBTSControlPS(TurnOn,BTS:AB) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:AB) 0 ; \
      set APSMpBTSControlPS(TurnOn,BTS:BX) 0 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BX) 0 ; \
      set APSMpBTSControlPS(TurnOn,BTS:BB1) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BB1) 1 ; \
      set APSMpBTSControlPS(TurnOn,BTS:CB1) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CB1) 1 ; \
      set APSMpBTSControlPS(TurnOn,BTS:BH) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BH1) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BH2) 1 ; \
      set APSMpBTSControlPS(TurnOn,BTS:BV) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BV1) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BV2) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BV3) 1 ; \
      set APSMpBTSControlPS(TurnOn,BTS:BQ) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BQ1) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BQ2) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BQ3) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BQ4) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BQ5) 1 ; \
      set APSMpBTSControlPS(TurnOn,BTS:BSQ) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ1) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ2) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ3) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ4) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ5) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ6) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ1H) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ2V) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ5H) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:BSQ6V) 1 ; \
      set APSMpBTSControlPS(TurnOn,BTS:CQ) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CQ1) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CQ2) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CQ3) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CQ1H) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CQ2V) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:CQ3H) 1 ; \
      set APSMpBTSControlPS(TurnOn,BTS:DQ) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:DQ1) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:DQ2) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:DQ1H) 1 ; \
      set APSMpBTSControlPS(TurnOnDetailed,BTS:DQ2V) 1"
    pack $frame.sr
}

proc APSMpBTSConditionPowerSuppliesInfo {} {
    return "For information about common safeguards and check please refer to OPSDoc_Boo_PEM006 under required reading.

This machine procedure is used to condition selected BTS power supplies.  In addition to selecting the power supplies to be conditioned, the PEMtool will ensure the power supplies are ON before starting the conditioning process. The operator must select an SCR file containing the magnet setpoints to be restored after conditioning is completed.  Typically the UBOP file will have magnet setpoints that will result in good storage ring injection efficiency (>70-80 %).
"
}

proc APSMpBTSConditionPowerSupplies {args} {
    set SCRFile ""
    set configured 0
    set conditionList ""
    set conditionListDetailed ""
    set turnOnList ""
    set turnOnListDetailed ""
    set conditionList ""
    set cycles 2
    set allSupplies 0

    APSStrictParseArguments {conditionList conditionListDetailed SCRFile configured cycles allSupplies}
    
    if {[llength $conditionList] == 0} {
        set conditionList [list BTS:AH BTS:AV BTS:AQ BTS:BH BTS:BV BTS:BQ BTS:BSQ BTS:CQ BTS:DQ BTS:AB BTS:BX BTS:BB1 BTS:CB1]
        set autoAdd 1
    }

    global status mpInterface execMode APSMpBTSControlPS
    set turnOnList $conditionList
    set turnOnListDetailed $conditionListDetailed
    set arrayName APSMpBTSControlPS
    if {![info exists $arrayName] || !$configured} {
        APSMpGenericPSArraySetup -arrayName $arrayName \
          -turnOnList $turnOnList \
          -turnOnListDetailed $turnOnListDetailed \
          -conditionList $conditionList \
          -conditionListDetailed $conditionListDetailed \
          -SCRFile $SCRFile \
          -cycles $cycles \
          -system booster
    }
    if {$allSupplies == 0} {
        set APSMpBTSControlPS(StartConditioning,BTS:AB) 0
        set APSMpBTSControlPS(StartConditioningDetailed,BTS:AB) 0
        set APSMpBTSControlPS(StartConditioning,BTS:BX) 0
        set APSMpBTSControlPS(StartConditioningDetailed,BTS:BX) 0
        set APSMpBTSControlPS(StartConditioning,BTS:BB1) 0
        set APSMpBTSControlPS(StartConditioningDetailed,BTS:BB1) 0
        set APSMpBTSControlPS(StartConditioning,BTS:CB1) 0
        set APSMpBTSControlPS(StartConditioningDetailed,BTS:CB1) 0
    }

    APSMpStep init -promptDialog \
      "APSLGenericPSDialog -packProc APSMpBTSConditionPSPackProc  -turnOn 0 -condition 1 -chooseSCR 1 -arrayName $arrayName -system Booster -frame "

    if {$SCRFile == ""} {
        set SCRFile $APSMpBTSControlPS(Filename)
    }
    set paramString ""
    foreach param {SCRFile} {
        append paramString "$param=\"[set $param]\" "
    }
    catch {APSLogScriptAction \
             -procedure APSMpBTSConditionPowerSupplies \
             -action initialize \
             -state ok \
             -parameters $paramString}


    if {[catch {exec sdds2stream $SCRFile -parameter=SnapshotDescription -page=1 -noquotes} results]} {
        APSMpReturn error "APSMpBTSConditionPowerSupplies: Invalid SCR file: $results"
    }    
    if {[string first "Limited save:" $results] != -1} {
        APSMpReturn error "APSMpBTSConditionPowerSupplies: LIMITED SAVE SCR files cannot be used for this procedure."
    }


    set conditionList [APSMpGenericPSArrayQuery \
                         -arrayName $arrayName \
                         -operation StartConditioning]
    set cld [APSMpConvertDeviceNames \
               -operation StartConditioning \
               -deviceList $conditionList \
               -system booster]
    set cld [APSMpConvertDetailedDeviceNames \
               -operation StartConditioning \
               -deviceList $cld \
               -arrayName $arrayName]

    if {![info exists $arrayName] || !$configured} {
        APSMpGenericPSArraySetup -arrayName $arrayName \
          -turnOnList $conditionList \
          -turnOnListDetailed $cld \
          -conditionList $conditionList \
          -conditionListDetailed $cld \
          -SCRFile $SCRFile \
          -cycles $cycles \
          -system booster
    }
    set conditionList [APSMpGenericPSArrayQuery \
                         -arrayName $arrayName \
                         -operation StartConditioning]
    set conditionList [APSMpConvertDeviceNames \
                         -operation StartConditioning \
                         -deviceList $conditionList \
                         -system booster]
    set conditionList [APSMpConvertDetailedDeviceNames \
                         -operation StartConditioning \
                         -deviceList $conditionList \
                         -arrayName $arrayName]
    
    set turnOnList $conditionList


    set conditionListModified $conditionList

    APSMpStep "Checking if BTS:AB or BTS:BX are in the process of doing a decay conditioning"
    set i [lsearch -exact $conditionListModified "BTS:AB"]
    if {$i != -1} {
        if [catch {exec cavget -list=BTS:AB:PS:StandardizeDecayC -num -printErrors} state] {
            APSMpReturn error "APSMpBTSConditionPowerSupplies: Error reading BTS:AB:PS:StandardizeDecayC $state"
        }
        if {$state} {
            if {[tk_dialog2 .warningDialog "Warning Message" \
                   "Previous BTS:AB conditioning may still ongoing\nSet BTS:AB:PS:StandardizeDecayC to 0 if you know it isn't\nFix then click Continue" \
                   warning -1 Continue "Abort Procedure"] == 1} {
                APSMpReturn error "APSMpBTSConditionPowerSupplies: User aborted"
            }
        }
        if [catch {exec cavget -list=BTS:DipoleSwitchingC -num -printErrors} state] {
            APSMpReturn error "APSMpBTSConditionPowerSupplies: Error reading BTS:DipoleSwitchingC $state"
        }
        if {$state} {
            APSMpReturn error "APSMpBTSConditionPowerSupplies: Dipole switching is still ongoing"
        }
    }
    set i [lsearch -exact $conditionListModified "BTS:BX"]
    if {$i != -1} {
        if [catch {exec cavget -list=BTS:BX:PS:StandardizeDecayC -num -printErrors} state] {
            APSMpReturn error "APSMpBTSConditionPowerSupplies: Error reading BTS:BX:PS:StandardizeDecayC $state"
        }
        if {$state} {
            if {[tk_dialog2 .warningDialog "Warning Message" \
                   "Previous BTS:BX conditioning may still ongoing\nSet BTS:BX:PS:StandardizeDecayC to 0 if you know it isn't\nFix then click Continue" \
                   warning -1 Continue "Abort Procedure"] == 1} {
                APSMpReturn error "APSMpBTSConditionPowerSupplies: User aborted"
            }
        }
        if [catch {exec cavget -list=BTS:DipoleSwitchingC -num -printErrors} state] {
            APSMpReturn error "APSMpBTSConditionPowerSupplies: Error reading BTS:DipoleSwitchingC $state"
        }
        if {$state} {
            APSMpReturn error "APSMpBTSConditionPowerSupplies: Dipole switching is still ongoing"
        }
    }

    APSMpStep "Stop Conditioning"
    if [catch {APSDevSend -cavput 1 \
                 -releaseLinks 1 \
                 -timeout 0 \
                 -group PSOps \
                 -operation StopConditioning \
                 -deviceList $conditionList} result] {
        APSMpReturn error "APSMpBTSConditionPowerSupplies: $result"
    }	

    APSMpStep "Turn on needed supplies" -proc APSMpLTurnOnPS
    if {[catch {APSMpBTSTurnOnPS -turnOnList $turnOnList -configured 1} result]} {
        APSMpReturn error "APSMpBTSConditionPowerSupplies: $result"	    
    }
    set pass 0
    while {1} {
        incr pass
        if [catch {APSDevSend -releaseLinks 1 \
                     -timeout 10 \
                     -group PSOps \
                     -operation QueryOn \
                     -deviceList $conditionList} result] {
            APSMpReturn error "APSMpBTSConditionPowerSupplies: $result"
        }
        set found 0
        foreach item $result device $conditionList {
            if {!$item} {
                set found 1
                if {$pass == 8} {
                    if {[tk_dialog2 .warningDialog "Warning Message" \
                           "$device magnets are not on\nFix then click Continue" \
                           warning -1 Continue "Abort Procedure"] == 1} {
                        APSMpReturn error "APSMpBTSConditionPowerSupplies: User aborted"
                    }
                }
            }
        }
        if {$found == 0} {
            break
        }
        APSWaitWithUpdate -waitSeconds 5	
    }    

    APSMpStep "Condition needed supplies" -proc APSMpBTSConditionPS
    if [catch {APSMpBTSConditionPS \
                 -arrayName $arrayName \
                 -configured 1 \
                 -SCRFile $APSMpBTSControlPS(Filename) } conditioning] {
        APSMpReturn error "APSMpBTSConditionPowerSupplies: $conditioning"
    }
    
    if $conditioning {
        APSMpBTSWaitForConditioningPS -deviceList $conditionList -cycles $cycles
    }
    
    set option ""
    set i 0
    foreach supply $conditionList {
        if {$i == 0} {
            append option "-match=column,ControlName=${supply}*"
            incr i
        } else {
            append option ",ControlName=${supply}*,|"
        }
    }
    
    if [string length $APSMpBTSControlPS(Filename)] {
        APSMpStep RestoringSCRValues 
        set tmpFile /tmp/[APSTmpString]
        
        if [catch {exec sddsprocess $APSMpBTSControlPS(Filename) $tmpFile \
                     -match=column,IsReadOnly=+n  -match=column,IsProtected=+y,! \
                     -match=column,Category=MainPS,Category=SteeringPS,| \
                     -match=col,ControlName=,! $option \
                     -match=column,ControlName=BTS:?B:CurrentAO,!} result] {
            catch {APSSendEMail -address soliday \
                     -subject "Problem restoring SCR file."  \
                     -message "Problem restoring SCR file\nAPSMpBTSConditionPowerSupplies\n$SCRFile \n$result"} 
            APSMpReturn error "Problem restoring SCR file: APSMpBTSConditionPowerSupplies: $SCRFile: $result"
        }
        if [catch {exec sddscasr -restore=verify $tmpFile} result] {
            catch {APSSendEMail -address soliday \
                     -subject "Problem restoring SCR file."  \
                     -message "Problem restoring SCR file\nAPSMpBTSConditionPowerSupplies\n$SCRFile \n$result"} 
            if {[tk_dialog2 .warningDialog "Warning Message" \
                   "Problem restoring SCR file\nAPSMpBTSConditionPowerSupplies\n$SCRFile \n$result\nFix then click Continue" \
                   warning -1 Continue "Abort Procedure"] == 1} {
                APSMpReturn error "APSMpBTSConditionPowerSupplies: User aborted"
            }
        }
    }
    
    APSMpReturn ok
}

proc APSMpBTSConditionPSPackProc {frame} {
    global APSMpBTSControlPS
    button $frame.booster -text "Booster Side Only" -command "set APSMpBTSControlPS(StartConditioning,BTS:AH) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AH1) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AH2) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AH3) 1 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:AV) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AV1) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AV2) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AV3) 1 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:AQ) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AQ1) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AQ2) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AQ3) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AQ4) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AQ5) 1 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:AB) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AB) 1 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:BX) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BX) 1 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:BB1) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BB1) 0 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:CB1) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CB1) 0 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:BH) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BH1) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BH2) 0 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:BV) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BV1) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BV2) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BV3) 0 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:BQ) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BQ1) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BQ2) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BQ3) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BQ4) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BQ5) 0 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:BSQ) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ1) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ2) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ3) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ4) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ5) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ6) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ1H) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ2V) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ5H) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ6V) 0 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:CQ) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CQ1) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CQ2) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CQ3) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CQ1H) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CQ2V) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CQ3H) 0 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:DQ) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:DQ1) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:DQ2) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:DQ1H) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:DQ2V) 0"

    pack $frame.booster
    button $frame.sr -text "  SR Side Only   " -command "set APSMpBTSControlPS(StartConditioning,BTS:AH) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AH1) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AH2) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AH3) 0 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:AV) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AV1) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AV2) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AV3) 0 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:AQ) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AQ1) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AQ2) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AQ3) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AQ4) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AQ5) 0 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:AB) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:AB) 0 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:BX) 0 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BX) 0 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:BB1) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BB1) 1 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:CB1) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CB1) 1 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:BH) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BH1) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BH2) 1 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:BV) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BV1) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BV2) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BV3) 1 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:BQ) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BQ1) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BQ2) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BQ3) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BQ4) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BQ5) 1 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:BSQ) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ1) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ2) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ3) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ4) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ5) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ6) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ1H) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ2V) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ5H) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:BSQ6V) 1 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:CQ) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CQ1) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CQ2) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CQ3) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CQ1H) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CQ2V) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:CQ3H) 1 ; \
      set APSMpBTSControlPS(StartConditioning,BTS:DQ) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:DQ1) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:DQ2) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:DQ1H) 1 ; \
      set APSMpBTSControlPS(StartConditioningDetailed,BTS:DQ2V) 1"
    pack $frame.sr
}

proc APSMpBTSConditionPS {args} {
    set arrayName APSMpBTSControlPS
    set configured 0
    set conditionList ""
    set conditionListDetailed ""
    set SCRFile ""
    APSStrictParseArguments {arrayName configured conditionList conditionListDetailed SCRFile}

    global $arrayName
    if {![info exists $arrayName] || !$configured} {
        APSMpGenericPSArraySetup -arrayName $arrayName \
          -conditionList $conditionList \
          -conditionListDetailed $conditionListDetailed \
          -system booster
    }

    APSMpStep init -promptDialog \
      "APSMpGenericPSDialog -condition 1 -arrayName $arrayName -system Booster -frame "
    set conditionList [APSMpGenericPSArrayQuery \
                         -arrayName $arrayName \
                         -operation StartConditioning]
    set conditionList [APSMpConvertDeviceNames \
                         -operation StartConditioning \
                         -deviceList $conditionList \
                         -system booster]
    set conditionList [APSMpConvertDetailedDeviceNames \
                         -operation StartConditioning \
                         -deviceList $conditionList \
                         -arrayName $arrayName]

    if ![llength $conditionList] {
        APSMpReturn ok 0
    }

    APSMpStep "Configure conditioning"
    set cycles [set ${arrayName}(StartConditioningInfo,cycles)]

#    if [catch {exec standardize -configure /home/helios/oagData/bts/magnetConditioning/standardize.config} result] {
#        APSMpReturn error "APSMpBTSConditionPS: $result"
#    }
    if [catch {exec standardize -configure /home/helios/oagData/bts/magnetConditioning/standardizeDwell.config} result] {
        APSMpReturn error "APSMpBTSConditionPS: $result"
    }
    if [catch {exec degauss -configure /home/helios/oagData/bts/magnetConditioning/degauss.config} result] {
        APSMpReturn error "APSMpBTSConditionPS: $result"
    }
    if [catch {exec degauss -configure /home/helios/oagData/bts/magnetConditioning/triDegauss.config} result] {
        APSMpReturn error "APSMpBTSConditionPS: $result"
    }
    if [catch {APSDevSend -releaseLinks 1 \
                 -group PSOps \
                 -operation SetConditioningCycles \
                 -deviceList $conditionList \
                 -value $cycles} result] {
        APSMpReturn error "APSMpBTSConditionPS: $result"
    }
    if {[string length $SCRFile] && [file exists $SCRFile]} {
        # extract the setpoints from the SCR file and use them as the 
        # conditioning endpoints
        APSMpStep "Configure conditioning from snapshot"

        set tmpRoot /tmp/[APSTmpString]
        if [catch {exec sddsprocess /home/helios/oagData/deviceConfig/booster/BTSOps.pvTable \
                     $tmpRoot.setpt -match=column,Operation=SetCurrent 
            exec sddsprocess /home/helios/oagData/deviceConfig/booster/BTSOps.pvTable \
                     $tmpRoot.setcond -match=column,Operation=SetConditioningEndpoint
            exec sddsxref -nowarning $SCRFile $tmpRoot.setpt -pipe=out \
                     -match=ControlName -take=DeviceName \
                     | sddsconvert -pipe -delete=column,ControlName \
                     | sddsxref -pipe=in $tmpRoot.setcond -nowarning \
                     $tmpRoot.snap -match=DeviceName -take=ControlName
            exec sddscasr -restore=verify $tmpRoot.snap} result] {
            catch {eval file delete -force $tmpRoot.setpt $tmpRoot.setcond $tmpRoot.snap}
            APSMpReturn error "APSMpBTSConditionPS: $result"
        }
        catch {eval file delete -force $tmpRoot.setpt $tmpRoot.setcond $tmpRoot.snap}
    }

    APSMpStep "Starting conditioning for [llength $conditionList] devices" -desc "Starting conditioning"

    set ab 0
    set bx 0
    set conditionListModified $conditionList
    APSMpStep "Checking if BTS:AB or BTS:BX is in the process of doing a decay conditioning"
    set i [lsearch -exact $conditionListModified "BTS:AB"]
    if {$i != -1} {
        if [catch {exec cavget -list=BTS:AB:PS:StandardizeDecayC -num -printErrors} state] {
            APSMpReturn error "APSMpBTSConditionPS: Error reading BTS:AB:PS:StandardizeDecayC $state"
        }
        if {$state} {
            if {[tk_dialog2 .warningDialog "Warning Message" \
                   "Previous BTS:AB conditioning may still ongoing\nSet BTS:AB:PS:StandardizeDecayC to 0 if you know it isn't\nFix then click Continue" \
                   warning -1 Continue "Abort Procedure"] == 1} {
                APSMpReturn error "APSMpBTSConditionPS: User aborted"
            }
        }
        if [catch {exec cavget -list=BTS:DipoleSwitchingC -num -printErrors} state] {
            APSMpReturn error "APSMpBTSConditionPS: Error reading BTS:DipoleSwitchingC $state"
        }
        if {$state} {
            APSMpReturn error "APSMpBTSConditionPS: Dipole switching is still ongoing"
        }
        set conditionListModified [lreplace $conditionListModified $i $i]
        set ab 1
    }
    set i [lsearch -exact $conditionListModified "BTS:BX"]
    if {$i != -1} {
        if [catch {exec cavget -list=BTS:BX:PS:StandardizeDecayC -num -printErrors} state] {
            APSMpReturn error "APSMpBTSConditionPS: Error reading BTS:BX:PS:StandardizeDecayC $state"
        }
        if {$state} {
            if {[tk_dialog2 .warningDialog "Warning Message" \
                   "Previous BTS:BX conditioning may still ongoing\nSet BTS:BX:PS:StandardizeDecayC to 0 if you know it isn't\nFix then click Continue" \
                   warning -1 Continue "Abort Procedure"] == 1} {
                APSMpReturn error "APSMpBTSConditionPS: User aborted"
            }
        }
        if [catch {exec cavget -list=BTS:DipoleSwitchingC -num -printErrors} state] {
            APSMpReturn error "APSMpBTSConditionPS: Error reading BTS:DipoleSwitchingC $state"
        }
        if {$state} {
            APSMpReturn error "APSMpBTSConditionPS: Dipole switching is still ongoing"
        }
        set conditionListModified [lreplace $conditionListModified $i $i]
        set bx 1
    }

    global abStatus bxStatus
    set abStatus ""

    if {$ab} {
        package require BLT
        if {[catch {exec cavget -list=BTS:AB:StandardizeC -list=.B,.C,.F,.G} ABValues]} {
            APSMpReturn error "APSMpBTSTurnOffPS: $ABValues"
        }
        if {[catch {exec cavput -list=BTS:AB:PS:StandardizeDecayC=1} results]} {
            APSMpReturn error "APSMpBTSConditionPS: $results"
        }
        blt::bgexec abStatus \
          standardizePVwithDecayCL -pv "BTS:AB:PS:SetCurrentC" \
          -lowerLimit [lindex $ABValues 0] -upperLimit [lindex $ABValues 1]  \
          -numSteps 240 -stepInterval .5 -numCycles [lindex $ABValues 2] \
          -finalSetpoint [lindex $ABValues 3] -approachFromUpperLimit 0 &
    }
    if {$bx} {
        package require BLT
        if {[catch {exec cavget -list=BTS:BX:StandardizeC -list=.B,.C,.F,.G} BXValues]} {
            APSMpReturn error "APSMpBTSTurnOffPS: $BXValues"
        }
        if {[catch {exec cavput -list=BTS:BX:PS:StandardizeDecayC=1} results]} {
            APSMpReturn error "APSMpBTSConditionPS: $results"
        }
        blt::bgexec bxStatus \
          standardizePVwithDecayCL -pv "BTS:BX:PS:SetCurrentC" \
          -lowerLimit [lindex $BXValues 0] -upperLimit [lindex $BXValues 1]  \
          -numSteps 240 -stepInterval .5 -numCycles [lindex $BXValues 2] \
          -finalSetpoint [lindex $BXValues 3] -approachFromUpperLimit 0 &
    }

    #Strip out trims here
    foreach quad "BTS:BSQ1 BTS:BSQ2 BTS:BSQ5 BTS:BSQ6 BTS:CQ1 BTS:CQ2 BTS:CQ3 BTS:DQ1 BTS:DQ2" \
      corr "BTS:BSQ1H BTS:BSQ2V BTS:BSQ5H BTS:BSQ6V BTS:CQ1H BTS:CQ2V BTS:CQ3H BTS:DQ1H BTS:DQ2V" {
          if {[lsearch -exact $conditionListModified $quad] != -1} {
              set i [lsearch -exact $conditionListModified $corr]
              if {$i >= 0} {
                  set conditionListModified [lreplace $conditionListModified $i $i]
              }
          }
      }

    if [catch {APSDevSend -releaseLinks 1 \
                 -group PSOps \
                 -operation StartConditioning \
                 -deviceList $conditionListModified \
                 -timeout 0} result] {
        APSMpReturn error "APSMpBTSConditionPS: $result"
    }

    APSMpStep "Waiting For Conditioning"
    if {$ab} { 
        if {$abStatus == ""} {
            tkwait variable abStatus
        }
    }
    if {$bx} { 
        if {$bxStatus == ""} {
            tkwait variable bxStatus
        }
    }
    if {$ab} { 
        if {[catch {exec cavput -list=BTS:AB:PS:StandardizeDecayC=0} results]} {
            APSMpReturn error "APSMpBTSConditionPS: $results"
        }
    }
    if {$bx} { 
        if {[catch {exec cavput -list=BTS:BX:PS:StandardizeDecayC=0} results]} {
            APSMpReturn error "APSMpBTSConditionPS: $results"
        }
    }

    APSMpReturn ok [llength $conditionList]
}

proc APSMpBTSWaitForConditioningPS {args} {
    set cycles 2
    set deviceList [APSDevSend -releaseLinks 1 \
                      -group PSOps \
                      -operation QueryConditioning]
    APSStrictParseArguments {deviceList cycles}

    set timeToStop [expr [clock seconds] + 600*${cycles}]
    set someConditioning 1
    APSMpStep "Waiting For Conditioning"
    set stepsTaken 0
    set query QueryConditioning
    while {($someConditioning) && ($timeToStop>[clock seconds])} {
        APSWaitWithUpdate -waitSeconds 10 -updateInterval 1
        set someConditioning 0
        incr stepsTaken
        if [catch {APSDevSend -releaseLinks 1 \
                     -timeout 10 \
                     -group PSOps \
                     -operation $query \
                     -deviceList $deviceList} result] {
            APSMpReturn error "APSMpBTSWaitForConditionPS: $result"
        }
        set someConditioning 0
        foreach item $result {
            incr someConditioning $item
        }
        if $someConditioning {
            APSMpStep "Waiting For Conditioning of $someConditioning ganged devices"
            APSWaitWithUpdate -waitSeconds 15 -updateInterval 1
            if $stepsTaken==5 {
                APSMpStep "You are getting sleepy..."
            }
            if $stepsTaken==15 {
                APSMpStep "You are getting very sleepy..."
            }
            if $stepsTaken==20 {
                APSMpStep "You are getting very, very sleepy..."
            }
        }
    }
    if $someConditioning {
        if {![APSQueryToProceed -message "Some supplies still conditioning.  What do you want to do?"]} {
            APSMpReturn error "Some supplies did not finish conditioning."
        }
    }
    APSMpReturn ok
}

proc APSMpBoosterDCRampStandardizeInitDialog {frame} {
    global APSMpBoosterDCRampStandardize
    
    set startVarList ""
    set endVarList ""
    set targetVarList ""
    set checkVarList ""
    foreach magnet {BM QF QD SF SD} start {0 0 0 0 0} end {0.4 0.4 0.4 0.4 0.4} {
        set APSMpBoosterDCRampStandardize($magnet) 1
        lappend startVarList APSMpBoosterDCRampStandardize(${magnet}rampStart)
        lappend endVarList APSMpBoosterDCRampStandardize(${magnet}rampEnd)
        lappend checkVarList APSMpBoosterDCRampStandardize($magnet)
        set APSMpBoosterDCRampStandardize(${magnet}rampStart) $start
        set APSMpBoosterDCRampStandardize(${magnet}rampEnd) $end
    }
    
    set APSMpBoosterDCRampStandardize(cycles) 1
    #2 minutes
    set APSMpBoosterDCRampStandardize(cycleTime) 60
    set APSMpBoosterDCRampStandardize(rampSteps) 5
    set APSMpBoosterDCRampStandardize(SCRFile) ""
    set APSMpBoosterDCRampStandardize(ShortFilename) ""
    
    set width 20
    APSLabel .label -parent $frame -text "                              BM                   QF                  QD                    SF                      SD"
    APSLabeledEntryFrame .start -parent $frame -label "Ramp Start:" -variableList $startVarList -width $width -orientation horizontal 
    APSLabeledEntryFrame .end -parent $frame -label "Ramp Stop:" -variableList $endVarList -width $width -orientation horizontal 
    #  APSLabeledEntryFrame .target -parent $frame -label "Target:" -variableList $targetVarList -width $width -orientation horizontal 
    
    set width 40
    APSLabeledEntry .cycles -parent $frame -textVariable APSMpBoosterDCRampStandardize(cycles) \
      -width $width -label "Cycles: " -contextHelp "the target value for BM AFG gain."
    APSLabeledEntry .time -parent $frame -textVariable APSMpBoosterDCRampStandardize(cycleTime) \
      -width $width -label "Cycle Time (seconds): " -contextHelp "number of standardize cycle."
    APSLabeledEntry .step -parent $frame -textVariable APSMpBoosterDCRampStandardize(rampSteps) \
      -width $width -label "Ramp Steps: " -contextHelp "ramp steps."
    APSCheckButtonFrame .check -parent $frame -orientation horizontal -allNone 1 \
      -label "Select magnet to standardize: " -buttonList {BM QF QD SF SD} -variableList $checkVarList 

    APSAddSCRDialog .scr \
      -arrayName  APSMpBoosterDCRampStandardize \
      -daysPast 7 \
      -system Booster \
      -defaultFile $APSMpBoosterDCRampStandardize(ShortFilename) \
      -parent $frame
}

proc APSMpBoosterDCRampStandardizeInfo {args} {
    return "standardize booster dipole DC ramp."
}

proc APSMpBoosterDCRampStandardize {args} {
    global APSMpBoosterDCRampStandardize 
    set SCRdir /home/helios/oagData/SCR/snapshots/Booster
    foreach magnet {BM QF QD SF SD} {
        set $magnet 1
        set ${magnet}rampStart 0
        set ${magnet}rampEnd 0.4
        #   set ${magnet}target 0.14
    }
    set cycles 1
    set cycleTime 120
    set rampSteps 5
    set ShortFilename ""
    APSStrictParseArguments {BM QF QD SF SD \
                               BMrampStart BMrampEnd \
                               QFrampStart QFrampEnd \
                               QDrampStart QDrampEnd \
                               SFrampStart SFrampEnd \
                               SDrampStart SDrampEnd \
                               cycles cycleTime rampSteps ShortFilename}
    
    APSMpStep init -promptDialog APSMpBoosterDCRampStandardizeInitDialog \
      -vars "BM QF QD SF SD \
                               BMrampStart BMrampEnd \
                               QFrampStart QFrampEnd \
                               QDrampStart QDrampEnd \
                               SFrampStart SFrampEnd \
                               SDrampStart SDrampEnd \
                               cycles cycleTime rampSteps ShortFilename"
    
    if ![string length $ShortFilename] {
        APSMpReturn error "SCR file not chosen."
    }
    set magnetList ""
    foreach magnet {BM QF QD SF SD} {
        if [set $magnet] {
            lappend magnetList $magnet
        }
    }
    if ![llength $magnetList] {
        APSMpReturn error "No magnets chosen."
    }
    
    set tmpFile /tmp/[APSTmpString]
    APSAddToTmpFileList -ID 1 -fileList $tmpFile
    if [catch {exec sddsprocess $SCRdir/$ShortFilename $tmpFile -match=col,ControlName=B:*:VoltageRT.GAIN  } result] {
        APSMpReturn error "APSMpBoosterDCRampStandardize4: error in obtaining VoltageRT.GAIN : $result"
    }
    foreach magnet $magnetList {
        set target [exec sddsprocess $tmpFile -match=col,ControlName=B:${magnet}:VoltageRT.GAIN -pipe=out | sdds2stream -pipe=in -col=ValueString]
        set ${magnet}target [expr $target - 0.01]
    }
    foreach magnet $magnetList {
        set target [set ${magnet}target]
        set rampStart [set ${magnet}rampStart]
        set rampEnd [set ${magnet}rampEnd]
        if {$target<$rampStart || $target>$rampEnd} {
            APSMpReturn error "Error for parameters of $magnet: the target has to be between the ramp start and end."
        }
    }
    foreach param {BM QF QD SF SD \
                     BMrampStart BMrampEnd \
                     QFrampStart QFrampEnd \
                     QDrampStart QDrampEnd \
                     SFrampStart SFrampEnd \
                     SDrampStart SDrampEnd \
                     cycles cycleTime rampSteps ShortFilename} {
        append paramString "$param=\"[set $param]\"  "
    }
    
    catch {APSLogScriptAction \
             -procedure  APSMpBoosterDCRampStandardize \
             -action initialize \
             -state ok \
             -parameters $paramString}
    
    set waitTime [expr int($cycleTime * 1.0 / (2 * $rampSteps) + 0.5)]
    
    set putComm ""
    foreach magnet $magnetList {
        set rampStart [set ${magnet}rampStart]
        set rampEnd [set ${magnet}rampEnd]
        set ${magnet}delta [expr ($rampEnd - $rampStart)/($rampSteps -1)]
        lappend putComm B:${magnet}:VoltageRT.GAIN=$rampStart
    }
    
    APSMpStep "move to ramp start -- ..."
    if [catch {exec cavput -list=[join $putComm ,] -pend=20} result] {
        APSMpReturn error "APSMpBoosterDCRampStandardize1: error moving to ramp start: $result"
    }
    
    for {set cycle 1} {$cycle<=$cycles} {incr cycle} {
        APSMpStep "Start cycle $cycle ..."
        APSMpStep "ramping from start to end..."
        for {set step 1} {$step<$rampSteps} {incr step} {
            set putComm ""
            foreach magnet $magnetList {
                set rampStart [set ${magnet}rampStart]
                set delta [set ${magnet}delta]
                set value [expr $rampStart + $step * $delta]
                lappend putComm B:${magnet}:VoltageRT.GAIN=$value
            }
            APSMpStep "Ramp step $step ..."
            if [catch {exec cavput -list=[join $putComm ,] -pend=20} result] {
                APSMpReturn error "APSMpBoosterDCRampStandardize2: error setting value: $result"
            }
            APSWaitWithUpdate -waitSeconds $waitTime
        }
        
        APSMpStep "ramping from end to start ..."
        for {set step 1} {$step<$rampSteps} {incr step} {
            set putComm ""
            foreach magnet $magnetList {
                set rampEnd [set ${magnet}rampEnd]
                set delta [set ${magnet}delta]
                set value [expr $rampEnd - $step * $delta]
                lappend putComm B:${magnet}:VoltageRT.GAIN=$value
            }
            APSMpStep "Ramp step $step ..."
            if [catch {exec cavput -list=[join $putComm ,] -pend=20} result] {
                APSMpReturn error "APSMpBoosterDCRampStandardize2: error setting value: $result"
            }
            APSWaitWithUpdate -waitSeconds $waitTime
        }
    }
    APSMpStep "ramping from start to target ..."
    for {set step 1} {$step<$rampSteps} {incr step} {
        set putComm ""
        set done 0
        foreach magnet $magnetList {
            set rampStart [set ${magnet}rampStart]
            set target [set ${magnet}target]
            set delta [set ${magnet}delta]
            set value [expr $rampStart + $step * $delta]
            if {$value>$target} {
                set value $target
                incr done
            }
            lappend putComm B:${magnet}:VoltageRT.GAIN=$value
        }
        APSMpStep "Ramp step $step ..."
        if [catch {exec cavput -list=[join $putComm ,] -pend=20} result] {
            APSMpReturn error "APSMpBoosterDCRampStandardize2: error setting value: $result"
        }
        APSWaitWithUpdate -waitSeconds $waitTime
        if {$done==[llength $magnetList]} {
            break
        }
    }
    APSMpStep "Restore BM AFG gain from $ShortFilename..."
    if [catch {exec sddsprocess $SCRdir/$ShortFilename -pipe=out -match=col,ControlName=B:*:VoltageRT.GAIN \
                 | sddscasr=verify -pipe=in -restore -pend=30 } result] {
        APSMpReturn error "APSMpBoosterDCRampStandardize4: error in restoring VoltageRT.GAIN : $result"
    }
    APSMpReturn ok
}


