
proc APSSRProcessDimtelTunes {args} {
    set rootname ""
    set plot 0
    set merge 1
    set procMethod Dimtel
    APSStrictParseArguments {rootname plot merge procMethod}
    if ![file exist $rootname.raw] {
	return -code error "APSSRProcessDimtelTunes(1): $rootname.raw does not exist!"
    }
    set drivePlane [lindex [exec sdds2stream $rootname.raw -par=DrivePlane] 0]
    switch $drivePlane {
	x -
	y {
	    set plList [string toupper $drivePlane]
	    set planeList $drivePlane
	}
	both {
	    set plList {X Y}
	    set planeList {x y}
	}
	default {
	    return -code error "APSSRProcessDimtelTunes(0): invalid DrivePlane - $drivePlane found."
	}
    }
    set fileList ""
    foreach plane $planeList pl $plList {
	set samples [lindex [exec sdds2stream -par=${pl}Samples $rootname.raw] 0]
	set points [expr $samples / 2]
	#remove extranouse rows
	if [catch {exec sddsprocess $rootname.raw -pipe=out \
		       "-reprint=par,ProcMethod,$procMethod" \
		       -filter=col,Index,0,$points \
		       "-def=para,RevolutionFrequency,RfFrequency 1296.0 /" \
		       "-def=col,${plane}Tune,${plane}Frequency RevolutionFrequency / 1000 * " \
		       "-def=para,%sTune,%sFrequency RevolutionFrequency / 1000 * ,select=?plane*Frequency,edit=%/Frequency//" \
		       | sddsprocess -pipe \
		       "-redefine=par,${plane}Tune,${pl}planeSBMarkerTune" \
		       | sddsconvert -pipe=in $rootname.$plane \
		       -retain=col,${plane}*  } result] {
	    return -code error "Error processing tune1: $result"
	}
	if {$procMethod=="Lorentzian"} {
	    if [catch {eval exec sddsprocess $rootname.$plane -pipe=out \
			   \"-test=column,${plane}Frequency ${pl}planeDrive0Freq - abs ${pl}planeDrive0FreqSpan 2 / > !\" \
			   -clip=15,15 \
			   | sddsconvert -pipe -delete=parameter,* \
			   | sddssmooth -pipe -despike -columns=${plane}SBspectrum \
			   | sddslorentzianfit -pipe -column=${plane}Tune,${plane}SBspectrum \
			   | sddsconvert -pipe -edit=parameter,lorentzianfit*,%/lorentzianfit/${plane}/a%/Center/Tune/ \
			   | sddsprocess -pipe=in ${rootname}.${plane}lfit \
			   "-reprint=par,ProcMethod,$procMethod" \
			   \"-redefine=parameter,${plane}TunePeakValue,${plane}Baseline ${plane}Height +\" } result] {
		return -code error "Lorentzian fit error (1) for $plane: $result"
	    }
	    if [catch {exec sddsconvert $rootname.$plane -del=par,xTune,yTune -pipe=out  \
			   | sddsxref -pipe=in  $rootname.${plane}lfit $rootname.$plane -leave=* -transfer=par,* -nowarn } result] {
		return -code error "Lorentzian fit error (2) for $plane: $result"
	    }
	}
	lappend fileList $rootname.$plane
    }
    if $merge {
	if [llength $fileList]==1 {
	    exec cp $fileList $rootname
	} else {
	    if [catch {exec sddscombine $rootname.x $rootname.y $rootname -over } result] {
		return -code error "Error combine data: $result"
	    }
	}
    }
    if $plot {
	switch $drivePlane {
	    both {
		exec sddsplot -gra=line,vary "-title=Single Bunch data" -topline=$rootname \
		    "-ylabel=Spectrum (dB)" "-xLabel=Tune" \
		    -col=xTune,xSBspectrum $rootname.x -leg=spec=x \
		    "-string=@xTune,p=0.05,q=0.95,format=xTune\=%.4f,sca=1.4" \
		    -col=yTune,ySBspectrum $rootname.y -leg=spec=y \
		    "-string=@yTune,p=0.55,q=0.95,format=yTune\=%.4f,sca=1.4" \
		    &
		exec sddsplot -dev=lpng -out=$rootname.png -thick=2 \
		    -gra=line,vary,thick=2 -title= -topline=$rootname \
		    "-ylabel=Spectrum (dB)" "-xLabel=Tune" \
		    -col=xTune,xSBspectrum $rootname.x -leg=spec=x \
		    "-string=@xTune,p=0.05,q=0.95,format=xTune\=%.4f,sca=1.4" \
		    -col=yTune,ySBspectrum $rootname.y -leg=spec=y \
		    "-string=@yTune,p=0.55,q=0.95,format=yTune\=%.4f,sca=1.4" \
		    &
	    } 
	    x {
		exec sddsplot -gra=line,vary "-title=Single Bunch data" -topline=$rootname \
		    "-ylabel=Spectrum (dB)" "-xLabel=Tune" \
		    -col=xTune,xSBspectrum $rootname.x -leg=spec=x \
		    "-string=@xTune,p=0.05,q=0.95,format=xTune\=%.4f,sca=1.4" \
		    &
		exec sddsplot -dev=lpng -out=$rootname.png -thick=2 \
		    -gra=line,vary,thick=2 -title= -topline=$rootname \
		    "-ylabel=Spectrum (dB)" "-xLabel=Tune" \
		    -col=xTune,xSBspectrum $rootname.x -leg=spec=x \
		    "-string=@xTune,p=0.05,q=0.95,format=xTune\=%.4f,sca=1.4" 
	    }
	    y {
		exec sddsplot -gra=line,vary "-title=Single Bunch data" -topline=$rootname \
		    "-ylabel=Spectrum (dB)" "-xLabel=Tune" \
		    -col=yTune,ySBspectrum $rootname.y -leg=spec=y \
		    "-string=@yTune,p=0.55,q=0.95,format=yTune\=%.4f,sca=1.4" \
		    &
		exec sddsplot -dev=lpng -out=$rootname.png -thick=2 \
		    -gra=line,vary,thick=2 -title= -topline=$rootname \
		    "-ylabel=Spectrum (dB)" "-xLabel=Tune" \
		    -col=yTune,ySBspectrum $rootname.y -leg=spec=y \
		    "-string=@yTune,p=0.55,q=0.95,format=yTune\=%.4f,sca=1.4" 
	    }
	}
    }    
}

proc APSSetupDimtelForTuneMeasurement {args} {
    set statusCallback ""
    set xDriveFreq 40
    set yDriveFreq 40
    set xDriveFreqSpan 60
    set yDriveFreqSpan 60
    set xDrivePeriod 25000
    set yDrivePeriod 25000
    set xDriveAmplitude 0.2
    set yDriveAmplitude 0
    set xMarkerLow 16
    set yMarkerLow 16
    set xMarkerHigh 90
    set yMarkerHigh 90
    set average 2
    APSStrictParseArguments {statusCallback xDriveFreq yDriveFreq xDriveFreqSpan yDriveFreqSpan \
			   xDrivePeriod yDrivePeriod xDriveAmplitude yDriveAmplitude \
			   xMarkerLow xMarkerHigh yMarkerLow yMarkerHigh average}
    
    if [string length $statusCallback] {
	eval $statusCallback {"setup dimtel for tune measurement..."}
	#eval $statusCallback {"Setup 1: set FIR gain to 0..."}
    }
    if {0} {
	if [catch {exec cavput -list=S-TFB: -list=X,Y -list=:FLT:GAIN=0 -pend=10} result] {
	    return -code error "APSSetupDimtelForTuneMeasurement(1): Error set FIR gain to 0: $result"
	}
	after 1000
	if [string length $statusCallback] {
	    eval $statusCallback {"Step 1a: load FIR gain..."}
	}
	set step 1
	if [catch {exec cavput -list=S-TFB: -list=X,Y -list=:BO:CPCOEFF=1 -pend=10} result] {
	    return -code error "APSSetupDimtelForTuneMeasurement(2): Error load FIR gain: $result"
	}
    }
    set step 1
    if [string length $statusCallback] {
	eval $statusCallback {"Step $step: turn on feedbacks..."}
    }
    if [catch {exec cavput -list=S-TFB: -list=X,Y -list=:FBCTRL=1 -pend=10} result] {
	return -code error "APSSetupDimtelForTuneMeasurement(3): Error turn on feedback: $result"
    }
    incr step
    after 1000
    #check if feedback is on
    if [catch {exec cavget -list=S-TFB: -list=X,Y -list=:FBCTRL -num -pend=10} valList] {
	return -code error "APSSetupDimtelForTuneMeasurement(4): Error check feedback status: $valList"
    }
    set xFB [lindex $valList 0]
    set yFB [lindex $valList 1]
    if {!$xFB && !$yFB} {
	return -code error "APSSetupDimtelForTuneMeasurement(5): Error: both X and Y feedback are off!"
    } elseif {!$xFB} {
	return -code error "APSSetupDimtelForTuneMeasurement(6): Error: X feedback is off!"
    } elseif {!$yFB} {
	return -code error "APSSetupDimtelForTuneMeasurement(7): Error: Y feedback is off!"
    }
    incr step
    if [string length $statusCallback] {
	eval $statusCallback {"Step $step: set drive source to 0..."}
    }
    if [catch {exec cavput -list=S-TFB: -list=X,Y -list=:DRIVE0:PATTERN=1 -pend=10} result] {
	return -code error "APSSetupDimtelForTuneMeasurement(8): Error set drive 0 pattern to 1: $result"
    }
    incr step
    if [string length $statusCallback] {
	eval $statusCallback {"Step $step: set SB acquisition controls..."}
    }
    #bunch number is set to 1, same as bunch number selected in drive source
    if [catch {exec cavput -list=S-TFB: -list=X,Y -list=:SB:BUNCH_ID=1 -pend=10} result] {
	return -code error "APSSetupDimtelForTuneMeasurement(9): Error set SB bunch number to 1: $result"
    }
    #set marker span >10 kHz, peak search option to MAX
    if [catch {exec cavput -list=S-TFB: -list=X,Y -list=:SB:SP_SEARCH1=MAX -pend=10} result] {
	return -code error "APSSetupDimtelForTuneMeasurement(10): Error set peak search option: $result"
    }
    
    if [catch {exec cavput -list=S-TFB:X:SB:SP_LOW1=$xMarkerLow,S-TFB:Y:SB:SP_LOW1=$yMarkerLow,S-TFB:X:SB:SP_HIGH1=$xMarkerHigh,S-TFB:Y:SB:SP_HIGH1=$yMarkerHigh -pend=10} result] {
	return -code error "APSSetupDimtelForTuneMeasurement(11): Error set SB lower/higher maker: $result"
    }
    
    incr step
    if [string length $statusCallback] {
	eval $statusCallback {"Step $step: setup drive freq, span, and amplitude etc..."}
    }
     if [catch {exec cavput -list=S-TFB:X:DRIVE0:FREQ=$xDriveFreq,S-TFB:Y:DRIVE0:FREQ=$yDriveFreq -pend=10 } result] {
	return -code error "APSSetupDimtelForTuneMeasurement(12): Error setting drive freq: $result"
    }
    if [catch {exec cavput -list=S-TFB:X:DRIVE0:SPAN=$xDriveFreqSpan,S-TFB:Y:DRIVE0:SPAN=$yDriveFreqSpan -pend=10 } result] {
	return -code error "APSSetupDimtelForTuneMeasurement(13): Error setting drive freq span: $result"
    }
   
    if [catch {exec cavput -list=S-TFB:X:DRIVE0:AMPL=$xDriveAmplitude,S-TFB:Y:DRIVE0:AMPL=$yDriveAmplitude -pend=10} result] {
	return -code error "APSSetupDimtelForTuneMeasurement(14): Error setting drive amp: $result"
    }
    if [catch {exec cavput -list=S-TFB:X:DRIVE0:PERIOD=$xDrivePeriod,S-TFB:Y:DRIVE0:PERIOD=$yDrivePeriod -pend=10} result] {
	return -code error "APSSetupDimtelForTuneMeasurement(15): Error setting drive period: $result"
    }
    #set average
    if [catch {exec cavput -list=S-TFB: -list=X,Y -list=:SB:SP:AVG=$average -pend=10} result] {
	return -code error "APSSetupDimtelForTuneMeasurement(16): Error setting SB waveform average: $result"
    }
    if [string length $statusCallback] {
	eval $statusCallback {"dimtel setup done."}
    }
}

#this is generic tune measurement procedure
proc APSSRMeasureTunesWithDimtel {args} {
    set setupDimtel 1
    set statusCallback ""
    set xDriveFreq 40
    set yDriveFreq 40
    set xDriveFreqSpan 60
    set yDriveFreqSpan 60
    set xDrivePeriod 25000
    set yDrivePeriod 25000
    set xDriveAmplitude 0.02
    set yDriveAmplitude 0
    set xMarkerLow 16
    set yMarkerLow 16
    set xMarkerHigh 90
    set yMarkerHigh 90
    set xOnly 1
    set yOnly 0
    set rootname ""
    set plot 0
    set decription ""
    set pause 5
    set average 2
    set merge 0
    set procMethod Dimtel
    APSStrictParseArguments {statusCallback xDriveFreq yDriveFreq xDriveFreqSpan yDriveFreqSpan average setupDimtel \
			   xDrivePeriod yDrivePeriod xDriveAmplitude yDriveAmplitude pause merge procMethod \
			   xMarkerLow xMarkerHigh yMarkerLow yMarkerHigh xOnly yOnly rootname plot description}
    if {!$xOnly && !$yOnly} {
	set drivePlane both
    } elseif {$xOnly} {
	set drivePlane x
    } elseif {$yOnly} {
	set drivePlane y
    } else {
	return -code error "APSSRMeasureTunesWithDimtel(0): Error invalid xOnly and yOnly provided!"
    }
    #read the marker mode
    if [catch {exec cavget -list=S-TFB: -list=X,Y -list=:SB:SP_SEARCH1 -cavput -pend=5} origMarkers] {
	return -code error "Error reading mark modes: $origMarkers"
    }
    #set marker to MAX
    if [catch {exec cavput -list=S-TFB: -list=X,Y -list=:SB:SP_SEARCH1=MAX -pend=5} result] {
	return -code error "Error setting marker mode to MAX: $result"
    }
    if $setupDimtel {
	if [catch {APSSetupDimtelForTuneMeasurement -statusCallback $statusCallback \
		       -average $average \
		       -xDriveFreq $xDriveFreq -yDriveFreq $yDriveFreq \
		       -xDriveFreqSpan $xDriveFreqSpan -yDriveFreqSpan $yDriveFreqSpan \
		       -xDrivePeriod $xDrivePeriod -yDrivePeriod $yDrivePeriod \
		       -xDriveAmplitude $xDriveAmplitude -yDriveAmplitude $yDriveAmplitude \
		       -xMarkerLow $xMarkerLow -xMarkerHigh $xMarkerHigh \
		       -yMarkerLow $yMarkerLow -yMarkerHigh $yMarkerHigh } result] {
	    catch {exec cavput -list=$origMarkers -pend=5}
	    return -code error "APSSRMeasureTunesWithDimtel(1): Error setup dimtel: $result"
	}
    }
    # why do we need to repeat this command, since it is in the above library?
    if [catch {exec cavput -list=S-TFB:X:DRIVE0:AMPL=$xDriveAmplitude,S-TFB:Y:DRIVE0:AMPL=$yDriveAmplitude -pend=10} result] {
	catch {exec cavput -list=$origMarkers -pend=5}
	return -code error "error set drive amplitude: Error setting drive amp: $result"
    }
    if [string length $statusCallback] {
        eval $statusCallback {"wait $pause seconds for dimtel averaging..."}
        after [expr 1000*$pause]
    }
    if [catch {exec sddswmonitor -scalar=/home/helios/oagData/sr/dimtel_iGp12/inputFiles/scalars.mon  /home/helios/oagData/sr/dimtel_iGp12/inputFiles/singleBunchSpectrum.wmon $rootname.raw -step=1 -erase} result] {
	catch {exec cavput -list=$origMarkers -pend=5}
	return -code error "APSSRMeasureTunesWithDimtel(3): Error reading SB waveform: $result"
    }
    if [catch {exec sddswmonitor -scalar=/home/helios/oagData/sr/dimtel_iGp12/inputFiles/scalars.mon  /home/helios/oagData/sr/dimtel_iGp12/inputFiles/singleBunchSpectrumTF.wmon $rootname.raw.TF -step=1 -erase} result] {
	catch {exec cavput -list=$origMarkers -pend=5}
	return -code error "APSSRMeasureTunesDimtel(2): Error reading SB waveform: $result"
    }
    catch {exec cavput -list=$origMarkers -pend=5}
    if [catch {exec cavput -list=S-TFB:X:DRIVE0:AMPL=0,S-TFB:Y:DRIVE0:AMPL=0 -pend=10} result] {
	catch {exec cavput -list=$origMarkers -pend=5}
	return -code error "error set drive amplitude: Error setting drive amp: $result"
    }
    
    set xSamples [exec cavget -list=S-TFB:X:SB:NFFT -pend=10]
    set ySamples [exec cavget -list=S-TFB:Y:SB:NFFT -pend=10]
    if [catch {exec sddsprocess -nowarn $rootname.raw -redefine=par,XSamples,$xSamples,type=long \
		   -reprint=par,DrivePlane,$drivePlane \
		   "-reprint=par,Description,[APSMakeSafeQualifierString $description]" \
		   -redefine=par,YSamples,$ySamples,type=long } result] {
	return -code error "APSSRMeasureTunesWithDimtel(4): Error define x/y samples parameter: $result"
    }
    APSAddToTmpFileList -ID tune -fileList $rootname.raw~
    if [catch {APSSRProcessDimtelTunes -rootname $rootname -plot $plot -merge $merge -procMethod $procMethod} result] {
	return -code error "APSSRMeasureTunesWithDimtel(5): Error processing tune data: $result"
    }
    if $plot {
	#plot raw data
	exec sddsplot $rootname.raw -gra=line,vary "-title=Single Bunch Raw data" -topline=$rootname \
	    "-ylabel=Spectrum (dB)" "-xLabel=Frequency(kHz)" \
	    -col=xFrequency,xSBspectrum  -leg=spec=x \
	    "-string=@XplaneSBPeakFreq,p=0.05,q=0.95,format=xPeakFreq\=%.4f,sca=1.4" \
	    "-string=@XplaneSBPeak,p=0.05,q=0.85,format=xPeak\=%.4f,sca=1.4" \
	    -col=yFrequency,ySBspectrum $rootname.raw -leg=spec=y \
	    "-string=@YplaneSBPeakFreq,p=0.55,q=0.95,format=yPeakFreq\=%.4f,sca=1.4" \
	    "-string=@YplaneSBPeak,p=0.55,q=0.85,format=yPeak\=%.4f,sca=1.4" &
    }
   
}


#this procedure is speficically for chromaticity measurement that  measures tunes in both planes sequentially 
proc APSGetSRTunesDimtel {args} {
    set output ""
    set description ""
    set statusCallback APSNoOp
    set xDriveFreq 40
    set yDriveFreq 40
    set xDriveFreqSpan 60
    set yDriveFreqSpan 60
    set xDrivePeriod 25000
    set yDrivePeriod 25000
    set xDriveAmplitude 0.2
    set yDriveAmplitude 0.02
    set xMarkerLow 16
    set yMarkerLow 16
    set xMarkerHigh 90
    set yMarkerHigh 90
    set pause 5
    set average 2
    set rfFrequency [exec cavget -list=A014-IETS:BTC:SRSetFreqM -floatFormat=%.1f -pendIoTime=5]
    set sideband -1
    set xchrom 6
    set ychrom 4
    set deltaFreq 0
    set alpha_c 2.819e-04
    set steps 5
    set plot 0
    set measHarmonic 1390
    set xFrequency ""
    set yFrequency ""
    set skipSetup 1
    set procMetod Dimtel
    if {[APSStrictParseArguments {output description statusCallback sideband xchrom ychrom deltaFreq steps alpha_c Dimtel \
				      xDriveFreq yDriveFreq xDriveFreqSpan yDriveFreqSpan average setupDimtel \
				      xDrivePeriod yDrivePeriod xDriveAmplitude yDriveAmplitude pause procMethod \
				      xMarkerLow xMarkerHigh yMarkerLow yMarkerHigh measHarmonic plot xFrequency yFrequency skipSetup}] || \
          ![string length $output] || $average<=0} {
        return -code error "APSGetSRTunesVSA: bad arguments"
    }
     #read the marker mode
    if [catch {exec cavget -list=S-TFB: -list=X,Y -list=:SB:SP_SEARCH1 -cavput -pend=5} origMarkers] {
	return -code error "Error reading mark modes: $origMarkers"
    }
    #set marker to MAX
    if [catch {exec cavput -list=S-TFB: -list=X,Y -list=:SB:SP_SEARCH1=MAX -pend=5} result] {
	return -code error "Error setting marker mode to MAX: $result"
    }
    #correct the delta frequency computation.
    set deltaFreq [expr $deltaFreq * $measHarmonic / 1296.0]
    set xDriveAmplitude0 $xDriveAmplitude
    set yDriveAmplitude0 $yDriveAmplitude
    if ![string length $xFrequency] {
	set xFrequency $rfFrequency
    }
    if ![string length $yFrequency] {
	set yFrequency $rfFrequency
    }
    foreach plane {x y} {
        set freq [set ${plane}Frequency]
        if $deltaFreq {
            set chrom [set ${plane}chrom]
            set step [lindex [split $output "-"] 1]
            set step [scan $step %ld]
            set step [expr $step - ($steps-1.0) / 2.0 ]
            set freq [expr $freq - $sideband * $deltaFreq * $step * $chrom / $alpha_c / 1296.0]
           # puts "$step $freq"
        }
	set ${plane}DriveAmplitude [set ${plane}DriveAmplitude0]
        switch $plane {
	    x {
		set otherplane y
	    } 
	    y {
		set otherplane x
	    }
	}
	set ${otherplane}DriveAmplitude 0
        eval $statusCallback "{set up dimtel for $plane...}"
	
        if !$skipSetup {
            if [catch {APSSetupDimtelForTuneMeasurement -statusCallback $statusCallback \
                           -average $average \
                           -xDriveFreq $xDriveFreq -yDriveFreq $yDriveFreq \
                           -xDriveFreqSpan $xDriveFreqSpan -yDriveFreqSpan $yDriveFreqSpan \
                           -xDrivePeriod $xDrivePeriod -yDrivePeriod $yDrivePeriod \
                           -xDriveAmplitude $xDriveAmplitude -yDriveAmplitude $yDriveAmplitude \
                           -xMarkerLow $xMarkerLow -xMarkerHigh $xMarkerHigh \
                           -yMarkerLow $yMarkerLow -yMarkerHigh $yMarkerHigh } result] {
		catch {exec cavput -list=$origMarkers -pend=5}
                return -code error "APSSRMeasureTunesDimtel(1): Error setup dimtel: $result"
            }
        }
        
	if [string length $statusCallback] {
	    eval $statusCallback {"wait $pause seconds for dimtel averaging..."}
	    after [expr 1000*$pause]
	}
	eval $statusCallback "{Dimtel has completed data acquisition}"
        if [catch {exec sddswmonitor -scalar=/home/helios/oagData/sr/dimtel_iGp12/inputFiles/scalars.mon  /home/helios/oagData/sr/dimtel_iGp12/inputFiles/singleBunchSpectrum.wmon.$plane $output.$plane.raw -step=1 -erase} result] {
	    catch {exec cavput -list=$origMarkers -pend=5}
	    return -code error "APSSRMeasureTunesDimtel(2): Error reading SB waveform: $result"
	}
	if [catch {exec sddswmonitor -scalar=/home/helios/oagData/sr/dimtel_iGp12/inputFiles/scalars.mon  /home/helios/oagData/sr/dimtel_iGp12/inputFiles/singleBunchSpectrumTF.wmon.$plane $output.$plane.raw.TF -step=1 -erase} result] {
	    catch {exec cavput -list=$origMarkers -pend=5}
	    return -code error "APSSRMeasureTunesDimtel(2): Error reading SB waveform: $result"
	}
	catch {exec cavput -list=$origMarkers -pend=5}
	if [catch {exec cavput -list=S-TFB:X:DRIVE0:AMPL=0,S-TFB:Y:DRIVE0:AMPL=0 -pend=10} result] {
	    catch {exec cavput -list=$origMarkers -pend=5}
	    return -code error "error set drive amplitude: Error setting drive amp: $result"
	}
	
        set xSamples [exec cavget -list=S-TFB:X:SB:NFFT -pend=10]
	set ySamples [exec cavget -list=S-TFB:Y:SB:NFFT -pend=10]
	if [catch {exec sddsprocess -nowarn $output.$plane.raw \
                     -redefine=par,XSamples,$xSamples,type=long \
                     "-redefine=par,${plane}CenterFreq,$freq 1.0e6 /,units=MHz" \
                     -reprint=par,DrivePlane,$plane \
                     "-reprint=par,Description,[APSMakeSafeQualifierString $description]" \
                     -redefine=par,YSamples,$ySamples,type=long } result] {
	    return -code error "APSSRMeasureTunesDimtel(3): Error processing raw data: $result"
	}
	APSAddToTmpFileList -ID tunes -fileList "$output.$plane.raw $output.$plane.raw.TF $output.$plane.raw~"
    }
    if [catch {exec sddsxref $output.x.raw $output.y.raw  -take=* -transfer=par,y*,Y* -pipe=out \
		   | sddsprocess -pipe=in $output.raw -reprint=par,DrivePlane,both } result] {
	return -code error "APSSRMeasureTunesDimtel(4): error combining x/y raw files: $result"
    }
    if [catch {exec sddsxref $output.x.raw.TF $output.y.raw.TF $output.raw.TF -take=* } result] {
	return -code error "APSSRMeasureTunesDimtel(5): error combining x/y raw TF files: $result"
    }
    if [catch {APSSRProcessDimtelTunes -rootname $output -plot $plot -merge 0} result] {
	return -code error "APSSRMeasureTunesDimtel(6): Error processing tune: $result"
    }
    
    if [catch {exec sddsxref $output.x $output.y $output -take=* -transfer=parameter,* -nowarning} result] {
        return -code error "APSSRMeasureTunesDimtel(4): $result"
    }
    
    eval $statusCallback "{Dimtel tune data acquired}"
    catch {file delete  $output.x  $output.y}
}
