1 | import usans |
---|
2 | |
---|
3 | class BT5DataSet: |
---|
4 | |
---|
5 | def __init__(self, fn=None): |
---|
6 | |
---|
7 | self.fileName = fn |
---|
8 | self.plot = None |
---|
9 | self.detdata = {} |
---|
10 | self.metadata = {} |
---|
11 | self.alignvals = {} |
---|
12 | self.alignvalstring = '' |
---|
13 | |
---|
14 | if (self.fileName != None): |
---|
15 | self.getBT5DataFromFile(self.fileName) |
---|
16 | |
---|
17 | |
---|
18 | def getBT5DataFromFile(self,fileName): |
---|
19 | ''' |
---|
20 | Takes a filename and returns a dictionary of the detector values |
---|
21 | keyed by varying value (ususally A2 or A5) |
---|
22 | ''' |
---|
23 | |
---|
24 | if usans.isBT5Data(fileName): |
---|
25 | |
---|
26 | motlist = [] |
---|
27 | |
---|
28 | #print "File: ",fileName |
---|
29 | inputfile = open(fileName, "r") |
---|
30 | |
---|
31 | inputdata = inputfile.readlines() |
---|
32 | |
---|
33 | mdtmp = inputdata[0].replace("'", "") |
---|
34 | mdtmp = mdtmp.split() |
---|
35 | |
---|
36 | #Sundry metadata about run settings |
---|
37 | (self.metadata['filename'], self.metadata['datetime'], |
---|
38 | self.metadata['mon'], self.metadata['prefactor'], |
---|
39 | self.metadata['base'], self.metadata['numpnts'], |
---|
40 | self.metadata['type']) = (mdtmp[0], ' '.join(mdtmp[1:5]), float(mdtmp[6]), int(mdtmp[7]), mdtmp[8], int(mdtmp[9]), mdtmp[10]) |
---|
41 | |
---|
42 | #Comment string |
---|
43 | self.metadata['title'] = inputdata[2].strip() |
---|
44 | |
---|
45 | #Start, step and end values for motors 1-6 |
---|
46 | motlist.append(inputdata[5].split()[1:]) |
---|
47 | motlist.append(inputdata[6].split()[1:]) |
---|
48 | motlist.append(inputdata[7].split()[1:]) |
---|
49 | motlist.append(inputdata[8].split()[1:]) |
---|
50 | motlist.append(inputdata[9].split()[1:]) |
---|
51 | motlist.append(inputdata[10].split()[1:]) |
---|
52 | self.metadata['motorvals'] = motlist |
---|
53 | |
---|
54 | for index in range(13, len(inputdata), 2): |
---|
55 | self.detdata[float(inputdata[index].split()[0])] = inputdata[index + 1].split(',') |
---|
56 | |
---|
57 | for key in self.detdata.keys(): |
---|
58 | for val in range(0, len(self.detdata[key])): |
---|
59 | self.detdata[key][val] = int(self.detdata[key][val]) |
---|
60 | |
---|
61 | inputfile.close() |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | def printDetectorData(self): |
---|
67 | ''' |
---|
68 | Print the contents of the file in a formatted fashion |
---|
69 | |
---|
70 | Takes a dictionary of data as provided by getBT5DataFromFile() and prints out the contents |
---|
71 | in a formatted fashion |
---|
72 | ''' |
---|
73 | motorvals = self.detdata.keys() |
---|
74 | motorvals.sort(cmp=numeric_compare) |
---|
75 | |
---|
76 | for motorval in motorvals: |
---|
77 | str = repr(motorval) + ":" |
---|
78 | str += "\tMon: " + repr(detdata[motorval][0]) |
---|
79 | str += "\tDet 1-5: " + repr(detdata[motorval][2]) |
---|
80 | str += "\t" + repr(detdata[motorval][1]) |
---|
81 | str += "\t" + repr(detdata[motorval][4]) |
---|
82 | str += "\t" + repr(detdata[motorval][5]) |
---|
83 | str += "\t" + repr(detdata[motorval][6]) |
---|
84 | str += "\tTrans: " + repr(detdata[motorval][3]) |
---|
85 | print str |
---|
86 | |
---|
87 | return 0 |
---|
88 | |
---|
89 | def calcAlignVals(self,mv): |
---|
90 | ''' |
---|
91 | Return the values we record in the logbook for a given motor position |
---|
92 | |
---|
93 | Takes a dictionary as provided by getBT5DataFromFile and returns a dictionary with |
---|
94 | keys Central, Trans and Sum |
---|
95 | ''' |
---|
96 | motorval = float(mv) |
---|
97 | |
---|
98 | self.alignvals['Central'] = self.detdata[motorval][1] |
---|
99 | self.alignvals['Trans'] = self.detdata[motorval][3] |
---|
100 | self.alignvals['Sum'] = self.detdata[motorval][1] + self.detdata[motorval][2] + self.detdata[motorval][4] + self.detdata[motorval][5] + self.detdata[motorval][6] |
---|
101 | self.alignvals['Monitor'] = self.detdata[motorval][0] |
---|
102 | self.alignvals['Sum/Monitor'] = float(self.alignvals['Sum'])/float(self.alignvals['Monitor']) |
---|
103 | |
---|
104 | self.alignvalstring = "#4: "+repr(self.alignvals['Central']) |
---|
105 | self.alignvalstring += " Trans: "+repr(self.alignvals['Trans']) |
---|
106 | self.alignvalstring += " Sum: "+repr(self.alignvals['Sum']) |
---|
107 | self.alignvalstring += " MCR: "+repr(self.alignvals['Monitor']) |
---|
108 | self.alignvalstring += " Sum/MCR: %5.3f" % self.alignvals['Sum/Monitor'] |
---|
109 | |
---|
110 | def maxDetCount(self, detector): |
---|
111 | ''' |
---|
112 | Return the maximum value and corresponding motor position for a given detector |
---|
113 | |
---|
114 | Takes a dictionary as provided by getBT5DataFromFile() and returns a dictionary with |
---|
115 | keys Position and Value |
---|
116 | ''' |
---|
117 | maxpos = '' |
---|
118 | maxval = 0 |
---|
119 | result = {} |
---|
120 | |
---|
121 | mvals = self.detdata.keys() |
---|
122 | det = {'1':2, '2':1, '3':4, '4':5, '5':6}[repr(detector)] |
---|
123 | |
---|
124 | for mval in mvals: |
---|
125 | if self.detdata[mval][det] > maxval: |
---|
126 | maxval = data[mval][det] |
---|
127 | maxpos = mval |
---|
128 | |
---|
129 | result['Position'] = maxpos |
---|
130 | result['Value'] = maxval |
---|
131 | |
---|
132 | return result |
---|
133 | |
---|
134 | def plot_dataset(self,axes,plottype=None): |
---|
135 | ''' |
---|
136 | Takes a matplotlib axes object and plots bt5 dataset on it. |
---|
137 | ''' |
---|
138 | data = self.detdata |
---|
139 | metadata = self.metadata |
---|
140 | |
---|
141 | if type is None: |
---|
142 | plottype = 'rate' |
---|
143 | |
---|
144 | if plottype == 'total': |
---|
145 | #generate totals |
---|
146 | xdata = [] |
---|
147 | ydata = [] |
---|
148 | |
---|
149 | mvals = data.keys() |
---|
150 | mvals.sort(usans.numeric_compare) |
---|
151 | for mval in mvals: |
---|
152 | xdata.append(mval) |
---|
153 | ydata.append(data[mval][1] + data[mval][2] + data[mval][4] + data[mval][5] + data[mval][6]) |
---|
154 | |
---|
155 | self.plot = axes.plot(xdata,ydata, 'bo', picker=5) |
---|
156 | |
---|
157 | elif plottype == 'rate': |
---|
158 | # generate countrate |
---|
159 | xdata = [] |
---|
160 | ydata = [] |
---|
161 | |
---|
162 | mvals = data.keys() |
---|
163 | mvals.sort(usans.numeric_compare) |
---|
164 | for mval in mvals: |
---|
165 | xdata.append(mval) |
---|
166 | |
---|
167 | if metadata['base'] == 'TIME': |
---|
168 | #Counting in TIME base, so normalize by seconds |
---|
169 | cnttime = metadata['mon'] |
---|
170 | for mval in mvals: |
---|
171 | ydata.append((data[mval][1] + data[mval][2] + data[mval][4] + data[mval][5] + data[mval][6]) / cnttime) |
---|
172 | else: |
---|
173 | #Must be counting in monitor base so normalize by monitor |
---|
174 | moncts = metadata['mon'] |
---|
175 | for mval in mvals: |
---|
176 | ydata.append((data[mval][1] + data[mval][2] + data[mval][4] + data[mval][5] + data[mval][6]) / cnttime) |
---|
177 | |
---|
178 | self.plot = axes.plot(xdata,ydata, 'bo', picker=5) |
---|
179 | |
---|
180 | |
---|
181 | elif plottype == 'trans': |
---|
182 | xdata = [] |
---|
183 | ydata = [] |
---|
184 | |
---|
185 | mvals = data.keys() |
---|
186 | mvals.sort(usans.numeric_compare) |
---|
187 | for mval in mvals: |
---|
188 | xdata.append(mval) |
---|
189 | ydata.append(data[mval][3]) |
---|
190 | |
---|
191 | self.plot = axes.plot(xdata,ydata, 'bo', picker=5) |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | elif plottype == 'mon': |
---|
196 | xdata = [] |
---|
197 | ydata = [] |
---|
198 | |
---|
199 | mvals = data.keys() |
---|
200 | mvals.sort(usans.numeric_compare) |
---|
201 | for mval in mvals: |
---|
202 | xdata.append(mval) |
---|
203 | ydata.append(data[mval][0]) |
---|
204 | |
---|
205 | self.plot = axes.plot(xdata,ydata, 'bo', picker=5) |
---|
206 | |
---|
207 | |
---|
208 | elif plottype == 'split': |
---|
209 | xdata = [] |
---|
210 | ydata1 = [] |
---|
211 | ydata2 = [] |
---|
212 | ydata3 = [] |
---|
213 | ydata4 = [] |
---|
214 | ydata5 = [] |
---|
215 | |
---|
216 | mvals = data.keys() |
---|
217 | mvals.sort(usans.numeric_compare) |
---|
218 | for mval in mvals: |
---|
219 | xdata.append(mval) |
---|
220 | ydata1.append(data[mval][1]) |
---|
221 | ydata2.append(data[mval][2]) |
---|
222 | ydata3.append(data[mval][4]) |
---|
223 | ydata4.append(data[mval][5]) |
---|
224 | ydata5.append(data[mval][6]) |
---|
225 | |
---|
226 | self.plot = axes.plot(xdata,ydata1, 'o', |
---|
227 | xdata,ydata2, 'o', |
---|
228 | xdata,ydata3, 'o', |
---|
229 | xdata,ydata4, 'o', |
---|
230 | xdata,ydata5, 'o') |
---|
231 | |
---|
232 | def remove_plot(self): |
---|
233 | |
---|
234 | for line in self.plot: |
---|
235 | axes = line.get_axes() |
---|
236 | axes.lines.remove(line) |
---|