

proc TogglePulsedMagnetEnables {args} {
    set location All
    APSStrictParseArguments {location}
    if [catch {exec cavget -list=LI:iLeavePermitCC -printErrors -pend=10 } interleaving] {
        return -code error "Error reading LI:iLeavePermitCC: $interleaving"
    }
    set GunList "L1:RG1:KIK:chargeTrigC=<value>,L1:RG2:KIK:chargeTrigC=<value>"
    set PARSeptumList "It:Par:PSPchargeBO.VAL=<value>,It:ParInjChargeDDG.GATE=<value>,It:ParExtChargeDDG.GATE=<value>"
    set PARKickerList "It:Ddg2chan4.GATE=<value>,It:ER1:er.OTL2=<value>"
    set BoosterInjList "It:Ddg3chan2.GATE=<value>,It:Ddg2chan6.GATE=<value>"
    set BoosterExtList "Mt:Ddg1chan4.GATE=<value>,Mt:Ddg1chan5.GATE=<value>,Mt:Ddg1chan0.GATE=<value>"
    set BoosterExtSpecialList "Mt:Ddg1chan4.GATE=<value>,Mt:Ddg1chan5.GATE=<value>"
    set changeAlpha 1
    set pvList ""
    switch $location {
        Gun {
            set pvList $GunList
        }
        PAR {
            set pvList $GunList,$PARKickerList,$PARSeptumList
        }
        PARKicker {
            set pvList $PARKickerList
            set changeAlpha 0
        }
        BoosterInj {
            set pvList $BoosterInjList
            set changeAlpha 0
        }
        GunToBoosterInj -
        GuntoBoosterInj {
            set pvList $GunList,$PARKickerList,$PARSeptumList,$BoosterInjList
        }
        BoosterExt {
            set pvList $BoosterExtList
            set changeAlpha 0
        }
        BoosterExtSpecial {
            set pvList $BoosterExtSpecialList
            set changeAlpha 0
        }
        GunToBoosterExtSpecial -
        GuntoBoosterExtSpecial {
            if $interleaving {
                set pvList $PARKickerList,$PARSeptumList,$BoosterInjList
            } else {
                set pvList $GunList,$PARKickerList,$PARSeptumList,$BoosterInjList,$BoosterExtSpecialList
            }
        }
        GunToBoosterExt -
        GuntoBoosterExt -
        All {
            set pvList $GunList,$PARKickerList,$PARSeptumList,$BoosterInjList,$BoosterExtList
        }
        default {
            return -code error -errorinfo "TogglePulsedMagnetEnables: Invalid location: $location" \
              "TogglePulsedMagnetEnables: Invalid location: $location"
        }
    }
    if {$interleaving && $changeAlpha} {
        #set RG2 alpha magnet to 140 to turn on gun kicker automatically
        if [catch {exec cavput -list=L1:RG2:LFA:CurrentAO=140 -pend=10} result] {
            return -code error -errorinfo "TogglePulsedMagnetEnables: Error setting RG2 alpha magnet to 140A: $result."
        }
    }
    if [string length $pvList] {
        if [catch {regsub -all "<value>" $pvList 0 pvList0} result] {
            return -code error -errorinfo "TogglePulsedMagnetEnables: Error substituting values regsub." \
              "TogglePulsedMagnetEnables: Error substituting values regsub."
        }
        if [catch {regsub -all "<value>" $pvList 1 pvList1} result] {
            return -code error -errorinfo "TogglePulsedMagnetEnables: Error substituting values regsub." \
              "TogglePulsedMagnetEnables: Error substituting values regsub."
        }
        if [catch {exec cavput -list=$pvList0 -pend=10} result] {
            return -code error -errorinfo "TogglePulsedMagnetEnables: Error trying to turn off timing enables." \
              "TogglePulsedMagnetEnables: Error trying to turn off timing enables."
        }
        after 1000
        if [catch {exec cavput -list=$pvList1 -pend=10} result] {
            return -code error -errorinfo "TogglePulsedMagnetEnables: Error trying to turn on timing enables." \
              "TogglePulsedMagnetEnables: Error trying to turn on timing enables."
        }
    }
    return -code ok
}

proc SetPulsedMagnetEnables {args} {
    set location All
    set state 0
    APSStrictParseArguments {location state}
    if [catch {exec cavget -list=LI:iLeavePermitCC -printErrors -pend=10 } interleaving] {
        return -code error "Error reading ACIS:LAL:INTRLV_MODE: $interleaving"
    }
    set GunList "L1:RG1:KIK:chargeTrigC=<value>,L1:RG2:KIK:chargeTrigC=<value>"
    set PARSeptumList "It:Par:PSPchargeBO.VAL=<value>,It:ParInjChargeDDG.GATE=<value>,It:ParExtChargeDDG.GATE=<value>"
    set PARKickerList "It:Ddg2chan4.GATE=<value>,It:ER1:er.OTL2=<value>"
    set BoosterInjList "It:Ddg3chan2.GATE=<value>,It:Ddg2chan6.GATE=<value>"
    set BoosterExtList "Mt:Ddg1chan4.GATE=<value>,Mt:Ddg1chan5.GATE=<value>,Mt:Ddg1chan0.GATE=<value>"
    set BoosterExtSpecialList "Mt:Ddg1chan4.GATE=<value>,Mt:Ddg1chan5.GATE=<value>"
    set pvList ""
    set changeAlpha 1
    switch $location {
        Gun {
            set pvList $GunList
        }
        PAR {
            set pvList $GunList,$PARKickerList,$PARSeptumList
        }
        PARKicker {
            set pvList $PARKickerList
            set changeAlpha 0
        }
        PARKickerSeptum {
            set pvList $PARKickerList,$PARSeptumList
            set changeAlpha 0
        }
        BoosterInj {
            set pvList $BoosterInjList
            set changeAlpha 0
        }
        GunToBoosterInj -
        GuntoBoosterInj {
            set pvList $GunList,$PARKickerList,$PARSeptumList,$BoosterInjList
        }
        BoosterExt {
            set pvList $BoosterExtList
            set changeAlpha 0
        }
        BoosterExtSpecial {
            set pvList $BoosterExtSpecialList
            set changeAlpha 0
        }
        GunToBoosterExtSpecial -
        GuntoBoosterExtSpecial {
            set pvList $GunList,$PARKickerList,$PARSeptumList,$BoosterInjList,$BoosterExtSpecialList
        }
        GunToBoosterExt -
        GuntoBoosterExt -
        All {
            set pvList $GunList,$PARKickerList,$PARSeptumList,$BoosterInjList,$BoosterExtList
        }
        default {
            return -code error -errorinfo "SetPulsedMagnetEnables: Invalid location: $location" \
              "SetPulsedMagnetEnables: Invalid location: $location"
        }
    }
    if {$interleaving && $changeAlpha} {
        #set RG2 alpha magnet (141 -turn on RG gun kicker, 0 - turn off RG gun kicker)
        switch $state {
            0 {
                set value 0
            }
            1 {
                set value 140
            }
            default {
                return -code error  -errorinfo "SetPulsedMagnetEnables: Invalid state: $state" \
                  "SetPulsedMagnetEnables: Invalid state: $state"
            }
        }
        if [catch {exec cavput -list=L1:RG2:LFA:CurrentAO=$value -pend=10} result] {
            return -code error  -errorinfo "SetPulsedMagnetEnables: error setting RG2 alpha magent to $value: $result" \
              "SetPulsedMagnetEnables: error setting RG2 alpha magnet to $value: $result"
        }
    }
    if [string length $pvList] {
        regsub -all "<value>" $pvList $state pvList0
        if [catch {exec cavput -list=$pvList0 -pend=10} result] {
            return -code error -errorinfo "SetPulsedMagnetEnables: Error trying to change timing enables." \
              "SetPulsedMagnetEnables: Error trying to change timing enables."
        }
    }
    return -code ok
}

