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 or 3 columns) |
---|
25 | """ |
---|
26 | ## File type |
---|
27 | type = ["ASCII files (*.txt)|*.txt", |
---|
28 | "ASCII files (*.dat)|*.dat"] |
---|
29 | ## List of allowed extensions |
---|
30 | ext=['.txt', '.TXT', '.dat', '.DAT'] |
---|
31 | |
---|
32 | def read(self, path): |
---|
33 | """ |
---|
34 | Load data file |
---|
35 | |
---|
36 | @param path: file path |
---|
37 | @return: Data1D object, or None |
---|
38 | @raise RuntimeError: when the file can't be opened |
---|
39 | @raise ValueError: when the length of the data vectors are inconsistent |
---|
40 | """ |
---|
41 | if os.path.isfile(path): |
---|
42 | basename = os.path.basename(path) |
---|
43 | root, extension = os.path.splitext(basename) |
---|
44 | if extension.lower() in self.ext: |
---|
45 | try: |
---|
46 | input_f = open(path,'r') |
---|
47 | except : |
---|
48 | raise RuntimeError, "ascii_reader: cannot open %s" % path |
---|
49 | buff = input_f.read() |
---|
50 | lines = buff.split('\n') |
---|
51 | x = numpy.zeros(0) |
---|
52 | y = numpy.zeros(0) |
---|
53 | dy = numpy.zeros(0) |
---|
54 | output = Data1D(x, y, dy=dy) |
---|
55 | self.filename = output.filename = basename |
---|
56 | |
---|
57 | data_conv_q = None |
---|
58 | data_conv_i = None |
---|
59 | |
---|
60 | if has_converter == True and output.x_unit != 'A^{-1}': |
---|
61 | data_conv_q = Converter('A^{-1}') |
---|
62 | # Test it |
---|
63 | data_conv_q(1.0, output.x_unit) |
---|
64 | |
---|
65 | if has_converter == True and output.y_unit != 'cm^{-1}': |
---|
66 | data_conv_i = Converter('cm^{-1}') |
---|
67 | # Test it |
---|
68 | data_conv_i(1.0, output.y_unit) |
---|
69 | |
---|
70 | |
---|
71 | # The first good line of data will define whether |
---|
72 | # we have 2-column or 3-column ascii |
---|
73 | has_error = None |
---|
74 | |
---|
75 | for line in lines: |
---|
76 | toks = line.split() |
---|
77 | try: |
---|
78 | _x = float(toks[0]) |
---|
79 | _y = float(toks[1]) |
---|
80 | |
---|
81 | if data_conv_q is not None: |
---|
82 | _x = data_conv_q(_x, units=output.x_unit) |
---|
83 | |
---|
84 | if data_conv_i is not None: |
---|
85 | _y = data_conv_i(_y, units=output.y_unit) |
---|
86 | |
---|
87 | # If we have an extra token, check |
---|
88 | # whether it can be interpreted as a |
---|
89 | # third column. |
---|
90 | _dy = None |
---|
91 | if len(toks)>2: |
---|
92 | try: |
---|
93 | _dy = float(toks[2]) |
---|
94 | |
---|
95 | if data_conv_i is not None: |
---|
96 | _dy = data_conv_i(_dy, units=output.y_unit) |
---|
97 | |
---|
98 | except: |
---|
99 | # The third column is not a float, skip it. |
---|
100 | pass |
---|
101 | |
---|
102 | # If we haven't set the 3rd column |
---|
103 | # flag, set it now. |
---|
104 | if has_error == None: |
---|
105 | has_error = False if _dy == None else True |
---|
106 | |
---|
107 | x = numpy.append(x, _x) |
---|
108 | y = numpy.append(y, _y) |
---|
109 | if has_error == True: |
---|
110 | dy = numpy.append(dy, _dy) |
---|
111 | |
---|
112 | except: |
---|
113 | # Couldn't parse this line, skip it |
---|
114 | pass |
---|
115 | |
---|
116 | |
---|
117 | # Sanity check |
---|
118 | if has_error == True and not len(y) == len(dy): |
---|
119 | raise RuntimeError, "ascii_reader: y and dy have different length" |
---|
120 | |
---|
121 | # If the data length is zero, consider this as |
---|
122 | # though we were not able to read the file. |
---|
123 | if len(x)==0: |
---|
124 | raise RuntimeError, "ascii_reader: could not load file" |
---|
125 | |
---|
126 | output.x = x |
---|
127 | output.y = y |
---|
128 | output.dy = dy if has_error == True else None |
---|
129 | if data_conv_q is not None: |
---|
130 | output.xaxis("\\rm{Q}", output.x_unit) |
---|
131 | else: |
---|
132 | output.xaxis("\\rm{Q}", 'A^{-1}') |
---|
133 | if data_conv_i is not None: |
---|
134 | output.yaxis("\\{I(Q)}", output.y_unit) |
---|
135 | else: |
---|
136 | output.yaxis("\\rm{I(Q)}","cm^{-1}") |
---|
137 | |
---|
138 | return output |
---|
139 | else: |
---|
140 | raise RuntimeError, "%s is not a file" % path |
---|
141 | return None |
---|
142 | |
---|
143 | if __name__ == "__main__": |
---|
144 | reader = Reader() |
---|
145 | #print reader.read("../test/test_3_columns.txt") |
---|
146 | print reader.read("../test/empty.txt") |
---|
147 | |
---|
148 | |
---|
149 | |
---|