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