Merged my unitary FFT wrappers (FFT_tools) as hooke.util.fft.
[hooke.git] / hooke / driver / mcs.py
1 # Copyright (C) 2009-2010 Allen Chen
2 #                         Massimo Sandal <devicerandom@gmail.com>
3 #                         W. Trevor King <wking@drexel.edu>
4 #
5 # This file is part of Hooke.
6 #
7 # Hooke is free software: you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation, either
10 # version 3 of the License, or (at your option) any later version.
11 #
12 # Hooke is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with Hooke.  If not, see
19 # <http://www.gnu.org/licenses/>.
20
21 """Driver for mcs fluorescence files
22 """
23
24 from .. import curve as lhc
25 from .. import libhooke as lh
26 import struct
27
28 class mcsDriver(lhc.Driver):
29
30     def __init__(self, filename):
31         '''
32         Open the RED (A) ones; the BLUE (D) mirror ones will be automatically opened
33         '''
34         #obtain name of blue files
35         othername=filename
36         if othername[-8]=='a': #fixme: how to make it general? (maybe should not be in driverspace but in environment...)
37             oth=list(othername)
38             oth[-8]='d'
39             othername=''.join(oth)
40         self.filename=filename
41         self.othername=othername
42
43         #print self.filename, self.othername
44
45         self.filedata=open(filename,'rb')
46         self.reddata=self.filedata.read()
47         self.filedata.close()
48
49         self.filebluedata=open(othername,'rb') #open also the blue ones
50         self.bluedata=self.filebluedata.read()
51         self.filebluedata.close()
52
53         self.filetype = 'mcs'
54         self.experiment = 'smfluo'
55
56     def is_me(self):
57         if self.filename[-3:].lower()=='mcs':
58             return True
59         else:
60             return False
61
62     def close_all(self):
63         self.filedata.close()
64         self.filebluedata.close()
65
66
67     def default_plots(self):
68         red_data=self.read_file(self.reddata)
69         blue_data=self.read_file(self.bluedata)
70         blue_data=[-1*float(item) for item in blue_data] #visualize blue as "mirror" of red
71
72         main_plot=lhc.PlotObject()
73         main_plot.add_set(range(len(red_data)),red_data)
74         main_plot.add_set(range(len(blue_data)),blue_data)
75         main_plot.normalize_vectors()
76         main_plot.units=['time','count']  #FIXME: if there's an header saying something about the time count, should be used
77         main_plot.destination=0
78         main_plot.title=self.filename
79         main_plot.colors=['red','blue']
80
81         return [main_plot]
82
83     def read_file(self, raw_data):
84         real_data=[]
85         intervalsperfile=struct.unpack('h', raw_data[10:12])[0] #read in number of intervals in this file
86                                                                 #this data is contained in bit offset 10-12 in mcs file
87         #see http://docs.python.org/library/struct.html#module-struct for additional explanation
88
89         numbytes=len(raw_data) #data is stored in 4-byte chunks, starting with pos 256
90         for j in range(0,intervalsperfile): #read in all intervals in file
91             temp=raw_data[256+j*4:256+j*4+4]    #data starts at byte offset 256
92             real_data.append(struct.unpack('i', temp)[0]) #[0] because it returns a 1-element tuple
93         return real_data