1 | """ |
---|
2 | SESANS reader (based on ASCII reader) |
---|
3 | |
---|
4 | Reader for .ses or .sesans file format |
---|
5 | |
---|
6 | Jurrian Bakker |
---|
7 | """ |
---|
8 | import numpy |
---|
9 | import os |
---|
10 | from sas.dataloader.data_info import SESANSData1D |
---|
11 | |
---|
12 | # Check whether we have a converter available |
---|
13 | has_converter = True |
---|
14 | try: |
---|
15 | from sas.data_util.nxsunit import Converter |
---|
16 | except: |
---|
17 | has_converter = False |
---|
18 | _ZERO = 1e-16 |
---|
19 | |
---|
20 | class Reader: |
---|
21 | """ |
---|
22 | Class to load sesans files (6 columns). |
---|
23 | """ |
---|
24 | ## File type |
---|
25 | type_name = "SESANS" |
---|
26 | |
---|
27 | ## Wildcards |
---|
28 | type = ["SESANS files (*.ses)|*.ses", |
---|
29 | "SESANS files (*..sesans)|*.sesans"] |
---|
30 | ## List of allowed extensions |
---|
31 | ext = ['.ses', '.SES', '.sesans', '.SESANS'] |
---|
32 | |
---|
33 | ## Flag to bypass extension check |
---|
34 | allow_all = True |
---|
35 | |
---|
36 | def read(self, path): |
---|
37 | |
---|
38 | # print "reader triggered" |
---|
39 | |
---|
40 | """ |
---|
41 | Load data file |
---|
42 | |
---|
43 | :param path: file path |
---|
44 | |
---|
45 | :return: SESANSData1D object, or None |
---|
46 | |
---|
47 | :raise RuntimeError: when the file can't be opened |
---|
48 | :raise ValueError: when the length of the data vectors are inconsistent |
---|
49 | """ |
---|
50 | if os.path.isfile(path): |
---|
51 | basename = os.path.basename(path) |
---|
52 | _, extension = os.path.splitext(basename) |
---|
53 | if self.allow_all or extension.lower() in self.ext: |
---|
54 | try: |
---|
55 | # Read in binary mode since GRASP frequently has no-ascii |
---|
56 | # characters that brakes the open operation |
---|
57 | input_f = open(path,'rb') |
---|
58 | except: |
---|
59 | raise RuntimeError, "sesans_reader: cannot open %s" % path |
---|
60 | buff = input_f.read() |
---|
61 | # print buff |
---|
62 | lines = buff.splitlines() |
---|
63 | # print lines |
---|
64 | #Jae could not find python universal line spliter: |
---|
65 | #keep the below for now |
---|
66 | # some ascii data has \r line separator, |
---|
67 | # try it when the data is on only one long line |
---|
68 | # if len(lines) < 2 : |
---|
69 | # lines = buff.split('\r') |
---|
70 | |
---|
71 | x = numpy.zeros(0) |
---|
72 | y = numpy.zeros(0) |
---|
73 | dy = numpy.zeros(0) |
---|
74 | lam = numpy.zeros(0) |
---|
75 | dlam = numpy.zeros(0) |
---|
76 | dx = numpy.zeros(0) |
---|
77 | |
---|
78 | #temp. space to sort data |
---|
79 | tx = numpy.zeros(0) |
---|
80 | ty = numpy.zeros(0) |
---|
81 | tdy = numpy.zeros(0) |
---|
82 | tlam = numpy.zeros(0) |
---|
83 | tdlam = numpy.zeros(0) |
---|
84 | tdx = numpy.zeros(0) |
---|
85 | # print "all good" |
---|
86 | output = SESANSData1D(x=x, y=y, lam=lam, dy=dy, dx=dx, dlam=dlam) |
---|
87 | # print output |
---|
88 | self.filename = output.filename = basename |
---|
89 | |
---|
90 | |
---|
91 | data_conv_z = None |
---|
92 | data_conv_P = None |
---|
93 | # print "passing" |
---|
94 | if has_converter == True and output.x_unit != 'A': |
---|
95 | data_conv_z = Converter('nm') |
---|
96 | # Test it |
---|
97 | data_conv_z(1.0, output.x_unit) |
---|
98 | # print data_conv_z |
---|
99 | # print data_conv_z(1.0, output.x_unit) |
---|
100 | if has_converter == True and output.y_unit != ' ': |
---|
101 | data_conv_P = Converter('a.u.') |
---|
102 | # Test it |
---|
103 | data_conv_P(1.0, output.y_unit) |
---|
104 | # print data_conv_P |
---|
105 | # print data_conv_P(1.0, output.y_unit) |
---|
106 | # The first good line of data will define whether |
---|
107 | # we have 2-column or 3-column ascii |
---|
108 | # print output.x_unit |
---|
109 | # print output.y_unit |
---|
110 | |
---|
111 | # print "past output" |
---|
112 | |
---|
113 | # has_error_dx = None |
---|
114 | # has_error_dy = None |
---|
115 | |
---|
116 | # #Initialize counters for data lines and header lines. |
---|
117 | # is_data = False # Has more than 5 lines |
---|
118 | # # More than "5" lines of data is considered as actual |
---|
119 | # # data unless that is the only data |
---|
120 | # mum_data_lines = 5 |
---|
121 | # # To count # of current data candidate lines |
---|
122 | # i = -1 |
---|
123 | # # To count total # of previous data candidate lines |
---|
124 | # i1 = -1 |
---|
125 | # # To count # of header lines |
---|
126 | # j = -1 |
---|
127 | # # Helps to count # of header lines |
---|
128 | # j1 = -1 |
---|
129 | # #minimum required number of columns of data; ( <= 4). |
---|
130 | # lentoks = 2 |
---|
131 | paramnames=[] |
---|
132 | paramvals=[] |
---|
133 | zvals=[] |
---|
134 | dzvals=[] |
---|
135 | lamvals=[] |
---|
136 | dlamvals=[] |
---|
137 | Pvals=[] |
---|
138 | dPvals=[] |
---|
139 | # print x |
---|
140 | # print zvals |
---|
141 | for line in lines: |
---|
142 | # Initial try for CSV (split on ,) |
---|
143 | # print line |
---|
144 | line=line.strip() |
---|
145 | # print line |
---|
146 | toks = line.split('\t') |
---|
147 | # print toks |
---|
148 | if len(toks)==2: |
---|
149 | paramnames.append(toks[0]) |
---|
150 | # print paramnames |
---|
151 | paramvals.append(toks[1]) |
---|
152 | # print paramvals |
---|
153 | if len(toks)>5: |
---|
154 | zvals.append(toks[0]) |
---|
155 | dzvals.append(toks[1]) |
---|
156 | lamvals.append(toks[2]) |
---|
157 | dlamvals.append(toks[3]) |
---|
158 | Pvals.append(toks[4]) |
---|
159 | dPvals.append(toks[5]) |
---|
160 | else: |
---|
161 | continue |
---|
162 | # print varheaders |
---|
163 | # print paramnames |
---|
164 | # print paramvals |
---|
165 | # print zvals |
---|
166 | # print len(zvals) |
---|
167 | |
---|
168 | x=[] |
---|
169 | y=[] |
---|
170 | lam=[] |
---|
171 | dx=[] |
---|
172 | dy=[] |
---|
173 | dlam=[] |
---|
174 | varheader=[zvals[0],dzvals[0],lamvals[0],dlamvals[0],Pvals[0],dPvals[0]] |
---|
175 | # print varheader |
---|
176 | valrange=range(len(zvals)-1) |
---|
177 | # print valrange |
---|
178 | for i in valrange: |
---|
179 | x.append(float(zvals[i+1])) |
---|
180 | y.append(float(Pvals[i+1])) |
---|
181 | lam.append(float(lamvals[i+1])) |
---|
182 | dy.append(float(dPvals[i+1])) |
---|
183 | dx.append(float(dzvals[i+1])) |
---|
184 | dlam.append(float(dlamvals[i+1])) |
---|
185 | |
---|
186 | x,y,lam,dy,dx,dlam = [ |
---|
187 | numpy.asarray(v, 'double') |
---|
188 | for v in (x,y,lam,dy,dx,dlam) |
---|
189 | ] |
---|
190 | |
---|
191 | # print x |
---|
192 | # print y |
---|
193 | # print dx |
---|
194 | # print dy |
---|
195 | # print len(x) |
---|
196 | # print len(y) |
---|
197 | # print len(dx) |
---|
198 | # print len(dy) |
---|
199 | |
---|
200 | |
---|
201 | input_f.close() |
---|
202 | # Sanity check |
---|
203 | # if has_error_dy == True and not len(y) == len(dy): |
---|
204 | # msg = "sesans_reader: y and dy have different length" |
---|
205 | # raise RuntimeError, msg |
---|
206 | # if has_error_dx == True and not len(x) == len(dx): |
---|
207 | # msg = "sesans_reader: y and dy have different length" |
---|
208 | # raise RuntimeError, msg |
---|
209 | # # If the data length is zero, consider this as |
---|
210 | # # though we were not able to read the file. |
---|
211 | # if len(x) == 0: |
---|
212 | # raise RuntimeError, "sesans_reader: could not load file" |
---|
213 | # print "alive" |
---|
214 | #Let's re-order the data to make cal. |
---|
215 | # curve look better some cases |
---|
216 | # ind = numpy.lexsort((ty, tx)) |
---|
217 | # for i in ind: |
---|
218 | # x[i] = tx[ind[i]] |
---|
219 | # y[i] = ty[ind[i]] |
---|
220 | # if has_error_dy == True: |
---|
221 | # dy[i] = tdy[ind[i]] |
---|
222 | # if has_error_dx == True: |
---|
223 | # dx[i] = tdx[ind[i]] |
---|
224 | # Zeros in dx, dy |
---|
225 | # if has_error_dx: |
---|
226 | # dx[dx == 0] = _ZERO |
---|
227 | # if has_error_dy: |
---|
228 | # dy[dy == 0] = _ZERO |
---|
229 | #Data |
---|
230 | output.x = x #[x != 0] |
---|
231 | # print output.x |
---|
232 | output.y = y #[x != 0] |
---|
233 | # print output.y |
---|
234 | # output.dy = dy[x != 0] if has_error_dy == True\ |
---|
235 | # else numpy.zeros(len(output.y)) |
---|
236 | # output.dx = dx[x != 0] if has_error_dx == True\ |
---|
237 | # else numpy.zeros(len(output.x)) |
---|
238 | output.dy = dy |
---|
239 | output.dx = dx |
---|
240 | output.lam = lam |
---|
241 | output.dlam = dlam |
---|
242 | |
---|
243 | |
---|
244 | # print "still alive" |
---|
245 | # if data_conv_z is not None: |
---|
246 | # output.xaxis("\\rm{z}", output.x_unit) |
---|
247 | # else: |
---|
248 | # output.xaxis("\\rm{z}", 'nm') |
---|
249 | # if data_conv_P is not None: |
---|
250 | # output.yaxis("\\rm{P/P0}", output.y_unit) |
---|
251 | # else: |
---|
252 | # output.yaxis("\\rm{P/P0}", "a.u.") |
---|
253 | output.xaxis("\\rm{z}", 'nm') |
---|
254 | output.yaxis("\\rm{P/P0}", " ") |
---|
255 | # Store loading process information |
---|
256 | output.meta_data['loader'] = self.type_name |
---|
257 | output.sample.thickness = float(paramvals[6]) |
---|
258 | output.sample.name = paramvals[1] |
---|
259 | output.sample.ID = paramvals[0] |
---|
260 | output.sample.zacceptance=float(paramvals[7]) |
---|
261 | output.vars=varheader |
---|
262 | |
---|
263 | # print "sesans_reader end" |
---|
264 | |
---|
265 | if len(output.x) < 1: |
---|
266 | raise RuntimeError, "%s is empty" % path |
---|
267 | # print output |
---|
268 | # print output.lam |
---|
269 | return output |
---|
270 | |
---|
271 | else: |
---|
272 | raise RuntimeError, "%s is not a file" % path |
---|
273 | return None |
---|