1 | |
---|
2 | ############################################################################ |
---|
3 | #This software was developed by the University of Tennessee as part of the |
---|
4 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
5 | #project funded by the US National Science Foundation. |
---|
6 | #If you use DANSE applications to do scientific research that leads to |
---|
7 | #publication, we ask that you acknowledge the use of the software with the |
---|
8 | #following sentence: |
---|
9 | #This work benefited from DANSE software developed under NSF award DMR-0520547. |
---|
10 | #copyright 2008, University of Tennessee |
---|
11 | ############################################################################# |
---|
12 | |
---|
13 | """ |
---|
14 | IGOR 2D reduced file reader |
---|
15 | """ |
---|
16 | |
---|
17 | import os, sys |
---|
18 | import numpy |
---|
19 | import math, logging |
---|
20 | from DataLoader.data_info import Data2D, Detector |
---|
21 | from DataLoader.manipulations import reader2D_converter |
---|
22 | |
---|
23 | # Look for unit converter |
---|
24 | has_converter = True |
---|
25 | try: |
---|
26 | from data_util.nxsunit import Converter |
---|
27 | except: |
---|
28 | has_converter = False |
---|
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: print str(output.detector[0]) |
---|
54 | output.detector.append(detector) |
---|
55 | |
---|
56 | # Get content |
---|
57 | dataStarted = False |
---|
58 | |
---|
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 | raise ValueError,"IgorReader: can't read this file, missing wavelength" |
---|
103 | |
---|
104 | #Find # of bins in a row assuming the detector is square. |
---|
105 | if dataStarted == True: |
---|
106 | try: |
---|
107 | value = float(line) |
---|
108 | except: |
---|
109 | # Found a non-float entry, skip it |
---|
110 | continue |
---|
111 | |
---|
112 | # Get total bin number |
---|
113 | |
---|
114 | i_tot_row += 1 |
---|
115 | i_tot_row=math.ceil(math.sqrt(i_tot_row))-1 |
---|
116 | #print "i_tot", i_tot_row |
---|
117 | size_x = i_tot_row#192#128 |
---|
118 | size_y = i_tot_row#192#128 |
---|
119 | output.data = numpy.zeros([size_x,size_y]) |
---|
120 | output.err_data = numpy.zeros([size_x,size_y]) |
---|
121 | |
---|
122 | #Read Header and 2D data |
---|
123 | for line in lines: |
---|
124 | # Find setup info line |
---|
125 | if isInfo: |
---|
126 | isInfo = False |
---|
127 | line_toks = line.split() |
---|
128 | # Wavelength in Angstrom |
---|
129 | try: |
---|
130 | wavelength = float(line_toks[1]) |
---|
131 | except: |
---|
132 | raise ValueError,"IgorReader: can't read this file, missing wavelength" |
---|
133 | # Distance in meters |
---|
134 | try: |
---|
135 | distance = float(line_toks[3]) |
---|
136 | except: |
---|
137 | raise ValueError,"IgorReader: can't read this file, missing distance" |
---|
138 | |
---|
139 | # Distance in meters |
---|
140 | try: |
---|
141 | transmission = float(line_toks[4]) |
---|
142 | except: |
---|
143 | raise ValueError,"IgorReader: can't read this file, missing transmission" |
---|
144 | |
---|
145 | if line.count("LAMBDA")>0: |
---|
146 | isInfo = True |
---|
147 | |
---|
148 | # Find center info line |
---|
149 | if isCenter: |
---|
150 | isCenter = False |
---|
151 | line_toks = line.split() |
---|
152 | |
---|
153 | # Center in bin number: Must substrate 1 because the index starts from 1 |
---|
154 | center_x = float(line_toks[0])-1 |
---|
155 | center_y = float(line_toks[1])-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 | # Store loading process information |
---|
293 | output.meta_data['loader'] = self.type_name |
---|
294 | output = reader2D_converter(output) |
---|
295 | |
---|
296 | return output |
---|
297 | |
---|
298 | if __name__ == "__main__": |
---|
299 | reader = Reader() |
---|
300 | print reader.read("../test/MAR07232_rest.ASC") |
---|
301 | |
---|