1 | |
---|
2 | |
---|
3 | ############################################################################ |
---|
4 | #This software was developed by the University of Tennessee as part of the |
---|
5 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
6 | #project funded by the US National Science Foundation. |
---|
7 | #If you use DANSE applications to do scientific research that leads to |
---|
8 | #publication, we ask that you acknowledge the use of the software with the |
---|
9 | #following sentence: |
---|
10 | #This work benefited from DANSE software developed under NSF award DMR-0520547. |
---|
11 | #copyright 2008, University of Tennessee |
---|
12 | ############################################################################# |
---|
13 | |
---|
14 | """ |
---|
15 | DANSE/SANS file reader |
---|
16 | """ |
---|
17 | |
---|
18 | import math |
---|
19 | import os |
---|
20 | #import copy |
---|
21 | import numpy |
---|
22 | import logging |
---|
23 | from sans.dataloader.data_info import Data2D, Detector |
---|
24 | from sans.dataloader.manipulations import reader2D_converter |
---|
25 | |
---|
26 | # Look for unit converter |
---|
27 | has_converter = True |
---|
28 | try: |
---|
29 | from data_util.nxsunit import Converter |
---|
30 | except: |
---|
31 | has_converter = False |
---|
32 | |
---|
33 | class Reader: |
---|
34 | """ |
---|
35 | Example data manipulation |
---|
36 | """ |
---|
37 | ## File type |
---|
38 | type_name = "DANSE" |
---|
39 | ## Wildcards |
---|
40 | type = ["DANSE files (*.sans)|*.sans"] |
---|
41 | ## Extension |
---|
42 | ext = ['.sans', '.SANS'] |
---|
43 | |
---|
44 | def read(self, filename=None): |
---|
45 | """ |
---|
46 | Open and read the data in a file |
---|
47 | @param file: path of the file |
---|
48 | """ |
---|
49 | |
---|
50 | read_it = False |
---|
51 | for item in self.ext: |
---|
52 | if filename.lower().find(item) >= 0: |
---|
53 | read_it = True |
---|
54 | |
---|
55 | if read_it: |
---|
56 | try: |
---|
57 | datafile = open(filename, 'r') |
---|
58 | except : |
---|
59 | raise RuntimeError,"danse_reader cannot open %s" % (filename) |
---|
60 | |
---|
61 | # defaults |
---|
62 | # wavelength in Angstrom |
---|
63 | wavelength = 10.0 |
---|
64 | # Distance in meter |
---|
65 | distance = 11.0 |
---|
66 | # Pixel number of center in x |
---|
67 | center_x = 65 |
---|
68 | # Pixel number of center in y |
---|
69 | center_y = 65 |
---|
70 | # Pixel size [mm] |
---|
71 | pixel = 5.0 |
---|
72 | # Size in x, in pixels |
---|
73 | size_x = 128 |
---|
74 | # Size in y, in pixels |
---|
75 | size_y = 128 |
---|
76 | # Format version |
---|
77 | fversion = 1.0 |
---|
78 | |
---|
79 | output = Data2D() |
---|
80 | output.filename = os.path.basename(filename) |
---|
81 | detector = Detector() |
---|
82 | output.detector.append(detector) |
---|
83 | |
---|
84 | output.data = numpy.zeros([size_x,size_y]) |
---|
85 | output.err_data = numpy.zeros([size_x, size_y]) |
---|
86 | |
---|
87 | data_conv_q = None |
---|
88 | data_conv_i = None |
---|
89 | |
---|
90 | if has_converter == True and output.Q_unit != '1/A': |
---|
91 | data_conv_q = Converter('1/A') |
---|
92 | # Test it |
---|
93 | data_conv_q(1.0, output.Q_unit) |
---|
94 | |
---|
95 | if has_converter == True and output.I_unit != '1/cm': |
---|
96 | data_conv_i = Converter('1/cm') |
---|
97 | # Test it |
---|
98 | data_conv_i(1.0, output.I_unit) |
---|
99 | |
---|
100 | read_on = True |
---|
101 | while read_on: |
---|
102 | line = datafile.readline() |
---|
103 | if line.find("DATA:") >= 0: |
---|
104 | read_on = False |
---|
105 | break |
---|
106 | toks = line.split(':') |
---|
107 | if toks[0] == "FORMATVERSION": |
---|
108 | fversion = float(toks[1]) |
---|
109 | if toks[0] == "WAVELENGTH": |
---|
110 | wavelength = float(toks[1]) |
---|
111 | elif toks[0] == "DISTANCE": |
---|
112 | distance = float(toks[1]) |
---|
113 | elif toks[0] == "CENTER_X": |
---|
114 | center_x = float(toks[1]) |
---|
115 | elif toks[0] == "CENTER_Y": |
---|
116 | center_y = float(toks[1]) |
---|
117 | elif toks[0] == "PIXELSIZE": |
---|
118 | pixel = float(toks[1]) |
---|
119 | elif toks[0] == "SIZE_X": |
---|
120 | size_x = int(toks[1]) |
---|
121 | elif toks[0] == "SIZE_Y": |
---|
122 | size_y = int(toks[1]) |
---|
123 | |
---|
124 | # Read the data |
---|
125 | data = [] |
---|
126 | error = [] |
---|
127 | if fversion == 1.0: |
---|
128 | data_str = datafile.readline() |
---|
129 | data = data_str.split(' ') |
---|
130 | else: |
---|
131 | read_on = True |
---|
132 | while read_on: |
---|
133 | data_str = datafile.readline() |
---|
134 | if len(data_str) == 0: |
---|
135 | read_on = False |
---|
136 | else: |
---|
137 | toks = data_str.split() |
---|
138 | try: |
---|
139 | val = float(toks[0]) |
---|
140 | err = float(toks[1]) |
---|
141 | if data_conv_i is not None: |
---|
142 | val = data_conv_i(val, units=output.y_unit) |
---|
143 | err = data_conv_i(err, units=output.y_unit) |
---|
144 | data.append(val) |
---|
145 | error.append(err) |
---|
146 | except: |
---|
147 | logging.info("Skipping line:%s,%s" %( data_str, |
---|
148 | sys.exc_value)) |
---|
149 | |
---|
150 | # Initialize |
---|
151 | x_vals = [] |
---|
152 | y_vals = [] |
---|
153 | ymin = None |
---|
154 | ymax = None |
---|
155 | xmin = None |
---|
156 | xmax = None |
---|
157 | |
---|
158 | # Qx and Qy vectors |
---|
159 | theta = pixel / distance / 100.0 |
---|
160 | stepq = 4.0 * math.pi/wavelength * math.sin(theta/2.0) |
---|
161 | for i_x in range(size_x): |
---|
162 | theta = (i_x - center_x + 1) * pixel / distance / 100.0 |
---|
163 | qx = 4.0 * math.pi / wavelength * math.sin(theta/2.0) |
---|
164 | |
---|
165 | if has_converter == True and output.Q_unit != '1/A': |
---|
166 | qx = data_conv_q(qx, units=output.Q_unit) |
---|
167 | |
---|
168 | x_vals.append(qx) |
---|
169 | if xmin == None or qx < xmin: |
---|
170 | xmin = qx |
---|
171 | if xmax == None or qx > xmax: |
---|
172 | xmax = qx |
---|
173 | |
---|
174 | ymin = None |
---|
175 | ymax = None |
---|
176 | for i_y in range(size_y): |
---|
177 | theta = (i_y - center_y + 1) * pixel / distance / 100.0 |
---|
178 | qy = 4.0 * math.pi/wavelength * math.sin(theta/2.0) |
---|
179 | |
---|
180 | if has_converter == True and output.Q_unit != '1/A': |
---|
181 | qy = data_conv_q(qy, units=output.Q_unit) |
---|
182 | |
---|
183 | y_vals.append(qy) |
---|
184 | if ymin == None or qy < ymin: |
---|
185 | ymin = qy |
---|
186 | if ymax == None or qy > ymax: |
---|
187 | ymax = qy |
---|
188 | |
---|
189 | # Store the data in the 2D array |
---|
190 | itot = 0 |
---|
191 | i_x = 0 |
---|
192 | i_y = -1 |
---|
193 | |
---|
194 | for i_pt in range(len(data)): |
---|
195 | try: |
---|
196 | value = float(data[i_pt]) |
---|
197 | except: |
---|
198 | # For version 1.0, the data were still |
---|
199 | # stored as strings at this point. |
---|
200 | msg = "Skipping entry (v1.0):%s,%s" %(str(data[i_pt]), |
---|
201 | sys.exc_value) |
---|
202 | logging.info(msg) |
---|
203 | |
---|
204 | # Get bin number |
---|
205 | if math.fmod(i_pt, size_x) == 0: |
---|
206 | i_x = 0 |
---|
207 | i_y += 1 |
---|
208 | else: |
---|
209 | i_x += 1 |
---|
210 | |
---|
211 | output.data[i_y][i_x] = value |
---|
212 | #output.data[size_y-1-i_y][i_x] = value |
---|
213 | if fversion>1.0: |
---|
214 | output.err_data[i_y][i_x] = error[i_pt] |
---|
215 | #output.err_data[size_y-1-i_y][i_x] = error[i_pt] |
---|
216 | |
---|
217 | |
---|
218 | # Store all data |
---|
219 | # Store wavelength |
---|
220 | if has_converter == True and output.source.wavelength_unit != 'A': |
---|
221 | conv = Converter('A') |
---|
222 | wavelength = conv(wavelength, |
---|
223 | units=output.source.wavelength_unit) |
---|
224 | output.source.wavelength = wavelength |
---|
225 | |
---|
226 | # Store distance |
---|
227 | if has_converter == True and detector.distance_unit != 'm': |
---|
228 | conv = Converter('m') |
---|
229 | distance = conv(distance, units=detector.distance_unit) |
---|
230 | detector.distance = distance |
---|
231 | |
---|
232 | # Store pixel size |
---|
233 | if has_converter == True and detector.pixel_size_unit != 'mm': |
---|
234 | conv = Converter('mm') |
---|
235 | pixel = conv(pixel, units=detector.pixel_size_unit) |
---|
236 | detector.pixel_size.x = pixel |
---|
237 | detector.pixel_size.y = pixel |
---|
238 | |
---|
239 | # Store beam center in distance units |
---|
240 | detector.beam_center.x = center_x*pixel |
---|
241 | detector.beam_center.y = center_y*pixel |
---|
242 | |
---|
243 | # Store limits of the image (2D array) |
---|
244 | xmin = xmin - stepq / 2.0 |
---|
245 | xmax = xmax + stepq / 2.0 |
---|
246 | ymin = ymin - stepq /2.0 |
---|
247 | ymax = ymax + stepq / 2.0 |
---|
248 | |
---|
249 | if has_converter == True and output.Q_unit != '1/A': |
---|
250 | xmin = data_conv_q(xmin, units=output.Q_unit) |
---|
251 | xmax = data_conv_q(xmax, units=output.Q_unit) |
---|
252 | ymin = data_conv_q(ymin, units=output.Q_unit) |
---|
253 | ymax = data_conv_q(ymax, units=output.Q_unit) |
---|
254 | output.xmin = xmin |
---|
255 | output.xmax = xmax |
---|
256 | output.ymin = ymin |
---|
257 | output.ymax = ymax |
---|
258 | |
---|
259 | # Store x and y axis bin centers |
---|
260 | output.x_bins = x_vals |
---|
261 | output.y_bins = y_vals |
---|
262 | |
---|
263 | # Units |
---|
264 | if data_conv_q is not None: |
---|
265 | output.xaxis("\\rm{Q_{x}}", output.Q_unit) |
---|
266 | output.yaxis("\\rm{Q_{y}}", output.Q_unit) |
---|
267 | else: |
---|
268 | output.xaxis("\\rm{Q_{x}}", 'A^{-1}') |
---|
269 | output.yaxis("\\rm{Q_{y}}", 'A^{-1}') |
---|
270 | |
---|
271 | if data_conv_i is not None: |
---|
272 | output.zaxis("\\rm{Intensity}", output.I_unit) |
---|
273 | else: |
---|
274 | output.zaxis("\\rm{Intensity}","cm^{-1}") |
---|
275 | |
---|
276 | if not fversion >= 1.0: |
---|
277 | msg = "Danse_reader can't read this file %s" % filename |
---|
278 | raise ValueError, msg |
---|
279 | else: |
---|
280 | logging.info("Danse_reader Reading %s \n" % filename) |
---|
281 | |
---|
282 | # Store loading process information |
---|
283 | output.meta_data['loader'] = self.type_name |
---|
284 | output = reader2D_converter(output) |
---|
285 | return output |
---|
286 | |
---|
287 | return None |
---|
288 | |
---|
289 | if __name__ == "__main__": |
---|
290 | reader = Reader() |
---|
291 | print reader.read("../test/MP_New.sans") |
---|
292 | |
---|