proc APSMpBooster_ConfigureInfo {} {
    return "This will run: APSMpBTSConditionPowerSupplies, APSMpBoosterPSAndCorrOnFullPower, APSMpPTBStartUp and APSMpBRFStartUp"
}

proc APSMpBooster_Configure {args} {
    #PTB
    set energy [APSPARLETDefaultEnergy]
    set conditioningTime 10
    #BRF
    set tempCheck 2
    set doRestore 1
    set finalPower 270
    #BTS
    set cycles 2
    set conditionList [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 conditionListDetailed ""
    set turnOnList $conditionList
    set turnOnListDetailed $conditionListDetailed
    global APSMpBTSControlPS
    set arrayName APSMpBTSControlPS
    APSMpGenericPSArraySetup -arrayName $arrayName \
      -turnOnList $turnOnList \
      -turnOnListDetailed $turnOnListDetailed \
      -conditionList $conditionList \
      -conditionListDetailed $conditionListDetailed \
      -SCRFile "" \
      -cycles $cycles \
      -system booster
    #BOOSTER PS
    set BM 0
    set QF 0
    set QD 0
    set SF 0
    set SD 0


    APSMpStep init -promptDialog APSMpBooster_ConfigureInitDialog \
      -vars {energy conditioningTime tempCheck doRestore finalPower BM QF QD SF SD}

    set conditionList [APSMpGenericPSArrayQuery \
			   -arrayName $arrayName \
			   -operation StartConditioning]
    set temp [APSMpConvertDeviceNames \
			   -operation StartConditioning \
			   -deviceList $conditionList \
			   -system booster]
    set conditionListDetailed [APSMpConvertDetailedDeviceNames \
			   -operation StartConditioning \
			   -deviceList $temp \
			   -arrayName $arrayName]
    set cycles [set ${arrayName}(StartConditioningInfo,cycles)]
    global apsLPLSCR apsBoosterSCR apsBRFSCR

    set id1 [APSMpParallel -mode "Automatic" -procedure "APSMpBTSConditionPowerSupplies -configured 1 -conditionList \"$conditionList\" -conditionListDetailed \"$conditionListDetailed\" -cycles $cycles -SCRFile $apsBoosterSCR(Filename)"]

    set id2 [APSMpParallel -mode "Automatic" -procedure "APSMpBoosterPSAndCorrOnFullPower -SCRFile $apsBoosterSCR(Filename) -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD"]

    set id3 [APSMpParallel -mode "Automatic" -procedure "APSMpPTBStartUp -energy $energy -conditioningTime $conditioningTime -restoreFile [file tail $apsLPLSCR(Filename)]"]

    set id4 [APSMpParallel -mode "Automatic" -procedure "APSMpBRFStartUp -tempCheck $tempCheck -doRestore $doRestore -finalPower $finalPower -restoreFile $apsBRFSCR(Filename)"]
    
    if {[catch {APSMpJoin $id1} results]} {
        if {[APSPEMTKDialog .warningDialog -text "Problem occured turning on and conditioning BTS power supplies\n $results"] == 1} {
            APSMpReturn error "APSMpBooster_Configure: $results"
        }
    } 
    if {[catch {APSMpJoin $id2} results]} {
        if {[APSPEMTKDialog .warningDialog -text "Problem occured running APSMpBoosterPSAndCorrOnFullPower\n $results"] == 1} {
            APSMpReturn error "APSMpBooster_Configure: $results"
        }
    } 
    if {[catch {APSMpJoin $id3} results]} {
        if {[APSPEMTKDialog .warningDialog -text "Problem occured with PTB startup\n $results"] == 1} {
            APSMpReturn error "APSMpBooster_Configure: $results"
        }
    } 
    if {[catch {APSMpJoin $id4} results]} {
        if {[APSPEMTKDialog .warningDialog -text "Problem occured with BRF startup\n $results"] == 1} {
            APSMpReturn error "APSMpBooster_Configure: $results"
        }
    } 
    APSMpReturn ok

}

proc APSMpBooster_ConfigureInitDialog {frame} {
    global APSMpBRFStartUp
    global apsttk
    set apsttk 1
    option clear
    
    frame ${frame}.left
    frame ${frame}.right
    pack ${frame}.left ${frame}.right -side left -fill y
    frame ${frame}.left.top
    frame ${frame}.left.bottom
    pack ${frame}.left.top -fill both
    pack ${frame}.left.bottom -side bottom -anchor sw
    
    APSAddSCRDialog .lplscr \
	-arrayName apsLPLSCR \
	-daysPast 7 \
	-system LPL \
	-parent $frame.left.bottom

    APSAddSCRDialog .boosterscr \
	-arrayName apsBoosterSCR \
	-daysPast 7 \
	-system Booster \
	-parent $frame.right

    APSAddSCRDialog .brfscr \
	-arrayName apsBRFSCR \
	-daysPast 7 \
	-system BRF \
	-parent $frame.right

    #PTB
    set w ${frame}.left.top.ptb
    ttk::labelframe $w -text "PTB"
    pack $w -anchor nw

    APSLabeledEntry .le1 -parent $w -label "Energy (MeV): " \
      -textVariable APSMpBooster_Configure(energy) \
      -contextHelp "Enter the energy of the beam you will be using in MeV."
      
    APSLabeledEntry .le2 -parent $w -label "Minutes for conditioning: " \
      -textVariable APSMpBooster_Configure(conditioningTime) \
      -contextHelp "Enter the number of minutes you want to allow for conditioning.  The actual conditioning time may be longer, since each magnet must be put through at least one ramp cycle."

    #BRF
    set w ${frame}.left.top.booster
    ttk::frame $w
    pack $w -anchor nw

    set w ${frame}.left.top.booster.brf
    ttk::labelframe $w -text "Booster RF"
    pack $w -anchor nw -side left

    APSLabeledEntry .le1 -parent $w -label "Final power (kW): " \
      -textVariable APSMpBooster_Configure(finalPower) -width 7 \
      -contextHelp "Enter the final RF power desired in kW."

    pack [frame $w.f1] -anchor nw
    APSRadioButtonFrame .rf0 -parent $w.f1 -label "Do restore: " \
      -buttonList {Yes No} -valueList {1 0} -packOption "-side left" \
      -variable APSMpBooster_Configure(doRestore) \
      -contextHelp "Select whether to do restore a file at all."

    APSRadioButtonFrame .rf1 -parent $w.f1 -label "Temperature check: " \
      -buttonList {"None" "Careful" "Very Careful"} -packOption "-side left" \
      -valueList {0 1 2} \
      -variable APSMpBooster_Configure(tempCheck) \
      -contextHelp "Select the type of klystron body temperature checking to do when raising the beam power.  For cold startup, use Very Careful, which waits for warmup and cooloff.  Careful mode just waits for the temperature to crest and begin to fall.  None mode doesn't wait at all, and isn't really recommended."
    

    #Booster

    APSCheckButtonFrame .cb -parent ${frame}.left.top.booster -label "Booster Power Supplies" -buttonList "BM QF QD SF SD" -variableList "APSMpBooster_Configure(BM) APSMpBooster_Configure(QF) APSMpBooster_Configure(QD) APSMpBooster_Configure(SF) APSMpBooster_Configure(SD)" -allNone 1 -packOption "-side left"


    #BTS
    set w ${frame}.left.top.bts
    ttk::labelframe $w -text "BTS Power Supplies"
    pack $w -anchor nw

    global apsBackground apsForeground apsEntryBackground
    option add *font OAGFont
    option add *background $apsBackground
    option add *foreground $apsForeground
    option add *Entry.background $apsEntryBackground
    set apsttk 0
    APSLGenericPSDialog -turnOn 0 -condition 1 -chooseSCR 0 -arrayName APSMpBTSControlPS -system Booster -frame $w
}

