Updated copyright blurbs in all files to '# Copyright'
[hooke.git] / hooke / driver / mcs.py
1 # Copyright
2
3 """Driver for mcs fluorescence files
4 """
5
6 from .. import curve as lhc
7 from .. import libhooke as lh
8 import struct
9
10 class mcsDriver(lhc.Driver):
11
12     def __init__(self, filename):
13         '''
14         Open the RED (A) ones; the BLUE (D) mirror ones will be automatically opened
15         '''
16         #obtain name of blue files
17         othername=filename
18         if othername[-8]=='a': #fixme: how to make it general? (maybe should not be in driverspace but in environment...)
19             oth=list(othername)
20             oth[-8]='d'
21             othername=''.join(oth)
22         self.filename=filename
23         self.othername=othername
24
25         #print self.filename, self.othername
26
27         self.filedata=open(filename,'rb')
28         self.reddata=self.filedata.read()
29         self.filedata.close()
30
31         self.filebluedata=open(othername,'rb') #open also the blue ones
32         self.bluedata=self.filebluedata.read()
33         self.filebluedata.close()
34
35         self.filetype = 'mcs'
36         self.experiment = 'smfluo'
37
38     def is_me(self):
39         if self.filename[-3:].lower()=='mcs':
40             return True
41         else:
42             return False
43
44     def close_all(self):
45         self.filedata.close()
46         self.filebluedata.close()
47
48
49     def default_plots(self):
50         red_data=self.read_file(self.reddata)
51         blue_data=self.read_file(self.bluedata)
52         blue_data=[-1*float(item) for item in blue_data] #visualize blue as "mirror" of red
53
54         main_plot=lhc.PlotObject()
55         main_plot.add_set(range(len(red_data)),red_data)
56         main_plot.add_set(range(len(blue_data)),blue_data)
57         main_plot.normalize_vectors()
58         main_plot.units=['time','count']  #FIXME: if there's an header saying something about the time count, should be used
59         main_plot.destination=0
60         main_plot.title=self.filename
61         main_plot.colors=['red','blue']
62
63         return [main_plot]
64
65     def read_file(self, raw_data):
66         real_data=[]
67         intervalsperfile=struct.unpack('h', raw_data[10:12])[0] #read in number of intervals in this file
68                                                                 #this data is contained in bit offset 10-12 in mcs file
69         #see http://docs.python.org/library/struct.html#module-struct for additional explanation
70
71         numbytes=len(raw_data) #data is stored in 4-byte chunks, starting with pos 256
72         for j in range(0,intervalsperfile): #read in all intervals in file
73             temp=raw_data[256+j*4:256+j*4+4]    #data starts at byte offset 256
74             real_data.append(struct.unpack('i', temp)[0]) #[0] because it returns a 1-element tuple
75         return real_data