|
| 1 | +""" |
| 2 | +Import SESANS data in SasData format |
| 3 | +""" |
| 4 | + |
| 5 | +from sasdata.data import SasData |
| 6 | +from sasdata.data_util.loader_exceptions import FileContentsException |
| 7 | +from sasdata.dataset_types import sesans |
| 8 | +from sasdata.quantities.quantity import Quantity |
| 9 | +from sasdata.metadata import ( |
| 10 | + Metadata, |
| 11 | + Sample, |
| 12 | + MetaNode, |
| 13 | + Process, |
| 14 | +) |
| 15 | +from sasdata.quantities import unit_parser, units |
| 16 | +from collections import defaultdict |
| 17 | +from itertools import groupby |
| 18 | +import re |
| 19 | +import numpy as np |
| 20 | + |
| 21 | + |
| 22 | +def parse_version(lines: list[str]) -> tuple[str, list[str]]: |
| 23 | + header = lines[0] |
| 24 | + m = re.search(r"FileFormatVersion\s+(\S+)", header) |
| 25 | + |
| 26 | + if m is None: |
| 27 | + raise FileContentsException( |
| 28 | + "Sesans file does not contain File Format Version header" |
| 29 | + ) |
| 30 | + |
| 31 | + return (m.group(0), lines[1:]) |
| 32 | + |
| 33 | + |
| 34 | +def parse_title(kvs: dict[str, str]) -> str: |
| 35 | + """Get the title from the key value store""" |
| 36 | + if "Title" in kvs: |
| 37 | + return kvs["Title"] |
| 38 | + elif "DataFileTitle" in kvs: |
| 39 | + return kvs["DataFileTitle"] |
| 40 | + for k, v in kvs.items(): |
| 41 | + if "Title" in k: |
| 42 | + return v |
| 43 | + return "" |
| 44 | + |
| 45 | + |
| 46 | +def parse_kvs_quantity(key: str, kvs: dict[str, str]) -> Quantity | None: |
| 47 | + if key not in kvs or key + "_unit" not in kvs: |
| 48 | + return None |
| 49 | + return Quantity(value=float(kvs[key]), units=unit_parser.parse(kvs[key + "_unit"])) |
| 50 | + |
| 51 | + |
| 52 | +def parse_sample(kvs: dict[str, str]) -> Sample: |
| 53 | + """Get the sample info from the key value store""" |
| 54 | + |
| 55 | + thickness = parse_kvs_quantity("Thickness", kvs) |
| 56 | + if thickness is None: |
| 57 | + raise FileContentsException( |
| 58 | + "SES format must include sample thickness to normalise calculations" |
| 59 | + ) |
| 60 | + |
| 61 | + return Sample( |
| 62 | + name=kvs.get("Sample"), |
| 63 | + sample_id=None, |
| 64 | + thickness=thickness, |
| 65 | + transmission=None, |
| 66 | + temperature=None, |
| 67 | + position=None, |
| 68 | + orientation=None, |
| 69 | + details=[], |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +def parse_process(kvs: dict[str, str]) -> Process: |
| 74 | + ymax = parse_kvs_quantity("Theta_ymax", kvs) |
| 75 | + zmax = parse_kvs_quantity("Theta_zmax", kvs) |
| 76 | + orientation = kvs.get("Orientation") |
| 77 | + |
| 78 | + if ymax is None: |
| 79 | + raise FileContentsException("SES file must specify Theta_ymax") |
| 80 | + if zmax is None: |
| 81 | + raise FileContentsException("SES file must specify Theta_zmax") |
| 82 | + if orientation is None: |
| 83 | + raise FileContentsException("SES file must include encoding orientation") |
| 84 | + |
| 85 | + terms: dict[str, str | Quantity] = { |
| 86 | + "ymax": ymax, |
| 87 | + "zmax": zmax, |
| 88 | + "orientation": orientation, |
| 89 | + } |
| 90 | + |
| 91 | + return Process( |
| 92 | + name="SESANS Processing", |
| 93 | + date=None, |
| 94 | + description="Polarisation measurement through a SESANS instrument", |
| 95 | + terms=terms, |
| 96 | + notes=[], |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +def parse_metanode(kvs: dict[str, str]) -> MetaNode: |
| 101 | + """Convert header into metanode""" |
| 102 | + contents: list[MetaNode] = [] |
| 103 | + title = parse_title(kvs) |
| 104 | + |
| 105 | + for k, v in kvs.items(): |
| 106 | + if v.endswith("_unit") and v[:-5] in kvs: |
| 107 | + # This is the unit for another term |
| 108 | + continue |
| 109 | + if v + "_unit" in kvs: |
| 110 | + contents.append( |
| 111 | + MetaNode( |
| 112 | + name=k, |
| 113 | + attrs={}, |
| 114 | + contents=Quantity( |
| 115 | + value=float(v), units=unit_parser.parse(kvs[k + "_unit"]) |
| 116 | + ), |
| 117 | + ) |
| 118 | + ) |
| 119 | + else: |
| 120 | + contents.append(MetaNode(name=k, attrs={}, contents=v)) |
| 121 | + |
| 122 | + return MetaNode(name=title, attrs={}, contents=contents) |
| 123 | + |
| 124 | + |
| 125 | +def parse_metadata(lines: list[str]) -> tuple[Metadata, dict[str, str], list[str]]: |
| 126 | + parts = [ |
| 127 | + [y for y in x] |
| 128 | + for (_, x) in groupby(lines, lambda x: x.startswith("BEGIN_DATA")) |
| 129 | + ] |
| 130 | + |
| 131 | + if len(parts) != 3: |
| 132 | + raise FileContentsException("SES file should have exactly one data section") |
| 133 | + |
| 134 | + # Parse key value store |
| 135 | + kvs: dict[str, str] = {} |
| 136 | + for line in parts[0]: |
| 137 | + m = re.search(r"(\S+)\s+(.+)\n", line) |
| 138 | + if not m: |
| 139 | + continue |
| 140 | + kvs[m.group(1)] = m.group(2) |
| 141 | + |
| 142 | + return ( |
| 143 | + Metadata( |
| 144 | + process=[parse_process(kvs)], |
| 145 | + instrument=None, |
| 146 | + sample=parse_sample(kvs), |
| 147 | + title=parse_title(kvs), |
| 148 | + run=[], |
| 149 | + definition=None, |
| 150 | + raw=parse_metanode(kvs), |
| 151 | + ), |
| 152 | + kvs, |
| 153 | + parts[2], |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +def parse_data(lines: list[str], kvs: dict[str, str]) -> dict[str, Quantity]: |
| 158 | + |
| 159 | + data_contents: dict[str, Quantity] = {} |
| 160 | + headers = lines[0].split() |
| 161 | + points = defaultdict(list) |
| 162 | + for line in lines[1:]: |
| 163 | + values = line.split() |
| 164 | + for idx, v in enumerate(values): |
| 165 | + points[headers[idx]].append(float(v)) |
| 166 | + |
| 167 | + for h in points.keys(): |
| 168 | + if h.endswith("_error") and h[:-6] in headers: |
| 169 | + # This was an error line |
| 170 | + continue |
| 171 | + unit = units.none |
| 172 | + if h + "_unit" in kvs: |
| 173 | + unit = unit_parser.parse(kvs[h + "_unit"]) |
| 174 | + |
| 175 | + error = None |
| 176 | + if h + "_error" in headers: |
| 177 | + error = Quantity( |
| 178 | + value=np.asarray(points[h + "_error"]), |
| 179 | + units=unit, |
| 180 | + ) |
| 181 | + |
| 182 | + data_contents[h] = Quantity( |
| 183 | + value=np.asarray(points[h]), |
| 184 | + units=unit, |
| 185 | + standard_error=error, |
| 186 | + ) |
| 187 | + |
| 188 | + for required in ["SpinEchoLEngth", "Depolarisation", "Wavelength"]: |
| 189 | + if required not in data_contents: |
| 190 | + raise FileContentsException(f"SES file missing {required}") |
| 191 | + |
| 192 | + return data_contents |
| 193 | + |
| 194 | + |
| 195 | +def parse_sesans(lines: list[str]) -> SasData: |
| 196 | + version, lines = parse_version(lines) |
| 197 | + metadata, kvs, lines = parse_metadata(lines) |
| 198 | + data_contents = parse_data(lines, kvs) |
| 199 | + return SasData( |
| 200 | + name="Sesans", |
| 201 | + dataset_type=sesans, |
| 202 | + data_contents=data_contents, |
| 203 | + metadata=metadata, |
| 204 | + verbose=False, |
| 205 | + ) |
| 206 | + |
| 207 | + |
| 208 | +def load_data(filename) -> SasData: |
| 209 | + with open(filename) as infile: |
| 210 | + lines = infile.readlines() |
| 211 | + return parse_sesans(lines) |
0 commit comments