# Sets gain to standby from a higher level for selected booster power supplies. 
# This is done in one step.  If a swap error is detected it retries until
# it is successful.

# Procedure gives information overview for APSMpBoosterPSDecreaseGainToStandbyInfo.

proc APSMpBoosterPSDecreaseGainToStandbyInfo {} {

    return "Sets gain to standby from a higher level for selected booster power supplies.\
This is done in one step.  If a swap error is detected it retries until\
it is successful."

}

# Procedure provides a dialogbox to select booster magnet supplies.

proc APSMpBoosterPSDecreaseGainToStandbyDialog {frame} {

    APSSetVarAndUpdate status "Select booster power supplies..."

    APSCheckButtonFrame .magnetSelectButton -parent $frame -label "Select magnet\npower supplies" \
        -variableList {APSMpBoosterPSDecreaseGainToStandby(BM)    \
                           APSMpBoosterPSDecreaseGainToStandby(QF)  \
                           APSMpBoosterPSDecreaseGainToStandby(QD)  \
                           APSMpBoosterPSDecreaseGainToStandby(SF)  \
                           APSMpBoosterPSDecreaseGainToStandby(SD)} \
        -buttonList {BM QF QD SF SD} \
        -allNone 1 \
        -packOption {-side top}

    return
}

# Sets selected booster power supplies ramping at full power.

proc APSMpBoosterPSDecreaseGainToStandby {args} {

    set BM 0
    set QF 0
    set QD 0
    set SF 0
    set SD 0
    set standbyGainLevel 0.4

    APSStrictParseArguments {BM QF QD SF SD}

    APSMpStep "Select Booster Power Supplies" -promptDialog APSMpBoosterPSDecreaseGainToStandbyDialog -vars {BM QF QD SF SD}
    
    set magnetList ""
    foreach magnet "BM QF QD SF SD" {
        if {[set $magnet]} {
            set magnetList "$magnet "
#            if {[catch {APScavget -list=B:${magnet}:VoltageRT.GAIN -pendIoTime=20 -numerical} presentPowerAFGGain]} {
#                APSMpReturn error "APSMpBoosterPSDecreaseGainToStandby: $presentPowerAFGGain"
#            }
#            if {$presentPowerAFGGain <= $standbyGainLevel} {
#                set $magnet 0
#            } else {
#                set magnetList "$magnet "
#            }
        }
    }
    if {![llength $magnetList]} {
        APSMpReturn ok
    }
    
    #Disable slope and zero
    APSMpStep "Disable slope and zero for $magnetList" -desc "Disable slope and zero for magnet"
    if {[catch {APSMpBoosterDisableSlopeAndZero -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSDecreaseGainToStandby: Problem occurred while disabling slope and zero\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSDecreaseGainToStandby: $results"
        }
    }
    #Set gain to standby
    APSMpStep  "Setting gain factor for $magnetList to the standby level."
    if {[catch {APSMpBoosterPSSetGain -value $standbyGainLevel -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSDecreaseGainToStandby: Problem occurred while setting gain to standby level\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSDecreaseGainToStandby: $results"
        }
    }

    APSMpReturn ok
}
