#
# $Log: not supported by cvs2svn $
# Revision 1.21  2009/09/24 17:39:10  soliday
# Added the APSMpBoosterPSSetDCStandbyPowerDirectly procedure which is
# to only be used by the PS group. Unlike the APSMpBoosterPSSetDCStandbyPower
# procedure, this one does not first bring the PS to normal standby mode first.
#
# Revision 1.20  2007/03/14 15:44:47  soliday
# Added a few more APSMpStep commands to log more information that would
# help me diagnose a possible problem.
#
# Revision 1.19  2007/03/01 17:24:27  soliday
# Added missing calls to APSLogScriptAction to store initialization values.
#
# Revision 1.18  2007/01/18 15:53:57  soliday
# Updated to work with the new domain names.
#
# Revision 1.17  2006/12/19 16:46:00  soliday
# Changed the procedure name of loadSafetyRamps to loadSafetyRamps2 because
# it was conflicting with a procedure of the same name in
# BRampControlAutoCorrection.tcl that accepted different arguments.
#
# Revision 1.16  2006/09/28 21:20:49  soliday
# Adjusted how it checks the ACIS PVs per Sereno's request.
#
# Revision 1.15  2006/08/24 14:35:59  soliday
# Changed an error message because some magnets do not have freewheel PVs.
#
# Revision 1.14  2006/05/22 21:55:11  soliday
# Added checks for acis inhibit and acis trips.
#
# Revision 1.13  2006/03/01 21:21:02  soliday
# Changed the threshold for the maximum average output DC current for the BM
# when the gain is set to zero.
#
# Revision 1.12  2004/10/28 15:48:15  soliday
# Updated the descriptions per Sereno's request.
#
# Revision 1.11  2004/10/21 16:36:52  soliday
# Updated the descriptions for the machine procedures to the descriptions given
# to me by Nick Sereno.
#
# Revision 1.10  2004/10/08 15:03:09  soliday
# Updated the description.
#
# Revision 1.9  2004/10/07 20:52:04  soliday
# Updated so that the supplies are checked to see if the related IOC was rebooted.
# If so it brings them back into a valid state before continuing.
#
# Revision 1.8  2004/04/15 20:21:15  soliday
# Added additional safety checks prior to starting the PS.
#
# Revision 1.7  2004/04/14 20:57:55  soliday
# Streamlined code and added a few additional safety checks. Also reduced
# the number of channel access search requests.
#
# Revision 1.6  2001/01/26 14:58:52  soliday
# Changed state variable to avoid conflicts.
#
# Revision 1.5  2000/11/07 18:09:20  soliday
# Fixed a problem with the loading of the safety ramps.
#
# Revision 1.4  2000/11/07 17:27:10  soliday
# Changed file copy command to APSFileCopy.
#
# Revision 1.3  2000/05/30 23:34:32  soliday
# Fixed bug with init step. Can't have dialog after that step.
#
# Revision 1.2  2000/05/30 22:19:15  soliday
# Added APSMpStep init so that the logDaemon can log the start of the pem.
#
# Revision 1.1  1999/11/24 17:26:17  soliday
# Now allows the user to start the PS in DC mode
#
#


proc APSMpBoosterPSSetDCStandbyPowerDirectlyInfo {} {

    return "For information about common safeguards and check please refer to OPSDoc_Boo_PEM001 under required reading.

WARNING: This procedure does not go normal standyby mode first. It assumes it is already there.  This procedure brings the selected supply(s) from any state to DC standby power.  It configures the supply regulator PVs for DC mode and loads the DC ramps.
"

}
proc APSMpBoosterPSSetDCStandbyPowerDirectlyDialog {frame} {

    APSSetVarAndUpdate status "Select booster power supplies..."

    APSCheckButtonFrame .magnetSelectButton -parent $frame -label "Select magnet\npower supplies" \
        -variableList {APSMpBoosterPSSetDCStandbyPowerDirectly(BM)    \
                           APSMpBoosterPSSetDCStandbyPowerDirectly(QF)  \
                           APSMpBoosterPSSetDCStandbyPowerDirectly(QD)  \
                           APSMpBoosterPSSetDCStandbyPowerDirectly(SF)  \
                           APSMpBoosterPSSetDCStandbyPowerDirectly(SD)} \
        -buttonList {BM QF QD SF SD} \
        -allNone 1 \
        -packOption {-side top}
    APSLabeledEntry .le1 -parent $frame -label "Standby Gain Level: " \
      -textVariable APSMpBoosterPSSetDCStandbyPowerDirectly(standbyGainLevel) -width 10

    return
}
proc APSMpBoosterPSSetDCStandbyPowerDirectly {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 standbyGainLevel}

    global execMode

    APSMpStep init -promptDialog APSMpBoosterPSSetDCStandbyPowerDirectlyDialog -vars {BM QF QD SF SD standbyGainLevel}
    
    if {$execMode != "Automatic"} {
        if {[catch {APScavget -list=BOO-ACIS:MCR:ExtInhibitLedM,BOO-ACIS:BooMode:BpM -num -printErrors -pend=10} acisState]} {
            APSMpReturn error "APSMpBoosterPSSetDCStandbyPowerDirectly: $acisState"
        }
        if {[lindex $acisState 1] != 1} {
            APSMpReturn error "APSMpBoosterPSSetDCStandbyPowerDirectly: Booster is not in beam permit mode. The Booster must be in beam permit mode before you can bring up any of the supplies."
        }
        if {$BM} {
            if {[lindex $acisState 0] != 0} {
                set BM 0
                if {[APSPEMTKDialog .warningDialog -text "Booster is in an acis inhibit state. BM has been unselected"] == 1} {
                    APSMpReturn error "APSMpBoosterPSSetDCStandbyPowerDirectly: Booster is in an acis inhibit state. BM has been unselected"
                }
            }
        }
    }

    set paramString ""
    foreach param {BM QF QD SF SD standbyGainLevel} {
        append paramString "$param=\"[set $param]\" "
    }
    catch {APSLogScriptAction \
             -procedure APSMpBoosterPSSetDCStandbyPowerDirectly \
             -action initialize \
             -state ok \
             -parameters $paramString}

    if $SD {
        if [catch {exec cavget -list=B:SD-U:PoweredM -pend=10 -printErrors} SDstatus] {
            APSMpReturn error "APSMpBoosterPSSetNoPower: Error reading SD status: $SDstatus"
        }
        if {$SDstatus == "B:SD-U"} {
            set SD 0
            APSMpStep "Ignore SD-U for setting DC standby power."
        }
    }

    set checked 0
    foreach magnet "BM QF QD SF SD" {
	if {[set $magnet] == 1} {
	    set checked 1
	}
    }
    if {!$checked} { 
	APSMpReturn ok
    }
    
    #Disable ramps
    APSMpStep "Disabling ramps" -proc APSMpBoosterPSEnableDisableRamps
    if {[catch {APSMpBoosterPSEnableDisableRamps -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD -state1 disable -standbyGainLevel $standbyGainLevel} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSSetDCStandbyPowerDirectly: Problem occurred while disabling ramps\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSSetDCStandbyPowerDirectly: $results"
        }
    }

    #Set FreeWheel PVs to DC mode
    APSMpStep "Set FreeWheel PVs to DC mode" -proc APSMpBoosterPSSetFreeWheelPVs
    if {[catch {APSMpBoosterPSSetFreeWheelPVs -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD -DC 1} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSSetDCStandbyPowerDirectly: Problem occurred while setting FreeWheel PVs to non-DC mode\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSSetDCStandbyPowerDirectly: $results"
        }
    }
    APSWaitWithUpdate -waitSeconds 1

    #Load DC ramps
    APSMpStep "Loading DC ramps" -proc loadDCRamps
    if {[catch {loadDCRamps -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSSetDCStandbyPowerDirectly: Problem occurred while loading DC ramps\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSSetDCStandbyPowerDirectly: $results"
        }
    }
    APSWaitWithUpdate -waitSeconds 1

    #Enable ramps
    APSMpStep "Enabling ramps" -proc APSMpBoosterPSEnableDisableRamps
    if {[catch {APSMpBoosterPSEnableDisableRamps -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD -state1 enable -standbyGainLevel $standbyGainLevel} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSSetDCStandbyPowerDirectly: Problem occurred while enabling ramps\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSSetDCStandbyPowerDirectly: $results"
        }
    }

    APSMpReturn ok
}


# Procedure gives information overview for APSMpBoosterPSSetDCStandbyPower.

proc APSMpBoosterPSSetDCStandbyPowerInfo {} {

    return "For information about common safeguards and check please refer to OPSDoc_Boo_PEM001 under required reading.

Brings the selected supply(s) from any state to DC standby power.  It configures the supply regulator PVs for DC mode and loads the DC ramps.
"

}

# Procedure provides a dialogbox to select booster magnet supplies.

proc APSMpBoosterPSSetDCStandbyPowerDialog {frame} {

    APSSetVarAndUpdate status "Select booster power supplies..."

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

    return
}

proc APSMpBoosterPSSetDCStandbyPower {args} {

    set BM 0
    set QF 0
    set QD 0
    set SF 0
    set SD 0
    
    APSStrictParseArguments {BM QF QD SF SD}

    global execMode

    APSMpStep init -promptDialog APSMpBoosterPSSetDCStandbyPowerDialog -vars {BM QF QD SF SD}
    
    if {$execMode != "Automatic"} {
        if {[catch {APScavget -list=BOO-ACIS:MCR:ExtInhibitLedM,BOO-ACIS:BooMode:BpM -num -printErrors -pend=10} acisState]} {
            APSMpReturn error "APSMpBoosterPSSetDCStandbyPower: $acisState"
        }
        if {[lindex $acisState 1] != 1} {
            APSMpReturn error "APSMpBoosterPSSetDCStandbyPower: Booster is not in beam permit mode. The Booster must be in beam permit mode before you can bring up any of the supplies."
        }
        if {$BM} {
            if {[lindex $acisState 0] != 0} {
                set BM 0
                if {[APSPEMTKDialog .warningDialog -text "Booster is in an acis inhibit state. BM has been unselected"] == 1} {
                    APSMpReturn error "APSMpBoosterPSSetDCStandbyPower: Booster is in an acis inhibit state. BM has been unselected"
                }
            }
        }
    }

    set paramString ""
    foreach param {BM QF QD SF SD} {
        append paramString "$param=\"[set $param]\" "
    }
    catch {APSLogScriptAction \
             -procedure APSMpBoosterPSSetDCStandbyPower \
             -action initialize \
             -state ok \
             -parameters $paramString}

    set checked 0
    foreach magnet "BM QF QD SF SD" {
	if {[set $magnet] == 1} {
	    set checked 1
	}
    }
    if {!$checked} { 
	APSMpReturn ok
    }

    #Set to standby power
    APSMpStep "Setting to standby power" -proc APSMpBoosterPSSetStandbyPower
    if {[catch {APSMpBoosterPSSetStandbyPower -BM $BM -QF $QF -QD $QD -SF-U $SF -SD $SD -exitDC 0} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSSetDCStandbyPower: Problem occurred while setting to standby power\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSSetDCStandbyPower: $results"
        }
    }
    APSWaitWithUpdate -waitSeconds 1
    
    #Disable ramps
    APSMpStep "Disabling ramps" -proc APSMpBoosterPSEnableDisableRamps
    if {[catch {APSMpBoosterPSEnableDisableRamps -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD -state1 disable} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSSetDCStandbyPower: Problem occurred while disabling ramps\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSSetDCStandbyPower: $results"
        }
    }

    #Set FreeWheel PVs to DC mode
    APSMpStep "Set FreeWheel PVs to DC mode" -proc APSMpBoosterPSSetFreeWheelPVs
    if {[catch {APSMpBoosterPSSetFreeWheelPVs -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD -DC 1} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSSetDCStandbyPower: Problem occurred while setting FreeWheel PVs to non-DC mode\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSSetDCStandbyPower: $results"
        }
    }
    APSWaitWithUpdate -waitSeconds 1

    #Load DC ramps
    APSMpStep "Loading DC ramps" -proc loadDCRamps
    if {[catch {loadDCRamps -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSSetDCStandbyPower: Problem occurred while loading DC ramps\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSSetDCStandbyPower: $results"
        }
    }
    APSWaitWithUpdate -waitSeconds 1

    #Enable ramps
    APSMpStep "Enabling ramps" -proc APSMpBoosterPSEnableDisableRamps
    if {[catch {APSMpBoosterPSEnableDisableRamps -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD -state1 enable} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSSetDCStandbyPower: Problem occurred while enabling ramps\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSSetDCStandbyPower: $results"
        }
    }

    APSMpReturn ok
}

proc APSMpBoosterPSExitDCStandbyPowerDirectlyInfo {} {

    return "WARNING: This procedure is for PS group use only. It exits DC Standby mode by: setting the gain to 0, loading safety ramps, setting freewheel PVs to non-DC mode, and finally setting the final gain value."
}

# Procedure provides a dialogbox to select booster magnet supplies.

proc APSMpBoosterPSExitDCStandbyPowerDirectlyDialog {frame} {

    APSSetVarAndUpdate status "Select booster power supplies..."

    APSCheckButtonFrame .magnetSelectButton -parent $frame -label "Select magnet\npower supplies" \
        -variableList {APSMpBoosterPSExitDCStandbyPowerDirectly(BM)    \
                           APSMpBoosterPSExitDCStandbyPowerDirectly(QF)  \
                           APSMpBoosterPSExitDCStandbyPowerDirectly(QD)  \
                           APSMpBoosterPSExitDCStandbyPowerDirectly(SF)  \
                           APSMpBoosterPSExitDCStandbyPowerDirectly(SD)} \
        -buttonList {BM QF QD SF SD} \
        -allNone 1 \
        -packOption {-side top}
    APSLabeledEntry .le1 -parent $frame -label "Final Gain Level: " \
      -textVariable APSMpBoosterPSExitDCStandbyPowerDirectly(finalGain) -width 10

    return
}


proc APSMpBoosterPSExitDCStandbyPowerDirectly {args} {

    set BM 0
    set QF 0
    set QD 0
    set SF 0
    set SD 0
    set setStandby 0
    set finalGain 0
    APSStrictParseArguments {BM QF QD SF SD setStandby finalGain}
    
    APSMpStep "Select Booster Power Supplies" -promptDialog APSMpBoosterPSExitDCStandbyPowerDirectlyDialog -vars {BM QF QD SF SD setStandby finalGain}


    if {[catch {APSMpBoosterPSExitDCStandbyPower -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD -setStandby $setStandby -finalGain $finalGain} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSExitDCStandbyPowerDirectly: Problem occurred while setting to standby power\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSExitDCStandbyPowerDirectly: $results"
        }
    }


    APSMpReturn ok
}

# Procedure gives information overview for APSMpBoosterPSSetDCStandbyPower.

proc APSMpBoosterPSExitDCStandbyPowerInfo {} {

    return "This program disables the ramps. Next it loads the safety ramp.\
Finally it re-enables the ramps."
}

# Procedure provides a dialogbox to select booster magnet supplies.

proc APSMpBoosterPSExitDCStandbyPowerDialog {frame} {

    APSSetVarAndUpdate status "Select booster power supplies..."

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

    return
}

proc APSMpBoosterPSExitDCStandbyPower {args} {

    set BM 0
    set QF 0
    set QD 0
    set SF 0
    set SD 0
    set setStandby 1
    set finalGain .4
    APSStrictParseArguments {BM QF QD SF SD setStandby finalGain}
    
    APSMpStep "Select Booster Power Supplies" -promptDialog APSMpBoosterPSExitDCStandbyPowerDialog -vars {BM QF QD SF SD setStandby finalGain}

    set checked 0
    foreach magnet "BM QF QD SF SD" {
	if {[set $magnet] == 1} {
	    set checked 1
	}
    }
    if {!$checked} { 
	APSMpReturn ok
    }

    if {$setStandby} {
        #Set to standby power
	APSMpStep "Setting to standby power" -proc APSMpBoosterPSSetStandbyPower
	if {[catch {APSMpBoosterPSSetStandbyPower -BM $BM -QF $QF -QD $QD -SF-U $SF -SD $SD} results]} {
            if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSExitDCStandbyPower: Problem occurred while setting to standby power\n$results"] == 1} {
                APSMpReturn error "APSMpBoosterPSExitDCStandbyPower: $results"
            }
	}
        APSWaitWithUpdate -waitSeconds 1
    }

    #Set gain to zero
    APSMpStep  "Setting gain to 0" -proc APSMpBoosterPSSetGain
    if {[catch {APSMpBoosterPSSetGain -value 0 -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSExitDCStandbyPower: Problem occurred while setting gain to zero\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSExitDCStandbyPower: $results"
        }
    }
    APSWaitWithUpdate -waitSeconds 5

    set BMCurrentScale 0.0152590
    set QFCurrentScale 0.015259021896696
    set QDCurrentScale 0.015259021896696
    set SFCurrentScale 0.0022888
    set SDCurrentScale 0.0022888

    set fileName /tmp/[APSTmpString]
    foreach magnet "BM QF QD SF SD" {
	if {[set $magnet] == 1} {
	    if {[catch {exec sddswmonitor $fileName -pvNames=B:${magnet}:CurrentWF -steps=1 -erase -dataType=long -scalar=/home/helios/oagData/booster/ramps/DCRamps/BoosterRampOffsetAndScale.sdds} results]} {
		APSMpReturn error "APSMpBoosterPSExitDCStandbyPower: $results"
	    }
	    if {[catch {exec sddsprocess $fileName -pipe=out "-define=col,${magnet}current,B:${magnet}:CurrentWF B:${magnet}:SlopeInterceptSUB.K * B:${magnet}:SlopeInterceptSUB.L +" "-redefine=col,${magnet}current,${magnet}current  [set ${magnet}CurrentScale] *" "-process=${magnet}current,ave,average" | sdds2stream -pipe=in -parameter=average} average]} {
		APSMpReturn error "APSMpBoosterPSExitDCStandbyPower: $average"
	    }
#	    if {[catch {exec sddsprocess $fileName -pipe=out "-define=col,${magnet}current,B:${magnet}:CurrentWF 32768 +" "-redefine=col,${magnet}current,${magnet}current 32767 > ? ${magnet}current 32767 - : ${magnet}current 32767 + $ " "-redefine=col,${magnet}current,${magnet}current [set ${magnet}CurrentScale] *" "-process=${magnet}current,average,average" | sdds2stream -pipe=in -parameter=average} average]} {
#		APSMpReturn error "APSMpBoosterPSExitDCStandbyPower: $average"
#	    }
	    file delete $fileName
	    if {$magnet == "BM"} {
		if {$average >= 60} {
		    APSMpReturn error "APSMpBoosterPSExitDCStandbyPower: average $magnet output DC current is $average, it should be below 60 amps."
		}
	    } else {
		if {$average >= 10} {
		    APSMpReturn error "APSMpBoosterPSExitDCStandbyPower: average $magnet output DC current is $average, it should be below 10 amps."
		}
	    }
	}
    }

    #Loading safety ramps
    APSMpStep "Loading safety ramps" -proc loadSafetyRamps2
    foreach magnet "BM QF QD SF SD" {
	if {[set $magnet] == 1} {
	    if {[catch {loadSafetyRamps2 -$magnet 1} results]} {
                if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSExitDCStandbyPower: Problem occurred while loading safety ramp for $magnet \n$results"] == 1} {
                    APSMpReturn error "APSMpBoosterPSExitDCStandbyPower: $results"
                }
	    }
	}
    }
    APSWaitWithUpdate -waitSeconds 1

    #Disabling ramps
#    APSMpStep "Disabling ramps" -proc APSMpBoosterPSEnableDisableRamps
#    if {[catch {APSMpBoosterPSEnableDisableRamps -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD -state1 disable} results]} {
#        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSExitDCStandbyPower: Problem occurred while disabling ramps\n$results"] == 1} {
#            APSMpReturn error "APSMpBoosterPSExitDCStandbyPower: $results"
#        }
#    }
#    APSWaitWithUpdate -waitSeconds 1

    #Set FreeWheel PVs to non-DC mode
    APSMpStep "Set FreeWheel PVs to non-DC mode" -proc APSMpBoosterPSSetFreeWheelPVs
    if {[catch {APSMpBoosterPSSetFreeWheelPVs -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD -DC 0} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSExitDCStandbyPower: Problem occurred while setting FreeWheel PVs to non-DC mode\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSExitDCStandbyPower: $results"
        }
    }
    APSWaitWithUpdate -waitSeconds 1
    
    
    #Enable ramps
#    APSMpStep "Enabling ramps" -proc APSMpBoosterPSEnableDisableRamps
#    if {[catch {APSMpBoosterPSEnableDisableRamps -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD -state1 enable} results]} {
#        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSExitDCStandbyPower: Problem occurred while enabling ramps\n$results"] == 1} {
#            APSMpReturn error "APSMpBoosterPSExitDCStandbyPower: $results"
#        }
#    }
#    APSWaitWithUpdate -waitSeconds 1

    #Set gain to .4
    APSMpStep  "Setting gain to ${finalGain}" -proc APSMpBoosterPSSetGain
    if {[catch {APSMpBoosterPSSetGain -value $finalGain -BM $BM -QF $QF -QD $QD -SF $SF -SD $SD} results]} {
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSExitDCStandbyPower: Problem occurred while setting gain value\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSExitDCStandbyPower: $results"
        }
    }

    APSMpReturn ok
}

proc APSMpBoosterPSCheckIfDCStandbyPower {args} {
    set magnet ""
    APSStrictParseArguments {magnet}

    set options ""
    if {$magnet == "BM"} {
	lappend options "It:Ddg5chan0.GATE" 
	lappend options "It:Ddg8chan0.GATE" 
	lappend options "It:Ddg3chan7.GATE"
	lappend options "It:Ddg5chan5.GATE"
        set DCModeValues "Disabled Disabled Disabled Enabled"
        set NonDCModeValues "Enabled Enabled Enabled Enabled"
    }
    if {$magnet == "QF"} {
	lappend options "It:Ddg5chan1.GATE" 
	lappend options "It:Ddg6chan1.GATE" 
	lappend options "It:Ddg8chan3.GATE"
	lappend options "It:Ddg8chan2.GATE"
        set DCModeValues "Disabled Disabled Disabled Enabled"
        set NonDCModeValues "Enabled Enabled Enabled Enabled"
    }
    if {$magnet == "QD"} {
	lappend options "It:Ddg5chan2.GATE" 
	lappend options "It:Ddg8chan1.GATE" 
	lappend options "It:Ddg8chan5.GATE"
	lappend options "It:Ddg8chan4.GATE"
        set DCModeValues "Disabled Disabled Disabled Enabled"
        set NonDCModeValues "Enabled Enabled Enabled Enabled"
    }
    if {$magnet == "SF"} {
	lappend options "It:Ddg5chan3.GATE" 
	lappend options "It:Ddg9chan0.GATE"
        set DCModeValues "Disabled Disabled"
        set NonDCModeValues "Enabled Enabled"
    }
    if {$magnet == "SD"} {
	lappend options "It:Ddg5chan4.GATE" 
	lappend options "It:Ddg9chan1.GATE"
        set DCModeValues "Disabled Disabled"
        set NonDCModeValues "Enabled Enabled"
    }
    set options [join $options ,]
    APSMpStep "Reading timing GATE values"
    if {[catch {APScavget -list=$options} modeValues]} {
	APSMpReturn error "APSMpBoosterPSCheckIfDCStandbyPower: $modeValues"
    }
    set modeValues [join $modeValues]
    
    #Check if the waveform is in DC (slope of zero)
    APSMpStep "Checking to see if the waveform has a slope of zero"
    set fileName /tmp/[APSTmpString]
    if {[catch {exec sddswmonitor -pvNames=B:${magnet}:VoltageRefWF $fileName -steps=1} results]} {
	APSMpReturn error "APSMpBoosterPSCheckIfDCStandbyPower: $results"
    }
    if {[catch {exec sddsprocess $fileName -pipe=out -filter=column,Index,200,400 -process=B:${magnet}:VoltageRefWF,slope,slope,functionOf=Index | sdds2stream -pipe -parameters=slope} slope]} {
	APSMpReturn error "APSMpBoosterPSCheckIfDCStandbyPower: $slope"
    }
    file delete $fileName
    if {($slope > -.05) && ($slope < .05)} {
        if {[catch {APScavget -list=B:${magnet}:VoltageRT.GAIN} gain]} {
            APSMpReturn error "APSMpBoosterPSCheckIfDCStandbyPower: $gain"
        }
        if {$gain > .4} {
            #Waveform is in DC but the gain is greater than .4
            APSMpStep "$magnet waveform is in DC but the gain is greater than .4"
            APSMpReturn error "$magnet waveform is in DC but the gain is greater than .4"
        }
        if {$modeValues != $DCModeValues} {
            #Mode PVs do not match what is expected
            APSMpStep "Unable to determine if the $magnet PS is in DC mode. Waveform is flat."
            APSMpReturn error "Unable to determine if the $magnet PS is in DC mode. Waveform is flat but the regulator PVs are not set for DC mode"
        }
        #Waveform is in DC
	APSMpReturn ok 1        
    } else {
        if {$modeValues != $NonDCModeValues} {
            #Mode PVs do not match what is expected
            APSMpStep "Unable to determine if the $magnet PS is in DC mode. Waveform is not flat."
            APSMpReturn error "Unable to determine if the $magnet PS is in DC mode. Waveform is not flat but the regulator PVs are set for DC mode"
        }
        #Waveform is not in DC
        APSMpReturn ok 0
    }
}

proc APSMpBoosterPSCheckForValidRampControlState {args} {
    set magnet ""
    APSStrictParseArguments {magnet}

    set options ""
    if {$magnet == "BM"} {
	lappend options "It:Ddg5chan0.GATE" 
	lappend options "It:Ddg8chan0.GATE" 
	lappend options "It:Ddg3chan7.GATE"
	lappend options "It:Ddg5chan5.GATE"
        set DCModeValues "Disabled Disabled Disabled Enabled"
        set NonDCModeValues "Enabled Enabled Enabled Enabled"
    }
    if {$magnet == "QF"} {
	lappend options "It:Ddg5chan1.GATE" 
	lappend options "It:Ddg6chan1.GATE" 
	lappend options "It:Ddg8chan3.GATE"
	lappend options "It:Ddg8chan2.GATE"
        set DCModeValues "Disabled Disabled Disabled Enabled"
        set NonDCModeValues "Enabled Enabled Enabled Enabled"
    }
    if {$magnet == "QD"} {
	lappend options "It:Ddg5chan2.GATE" 
	lappend options "It:Ddg8chan1.GATE" 
	lappend options "It:Ddg8chan5.GATE"
	lappend options "It:Ddg8chan4.GATE"
        set DCModeValues "Disabled Disabled Disabled Enabled"
        set NonDCModeValues "Enabled Enabled Enabled Enabled"
    }
    if {$magnet == "SF"} {
	lappend options "It:Ddg5chan3.GATE" 
	lappend options "It:Ddg9chan0.GATE"
        set DCModeValues "Disabled Disabled"
        set NonDCModeValues "Enabled Enabled"
    }
    if {$magnet == "SD"} {
	lappend options "It:Ddg5chan4.GATE" 
	lappend options "It:Ddg9chan1.GATE"
        set DCModeValues "Disabled Disabled"
        set NonDCModeValues "Enabled Enabled"
    }
    set options [join $options ,]
    if {[catch {APScavget -list=$options} modeValues]} {
        return 0
	#APSMpReturn error "APSMpBoosterPSCheckIfDCStandbyPower: $modeValues"
    }
    set modeValues [join $modeValues]
    if {($modeValues != $DCModeValues) && ($modeValues != $NonDCModeValues)} {
        #Mode PVs do not match what is expected
        return 0
    }
    if {[catch {exec caget -t B:${magnet}:VoltageRT} rampvalues]} {
        return 0
    }
    set rampvalues [lrange $rampvalues 1 end]
    if {0} {
        if {[catch {exec sdds2stream /home/helios/oagData/booster/ramps/DCRamps/${magnet}Vref.afg100 -col=RampSetpoint} rampdcvalues]} {
            return 0
        }
        set mode DC
        foreach rampvalue $rampvalues rampdcvalue $rampdcvalues {
            if {$rampvalue != [format %f $rampdcvalue]} {
                set mode "Non-DC"
                break
            }
        }
    } else {
        set mode DC
        if {([lindex $rampvalues 0] != [lindex $rampvalues 10]) || ([lindex $rampvalues 0] != [lindex $rampvalues end]) || ([lindex $rampvalues 0] != [lindex $rampvalues end-20])} {
            set mode "Non-DC"
        }
    }
    if {($mode == "DC") && ($modeValues == $NonDCModeValues)} {
        return 0
    }
    if {($mode == "Non-DC") && ($modeValues == $DCModeValues)} {
        return 0
    }
    if {($modeValues != $DCModeValues)} {
        #Non-DC mode found
        return 1
    }
    #DC mode found
    return 2
}



#Loads selected booster magnet safety ramps.

proc loadDCRamps {args} {
    
    global status apsNetworkDomain
    
    set BM 0
    set QF 0
    set QD 0
    set SF 0
    set SD 0
    set magnetlist "BM QF QD SF SD"
    
    APSStrictParseArguments {BM QF QD SF SD}
    
    set listindex 0
    set selectlist ""
    set rampdir "/home/helios/oagData/booster/ramps"
    set notInAutoList ""
    
    if {([string compare $apsNetworkDomain "accel.ntw0rk"] == 0) || ([string compare $apsNetworkDomain "aps4.anl.gov"] == 0)} {
        foreach magnet $magnetlist {
            if {[set $magnet]==1} {
                APSMpStep "Loading DC ramp for $magnet..."
                lappend selectlist $magnet
                if {[catch {APSFileCopy -from ${rampdir}/DCRamps/${magnet}Vref.afg100 -to ${rampdir}/current} results]} {
                    if {[APSPEMTKDialog .warningDialog -text "loadDCRamps: Problem copying file\n$results"] == 1} {
                        APSMpReturn error "loadDCRamps: $results"
                    }                    
                }
                if {[catch {exec rampload ${rampdir}/current/${magnet}Vref.afg100} results]} {
                    if {[APSPEMTKDialog .warningDialog -text "loadDCRamps: Problem occurred while running rampload ${rampdir}/current/${magnet}Vref.afg100\n$results"] == 1} {
                        APSMpReturn error "loadDCRamps: $results"
                    }
                }
                if {[catch {APScavget -list=B:${magnet}:VoltageRT.MODE} autoState]} {
                    APSMpReturn error "loadDCRamps: $autoState"
                }
                if {$autoState!="AUTO"} {
                    lappend notInAutoList $magnet
                }
                APSWaitWithUpdate -waitSeconds 2
            }
            incr listindex
        }
        if {$selectlist!="" && $notInAutoList==""} { 
            APSMpStep "DC ramp(s) for $selectlist loaded."
        } elseif {$selectlist!="" && $notInAutoList!=""} {
            APSMpStep "DC ramp(s) for $selectlist loaded.  Auto mode NOT activated for $notInAutoList."
            APSAlertBox .alert -errorMessage "Warning:  Auto mode NOT activated for $notInAutoList."
        }
    }
    APSMpReturn ok
}

proc APSMpBoosterPSSetFreeWheelPVs {args} {
    set BM 0
    set QF 0
    set QD 0
    set SF 0
    set SD 0
    set DC 0
    APSStrictParseArguments {BM QF QD SF SD DC}

    set options ""
    if {$DC} {
        if {$BM} {
            lappend options "It:Ddg5chan0.GATE=Disabled" 
            lappend options "It:Ddg8chan0.GATE=Disabled" 
            lappend options "It:Ddg3chan7.GATE=Disabled"
            lappend options "It:Ddg5chan5.GATE=Enabled"
        }
        if {$QF} {
            lappend options "It:Ddg5chan1.GATE=Disabled" 
            lappend options "It:Ddg6chan1.GATE=Disabled" 
            lappend options "It:Ddg8chan3.GATE=Disabled"
            lappend options "It:Ddg8chan2.GATE=Enabled"
        }
        if {$QD} {
            lappend options "It:Ddg5chan2.GATE=Disabled" 
            lappend options "It:Ddg8chan1.GATE=Disabled" 
            lappend options "It:Ddg8chan5.GATE=Disabled"
            lappend options "It:Ddg8chan4.GATE=Enabled"
        }
        if {$SF} {
            lappend options "It:Ddg5chan3.GATE=Disabled" 
            lappend options "It:Ddg9chan0.GATE=Disabled"
        }
        if {$SD} {
            lappend options "It:Ddg5chan4.GATE=Disabled" 
            lappend options "It:Ddg9chan1.GATE=Disabled"
        }
    } else {
        if {$BM} {
            lappend options "It:Ddg5chan0.GATE=Enabled" 
            lappend options "It:Ddg8chan0.GATE=Enabled" 
            lappend options "It:Ddg3chan7.GATE=Enabled"
            lappend options "It:Ddg5chan5.GATE=Enabled"
        }
        if {$QF} {
            lappend options "It:Ddg5chan1.GATE=Enabled" 
            lappend options "It:Ddg6chan1.GATE=Enabled" 
            lappend options "It:Ddg8chan3.GATE=Enabled"
            lappend options "It:Ddg8chan2.GATE=Enabled"
        }
        if {$QD} {
            lappend options "It:Ddg5chan2.GATE=Enabled" 
            lappend options "It:Ddg8chan1.GATE=Enabled" 
            lappend options "It:Ddg8chan5.GATE=Enabled"
            lappend options "It:Ddg8chan4.GATE=Enabled"
        }
        if {$SF} {
            lappend options "It:Ddg5chan3.GATE=Enabled" 
            lappend options "It:Ddg9chan0.GATE=Enabled"
        }
        if {$SD} {
            lappend options "It:Ddg5chan4.GATE=Enabled" 
            lappend options "It:Ddg9chan1.GATE=Enabled"
        }
    }
    if {[string length $options] == 0} {
        APSMpReturn ok
    }
    
    set options [join $options ,]
    if {[catch {APScavput -list=$options} results]} {
        if {$DC} {
            set mode "DC"
        } else {
            set mode "non-DC"
        }
        if {[APSPEMTKDialog .warningDialog -text "APSMpBoosterPSSetFreeWheelPVs: Problem occurred while setting FreeWheel PVs to $mode mode\n$results"] == 1} {
            APSMpReturn error "APSMpBoosterPSSetFreeWheelPVs: $results"
        }
    }
    APSMpReturn ok
}













# Procedure gives information overview for APSMpBoosterPSSetDCStandbyPower.

proc APSMpBoosterPSExitDCStandbyPowerInfo {} {

    return "This program disables the ramps. Next it loads the safety ramp.\
Finally it re-enables the ramps."
}

# Procedure provides a dialogbox to select booster magnet supplies.

proc APSMpBoosterPSExitDCStandbyPowerDialog {frame} {

    APSSetVarAndUpdate status "Select booster power supplies..."

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

    return
}


