proc APSMpBoosterShutdownForAccessInfo {} {
    return "This will run: APSMpPTBShutdown, APSMpBRFShutDown, APSMpBoosterPSandCorrSetNoPower and APSMpBTSTurnOffPS"
}

proc APSMpBoosterShutdownForAccessInitDialog {frame} {
    pack [frame $frame.top]
    pack [frame $frame.bottom]
    pack [labelframe $frame.top.left -text "Booster PS and Correctors"] -side left -padx 5
    pack [labelframe $frame.top.center -text "Booster Pulsed Supplies"] -anchor n -side left -padx 5
    pack [labelframe $frame.top.right -text PTB] -side left -anchor n -padx 5

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

    APSRadioButtonFrame .rb2 \
      -parent $frame.top.left \
      -label "Set Booster Corrector\nPower Supply Options" \
      -variable APSMpBoosterShutdownForAccess(selection) \
      -buttonList {Horizontal Vertical Both} \
      -valueList {h v b} \
      -contextHelp "These buttons select various combinations of booster corrector supplies."

    APSCheckButtonFrame .ikButtons \
      -parent $frame.top.center \
      -label "Select Booster Kickers" \
      -variableList {APSMpBoosterShutdownForAccess(IK) \
		       APSMpBoosterShutdownForAccess(EK)} \
      -buttonList "B:IK B:EK" \
      -allNone 1
    APSCheckButtonFrame .isButtons \
      -parent $frame.top.center \
      -label "Select Booster Septums" \
      -variableList {APSMpBoosterShutdownForAccess(IS) \
		       APSMpBoosterShutdownForAccess(ES1) \
                       APSMpBoosterShutdownForAccess(ES2)} \
      -buttonList "B:IS B:ES1 B:ES2" \
      -allNone 1

    APSRadioButtonFrame .rb1 \
      -parent $frame.top.right \
      -label "DC Magnet Mode" \
      -variable APSMpBoosterShutdownForAccess(mode) \
      -buttonList {Ramped Abrupt} \
      -valueList {ramped abrupt} \
      -contextHelp \
      "Choose whether to ramp DC supplies to zero or set them to zero abruptly."

    APSLabeledEntry .le2 \
      -parent $frame.top.right \
      -label "Seconds for rampdown" \
      -textVariable APSMpBoosterShutdownForAccess(rampdownTime) \
      -contextHelp "Give the approximate maximum number of seconds for rampdown."

}

proc APSMpBoosterShutdownForAccess {args} {
    set mode "ramped"
    set rampdownTime 60
    set BM 1
    set QF 1
    set QD 1
    set SF 1
    set SD 1
    set selection b
    set IS 1
    set IK 1
    set ES1 1
    set ES2 1
    set EK 1
    #set turnOffList [list BTS:AH BTS:AV BTS:AQ BTS:BH BTS:BV BTS:BQ BTS:CH BTS:CV BTS:CQ BTS:AB BTS:BB BTS:BX]
    set turnOffList [list BTS:AH BTS:AV BTS:AQ BTS:AB BTS:BX]
    set turnOffListDetailed ""

    APSStrictParseArguments {mode rampdownTime BM QF QD SF SD selection turnOffList turnOffListDetailed IS IK ES1 ES2 EK}

    set arrayName APSMpBoosterShutdownForAccess
    global $arrayName
    if {![info exists $arrayName] || !$configured} {
        APSMpGenericPSArraySetup -arrayName $arrayName \
          -turnOffList $turnOffList \
          -turnOffListDetailed $turnOffListDetailed \
          -system booster
    }
    if {[APSPEMTKDialog .warningDialog -text "Disable BIK PVMonitor prior to running. Also check to ensure BTS double stop is open."] == 1} {
        APSMpReturn error "Exited by user"
    }

    APSMpStep init -promptDialog \
      "APSLGenericPSDialog -packProc APSMpBoosterShutdownForAccessInitDialog -turnOff 1 -arrayName $arrayName -system Booster -frame " -vars {mode rampdownTime BM QF QD SF SD selection IS IK ES1 ES2 EK}

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



    set id1 [APSMpParallel -mode "Automatic" -procedure "APSMpBTSTurnOffPS -turnOffList \"$turnOffList\" -turnOffListDetailed \"$turnOffListDetailed\" -configured 1 -arrayName $arrayName"]

    set id2 [APSMpParallel -mode "Automatic" -procedure "APSMpBoosterPSandCorrSetNoPower -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD -selection $selection"]

    set id3 [APSMpParallel -mode "Automatic" -procedure "APSMpPTBShutdown -mode $mode -rampdownTime $rampdownTime"]

    set id4 [APSMpParallel -mode "Automatic" -procedure "APSMpBRFShutDown"]

    set id5 [APSMpParallel -mode "Automatic" -procedure "APSMpBPSTurnOffPulsedSupplies -IS $IS -IK $IK -ES1 $ES1 -ES2 $ES2 -EK $EK"]
    

    if {[catch {APSMpJoin $id1} results]} {
        if {[APSPEMTKDialog .warningDialog -text "Problem occured turning off BTS power supplies\nAPSMpBoosterShutdownForAccess: $results"] == 1} {
            APSMpReturn error "APSMpBoosterShutdownForAccess: $results"
        }
    } 
    if {[catch {APSMpJoin $id2} results]} {
        if {[APSPEMTKDialog .warningDialog -text "Problem occured running APSMpBoosterPSandCorrSetNoPower\nAPSMpBoosterShutdownForAccess: $results"] == 1} {
            APSMpReturn error "APSMpBoosterShutdownForAccess: $results"
        }
    } 
    if {[catch {APSMpJoin $id3} results]} {
        if {[APSPEMTKDialog .warningDialog -text "Problem occured with PTB shutdown\nAPSMpBoosterShutdownForAccess: $results"] == 1} {
            APSMpReturn error "APSMpBoosterShutdownForAccess: $results"
        }
    } 
    if {[catch {APSMpJoin $id4} results]} {
        if {[APSPEMTKDialog .warningDialog -text "Problem occured with BRF shutdown\nAPSMpBoosterShutdownForAccess: $results"] == 1} {
            APSMpReturn error "APSMpBoosterShutdownForAccess: $results"
        }
    } 

    if {[catch {APSMpJoin $id5} results]} {
        if {[APSPEMTKDialog .warningDialog -text "Problem occured with Booster pulsed supplies shutdown\nAPSMpBoosterShutdownForAccess: $results"] == 1} {
            APSMpReturn error "APSMpBoosterShutdownForAccess: $results"
        }
    } 

    APSMpReturn ok

}
