224 ('Trig delay chan 2', decode_float, None, 4), |
225 ('Trig delay chan 2', decode_float, None, 4), |
225 ('Start freq value', decode_float, None, 8), |
226 ('Start freq value', decode_float, None, 8), |
226 ('Start data value', decode_float, None, 8), |
227 ('Start data value', decode_float, None, 8), |
227 ] |
228 ] |
228 |
229 |
229 def decode_file(filename): |
230 def decode_trace(data): |
230 d = open(filename).read() |
231 d = data |
231 |
232 |
232 typ = d[:2] |
233 typ = d[:2] |
233 assert typ == "#A" |
234 assert typ == "#A" |
234 |
235 |
235 totlen = struct.unpack('>h', d[2:4])[0] |
236 totlen = struct.unpack('>h', d[2:4])[0] |
246 val = dtype.get(int(v), "N/A") |
247 val = dtype.get(int(v), "N/A") |
247 else: |
248 else: |
248 val = dtype(v) |
249 val = dtype(v) |
249 else: |
250 else: |
250 val = dtype(d[idx: idx+nbytes]) |
251 val = dtype(d[idx: idx+nbytes]) |
251 |
|
252 print idx, hex(idx), nam, ":", val |
|
253 header[nam] = val |
252 header[nam] = val |
254 idx += nbytes |
253 idx += nbytes |
255 |
|
256 resu = [] |
254 resu = [] |
257 |
|
258 for i in range(header["Number of elements"]): |
255 for i in range(header["Number of elements"]): |
259 resu.append(decode_float(d[idx: idx+4])) |
256 resu.append(decode_float(d[idx: idx+4])) |
260 idx += 4 |
257 idx += 4 |
261 |
258 return header, numpy.array(resu, dtype=float) |
262 #print "resu = ", resu |
259 |
263 #return |
260 def format_header(header, head_struct, columns=80): |
264 import pylab |
261 todisp = [] |
265 import numpy |
262 for row in head_struct: |
266 resu = numpy.array(resu, dtype=float) |
263 key = row[0] |
267 print "max = ", max(resu) |
264 val = header.get(key, "N/A") |
268 #xr = numpy.linspace(0, header['Delta X-axis'], len(resu)) |
265 if isinstance(val, basestring): |
269 sf = header['Start freq value'] |
266 val = repr(val) |
270 xr = numpy.linspace(sf, sf+20000, len(resu)) |
267 else: |
271 mn = min(resu[resu>0]) |
268 val = str(val) |
272 resu[resu==0] = mn |
269 todisp.append((key+":", val)) |
273 pylab.plot(xr, 10*numpy.log10(resu)) |
270 maxk = max([len(k) for k, v in todisp]) |
274 pylab.show() |
271 maxv = max([len(v) for k, v in todisp]) |
275 |
272 fmt = "%%-%ds %%-%ds"%(maxk, maxv) |
276 # tt=0 |
273 w = maxk+maxv+4 |
277 # for i, (nam, dtype, nbytes) in enumerate(HEADER): |
274 ncols = columns/w |
278 # if dtype == str: |
275 nrows = len(todisp)/ncols |
279 # nb = ord(struct.unpack('c', d[idx])[0]) |
276 print "w=", w |
280 # val = d[idx+1:idx+1+nb] |
277 print "ncols=", ncols |
281 # else: |
278 print "nrows=", nrows |
282 # v = struct.unpack('>d', d[idx: idx+(nbytes*4)])[0] |
279 res = "" |
283 # if isinstance(dtype, dict): |
280 for i in range(nrows): |
284 # val = dtype.get(int(v), "N/A") |
281 res += "| ".join([fmt%todisp[j*nrows+i] for j in range(ncols)]) + "\n" |
285 # else: |
282 return res |
286 # val = dtype(v) |
|
287 # print idx, nam, ":", val |
|
288 # idx += nbytes*4 |
|
289 |
|
290 |
|
291 |
283 |
292 |
284 |
293 if __name__ == "__main__": |
285 if __name__ == "__main__": |
294 import sys |
286 import sys |
295 decode_file(sys.argv[1]) |
287 import optparse |
296 |
288 opt = optparse.OptionParser("A simple tool for tracing a dumped trace") |
297 |
289 opt.add_option('-f', '--filename', default=None, |
|
290 dest='filename', |
|
291 help='Output filename. If not set, read from stdin') |
|
292 opt.add_option('-m', '--mode', default='binary', |
|
293 dest='mode', |
|
294 help='Dumping mode (may be "binary" [default], "ascii" or "ansi")', |
|
295 ) |
|
296 opt.add_option('-d', '--display-header', default=False, |
|
297 action="store_true", |
|
298 dest="displayheader", |
|
299 help="Display the trace header") |
|
300 opt.add_option('-P', '--noplot-trace', default=True, |
|
301 action="store_false", |
|
302 dest="plot", |
|
303 help="Do not display the plot of the trace") |
|
304 opt.add_option('-x', '--xmode', default='lin', |
|
305 dest='xmode', |
|
306 help='X coordinate mode (may be "lin" [default] or "log")') |
|
307 opt.add_option('-y', '--ymode', default='lin', |
|
308 dest='ymode', |
|
309 help='Y coordinate mode (may be "lin" [default], "log" or "db")') |
|
310 |
|
311 options, argv = opt.parse_args(sys.argv) |
|
312 |
|
313 |
|
314 if options.filename is None: |
|
315 print "Can't deal stdin for now..." |
|
316 sys.exit(1) |
|
317 try: |
|
318 header, data = decode_trace(open(options.filename, 'rb').read()) |
|
319 except Exception, e: |
|
320 print "ERROR: can't read %s an interpret it as a HP3562 trace"%options.filename |
|
321 print e |
|
322 sys.exit(1) |
|
323 |
|
324 if options.displayheader: |
|
325 print format_header(header, HEADER, 100) |
|
326 if options.plot: |
|
327 f0 = header['Start freq value'] |
|
328 dx = header['Delta X-axis'] |
|
329 n = header['Number of elements'] |
|
330 x = numpy.linspace(f0, f0+dx*n, len(data)) |
|
331 y = data.copy() |
|
332 |
|
333 import pylab |
|
334 if options.ymode != "lin": |
|
335 minv = min(y[y>0]) |
|
336 y[y==0] = minv |
|
337 y = numpy.log10(y) |
|
338 if options.ymode == "db": |
|
339 y = y*10 |
|
340 pylab.ylabel('db') |
|
341 pylab.grid() |
|
342 pylab.plot(x, y) |
|
343 pylab.xlabel('frequency') |
|
344 pylab.show() |
298 |
345 |
299 |
346 |