#!/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 "getVXATraceData -filename <string>"
set filename ""
set verbose 0
set args $argv
set instName 10.6.37.49
APSParseArguments {filename measTune verbose instName}
if {$filename == ""} {
    puts stderr $usage
    exit
}
#set command0 ":WAVEFORM:DATA?"
#set command1 "*LRN?"
#set command2 "*IDN?"
#set command3 ":SYST:VERS?"
#set command4 ":SYST:TIME?"
set command ":CALC:VECT:DATA1?"
# this is the MXA or VXA?

# mode will be either VSA or SA
if [catch {exec hpSocketSend $instName ":INSTrument:Select?"} instrumentMode] {
    puts stderr "Problem with getting instrument mode"
}

# parse for measTune but don't use it anymore

#exec hpSocketSend $instName ":SENS:VECT:AVER:STAT OFF"
#exec hpSocketSend $instName ":VECT:AVER:COUN 20"
#exec hpSocketSend $instName ":SENS:VECT:AVER:STAT ON"
#after 3000 

set startFreq [exec hpSocketSend $instName ":SENS:FREQ:STAR?"]
set endFreq   [exec hpSocketSend $instName ":SENS:FREQ:STOP?"]
switch $instrumentMode {
    VSA {
        set RBW   [exec hpSocketSend $instName "VECT:BANDwidth:RESolution?"]
        set command ":CALC:VECT:DATA1?"
    }
    SA {
        set RBW   [exec hpSocketSend $instName "BANDwidth:RESolution?"]
        set command "TRACE:DATA? TRACE1"
    }
    VECT {
        #hpvec par vect mode command is as following
        set RBW   [exec hpSocketSend $instName "BANDwidth:RESolution?"]
        set command "CALC:DATA?"
    }
}

set fh [open ${filename}.1 w]
set response [exec hpSocketSend $instName $command ] 
regsub -all {,} $response "\n" response
puts $fh $response
close $fh

if [catch {exec csv2sdds \
             -col=name=WaveformPower,type=double,units=dBm \
             ${filename}.1 -pipe=out \
             | sddsprocess -pipe -define=par,Points,n_rows \
             | sddsprocess \
             -print=para,InstrumentMode,$instrumentMode \
             -def=para,StartFrequency,$startFreq,units=Hz \
             -def=para,EndFrequency,$endFreq,units=Hz \
             -def=para,RBW,$RBW,units=Hz \
             "-define=col,Frequency,$startFreq $endFreq $startFreq - Points 1 - / i_row * + 1.0e6 /,units=MHz" \
             "-define=col,Waveform,10 WaveformPower 20 / 0.5 - pow,units=V" \
             -pipe=in $filename } result] {
    puts stderr "Error in getVXATraceData: $result"
    exit 1
}

file delete -force ${filename}.1
exit 0


# Local Variables:
# mode: tcl
# End:
