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 | motlist = [] |
---|
24 | |
---|
25 | #print "File: ",fileName |
---|
26 | inputfile = open(fileName, "r") |
---|
27 | |
---|
28 | inputdata = inputfile.readlines() |
---|
29 | |
---|
30 | if len(inputdata) < 2: |
---|
31 | inputfile.close() |
---|
32 | return 0,0 |
---|
33 | |
---|
34 | if inputdata[1].find('Filename') > 0: |
---|
35 | |
---|
36 | mdtmp = inputdata[0].replace("'","") |
---|
37 | mdtmp = mdtmp.split() |
---|
38 | |
---|
39 | #Sundry metadata about run settings |
---|
40 | (metadata['filename'], metadata['datetime'], |
---|
41 | metadata['mon'],metadata['prefactor'], |
---|
42 | metadata['base'],metadata['numpnts'], |
---|
43 | metadata['type']) = (mdtmp[0],' '.join(mdtmp[1:5]),float(mdtmp[6]),int(mdtmp[7]),mdtmp[8],int(mdtmp[9]),mdtmp[10]) |
---|
44 | |
---|
45 | #Comment string |
---|
46 | metadata['title'] = inputdata[2].strip() |
---|
47 | |
---|
48 | #Start, step and end values for motors 1-6 |
---|
49 | motlist.append(inputdata[5].split()[1:]) |
---|
50 | motlist.append(inputdata[6].split()[1:]) |
---|
51 | motlist.append(inputdata[7].split()[1:]) |
---|
52 | motlist.append(inputdata[8].split()[1:]) |
---|
53 | motlist.append(inputdata[9].split()[1:]) |
---|
54 | motlist.append(inputdata[10].split()[1:]) |
---|
55 | metadata['motorvals'] = motlist |
---|
56 | |
---|
57 | for index in range(13, len(inputdata), 2): |
---|
58 | detdata[float(inputdata[index].split()[0])] = inputdata[index + 1].split(',') |
---|
59 | |
---|
60 | for key in detdata.keys(): |
---|
61 | for val in range(0, len(detdata[key])): |
---|
62 | detdata[key][val] = int(detdata[key][val]) |
---|
63 | |
---|
64 | inputfile.close() |
---|
65 | return detdata,metadata |
---|
66 | |
---|
67 | else: |
---|
68 | inputfile.close() |
---|
69 | return 0,0 |
---|
70 | |
---|
71 | def printBT5DetData(detdata): |
---|
72 | ''' |
---|
73 | Print the contents of the file in a formatted fashion |
---|
74 | |
---|
75 | Takes a dictionary of data as provided by getBT5DataFromFile() and prints out the contents |
---|
76 | in a formatted fashion |
---|
77 | ''' |
---|
78 | motorvals = detdata.keys() |
---|
79 | motorvals.sort(cmp=numeric_compare) |
---|
80 | |
---|
81 | for motorval in motorvals: |
---|
82 | str = repr(motorval) + ":" |
---|
83 | str += "\tMon: " + repr(detdata[motorval][0]) |
---|
84 | str += "\tDet 1-5: " + repr(detdata[motorval][2]) |
---|
85 | str += "\t" + repr(detdata[motorval][1]) |
---|
86 | str += "\t" + repr(detdata[motorval][4]) |
---|
87 | str += "\t" + repr(detdata[motorval][5]) |
---|
88 | str += "\t" + repr(detdata[motorval][6]) |
---|
89 | str += "\tTrans: " + repr(detdata[motorval][3]) |
---|
90 | print str |
---|
91 | |
---|
92 | return 0 |
---|
93 | |
---|
94 | def getAlignVals(data, motorval): |
---|
95 | ''' |
---|
96 | Return the values we record in the logbook for a given motor position |
---|
97 | |
---|
98 | Takes a dictionary as provided by getBT5DataFromFile and returns a dictionary with |
---|
99 | keys Central, Trans and Sum |
---|
100 | ''' |
---|
101 | alignvals = {} |
---|
102 | |
---|
103 | alignvals['Central'] = data[motorval][1] |
---|
104 | alignvals['Trans'] = data[motorval][3] |
---|
105 | alignvals['Sum'] = data[motorval][1] + data[motorval][2] + data[motorval][4] + data[motorval][5] + data[motorval][6] |
---|
106 | return alignvals |
---|
107 | |
---|
108 | def maxDetCount(data, detector): |
---|
109 | ''' |
---|
110 | Return the maximum value and corresponding motor position for a given detector |
---|
111 | |
---|
112 | Takes a dictionary as provided by getBT5DataFromFile() and returns a dictionary with |
---|
113 | keys Position and Value |
---|
114 | ''' |
---|
115 | maxpos = '' |
---|
116 | maxval = 0 |
---|
117 | result = {} |
---|
118 | |
---|
119 | mvals = data.keys() |
---|
120 | det = {'1':2, '2':1, '3':4, '4':5, '5':6}[repr(detector)] |
---|
121 | |
---|
122 | for mval in mvals: |
---|
123 | if data[mval][det] > maxval: |
---|
124 | maxval = data[mval][det] |
---|
125 | maxpos = mval |
---|
126 | |
---|
127 | result['Position'] = maxpos |
---|
128 | result['Value'] = maxval |
---|
129 | |
---|
130 | return result |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | if __name__ == '__main__': |
---|
136 | import sys |
---|
137 | data,metadata = getBT5DataFromFile(sys.argv[1]) |
---|
138 | printBT5DetData(data) |
---|
139 | |
---|
140 | maxinfo = maxDetCount(data, 2) |
---|
141 | print maxinfo |
---|
142 | avals = getAlignVals(data, maxinfo['Position']) |
---|
143 | print avals |
---|