source: sans/utils/bt5/bt5utils/bt5reader.py @ 655

Last change on this file since 655 was 450, checked in by ajj, 14 years ago

adding combine util to svn

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