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, Detector |
---|
14 | |
---|
15 | has_converter = True |
---|
16 | try: |
---|
17 | from data_util.nxsunit import Converter |
---|
18 | except: |
---|
19 | has_converter = False |
---|
20 | |
---|
21 | class Reader: |
---|
22 | """ |
---|
23 | Class to load IGOR reduced .ABS files |
---|
24 | """ |
---|
25 | ## File type |
---|
26 | type = ["IGOR 1D files (*.abs)|*.abs"] |
---|
27 | ## List of allowed extensions |
---|
28 | ext=['.abs', '.ABS'] |
---|
29 | |
---|
30 | def read(self, path): |
---|
31 | """ |
---|
32 | Load data file |
---|
33 | |
---|
34 | @param path: file path |
---|
35 | @return: Data1D object, or None |
---|
36 | @raise RuntimeError: when the file can't be opened |
---|
37 | @raise ValueError: when the length of the data vectors are inconsistent |
---|
38 | """ |
---|
39 | if os.path.isfile(path): |
---|
40 | basename = os.path.basename(path) |
---|
41 | root, extension = os.path.splitext(basename) |
---|
42 | if extension.lower() in self.ext: |
---|
43 | try: |
---|
44 | input_f = open(path,'r') |
---|
45 | except : |
---|
46 | raise RuntimeError, "abs_reader: cannot open %s" % path |
---|
47 | buff = input_f.read() |
---|
48 | lines = buff.split('\n') |
---|
49 | x = numpy.zeros(0) |
---|
50 | y = numpy.zeros(0) |
---|
51 | dy = numpy.zeros(0) |
---|
52 | dx = numpy.zeros(0) |
---|
53 | output = Data1D(x, y, dy=dy, dx=dx) |
---|
54 | detector = Detector() |
---|
55 | output.detector.append(detector) |
---|
56 | output.filename = basename |
---|
57 | |
---|
58 | is_info = False |
---|
59 | is_center = False |
---|
60 | is_data_started = False |
---|
61 | |
---|
62 | data_conv_q = None |
---|
63 | data_conv_i = None |
---|
64 | |
---|
65 | if has_converter == True and output.x_unit != '1/A': |
---|
66 | data_conv_q = Converter('1/A') |
---|
67 | # Test it |
---|
68 | data_conv_q(1.0, output.x_unit) |
---|
69 | |
---|
70 | if has_converter == True and output.y_unit != '1/cm': |
---|
71 | data_conv_i = Converter('1/cm') |
---|
72 | # Test it |
---|
73 | data_conv_i(1.0, output.y_unit) |
---|
74 | |
---|
75 | for line in lines: |
---|
76 | |
---|
77 | # Information line 1 |
---|
78 | if is_info==True: |
---|
79 | is_info = False |
---|
80 | line_toks = line.split() |
---|
81 | |
---|
82 | # Wavelength in Angstrom |
---|
83 | try: |
---|
84 | value = float(line_toks[1]) |
---|
85 | if has_converter==True and output.source.wavelength_unit != 'A': |
---|
86 | conv = Converter('A') |
---|
87 | output.source.wavelength = conv(value, units=output.source.wavelength_unit) |
---|
88 | else: |
---|
89 | output.source.wavelength = value |
---|
90 | except: |
---|
91 | #goes to ASC reader |
---|
92 | raise RuntimeError, "abs_reader: cannot open %s" % path |
---|
93 | #raise ValueError,"IgorReader: can't read this file, missing wavelength" |
---|
94 | |
---|
95 | # Distance in meters |
---|
96 | try: |
---|
97 | value = float(line_toks[3]) |
---|
98 | if has_converter==True and detector.distance_unit != 'm': |
---|
99 | conv = Converter('m') |
---|
100 | detector.distance = conv(value, units=detector.distance_unit) |
---|
101 | else: |
---|
102 | detector.distance = value |
---|
103 | except: |
---|
104 | #goes to ASC reader |
---|
105 | raise RuntimeError, "abs_reader: cannot open %s" % path |
---|
106 | |
---|
107 | # Transmission |
---|
108 | try: |
---|
109 | output.sample.transmission = float(line_toks[4]) |
---|
110 | except: |
---|
111 | # Transmission is not a mandatory entry |
---|
112 | pass |
---|
113 | |
---|
114 | # Thickness in mm |
---|
115 | try: |
---|
116 | value = float(line_toks[5]) |
---|
117 | if has_converter==True and output.sample.thickness_unit != 'cm': |
---|
118 | conv = Converter('cm') |
---|
119 | output.sample.thickness = conv(value, units=output.sample.thickness_unit) |
---|
120 | else: |
---|
121 | output.sample.thickness = value |
---|
122 | except: |
---|
123 | # Thickness is not a mandatory entry |
---|
124 | pass |
---|
125 | |
---|
126 | #MON CNT LAMBDA DET ANG DET DIST TRANS THICK AVE STEP |
---|
127 | if line.count("LAMBDA")>0: |
---|
128 | is_info = True |
---|
129 | |
---|
130 | # Find center info line |
---|
131 | if is_center==True: |
---|
132 | is_center = False |
---|
133 | line_toks = line.split() |
---|
134 | # Center in bin number |
---|
135 | center_x = float(line_toks[0]) |
---|
136 | center_y = float(line_toks[1]) |
---|
137 | |
---|
138 | # Bin size |
---|
139 | if has_converter==True and detector.pixel_size_unit != 'mm': |
---|
140 | conv = Converter('mm') |
---|
141 | detector.pixel_size.x = conv(5.0, units=detector.pixel_size_unit) |
---|
142 | detector.pixel_size.y = conv(5.0, units=detector.pixel_size_unit) |
---|
143 | else: |
---|
144 | detector.pixel_size.x = 5.0 |
---|
145 | detector.pixel_size.y = 5.0 |
---|
146 | |
---|
147 | # Store beam center in distance units |
---|
148 | # Det 640 x 640 mm |
---|
149 | if has_converter==True and detector.beam_center_unit != 'mm': |
---|
150 | conv = Converter('mm') |
---|
151 | detector.beam_center.x = conv(center_x*5.0, units=detector.beam_center_unit) |
---|
152 | detector.beam_center.y = conv(center_y*5.0, units=detector.beam_center_unit) |
---|
153 | else: |
---|
154 | detector.beam_center.x = center_x*5.0 |
---|
155 | detector.beam_center.y = center_y*5.0 |
---|
156 | |
---|
157 | # Detector type |
---|
158 | try: |
---|
159 | detector.name = line_toks[7] |
---|
160 | except: |
---|
161 | # Detector name is not a mandatory entry |
---|
162 | pass |
---|
163 | |
---|
164 | #BCENT(X,Y) A1(mm) A2(mm) A1A2DIST(m) DL/L BSTOP(mm) DET_TYP |
---|
165 | if line.count("BCENT")>0: |
---|
166 | is_center = True |
---|
167 | |
---|
168 | # Parse the data |
---|
169 | if is_data_started==True: |
---|
170 | toks = line.split() |
---|
171 | |
---|
172 | try: |
---|
173 | _x = float(toks[0]) |
---|
174 | _y = float(toks[1]) |
---|
175 | _dy = float(toks[2]) |
---|
176 | _dx = float(toks[3]) |
---|
177 | |
---|
178 | if data_conv_q is not None: |
---|
179 | _x = data_conv_q(_x, units=output.x_unit) |
---|
180 | _dx = data_conv_i(_dx, units=output.x_unit) |
---|
181 | |
---|
182 | if data_conv_i is not None: |
---|
183 | _y = data_conv_i(_y, units=output.y_unit) |
---|
184 | _dy = data_conv_i(_dy, units=output.y_unit) |
---|
185 | |
---|
186 | x = numpy.append(x, _x) |
---|
187 | y = numpy.append(y, _y) |
---|
188 | dy = numpy.append(dy, _dy) |
---|
189 | dx = numpy.append(dx, _dx) |
---|
190 | |
---|
191 | except: |
---|
192 | # Could not read this data line. If we are here |
---|
193 | # it is because we are in the data section. Just |
---|
194 | # skip it. |
---|
195 | pass |
---|
196 | |
---|
197 | #The 6 columns are | Q (1/A) | I(Q) (1/cm) | std. dev. I(Q) (1/cm) | sigmaQ | meanQ | ShadowFactor| |
---|
198 | if line.count("The 6 columns")>0: |
---|
199 | is_data_started = True |
---|
200 | |
---|
201 | # Sanity check |
---|
202 | if not len(y) == len(dy): |
---|
203 | raise ValueError, "abs_reader: y and dy have different length" |
---|
204 | |
---|
205 | # If the data length is zero, consider this as |
---|
206 | # though we were not able to read the file. |
---|
207 | if len(x)==0: |
---|
208 | raise ValueError, "ascii_reader: could not load file" |
---|
209 | |
---|
210 | output.x = x |
---|
211 | output.y = y |
---|
212 | output.dy = dy |
---|
213 | output.dx = dx |
---|
214 | if data_conv_q is not None: |
---|
215 | output.xaxis("\\rm{Q}", output.x_unit) |
---|
216 | else: |
---|
217 | output.xaxis("\\rm{Q}", 'A^{-1}') |
---|
218 | if data_conv_i is not None: |
---|
219 | output.yaxis("\\{I(Q)}", output.y_unit) |
---|
220 | else: |
---|
221 | output.yaxis("\\rm{I(Q)}","cm^{-1}") |
---|
222 | return output |
---|
223 | else: |
---|
224 | raise RuntimeError, "%s is not a file" % path |
---|
225 | return None |
---|
226 | |
---|
227 | if __name__ == "__main__": |
---|
228 | reader = Reader() |
---|
229 | print reader.read("../test/jan08002.ABS") |
---|
230 | |
---|
231 | |
---|
232 | |
---|