#!/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 args $argv

set instrument tekdpo9
set plot 0
set filename ""
set channelList {1 2 3 4}
APSParseArguments {instrument filename plot channelList}

set errorMessage "Usage: $argv0 -filename <filename> -instrument <name>"
if {[string length $filename] == 0 } {
    puts stderr $errorMessage
    exit
}

#horizontal - Time parameters
if [catch {exec agilentcomm -ip $instrument -c "HOR?"} horPars] {
    puts stderr "Error reading scope $instrument: $horPars"
    exit 1
}
set dataList [split $horPars ";"]
set delay [lindex $dataList 0]
set sampleRate [lindex $dataList 6]
set div [lindex $dataList 4]
set divs [lindex $dataList 5]
set units [regsub -all {"} [lindex $dataList 7] ""]
set points [lindex $dataList 8]

set timeStamp "[exec date]"
set opt ""
if [catch {exec agilentcomm -ip $instrument -c "DAT:START 1"
    exec agilentcomm -ip $instrument -c "DAT:STOP $points" } result] {
    puts stderr "Error in setup $instrument: $result"
    exit 1
}

foreach chan $channelList {
    puts "getting data for channel $chan..."
    if [catch {exec agilentcomm -ip $instrument -c "DAT:SOU CH$chan" 
        exec agilentcomm -ip $instrument -c "DAT:ENC ASCI" } result] {
        puts stderr "Error setting scope data source to channel $chan: $result"
        exit 1
    }
    after 1000
    if [catch {exec agilentcomm -ip $instrument -c "CURV?"} data] {
        puts stderr "Error reading scope data: $data"
        exit 1
    }
    if [catch {exec agilentcomm -ip $instrument -c "WFMO:YMU?"} scale] {
        puts stderr "Error reading scope data channel scale: $scale"
        exit 1
    }
    if [catch {exec agilentcomm -ip $instrument -c "WFMO:YOF?"} offset] {
        puts stderr "Error reading scope data channel offset: $offset"
        exit 1
    }
    if [catch {exec agilentcomm -ip $instrument -c "WFMO:YUN?"} units1] {
        puts stderr "Error reading scope channel units: $units1"
        exit 1
    }
    
    set units1  [regsub -all {"} $units1 ""]
    set chan${chan}units $units1
    set chan${chan}scale $scale
    set chan${chan}offset $offset
    lappend data1(ColumnNames) Channel$chan
    set data1(ColumnInfo.Channel$chan) "type SDDS_DOUBLE"
    set data1(Column.Channel$chan) [list [split $data ,]]
    lappend opt "-redefine=col,Channel$chan,Channel$chan $offset - $scale *,units=$units1"
}

if [catch {sdds save $filename.1 data1} result] {
    puts stderr "Error saving data: $result"
    exit 1
}
if [catch {eval exec sddsprocess $filename.1 -pipe=out \
            \"-print=par,TimeStamp,[APSMakeSafeQualifierString $timeStamp]\" \
             -define=col,Index,i_row,type=long \
             | sddsprocess -pipe=in $filename \
             "$opt" \
             \"-define=col,Time,Index $sampleRate / $delay +,units=$units\" } result] {
    puts stderr "Error processing data: $result"
    exit 1
}

exec rm $filename.1
if $plot {
    exec sddsplot -col=Time,Chan* -grap=line,vary -leg $filename &
}

exit
