# HG changeset patch # User David Douard # Date 1197914238 -3600 # Node ID 3ccb0023cf41b7d384ca83d72de5dfd7e157a0aa # Parent 2999318b49a2e7d0435ac174ea873eb9de814efc created a HP3562A submodule for specific code diff -r 2999318b49a2 -r 3ccb0023cf41 HP3562A/dump_trace.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HP3562A/dump_trace.py Mon Dec 17 18:57:18 2007 +0100 @@ -0,0 +1,74 @@ +import sys +import time +import gpib + +class HP3562dumper(gpib.GPIB): + + MODES = {'trace': 'DD', + 'state': 'DS', + 'coord': 'DC', + } + + FORMATS = {'binary': 'BN', + 'ascii': 'AS', + 'ansi': 'AN'} + + def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1, + address=0): + super(HP3562dumper, self).__init__(device, baudrate, timeout, address, mode=1) + + def dump(self, mode='trace', format="binary"): + format = format.lower() + mode = mode.lower() + assert mode in self.MODES + assert format in self.FORMATS + cmd = self.MODES[mode] + self.FORMATS[format] + + res = "" + print "command = ", cmd + self._cnx.write('%s\r'%cmd) + i = 0 + while ih', d[2:4])[0] + idx = 4 + tt=0 + header = {} + for i, (nam, dtype, fmt, nbytes) in enumerate(HEADER): + if dtype == str: + val = decode_string(d[idx:]) + else: + if fmt: + v = struct.unpack('>'+fmt, d[idx: idx+nbytes])[0] + if isinstance(dtype, dict): + val = dtype.get(int(v), "N/A") + else: + val = dtype(v) + else: + val = dtype(d[idx: idx+nbytes]) + header[nam] = val + idx += nbytes + resu = [] + for i in range(header["Number of elements"]): + resu.append(decode_float(d[idx: idx+4])) + idx += 4 + return header, numpy.array(resu, dtype=float) + +def format_header(header, head_struct, columns=80): + todisp = [] + for row in head_struct: + key = row[0] + val = header.get(key, "N/A") + if isinstance(val, basestring): + val = repr(val) + else: + val = str(val) + todisp.append((key+":", val)) + maxk = max([len(k) for k, v in todisp]) + maxv = max([len(v) for k, v in todisp]) + fmt = "%%-%ds %%-%ds"%(maxk, maxv) + w = maxk+maxv+4 + ncols = columns/w + nrows = len(todisp)/ncols + print "w=", w + print "ncols=", ncols + print "nrows=", nrows + res = "" + for i in range(nrows): + res += "| ".join([fmt%todisp[j*nrows+i] for j in range(ncols)]) + "\n" + return res + + +if __name__ == "__main__": + import sys + import optparse + opt = optparse.OptionParser("A simple tool for tracing a dumped trace") + opt.add_option('-f', '--filename', default=None, + dest='filename', + help='Output filename. If not set, read from stdin') + opt.add_option('-m', '--mode', default='binary', + dest='mode', + help='Dumping mode (may be "binary" [default], "ascii" or "ansi")', + ) + opt.add_option('-d', '--display-header', default=False, + action="store_true", + dest="displayheader", + help="Display the trace header") + opt.add_option('-P', '--noplot-trace', default=True, + action="store_false", + dest="plot", + help="Do not display the plot of the trace") + opt.add_option('-x', '--xmode', default='lin', + dest='xmode', + help='X coordinate mode (may be "lin" [default] or "log")') + opt.add_option('-y', '--ymode', default='lin', + dest='ymode', + help='Y coordinate mode (may be "lin" [default], "log" or "db")') + + options, argv = opt.parse_args(sys.argv) + + + if options.filename is None: + print "Can't deal stdin for now..." + sys.exit(1) + try: + header, data = decode_trace(open(options.filename, 'rb').read()) + except Exception, e: + print "ERROR: can't read %s an interpret it as a HP3562 trace"%options.filename + print e + sys.exit(1) + + if options.displayheader: + print format_header(header, HEADER, 100) + if options.plot: + f0 = header['Start freq value'] + dx = header['Delta X-axis'] + n = header['Number of elements'] + x = numpy.linspace(f0, f0+dx*n, len(data)) + y = data.copy() + + import pylab + if options.ymode != "lin": + minv = min(y[y>0]) + y[y==0] = minv + y = numpy.log10(y) + if options.ymode == "db": + y = y*10 + pylab.ylabel('db') + pylab.grid() + pylab.plot(x, y) + pylab.xlabel('frequency') + pylab.show() + + diff -r 2999318b49a2 -r 3ccb0023cf41 dump_trace.py --- a/dump_trace.py Fri Dec 14 00:24:11 2007 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,74 +0,0 @@ -import sys -import time -import gpib - -class HP3562dumper(gpib.GPIB): - - MODES = {'trace': 'DD', - 'state': 'DS', - 'coord': 'DC', - } - - FORMATS = {'binary': 'BN', - 'ascii': 'AS', - 'ansi': 'AN'} - - def __init__(self, device="/dev/ttyUSB0", baudrate=115200, timeout=0.1, - address=0): - super(HP3562dumper, self).__init__(device, baudrate, timeout, address, mode=1) - - def dump(self, mode='trace', format="binary"): - format = format.lower() - mode = mode.lower() - assert mode in self.MODES - assert format in self.FORMATS - cmd = self.MODES[mode] + self.FORMATS[format] - - res = "" - print "command = ", cmd - self._cnx.write('%s\r'%cmd) - i = 0 - while ih', d[2:4])[0] - idx = 4 - tt=0 - header = {} - for i, (nam, dtype, fmt, nbytes) in enumerate(HEADER): - if dtype == str: - val = decode_string(d[idx:]) - else: - if fmt: - v = struct.unpack('>'+fmt, d[idx: idx+nbytes])[0] - if isinstance(dtype, dict): - val = dtype.get(int(v), "N/A") - else: - val = dtype(v) - else: - val = dtype(d[idx: idx+nbytes]) - header[nam] = val - idx += nbytes - resu = [] - for i in range(header["Number of elements"]): - resu.append(decode_float(d[idx: idx+4])) - idx += 4 - return header, numpy.array(resu, dtype=float) - -def format_header(header, head_struct, columns=80): - todisp = [] - for row in head_struct: - key = row[0] - val = header.get(key, "N/A") - if isinstance(val, basestring): - val = repr(val) - else: - val = str(val) - todisp.append((key+":", val)) - maxk = max([len(k) for k, v in todisp]) - maxv = max([len(v) for k, v in todisp]) - fmt = "%%-%ds %%-%ds"%(maxk, maxv) - w = maxk+maxv+4 - ncols = columns/w - nrows = len(todisp)/ncols - print "w=", w - print "ncols=", ncols - print "nrows=", nrows - res = "" - for i in range(nrows): - res += "| ".join([fmt%todisp[j*nrows+i] for j in range(ncols)]) + "\n" - return res - - -if __name__ == "__main__": - import sys - import optparse - opt = optparse.OptionParser("A simple tool for tracing a dumped trace") - opt.add_option('-f', '--filename', default=None, - dest='filename', - help='Output filename. If not set, read from stdin') - opt.add_option('-m', '--mode', default='binary', - dest='mode', - help='Dumping mode (may be "binary" [default], "ascii" or "ansi")', - ) - opt.add_option('-d', '--display-header', default=False, - action="store_true", - dest="displayheader", - help="Display the trace header") - opt.add_option('-P', '--noplot-trace', default=True, - action="store_false", - dest="plot", - help="Do not display the plot of the trace") - opt.add_option('-x', '--xmode', default='lin', - dest='xmode', - help='X coordinate mode (may be "lin" [default] or "log")') - opt.add_option('-y', '--ymode', default='lin', - dest='ymode', - help='Y coordinate mode (may be "lin" [default], "log" or "db")') - - options, argv = opt.parse_args(sys.argv) - - - if options.filename is None: - print "Can't deal stdin for now..." - sys.exit(1) - try: - header, data = decode_trace(open(options.filename, 'rb').read()) - except Exception, e: - print "ERROR: can't read %s an interpret it as a HP3562 trace"%options.filename - print e - sys.exit(1) - - if options.displayheader: - print format_header(header, HEADER, 100) - if options.plot: - f0 = header['Start freq value'] - dx = header['Delta X-axis'] - n = header['Number of elements'] - x = numpy.linspace(f0, f0+dx*n, len(data)) - y = data.copy() - - import pylab - if options.ymode != "lin": - minv = min(y[y>0]) - y[y==0] = minv - y = numpy.log10(y) - if options.ymode == "db": - y = y*10 - pylab.ylabel('db') - pylab.grid() - pylab.plot(x, y) - pylab.xlabel('frequency') - pylab.show() - -