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