#
# $Log: not supported by cvs2svn $
# Revision 1.11  2005/04/07 16:14:35  soliday
# Updated to identify limited save files and not allow the operator to use
# them.
#
# Revision 1.10  2004/06/30 14:53:12  soliday
# It now forces the user to select an SCR file.
#
# Revision 1.9  2004/05/14 14:56:29  soliday
# Made changes related to the BPM name changes.
#
# Revision 1.8  2004/01/29 17:04:16  soliday
# Updated with more error/warning dialogs.
#
# Revision 1.7  2003/10/27 20:22:18  soliday
# Replaced existing warning dialogs with calls to APSPEMTKDialog.
# Added logging of the selections on the initial dialog.
#
# Revision 1.6  2002/10/22 16:49:56  soliday
# Changed error message email address
#
# Revision 1.5  2002/10/22 16:42:16  soliday
# Fixed a few bugs in the MCR
#
# Revision 1.4  2002/07/11 20:04:09  soliday
# Added the ability to set the mode on the modulators.
#
# Revision 1.3  2002/05/29 16:52:49  soliday
# Commited changes made during PEM studies.
#
# Revision 1.2  2002/05/23 21:48:30  soliday
# Temporary version.
#
# Revision 1.1  2001/11/06 17:33:26  soliday
# First version.
#
#
#

proc LINAC_WaitForPowerControls {args} {
    set L1 0
    set L2 0
    set L3 0
    set L4 0
    set L5 0
    set suspend 0
    APSStrictParseArguments {L1 L2 L3 L4 L5 suspend}
    set sectors "L1 L2 L3 L4 L5"
    set options ""
    if {$L1} {lappend options L1:KY:DC2}
    if {$L2} {lappend options L2:SD:DC1}
    if {$L3} {lappend options L3:KY:DC2}
    if {$L4} {lappend options L4:SD:DC1}
    if {$L5} {lappend options L5:SD:DC1}
    if {![llength $options]} {
	APSMpReturn ok
    }
    set options "-list=[join $options ,]"
    set i 0
    while {1} {
	if {[catch {exec cavget -list=L1:KY:DC2,L2:SD:DC1,L3:KY:DC2,L4:SD:DC1,L5:SD:DC1 \
			-list=ARF:ErrorM -num -pend=20} errorValues]} {
	    APSMpReturn error "LINAC_WaitForPowerControls: $errorValues"
	}
	set wait 0
	foreach sector $sectors errorValue $errorValues {
	    if {[set $sector]} {
		if {[expr abs($errorValue)] > 1000e3} {
		    set wait 1
		    break
		}
	    }
	}
	if {$i > 60} {
	    break
	}
	if {$wait} {
	    APSMpStep "Wait for RF power errors to minimize"
	    incr i 5
	    APSWaitWithUpdate -waitSeconds 5
	} else {
	    break
	}
    }
    set sec ""
    foreach sector $sectors errorValue $errorValues {
	if {[set $sector]} {
	    if {[expr abs($errorValue)] > 1000e3} {
		append sec "$sector "
	    }
	}
    }
    if {[llength $sec]} {
        if {[APSPEMTKDialog .warningDialog -text "RF power errors are greater than 1000e3 in sector(s) $sec"] == 1} {
	    APSMpReturn error "LINAC_WaitForPowerControls: RF power errors are greater than 1000e3 in sector(s) $sec"
	}	
    }

    APSMpReturn ok
}
