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