Package dics :: Module temp_plotter
[hide private]
[frames] | no frames]

Source Code for Module dics.temp_plotter

 1  #!/usr/bin/env python 
 2   
 3  import datetime 
 4  import re 
 5  import sys 
 6  import time 
 7   
 8  import matplotlib 
 9  matplotlib.use('GTKAgg') 
10  from matplotlib.backends.backend_gtkagg import FigureCanvasGTKAgg as \ 
11       FigureCanvas 
12  from matplotlib.backends.backend_gtk import NavigationToolbar2GTK as \ 
13       NavigationToolbar 
14  from matplotlib.axes import Subplot 
15  from matplotlib.figure import Figure 
16   
17  import gtk 
18   
19  # Make sure we're been given a filename as a command line argument. 
20  if len(sys.argv) < 2: 
21      print 'Usage: temp_plotter.py <filename>' 
22      sys.exit(2) 
23  # Now attempt to open the file. 
24  filename = sys.argv[1] 
25  try: 
26      log_file = file(filename, 'r') 
27  except IOError: 
28      print 'Error: Failed to open file %s' % filename 
29      sys.exit(1) 
30       
31  # Grab the data and release the file. 
32  lines = log_file.readlines() 
33  log_file.close() 
34   
35  # Now parse the data. 
36  # Need these in order to parse the date/times. 
37  pattern = re.compile(r'(?P<time>^\d+\w+\d+\s+\d+:\d+:\d+)\s+' \ 
38                       r'(?P<cjt>\d+\.\d*)\s+' \ 
39                       r'(?:(\d+\.\d*)(?:\s+))+') 
40  timefmt = '%d%b%y %H:%M:%S' 
41   
42  # Run the lines through the regex 
43  matches = [pattern.match(line) for line in lines] 
44  # Now construct a data array 
45  data = [\ 
46      # Converting from a string to a datetime.datetime object is stupidly 
47      # awkward, have to use time.strptime to convert into a time.struct_time 
48      # but even that's not usable for constructing a datetime, first convert 
49      # to a POSIX timestamp using time.mktime. 
50          [datetime.datetime.fromtimestamp(\ 
51          time.mktime(time.strptime(match.group('time'), timefmt)) \ 
52          )] \ 
53          + [float(temp) for temp in match.groups()[1:]] \ 
54          for match in matches] 
55   
56  # Now output it. 
57   
58  win = gtk.Window() 
59  win.set_default_size(800,600) 
60  win.set_title("Test temperatures") 
61  win.connect("destroy", lambda x: gtk.main_quit()) 
62   
63  vbox = gtk.VBox() 
64  win.add(vbox) 
65   
66  fig = Figure(figsize=(5,4), dpi=100) 
67  ax = fig.add_subplot(111) 
68   
69  ax.plot_date(matplotlib.dates.date2num([entry[0] for entry in data]), [\ 
70      entry[2] for entry in data], 'b-', label='Temperature 1') 
71  ax.plot_date(matplotlib.dates.date2num([entry[0] for entry in data]), [\ 
72      entry[1] for entry in data], 'r-', label='Cold Junction') 
73  ax.legend() 
74  ax.set_xlabel('Date/time') 
75  ax.set_ylabel('Temperature (degrees Celsius)') 
76   
77  canvas = FigureCanvas(fig) 
78  vbox.pack_start(canvas) 
79   
80  toolbar = NavigationToolbar(canvas, win) 
81  vbox.pack_start(toolbar, False, False) 
82   
83  win.show_all() 
84  gtk.main() 
85