1 | """ |
---|
2 | ASCII reader |
---|
3 | """ |
---|
4 | ############################################################################ |
---|
5 | #This software was developed by the University of Tennessee as part of the |
---|
6 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
7 | #project funded by the US National Science Foundation. |
---|
8 | #If you use DANSE applications to do scientific research that leads to |
---|
9 | #publication, we ask that you acknowledge the use of the software with the |
---|
10 | #following sentence: |
---|
11 | #This work benefited from DANSE software developed under NSF award DMR-0520547. |
---|
12 | #copyright 2008, University of Tennessee |
---|
13 | ############################################################################# |
---|
14 | |
---|
15 | |
---|
16 | import numpy |
---|
17 | import os |
---|
18 | from sans.dataloader.data_info import Data1D |
---|
19 | |
---|
20 | # Check whether we have a converter available |
---|
21 | has_converter = True |
---|
22 | try: |
---|
23 | from data_util.nxsunit import Converter |
---|
24 | except: |
---|
25 | has_converter = False |
---|
26 | _ZERO = 1e-16 |
---|
27 | |
---|
28 | |
---|
29 | class Reader: |
---|
30 | """ |
---|
31 | Class to load ascii files (2, 3 or 4 columns). |
---|
32 | """ |
---|
33 | ## File type |
---|
34 | type_name = "ASCII" |
---|
35 | |
---|
36 | ## Wildcards |
---|
37 | type = ["ASCII files (*.txt)|*.txt", |
---|
38 | "ASCII files (*.dat)|*.dat", |
---|
39 | "ASCII files (*.abs)|*.abs", |
---|
40 | "CSV files (*.csv)|*.csv"] |
---|
41 | ## List of allowed extensions |
---|
42 | ext = ['.txt', '.TXT', '.dat', '.DAT', '.abs', '.ABS', 'csv', 'CSV'] |
---|
43 | |
---|
44 | ## Flag to bypass extension check |
---|
45 | allow_all = True |
---|
46 | |
---|
47 | def read(self, path): |
---|
48 | """ |
---|
49 | Load data file |
---|
50 | |
---|
51 | :param path: file path |
---|
52 | |
---|
53 | :return: Data1D object, or None |
---|
54 | |
---|
55 | :raise RuntimeError: when the file can't be opened |
---|
56 | :raise ValueError: when the length of the data vectors are inconsistent |
---|
57 | """ |
---|
58 | if os.path.isfile(path): |
---|
59 | basename = os.path.basename(path) |
---|
60 | _, extension = os.path.splitext(basename) |
---|
61 | if self.allow_all or extension.lower() in self.ext: |
---|
62 | try: |
---|
63 | # Read in binary mode since GRASP frequently has no-ascii |
---|
64 | # characters that brakes the open operation |
---|
65 | input_f = open(path,'rb') |
---|
66 | except: |
---|
67 | raise RuntimeError, "ascii_reader: cannot open %s" % path |
---|
68 | buff = input_f.read() |
---|
69 | lines = buff.split('\n') |
---|
70 | |
---|
71 | #Jae could not find python universal line spliter: |
---|
72 | #keep the below for now |
---|
73 | # some ascii data has \r line separator, |
---|
74 | # try it when the data is on only one long line |
---|
75 | if len(lines) < 2 : |
---|
76 | lines = buff.split('\r') |
---|
77 | |
---|
78 | x = numpy.zeros(0) |
---|
79 | y = numpy.zeros(0) |
---|
80 | dy = numpy.zeros(0) |
---|
81 | dx = numpy.zeros(0) |
---|
82 | |
---|
83 | #temp. space to sort data |
---|
84 | tx = numpy.zeros(0) |
---|
85 | ty = numpy.zeros(0) |
---|
86 | tdy = numpy.zeros(0) |
---|
87 | tdx = numpy.zeros(0) |
---|
88 | |
---|
89 | output = Data1D(x, y, dy=dy, dx=dx) |
---|
90 | self.filename = output.filename = basename |
---|
91 | |
---|
92 | data_conv_q = None |
---|
93 | data_conv_i = None |
---|
94 | |
---|
95 | if has_converter == True and output.x_unit != '1/A': |
---|
96 | data_conv_q = Converter('1/A') |
---|
97 | # Test it |
---|
98 | data_conv_q(1.0, output.x_unit) |
---|
99 | |
---|
100 | if has_converter == True and output.y_unit != '1/cm': |
---|
101 | data_conv_i = Converter('1/cm') |
---|
102 | # Test it |
---|
103 | data_conv_i(1.0, output.y_unit) |
---|
104 | |
---|
105 | # The first good line of data will define whether |
---|
106 | # we have 2-column or 3-column ascii |
---|
107 | has_error_dx = None |
---|
108 | has_error_dy = None |
---|
109 | |
---|
110 | #Initialize counters for data lines and header lines. |
---|
111 | is_data = False # Has more than 5 lines |
---|
112 | # More than "5" lines of data is considered as actual |
---|
113 | # data unless that is the only data |
---|
114 | mum_data_lines = 5 |
---|
115 | # To count # of current data candidate lines |
---|
116 | i = -1 |
---|
117 | # To count total # of previous data candidate lines |
---|
118 | i1 = -1 |
---|
119 | # To count # of header lines |
---|
120 | j = -1 |
---|
121 | # Helps to count # of header lines |
---|
122 | j1 = -1 |
---|
123 | #minimum required number of columns of data; ( <= 4). |
---|
124 | lentoks = 2 |
---|
125 | for line in lines: |
---|
126 | toks = line.split(',') |
---|
127 | if len(toks) < 2: |
---|
128 | toks = line.split() |
---|
129 | try: |
---|
130 | #Make sure that all columns are numbers. |
---|
131 | for colnum in range(len(toks)): |
---|
132 | float(toks[colnum]) |
---|
133 | |
---|
134 | _x = float(toks[0]) |
---|
135 | _y = float(toks[1]) |
---|
136 | |
---|
137 | #Reset the header line counters |
---|
138 | if j == j1: |
---|
139 | j = 0 |
---|
140 | j1 = 0 |
---|
141 | |
---|
142 | if i > 1: |
---|
143 | is_data = True |
---|
144 | |
---|
145 | if data_conv_q is not None: |
---|
146 | _x = data_conv_q(_x, units=output.x_unit) |
---|
147 | |
---|
148 | if data_conv_i is not None: |
---|
149 | _y = data_conv_i(_y, units=output.y_unit) |
---|
150 | |
---|
151 | # If we have an extra token, check |
---|
152 | # whether it can be interpreted as a |
---|
153 | # third column. |
---|
154 | _dy = None |
---|
155 | if len(toks) > 2: |
---|
156 | try: |
---|
157 | _dy = float(toks[2]) |
---|
158 | |
---|
159 | if data_conv_i is not None: |
---|
160 | _dy = data_conv_i(_dy, units=output.y_unit) |
---|
161 | |
---|
162 | except: |
---|
163 | # The third column is not a float, skip it. |
---|
164 | pass |
---|
165 | |
---|
166 | # If we haven't set the 3rd column |
---|
167 | # flag, set it now. |
---|
168 | if has_error_dy == None: |
---|
169 | has_error_dy = False if _dy == None else True |
---|
170 | |
---|
171 | #Check for dx |
---|
172 | _dx = None |
---|
173 | if len(toks) > 3: |
---|
174 | try: |
---|
175 | _dx = float(toks[3]) |
---|
176 | |
---|
177 | if data_conv_i is not None: |
---|
178 | _dx = data_conv_i(_dx, units=output.x_unit) |
---|
179 | |
---|
180 | except: |
---|
181 | # The 4th column is not a float, skip it. |
---|
182 | pass |
---|
183 | |
---|
184 | # If we haven't set the 3rd column |
---|
185 | # flag, set it now. |
---|
186 | if has_error_dx == None: |
---|
187 | has_error_dx = False if _dx == None else True |
---|
188 | |
---|
189 | #After talked with PB, we decided to take care of only |
---|
190 | # 4 columns of data for now. |
---|
191 | #number of columns in the current line |
---|
192 | #To remember the # of columns in the current |
---|
193 | #line of data |
---|
194 | new_lentoks = len(toks) |
---|
195 | |
---|
196 | #If the previous columns not equal to the current, |
---|
197 | #mark the previous as non-data and reset the dependents. |
---|
198 | if lentoks != new_lentoks: |
---|
199 | if is_data == True: |
---|
200 | break |
---|
201 | else: |
---|
202 | i = -1 |
---|
203 | i1 = 0 |
---|
204 | j = -1 |
---|
205 | j1 = -1 |
---|
206 | |
---|
207 | #Delete the previously stored lines of data candidates |
---|
208 | # if is not data. |
---|
209 | if i < 0 and -1 < i1 < mum_data_lines and \ |
---|
210 | is_data == False: |
---|
211 | try: |
---|
212 | x = numpy.zeros(0) |
---|
213 | y = numpy.zeros(0) |
---|
214 | except: |
---|
215 | pass |
---|
216 | |
---|
217 | x = numpy.append(x, _x) |
---|
218 | y = numpy.append(y, _y) |
---|
219 | |
---|
220 | if has_error_dy == True: |
---|
221 | #Delete the previously stored lines of |
---|
222 | # data candidates if is not data. |
---|
223 | if i < 0 and -1 < i1 < mum_data_lines and \ |
---|
224 | is_data == False: |
---|
225 | try: |
---|
226 | dy = numpy.zeros(0) |
---|
227 | except: |
---|
228 | pass |
---|
229 | dy = numpy.append(dy, _dy) |
---|
230 | |
---|
231 | if has_error_dx == True: |
---|
232 | #Delete the previously stored lines of |
---|
233 | # data candidates if is not data. |
---|
234 | if i < 0 and -1 < i1 < mum_data_lines and \ |
---|
235 | is_data == False: |
---|
236 | try: |
---|
237 | dx = numpy.zeros(0) |
---|
238 | except: |
---|
239 | pass |
---|
240 | dx = numpy.append(dx, _dx) |
---|
241 | |
---|
242 | #Same for temp. |
---|
243 | #Delete the previously stored lines of data candidates |
---|
244 | # if is not data. |
---|
245 | if i < 0 and -1 < i1 < mum_data_lines and\ |
---|
246 | is_data == False: |
---|
247 | try: |
---|
248 | tx = numpy.zeros(0) |
---|
249 | ty = numpy.zeros(0) |
---|
250 | except: |
---|
251 | pass |
---|
252 | |
---|
253 | tx = numpy.append(tx, _x) |
---|
254 | ty = numpy.append(ty, _y) |
---|
255 | |
---|
256 | if has_error_dy == True: |
---|
257 | #Delete the previously stored lines of |
---|
258 | # data candidates if is not data. |
---|
259 | if i < 0 and -1 < i1 < mum_data_lines and \ |
---|
260 | is_data == False: |
---|
261 | try: |
---|
262 | tdy = numpy.zeros(0) |
---|
263 | except: |
---|
264 | pass |
---|
265 | tdy = numpy.append(tdy, _dy) |
---|
266 | if has_error_dx == True: |
---|
267 | #Delete the previously stored lines of |
---|
268 | # data candidates if is not data. |
---|
269 | if i < 0 and -1 < i1 < mum_data_lines and \ |
---|
270 | is_data == False: |
---|
271 | try: |
---|
272 | tdx = numpy.zeros(0) |
---|
273 | except: |
---|
274 | pass |
---|
275 | tdx = numpy.append(tdx, _dx) |
---|
276 | |
---|
277 | #reset i1 and flag lentoks for the next |
---|
278 | if lentoks < new_lentoks: |
---|
279 | if is_data == False: |
---|
280 | i1 = -1 |
---|
281 | #To remember the # of columns on the current line |
---|
282 | # for the next line of data |
---|
283 | lentoks = len(toks) |
---|
284 | |
---|
285 | #Reset # of header lines and counts # |
---|
286 | # of data candidate lines |
---|
287 | if j == 0 and j1 == 0: |
---|
288 | i1 = i + 1 |
---|
289 | i += 1 |
---|
290 | except: |
---|
291 | |
---|
292 | # It is data and meet non - number, then stop reading |
---|
293 | if is_data == True: |
---|
294 | break |
---|
295 | lentoks = 2 |
---|
296 | #Counting # of header lines |
---|
297 | j += 1 |
---|
298 | if j == j1 + 1: |
---|
299 | j1 = j |
---|
300 | else: |
---|
301 | j = -1 |
---|
302 | #Reset # of lines of data candidates |
---|
303 | i = -1 |
---|
304 | |
---|
305 | # Couldn't parse this line, skip it |
---|
306 | pass |
---|
307 | |
---|
308 | input_f.close() |
---|
309 | # Sanity check |
---|
310 | if has_error_dy == True and not len(y) == len(dy): |
---|
311 | msg = "ascii_reader: y and dy have different length" |
---|
312 | raise RuntimeError, msg |
---|
313 | if has_error_dx == True and not len(x) == len(dx): |
---|
314 | msg = "ascii_reader: y and dy have different length" |
---|
315 | raise RuntimeError, msg |
---|
316 | # If the data length is zero, consider this as |
---|
317 | # though we were not able to read the file. |
---|
318 | if len(x) == 0: |
---|
319 | raise RuntimeError, "ascii_reader: could not load file" |
---|
320 | |
---|
321 | #Let's re-order the data to make cal. |
---|
322 | # curve look better some cases |
---|
323 | ind = numpy.lexsort((ty, tx)) |
---|
324 | for i in ind: |
---|
325 | x[i] = tx[ind[i]] |
---|
326 | y[i] = ty[ind[i]] |
---|
327 | if has_error_dy == True: |
---|
328 | dy[i] = tdy[ind[i]] |
---|
329 | if has_error_dx == True: |
---|
330 | dx[i] = tdx[ind[i]] |
---|
331 | # Zeros in dx, dy |
---|
332 | if has_error_dx: |
---|
333 | dx[dx == 0] = _ZERO |
---|
334 | if has_error_dy: |
---|
335 | dy[dy == 0] = _ZERO |
---|
336 | #Data |
---|
337 | output.x = x[x != 0] |
---|
338 | output.y = y[x != 0] |
---|
339 | output.dy = dy[x != 0] if has_error_dy == True\ |
---|
340 | else numpy.zeros(len(output.y)) |
---|
341 | output.dx = dx[x != 0] if has_error_dx == True\ |
---|
342 | else numpy.zeros(len(output.x)) |
---|
343 | |
---|
344 | if data_conv_q is not None: |
---|
345 | output.xaxis("\\rm{Q}", output.x_unit) |
---|
346 | else: |
---|
347 | output.xaxis("\\rm{Q}", 'A^{-1}') |
---|
348 | if data_conv_i is not None: |
---|
349 | output.yaxis("\\rm{Intensity}", output.y_unit) |
---|
350 | else: |
---|
351 | output.yaxis("\\rm{Intensity}", "cm^{-1}") |
---|
352 | |
---|
353 | # Store loading process information |
---|
354 | output.meta_data['loader'] = self.type_name |
---|
355 | if len(output.x) < 1: |
---|
356 | raise RuntimeError, "%s is empty" % path |
---|
357 | return output |
---|
358 | |
---|
359 | else: |
---|
360 | raise RuntimeError, "%s is not a file" % path |
---|
361 | return None |
---|