sal-xarray 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
sal_xarray/main.py ADDED
@@ -0,0 +1,116 @@
1
+ """Xarray SAL backend entrypoint."""
2
+
3
+ import os
4
+ import re
5
+ from typing import Any, Iterable
6
+
7
+ import numpy as np
8
+ import xarray as xr
9
+ from sal.client import SALClient
10
+
11
+ from xarray.backends import BackendEntrypoint
12
+ from xarray.backends.common import ReadBuffer
13
+ from xarray.backends.common import AbstractDataStore
14
+
15
+
16
+ class SALBackendEntrypoint(BackendEntrypoint):
17
+ """Xarray SAL backend entrypoint."""
18
+
19
+ def open_dataset(
20
+ self,
21
+ filename_or_obj: str
22
+ | os.PathLike[Any]
23
+ | ReadBuffer
24
+ | bytes
25
+ | memoryview
26
+ | AbstractDataStore,
27
+ *,
28
+ drop_variables: str | Iterable[str] | None = None,
29
+ host: str = "https://sal.jetdata.eu",
30
+ ) -> xr.Dataset:
31
+ """Open a SAL dataset given a signal name and shot number.
32
+
33
+ Parameters
34
+ ----------
35
+ filename_or_obj : str
36
+ SAL dataset specified as sal://pulse/<shot_number>/<signal_name>
37
+ drop_variables : str or list of str, optional
38
+ Variables to drop from the dataset (not used).
39
+ """
40
+
41
+ if not isinstance(filename_or_obj, str):
42
+ raise ValueError("SAL backend only supports string filenames.")
43
+
44
+ if "sal://" not in filename_or_obj:
45
+ raise ValueError("SAL dataset must start with the sal:// scheme")
46
+
47
+ name = filename_or_obj.replace("sal://", "")
48
+ match = re.search(r"sal://pulse/(\d+)/", filename_or_obj)
49
+
50
+ if not match:
51
+ raise ValueError(
52
+ "Invalid SAL URL format. Expected sal://pulse/<shot_number>/<signal_name>"
53
+ )
54
+
55
+ shot = int(match.group(1))
56
+ name = "/" + name
57
+
58
+ client = SALClient(host=host)
59
+ try:
60
+ signal = client.get(name)
61
+ except Exception as e:
62
+ raise RuntimeError(f"Failed to retrieve SAL signal '{name}': {e}") from e
63
+
64
+ data = self.parse_data(signal)
65
+ data.attrs["shot_id"] = shot
66
+
67
+ status = xr.DataArray(signal.mask.status, dims=data.dims, coords=data.coords)
68
+ status.attrs["description"] = signal.mask.description
69
+ status.attrs["flag_values"] = np.unique(signal.mask.status)
70
+ status.attrs["flag_meanings"] = signal.mask.key.tolist()
71
+
72
+ dataset = data.to_dataset(name="data")
73
+ dataset["status"] = status
74
+ return dataset
75
+
76
+ def parse_data(self, signal) -> xr.DataArray:
77
+ """Parse a SAL LeafReport into an xarray DataArray."""
78
+
79
+ dims = {}
80
+ for index, item in enumerate(signal.dimensions):
81
+ name = f"dim_{index}"
82
+ if item.temporal:
83
+ name = "time"
84
+ dim = xr.DataArray(item.data, dims=[name], name=name)
85
+ dim.attrs["description"] = item.description
86
+ dim.attrs["units"] = item.units
87
+
88
+ dims[dim.name] = dim
89
+
90
+ data = xr.DataArray(
91
+ signal.data,
92
+ dims=[d.name for d in dims.values()],
93
+ coords={d.name: d for d in dims.values()},
94
+ )
95
+
96
+ data.attrs["description"] = signal.description
97
+ data.attrs["units"] = signal.units
98
+ data.attrs["sal_name"] = name
99
+
100
+ return data
101
+
102
+ def open_datatree(self, filename_or_obj, *, drop_variables=None):
103
+ self.open_dataset(filename_or_obj, drop_variables=drop_variables)
104
+
105
+ def open_groups_as_dict(self, filename_or_obj, **kwargs):
106
+ """Open groups as a dictionary (not supported for SAL backend)."""
107
+ raise NotImplementedError("SAL backend does not support opening groups")
108
+
109
+ open_dataset_parameters = ["filename_or_obj", "drop_variables"]
110
+
111
+ def guess_can_open(self, filename_or_obj):
112
+ return filename_or_obj.startswith("sal://")
113
+
114
+ description = "Use SAL data in Xarray"
115
+
116
+ url = "https://github.com/samueljackson92/sal-xarray"
@@ -0,0 +1,123 @@
1
+ Metadata-Version: 2.4
2
+ Name: sal-xarray
3
+ Version: 0.1.0
4
+ Summary: SAL backend for xarray
5
+ Author-email: Samuel Jackson <samuel.jackson@ukaea.uk>
6
+ Maintainer-email: Samuel Jackson <samuel.jackson@ukaea.uk>
7
+ Project-URL: repository, https://github.com/samueljackson92/sal-xarray
8
+ Classifier: Development Status :: 4 - Beta
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.11
11
+ Classifier: Programming Language :: Python :: 3.12
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Requires-Python: <3.13,>=3.11
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+ Requires-Dist: numpy<2
17
+ Requires-Dist: sal>=1.2.5
18
+ Requires-Dist: xarray>=2025.12.0
19
+ Dynamic: license-file
20
+
21
+ # sal-xarray
22
+
23
+ An xarray backend for SAL (Simple Access Layer) that enables seamless integration of SAL data sources with the xarray ecosystem.
24
+
25
+ ## Overview
26
+
27
+ sal-xarray provides a backend plugin for xarray that allows you to access SAL data sources using xarray's familiar API. It automatically handles the conversion of SAL signals to xarray DataArrays and Datasets, making it easy to work with SAL data in scientific Python workflows.
28
+ ## Features
29
+
30
+ - **xarray Integration**: Access SAL data using xarray's `open_dataset` function
31
+ - **Automatic Conversion**: Converts SAL signals to xarray DataArrays with proper coordinates and metadata
32
+ - **Status Mask Handling**: Includes status mask data alongside signal data
33
+ - **URI-based Access**: Simple URI scheme (`sal://pulse/<shot_number>/<signal_name>`) for accessing data
34
+
35
+ ## Installation
36
+
37
+ ```bash
38
+ pip install sal-xarray
39
+ ```
40
+
41
+ Or using `uv`:
42
+
43
+ ```bash
44
+ uv pip install sal-xarray
45
+ ```
46
+
47
+ ## Usage
48
+
49
+ Open a SAL dataset using xarray:
50
+
51
+ ```python
52
+ import xarray as xr
53
+
54
+ # Open a SAL signal by name and shot number
55
+ ds = xr.open_dataset("sal://pulse/87737/ppf/signal/jetppf/magn/ipla", engine="sal")
56
+
57
+ # Access the data
58
+ data = ds["data"]
59
+ status = ds["status"]
60
+
61
+ # The dataset includes time coordinates
62
+ time = ds["time"]
63
+
64
+ # Access metadata
65
+ units = ds["data"].attrs["units"]
66
+ signal_name = ds["data"].attrs["sal_name"]
67
+ ```
68
+
69
+ The URI format is: `sal://pulse/<shot_number>/<signal_name>`
70
+
71
+ ## Data Structure
72
+
73
+ When you open a SAL dataset, sal-xarray creates an xarray Dataset with:
74
+ - **data**: The signal data as a DataArray
75
+ - **status**: The status data as a DataArray
76
+ - **time**: Time coordinates (dimension)
77
+ - **attrs**: Metadata including units and SAL signal name
78
+
79
+ ## Development
80
+
81
+ ### Setup
82
+
83
+ Clone the repository and install development dependencies:
84
+
85
+ ```bash
86
+ git clone https://github.com/samueljackson92/sal-xarray.git
87
+ cd sal-xarray
88
+ uv sync
89
+ ```
90
+
91
+ ### Running Tests
92
+
93
+ ```bash
94
+ uv run pytest tests/
95
+ ```
96
+
97
+ ### Code Quality
98
+
99
+ The project uses ruff for linting and formatting, and pylint for additional checks:
100
+
101
+ ```bash
102
+ # Run ruff
103
+ uv run ruff check sal_xarray tests
104
+ uv run ruff format sal_xarray tests
105
+
106
+ # Run pylint
107
+ uv run pylint sal_xarray
108
+
109
+ # Run ty
110
+ uv run ty check sal_xarray
111
+ ```
112
+
113
+ ## Contributing
114
+
115
+ Contributions are welcome! Please ensure:
116
+
117
+ 1. Tests pass for any new functionality
118
+ 2. Code follows the project's style guidelines (ruff and pylint)
119
+ 3. Documentation is updated as needed
120
+
121
+ ## License
122
+
123
+ MIT License
@@ -0,0 +1,7 @@
1
+ sal_xarray/main.py,sha256=dQNLmfYTj6Buy-Jkv_nx_Hejv7wa2-DpvXDOOK-iqlA,3764
2
+ sal_xarray-0.1.0.dist-info/licenses/LICENSE,sha256=N_v8bcUerdJcYVvboaEnKJxOrcnAddHaM3na6KFy0B0,1071
3
+ sal_xarray-0.1.0.dist-info/METADATA,sha256=ZCG7NqT0ATsCGVDuLcgX-HDeo2_JddLx2HMkYCfCcJA,3140
4
+ sal_xarray-0.1.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
5
+ sal_xarray-0.1.0.dist-info/entry_points.txt,sha256=CwN-BKTBiu1MTuXPfRGTNf3XjLVr5AG32nMqFInaVpw,61
6
+ sal_xarray-0.1.0.dist-info/top_level.txt,sha256=USvgVet1x_Qj3I8NUxJ95ZzGK2at3Mq98KT948Ltr7k,11
7
+ sal_xarray-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (80.9.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [xarray.backends]
2
+ sal = sal_xarray.main:SALBackendEntrypoint
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Samuel Jackson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ sal_xarray