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