-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtdsRAW2CSV.rb
213 lines (195 loc) · 11.3 KB
/
tdsRAW2CSV.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env -S ruby
# -*- Mode:ruby; Coding:us-ascii-unix; fill-column:158 -*-
#########################################################################################################################################################.H.S.##
##
# @file tdsRAW2CSV.rb
# @author Mitch Richling http://www.mitchr.me/
# @brief Convert RAW preamble & waveform data from Tektronix TDS 2000 & 3000B series oscilloscopes into a CSV files.@EOL
# @std Ruby_3
# @copyright
# @parblock
# Copyright (c) 2023, Mitchell Jay Richling <http://www.mitchr.me/> All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions, and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions, and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# @endparblock
#########################################################################################################################################################.H.E.##
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
require 'optparse'
require 'optparse/time'
require 'fileutils'
require 'set'
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
# Process start time
processStartTime = Time.new
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
# Print stuff to STDOUT & STDERR immediately -- important on windows
$stdout.sync = true
$stderr.sync = true
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
# Get options
adjV = true # Convert samples to voltage, or just integers
outTitle = true # Output titles
outSep = ',' # Output separator
verbose = 1 # Verbose level
outFileN = nil
preFileN = nil
opts = OptionParser.new do |opts|
opts.banner = ""
opts.separator("Transform Tektronix waveform data into CSV ")
opts.separator(" ")
opts.separator("Usage: sdsRAW2CSV.rb [options] -p preamble_file data_file ")
opts.separator(" ")
opts.separator(" Input Files: ")
opts.separator(" preamble_file .. The resuls of a :WFMPRe? call. ")
opts.separator(" data_file ...... The resuls of a :CURVe? call. ")
opts.separator(" ")
opts.separator(" Options: ")
opts.on("-h", "--help", "Show this message ") { STDERR.puts opts; exit }
opts.on("-v INT", "--verbose INT", "Verbose level ") { |v| verbose = v.to_i }
opts.separator(" 1 - Errors ")
opts.separator(" 2 - Warnings ")
opts.separator(" 3 - Progress (DEFAULT) ")
opts.separator(" 5 - Arguments ")
opts.separator(" 7 - Metadata ")
opts.on("-t Y/N", "--title Y/N", "Print titles (DEFAULT: Y) ") { |v| outTitle = !!(v.match(/^[yt1]/i)) }
opts.on("-s SEP", "--separator SEP", "Separator (DEFAULT: comma) ") { |v| outSep = v }
opts.on("-p FILE", "--preamble FILE", "File containing preamble data ") { |v| preFileN = v }
opts.on("-o FILE", "--output FILE", "Output file ") { |v| outFileN = v }
opts.separator(" ")
opts.separator("Only tested on the TDS2000 & TDS3000B series. ")
opts.separator(" ")
end
opts.parse!(ARGV)
if ( !(outFileN)) then
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - ERROR(1): Must provide an output file!\n\n")
STDERR.puts(opts)
exit
end
if ( !(preFileN)) then
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - ERROR(1): Must provide a preamble file!\n\n")
STDERR.puts(opts)
exit
end
outFileD = nil
if (outFileD == '-') then
outFileD = STDOUT
else
outFileD = open(outFileN, "wb");
end
dataFileName = nil
if (ARGV.length < 1) then
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - ERROR(1): Must provide a waveform data file!\n\n")
STDERR.puts(opts)
exit
elsif (ARGV.length > 1) then
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - ERROR(1): May provide only one waveform data file!\n\n")
STDERR.puts(opts)
exit
else
dataFileName = ARGV[0]
end
if verbose >= 5 then
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - INFO(5): outTitle ....... #{outTitle.inspect }")
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - INFO(5): outSep ......... #{outSep.inspect }")
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - INFO(5): verbose ........ #{verbose.inspect }")
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - INFO(5): outFileN ....... #{outFileN.inspect }")
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - INFO(5): preFileN ....... #{preFileN.inspect }")
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - INFO(5): dataFileName ... #{dataFileName.inspect }")
end
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
if verbose >= 3 then
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - INFO(3): Read, Parse, & Extract preamble data")
end
preamble_data_hash = ['BYT_Nr', 'BIT_Nr', 'ENCdg', 'BN_Fmt',
'BYT_Or', 'NR_Pt', 'WFID', 'PT_FMT',
'XINcr', 'PT_Off', 'XZERo', 'XUNit',
'YMUlt', 'YZEro', 'YOFf', 'YUNit'].zip(File.read(preFileN, :encoding=>'binary').strip.split(';').map {|x| x.strip.sub(/^.* */, '')}).to_h
# Report on preamble data
if verbose >= 7 then
['BN_Fmt', 'BYT_Nr', 'BYT_Or', 'ENCdg', 'PT_FMT', 'PT_Off', 'XINcr', 'XZERo', 'YMUlt', 'YOFf', 'YZERo'].each do |k|
STDERR.printf("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - INFO(7): %9s : %s\n", k, preamble_data_hash[k])
end
end
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
if verbose >= 3 then
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - INFO(3): Reading and converting Y data")
end
yDataPts = nil
if (preamble_data_hash['ENCdg'] == 'BIN') then
if (preamble_data_hash['BYT_Nr'] == '1') then
if (preamble_data_hash['BN_Fmt'] == 'RP') then
wave_unpack_code = 'C*' # 1 byte, Unsigned, no-endian
else
wave_unpack_code = 'c*' # 1 byte, Signed, no-endian
end
else
if (preamble_data_hash['BYT_Or'] == 'LSB') then
if (preamble_data_hash['BN_Fmt'] == 'RP') then
wave_unpack_code = 'S<*' # 2 byte, Unsigned, little-endian
else
wave_unpack_code = 's<*' # 2 byte, Signed, little-endian
end
else
if (preamble_data_hash['BN_Fmt'] == 'RP') then
wave_unpack_code = 'S>*' # 2 byte, Unsigned, big-endian
else
wave_unpack_code = 's>*' # 2 byte, Signed, big-endian
end
end
end
# Binary waveform data starts with a variable length ASCII header starting!! The header starts with the literal '#' character. This is followed by a
# single ASCII digit indicating the number of digits that follow. The remaining characters are ASCII digits indicating the number of bytes that follow. We
# use this length to avoid converting any extra data at the end of the record (like a newline).
tmp = File.read(dataFileName, :encoding=>'binary')
yDataPts = tmp[(2+tmp[1].to_i)..((1+tmp[1].to_i) + tmp[2..(1+tmp[1].to_i)].to_i)].unpack(wave_unpack_code)
else
yDataPts = File.read(dataFileName, :encoding=>'binary').chomp.sub(/^.*:CURVE */, '').split(',')
end
if verbose >= 3 then
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - INFO(3): Found #{yDataPts.length} points")
end
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
if verbose >= 3 then
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - INFO(3): Printing Data")
end
if outTitle then
if (preamble_data_hash['PT_FMT'] == 'Y') then
outFileD.puts("t#{outSep}v")
else
outFileD.puts("t#{outSep}v1#{outSep}v2")
end
end
xData = 1
while (xData <= yDataPts.length) do
yData = yDataPts[xData - 1]
xpt = preamble_data_hash['XZERo'].to_f + ( xData.to_f - preamble_data_hash['PT_Off'].to_f ) * preamble_data_hash['XINcr'].to_f
ypt = preamble_data_hash['YZERo'].to_f + ( yData.to_f - preamble_data_hash['YOFf'].to_f ) * preamble_data_hash['YMUlt'].to_f
xData += 1
if (preamble_data_hash['PT_FMT'] == 'Y') then
outFileD.printf("%0.5g#{outSep}%0.5g\n", xpt, ypt)
else
yData = yDataPts[xData - 1]
ypt2 = preamble_data_hash['YZERo'].to_f + ( yData.to_f - preamble_data_hash['YOFf'].to_f ) * preamble_data_hash['YMUlt'].to_f
outFileD.printf("%0.5g#{outSep}%0.5g#{outSep}%0.5g\n", xpt, ypt, ypt2)
xData += 1;
end
end
#---------------------------------------------------------------------------------------------------------------------------------------------------------------
if verbose >= 3 then
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - INFO(3): Total runtime: #{Time.new - processStartTime}")
STDERR.puts("tdsRAW2CSV: #{Time.new.inspect.ljust(35, ' ')} - INFO(3): Finished")
end