proc APSMpPG1_SwitchToL3DownInfo {} {
    return "This is a Linac Waveguide Switch procedure. This procedure will first attenuate L3 rf power followed by taking its modulator to ready-to-pulse. Then it will position S-band switch 4, to position 2 restoring (Mode 0)or K3 Down. See Linac Waveguide Switches MEDM Display for position chart and rack indication. MEDM display (ACIS_LP_WG.adl) for details."
}

proc APSMpPG1_SwitchToL3DownDialog {} {
    pack [message $frame.msg -aspect 300 -justify left -text "Click OK to execute."]
    return
}

proc APSMpPG1_SwitchToL3Down {args} {
    set gun RG2
    APSStrictParseArguments {gun}

    APSMpStep init -promptDialog APSMpPromptInitDialog
    
#    if {[catch {APScavget -list=ACIS:GB_BP_MD -num -pend=20} gunRoomStatus]} {
#	APSMpReturn error "APSMpPG1_SwitchToL3Down: $gunRoomStatus"
#    }
#    if {$gunRoomStatus != 1} {
#	if {[APSPEMTKDialog .warningDialog -text "The Gun Room status is not in Beam Permit mode. ACIS:GB_BP_MD=$gunRoomStatus"] == 1} {
#	    APSMpReturn error "APSMpPG1_SwitchToL3Down: The Gun Room status is not in Beam Permit mode. ACIS:GB_BP_MD=$gunRoomStatus"
#	}
#    }

    APSMpStep "Close L1:PC1:GV2 gate valve" 
    catch {exec cavput -list=L1:PC1:GV2:closeCmdBO=1}
    after 1000

    APSMpStep "Shutdown till ready to pulse is off in L3" -desc "Shutdown till ready to pulse is off" -proc LMOCS_ShutdownTillReadyToPulseOff
    if {[catch {LMOCS_ShutdownTillReadyToPulseOff -sector L3} result]} {
        if {[APSPEMTKDialog .warningDialog -text "Error while shutting down to ready to pulse is off in L3 \n$result"] == 1} {
            APSMpReturn error "LMOCS_ShutdownTillReadyToPulseOff: $result"
        }
    }

    APSMpStep "Requesting remote switch control"
    if {[catch {APScavput -list=LRFSCS:MCR:RemoteRequest=1 -pend=20} result]} {
	if {[APSPEMTKDialog .warningDialog -text "Problem requesting remote switching control: $result"] == 1} {
	    APSMpReturn error "Problem requesting remote switching control: $result"
	}
    }
    APSWaitWithUpdate -waitSeconds 5
    APSMpStep "Verifying remote switch control"
    if {[catch {APScavget -list=LRFSCS:MCR:RemoteEnabled -num -pend=20} result]} {
	APSMpReturn error "Problem verifying remote switch control"
    }
    if {$result != 1} {
	if {[APSPEMTKDialog .warningDialog -text "Problem requesting remote switching control"] == 1} {
	    APSMpReturn error "Problem requesting remote switching control"
	}
    }
    
    if {[catch {APScavget -list=LRFSCS:PLC:ModeState -num -pend=20} modestate]} {
        APSMpReturn error "Problem getting the mode state: $modestate"
    }
    if {($modestate != 5)} {
        APSMpStep "Switch To Normal mode"
        if {[catch {APScavput -list=LRFSCS:MCR:SelectNormalTest=1 -pend=20} result]} {
            if {[APSPEMTKDialog .warningDialog -text "Problem changing to Normal mode: $result"] == 1} {
                APSMpReturn error "Problem changing to Normal mode: $result"
            }
        }
        APSMpStep "Verifying we are now in Normal mode"
        set tries 0
        while {$tries <= 25} {
            APSWaitWithUpdate -waitSeconds 1
            if {[catch {APScavget -list=LRFSCS:PLC:ModeState -num -pend=20} modestate]} {
                APSMpReturn error "Problem getting the mode state: $modestate"
            }
            if {($modestate != 4)} {
                if {$tries == 25} {
                    if {[APSPEMTKDialog .warningDialog -text "Unable to switch to Normal mode"] == 1} {
                        APSMpReturn error "Unable to switch to Normal mode"
                    }
                } else {
                    incr tries
                }
            } else {
                break
            }
            if {($modestate == 3)} {
                #If there was a problem switching and the operator switched back to Normal manually 
                #it is possible they switched here from K1 or K2 down mode.
                break
            }
            if {($modestate == 5)} {
                #If there was a problem switching and the operator switched back to Normal manually 
                #it is possible they switched here from some other mode.
                break
            }
        }
        if {[catch {APScavget -list=LRFSCS:PLC:SwitchFault -num -pend=20} switchfault]} {
            APSMpReturn error "Problem getting the mode state: $switchfault"
        }
        if {($switchfault != 12)} {
            if {[APSPEMTKDialog .warningDialog -text "Switch faults seen while switching to Normal mode"] == 1} {
                APSMpReturn error "Switch faults seen while switching to Normal mode"
            }
        }
    }

    if {[catch {APScavget -list=LRFSCS:PLC:GunState -num -pend=20} gunstate]} {
        APSMpReturn error "Problem getting the mode state: $gunstate"
    }
    if {($gunstate == 2)} { #PG1

        APSMpStep "Shutdown till ready to pulse is off in L1" -desc "Shutdown till ready to pulse is off" -proc LMOCS_ShutdownTillReadyToPulseOff
        if {[catch {LMOCS_ShutdownTillReadyToPulseOff -sector L1} result]} {
            if {[APSPEMTKDialog .warningDialog -text "Error while shutting down to ready to pulse is off in L1 \n$result"] == 1} {
                APSMpReturn error "LMOCS_ShutdownTillReadyToPulseOff: $result"
            }
        }

        APSMpStep "Bring L2 to Standby mode"
        if {[catch {TurnOffK400 -klystron K2 -leaveInStandby 1} result]} {
            if {[APSPEMTKDialog .warningDialog -text "Error while shutting down to ready to pulse is off in L2 \n$result"] == 1} {
                APSMpReturn error "TurnOffK400: $result"
            }
        }


        APSMpStep "Switch To $gun mode"
        if {[catch {APScavput -list=LRFSCS:MCR:Select${gun}=1 -pend=20} result]} {
            if {[APSPEMTKDialog .warningDialog -text "Problem changing to $gun mode: $result"] == 1} {
                APSMpReturn error "Problem changing to $gun mode: $result"
            }
        }
        APSMpStep "Verifying we are now in $gun mode"
        set tries 0
        while {$tries <= 25} {
            APSWaitWithUpdate -waitSeconds 1
            if {[catch {APScavget -list=LRFSCS:PLC:GunState -num -pend=20} gunstate]} {
                APSMpReturn error "Problem getting the mode state: $gunstate"
            }
            if {(($gun == "RG2") && ($gunstate != 1)) || (($gun == "RG1") && ($gunstate != 0))} {
                if {$tries == 25} {
                    if {[APSPEMTKDialog .warningDialog -text "Unable to switch to $gun mode"] == 1} {
                        APSMpReturn error "Unable to switch to $gun mode"
                    }
                } else {
                    incr tries
                }
            } else {
                break
            }
        }
    }


    APSMpStep "Switch To K3 Down (Condition) mode"
    if {[catch {APScavput -list=LRFSCS:MCR:SelectK3Down=1 -pend=20} result]} {
	if {[APSPEMTKDialog .warningDialog -text "Problem changing to K3 Down (Condition) mode: $result"] == 1} {
	    APSMpReturn error "Problem changing to K3 Down mode: $result"
	}
    }
    APSMpStep "Verifying we are now in K3 Down (Condition) mode"
    set tries 0
    while {$tries <= 25} {
	APSWaitWithUpdate -waitSeconds 1
	if {[catch {APScavget -list=LRFSCS:PLC:ModeState -num -pend=20} modestate]} {
	    APSMpReturn error "Problem getting the mode state: $modestate"
	}
	if {($modestate != 7)} {
	    if {$tries == 25} {
		if {[APSPEMTKDialog .warningDialog -text "Unable to switch to K3 Down (Condition) mode"] == 1} {
		    APSMpReturn error "Unable to switch to K3 Down (Condition) mode"
		}
	    } else {
		incr tries
	    }
	} else {
	    break
	}
    }
    if {[catch {APScavget -list=LRFSCS:PLC:SwitchFault -num -pend=20} switchfault]} {
	APSMpReturn error "Problem getting the mode state: $switchfault"
    }
    if {($switchfault != 12)} {
	if {[APSPEMTKDialog .warningDialog -text "Switch faults seen while switching to K3 Down mode"] == 1} {
	    APSMpReturn error "Switch faults seen while switching to K3 Down mode"
	}
    }
    
    APSMpStep "Run mode switch"
    if {[catch {APScavput -list=LRFSCS:MCR:SelectRun=1 -pend=20} result]} {
	if {[APSPEMTKDialog .warningDialog -text "Problem running mode switch: $result"] == 1} {
	    APSMpReturn error "Problem running mode switch: $result"
	}
    }
    
    APSMpStep "Set L3 Timing source to LI RF Rate"
    if {[catch {APScavput -list=LI:TM:L3TimingSourceC=0 -pend=20} result]} {
	if {[APSPEMTKDialog .warningDialog -text "Problem setting L3 timing source: $result"] == 1} {
	    APSMpReturn error "Problem setting L3 timing source: $result"
	}
    }
    catch {APScavput -list=It:L:B00_enb_bo.VAL=1,It:L:B02_enb_bo.VAL=1,It:L:B04_enb_bo.VAL=1,It:L:B06_enb_bo.VAL=0,It:L:B08_enb_bo.VAL=0 -num -pend=20}
    
    APSMpReturn ok

}


proc APSMpPG1_SwitchToNormalModeInfo {} {
    return "This is a Linac Waveguide Switch procedure only for (Mode 4) or  K3-->Normal mode for beamline conditioning operation. This procedure will first attenuate L3 rf power followed by taking its modulator to ready-to-pulse. Then it will position S-band switches 4 to position 1. See Linac Waveguide Switches MEDM display for position chart and rack indication. (ACIS_LP_WG.adl) for details"
}

proc APSMpPG1_SwitchToNormalModeDialog {} {
    pack [message $frame.msg -aspect 300 -justify left -text "Click OK to execute."]
    return
}

proc APSMpPG1_SwitchToNormalMode {args} {

    APSMpStep init -promptDialog APSMpPromptInitDialog
    
    if {[catch {APScavget -list=ACIS:LA_BP_MODE -num -pend=20} beamPermitStatus]} {
	APSMpReturn error "APSMpPG1_SwitchToNormalMode: $beamPermitStatus"
    }
    if {$beamPermitStatus != 1} {
	if {[APSPEMTKDialog .warningDialog \
	       -text "The Linac status is not in Beam Permit mode. ACIS:LA_BP_MODE=$beamPermitStatus"] == 1} {
	    APSMpReturn error "APSMpPG1_SwitchToNormalMode: The Linac status is not in Beam Permit mode. ACIS:LA_BP_MODE=$beamPermitStatus"
	}
    }

    APSMpStep "Shutdown till ready to pulse is off in L3" -desc "Shutdown till ready to pulse is off" -proc LMOCS_ShutdownTillReadyToPulseOff
    if {[catch {LMOCS_ShutdownTillReadyToPulseOff -sector L3} result]} {
        if {[APSPEMTKDialog .warningDialog -text "Error while shutting down to ready to pulse is off in L3 \n$result"] == 1} {
            APSMpReturn error "APSMpPG1_SwitchToNormalMode: $result"
        }
    }

    APSMpStep "Requesting remote switch control"
    if {[catch {APScavput -list=LRFSCS:MCR:RemoteRequest=1 -pend=20} result]} {
	if {[APSPEMTKDialog .warningDialog -text "Problem requesting remote switching control: $result"] == 1} {
	    APSMpReturn error "Problem requesting remote switching control: $result"
	}
    }
    APSWaitWithUpdate -waitSeconds 5
    APSMpStep "Verifying remote switch control"
    if {[catch {APScavget -list=LRFSCS:MCR:RemoteEnabled -num -pend=20} result]} {
	APSMpReturn error "Problem verifying remote switch control"
    }
    if {$result != 1} {
	if {[APSPEMTKDialog .warningDialog -text "Problem requesting remote switching control"] == 1} {
	    APSMpReturn error "Problem requesting remote switching control"
	}
    }
    
    APSMpStep "Switch To Normal mode"
    if {[catch {APScavput -list=LRFSCS:MCR:SelectNormalWater=1 -pend=20} result]} {
	if {[APSPEMTKDialog .warningDialog -text "Problem changing to Normal mode: $result"] == 1} {
	    APSMpReturn error "Problem changing to Normal mode: $result"
	}
    }
    APSMpStep "Verifying we are now in Normal mode"
    set tries 0
    while {$tries <= 25} {
	APSWaitWithUpdate -waitSeconds 1
	if {[catch {APScavget -list=LRFSCS:PLC:ModeState -num -pend=20} modestate]} {
	    APSMpReturn error "Problem getting the mode state: $modestate"
	}
	if {($modestate != 5)} {
	    if {$tries == 25} {
		if {[APSPEMTKDialog .warningDialog -text "Unable to switch to Normal mode"] == 1} {
		    APSMpReturn error "Unable to switch to Normal mode"
		}
	    } else {
		incr tries
	    }
	} else {
	    break
	}
	if {($modestate == 3)} {
	    #If there was a problem switching and the operator switched back to Normal manually 
	    #it is possible they switched here from K1 or K2 down mode.
	    break
	}
	if {($modestate == 4)} {
	    #If there was a problem switching and the operator switched back to Normal manually 
	    #it is possible they switched here from the test stand mode.
	    break
	}
    }
    if {[catch {APScavget -list=LRFSCS:PLC:SwitchFault -num -pend=20} switchfault]} {
	APSMpReturn error "Problem getting the mode state: $switchfault"
    }
    if {($switchfault != 12)} {
	if {[APSPEMTKDialog .warningDialog -text "Switch faults seen while switching to Normal mode"] == 1} {
	    APSMpReturn error "Switch faults seen while switching to Normal mode"
	}
    }
    APSMpStep "Run mode switch"
    if {[catch {APScavput -list=LRFSCS:MCR:SelectRun=1 -pend=20} result]} {
	if {[APSPEMTKDialog .warningDialog -text "Problem running mode switch: $result"] == 1} {
	    APSMpReturn error "Problem running mode switch: $result"
	}
    }
    APSMpStep "Set L3 Timing source to Asycn Rate"
    if {[catch {APScavput -list=LI:TM:L3TimingSourceC=3 -pend=20} result]} {
	if {[APSPEMTKDialog .warningDialog -text "Problem setting L3 timing source: $result"] == 1} {
	    APSMpReturn error "Problem setting L3 timing source: $result"
	}
    }

    APSMpReturn ok
}

proc APSMpPG1_SwitchToPG1OperationsModeInfo {} {
    return "This is a Linac Waveguide Switch procedure only for (Mode 4)+(PG1) or  K3-->Normal mode for beamline conditioning operation plus selecting the photocathode gun (PG1). This procedure will first attenuate L1, L2, L3 rf power followed by taking its modulator to ready-to-pulse. Then it will position S-band switches 4 and 1 to position 1 and 2 respectively. See Linac Waveguide Switches MEDM display for position chart and rack indication. (ACIS_LP_WG.adl) for details"
}

proc APSMpPG1_SwitchToPG1OperationsModeDialog {} {
    pack [message $frame.msg -aspect 300 -justify left -text "Click OK to execute."]
    return
}

proc APSMpPG1_SwitchToPG1OperationsMode {args} {

    APSMpStep init -promptDialog APSMpPromptInitDialog
    
    if {[catch {APScavget -list=ACIS:LA_BP_MODE -num -pend=20} beamPermitStatus]} {
	APSMpReturn error "APSMpPG1_SwitchToPG1OperationsMode: $beamPermitStatus"
    }
    if {$beamPermitStatus != 1} {
	if {[APSPEMTKDialog .warningDialog \
	       -text "The Linac status is not in Beam Permit mode. ACIS:LA_BP_MODE=$beamPermitStatus"] == 1} {
	    APSMpReturn error "APSMpPG1_SwitchToPG1OperationsMode: The Linac status is not in Beam Permit mode. ACIS:LA_BP_MODE=$beamPermitStatus"
	}
    }

    APSMpStep "Shutdown till ready to pulse is off in L3" -desc "Shutdown till ready to pulse is off" -proc LMOCS_ShutdownTillReadyToPulseOff
    if {[catch {LMOCS_ShutdownTillReadyToPulseOff -sector L3} result]} {
        if {[APSPEMTKDialog .warningDialog -text "Error while shutting down to ready to pulse is off in L3 \n$result"] == 1} {
            APSMpReturn error "APSMpPG1_SwitchToPG1OperationsMode: $result"
        }
    }
    APSMpStep "Bring L2 down to Standby mode"
    if {[catch {TurnOffK400 -klystron K2 -leaveInStandby 1} result]} {
        if {[APSPEMTKDialog .warningDialog -text "Error while bringing L2 to Standby mode \n$result"] == 1} {
            APSMpReturn error "APSMpPG1_SwitchToPG1OperationsMode: $result"
        }
    }
    APSMpStep "Shutdown till ready to pulse is off in L1" -desc "Shutdown till ready to pulse is off" -proc LMOCS_ShutdownTillReadyToPulseOff
    if {[catch {LMOCS_ShutdownTillReadyToPulseOff -sector L1} result]} {
        if {[APSPEMTKDialog .warningDialog -text "Error while shutting down to ready to pulse is off in L1 \n$result"] == 1} {
            APSMpReturn error "APSMpPG1_SwitchToPG1OperationsMode: $result"
        }
    }

    APSMpStep "Requesting remote switch control"
    if {[catch {APScavput -list=LRFSCS:MCR:RemoteRequest=1 -pend=20} result]} {
	if {[APSPEMTKDialog .warningDialog -text "Problem requesting remote switching control: $result"] == 1} {
	    APSMpReturn error "Problem requesting remote switching control: $result"
	}
    }
    APSWaitWithUpdate -waitSeconds 5
    APSMpStep "Verifying remote switch control"
    if {[catch {APScavget -list=LRFSCS:MCR:RemoteEnabled -num -pend=20} result]} {
	APSMpReturn error "Problem verifying remote switch control"
    }
    if {$result != 1} {
	if {[APSPEMTKDialog .warningDialog -text "Problem requesting remote switching control"] == 1} {
	    APSMpReturn error "Problem requesting remote switching control"
	}
    }
    
    APSMpStep "Switch To Normal mode"
    if {[catch {APScavput -list=LRFSCS:MCR:SelectNormalWater=1 -pend=20} result]} {
	if {[APSPEMTKDialog .warningDialog -text "Problem changing to Normal mode: $result"] == 1} {
	    APSMpReturn error "Problem changing to Normal mode: $result"
	}
    }
    APSMpStep "Verifying we are now in Normal mode"
    set tries 0
    while {$tries <= 25} {
	APSWaitWithUpdate -waitSeconds 1
	if {[catch {APScavget -list=LRFSCS:PLC:ModeState -num -pend=20} modestate]} {
	    APSMpReturn error "Problem getting the mode state: $modestate"
	}
	if {($modestate != 5)} {
	    if {$tries == 25} {
		if {[APSPEMTKDialog .warningDialog -text "Unable to switch to Normal mode"] == 1} {
		    APSMpReturn error "Unable to switch to Normal mode"
		}
	    } else {
		incr tries
	    }
	} else {
	    break
	}
	if {($modestate == 3)} {
	    #If there was a problem switching and the operator switched back to Normal manually 
	    #it is possible they switched here from K1 or K2 down mode.
	    break
	}
	if {($modestate == 4)} {
	    #If there was a problem switching and the operator switched back to Normal manually 
	    #it is possible they switched here from the test stand mode.
	    break
	}
    }
    if {[catch {APScavget -list=LRFSCS:PLC:SwitchFault -num -pend=20} switchfault]} {
	APSMpReturn error "Problem getting the mode state: $switchfault"
    }
    if {($switchfault != 12)} {
	if {[APSPEMTKDialog .warningDialog -text "Switch faults seen while switching to Normal mode"] == 1} {
	    APSMpReturn error "Switch faults seen while switching to Normal mode"
	}
    }
    

    APSMpStep "Switch To TestRoom mode"
    if {[catch {APScavput -list=LRFSCS:MCR:SelectPG1=1 -pend=20} result]} {
	if {[APSPEMTKDialog .warningDialog -text "Problem changing to PG1 mode: $result"] == 1} {
	    APSMpReturn error "Problem changing to PG1 mode: $result"
	}
    }
    APSMpStep "Verifying we are now in PG1 mode"
    set tries 0
    while {$tries <= 25} {
	APSWaitWithUpdate -waitSeconds 1
	if {[catch {APScavget -list=LRFSCS:GUNSEL:2 -num -pend=20} modestate]} {
	    APSMpReturn error "Problem getting the mode state: $modestate"
	}
	if {($modestate != 1)} {
	    if {$tries == 25} {
		if {[APSPEMTKDialog .warningDialog -text "Unable to switch to PG1 mode"] == 1} {
		    APSMpReturn error "Unable to switch to PG1 mode"
		}
	    } else {
		incr tries
	    }
	} else {
	    break
	}
    }
    
    APSMpStep "Run mode switch"
    if {[catch {APScavput -list=LRFSCS:MCR:SelectRun=1 -pend=20} result]} {
	if {[APSPEMTKDialog .warningDialog -text "Problem running mode switch: $result"] == 1} {
	    APSMpReturn error "Problem running mode switch: $result"
	}
    }

    APSMpReturn ok
}

proc StartPCGunOnlyInterleaveModeInfo {} {
   return "this pem run PCG beam ONLY in linac interleaving mode (waveguide switch is in normal mode, L3->PCGun, L1->RG2."
}

proc StartPCGunOnlyInterleaveMode {} {
    APSMpStep "turn the interleaving key to give permission for interleaving"
    APSInfoWindow .info -name "Turn interleaving key" -infoMessage "Please turn the interleaving key to give permission for interleaving\nPress Ok to contine when you are done" -modal 1
    APSMpStep "set RG2 alpha magnet current to zero"
    if [catch {exec cavput -list=L1:RG2:LFA:CurrentAO=0 -pend=30} result] {
        APSMpReturn error "Error setting RG2 alpha magnet to 0: $result"
    }
    APSMpStep "waiting to RG2 alpha magnet comes down to zero."
    if [catch {exec cawait -waitFor=L1:RG2:LFA:CurrentAI,equalto=0 -time=60} result] {
        APSMpReturn error "Error waiting for alpha magnet change to 0: $result"
    }
    APSMpStep "turn off alpha magnet..."
    if [catch {exec cavput -list=L1:RG2:LFA:OffBO=1 -pend=30} result] {
        APSMpReturn error "Error turn off alpha magnet: $result"
    }
    APSMpStep "wait for alpha magnet to turn off..."
    if [catch {exec cawait -waitFor=L1:RG2:LFA:StatusCC,equal=2 -time=30} result] {
        APSMpReturn error "Error in turning off alpha magnet: $result"
    }
    APSMpStep "open the L1:PC1:GV2 gate valve to enter interleaving mode"
    if [catch {exec cavput -list=L1:PC1:GV2:openCmdBO=1 -pend=30} result] {
        APSMpReturn error "Error open L1:PC1:GV2 gate valve: $result"
    }
    after 2000
    APSMpStep "wait gate volve open..."
    while {1} {
        if [catch {APScavget -list=L3:IC1:MISC13M -pend=30} state] {
            APSMpReturn error "Error reading L3:IC1:MISC13M: $state"
        }
        if {$state=="Enabled"} {
            break
        }
        after 1000
    }
    APSMpStep "reset the L3 interlocks to make sure PC1 GV2 row has two green buttons (both chains)"
    if [catch {exec  cavput -list=L3:IC1:MiscResetC=1  -pend=30} result] {
        APSMpReturn error "Error reset L3 interlocks: $result"
    }
    APSMpStep "wait for  PC1 GV2 to become green."
    after 5000
    if [catch {exec cavget -list=L3:IC1:MiscPermitM,L3:IC1:MISC13LatchM -pend=30} L3state] {
        APSMpReturn error "Error reading PC1 GV2 state: $sL3tate"
    }
    set permit [lindex $L3state 0]
    set latchM [lindex $L3state 1]
    if {$permit=="Disabled" || $latchM=="Disabled"} {
        APSMpReturn error "Can not reset L3 interlocks!"
    }
    APSMpStep "set L1:PC1:BM1 to zero and wait for 3 seconds..."
    if [catch {exec cavput -list=L1:PC1:BM1:PS:CurrentAO=0 -pend=30} result] {
        APSMpReturn error "Error set  L1:PC1:BM1 to zero: $result"
    }
    after 3000
    APSMpStep "start deguass  L1:PC1:BM1..."
    if [catch {exec cavput -list=L1:PC1:BM1:PS:StdSU.PROC=1 -pend=30} result] {
        APSMpReturn error "Error start deguassing L1:PC1:BM: $result"
    }
    APSMpStep "waiting for L1:PC1:BM1 deguass done (about 6 minutes, be patient..."
    APSWaitWithUpdate -waitSeconds 330 -updateInterval 1
    if [catch {exec cavget -list=L1:PC1:BM1:PS:StatusCALC -num -pend=30} status] {
        APSMpReturn error "Error checking degauss status: $status"
    }
    if {$status==0} {
        APSMpStep "StartPCGunOnlyInterleaveMode done."
    } else {
        APSInfoWindow .info "Degauss seems not done yet, please wait for the status to be green" -modal 1
    }
    APSMpReturn ok
}

proc StopPCGunOnlyInterleaveModeInfo {} {
   return "this pem stop PCG beam ONLY in linac interleaving mode and return normal operation."
}

proc StopPCGunOnlyInterleaveMode {} {
    APSMpStep "turning on the L1:PC1:BM1:PS:CurrentAO to 3A"
    if [catch {exec cavput -list=L1:PC1:BM1:PS:CurrentAO=3 -pend=30} result] {
        APSMpReturn error "error setting L1:PC1:BM1:PS:CurrentAO to 3: $result"
    }
    after 2000
    APSMpStep "check L1:PC1:BM1:PS (>2.8A)"
    while {1} {
        if [catch {APScavget -list=L1:PC1:BM1:PS:CurrentAI -pend=30} current] {
            APSMpReturn error "Error reading: L1:PC1:BM1:PS:CurrentAI: $current"
        }
        if {$current>2.8} {
            break
        }
        after 1000
    }
    APSMpStep "check if L3:IC1:MISC12M is green"
    while {1} {
        if [catch {APScavget -list=L3:IC1:MISC12M -pend=30} state] {
            APSMpReturn error "Error reading L3:IC1:MISC13M: $state"
        }
        if {$state=="Enabled"} {
            break
        }
        after 1000
    }
    APSMpStep "Reset L3 interlocks..."
    if [catch {exec  cavput -list=L3:IC1:MiscResetC=1  -pend=30} result] {
        APSMpReturn error "Error reset L3 interlocks: $result"
    }
    APSMpStep "wait for  PC1 GV2 to become green."
    after 5000
    if [catch {exec cavget -list=L3:IC1:MISC12LatchM -pend=30} state] {
        APSMpReturn error "Error reading PC1 BM state: $state"
    }
    if {$state!="Enabled"} {
        APSMpReturn error "Can not reset L3 interlocks!"
    }
    
    APSMpStep "close L1:PC1:GV2"
    if [catch {exec cavput -list=L1:PC1:GV2:closeCmdBO=1 -pend=30} result] {
        APSMpReturn error "Error close L1:PC1:GV2: $result"
    }
    APSMpStep "turn on alpha magnet..."
    if [catch {exec cavput -list=L1:RG2:LFA:ResetSQ=1 -pend=30} result] {
        APSMpReturn error "error turn on alpha magnet: $result"
    }
    APSMpStep "Waiting for alpha magnet to turn on..."
    if [catch {exec cawait -wait=L1:RG2:LFA:StatusCC,equal=0 -time=30} result] {
        APSMpReturn error "Error turning on alpha magnet: $result"
    }
    APSMpStep "turn on RG2 alpha magnet to 140A and wait for 5 seconds..."
    if [catch {exec cavput -list=L1:RG2:LFA:CurrentAO=140 -pend=30} result] {
        APSMpReturn error "Error setting RG2 alpha magnet to 140: $result"
    }
    APSWaitWithUpdate -waitSeconds 5 -updateInterval 1
    
    APSMpStep "turn the interleaving key to disable  position"
    APSInfoWindow .info -name "Turn interleaving key" -infoMessage "Please turn the interleaving key to \"disable\" poistion\nPress Ok to contine when you are done" -modal 1
    APSMpReturn ok
}
