#!/bin/sh
# \
  exec oagtclsh "$0" "$@"

set auto_path [linsert $auto_path 0 /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]
APSStandardSetup

set usage "getxytunes -root <file> -description <string> \[-statusCallback <string>\] \[-only \{x|y\}\] -verbose <0|1> -plot \{0|1\} \[-pause <pause seconds after setup dimtel>\] \[-average <value>\] \[-xDriveAmplitude <x drive amplitude(dB)>\] \[-yDriveAmplitude <y drive amplitude (dB)\]  \[-xDriveFreq <x drive freq(kHz)>\] \[-yDriveFreq <y drive frequency (kHz)\] \[-xDriveFreqSpan <x drive freq span(kHz)>\] \[-yDriveFreqSpan <y drive frequency span (kHz)\] \[-xDrivePeriod <x drive period>\] \[-yDrivePeriod <y drive period>\] \[-xMarkerLow <x lower marker>\] \[-yMarkerLow <y lower marker>\]  \[-xMarkerHigh <x higher marker>\] \[-yMarkerHigh <y higher marker>\] \[-setup <0|1>\] \[-pause <value>\]"
set root ""
set description ""
set plot 0
set only ""
set verbose 0
set paramFile /home/helios/oagData/sr/dispChrom/dimtelParameters.sdds
set nameList [exec sdds2stream -col=ParameterName $paramFile]
set valList [exec sdds2stream -col=ParameterValue $paramFile]
foreach name $nameList val $valList {
   set $name [format %.1f $val]
}
set setup 0
set pause 5
set average 2
set procMethod Dimtel
set args $argv
if {[APSStrictParseArguments {root description plot verbose only xDriveFreq yDriveFreq xDriveFreqSpan yDriveFreqSpan \
                                   setup average xDrivePeriod yDrivePeriod xDriveAmplitude yDriveAmplitude xMarkerLow  \
                                   yMarkerLow xMarkerHigh yMarkerHigh pause procMethod}]} {
    return -code error "invalid arguments"
}

set env(EPICS_CA_ADDR_LIST) "10.6.27.93 10.6.27.102 10.6.27.103"
if ![string length $root] {
    puts stderr $usage
    puts stderr "Error: root not provided."
    exit 1
}
if [llength [glob -nocomplain $root.*]]!=0 {
    puts stderr "In use: $root"
    puts stderr "[lsort [glob -nocomplain $root.*]]"
    exit 1
}

switch $only {
    x {
	set xOnly 1
	set yOnly 0
	set yDriveAmplitude 0
    }
    y {
	set xOnly 0
	set yOnly 1
	set xDriveAmplitude 0
    }
    default {
	set xOnly 0
	set yOnly 0
    }
}
if $verbose {
    set statusCallback puts
} else {
    set statusCallback ""
}
if {!$xOnly && !$yOnly} {
    #measure both plan sequentially
    set oldxDrive $xDriveAmplitude
    set oldyDrive $yDriveAmplitude
    set xDriveAmplitude 0
    set yDriveAmplitude 0
    foreach plane {x y} {
	set ${plane}Only 1
	if $verbose {
	    puts "measuring $plane tune..."
	}
	set ${plane}DriveAmplitude [set old${plane}Drive]
	if [catch {APSSRMeasureTunesWithDimtel -statusCallback $statusCallback -plot 0 -pause $pause \
		       -procMethod $procMethod \
		       -statusCallback $statusCallback \
		       -average $average \
		       -description $description -rootname $root -setupDimtel $setup -xOnly $xOnly -yOnly $yOnly \
		       -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] {
	    puts stderr "Error measure SR tune with dimtel: $result"
	    exit 1
	}
	set xOnly 0
	set xDriveAmplitude 0
    }
    if [catch {exec sddscombine $root.x $root.y $root.sdds -over  } result] {
	puts stderr "Error measure SR tune with dimtel1: $result"
    }
    
    if $plot {
	exec sddsplot -gra=line,vary "-title=Single Bunch data" -topline=$root \
	    "-ylabel=Spectrum (dB)" "-xLabel=Tune" \
	    -col=xTune,xSBspectrum $root.x -leg=spec=x \
	    "-string=@XplaneSBMarkerTune,p=0.05,q=0.95,format=xTune\=%.4f,sca=1.4" \
	    -col=yTune,ySBspectrum $root.y -leg=spec=y \
	    "-string=@YplaneSBMarkerTune,p=0.55,q=0.95,format=yTune\=%.4f,sca=1.4" \
	    &
	exec sddsplot -dev=lpng -out=$root.png -thick=2 \
	    -gra=line,vary,thick=2 -title= -topline=$root \
	    "-ylabel=Spectrum (dB)" "-xLabel=Tune" \
	    -col=xTune,xSBspectrum $root.x -leg=spec=x \
	    "-string=@XplaneSBMarkerTune,p=0.05,q=0.95,format=xTune\=%.4f,sca=1.4" \
	    -col=yTune,ySBspectrum $root.y -leg=spec=y \
	    "-string=@YplaneSBMarkerTune,p=0.55,q=0.95,format=yTune\=%.4f,sca=1.4" \
	    &
    }
} else {
    if $verbose {
	puts "measuring $only tune..."
    }
    if [catch {APSSRMeasureTunesWithDimtel -statusCallback $statusCallback -plot $plot -pause $pause \
		   -procMethod $procMethod \
		   -average $average \
		   -statusCallback $statusCallback \
		   -description $description -rootname $root -setupDimtel $setup -xOnly $xOnly -yOnly $yOnly \
		   -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] {
	puts stderr "Error measure SR tune with dimtel: $result"
	exit 1
    }
}

if $verbose {
    puts "tune measurement done."
}
exit
