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 | class Reader: |
---|
16 | """ |
---|
17 | Class to load IGOR reduced .ABS files |
---|
18 | """ |
---|
19 | ## List of allowed extensions |
---|
20 | ext=['.abs'] |
---|
21 | |
---|
22 | def read(self, path): |
---|
23 | """ |
---|
24 | Load data file |
---|
25 | |
---|
26 | @param path: file path |
---|
27 | @return: Data1D object, or None |
---|
28 | @raise RuntimeError: when the file can't be opened |
---|
29 | @raise ValueError: when the length of the data vectors are inconsistent |
---|
30 | """ |
---|
31 | if os.path.isfile(path): |
---|
32 | basename = os.path.basename(path) |
---|
33 | root, extension = os.path.splitext(basename) |
---|
34 | if extension.lower() in self.ext: |
---|
35 | try: |
---|
36 | input_f = open(path,'r') |
---|
37 | except : |
---|
38 | raise RuntimeError, "abs_reader: cannot open %s" % path |
---|
39 | buff = input_f.read() |
---|
40 | lines = buff.split('\n') |
---|
41 | x = numpy.zeros(0) |
---|
42 | y = numpy.zeros(0) |
---|
43 | dy = numpy.zeros(0) |
---|
44 | output = Data1D(x, y, dy=dy) |
---|
45 | self.filename = output.filename = basename |
---|
46 | |
---|
47 | is_info = False |
---|
48 | is_center = False |
---|
49 | is_data_started = False |
---|
50 | |
---|
51 | for line in lines: |
---|
52 | |
---|
53 | # Information line 1 |
---|
54 | if is_info==True: |
---|
55 | is_info = False |
---|
56 | line_toks = line.split() |
---|
57 | |
---|
58 | # Wavelength in Angstrom |
---|
59 | try: |
---|
60 | output.source.wavelength = float(line_toks[1]) |
---|
61 | except: |
---|
62 | raise ValueError,"IgorReader: can't read this file, missing wavelength" |
---|
63 | |
---|
64 | # Distance in meters |
---|
65 | try: |
---|
66 | output.detector.distance = float(line_toks[3])*1000.0 |
---|
67 | except: |
---|
68 | raise ValueError,"IgorReader: can't read this file, missing distance" |
---|
69 | |
---|
70 | # Transmission |
---|
71 | try: |
---|
72 | output.sample.transmission = float(line_toks[4]) |
---|
73 | except: |
---|
74 | # Transmission is not a mandatory entry |
---|
75 | pass |
---|
76 | |
---|
77 | # Thickness |
---|
78 | try: |
---|
79 | output.sample.thickness = float(line_toks[5]) |
---|
80 | except: |
---|
81 | # Thickness is not a mandatory entry |
---|
82 | pass |
---|
83 | |
---|
84 | #MON CNT LAMBDA DET ANG DET DIST TRANS THICK AVE STEP |
---|
85 | if line.count("LAMBDA")>0: |
---|
86 | is_info = True |
---|
87 | |
---|
88 | # Find center info line |
---|
89 | if is_center==True: |
---|
90 | is_center = False |
---|
91 | line_toks = line.split() |
---|
92 | # Center in bin number |
---|
93 | center_x = float(line_toks[0]) |
---|
94 | center_y = float(line_toks[1]) |
---|
95 | output.detector.beam_center.x = center_x |
---|
96 | output.detector.beam_center.y = center_y |
---|
97 | |
---|
98 | # Detector type |
---|
99 | try: |
---|
100 | output.detector.name = line_toks[7] |
---|
101 | except: |
---|
102 | # Detector name is not a mandatory entry |
---|
103 | pass |
---|
104 | |
---|
105 | #BCENT(X,Y) A1(mm) A2(mm) A1A2DIST(m) DL/L BSTOP(mm) DET_TYP |
---|
106 | if line.count("BCENT")>0: |
---|
107 | is_center = True |
---|
108 | |
---|
109 | # Parse the data |
---|
110 | if is_data_started==True: |
---|
111 | toks = line.split() |
---|
112 | try: |
---|
113 | _x = float(toks[0]) |
---|
114 | _y = float(toks[1]) |
---|
115 | _dy = float(toks[2]) |
---|
116 | |
---|
117 | x = numpy.append(x, _x) |
---|
118 | y = numpy.append(y, _y) |
---|
119 | dy = numpy.append(dy, _dy) |
---|
120 | except: |
---|
121 | # Could not read this data line. If we are here |
---|
122 | # it is because we are in the data section. Just |
---|
123 | # skip it. |
---|
124 | pass |
---|
125 | |
---|
126 | #The 6 columns are | Q (1/A) | I(Q) (1/cm) | std. dev. I(Q) (1/cm) | sigmaQ | meanQ | ShadowFactor| |
---|
127 | if line.count("The 6 columns")>0: |
---|
128 | is_data_started = True |
---|
129 | |
---|
130 | # Sanity check |
---|
131 | if not len(y) == len(dy): |
---|
132 | raise ValueError, "abs_reader: y and dy have different length" |
---|
133 | |
---|
134 | output.x = x |
---|
135 | output.y = y |
---|
136 | output.dy = dy |
---|
137 | output.xaxis("\\rm{Q}", 'A^{-1}') |
---|
138 | output.yaxis("\\rm{I(Q)} ","cm^{-1}") |
---|
139 | |
---|
140 | return output |
---|
141 | else: |
---|
142 | raise RuntimeError, "%s is not a file" % path |
---|
143 | return None |
---|
144 | |
---|
145 | if __name__ == "__main__": |
---|
146 | reader = Reader() |
---|
147 | print reader.read("../test/jan08002.ABS") |
---|
148 | |
---|
149 | |
---|
150 | |
---|