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