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