1 | import os, sys |
---|
2 | import pylab |
---|
3 | from copy import deepcopy |
---|
4 | import math |
---|
5 | class Reader: |
---|
6 | """ Simple data reader for Igor data files """ |
---|
7 | |
---|
8 | def __init__(self, filename=None): |
---|
9 | """ Init |
---|
10 | @param filename: Name of Igor data file to read |
---|
11 | """ |
---|
12 | self.file = filename |
---|
13 | self.x = [] |
---|
14 | self.y = [] |
---|
15 | self.image = [] |
---|
16 | |
---|
17 | # Detector info |
---|
18 | self.wavelength = 0.0 |
---|
19 | self.distance = 0.0 |
---|
20 | self.center_x = 63.5 |
---|
21 | self.center_y = 63.5 |
---|
22 | |
---|
23 | def read(self,filename=None): |
---|
24 | """ Read file """ |
---|
25 | # Check if the file is there |
---|
26 | self.file=filename |
---|
27 | if not os.path.isfile(self.file): |
---|
28 | raise ValueError, \ |
---|
29 | "Specified file %s is not a regular file" % self.file |
---|
30 | |
---|
31 | # Read file |
---|
32 | f = open(self.file,'r') |
---|
33 | buf = f.read() |
---|
34 | |
---|
35 | # Get content |
---|
36 | dataStarted = False |
---|
37 | |
---|
38 | |
---|
39 | lines = buf.split('\n') |
---|
40 | itot = 0 |
---|
41 | self.x = [] |
---|
42 | self.y = [] |
---|
43 | self.image = [] |
---|
44 | |
---|
45 | ncounts = 0 |
---|
46 | |
---|
47 | #x = pylab.arange(0, 128, 1) |
---|
48 | #y = pylab.arange(0, 128, 1) |
---|
49 | x = pylab.arange(-.5, .5, 1.0/128) |
---|
50 | y = pylab.arange(-.5, .5, 1.0/128) |
---|
51 | X, Y = pylab.meshgrid(x, y) |
---|
52 | Z = deepcopy(X) |
---|
53 | |
---|
54 | xmin = None |
---|
55 | xmax = None |
---|
56 | ymin = None |
---|
57 | ymax = None |
---|
58 | |
---|
59 | i_x = 0 |
---|
60 | i_y = -1 |
---|
61 | |
---|
62 | isInfo = False |
---|
63 | isCenter = False |
---|
64 | for line in lines: |
---|
65 | |
---|
66 | # Find setup info line |
---|
67 | if isInfo: |
---|
68 | isInfo = False |
---|
69 | line_toks = line.split() |
---|
70 | # Wavelength in Angstrom |
---|
71 | try: |
---|
72 | wavelength = float(line_toks[1]) |
---|
73 | except: |
---|
74 | raise ValueError,"IgorReader: can't read this file, missing wavelength" |
---|
75 | # Distance in meters |
---|
76 | try: |
---|
77 | distance = float(line_toks[3]) |
---|
78 | except: |
---|
79 | raise ValueError,"IgorReader: can't read this file, missing distance" |
---|
80 | |
---|
81 | if line.count("LAMBDA")>0: |
---|
82 | isInfo = True |
---|
83 | |
---|
84 | # Find center info line |
---|
85 | if isCenter: |
---|
86 | isCenter = False |
---|
87 | line_toks = line.split() |
---|
88 | # Center in bin number |
---|
89 | center_x = float(line_toks[0]) |
---|
90 | center_y = float(line_toks[1]) |
---|
91 | |
---|
92 | if line.count("BCENT")>0: |
---|
93 | isCenter = True |
---|
94 | |
---|
95 | |
---|
96 | # Find data start |
---|
97 | if line.count("***")>0: |
---|
98 | dataStarted = True |
---|
99 | |
---|
100 | # Check that we have all the info |
---|
101 | if wavelength == None \ |
---|
102 | or distance == None \ |
---|
103 | or center_x == None \ |
---|
104 | or center_y == None: |
---|
105 | raise ValueError, "IgorReader:Missing information in data file" |
---|
106 | |
---|
107 | if dataStarted == True: |
---|
108 | try: |
---|
109 | value = float(line) |
---|
110 | except: |
---|
111 | continue |
---|
112 | |
---|
113 | # Get bin number |
---|
114 | if math.fmod(itot, 128)==0: |
---|
115 | i_x = 0 |
---|
116 | i_y += 1 |
---|
117 | else: |
---|
118 | i_x += 1 |
---|
119 | |
---|
120 | Z[i_y][i_x] = value |
---|
121 | ncounts += 1 |
---|
122 | |
---|
123 | # Det 640 x 640 mm |
---|
124 | # Q = 4pi/lambda sin(theta/2) |
---|
125 | # Bin size is 0.5 cm |
---|
126 | theta = (i_x-center_x+1)*0.5 / distance / 100.0 |
---|
127 | qx = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
128 | if xmin==None or qx<xmin: |
---|
129 | xmin = qx |
---|
130 | if xmax==None or qx>xmax: |
---|
131 | xmax = qx |
---|
132 | |
---|
133 | theta = (i_y-center_y+1)*0.5 / distance / 100.0 |
---|
134 | qy = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
135 | if ymin==None or qy<ymin: |
---|
136 | ymin = qy |
---|
137 | if ymax==None or qy>ymax: |
---|
138 | ymax = qy |
---|
139 | |
---|
140 | |
---|
141 | if not qx in self.x: |
---|
142 | self.x.append(qx) |
---|
143 | if not qy in self.y: |
---|
144 | self.y.append(qy) |
---|
145 | |
---|
146 | itot += 1 |
---|
147 | |
---|
148 | |
---|
149 | theta = 0.25 / distance / 100.0 |
---|
150 | xstep = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
151 | |
---|
152 | theta = 0.25 / distance / 100.0 |
---|
153 | ystep = 4.0*math.pi/wavelength * math.sin(theta/2.0) |
---|
154 | |
---|
155 | # Store q max |
---|
156 | if xmax>ymax: |
---|
157 | self.qmax = xmax |
---|
158 | else: |
---|
159 | self.qmax = ymax |
---|
160 | |
---|
161 | #config.printEVT("Read %g points from file %s" % (ncounts, self.file)) |
---|
162 | |
---|
163 | self.wavelength = wavelength |
---|
164 | self.distance = distance*1000.0 |
---|
165 | self.center_x = center_x |
---|
166 | self.center_y = center_y |
---|
167 | print "IgorReader reading %s"%self.file |
---|
168 | return Z, xmin-xstep/2.0, xmax+xstep/2.0, ymin-ystep/2.0, ymax+ystep/2.0 |
---|
169 | |
---|