#!/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> \[-only \{x|y\}\] -plotTunes \{0|1\} -plotOnly \{0|1\} -measHarmonic <integer> -dividingLine <value> \[-MXA \{1|0\}\] \[-xTune <fraction x tune>\] \[-yTune <fraction y tune>\] \[-xSourceLevel <x DBm>\] \[-ySouceLevel <y DBm\] \[-range <MXA range value>\] \[-restoreVSA <0|1>\] \[-xP0ReducedGain <value>\] \[-yP0ReducedGain <value>\]"
set root ""
set description ""
set plotTunes 1
set plotOnly 0
set only ""
set measHarmonic 1315
set measHarmonic 1390
set xTune 0.17
set yTune 0.22
set dividingLine 0.2
set MXA 1
set xSourceLevel 3
set ySourceLevel 0
set xP0ReducedGain 2.0
set yP0ReducedGain 2.0
set xDimtelReducedGain 1
set yDimtelReducedGain 1
set range -3
set restoreVSA 0
set args $argv
APSParseArguments {root description plotTunes only plotOnly measHarmonic dividingLine MXA xSourceLevel ySourceLevel xTune yTune range xP0ReducedGain yP0ReducedGain restoreVSA xDimtelReducedGain yDimtelReducedGain}

if ![string length $root] {
    puts stderr $usage
    puts stderr "Error: root not provided."
    exit 1
}
#if ![string length $only] {
#    puts stderr $usage
#    puts stderr "Error: only (x or y) has to be provided."
#    exit 1
#}
if $restoreVSA {
    puts "restoring VSA (wait 25 seconds) ..."
    if [catch {exec hpVecRestore -filename /home/helios/oagData/sr/hp89441A/states/MXA-TUNE.sta -unit sr} result] {
        puts stderr "Error restoring VSA: $result"
        exit 1
    }
}

if {!$plotOnly} {
    if ![string length $description] {
        puts stderr $usage
        puts stderr "Error: description not provided."
        exit 1
    }
    
    set doX 1
    set doY 1
    switch $only {
        x {
            set doY 0
        }
        y {
            set doX 0
        }
       # "" {
       # }
       # default {
       #     puts stderr "Invalid -only syntax"
       #     puts stderr "$usage"
       #     exit 1
       # }
    }
    
    foreach plane {x y} {
        if ![set do[string toupper $plane]] {
            continue
        }
        if {[file exists $root-$plane.sdds]} {
            puts stderr "File $root-$plane.sdds already exists!"
            exit
        }
        set rfFrequency [exec cavget -list=BRF:S:Hp8657RefFreq -floatFormat=%.1f -pendIoTime=20]
        set harmonic 1296
        set revFrequency [expr $rfFrequency / 1296]
        set sideband -1
        set expectedXtune $xTune
        set xFrequency [expr $revFrequency * ($measHarmonic + $expectedXtune * $sideband)]
        set expectedYtune $yTune
        set yFrequency [expr $revFrequency * ($measHarmonic + $expectedYtune * $sideband)]
        
        set freq [set ${plane}Frequency]
        set freq [set ${plane}Frequency]
        set sourceLevel [set ${plane}SourceLevel]
        
        puts "setup scope..."
        
        if [catch {APSSetupSRVSAScope -sourceLevel $sourceLevel -plane $plane -root $root \
                     -average 20 -freq $freq -MXA $MXA -range $range} result] {
            puts stderr "Error in setting up scope: $result"
            if [catch {APSSetTuneMultiplexer -mode NASA } result] {
                puts stderr "Unable to set VSA mux1 to NASA: $result"
            }
            exit 1
        }
        #after 7000
        puts "collecting data ..."
        if [catch {APSGetSRTuneWaveformHPVSA -output $root-$plane -dividingLine $dividingLine \
                     -xP0ReducedGain $xP0ReducedGain -yP0ReducedGain $yP0ReducedGain \
                     -xDimtelReducedGain $xDimtelReducedGain -yDimtelReducedGain $yDimtelReducedGain \
                     -description  {$description} -MXA $MXA -plane $plane -measHarmonic $measHarmonic \
                 } result] {
            puts stderr "Error in reading MXA waveform data: $result"
            if [catch {APSSetTuneMultiplexer -mode NASA } result] {
                puts stderr "Unable to set VSA mux2: $result"
            }
            exit 1
        }
        
        if [catch {exec sddsconvert $root-$plane $root-$plane.sdds \
                     -editnames=col,${plane}*,%/${plane}// -editnames=par,${plane}*,%/${plane}// } result] {
            puts stderr "Error in processing data: $result"
            if [catch {APSSetTuneMultiplexer -mode NASA } result] {
                 puts stderr "Unable to set VSA mux3: $result"
            }
            exit 1
        }
        exec rm $root-$plane
       # set output$plane ${root}-$plane.sdds
    }
    puts "turn off source ..."
    catch {exec hpSocketSend hpvecsr "OUTP OFF"} result
    if [catch {APSSetTuneMultiplexer -mode NASA } result] {
        puts stderr "Unable to set VSA mux3: $result"
    }
    if [catch {exec cavput -list=SR:TUNE:Mux4Sw2BO=OFF -pend=10} result] {
        puts stderr "Unable to turn off tune switch: $result"
    }
}

if [string length $only] {
    set output [glob ${root}-${only}.sdds]
} else {
    set output [glob ${root}-?.sdds]
}

if $plotTunes {
    foreach file $output {
        if [catch {eval exec sddsplot -topline=@TuneString  \
                     -col=Tune,Waveform $file -grap=line,vary -end \
                     -col=Tune,WaveformPower $file -grap=line,vary -end \
                     & 
        } result ] {
            puts stderr $result
            exit 1
        }
    }
}

exit 0

# Local Variables:
# mode: tcl
# End:
