[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) |
---|
[cb9feea8] | 51 | if not (self.allow_all or extension.lower() in self.ext): |
---|
| 52 | raise RuntimeError("{} has an unrecognized file extension".format(path)) |
---|
[5e326a6] | 53 | else: |
---|
[cb9feea8] | 54 | raise RunetimeError("{} is not a file".format(path)) |
---|
| 55 | with open(path, 'r') as input_f: |
---|
| 56 | # Read in binary mode since GRASP frequently has no-ascii |
---|
| 57 | # characters that brakes the open operation |
---|
| 58 | line = input_f.readline() |
---|
| 59 | params = {} |
---|
[2b310602] | 60 | while not line.startswith("BEGIN_DATA"): |
---|
| 61 | terms = line.split() |
---|
| 62 | if len(terms) >= 2: |
---|
| 63 | params[terms[0]] = " ".join(terms[1:]) |
---|
[cb9feea8] | 64 | line = input_f.readline() |
---|
[2b310602] | 65 | self.params = params |
---|
| 66 | headers = input_f.readline().split() |
---|
| 67 | |
---|
[cb9feea8] | 68 | data = np.loadtxt(input_f) |
---|
| 69 | if data.size < 1: |
---|
| 70 | raise RuntimeError("{} is empty".format(path)) |
---|
[2b310602] | 71 | x = data[:, headers.index("SpinEchoLength")] |
---|
| 72 | dx = data[:, headers.index("SpinEchoLength_error")] |
---|
| 73 | lam = data[:, headers.index("Wavelength")] |
---|
| 74 | dlam = data[:, headers.index("Wavelength_error")] |
---|
| 75 | y = data[:, headers.index("Depolarisation")] |
---|
| 76 | dy = data[:, headers.index("Depolarisation_error")] |
---|
| 77 | |
---|
| 78 | lam_unit = self._unit_fetch("Wavelength") |
---|
| 79 | x, x_unit = self._unit_conversion(x, "A", self._unit_fetch("SpinEchoLength")) |
---|
[cb9feea8] | 80 | dx, dx_unit = self._unit_conversion( |
---|
| 81 | dx, lam_unit, |
---|
[2b310602] | 82 | self._unit_fetch("SpinEchoLength")) |
---|
[cb9feea8] | 83 | dlam, dlam_unit = self._unit_conversion( |
---|
| 84 | dlam, lam_unit, |
---|
[2b310602] | 85 | self._unit_fetch("Wavelength")) |
---|
| 86 | y_unit = self._unit_fetch("Depolarisation") |
---|
[cb9feea8] | 87 | |
---|
| 88 | output = Data1D(x=x, y=y, lam=lam, dy=dy, dx=dx, dlam=dlam, |
---|
| 89 | isSesans=True) |
---|
[2b310602] | 90 | |
---|
| 91 | output.y_unit = y_unit |
---|
| 92 | output.x_unit = x_unit |
---|
[cb9feea8] | 93 | self.filename = output.filename = basename |
---|
| 94 | output.xaxis(r"\rm{z}", x_unit) |
---|
| 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) |
---|
| 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"] |
---|
[2b310602] | 101 | output.sample.thickness = float( |
---|
| 102 | self._unit_conversion( |
---|
| 103 | params["Thickness"], "cm", self._unit_fetch("Thickness"))[0]) |
---|
[cb9feea8] | 104 | |
---|
| 105 | output.sample.zacceptance = ( |
---|
[2b310602] | 106 | float(params["Theta_zmax"]), |
---|
| 107 | self._unit_fetch("Theta_zmax")) |
---|
[cb9feea8] | 108 | |
---|
| 109 | output.sample.yacceptance = ( |
---|
[2b310602] | 110 | float(params["Theta_ymax"]), |
---|
| 111 | self._unit_fetch("Theta_ymax")) |
---|
[cb9feea8] | 112 | return output |
---|
[26d4864] | 113 | |
---|
[2d866370] | 114 | @staticmethod |
---|
| 115 | def _unit_conversion(value, value_unit, default_unit): |
---|
[09a0be5] | 116 | """ |
---|
| 117 | Performs unit conversion on a measurement. |
---|
| 118 | |
---|
| 119 | :param value: The magnitude of the measurement |
---|
| 120 | :param value_unit: a string containing the final desired unit |
---|
| 121 | :param default_unit: a string containing the units of the original measurement |
---|
| 122 | :return: The magnitude of the measurement in the new units |
---|
| 123 | """ |
---|
[bc6532e] | 124 | # (float, string, string) -> float |
---|
[def97a0] | 125 | if has_converter and value_unit != default_unit: |
---|
[26d4864] | 126 | data_conv_q = Converter(value_unit) |
---|
| 127 | value = data_conv_q(value, units=default_unit) |
---|
| 128 | new_unit = default_unit |
---|
| 129 | else: |
---|
| 130 | new_unit = value_unit |
---|
[e935ddb1] | 131 | return value, new_unit |
---|
| 132 | |
---|
[2b310602] | 133 | def _unit_fetch(self, unit): |
---|
| 134 | return self.params[unit+"_unit"] |
---|