[5e326a6] | 1 | """ |
---|
[edfc8ac] | 2 | SESANS reader (based on ASCII reader) |
---|
[b2c28a5] | 3 | |
---|
[edfc8ac] | 4 | Reader for .ses or .sesans file format |
---|
[b2c28a5] | 5 | |
---|
| 6 | Jurrian Bakker |
---|
[5e326a6] | 7 | """ |
---|
[9a5097c] | 8 | import numpy as np |
---|
[5e326a6] | 9 | import os |
---|
[b2c28a5] | 10 | from sas.sascalc.dataloader.file_reader_base_class import FileReader |
---|
| 11 | from sas.sascalc.dataloader.data_info import plottable_1D, DataInfo |
---|
| 12 | from sas.sascalc.dataloader.loader_exceptions import FileContentsException, DataReaderException |
---|
[5e326a6] | 13 | |
---|
| 14 | # Check whether we have a converter available |
---|
| 15 | has_converter = True |
---|
| 16 | try: |
---|
[b699768] | 17 | from sas.sascalc.data_util.nxsunit import Converter |
---|
[5e326a6] | 18 | except: |
---|
| 19 | has_converter = False |
---|
| 20 | _ZERO = 1e-16 |
---|
| 21 | |
---|
[b2c28a5] | 22 | class Reader(FileReader): |
---|
[5e326a6] | 23 | """ |
---|
| 24 | Class to load sesans files (6 columns). |
---|
| 25 | """ |
---|
[def97a0] | 26 | # File type |
---|
[5e326a6] | 27 | type_name = "SESANS" |
---|
[b2c28a5] | 28 | |
---|
[5e326a6] | 29 | ## Wildcards |
---|
| 30 | type = ["SESANS files (*.ses)|*.ses", |
---|
| 31 | "SESANS files (*..sesans)|*.sesans"] |
---|
[def97a0] | 32 | # List of allowed extensions |
---|
[5e326a6] | 33 | ext = ['.ses', '.SES', '.sesans', '.SESANS'] |
---|
[b2c28a5] | 34 | |
---|
[def97a0] | 35 | # Flag to bypass extension check |
---|
[5e326a6] | 36 | allow_all = True |
---|
[26d4864] | 37 | |
---|
[b2c28a5] | 38 | def get_file_contents(self): |
---|
| 39 | self.current_datainfo = DataInfo() |
---|
| 40 | self.current_dataset = plottable_1D(np.array([]), np.array([])) |
---|
| 41 | self.current_datainfo.isSesans = True |
---|
| 42 | self.output = [] |
---|
| 43 | |
---|
[5a8cdbb] | 44 | line = self.f_open.readline() |
---|
| 45 | params = {} |
---|
| 46 | while not line.startswith("BEGIN_DATA"): |
---|
| 47 | terms = line.split() |
---|
| 48 | if len(terms) >= 2: |
---|
| 49 | params[terms[0]] = " ".join(terms[1:]) |
---|
| 50 | line = self.f_open.readline() |
---|
| 51 | self.params = params |
---|
| 52 | |
---|
| 53 | if "FileFormatVersion" not in self.params: |
---|
| 54 | raise FileContentsException("SES file missing FileFormatVersion") |
---|
| 55 | if float(self.params["FileFormatVersion"]) >= 2.0: |
---|
| 56 | raise FileContentsException("SASView only supports SES version 1") |
---|
| 57 | |
---|
| 58 | if "SpinEchoLength_unit" not in self.params: |
---|
| 59 | raise FileContentsException("SpinEchoLength has no units") |
---|
| 60 | if "Wavelength_unit" not in self.params: |
---|
| 61 | raise FileContentsException("Wavelength has no units") |
---|
| 62 | if params["SpinEchoLength_unit"] != params["Wavelength_unit"]: |
---|
| 63 | raise FileContentsException("The spin echo data has rudely used " |
---|
| 64 | "different units for the spin echo length " |
---|
| 65 | "and the wavelength. While sasview could " |
---|
| 66 | "handle this instance, it is a violation " |
---|
| 67 | "of the file format and will not be " |
---|
| 68 | "handled by other software.") |
---|
| 69 | |
---|
| 70 | headers = self.f_open.readline().split() |
---|
| 71 | |
---|
| 72 | self._insist_header(headers, "SpinEchoLength") |
---|
| 73 | self._insist_header(headers, "Depolarisation") |
---|
| 74 | self._insist_header(headers, "Depolarisation_error") |
---|
| 75 | self._insist_header(headers, "Wavelength") |
---|
| 76 | |
---|
[be43448] | 77 | data = np.loadtxt(self.f_open) |
---|
| 78 | |
---|
| 79 | if data.shape[1] != len(headers): |
---|
| 80 | raise FileContentsException( |
---|
| 81 | "File has {} headers, but {} columns".format( |
---|
| 82 | len(headers), |
---|
| 83 | data.shape[1])) |
---|
| 84 | |
---|
| 85 | if not data.size: |
---|
| 86 | raise FileContentsException("{} is empty".format(path)) |
---|
| 87 | x = data[:, headers.index("SpinEchoLength")] |
---|
| 88 | if "SpinEchoLength_error" in headers: |
---|
| 89 | dx = data[:, headers.index("SpinEchoLength_error")] |
---|
| 90 | else: |
---|
| 91 | dx = x * 0.05 |
---|
| 92 | lam = data[:, headers.index("Wavelength")] |
---|
| 93 | if "Wavelength_error" in headers: |
---|
| 94 | dlam = data[:, headers.index("Wavelength_error")] |
---|
| 95 | else: |
---|
| 96 | dlam = lam * 0.05 |
---|
| 97 | y = data[:, headers.index("Depolarisation")] |
---|
| 98 | dy = data[:, headers.index("Depolarisation_error")] |
---|
| 99 | |
---|
| 100 | lam_unit = self._unit_fetch("Wavelength") |
---|
| 101 | x, x_unit = self._unit_conversion(x, "A", |
---|
| 102 | self._unit_fetch( |
---|
| 103 | "SpinEchoLength")) |
---|
| 104 | dx, dx_unit = self._unit_conversion( |
---|
| 105 | dx, lam_unit, |
---|
| 106 | self._unit_fetch("SpinEchoLength")) |
---|
| 107 | dlam, dlam_unit = self._unit_conversion( |
---|
| 108 | dlam, lam_unit, |
---|
| 109 | self._unit_fetch("Wavelength")) |
---|
| 110 | y_unit = self._unit_fetch("Depolarisation") |
---|
| 111 | |
---|
| 112 | self.current_dataset.x = x |
---|
| 113 | self.current_dataset.y = y |
---|
| 114 | self.current_dataset.lam = lam |
---|
| 115 | self.current_dataset.dy = dy |
---|
| 116 | self.current_dataset.dx = dx |
---|
| 117 | self.current_dataset.dlam = dlam |
---|
| 118 | self.current_datainfo.isSesans = True |
---|
| 119 | |
---|
| 120 | self.current_datainfo._yunit = y_unit |
---|
| 121 | self.current_datainfo._xunit = x_unit |
---|
| 122 | self.current_datainfo.source.wavelength_unit = lam_unit |
---|
| 123 | self.current_datainfo.source.wavelength = lam |
---|
| 124 | self.current_datainfo.filename = os.path.basename(self.f_open.name) |
---|
| 125 | self.current_dataset.xaxis(r"\rm{z}", x_unit) |
---|
| 126 | # Adjust label to ln P/(lam^2 t), remove lam column refs |
---|
| 127 | self.current_dataset.yaxis(r"\rm{ln(P)/(t \lambda^2)}", y_unit) |
---|
| 128 | # Store loading process information |
---|
| 129 | self.current_datainfo.meta_data['loader'] = self.type_name |
---|
| 130 | self.current_datainfo.sample.name = params["Sample"] |
---|
| 131 | self.current_datainfo.sample.ID = params["DataFileTitle"] |
---|
| 132 | self.current_datainfo.sample.thickness = self._unit_conversion( |
---|
| 133 | float(params["Thickness"]), "cm", |
---|
| 134 | self._unit_fetch("Thickness"))[0] |
---|
| 135 | |
---|
| 136 | self.current_datainfo.sample.zacceptance = ( |
---|
| 137 | float(params["Theta_zmax"]), |
---|
| 138 | self._unit_fetch("Theta_zmax")) |
---|
| 139 | |
---|
| 140 | self.current_datainfo.sample.yacceptance = ( |
---|
| 141 | float(params["Theta_ymax"]), |
---|
| 142 | self._unit_fetch("Theta_ymax")) |
---|
| 143 | |
---|
| 144 | self.send_to_output() |
---|
[26d4864] | 145 | |
---|
[2d866370] | 146 | @staticmethod |
---|
[e801a4e] | 147 | def _insist_header(headers, name): |
---|
| 148 | if name not in headers: |
---|
[5a8cdbb] | 149 | raise FileContentsException( |
---|
[e801a4e] | 150 | "Missing {} column in spin echo data".format(name)) |
---|
| 151 | |
---|
| 152 | @staticmethod |
---|
[2d866370] | 153 | def _unit_conversion(value, value_unit, default_unit): |
---|
[09a0be5] | 154 | """ |
---|
| 155 | Performs unit conversion on a measurement. |
---|
| 156 | |
---|
| 157 | :param value: The magnitude of the measurement |
---|
| 158 | :param value_unit: a string containing the final desired unit |
---|
[f6c2555] | 159 | :param default_unit: string with the units of the original measurement |
---|
[09a0be5] | 160 | :return: The magnitude of the measurement in the new units |
---|
| 161 | """ |
---|
[bc6532e] | 162 | # (float, string, string) -> float |
---|
[def97a0] | 163 | if has_converter and value_unit != default_unit: |
---|
[857cc58] | 164 | data_conv_q = Converter(default_unit) |
---|
| 165 | value = data_conv_q(value, units=value_unit) |
---|
[26d4864] | 166 | new_unit = default_unit |
---|
| 167 | else: |
---|
| 168 | new_unit = value_unit |
---|
[b2c28a5] | 169 | return value, new_unit |
---|
[e935ddb1] | 170 | |
---|
[2b310602] | 171 | def _unit_fetch(self, unit): |
---|
| 172 | return self.params[unit+"_unit"] |
---|