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 as np |
---|
9 | import os |
---|
10 | from sas.sascalc.dataloader.data_info import Data1D |
---|
11 | |
---|
12 | # Check whether we have a converter available |
---|
13 | has_converter = True |
---|
14 | try: |
---|
15 | from sas.sascalc.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 | with open(path, 'r') as input_f: |
---|
55 | # Read in binary mode since GRASP frequently has no-ascii |
---|
56 | # characters that brakes the open operation |
---|
57 | line = input_f.readline() |
---|
58 | params = {} |
---|
59 | while line.strip() != "": |
---|
60 | if line.strip() == "": |
---|
61 | break |
---|
62 | terms = line.strip().split("\t") |
---|
63 | params[terms[0].strip()] = " ".join(terms[1:]).strip() |
---|
64 | line = input_f.readline() |
---|
65 | headers_temp = input_f.readline().strip().split("\t") |
---|
66 | headers = {} |
---|
67 | for h in headers_temp: |
---|
68 | temp = h.strip().split() |
---|
69 | headers[h[:-1].strip()] = temp[-1][1:-1] |
---|
70 | data = np.loadtxt(input_f) |
---|
71 | x = data[:, 0] |
---|
72 | dx = data[:, 3] |
---|
73 | lam = data[:, 4] |
---|
74 | dlam = data[:, 5] |
---|
75 | y = data[:, 1] |
---|
76 | dy = data[:, 2] |
---|
77 | |
---|
78 | lam_unit = _header_fetch(headers, "wavelength") |
---|
79 | if lam_unit == "AA": |
---|
80 | lam_unit = "A" |
---|
81 | p_unit = headers["polarisation"] |
---|
82 | |
---|
83 | x, x_unit = self._unit_conversion(x, lam_unit, |
---|
84 | _fetch_unit(headers, |
---|
85 | "spin echo length")) |
---|
86 | dx, dx_unit = self._unit_conversion(dx, lam_unit, |
---|
87 | _fetch_unit(headers, |
---|
88 | "error SEL")) |
---|
89 | dlam, dlam_unit = self._unit_conversion(dlam, lam_unit, |
---|
90 | _fetch_unit(headers, |
---|
91 | "error wavelength")) |
---|
92 | y_unit = r'\AA^{-2} cm^{-1}' |
---|
93 | |
---|
94 | output = Data1D(x=x, y=y, lam=lam, dy = dy, dx=dx, dlam=dlam, isSesans=True) |
---|
95 | self.filename = output.filename = basename |
---|
96 | output.xaxis(r"\rm{z}", x_unit) |
---|
97 | output.yaxis(r"\rm{ln(P)/(t \lambda^2)}", y_unit) # Adjust label to ln P/(lam^2 t), remove lam column refs |
---|
98 | # Store loading process information |
---|
99 | output.meta_data['loader'] = self.type_name |
---|
100 | #output.sample.thickness = float(paramvals[6]) |
---|
101 | output.sample.name = params["Sample"] |
---|
102 | output.sample.ID = params["DataFileTitle"] |
---|
103 | |
---|
104 | output.sample.zacceptance = (float(_header_fetch(params, "Q_zmax")), |
---|
105 | _fetch_unit(params, "Q_zmax")) |
---|
106 | |
---|
107 | output.sample.yacceptance = (float(_header_fetch(params, "Q_ymax")), |
---|
108 | _fetch_unit(params, "Q_ymax")) |
---|
109 | |
---|
110 | if len(output.x) < 1: |
---|
111 | raise RuntimeError, "%s is empty" % path |
---|
112 | return output |
---|
113 | |
---|
114 | else: |
---|
115 | raise RuntimeError, "%s is not a file" % path |
---|
116 | return None |
---|
117 | |
---|
118 | def _unit_conversion(self, value, value_unit, default_unit): |
---|
119 | if has_converter == True and value_unit != default_unit: |
---|
120 | data_conv_q = Converter(value_unit) |
---|
121 | value = data_conv_q(value, units=default_unit) |
---|
122 | new_unit = default_unit |
---|
123 | else: |
---|
124 | new_unit = value_unit |
---|
125 | return value, new_unit |
---|
126 | |
---|
127 | |
---|
128 | def _header_fetch(headers, key): |
---|
129 | index = [k for k in headers.keys() |
---|
130 | if k.startswith(key)][0] |
---|
131 | return headers[index] |
---|
132 | |
---|
133 | def _fetch_unit(params, key): |
---|
134 | index = [k for k in params.keys() |
---|
135 | if k.startswith(key)][0] |
---|
136 | unit = index.strip().split()[-1][1:-1] |
---|
137 | if unit.startswith(r"\A"): |
---|
138 | unit = "1/A" |
---|
139 | return unit |
---|