diff 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
line wrap: on
line diff
--- a/plotss.py	Wed Sep 28 09:21:31 2011 +0930
+++ b/plotss.py	Wed Sep 28 11:45:52 2011 +0930
@@ -5,36 +5,50 @@
 import pylab
 import sys
 
-def doplot(fname):
-    f = file(fname)
-    opts = {}
-    for line in f:
-        key, value = line.strip().split(' ', 1)
-        try:
-            opts[key] = int(value)
-        except exceptions.ValueError, e:
+class DataFile(object):
+    def __init__(self, fname):
+        f = file(fname)
+        self.opts = {}
+        for line in f:
+            key, value = line.strip().split(' ', 1)
+            if key == "XDATA":
+                self.xdata = numpy.fromstring(value, sep = ', ')
+                continue
+            
+            if key == "YDATA":
+                self.ydata = numpy.fromstring(value, sep = ', ')
+                continue
+            
             try:
-                opts[key] = float(value)
+                self.opts[key] = int(value)
             except exceptions.ValueError, e:
-                opts[key] = value
-        
-    xdata = numpy.fromstring(opts['XDATA'], sep = ', ')
-    ydata = numpy.fromstring(opts['YDATA'], sep = ', ')
-    xdata /= 1e6
-    pylab.title("Tag \'" + opts['TAG'] + "\' at " + opts['TIMESTAMP'])
+                try:
+                    self.opts[key] = float(value)
+                except exceptions.ValueError, e:
+                    self.opts[key] = value
+
+    def __getitem__(self, k):
+        return self.opts[k]
+    
+def doplot(fname):
+    dfile = DataFile(fname)
+    
+    xdata = dfile.xdata / 1e6
+    ydata = dfile.ydata
+    pylab.title("Tag \'" + dfile['TAG'] + "\' at " + dfile['TIMESTAMP'])
     pylab.xlabel("Frequency (MHz)")
     pylab.ylabel("Level (dBm)")
     annstr = "FStart\t%.2f MHz\nFStop\t%.2f MHz\nPoints\t%d" % (
-        float(opts['FSTART']) / 1e6,
-        float(opts['FSTOP']) / 1e6,
+        float(dfile['FSTART']) / 1e6,
+        float(dfile['FSTOP']) / 1e6,
         len(xdata))
     pylab.annotate(annstr, xy=(5, -40),  xycoords='axes points')
 
     annstr = "Video BW\t%.1f kHz\nResol. BW\t%.1f kHz\nAttenuation\t%.1f dB\nRef Level\t%.1f dBm" % (
-        float(opts['VIDBW']) / 1e3,
-        float(opts['RESBW']) / 1e3,
-        float(opts['ATTEN']),
-        float(opts['REFLEV']))        
+        float(dfile['VIDBW']) / 1e3,
+        float(dfile['RESBW']) / 1e3,
+        float(dfile['ATTEN']),
+        float(dfile['REFLEV']))        
     pylab.annotate(annstr, xy=(-140, -55),  xycoords='axes points')
     pylab.grid(True)
     pylab.plot(xdata, ydata, linestyle='solid', marker='.')