[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 | |
---|
| 20 | class Reader: |
---|
| 21 | """ |
---|
| 22 | Class to load sesans files (6 columns). |
---|
| 23 | """ |
---|
| 24 | ## File type |
---|
| 25 | type_name = "SESANS" |
---|
[ecc8d1a8] | 26 | |
---|
[5e326a6] | 27 | ## Wildcards |
---|
| 28 | type = ["SESANS files (*.ses)|*.ses", |
---|
| 29 | "SESANS files (*..sesans)|*.sesans"] |
---|
| 30 | ## List of allowed extensions |
---|
| 31 | ext = ['.ses', '.SES', '.sesans', '.SESANS'] |
---|
[ecc8d1a8] | 32 | |
---|
[5e326a6] | 33 | ## Flag to bypass extension check |
---|
| 34 | allow_all = True |
---|
[ecc8d1a8] | 35 | |
---|
[5e326a6] | 36 | def read(self, path): |
---|
[ecc8d1a8] | 37 | |
---|
[5e326a6] | 38 | # print "reader triggered" |
---|
[ecc8d1a8] | 39 | |
---|
[5e326a6] | 40 | """ |
---|
| 41 | Load data file |
---|
[ecc8d1a8] | 42 | |
---|
[5e326a6] | 43 | :param path: file path |
---|
[ecc8d1a8] | 44 | |
---|
[5e326a6] | 45 | :return: SESANSData1D object, or None |
---|
[ecc8d1a8] | 46 | |
---|
[5e326a6] | 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: |
---|
[e935ddb1] | 54 | with open(path, 'r') as input_f: |
---|
[5e326a6] | 55 | # Read in binary mode since GRASP frequently has no-ascii |
---|
| 56 | # characters that brakes the open operation |
---|
[e935ddb1] | 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] |
---|
[26d4864] | 77 | |
---|
[e935ddb1] | 78 | lam_unit = _header_fetch(headers, "wavelength") |
---|
| 79 | if lam_unit == "AA": |
---|
| 80 | lam_unit = "A" |
---|
| 81 | p_unit = headers["polarisation"] |
---|
[a9f579c] | 82 | |
---|
[e935ddb1] | 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}' |
---|
[26d4864] | 93 | |
---|
[e935ddb1] | 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"] |
---|
[26d4864] | 103 | |
---|
[e935ddb1] | 104 | output.sample.zacceptance = (float(_header_fetch(params, "Q_zmax")), |
---|
| 105 | _fetch_unit(params, "Q_zmax")) |
---|
[5e326a6] | 106 | |
---|
[e935ddb1] | 107 | output.sample.yacceptance = (float(_header_fetch(params, "Q_ymax")), |
---|
| 108 | _fetch_unit(params, "Q_ymax")) |
---|
[5e326a6] | 109 | |
---|
| 110 | if len(output.x) < 1: |
---|
| 111 | raise RuntimeError, "%s is empty" % path |
---|
| 112 | return output |
---|
[26d4864] | 113 | |
---|
[5e326a6] | 114 | else: |
---|
| 115 | raise RuntimeError, "%s is not a file" % path |
---|
| 116 | return None |
---|
[26d4864] | 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 |
---|
[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 | |
---|
| 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 |
---|