
proc RFGUN_ChangeTimingInfo {} {
    return "RFGUN Change Timing Procedure\n\
\n\
This is called by:\n\
PCGUN_SwitchOverToRFGunWithPS\n\
\n\
This procedure does the following:\n\
1. Sets the Laser trigger to \"Test Stand RF\".\n\
2. Changes the beam trigger timing to 2Hz.\n\
3. Changes the Timing Reference Source to \"PAR RF\".\n\
4. Changes the beam trigger timing to 10Hz.\n\
5. Sets timing source to \"LI Async Rate\".\n\
6. Depresses the \"Set Timing for RF-Gun and Kicker\" button."
}

proc RFGUN_ChangeTimingDialog {frame} {
    APSSetVarAndUpdate status "Click OK to set timing for RF-Gun"
    pack [message $frame.msg -aspect 300 -justify left \
	      -text "Click OK to set timing for RF-Gun"]
    return
}

proc RFGUN_ChangeTiming {args} {
    global execMode
    set includePar 1
    set bypassPar 0
    
    if {$execMode == "Manual"} {
	set prefix "Press Continue to "
    } else {
	set prefix ""
    }

    APSStrictParseArguments {includePar bypassPar}
    APSMpStep init -promptDialog RFGUN_ChangeTimingDialog

    set paramString ""
    foreach param {includePar bypassPar} {
        append paramString "$param=\"[set $param]\" "
    }
    catch {APSLogScriptAction -procedure RFGUN_ChangeTiming -action initialize -state ok -parameters $paramString}

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

    APSMpStep "${prefix}Set Laser trigger to Test Stand RF" \
	-desc "Set Laser trigger to Test Stand RF"
    if [catch {APSPCGUN_SetLaserTrigger -state "TestStandRF"} result] {
        if {[APSPEMTKDialog .warningDialog -text "Unable set Laser trigger to Test Stand RF\n$result"] == 1} {
            APSMpReturn error "RFGUN_ChangeTiming: $result"
        }
#	APSMpReturn error "RFGUN_ChangeTiming: $result"
    }
#    APSMpStep "${prefix}Change the Beam Trigger Timing to 2Hz on the Linac Injection Sequence Screen" -desc "Change the Beam Trigger Timing to 2Hz on the Linac Injection Sequence Screen"
#    if [catch {APSLINAC_SetBeamTriggerRate -state "--2Hz--"} result] {
#        if {[APSPEMTKDialog .warningDialog -text "Unable change the Beam Trigger Timing to 2Hz on the Linac Injection Sequence Screen\n$result"] == 1} {
#            APSMpReturn error "RFGUN_ChangeTiming: $result"
#        }
#    }
#    APSMpStep "${prefix}Change the Timing Reference Source to PAR RF" -desc "Change the Timing Reference Source to PAR RF"
#    if [catch {APSLINAC_SetInjectionTimingTrigger -state PAR} result] {
#        if {[APSPEMTKDialog .warningDialog -text "Unable change the timing Reference Source to PAR RF\n$result"] == 1} {
#            APSMpReturn error "RFGUN_ChangeTiming: $result"
#        }
#    }
    if {[catch {exec cavget -list=It:ByPassParC -num -pend=20} currentParState]} {
        APSMpReturn error "RFGUN_ChangeTiming: $currentParState"
    }
    APSMpStep "${prefix}Set the PAR Inc/Exc PV" -desc "Set the PAR Inc/Exc PV"
    if {$includePar} {
        if {$currentParState != 0} {
            if {[catch {exec cavput -list=It:ByPassParC=0 -num -pend=20} results]} {
                if {[APSPEMTKDialog .warningDialog -text "Unable to switch to Include Par\n$results"] == 1} {
                    APSMpReturn error "RFGUN_ChangeTiming: $results"
                }
#                APSMpReturn error "RFGUN_ChangeTiming: Unable to switch to Include Par"
            }
        }
#        APSMpStep "${prefix}Change the Beam Trigger Timing to 10Hz on the Linac Injection Sequence Screen" -desc "Change the Beam Trigger Timing to 10Hz on the Linac Injection Sequence Screen"
#        if [catch {APSLINAC_SetBeamTriggerRate -state {{RFG 10hz}}} result] {
#            if {[APSPEMTKDialog .warningDialog -text "Unable change the Beam Trigger Timing to 10Hz on the Linac Injection Sequence Screen\n$result"] == 1} {
#                APSMpReturn error "RFGUN_ChangeTiming: $result"
#            }
#        }
        APSWaitWithUpdate -waitSeconds 2
        catch {exec cavput -list=It:L:B06_enb_bo.VAL=0,It:L:B08_enb_bo.VAL=0 -num -pend=20}
    } elseif {$bypassPar} {
        if {$currentParState != 1} {
            if {[catch {exec cavput -list=It:ByPassParC=1 -num -pend=20} results]} {
                if {[APSPEMTKDialog .warningDialog -text "Unable to switch to Bypass Par\n$results"] == 1} {
                    APSMpReturn error "RFGUN_ChangeTiming: $results"
                }
#                APSMpReturn error "RFGUN_ChangeTiming: Unable so switch to Bypass Par"
            }
        }
    }
    APSMpStep "${prefix}Depress the \"Set Timing for RF-Gun and Kicker\" button" \
	-desc "Depress the \"Set Timing for RF-Gun and Kicker\" button"
    if [catch {APSPCGUN_SetTimingForRFGun} result] {
        if {[APSPEMTKDialog .warningDialog -text "Unable to set timing for RF-Gun and kicker\n$result"] == 1} {
            APSMpReturn error "RFGUN_ChangeTiming: $result"
        }
#	APSMpReturn error "RFGUN_ChangeTiming: $result"	
    }
    if {[catch {exec cavput -list=L1:RG1:KIK:chargeTrigC=1,L1:RG2:KIK:chargeTrigC=1 -pend=20} results]} {
	bell
        if {[APSPEMTKDialog .warningDialog -text "Unable to set RF gun charge trig PV\n$results"] == 1} {
	    APSMpReturn error "RFGUN_ChangeTiming: Unable to set RF gun charge trig PV to 1 : $results"
	}
    }
    APSMpReturn ok
}

