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