comparison plotss.py @ 36:ff63d71e1383

Separate out data file reader into a new class for ease of reuse.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 28 Sep 2011 11:45:52 +0930
parents e80c2ff5fa87
children 3d2306e39700
comparison
equal deleted inserted replaced
35:eaee6ea8d14e 36:ff63d71e1383
3 import exceptions 3 import exceptions
4 import numpy 4 import numpy
5 import pylab 5 import pylab
6 import sys 6 import sys
7 7
8 class DataFile(object):
9 def __init__(self, fname):
10 f = file(fname)
11 self.opts = {}
12 for line in f:
13 key, value = line.strip().split(' ', 1)
14 if key == "XDATA":
15 self.xdata = numpy.fromstring(value, sep = ', ')
16 continue
17
18 if key == "YDATA":
19 self.ydata = numpy.fromstring(value, sep = ', ')
20 continue
21
22 try:
23 self.opts[key] = int(value)
24 except exceptions.ValueError, e:
25 try:
26 self.opts[key] = float(value)
27 except exceptions.ValueError, e:
28 self.opts[key] = value
29
30 def __getitem__(self, k):
31 return self.opts[k]
32
8 def doplot(fname): 33 def doplot(fname):
9 f = file(fname) 34 dfile = DataFile(fname)
10 opts = {} 35
11 for line in f: 36 xdata = dfile.xdata / 1e6
12 key, value = line.strip().split(' ', 1) 37 ydata = dfile.ydata
13 try: 38 pylab.title("Tag \'" + dfile['TAG'] + "\' at " + dfile['TIMESTAMP'])
14 opts[key] = int(value)
15 except exceptions.ValueError, e:
16 try:
17 opts[key] = float(value)
18 except exceptions.ValueError, e:
19 opts[key] = value
20
21 xdata = numpy.fromstring(opts['XDATA'], sep = ', ')
22 ydata = numpy.fromstring(opts['YDATA'], sep = ', ')
23 xdata /= 1e6
24 pylab.title("Tag \'" + opts['TAG'] + "\' at " + opts['TIMESTAMP'])
25 pylab.xlabel("Frequency (MHz)") 39 pylab.xlabel("Frequency (MHz)")
26 pylab.ylabel("Level (dBm)") 40 pylab.ylabel("Level (dBm)")
27 annstr = "FStart\t%.2f MHz\nFStop\t%.2f MHz\nPoints\t%d" % ( 41 annstr = "FStart\t%.2f MHz\nFStop\t%.2f MHz\nPoints\t%d" % (
28 float(opts['FSTART']) / 1e6, 42 float(dfile['FSTART']) / 1e6,
29 float(opts['FSTOP']) / 1e6, 43 float(dfile['FSTOP']) / 1e6,
30 len(xdata)) 44 len(xdata))
31 pylab.annotate(annstr, xy=(5, -40), xycoords='axes points') 45 pylab.annotate(annstr, xy=(5, -40), xycoords='axes points')
32 46
33 annstr = "Video BW\t%.1f kHz\nResol. BW\t%.1f kHz\nAttenuation\t%.1f dB\nRef Level\t%.1f dBm" % ( 47 annstr = "Video BW\t%.1f kHz\nResol. BW\t%.1f kHz\nAttenuation\t%.1f dB\nRef Level\t%.1f dBm" % (
34 float(opts['VIDBW']) / 1e3, 48 float(dfile['VIDBW']) / 1e3,
35 float(opts['RESBW']) / 1e3, 49 float(dfile['RESBW']) / 1e3,
36 float(opts['ATTEN']), 50 float(dfile['ATTEN']),
37 float(opts['REFLEV'])) 51 float(dfile['REFLEV']))
38 pylab.annotate(annstr, xy=(-140, -55), xycoords='axes points') 52 pylab.annotate(annstr, xy=(-140, -55), xycoords='axes points')
39 pylab.grid(True) 53 pylab.grid(True)
40 pylab.plot(xdata, ydata, linestyle='solid', marker='.') 54 pylab.plot(xdata, ydata, linestyle='solid', marker='.')
41 pylab.show() 55 pylab.show()
42 56