source: sans/utils/bt5/bt5plot2/usans.py @ 443

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

Handle plot removal, deal with rescaling on plot removal.

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