#!/usr/bin/env python # encoding: utf8 # # Autor: Markus Feldmann # import wx from matplotlib.figure import Figure from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg import cPickle import pickle import time import numpy import os class HistoApp(wx.App): def OnInit(self): f_daten = file('../../../peakdaten.dat','r') starttime, endtime, samp_rate = cPickle.load(f_daten) f_daten.close() wx.EVT_CLOSE(self, self.OnQuit) timescale, nitems = self.gettimescale(starttime, endtime) print "Mehrfrequente Messung",\ "\nStartzeit: ",time.ctime(float(starttime)),\ "\nEndzeit: ",time.ctime(float(endtime)),\ "\nGesamte Zeitspanne: ",float(endtime - starttime)," sek.",\ "\nZeitdifferenz von Sample zum nächsten: ",float(timescale)," sek.",\ "\nAnzahl Samples: ",nitems print 'Stufe 1' self.extrahierePeaks(starttime,endtime,timescale,nitems) print 'Stufe 2' self.frame = HistoFrame(parent=None,\ title='Histogramm einer Mehrfrequenten Impulsmessung',\ size=(640, 480)) self.frame.Show() return True def gettimescale(self,starttime, endtime): filename = '../../../peakdetektor1.hex' file_size = os.path.getsize(filename) nitems = 0 nitems = file_size if nitems is not 0: #Zeitabstand von einem Sample zum naechsten timescale = float(endtime - starttime)/float(nitems) else: timescale = float(endtime - starttime) return timescale, nitems def extrahierePeaks(self,starttime,endtime,timescale,nitems): f_output = open('../../../peakswerteundzeit.dat','wb') f_p_1 = open('../../../peakdetektor1.hex','rb') f_p_2 = open('../../../peakdetektor2.hex','rb') f_p_3 = open('../../../peakdetektor3.hex','rb') f_pv_1 = open('../../../peakband1.hex','rb') f_pv_2 = open('../../../peakband2.hex','rb') f_pv_3 = open('../../../peakband3.hex','rb') a_p_1 = numpy.fromfile(f_p_1,dtype='b',count=-1) a_p_2 = numpy.fromfile(f_p_2,dtype='b',count=-1) a_p_3 = numpy.fromfile(f_p_3,dtype='b',count=-1) a_pv_1 = numpy.fromfile(f_pv_1,dtype='f',count=-1) a_pv_2 = numpy.fromfile(f_pv_2,dtype='f',count=-1) a_pv_3 = numpy.fromfile(f_pv_3,dtype='f',count=-1) i = 0 a_time = [] a_a = [] a_b = [] a_c = [] a_ar = [] a_ai = [] a_br = [] a_bi = [] a_cr = [] a_ci = [] while i <= nitems-1: if (a_p_1[i] or a_p_2[i] or a_p_3[i]): a_time += [timescale*i] a_a.append(a_p_1[i]) # print "Mit i ist ",i," ergibt sich für a_p_1[i] ",a_p_1[i],\ # "und für a_a ",a_a[:] a_b.append(a_p_2[i]) a_c.append(a_p_3[i]) a_ar.append(a_pv_1[i*2]) a_ai.append(a_pv_1[i*2+1]) a_br.append(a_pv_2[i*2]) a_bi.append(a_pv_2[i*2+1]) a_cr.append(a_pv_3[i*2]) a_ci.append(a_pv_3[i*2+1]) i += 1 cPickle.dump(a_time,f_output) cPickle.dump(a_a,f_output) cPickle.dump(a_b,f_output) cPickle.dump(a_c,f_output) cPickle.dump(a_ar,f_output) cPickle.dump(a_ai,f_output) cPickle.dump(a_br,f_output) cPickle.dump(a_bi,f_output) cPickle.dump(a_cr,f_output) cPickle.dump(a_ci,f_output) f_output.flush() f_output.close() def OnQuit(self): f_peaks_1.close() f_peaks_2.close() f_peaks_3.close() f_peakvalue_1.close() f_peakvalue_2.close() f_peakvalue_3.close() class HistoFrame(wx.Frame): def __init__(self, *args, **kwargs): wx.Frame.__init__(self, *args, **kwargs) self.Bind(wx.EVT_MENU, self.onQuit, id=105) self.makeMenu() self.graphWindow = GraphWindow(self) def makeMenu(self): menubar = wx.MenuBar() file = wx.Menu() quit = wx.MenuItem(file, 105, '&Quit\tCtrrl+Q', 'Quit the Application') file.AppendItem(quit) menubar.Append(file, '&File') self.SetMenuBar(menubar) def onQuit(self,event): self.Close() class GraphWindow(wx.Window): def __init__(self, *args, **kwargs): wx.Window.__init__(self, *args, **kwargs) self.lines = [] self.figure = Figure() self.canvas = FigureCanvasWxAgg(self, -1, self.figure) f_dat = open('../../../peakswerteundzeit.dat','rb') nitems = 2 self.a_time = cPickle.load(f_dat) self.a_a = cPickle.load(f_dat) self.a_b = cPickle.load(f_dat) self.a_c = cPickle.load(f_dat) self.a_ar = cPickle.load(f_dat) self.a_ai = cPickle.load(f_dat) self.a_br = cPickle.load(f_dat) self.a_bi = cPickle.load(f_dat) self.a_cr = cPickle.load(f_dat) self.a_ci = cPickle.load(f_dat) self.draw(nitems) def draw(self,nitems): if not hasattr(self, 'subplot1'): self.subplot1 = self.figure.add_subplot(211) self.subplot2 = self.figure.add_subplot(212) a = numpy.array(self.a_a[0:nitems]) b = numpy.array(self.a_b[0:nitems]) c = numpy.array(self.a_c[0:nitems]) d = numpy.array(self.a_time[0:nitems]) print a, b, c, d, numpy.add(a,b) bar1 = self.subplot1.bar(d,a, color='red', edgecolor='red',align='edge') bar2 = self.subplot1.bar(d,b, color='green', edgecolor='green',align='edge', bottom=a) bar3 = self.subplot1.bar(d,c, color='blue', edgecolor='blue',align='edge', bottom=numpy.add(a,b)) if __name__ == '__main__': app = HistoApp(0) app.MainLoop()