1 | """ |
---|
2 | IGOR 2D reduced file reader |
---|
3 | """ |
---|
4 | ############################################################################ |
---|
5 | #This software was developed by the University of Tennessee as part of the |
---|
6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
7 | #project funded by the US National Science Foundation. |
---|
8 | #If you use DANSE applications to do scientific research that leads to |
---|
9 | #publication, we ask that you acknowledge the use of the software with the |
---|
10 | #following sentence: |
---|
11 | #This work benefited from DANSE software developed under NSF award DMR-0520547. |
---|
12 | #copyright 2008, University of Tennessee |
---|
13 | ############################################################################# |
---|
14 | import os |
---|
15 | import numpy |
---|
16 | import math |
---|
17 | #import logging |
---|
18 | from sas.sascalc.dataloader.data_info import Data2D |
---|
19 | from sas.sascalc.dataloader.data_info import Detector |
---|
20 | from sas.sascalc.dataloader.manipulations import reader2D_converter |
---|
21 | |
---|
22 | # Look for unit converter |
---|
23 | has_converter = True |
---|
24 | try: |
---|
25 | from sas.sascalc.data_util.nxsunit import Converter |
---|
26 | except: |
---|
27 | has_converter = False |
---|
28 | |
---|
29 | |
---|
30 | class Reader: |
---|
31 | """ Simple data reader for Igor data files """ |
---|
32 | ## File type |
---|
33 | type_name = "IGOR 2D" |
---|
34 | ## Wildcards |
---|
35 | type = ["IGOR 2D files (*.ASC)|*.ASC"] |
---|
36 | ## Extension |
---|
37 | ext=['.ASC', '.asc'] |
---|
38 | |
---|
39 | def read(self, filename=None): |
---|
40 | """ Read file """ |
---|
41 | if not os.path.isfile(filename): |
---|
42 | raise ValueError, \ |
---|
43 | "Specified file %s is not a regular file" % filename |
---|
44 | |
---|
45 | # Read file |
---|
46 | f = open(filename, 'r') |
---|
47 | buf = f.read() |
---|
48 | |
---|
49 | # Instantiate data object |
---|
50 | output = Data2D() |
---|
51 | output.filename = os.path.basename(filename) |
---|
52 | detector = Detector() |
---|
53 | if len(output.detector) > 0: |
---|
54 | print str(output.detector[0]) |
---|
55 | output.detector.append(detector) |
---|
56 | |
---|
57 | # Get content |
---|
58 | dataStarted = False |
---|
59 | |
---|
60 | lines = buf.split('\n') |
---|
61 | itot = 0 |
---|
62 | x = [] |
---|
63 | y = [] |
---|
64 | |
---|
65 | ncounts = 0 |
---|
66 | |
---|
67 | xmin = None |
---|
68 | xmax = None |
---|
69 | ymin = None |
---|
70 | ymax = None |
---|
71 | |
---|
72 | i_x = 0 |
---|
73 | i_y = -1 |
---|
74 | i_tot_row = 0 |
---|
75 | |
---|
76 | isInfo = False |
---|
77 | isCenter = False |
---|
78 | |
---|
79 | data_conv_q = None |
---|
80 | data_conv_i = None |
---|
81 | |
---|
82 | if has_converter == True and output.Q_unit != '1/A': |
---|
83 | data_conv_q = Converter('1/A') |
---|
84 | # Test it |
---|
85 | data_conv_q(1.0, output.Q_unit) |
---|
86 | |
---|
87 | if has_converter == True and output.I_unit != '1/cm': |
---|
88 | data_conv_i = Converter('1/cm') |
---|
89 | # Test it |
---|
90 | data_conv_i(1.0, output.I_unit) |
---|
91 | |
---|
92 | for line in lines: |
---|
93 | |
---|
94 | # Find setup info line |
---|
95 | if isInfo: |
---|
96 | isInfo = False |
---|
97 | line_toks = line.split() |
---|
98 | # Wavelength in Angstrom |
---|
99 | try: |
---|
100 | wavelength = float(line_toks[1]) |
---|
101 | except: |
---|
102 | msg = "IgorReader: can't read this file, missing wavelength" |
---|
103 | raise ValueError, msg |
---|
104 | |
---|
105 | #Find # of bins in a row assuming the detector is square. |
---|
106 | if dataStarted == True: |
---|
107 | try: |
---|
108 | value = float(line) |
---|
109 | except: |
---|
110 | # Found a non-float entry, skip it |
---|
111 | continue |
---|
112 | |
---|
113 | # Get total bin number |
---|
114 | |
---|
115 | i_tot_row += 1 |
---|
116 | i_tot_row = math.ceil(math.sqrt(i_tot_row)) - 1 |
---|
117 | #print "i_tot", i_tot_row |
---|
118 | size_x = i_tot_row # 192#128 |
---|
119 | size_y = i_tot_row # 192#128 |
---|
120 | output.data = numpy.zeros([size_x, size_y]) |
---|
121 | output.err_data = numpy.zeros([size_x, size_y]) |
---|
122 | |
---|
123 | #Read Header and 2D data |
---|
124 | for line in lines: |
---|
125 | # Find setup info line |
---|
126 | if isInfo: |
---|
127 | isInfo = False |
---|
128 | line_toks = line.split() |
---|
129 | # Wavelength in Angstrom |
---|
130 | try: |
---|
131 | wavelength = float(line_toks[1]) |
---|
132 | except: |
---|
133 | msg = "IgorReader: can't read this file, missing wavelength" |
---|
134 | raise ValueError, msg |
---|
135 | # Distance in meters |
---|
136 | try: |
---|
137 | distance = float(line_toks[3]) |
---|
138 | except: |
---|
139 | msg = "IgorReader: can't read this file, missing distance" |
---|
140 | raise ValueError, msg |
---|
141 | |
---|
142 | # Distance in meters |
---|
143 | try: |
---|
144 | transmission = float(line_toks[4]) |
---|
145 | except: |
---|
146 | msg = "IgorReader: can't read this file, " |
---|
147 | msg += "missing transmission" |
---|
148 | raise ValueError, msg |
---|
149 | |
---|
150 | if line.count("LAMBDA") > 0: |
---|
151 | isInfo = True |
---|
152 | |
---|
153 | # Find center info line |
---|
154 | if isCenter: |
---|
155 | isCenter = False |
---|
156 | line_toks = line.split() |
---|
157 | |
---|
158 | # Center in bin number: Must substrate 1 because |
---|
159 | #the index starts from 1 |
---|
160 | center_x = float(line_toks[0]) - 1 |
---|
161 | center_y = float(line_toks[1]) - 1 |
---|
162 | |
---|
163 | if line.count("BCENT") > 0: |
---|
164 | isCenter = True |
---|
165 | |
---|
166 | # Find data start |
---|
167 | if line.count("***")>0: |
---|
168 | dataStarted = True |
---|
169 | |
---|
170 | # Check that we have all the info |
---|
171 | if wavelength == None \ |
---|
172 | or distance == None \ |
---|
173 | or center_x == None \ |
---|
174 | or center_y == None: |
---|
175 | msg = "IgorReader:Missing information in data file" |
---|
176 | raise ValueError, msg |
---|
177 | |
---|
178 | if dataStarted == True: |
---|
179 | try: |
---|
180 | value = float(line) |
---|
181 | except: |
---|
182 | # Found a non-float entry, skip it |
---|
183 | continue |
---|
184 | |
---|
185 | # Get bin number |
---|
186 | if math.fmod(itot, i_tot_row) == 0: |
---|
187 | i_x = 0 |
---|
188 | i_y += 1 |
---|
189 | else: |
---|
190 | i_x += 1 |
---|
191 | |
---|
192 | output.data[i_y][i_x] = value |
---|
193 | ncounts += 1 |
---|
194 | |
---|
195 | # Det 640 x 640 mm |
---|
196 | # Q = 4pi/lambda sin(theta/2) |
---|
197 | # Bin size is 0.5 cm |
---|
198 | #REmoved +1 from theta = (i_x-center_x+1)*0.5 / distance |
---|
199 | # / 100.0 and |
---|
200 | #REmoved +1 from theta = (i_y-center_y+1)*0.5 / |
---|
201 | # distance / 100.0 |
---|
202 | #ToDo: Need complete check if the following |
---|
203 | # covert process is consistent with fitting.py. |
---|
204 | theta = (i_x - center_x) * 0.5 / distance / 100.0 |
---|
205 | qx = 4.0 * math.pi / wavelength * math.sin(theta/2.0) |
---|
206 | |
---|
207 | if has_converter == True and output.Q_unit != '1/A': |
---|
208 | qx = data_conv_q(qx, units=output.Q_unit) |
---|
209 | |
---|
210 | if xmin == None or qx < xmin: |
---|
211 | xmin = qx |
---|
212 | if xmax == None or qx > xmax: |
---|
213 | xmax = qx |
---|
214 | |
---|
215 | theta = (i_y - center_y) * 0.5 / distance / 100.0 |
---|
216 | qy = 4.0 * math.pi / wavelength * math.sin(theta / 2.0) |
---|
217 | |
---|
218 | if has_converter == True and output.Q_unit != '1/A': |
---|
219 | qy = data_conv_q(qy, units=output.Q_unit) |
---|
220 | |
---|
221 | if ymin == None or qy < ymin: |
---|
222 | ymin = qy |
---|
223 | if ymax == None or qy > ymax: |
---|
224 | ymax = qy |
---|
225 | |
---|
226 | if not qx in x: |
---|
227 | x.append(qx) |
---|
228 | if not qy in y: |
---|
229 | y.append(qy) |
---|
230 | |
---|
231 | itot += 1 |
---|
232 | |
---|
233 | |
---|
234 | theta = 0.25 / distance / 100.0 |
---|
235 | xstep = 4.0 * math.pi / wavelength * math.sin(theta / 2.0) |
---|
236 | |
---|
237 | theta = 0.25 / distance / 100.0 |
---|
238 | ystep = 4.0 * math.pi/ wavelength * math.sin(theta / 2.0) |
---|
239 | |
---|
240 | # Store all data ###################################### |
---|
241 | # Store wavelength |
---|
242 | if has_converter == True and output.source.wavelength_unit != 'A': |
---|
243 | conv = Converter('A') |
---|
244 | wavelength = conv(wavelength, units=output.source.wavelength_unit) |
---|
245 | output.source.wavelength = wavelength |
---|
246 | |
---|
247 | # Store distance |
---|
248 | if has_converter == True and detector.distance_unit != 'm': |
---|
249 | conv = Converter('m') |
---|
250 | distance = conv(distance, units=detector.distance_unit) |
---|
251 | detector.distance = distance |
---|
252 | |
---|
253 | # Store transmission |
---|
254 | output.sample.transmission = transmission |
---|
255 | |
---|
256 | # Store pixel size |
---|
257 | pixel = 5.0 |
---|
258 | if has_converter == True and detector.pixel_size_unit != 'mm': |
---|
259 | conv = Converter('mm') |
---|
260 | pixel = conv(pixel, units=detector.pixel_size_unit) |
---|
261 | detector.pixel_size.x = pixel |
---|
262 | detector.pixel_size.y = pixel |
---|
263 | |
---|
264 | # Store beam center in distance units |
---|
265 | detector.beam_center.x = center_x * pixel |
---|
266 | detector.beam_center.y = center_y * pixel |
---|
267 | |
---|
268 | # Store limits of the image (2D array) |
---|
269 | xmin = xmin - xstep / 2.0 |
---|
270 | xmax = xmax + xstep / 2.0 |
---|
271 | ymin = ymin - ystep / 2.0 |
---|
272 | ymax = ymax + ystep / 2.0 |
---|
273 | if has_converter == True and output.Q_unit != '1/A': |
---|
274 | xmin = data_conv_q(xmin, units=output.Q_unit) |
---|
275 | xmax = data_conv_q(xmax, units=output.Q_unit) |
---|
276 | ymin = data_conv_q(ymin, units=output.Q_unit) |
---|
277 | ymax = data_conv_q(ymax, units=output.Q_unit) |
---|
278 | output.xmin = xmin |
---|
279 | output.xmax = xmax |
---|
280 | output.ymin = ymin |
---|
281 | output.ymax = ymax |
---|
282 | |
---|
283 | # Store x and y axis bin centers |
---|
284 | output.x_bins = x |
---|
285 | output.y_bins = y |
---|
286 | |
---|
287 | # Units |
---|
288 | if data_conv_q is not None: |
---|
289 | output.xaxis("\\rm{Q_{x}}", output.Q_unit) |
---|
290 | output.yaxis("\\rm{Q_{y}}", output.Q_unit) |
---|
291 | else: |
---|
292 | output.xaxis("\\rm{Q_{x}}", 'A^{-1}') |
---|
293 | output.yaxis("\\rm{Q_{y}}", 'A^{-1}') |
---|
294 | |
---|
295 | if data_conv_i is not None: |
---|
296 | output.zaxis("\\rm{Intensity}", output.I_unit) |
---|
297 | else: |
---|
298 | output.zaxis("\\rm{Intensity}", "cm^{-1}") |
---|
299 | |
---|
300 | # Store loading process information |
---|
301 | output.meta_data['loader'] = self.type_name |
---|
302 | output = reader2D_converter(output) |
---|
303 | |
---|
304 | return output |
---|