Wed, 16 Jan 2008 01:21:06 +0100
add some example hpgl files
0 | 1 | # -*- coding: utf-8 -*- |
14
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
2 | """ |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
3 | trace_decoder |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
4 | ============= |
0 | 5 | |
14
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
6 | Module for decoding a trace generated by the HP3562A DSA, using the |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
7 | GPIB command "DDBN" (Dump Data BiNary). |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
8 | |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
9 | This file can be exectued as a python script. Use '-h' for more informations. |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
10 | """ |
0 | 11 | import struct |
10
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
12 | import numpy |
16
de9122b5680a
Several improvements (code refactoring) + add stuff to decode binary coordinate data block ("DCBN" command)
David Douard <david.douard@logilab.fr>
parents:
14
diff
changeset
|
13 | from HP3562A import format_header, decode_float, decode_string, decode_header, read_trace |
14
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
14 | from HP3562A.enum_types import * |
0 | 15 | |
16 | ||
17 | HEADER = [ ("Display function", EDSP, 'h', 2), | |
18 | ('Number of elements', int, 'h', 2), | |
19 | ('Displayed elements', int, 'h', 2), | |
20 | ('Number of averages', int, 'h', 2), | |
21 | ('Channel selection', ECH, 'h', 2), | |
22 | ('Overflow status', EOVR, 'h', 2), | |
23 | ('Overlap percentage', int, 'h', 2), | |
24 | ('Domain', EDOM, 'h', 2), | |
25 | ('Volts peak/rms', EVLT, 'h', 2), | |
26 | ('Amplitude units', EAMP, 'h', 2), | |
27 | ('X axis units', EXAXIS, 'h', 2), | |
28 | ('Auto math label', str, 's', 14), | |
29 | ('Trace label', str, 's', 22), | |
30 | ('EU label 1', str, 's', 6), | |
31 | ('EU label 2', str, 's', 6), | |
32 | ('Float/Interger', bool, 'h', 2), | |
33 | ('Complex/Real', bool, 'h', 2), | |
34 | ('Live/Recalled', bool, 'h', 2), | |
35 | ('Math result', bool, 'h', 2), | |
36 | ('Real/Complex input', bool, 'h', 2), | |
37 | ('Log/Linear data', bool, 'h', 2), | |
38 | ('Auto math', bool, 'h', 2), | |
39 | ('Real time status', bool, 'h', 2), | |
40 | ('Measurement mode', EMEAS, 'h', 2), | |
41 | ('Window', EWIN, 'h', 2), | |
14
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
42 | ('Demod type channel 1', EDEMOD, 'h', 2), |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
43 | ('Demod type channel 2', EDEMOD, 'h', 2), |
0 | 44 | ('Demod active channel 1', bool, 'h', 2), |
45 | ('Demod active channel 2', bool, 'h', 2), | |
46 | ('Average status', EAVG, 'h', 2), | |
47 | ('Not used', int, 'hh', 4), | |
48 | ('Samp freq/2 (real)', decode_float, None, 4), | |
49 | ('Samp freq/2 (imag)', decode_float, None, 4), | |
50 | ('Not used', decode_float, None, 4), | |
51 | ('Delta X-axis', decode_float, None, 4), | |
52 | ('Max range', decode_float, None, 4), | |
53 | ('Start time value', decode_float, None, 4), | |
54 | ('Expon wind const 1', decode_float, None, 4), | |
55 | ('Expon wind const 2', decode_float, None, 4), | |
56 | ('EU value chan 1', decode_float, None, 4), | |
57 | ('EU value chan 2', decode_float, None, 4), | |
58 | ('Trig delay chan 1', decode_float, None, 4), | |
59 | ('Trig delay chan 2', decode_float, None, 4), | |
60 | ('Start freq value', decode_float, None, 8), | |
61 | ('Start data value', decode_float, None, 8), | |
62 | ] | |
63 | ||
16
de9122b5680a
Several improvements (code refactoring) + add stuff to decode binary coordinate data block ("DCBN" command)
David Douard <david.douard@logilab.fr>
parents:
14
diff
changeset
|
64 | def decode_trace(data, idx=0): |
14
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
65 | """ |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
66 | Decode the data (as generated by the HP3562A DSA in response to a |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
67 | "DDBN" command), and returns a couple (header, values). |
0 | 68 | |
14
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
69 | header is the dictionnary of the header of the dumped data block, |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
70 | value is a numpy array holding the trace (vector of float or |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
71 | complex values). |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
72 | """ |
16
de9122b5680a
Several improvements (code refactoring) + add stuff to decode binary coordinate data block ("DCBN" command)
David Douard <david.douard@logilab.fr>
parents:
14
diff
changeset
|
73 | header, idx = decode_header(data, HEADER, idx) |
de9122b5680a
Several improvements (code refactoring) + add stuff to decode binary coordinate data block ("DCBN" command)
David Douard <david.douard@logilab.fr>
parents:
14
diff
changeset
|
74 | return header, read_trace(data, idx, header["Number of elements"]) |
14
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
75 | |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
76 | def main(): |
0 | 77 | import sys |
10
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
78 | import optparse |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
79 | opt = optparse.OptionParser("A simple tool for tracing a dumped trace") |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
80 | opt.add_option('-f', '--filename', default=None, |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
81 | dest='filename', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
82 | help='Output filename. If not set, read from stdin') |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
83 | opt.add_option('-m', '--mode', default='binary', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
84 | dest='mode', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
85 | help='Dumping mode (may be "binary" [default], "ascii" or "ansi")', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
86 | ) |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
87 | opt.add_option('-d', '--display-header', default=False, |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
88 | action="store_true", |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
89 | dest="displayheader", |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
90 | help="Display the trace header") |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
91 | opt.add_option('-P', '--noplot-trace', default=True, |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
92 | action="store_false", |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
93 | dest="plot", |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
94 | help="Do not display the plot of the trace") |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
95 | opt.add_option('-x', '--xmode', default='lin', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
96 | dest='xmode', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
97 | help='X coordinate mode (may be "lin" [default] or "log")') |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
98 | opt.add_option('-y', '--ymode', default='lin', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
99 | dest='ymode', |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
100 | help='Y coordinate mode (may be "lin" [default], "log" or "db")') |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
101 | |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
102 | options, argv = opt.parse_args(sys.argv) |
0 | 103 | |
104 | ||
10
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
105 | if options.filename is None: |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
106 | print "Can't deal stdin for now..." |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
107 | sys.exit(1) |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
108 | try: |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
109 | header, data = decode_trace(open(options.filename, 'rb').read()) |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
110 | except Exception, e: |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
111 | print "ERROR: can't read %s an interpret it as a HP3562 trace"%options.filename |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
112 | print e |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
113 | sys.exit(1) |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
114 | |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
115 | if options.displayheader: |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
116 | print format_header(header, HEADER, 100) |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
117 | if options.plot: |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
118 | f0 = header['Start freq value'] |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
119 | dx = header['Delta X-axis'] |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
120 | n = header['Number of elements'] |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
121 | x = numpy.linspace(f0, f0+dx*n, len(data)) |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
122 | y = data.copy() |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
123 | |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
124 | import pylab |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
125 | if options.ymode != "lin": |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
126 | minv = min(y[y>0]) |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
127 | y[y==0] = minv |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
128 | y = numpy.log10(y) |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
129 | if options.ymode == "db": |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
130 | y = y*10 |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
131 | pylab.ylabel('db') |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
132 | pylab.grid() |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
133 | pylab.plot(x, y) |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
134 | pylab.xlabel('frequency') |
2999318b49a2
many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents:
6
diff
changeset
|
135 | pylab.show() |
0 | 136 | |
137 | ||
14
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
138 | if __name__ == "__main__": |
07e2cbf140df
several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents:
11
diff
changeset
|
139 | main() |