#
# $Log: not supported by cvs2svn $
# Revision 1.9  2007/10/31 16:01:34  soliday
# Added the option to ignore the linac beam rate. This is needed when running
# the laser without the PC Gun.
#
# Revision 1.8  2007/06/25 19:57:53  soliday
# Updated to check if cavget returns ? when getting the thermocouple values.
#
# Revision 1.7  2007/04/04 15:18:28  soliday
# Added an extra check of the BWD at the end of the startup procedure.
#
# Revision 1.6  2007/04/04 15:09:15  soliday
# Modified so that it enables the Test Stand RF Laser Trigger.
#
# Revision 1.5  2007/04/03 17:11:34  soliday
# Updated code to start and stop laser.
#
# Revision 1.4  2005/09/26 14:54:06  shang
# added prompting dialog to LASER_StartupFromStandby pem
#
# Revision 1.3  2004/01/29 17:04:16  soliday
# Updated with more error/warning dialogs.
#
# Revision 1.2  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.1  2002/02/20 20:24:35  soliday
# First version.
#
#
#

proc LASER_StartupFromStandbyInfo {} {

    return "This procedure brings up the LEUTL drive laser up from standby (i.e. between runs). This procedure assumes the laser has been previously placed in standby. This procedure is not recommended after an IOC reboot, power disruption or LACIS trip."

}

proc LASER_StartupFromStandby {args} {
    global execMode
    
    APSMpStep init -promptDialog APSMpPromptInitDialog
    if {$execMode == "Manual"} {
	set prefix "Press Continue to "
    } else {
	set prefix ""
    }
    package require Itcl
    package require Itk
    package require iwidgets

    if [catch {APSLinkToLINACPVs} result] {
	APSMpReturn error "LASER_StartupFromStandby: $result"
    }
    
    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Verify that the LACIS is in Danger mode" -desc "Verify that the LACIS is in Danger mode"
        if [catch {APSDevSend \
                     -group LinacLaser \
                     -operation QueryLACISStatusDanger \
                     -deviceList 0} result] {
            APSMpReturn error "LASER_StartupFromStandby: $result"
        }
        if {!$result} {
            for {set i 0} {$i < 3} {incr i} {bell}
            if {[APSPEMTKDialog .warningDialog -text "LACIS is not in Danger mode."] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: LACIS is not in Danger mode"
            }
        } else {
            set fail 0
        }
    }

    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Verify that LACIS Emergency Off is OK" -desc "Verify that LACIS Emergency Off is OK"
        if [catch {APSDevSend \
                     -group LinacLaser \
                     -operation QueryLACISEmergencyOffOK \
                     -deviceList 0} result] {
            APSMpReturn error "LASER_StartupFromStandby: $result"
        }
        if {!$result} {
            for {set i 0} {$i < 3} {incr i} {bell}
            if {[APSPEMTKDialog .warningDialog -text "LACIS Emergency Off is not in OK mode."] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: LACIS Emergency Off is not OK mode"
            }
        } else {
            set fail 0
        }
    }

    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Verify that LACIS AC is On" -desc "Verify that LACIS AC is On"
        if [catch {APSDevSend \
                     -group LinacLaser \
                     -operation QueryLACISACStatusOn \
                     -deviceList 0} result] {
            APSMpReturn error "LASER_StartupFromStandby: $result"
        }
        if {!$result} {
            for {set i 0} {$i < 3} {incr i} {bell}
            if {[APSPEMTKDialog .warningDialog -text "LACIS AC Status is not On."] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: LACIS AC Status is not On"
            }
        } else {
            set fail 0
        }
    }

    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Verify that the LACIS Door Status is OK" -desc "Verify that the LACIS Door Status is OK"
        if [catch {APSDevSend \
                     -group LinacLaser \
                     -operation QueryLACISDoorStatusOK \
                     -deviceList 0} result] {
            APSMpReturn error "LASER_StartupFromStandby: $result"
        }
        if {!$result} {
            for {set i 0} {$i < 3} {incr i} {bell}
            if {[APSPEMTKDialog .warningDialog -text "LACIS Door Status is not OK."] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: LACIS Door Status is not OK"
            }
        } else {
            set fail 0
        }
    }

    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Verify that the LACIS Start Status is ready" -desc "Verify that the LACIS Start Status is ready"
        if [catch {APSDevSend \
                     -group LinacLaser \
                     -operation QueryLACISStartStatusOK \
                     -deviceList 0} result] {
            APSMpReturn error "LASER_StartupFromStandby: $result"
        }
        if {!$result} {
            for {set i 0} {$i < 3} {incr i} {bell}
            if {[APSPEMTKDialog .warningDialog -text "LACIS Start Status is not ready."] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: LACIS Start Status is not ready"
            }
        } else {
            set fail 0
        }
    }

    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Check shutter controls for tripped condition" -desc "Check shutter controls for tripped condition"
        if {[catch {APScavget -list=L:LR:shutter1IntM,L:LR:shutter2IntM,L:LR:shutter3IntM,L:LR:shutter4IntM,L:LR:GV1:shutterCheckCO.B,L:LR:GV1:shutterCheckCO.C -num -pend=20} results]} {
            APSMpReturn error "LASER_StartupFromStandby: $results"
        }
        if {[join [split $results \n]] != "1 1 1 1 1 0"} {
            for {set i 0} {$i < 3} {incr i} {bell}
            if {[APSPEMTKDialog .warningDialog -text "Shutter controls are in tripped condition"] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: Shutter controls are in tripped condition"
            } else {
                break
            }
        } else {
            set fail 0
        }
    }
    
    APSMpStep "${prefix}Check which shutters are not closed" -desc "Check which shutters are not closed"
    if {[catch {APScavget -list=L:LR:shutter1M,L:LR:shutter2M,L:LR:shutter3M,L:LR:shutter4M,L:LR:GV1:shutterM -num -pend=20} results]} {
	APSMpReturn error "LASER_StartupFromStandby: $results"
    }
    set deviceList ""
    foreach result $results expected "0 0 0 0 1" device "L:LR:shutter1C L:LR:shutter2C L:LR:shutter3C L:LR:shutter4C L:LR:GV1:shutterC" {
    	if {$result != $expected} {
            lappend deviceList $device
        }    
    }
    if {[llength $deviceList]} {
        APSMpStep "${prefix}Close shutters" -desc "Close shutters"
        if {[catch {APScavput -list=[join $deviceList ,]=0 -num -pend=20} results]} {
            if {[APSPEMTKDialog .warningDialog -text "Error closing shutters./n$result"] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: $result"
            }
        }
        APSWaitWithUpdate -waitSeconds 4
        
        set fail 1
        while {$fail} {
            APSMpStep "${prefix}Verify that all the shutters are closed" -desc "Verify that all the shutters are closed"
            if {[catch {APScavget -list=L:LR:shutter1M,L:LR:shutter2M,L:LR:shutter3M,L:LR:shutter4M,L:LR:GV1:shutterM -num -pend=20} results]} {
                APSMpReturn error "LASER_StartupFromStandby: $results"
            }
            set problem 0
            foreach result $results expected "0 0 0 0 1" {
                if {$result != $expected} {
                    set problem 1
                }
            }
            if {$problem} {
                if {[APSPEMTKDialog .warningDialog -text "Unable to close all the LACIS shutters."] == 1} {
                    APSMpReturn error "LASER_StartupFromStandby: Unable to close all the LACIS shutters"
                }
            } else {
                set fail 0
            }
        }
    }

    APSMpStep "${prefix}Enable Test Stand RF Trigger" -desc "Enable Test Stand RF Trigger"
    if [catch {APSDevSend -group LinacLaser -operation SetLaserTriggerTestStandRF -deviceList 0} result] {
        if {[APSPEMTKDialog .warningDialog -text "Test Stand RF Trigger is not ready."] == 1} {
	    APSMpReturn error "LASER_StartupFromStandby: Test Stand RF Trigger is not ready"
	}
    }

    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Check Beam Rate" -desc "Check Beam Rate"
        if {[catch {APScavget -list=LI:TM:itsBeamRateCC,LI:TM:gunTrigCountCC -num -pend=20} results]} {
            APSMpReturn error "LASER_StartupFromStandby: $results"
        }
        set problem 0
        foreach result $results expected "6 6" {
            if {$result != $expected} {
                set problem 1
            }
        }
        if {$problem} {
            set results [APSPEMTKDialog .warningDialog -text "Beam rate is not set to 6Hz" -strings {"Recheck" "Ignore" "Abort Procedure"}]
            if {$results == 2} {
                APSMpReturn error "LASER_StartupFromStandby: Beam rate is not set to 6Hz"
            }
            if {$results == 1} {
                set fail 0
            }
        } else {
            set fail 0
        }
    }

    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Check L6 RF Rate" -desc "Check L6 RF Rate"
        if {[catch {APScavget -list=LI:TM:localModulatorRateAO,LI:TM:asyncRfRateMeasM -num -pend=20} results]} {
            APSMpReturn error "LASER_StartupFromStandby: $results"
        }
        set problem 0
        foreach result $results expected "6.0 6" {
            if {$result != $expected} {
                set problem 1
            }
        }
        if {$problem} {
            if {[APSPEMTKDialog .warningDialog -text "L6 RF rate is not set to 6Hz"] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: L6 RF rate is not set to 6Hz"
            }
        } else {
            set fail 0
        }
    }

    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Check H2O Flow" -desc "Check H2O Flow"
        if {[catch {APScavget -list=L:LR:ChilledWaterFlow -num -pend=20} results]} {
            APSMpReturn error "LASER_StartupFromStandby: $results"
        }
        if {$results < 2.5} {
            if {[APSPEMTKDialog .warningDialog -text "H2O flow is less than 2.5gpm"] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: H2O flow is less than 2.5gpm"
            }
        } else {
            set fail 0
        }
    }

    APSMpStep "${prefix}Enable HV Power Supply" -desc "Enable HV Power Supply"
    if [catch {exec cavput -list=L:LR:REGEN:hvOnC=1} results] {
    }
    APSWaitWithUpdate -waitSeconds 1
    if [catch {exec cavput -list=L:LR:REGEN:hvOnC=0} results] {
    }
    APSWaitWithUpdate -waitSeconds 1
    if [catch {exec cavput -list=L:LR:REGEN:hvOnC=1} results] {
    }

    set tries 1
    while {1} {
	APSWaitWithUpdate -waitSeconds 5
	APSMpStep "${prefix}Verify that amplifier is firing by looking at the end of charge indicator" -desc "Verify that amplifier is firing by looking at the end of charge indicator"
	if [catch {APSDevSend \
		       -group LinacLaser \
		       -operation QueryLRREGENendOfChargeNoAlarm \
		       -deviceList 0} result] {
	    APSMpReturn error "LASER_StartupFromStandby: $result"
	}
	if {!$result} {
	    if {$tries == 5} {
		for {set i 0} {$i < 3} {incr i} {bell}
                if {[APSPEMTKDialog .warningDialog -text "The amplifier is not firing"] == 1} {
		    APSMpReturn error "LASER_StartupFromStandby: The amplifier is not firing"
		}
		set tries 1
	    } else {
                if [catch {exec cavput -list=L:LR:REGEN:hvOnC=0} results] {
                }
                APSWaitWithUpdate -waitSeconds 1
                if [catch {exec cavput -list=L:LR:REGEN:hvOnC=1} results] {
                }
                incr tries
            }
	} else {
	    break
	}
    }

    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Verify that all REGEN Amp is in Caution Mode" -desc "Verify that all REGEN Amp is in Caution Mode"
        if [catch {APSDevSend \
                     -group LinacLaser \
                     -operation QueryLRREGENAmpCaution \
                     -deviceList 0} result] {
            APSMpReturn error "LASER_StartupFromStandby: $result"
        }
        if {!$result} {
            for {set i 0} {$i < 3} {incr i} {bell}
            if {[APSPEMTKDialog .warningDialog -text "REGEN Amp is not in Caution Mode."] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: REGEN Amp is not in Caution Mode"
            }
        } else {
            set fail 0
        }
    }

    APSMpStep "${prefix}Turn on LASER Oscillator" -desc "Turn on LASER Oscillator"
    if [catch {APSDevSend \
		   -group LinacLaser \
		   -operation TurnOnLROscillator \
		   -deviceList 0} result] {
        if {[APSPEMTKDialog .warningDialog -text "Error turning on the LASER oscillator./n$result"] == 1} {
	    APSMpReturn error "LASER_StartupFromStandby: $result"
	}
    }
    APSWaitWithUpdate -waitSeconds 5
    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Verify that Oscillator turns on" -desc "Verify that Oscillator turns on"
        if [catch {APSDevSend \
                     -group LinacLaser \
                     -operation QueryLROscillatorOn \
                     -deviceList 0} result] {
            APSMpReturn error "LASER_StartupFromStandby: $result"
        }
        if {!$result} {
            if [catch {APSDevSend \
                         -group LinacLaser \
                         -operation QueryLROscillatorCaution \
                         -deviceList 0} result] {
                APSMpReturn error "LASER_StartupFromStandby: $result"
            }
            if {!($result)} {
                for {set i 0} {$i < 3} {incr i} {bell}
                if {[APSPEMTKDialog .warningDialog -text "Unable to turn on LASER Oscilator."] == 1} {
                    APSMpReturn error "LASER_StartupFromStandby: Unable to turn on LASER Oscilator"
                }
            }
        } else {
            set fail 0
        }
    }

    APSMpStep "${prefix}Wait up to 60 minutes for head thermocouples to reach 81 and 80 degrees" -desc "Wait up to 60 minutes for head thermocouples to reach 81 and 80 degrees"
    global apsLASER_StartupFromStandbyDialog
    set apsLASER_StartupFromStandbyDialog 0
    APSDialogBox .waitDialog \
	-name "LASER Startup Wait Dialog" \
	-okCommand "set apsLASER_StartupFromStandbyDialog 1" \
	-cancelCommand "set apsLASER_StartupFromStandbyDialog 2"
    .waitDialog.buttonRow.ok.button configure -text Continue
    .waitDialog.buttonRow.cancel.button configure -text Abort
    pack [label .waitDialog.userFrame.label -text "Waiting for thermocouples to reach 81 and 80 degrees\nClick Continue to skip 60 minute wait"]
    iwidgets::feedback .waitDialog.userFrame.fb -labeltext Status -steps 3600
    pack .waitDialog.userFrame.fb -padx 10 -pady 10 -fill both -expand true
    
    set initstep [clock seconds]
    set step $initstep
    set laststep [expr {$initstep + 3600}]
    while {$step < $laststep} {
	if {[catch {APScavget -list=L:LR:TC:K:temp2M,L:LR:TC:K:temp3M -pend=20} results]} {
	    APSMpReturn error "LASER_StartupFromStandby: Unable to read thermocouple values: $results"
	}
	set pass 1
        if {([lindex $results 0] == "?") || ([lindex $results 1] == "?")} {
            set pass 0
	} elseif {([lindex $results 0] < 81) || ([lindex $results 1] < 80)} {
	    set pass 0
	}
	if {$pass} {
	    break
	} else {
	    if {$apsLASER_StartupFromStandbyDialog == 2} {
		APSMpReturn error "LASER_StartupFromStandby: Aborted by user while waiting for warmup"
	    } elseif {$apsLASER_StartupFromStandbyDialog == 1} {
		break
	    }
	    after 1000
	    set s [clock seconds]
	    for {set i $step} {$i < $s} {incr i} {
		catch {.waitDialog.userFrame.fb step}
	    }
	    set step $s
	    update
	}
    }

#    APSWaitWithUpdate -waitSeconds 900 -abortVariable apsLASER_StartupFromStandbyDialog -updateCommand {catch {.waitDialog.userFrame.fb step}}
    destroy .waitDialog

    if {$apsLASER_StartupFromStandbyDialog == 2} {
	APSMpReturn error "LASER_StartupFromStandby: Aborted by user while waiting for warmup"
    }

    if {$step >= $laststep} {
        if {[APSPEMTKDialog .warningDialog -text "Laser head thermocouples are not reaching 81 and 80 degrees which is their operating temperature"] == 1} {
	    APSMpReturn error "LASER_StartupFromStandby: Laser head thermocouples are not reaching 81 and 80 degrees which is their operating temperature"
	}
    }
    

    APSMpStep "${prefix}Open shutter 1 (Oscillator)" -desc "Open shutter 1 (Oscillator)"
    if [catch {APSDevSend \
		   -group LinacLaser \
		   -operation OpenLRShutter \
		   -deviceList 1} result] {
        if {[APSPEMTKDialog .warningDialog -text "Error opening shutter 1 (oscillator)./n$result"] == 1} {
	    APSMpReturn error "LASER_StartupFromStandby: $result"
	}
    }
    APSWaitWithUpdate -waitSeconds 2

    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Verify that shutter 1 is open" -desc "Verify that shutter 1 is open"
        if [catch {APSDevSend \
                     -group LinacLaser \
                     -operation QueryLRShutterOpen \
                     -deviceList 1} result] {
            APSMpReturn error "LASER_StartupFromStandby: $result"
        }
        if {!($result)} {
            for {set i 0} {$i < 3} {incr i} {bell}
            if {[APSPEMTKDialog .warningDialog -text "Unable to open LACIS shutter 1."] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: Unable to open LACIS shutter 1"
            }
        } else {
            set fail 0
        }
    }

    APSMpStep "${prefix}Wait for Oscillator Phase Lock" -desc "Wait for Oscillator Phase Lock"
    set time [expr [clock seconds] + 40]
    while {1} {
	if [catch {APSDevSend \
		       -group LinacLaser \
		       -operation QueryLROscillatorPhaseLocked \
		       -deviceList 0} result] {
	    APSMpReturn error "LASER_StartupFromStandby: $result"
	}
	if {$result} {
	    break
	} elseif {[clock seconds] > $time} {
	    for {set i 0} {$i < 3} {incr i} {bell}
            if {[APSPEMTKDialog .warningDialog -text "Oscillator Phase Locked is not ready."] == 1} {
		APSMpReturn error "LASER_StartupFromStandby: Oscillator Phase Locked is not ready"
	    }
	} else {
	    APSWaitWithUpdate -waitSeconds 1
	}
    }

    APSMpStep "${prefix}Reset the BWD" -desc "Reset the BWD"
    if [catch {APSDevSend \
		   -group LinacLaser \
		   -operation ResetLRBWDetector \
		   -deviceList 0} result] {
        if {[APSPEMTKDialog .warningDialog -text "Error resetting the BWD./n$result"] == 1} {
	    APSMpReturn error "LASER_StartupFromStandby: $result"
	}
    }
    APSWaitWithUpdate -waitSeconds 10

    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Verify that the BWD / Stretcher go green" -desc "Verify that the BWD / Stretcher go green"
	if {[catch {APScavget -list=L:LR:REGEN:bwDetectorTripM -num -pend=20} result]} {
	    APSMpReturn error "LASER_StartupFromStandby: $result"
	}
        if {!$result} {
            for {set i 0} {$i < 3} {incr i} {bell}
            if {[APSPEMTKDialog .warningDialog -text "BWD / Stretcher did not go green."] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: BWD / Stretcher did not go green"
            }
        } else {
            set fail 0
        }
    }

    set fail 1
    while {$fail} {
        APSMpStep "${prefix}Verify that REGEN Amp is on" -desc "Verify that REGEN Amp is on"
        if [catch {APSDevSend \
                     -group LinacLaser \
                     -operation QueryLRREGENAmpOn \
                     -deviceList 0} result] {
            APSMpReturn error "LASER_StartupFromStandby: $result"
        }
        if {!($result)} {
            for {set i 0} {$i < 3} {incr i} {bell}
            if {[APSPEMTKDialog .warningDialog -text "REGEN Amp is not on."] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: REGEN Amp is not on"
            }
        } else {
            set fail 0
        }
    }

    APSMpStep "${prefix}Open shutter 2 (Amplifier)" -desc "Open shutter 2 (Amplifier)"
    if [catch {APSDevSend \
		   -group LinacLaser \
		   -operation OpenLRShutter \
		   -deviceList 2} result] {
        if {[APSPEMTKDialog .warningDialog -text "Error opening shutter 2 (amplifier)./n$result"] == 1} {
	    APSMpReturn error "LASER_StartupFromStandby: $result"
	}
    }
    APSWaitWithUpdate -waitSeconds 2

    APSMpStep "${prefix}Verify that shutter 2 is open" -desc "Verify that shutter 2 is open"
    if [catch {APSDevSend \
		   -group LinacLaser \
		   -operation QueryLRShutterOpen \
		   -deviceList 2} result] {
	APSMpReturn error "LASER_StartupFromStandby: $result"
    }
    if {!($result)} {
	for {set i 0} {$i < 3} {incr i} {bell}
        if {[APSPEMTKDialog .warningDialog -text "Unable to open LACIS shutter 2."] == 1} {
	    APSMpReturn error "LASER_StartupFromStandby: Unable to open LACIS shutter 2"
	}
    }

    APSMpStep "${prefix}Open shutter 4 (Amplifier)" -desc "Open shutter 4 (Amplifier)"
    if [catch {APSDevSend \
		   -group LinacLaser \
		   -operation OpenLRShutter \
		   -deviceList 4} result] {
        if {[APSPEMTKDialog .warningDialog -text "Error opening shutter 4 (amplifier)./n$result"] == 1} {
	    APSMpReturn error "LASER_StartupFromStandby: $result"
	}
    }
    APSWaitWithUpdate -waitSeconds 2

    APSMpStep "${prefix}Verify that shutter 4 is open" -desc "Verify that shutter 4 is open"
    if [catch {APSDevSend \
		   -group LinacLaser \
		   -operation QueryLRShutterOpen \
		   -deviceList 4} result] {
	APSMpReturn error "LASER_StartupFromStandby: $result"
    }
    if {!($result)} {
	for {set i 0} {$i < 3} {incr i} {bell}
        if {[APSPEMTKDialog .warningDialog -text "Unable to open LACIS shutter 4."] == 1} {
	    APSMpReturn error "LASER_StartupFromStandby: Unable to open LACIS shutter 4"
	}
    }

    APSMpStep "${prefix}Open ACIS shutter" -desc "Open ACIS shutter"
    if {[catch {APScavput -list=L:LR:GV1:shutterC=1 -num -pend=20} results]} {
	if {[APSPEMTKDialog .warningDialog -text "Error opening ACIS shutter./n$result"] == 1} {
	    APSMpReturn error "LASER_StartupFromStandby: $result"
	}
    }

    APSMpStep "${prefix}Pause 30 seconds before checking BWD" -desc "Pause 30 seconds before checking BWD"
    APSWaitWithUpdate -waitSeconds 30
    

    APSMpStep "${prefix}Check BWD" -desc "Check BWD"
    if {[catch {APScavget -list=L:LR:REGEN:bwDetectorTripM -num -pend=20} result]} {
        APSMpReturn error "LASER_StartupFromStandby: $result"
    }
    if {!$result} {
        APSMpStep "${prefix}Reset the BWD" -desc "Reset the BWD"
        if [catch {APSDevSend \
                     -group LinacLaser \
                     -operation ResetLRBWDetector \
                     -deviceList 0} result] {
            if {[APSPEMTKDialog .warningDialog -text "Error resetting the BWD./n$result"] == 1} {
                APSMpReturn error "LASER_StartupFromStandby: $result"
            }
        }
        APSWaitWithUpdate -waitSeconds 10
        
        set fail 1
        while {$fail} {
            APSMpStep "${prefix}Verify that the BWD / Stretcher go green" -desc "Verify that the BWD / Stretcher go green"
            if {[catch {APScavget -list=L:LR:REGEN:bwDetectorTripM -num -pend=20} result]} {
                APSMpReturn error "LASER_StartupFromStandby: $result"
            }
            if {!$result} {
                for {set i 0} {$i < 3} {incr i} {bell}
                if {[APSPEMTKDialog .warningDialog -text "BWD / Stretcher did not go green."] == 1} {
                    APSMpReturn error "LASER_StartupFromStandby: BWD / Stretcher did not go green"
                }
            } else {
                set fail 0
            }
        }
    }



    APSMpReturn ok
}

proc LASER_BringDownToStandbyInfo {} {

    return "This procedure brings down the LEUTL drive laser to standby."

}

proc LASER_BringDownToStandby {args} {
    global execMode
    
    APSMpStep init -promptDialog APSMpPromptInitDialog
    if {$execMode == "Manual"} {
	set prefix "Press Continue to "
    } else {
	set prefix ""
    }

    if [catch {APSLinkToLINACPVs} result] {
	APSMpReturn error "LASER_StartupFromStandby: $result"
    }

    APSMpStep "${prefix}Check which shutters are not closed" -desc "Check which shutters are not closed"
    if {[catch {APScavget -list=L:LR:shutter1M,L:LR:shutter2M,L:LR:shutter3M,L:LR:shutter4M,L:LR:GV1:shutterM -num -pend=20} results]} {
	APSMpReturn error "LASER_BringDownToStandby: $results"
    }
    set deviceList ""
    foreach result $results expected "0 0 0 0 1" device "L:LR:shutter1C L:LR:shutter2C L:LR:shutter3C L:LR:shutter4C L:LR:GV1:shutterC" {
    	if {$result != $expected} {
            lappend deviceList $device
        }    
    }
    if {[llength $deviceList]} {
        APSMpStep "${prefix}Close shutters" -desc "Close shutters"
        if {[catch {APScavput -list=[join $deviceList ,]=0 -num -pend=20} results]} {
            if {[APSPEMTKDialog .warningDialog -text "Error closing shutters./n$result"] == 1} {
                APSMpReturn error "LASER_BringDownToStandby: $result"
            }
        }
        APSWaitWithUpdate -waitSeconds 4
        
        set fail 1
        while {$fail} {
            APSMpStep "${prefix}Verify that all the shutters are closed" -desc "Verify that all the shutters are closed"
            if {[catch {APScavget -list=L:LR:shutter1M,L:LR:shutter2M,L:LR:shutter3M,L:LR:shutter4M,L:LR:GV1:shutterM -num -pend=20} results]} {
                APSMpReturn error "LASER_BringDownToStandby: $results"
            }
            set problem 0
            foreach result $results expected "0 0 0 0 1" {
                if {$result != $expected} {
                    set problem 1
                }
            }
            if {$problem} {
                if {[APSPEMTKDialog .warningDialog -text "Unable to close all the LACIS shutters."] == 1} {
                    APSMpReturn error "LASER_BringDownToStandby: Unable to close all the LACIS shutters"
                }
            } else {
                set fail 0
            }
        }
    }

    APSMpStep "${prefix}Disable HV Power Supply" -desc "Enable HV Power Supply"
    if [catch {exec cavput -list=L:LR:REGEN:hvOnC=0} results] {
    }
    APSWaitWithUpdate -waitSeconds 1
    APSMpStep "${prefix}Set LASER Oscillator to Standby" -desc "Turn on LASER Oscillator"
    if [catch {APSDevSend \
		   -group LinacLaser \
		   -operation SetStandbyLROscillator \
		   -deviceList 0} result] {
        if {[APSPEMTKDialog .warningDialog -text "Error setting the LASER oscillator to standby./n$result"] == 1} {
	    APSMpReturn error "LASER_BringDownToStandby: $result"
	}
    }

    APSMpReturn ok
}
