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 | metadata = {} |
---|
23 | |
---|
24 | inputfile = open(fileName, "r") |
---|
25 | |
---|
26 | inputdata = inputfile.readlines() |
---|
27 | |
---|
28 | mdtmp = inputdata[0].split(' ') |
---|
29 | |
---|
30 | print mdtmp |
---|
31 | |
---|
32 | for index in range(13, len(inputdata), 2): |
---|
33 | detdata[inputdata[index].split()[0]] = inputdata[index + 1].split(',') |
---|
34 | |
---|
35 | for key in detdata.keys(): |
---|
36 | for val in range(0, len(detdata[key])): |
---|
37 | detdata[key][val] = int(detdata[key][val]) |
---|
38 | |
---|
39 | inputfile.close() |
---|
40 | return detdata |
---|
41 | |
---|
42 | def printBT5DetData(detdata): |
---|
43 | ''' |
---|
44 | Print the contents of the file in a formatted fashion |
---|
45 | |
---|
46 | Takes a dictionary of data as provided by getBT5DataFromFile() and prints out the contents |
---|
47 | in a formatted fashion |
---|
48 | ''' |
---|
49 | motorvals = detdata.keys() |
---|
50 | motorvals.sort(cmp=numeric_compare) |
---|
51 | |
---|
52 | for motorval in motorvals: |
---|
53 | str = motorval + ":" |
---|
54 | str += "\tMon: " + repr(detdata[motorval][0]) |
---|
55 | str += "\tDet 1-5: " + repr(detdata[motorval][2]) |
---|
56 | str += "\t" + repr(detdata[motorval][1]) |
---|
57 | str += "\t" + repr(detdata[motorval][4]) |
---|
58 | str += "\t" + repr(detdata[motorval][5]) |
---|
59 | str += "\t" + repr(detdata[motorval][6]) |
---|
60 | str += "\tTrans: " + repr(detdata[motorval][3]) |
---|
61 | print str |
---|
62 | |
---|
63 | return 0 |
---|
64 | |
---|
65 | def getAlignVals(data, motorval): |
---|
66 | ''' |
---|
67 | Return the values we record in the logbook for a given motor position |
---|
68 | |
---|
69 | Takes a dictionary as provided by getBT5DataFromFile and returns a dictionary with |
---|
70 | keys Central, Trans and Sum |
---|
71 | ''' |
---|
72 | alignvals = {} |
---|
73 | |
---|
74 | alignvals['Central'] = data[motorval][1] |
---|
75 | alignvals['Trans'] = data[motorval][3] |
---|
76 | alignvals['Sum'] = data[motorval][1] + data[motorval][2] + data[motorval][4] + data[motorval][5] + data[motorval][6] |
---|
77 | return alignvals |
---|
78 | |
---|
79 | def maxDetCount(data, detector): |
---|
80 | ''' |
---|
81 | Return the maximum value and corresponding motor position for a given detector |
---|
82 | |
---|
83 | Takes a dictionary as provided by getBT5DataFromFile() and returns a dictionary with |
---|
84 | keys Position and Value |
---|
85 | ''' |
---|
86 | maxpos = '' |
---|
87 | maxval = 0 |
---|
88 | result = {} |
---|
89 | |
---|
90 | mvals = data.keys() |
---|
91 | det = {'1':2, '2':1, '3':4, '4':5, '5':6}[repr(detector)] |
---|
92 | |
---|
93 | for mval in mvals: |
---|
94 | if data[mval][det] > maxval: |
---|
95 | maxval = data[mval][det] |
---|
96 | maxpos = mval |
---|
97 | |
---|
98 | result['Position'] = maxpos |
---|
99 | result['Value'] = maxval |
---|
100 | |
---|
101 | return result |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | if __name__ == '__main__': |
---|
107 | import sys |
---|
108 | data = getBT5DataFromFile(sys.argv[1]) |
---|
109 | printBT5DetData(data) |
---|
110 | |
---|
111 | maxinfo = maxDetCount(data, 2) |
---|
112 | print maxinfo |
---|
113 | avals = getAlignVals(data, maxinfo['Position']) |
---|
114 | print avals |
---|