[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 | """ |
---|
[0ac6e11] | 8 | import logging |
---|
[e935ddb1] | 9 | import numpy as np |
---|
[5e326a6] | 10 | import os |
---|
[b5db35d] | 11 | from sas.sascalc.dataloader.data_info import Data1D |
---|
[5e326a6] | 12 | |
---|
| 13 | # Check whether we have a converter available |
---|
| 14 | has_converter = True |
---|
| 15 | try: |
---|
[b699768] | 16 | from sas.sascalc.data_util.nxsunit import Converter |
---|
[5e326a6] | 17 | except: |
---|
| 18 | has_converter = False |
---|
| 19 | _ZERO = 1e-16 |
---|
| 20 | |
---|
[def97a0] | 21 | |
---|
[5e326a6] | 22 | class Reader: |
---|
| 23 | """ |
---|
| 24 | Class to load sesans files (6 columns). |
---|
| 25 | """ |
---|
[def97a0] | 26 | # File type |
---|
[5e326a6] | 27 | type_name = "SESANS" |
---|
[ecc8d1a8] | 28 | |
---|
[def97a0] | 29 | # Wildcards |
---|
[5e326a6] | 30 | type = ["SESANS files (*.ses)|*.ses", |
---|
| 31 | "SESANS files (*..sesans)|*.sesans"] |
---|
[def97a0] | 32 | # List of allowed extensions |
---|
[5e326a6] | 33 | ext = ['.ses', '.SES', '.sesans', '.SESANS'] |
---|
[ecc8d1a8] | 34 | |
---|
[def97a0] | 35 | # Flag to bypass extension check |
---|
[5e326a6] | 36 | allow_all = True |
---|
[ecc8d1a8] | 37 | |
---|
[5e326a6] | 38 | def read(self, path): |
---|
| 39 | """ |
---|
| 40 | Load data file |
---|
[ecc8d1a8] | 41 | |
---|
[5e326a6] | 42 | :param path: file path |
---|
[ecc8d1a8] | 43 | |
---|
[5e326a6] | 44 | :return: SESANSData1D object, or None |
---|
[ecc8d1a8] | 45 | |
---|
[5e326a6] | 46 | :raise RuntimeError: when the file can't be opened |
---|
| 47 | :raise ValueError: when the length of the data vectors are inconsistent |
---|
| 48 | """ |
---|
| 49 | if os.path.isfile(path): |
---|
| 50 | basename = os.path.basename(path) |
---|
| 51 | _, extension = os.path.splitext(basename) |
---|
[cb9feea8] | 52 | if not (self.allow_all or extension.lower() in self.ext): |
---|
| 53 | raise RuntimeError("{} has an unrecognized file extension".format(path)) |
---|
[5e326a6] | 54 | else: |
---|
[cb9feea8] | 55 | raise RunetimeError("{} is not a file".format(path)) |
---|
| 56 | with open(path, 'r') as input_f: |
---|
| 57 | # Read in binary mode since GRASP frequently has no-ascii |
---|
| 58 | # characters that brakes the open operation |
---|
| 59 | line = input_f.readline() |
---|
| 60 | params = {} |
---|
[2b310602] | 61 | while not line.startswith("BEGIN_DATA"): |
---|
| 62 | terms = line.split() |
---|
| 63 | if len(terms) >= 2: |
---|
| 64 | params[terms[0]] = " ".join(terms[1:]) |
---|
[cb9feea8] | 65 | line = input_f.readline() |
---|
[2b310602] | 66 | self.params = params |
---|
| 67 | headers = input_f.readline().split() |
---|
| 68 | |
---|
[cb9feea8] | 69 | data = np.loadtxt(input_f) |
---|
| 70 | if data.size < 1: |
---|
| 71 | raise RuntimeError("{} is empty".format(path)) |
---|
[2b310602] | 72 | x = data[:, headers.index("SpinEchoLength")] |
---|
[8e0dcac] | 73 | if "SpinEchoLength_error" in headers: |
---|
| 74 | dx = data[:, headers.index("SpinEchoLength_error")] |
---|
| 75 | else: |
---|
| 76 | dx = x*0.05 |
---|
[2b310602] | 77 | lam = data[:, headers.index("Wavelength")] |
---|
[8e0dcac] | 78 | if "Wavelength_error" in headers: |
---|
| 79 | dlam = data[:, headers.index("Wavelength_error")] |
---|
| 80 | else: |
---|
| 81 | dlam = lam*0.05 |
---|
[2b310602] | 82 | y = data[:, headers.index("Depolarisation")] |
---|
| 83 | dy = data[:, headers.index("Depolarisation_error")] |
---|
| 84 | |
---|
| 85 | lam_unit = self._unit_fetch("Wavelength") |
---|
| 86 | x, x_unit = self._unit_conversion(x, "A", self._unit_fetch("SpinEchoLength")) |
---|
[cb9feea8] | 87 | dx, dx_unit = self._unit_conversion( |
---|
| 88 | dx, lam_unit, |
---|
[2b310602] | 89 | self._unit_fetch("SpinEchoLength")) |
---|
[cb9feea8] | 90 | dlam, dlam_unit = self._unit_conversion( |
---|
| 91 | dlam, lam_unit, |
---|
[2b310602] | 92 | self._unit_fetch("Wavelength")) |
---|
| 93 | y_unit = self._unit_fetch("Depolarisation") |
---|
[cb9feea8] | 94 | |
---|
| 95 | output = Data1D(x=x, y=y, lam=lam, dy=dy, dx=dx, dlam=dlam, |
---|
| 96 | isSesans=True) |
---|
[2b310602] | 97 | |
---|
| 98 | output.y_unit = y_unit |
---|
| 99 | output.x_unit = x_unit |
---|
[0ac6e11] | 100 | output.source.wavelength_unit = lam_unit |
---|
| 101 | output.source.wavelength = lam |
---|
[cb9feea8] | 102 | self.filename = output.filename = basename |
---|
| 103 | output.xaxis(r"\rm{z}", x_unit) |
---|
| 104 | # Adjust label to ln P/(lam^2 t), remove lam column refs |
---|
| 105 | output.yaxis(r"\rm{ln(P)/(t \lambda^2)}", y_unit) |
---|
| 106 | # Store loading process information |
---|
| 107 | output.meta_data['loader'] = self.type_name |
---|
| 108 | output.sample.name = params["Sample"] |
---|
| 109 | output.sample.ID = params["DataFileTitle"] |
---|
[857cc58] | 110 | output.sample.thickness = self._unit_conversion( |
---|
| 111 | float(params["Thickness"]), "cm", |
---|
| 112 | self._unit_fetch("Thickness"))[0] |
---|
[cb9feea8] | 113 | |
---|
| 114 | output.sample.zacceptance = ( |
---|
[2b310602] | 115 | float(params["Theta_zmax"]), |
---|
| 116 | self._unit_fetch("Theta_zmax")) |
---|
[cb9feea8] | 117 | |
---|
| 118 | output.sample.yacceptance = ( |
---|
[2b310602] | 119 | float(params["Theta_ymax"]), |
---|
| 120 | self._unit_fetch("Theta_ymax")) |
---|
[cb9feea8] | 121 | return output |
---|
[26d4864] | 122 | |
---|
[2d866370] | 123 | @staticmethod |
---|
| 124 | def _unit_conversion(value, value_unit, default_unit): |
---|
[09a0be5] | 125 | """ |
---|
| 126 | Performs unit conversion on a measurement. |
---|
| 127 | |
---|
| 128 | :param value: The magnitude of the measurement |
---|
| 129 | :param value_unit: a string containing the final desired unit |
---|
| 130 | :param default_unit: a string containing the units of the original measurement |
---|
| 131 | :return: The magnitude of the measurement in the new units |
---|
| 132 | """ |
---|
[bc6532e] | 133 | # (float, string, string) -> float |
---|
[def97a0] | 134 | if has_converter and value_unit != default_unit: |
---|
[857cc58] | 135 | data_conv_q = Converter(default_unit) |
---|
| 136 | value = data_conv_q(value, units=value_unit) |
---|
[26d4864] | 137 | new_unit = default_unit |
---|
| 138 | else: |
---|
| 139 | new_unit = value_unit |
---|
[e935ddb1] | 140 | return value, new_unit |
---|
| 141 | |
---|
[2b310602] | 142 | def _unit_fetch(self, unit): |
---|
| 143 | return self.params[unit+"_unit"] |
---|