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