proc APSBoosterTurnOnOffTuneDrive {args} {
    set onoff ""
    set xdrive ""
    set ydrive ""
    set statusCallback ""
    set Xxdrive 134
    set Xydrive 154
    set Yxdrive 134
    set Yydrive 0
    APSStrictParseArguments {onoff xdrive ydrive statusCallback Xxdrive Xydrive Yxdrive Yydrive}
    if [string length $statusCallback] {
        eval $statusCallback  {"turn $onoff tune drive ..."}
    }
    if {(![string length $xdrive] || ![string length $ydrive]) && $onoff=="on"} {
        if [catch {exec cavget -list=B:TUNE:HP_1366A_P0_MBBO -pend=20} plane] {
            return -code error "Error turning on booster tune drive1: $plane"
        }
        if {$plane=="OFF" || $plane=="SPARE"} {
            return -code error "drive is not being turned on.\nPlease select a plane X/Y/S on booster tune measurement multiplexer first before turn it on."
        }
        set xdrive [set ${plane}xdrive]
        set ydrive [set ${plane}ydrive]
    }
    set onPVList [list B:DG1:aDelaySetAO=4.5e-7 \
                    B:DG1:bDelaySetAO=2.0e-6 \
                    B:DG1:cDelaySetAO=7.0e-7 \
                    B:DG1:dDelaySetAO=2.0e-6 \
                    B:DG1:trigModeSetMO=1 \
                    B:DG1:aOutputModeSetMO=0 \
                    B:DG1:cOutputModeSetMO=0 \
                    B:DG1:aOutputZSetBO=0 \
                    B:DG1:cOutputZSetBO=0 \
                    B:DG1:aOutputAmpSetAO=1 \
                    B:DG1:cOutputAmpSetAO=1 \
                    B:DG1:aOutputOffsetSetAO=0.0 \
                    B:DG1:cOutputOffsetSetAO=0.0 \
                    B:DG1:trigInputAmpSetAO=1.0 \
                    BOOTUNE:XAXIS:MATRE200SetVoltage=$xdrive \
                    BOOTUNE:YAXIS:MATRE200SetVoltage=$ydrive ]
    if {$onoff=="on"} {
        if [catch {exec cavput -pend=30 -list=[join $onPVList ,] } result] {
            return -code error "Error turning on booster tune drive: $result"
        }
    } else {
        if [catch {exec cavput -pend=30 \
                     -list=BOOTUNE:XAXIS:MATRE200SetVoltage=0,BOOTUNE:YAXIS:MATRE200SetVoltage=0 } result] {
            return -code error "Error turning off booster tune drive: $result"
        }
    }
    after 2000
    if [string length $statusCallback] {
        eval $statusCallback {"done."}
    }
}


proc APSBoosterSwitchTuneInput {args} {
    set plane ""
    set Xxdrive 134
    set Xydrive 154
    set Yxdrive 134
    set Yydrive 0
    set SUMxdrive 120
    set SUMydrive 20
    set statusCallback ""
    APSStrictParseArguments {plane Xxdrive Xydrive Yxdrive Yydrive SUMxdrive SUMydrive statusCallback}
    
    if [string length $statusCallback] {
        eval $statusCallback {"Switching to $plane ..."}
    }
    set plane [string toupper $plane]
    
    set inputChannel $plane
    switch $plane {
        S {
            set plane SUM
            set xdrive 0
            set ydrive 0
            set onoff off
            
        }
        X {
            set xdrive $Xxdrive
            set ydrive $Xydrive
            set onoff on
        }
        Y {
            set xdrive $Yxdrive
            set ydrive $Yydrive
            set onoff on
        }
        default {
            set xdrive 0
            set ydrive 0
            set onoff off
        }
    }
    if [catch { APSBoosterTurnOnOffTuneDrive -xdrive $xdrive -ydrive $ydrive -onoff $onoff} result] {
        return -code error "Error in turnOnOffTuneDrive: $result"
    }
    if [catch {exec cavput -list=B:TUNE:HP_1366A_P0_MBBO=$plane,BOOTUNE:XAXIS:MATRE200SetVoltage=$xdrive,BOOTUNE:YAXIS:MATRE200SetVoltage=$ydrive } result] {
        return -code error "Unable to switch to $plane plane: $result"
    }
    if [catch {exec cavput -list=BOOTUNE:XAXIS:MATRE200SW=1,BOOTUNE:YAXIS:MATRE200SW=1 -pend=20} result] {
        return -code error "Unable to set  BOOTUNE:XAXIS:MATRE200SW  and BOOTUNE:YAXIS:MATRE200SW to 1: $result"
    }
    #load tune parameters
    if {$plane!="S"} {
	if [catch {exec sddscasr -restore \
		       /home/helios/oagData/booster/tuneArchive/inputFiles/tuneconfig.[string tolower $plane] -pend=30} result] {
	    return -code error  "Error in loading tune parameters for $plane plane: $result"
	}
    }
    after 2000
    if [string length $statusCallback] {
        eval $statusCallback  {"done"}
    }
}
