# This program turns on or off horizontal and/or vertical 
# booster corrector power supplies or raw supplies.
# This program turns the booster corrector raw supplies on
# at 65 Volts, loads zero ramp tables, and either turns
# selected correctors on or off.  If off is selected, the 
# raw supplies are also turned off.  "Fast" mode does not
# load zero ramps when turning the supplies off.  "Fast"
# mode will not perform INIT/AUTO/ACTIVATE procedure before
# turning on.  "Raw Only" turns on only the raw supplies,
# loads 65 Volts as their setpoint and does this in "Fast" mode.
# If on is selected it loads the booster corrector ramps
# for both horizontal and vertical planes.
 

proc APSMpBoosterSetCorrAndRampsOnOffInfo {} {

    return "For more detailed information please refer to OPSDoc_Boo_PEM002 under required reading.

This procedure is used to configure the booster corrector Arbitrary Function Generators (AFGs), booster horizontal and vertical corrector supplies and booster corrector raw supplies.  The operator is asked to select the option to turn the supplies On or Off.  The operator also must choose horizontal, vertical or both sets of correctors to turn on/off.  There are two options \"Fast\" and \"Raw Only\" the operator may select if desired.  The operator must also choose an SCR file which specifies the booster lattice.  Knowledge of the desired lattice is required since each of the three different booster lattices require different corrector ramps.  

If \"On\" is selected and \"Fast\" and \"Raw Only\" are not selected, the procedure turns the booster corrector raw supplies on, loads 65 Volts in the raw supply voltage setpoints, turns the selected correctors on, re-initializes the corrector AFGs by performing the INIT/AUTO/ACTIVATE procedure and loads ramp tables with zero setpoints in the corrector AFGs.  The supply then loads the corrector ramps corresponding to the lattice defined by the SCR file.  If \"Off\" is selected, both raw and corrector supplies are turned off, INIT/AUTO/ACTIVATE is executed and ramp tables with zero setpoints are loaded in the AFGs.  The corrector ramps corresponding to the lattice are NOT loaded if \"Off\" is selected (ie. only ramps with zero setpoints are loaded in the AFGs).  After a shutdown, one should execute this machine procedure without \"Fast\" or \"Raw Only\" selected so that the AFGs are properly reinitialized.

If \"Fast\" is selected with either \"On\" or \"Off\", the INIT/AUTO/ACTIVATE procedure is NOT executed and ramp tables with zero setpoints are NOT loaded.  If \"Raw Only\" is selected with either \"On\" or \"Off\", only the raw supplies are turned on or off.  When \"Raw Only\" is selected the INIT/AUTO/ACTIVATE procedure is NOT executed and ramp tables with zero setpoints are NOT loaded. Typically, these options are only used infrequently.

The INIT/AUTO/ACTIVATE is an initialization procedure used to reset the corrector AFGs.  If  after a shutdown the AFGs are found not to function, most of the time this procedure will reset them.  If this procedure fails to reset the corrector AFGs (ie. they won't accept corrector ramp tables for a valid lattice) a GESPAC reset is required.  After the GESPAC reset, this procedure should be executed again.
"

    return
}

proc APSMpBoosterSetCorrAndRampsOnOffDialog {frame} {

    pack [frame $frame.top]
    set frame $frame.top

    APSSetVarAndUpdate status "Set Booster Corrector Power Supply Options"
    label $frame.la1 -text "Set Booster Corrector Power Supply Options"
    pack $frame.la1 -side top
    APSRadioButtonFrame .rb1 -parent $frame -label "" \
	-variable APSMpBoosterSetCorrAndRampsOnOff(state1) \
	-buttonList {On Off} \
	-valueList {on off} \
	-packOption {-side top} \
	-contextHelp "These buttons turn the booster corrector raw supplies on at 65 Volts, load zero ramp tables, and either turn selected correctors on or off.  If off is selected, the raw supplies are also turned off."
    
    APSRadioButtonFrame .rb2 -parent $frame -label "" \
	-variable APSMpBoosterSetCorrAndRampsOnOff(selection) \
	-buttonList {Horizontal Vertical Both} \
	-valueList {h v b} \
	-packOption {-side top} \
	-contextHelp "These buttons select various combinations of booster corrector supplies."
    
    APSCheckButtonFrame .cb1 -parent $frame -label "" \
	-packOption {-side top} \
	-buttonList {Fast "Raw Only"} \
	-variableList "APSMpBoosterSetCorrAndRampsOnOff(Fast) APSMpBoosterSetCorrAndRampsOnOff(RawOnly)" \
	-contextHelp "\"Fast\" mode does not load zero ramps when turning the supplies off.  \"Fast\" mode will not perform INIT/AUTO/ACTIVATE procedure before turning on.  \"Raw Only\" turns on only the raw supplies, loads 65 Volts as their setpoint and does this in \"Fast\" mode."
    
    return
}

proc APSMpBoosterSetCorrAndRampsOnOff {args} {
    set state1 off
    set selection b
    set Fast 0
    set RawOnly 0
    set SCRFile ""

    APSStrictParseArguments {state1 selection Fast RawOnly SCRFile}

    set arrayName APSBPSControlPS
    global $arrayName
    APSMpGenericPSArraySetup -arrayName $arrayName \
	-SCRFile $SCRFile -system booster

    APSMpStep init -promptDialog "APSMpGenericPSDialog -packProc APSMpBoosterSetCorrAndRampsOnOffDialog -chooseSCR 1 -arrayName $arrayName -system Booster -frame " -vars {state1 selection Fast RawOnly}

#    APSMpStep init -promptDialog APSMpBoosterSetCorrAndRampsOnOffDialog -vars {state1 selection Fast RawOnly}

    set SCRFile $APSBPSControlPS(Filename)

    set paramString ""
    foreach param {state1 selection Fast RawOnly SCRFile} {
        append paramString "$param=\"[set $param]\" "
    }
    catch {APSLogScriptAction \
             -procedure APSMpBoosterSetCorrAndRampsOnOff \
             -action initialize \
             -state ok \
             -parameters $paramString}

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

    APSMpStep "Turn $state1 corrector Power Supplies" -desc "Turn on or off corrector power supplies" -proc APSMpBoosterSetCorrPowerSuppliesOnOff
    if [catch {eval APSMpBoosterSetCorrPowerSuppliesOnOff -state1 $state1 -selection $selection -Fast $Fast -RawOnly $RawOnly} results] {
	APSMpReturn error "APSMpBoosterSetCorrAndRampsOnOff: $results"
    }
    update

    if {[string compare $state1 on]==0} {

        if [catch {exec sddsselect $SCRFile /home/helios/oagData/pem/booster/SCRFilterFile -match=ControlName -pipe=out | sdds2stream -column=ControlName,ValueString -pipe=in} results] {
            APSMpReturn error "APSMpBoosterSetCorrAndRampsOnOff: Invalid SCR file"
        }
        foreach {controlName valueString} $results {
            if {"B:QD:SlopeRefAO" == $controlName} {
                set qdslope $valueString
            } elseif {"B:QF:SlopeRefAO" == $controlName} {
                set qfslope $valueString
            }   
        }
        set boosterLattice [APSMpCheckBoosterLattice -qd $qdslope -qf $qfslope]
	if [string compare $boosterLattice "unknown"]==0 {
	    global Lattice
	    set Lattice ""
	    APSDialogBox .lattice -name "Choose Lattice Dialog" \
		-okCommand "update" -cancelCommand "destroy .lattice"
	    APSRadioButtonFrame .radio -parent .lattice.userFrame -buttonList "92nm 109nm 132nm" \
		-variable Lattice -valueList "x13.75-y5.80 x12.75-y9.80 x11.75-y9.80" \
		-label "Lattice: " -orientation Horizontal
	    tkwait window .lattice
	    if ![string length $Lattice] {
		APSMpReturn error "The lattice was not chosen!"
	    }
	    set boosterLattice $Lattice
	}
	
	APSMpStep "Load corrector ramps" -desc "Load corrector ramps if needed" -proc APSMpBoosterLoadCorrectorRamps
	if [catch {eval APSMpBoosterLoadCorrectorRamps -loadAdl 0 -lattice $boosterLattice} results] {
	    APSMpReturn error "APSMpBoosterSetCorrAndRampsOnOff: $results"
	}
	update

        APSMpStep "Check and Plot corrector ramps" -desc "Check and Plot corrector ramps" -proc APSMpBoosterCheckCorrectorRampTable
        if {[catch {APSMpBoosterCheckCorrectorRampTable -BoosterLattice $boosterLattice} results]} {
            APSMpReturn error "APSMpBoosterSetCorrAndRampsOnOff: $results"
        }

    }
    APSMpReturn ok
}
