[5e326a6] | 1 | """ |
---|
[edfc8ac] | 2 | SESANS reader (based on ASCII reader) |
---|
[ecc8d1a8] | 3 | |
---|
[edfc8ac] | 4 | Reader for .ses or .sesans file format |
---|
[ecc8d1a8] | 5 | |
---|
| 6 | Jurrian Bakker |
---|
[5e326a6] | 7 | """ |
---|
[e935ddb1] | 8 | import numpy as np |
---|
[5e326a6] | 9 | import os |
---|
[b5db35d] | 10 | from sas.sascalc.dataloader.data_info import Data1D |
---|
[5e326a6] | 11 | |
---|
| 12 | # Check whether we have a converter available |
---|
| 13 | has_converter = True |
---|
| 14 | try: |
---|
[b699768] | 15 | from sas.sascalc.data_util.nxsunit import Converter |
---|
[5e326a6] | 16 | except: |
---|
| 17 | has_converter = False |
---|
| 18 | _ZERO = 1e-16 |
---|
| 19 | |
---|
[def97a0] | 20 | |
---|
[5e326a6] | 21 | class Reader: |
---|
| 22 | """ |
---|
| 23 | Class to load sesans files (6 columns). |
---|
| 24 | """ |
---|
[def97a0] | 25 | # File type |
---|
[5e326a6] | 26 | type_name = "SESANS" |
---|
[ecc8d1a8] | 27 | |
---|
[def97a0] | 28 | # Wildcards |
---|
[5e326a6] | 29 | type = ["SESANS files (*.ses)|*.ses", |
---|
| 30 | "SESANS files (*..sesans)|*.sesans"] |
---|
[def97a0] | 31 | # List of allowed extensions |
---|
[5e326a6] | 32 | ext = ['.ses', '.SES', '.sesans', '.SESANS'] |
---|
[ecc8d1a8] | 33 | |
---|
[def97a0] | 34 | # Flag to bypass extension check |
---|
[5e326a6] | 35 | allow_all = True |
---|
[ecc8d1a8] | 36 | |
---|
[5e326a6] | 37 | def read(self, path): |
---|
| 38 | """ |
---|
| 39 | Load data file |
---|
[ecc8d1a8] | 40 | |
---|
[5e326a6] | 41 | :param path: file path |
---|
[ecc8d1a8] | 42 | |
---|
[5e326a6] | 43 | :return: SESANSData1D object, or None |
---|
[ecc8d1a8] | 44 | |
---|
[5e326a6] | 45 | :raise RuntimeError: when the file can't be opened |
---|
| 46 | :raise ValueError: when the length of the data vectors are inconsistent |
---|
| 47 | """ |
---|
| 48 | if os.path.isfile(path): |
---|
| 49 | basename = os.path.basename(path) |
---|
| 50 | _, extension = os.path.splitext(basename) |
---|
| 51 | if self.allow_all or extension.lower() in self.ext: |
---|
[e935ddb1] | 52 | with open(path, 'r') as input_f: |
---|
[5e326a6] | 53 | # Read in binary mode since GRASP frequently has no-ascii |
---|
| 54 | # characters that brakes the open operation |
---|
[e935ddb1] | 55 | line = input_f.readline() |
---|
| 56 | params = {} |
---|
| 57 | while line.strip() != "": |
---|
| 58 | if line.strip() == "": |
---|
| 59 | break |
---|
| 60 | terms = line.strip().split("\t") |
---|
| 61 | params[terms[0].strip()] = " ".join(terms[1:]).strip() |
---|
| 62 | line = input_f.readline() |
---|
| 63 | headers_temp = input_f.readline().strip().split("\t") |
---|
| 64 | headers = {} |
---|
| 65 | for h in headers_temp: |
---|
| 66 | temp = h.strip().split() |
---|
| 67 | headers[h[:-1].strip()] = temp[-1][1:-1] |
---|
| 68 | data = np.loadtxt(input_f) |
---|
| 69 | x = data[:, 0] |
---|
| 70 | dx = data[:, 3] |
---|
| 71 | lam = data[:, 4] |
---|
| 72 | dlam = data[:, 5] |
---|
| 73 | y = data[:, 1] |
---|
| 74 | dy = data[:, 2] |
---|
[26d4864] | 75 | |
---|
[e935ddb1] | 76 | lam_unit = _header_fetch(headers, "wavelength") |
---|
| 77 | if lam_unit == "AA": |
---|
| 78 | lam_unit = "A" |
---|
[def97a0] | 79 | |
---|
| 80 | x, x_unit = self._unit_conversion( |
---|
| 81 | x, lam_unit, |
---|
| 82 | _fetch_unit(headers, "spin echo length")) |
---|
| 83 | dx, dx_unit = self._unit_conversion( |
---|
| 84 | dx, lam_unit, |
---|
| 85 | _fetch_unit(headers, "error SEL")) |
---|
| 86 | dlam, dlam_unit = self._unit_conversion( |
---|
| 87 | dlam, lam_unit, |
---|
| 88 | _fetch_unit(headers, "error wavelength")) |
---|
[e935ddb1] | 89 | y_unit = r'\AA^{-2} cm^{-1}' |
---|
[26d4864] | 90 | |
---|
[def97a0] | 91 | output = Data1D(x=x, y=y, lam=lam, dy=dy, dx=dx, dlam=dlam, |
---|
| 92 | isSesans=True) |
---|
[e935ddb1] | 93 | self.filename = output.filename = basename |
---|
| 94 | output.xaxis(r"\rm{z}", x_unit) |
---|
[def97a0] | 95 | # Adjust label to ln P/(lam^2 t), remove lam column refs |
---|
| 96 | output.yaxis(r"\rm{ln(P)/(t \lambda^2)}", y_unit) |
---|
[e935ddb1] | 97 | # Store loading process information |
---|
| 98 | output.meta_data['loader'] = self.type_name |
---|
| 99 | output.sample.name = params["Sample"] |
---|
| 100 | output.sample.ID = params["DataFileTitle"] |
---|
[26d4864] | 101 | |
---|
[def97a0] | 102 | output.sample.zacceptance = ( |
---|
| 103 | float(_header_fetch(params, "Q_zmax")), |
---|
| 104 | _fetch_unit(params, "Q_zmax")) |
---|
[5e326a6] | 105 | |
---|
[def97a0] | 106 | output.sample.yacceptance = ( |
---|
| 107 | float(_header_fetch(params, "Q_ymax")), |
---|
| 108 | _fetch_unit(params, "Q_ymax")) |
---|
[5e326a6] | 109 | |
---|
| 110 | if len(output.x) < 1: |
---|
[def97a0] | 111 | raise RuntimeError("%s is empty" % path) |
---|
[5e326a6] | 112 | return output |
---|
[26d4864] | 113 | |
---|
[5e326a6] | 114 | else: |
---|
[def97a0] | 115 | raise RuntimeError("%s is not a file" % path) |
---|
[5e326a6] | 116 | return None |
---|
[26d4864] | 117 | |
---|
| 118 | def _unit_conversion(self, value, value_unit, default_unit): |
---|
[def97a0] | 119 | if has_converter and value_unit != default_unit: |
---|
[26d4864] | 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 |
---|
[e935ddb1] | 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 | |
---|
[def97a0] | 133 | |
---|
[e935ddb1] | 134 | def _fetch_unit(params, key): |
---|
| 135 | index = [k for k in params.keys() |
---|
| 136 | if k.startswith(key)][0] |
---|
| 137 | unit = index.strip().split()[-1][1:-1] |
---|
| 138 | if unit.startswith(r"\A"): |
---|
| 139 | unit = "1/A" |
---|
| 140 | return unit |
---|