1 | #!/usr/bin/python |
---|
2 | |
---|
3 | #import Tkinter as Tk |
---|
4 | |
---|
5 | def numeric_compare(x,y): |
---|
6 | x = float(x) |
---|
7 | y = float(y) |
---|
8 | |
---|
9 | if x < y: |
---|
10 | return -1 |
---|
11 | elif x==y: |
---|
12 | return 0 |
---|
13 | else: # x>y |
---|
14 | return 1 |
---|
15 | |
---|
16 | def getBT5DataFromFile(fileName): |
---|
17 | ''' |
---|
18 | Takes a filename and returns a dictionary of the detector values |
---|
19 | keyed by varying value (ususally A2 or A5) |
---|
20 | ''' |
---|
21 | detdata = {} |
---|
22 | summary = {} |
---|
23 | |
---|
24 | inputfile = open(fileName, "r") |
---|
25 | |
---|
26 | inputdata = inputfile.readlines() |
---|
27 | |
---|
28 | header = inputdata[0:13] |
---|
29 | |
---|
30 | for index in range(13,len(inputdata),2): |
---|
31 | summary[inputdata[index].split()[0]] = inputdata[index].split() |
---|
32 | detdata[inputdata[index].split()[0]] = inputdata[index+1].split(',') |
---|
33 | |
---|
34 | for key in detdata.keys(): |
---|
35 | for val in range(0,len(detdata[key])): |
---|
36 | detdata[key][val] = int(detdata[key][val]) |
---|
37 | |
---|
38 | inputfile.close() |
---|
39 | return (header,summary,detdata) |
---|
40 | |
---|
41 | def printBT5FormatData(header,summarydata,scalardata): |
---|
42 | ''' |
---|
43 | Print out header and data in .bt5 format |
---|
44 | ''' |
---|
45 | for line in header: |
---|
46 | print line, |
---|
47 | |
---|
48 | summarykeys = summarydata.keys() |
---|
49 | summarykeys.sort(numeric_compare) |
---|
50 | |
---|
51 | for key in summarykeys: |
---|
52 | print '\t%s\t%s\t%s\t%s\t%s' % tuple(summarydata[key]) |
---|
53 | print '%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d' % tuple(scalardata[key]) |
---|
54 | |
---|
55 | return (0) |
---|
56 | |
---|
57 | def addBT5Data(filenamelist): |
---|
58 | ''' |
---|
59 | Takes a list of filenames, loads the data and sums it. |
---|
60 | ''' |
---|
61 | headers = {} |
---|
62 | scalars = {} |
---|
63 | summarys = {} |
---|
64 | |
---|
65 | for i in range (0,len(filenamelist)): |
---|
66 | (headers[i],summarys[i],scalars[i]) = getBT5DataFromFile(filenamelist[i]) |
---|
67 | |
---|
68 | #for key in headers: |
---|
69 | # printBT5FormatData(headers[key],summarys[key],scalars[key]) |
---|
70 | |
---|
71 | outheader = headers[0] |
---|
72 | outscalars = scalars[0] |
---|
73 | outsummary = summarys[0] |
---|
74 | |
---|
75 | for key in summarys[0]: |
---|
76 | for i in range(1,len(filenamelist)): |
---|
77 | #Fix the summary data |
---|
78 | outsummary[key][1] = float(outsummary[key][1])+float(summarys[i][key][1]) |
---|
79 | outsummary[key][2] = int(outsummary[key][2])+int(summarys[i][key][2]) |
---|
80 | outsummary[key][3] = int(outsummary[key][3])+int(summarys[i][key][3]) |
---|
81 | outsummary[key][4] = int(outsummary[key][4])+int(summarys[i][key][4]) |
---|
82 | #Fix the scalar values |
---|
83 | for j in range(0,16): |
---|
84 | outscalars[key][j] = outscalars[key][j]+scalars[i][key][j] |
---|
85 | |
---|
86 | |
---|
87 | return (outheader,outsummary,outscalars) |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | def printBT5DetData(detdata): |
---|
92 | ''' |
---|
93 | Print the contents of the file in a formatted fashion |
---|
94 | |
---|
95 | Takes a dictionary of data as provided by getBT5DataFromFile() and prints out the contents |
---|
96 | in a formatted fashion |
---|
97 | ''' |
---|
98 | motorvals = detdata.keys() |
---|
99 | motorvals.sort(cmp=numeric_compare) |
---|
100 | |
---|
101 | for motorval in motorvals: |
---|
102 | str = motorval+":" |
---|
103 | str += "\tMon: "+repr(detdata[motorval][0]) |
---|
104 | str += "\tDet 1-5: "+repr(detdata[motorval][2]) |
---|
105 | str += "\t"+repr(detdata[motorval][1]) |
---|
106 | str += "\t"+repr(detdata[motorval][4]) |
---|
107 | str += "\t"+repr(detdata[motorval][5]) |
---|
108 | str += "\t"+repr(detdata[motorval][6]) |
---|
109 | str += "\tTrans: "+repr(detdata[motorval][3]) |
---|
110 | print str |
---|
111 | |
---|
112 | return 0 |
---|
113 | |
---|
114 | def getAlignVals(data,motorval): |
---|
115 | ''' |
---|
116 | Return the values we record in the logbook for a given motor position |
---|
117 | |
---|
118 | Takes a dictionary as provided by getBT5DataFromFile and returns a dictionary with |
---|
119 | keys Central, Trans and Sum |
---|
120 | ''' |
---|
121 | alignvals = {} |
---|
122 | |
---|
123 | alignvals['Central'] = data[motorval][1] |
---|
124 | alignvals['Trans'] = data[motorval][3] |
---|
125 | alignvals['Sum'] = data[motorval][1]+data[motorval][2]+data[motorval][4]+data[motorval][5]+data[motorval][6] |
---|
126 | return alignvals |
---|
127 | |
---|
128 | def maxDetCount(data,detector): |
---|
129 | ''' |
---|
130 | Return the maximum value and corresponding motor position for a given detector |
---|
131 | |
---|
132 | Takes a dictionary as provided by getBT5DataFromFile() and returns a dictionary with |
---|
133 | keys Position and Value |
---|
134 | ''' |
---|
135 | maxpos = '' |
---|
136 | maxval = 0 |
---|
137 | result = {} |
---|
138 | |
---|
139 | mvals = data.keys() |
---|
140 | det = {'1':2, '2':1, '3':4, '4':5, '5':6}[repr(detector)] |
---|
141 | |
---|
142 | for mval in mvals: |
---|
143 | if data[mval][det] > maxval: |
---|
144 | maxval = data[mval][det] |
---|
145 | maxpos = mval |
---|
146 | |
---|
147 | result['Position'] = maxpos |
---|
148 | result['Value'] = maxval |
---|
149 | |
---|
150 | return result |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | |
---|
155 | if __name__ == '__main__': |
---|
156 | import sys |
---|
157 | (header,summary,data) = getBT5DataFromFile(sys.argv[1]) |
---|
158 | for line in header: |
---|
159 | print line, |
---|
160 | |
---|
161 | printBT5FormatData(header,summary,data) |
---|
162 | |
---|
163 | maxinfo = maxDetCount(data,2) |
---|
164 | print maxinfo |
---|
165 | avals = getAlignVals(data,maxinfo['Position']) |
---|
166 | print avals |
---|