Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
with:
enableCrossOsArchive: true
path: ~/.cache/sdf_datasets
key: sdf-datasets-17991042
key: sdf-datasets-23887747

- name: Setup uv
id: setup-uv
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
uses: actions/cache@v6
with:
path: ~/.cache/sdf_datasets
key: sdf-datasets-17991042
key: sdf-datasets-23887747

- name: Test with pytest
run: uv run pytest
1 change: 1 addition & 0 deletions src/sdf_xarray/csdf.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ cdef extern from "sdf.h":
int64_t data_location
int32_t ndims, geometry, datatype, blocktype
int32_t stagger, datatype_out
int ndim_labels, ndim_units
char const_value[16]
char* id
char* units
Expand Down
2 changes: 1 addition & 1 deletion src/sdf_xarray/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def fetch_dataset(
if not cache_path.is_dir():
datasets = pooch.create(
path=cache_dir,
base_url="doi:10.5281/zenodo.17618509",
base_url="doi:10.5281/zenodo.23887747",
registry=None,
retry_if_failed=10,
)
Expand Down
29 changes: 24 additions & 5 deletions src/sdf_xarray/sdf_interface.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cimport csdf
import dataclasses
import re
import time
import warnings

from libc.string cimport memcpy

Expand Down Expand Up @@ -67,6 +68,20 @@ cdef class Variable(Block):
)


cdef tuple _safely_decode_dim_strings(char** strings, int nstrings, int ndims):
"""Decode a mesh's per-dimension labels or units

The SDF C library can make meshes with fewer of these than dimensions: the
derived CPU-split grids copy them from the first mesh in the file, which may
be a lower-dimension dist_fn grid. Missing entries are returned as ``""``,
and `Mesh.from_block` warns about them
"""
return tuple(
strings[i].decode("UTF-8") if i < nstrings and strings[i] != NULL else ""
for i in range(ndims)
)


@dataclasses.dataclass
cdef class Mesh(Block):
units: tuple[str]
Expand All @@ -83,16 +98,20 @@ cdef class Mesh(Block):

@staticmethod
cdef Mesh from_block(str name, csdf.sdf_block_t* block, SDFFile sdffile):
if block.ndim_labels < block.ndims or block.ndim_units < block.ndims:
warnings.warn(
f"Mesh '{block.id.decode('UTF-8')}' has {block.ndims} dimensions "
f"but only {block.ndim_labels} labels and {block.ndim_units} units "
"in the SDF file, the missing ones are set to ''"
)
return Mesh(
_id=block.id.decode("UTF-8"),
name=name,
dtype=_sdf_type_mapping[block.datatype_out],
shape=tuple(block.dims[i] for i in range(block.ndims)),
units=tuple(
block.dim_units[i].decode("UTF-8") for i in range(block.ndims)
),
labels=tuple(
block.dim_labels[i].decode("UTF-8") for i in range(block.ndims)
units=_safely_decode_dim_strings(block.dim_units, block.ndim_units, block.ndims),
labels=_safely_decode_dim_strings(
block.dim_labels, block.ndim_labels, block.ndims
),
mults=(
tuple(block.dim_mults[i] for i in range(block.ndims))
Expand Down
12 changes: 12 additions & 0 deletions tests/test_cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from sdf_xarray import SDFFile, download

TEST_FILES_DIR = download.fetch_dataset("test_files_1D")
TEST_FILES_MISSING_LABELS_AND_UNITS = download.fetch_dataset(
"test_missing_labels_and_units"
)


def test_sdffile():
Expand Down Expand Up @@ -184,3 +187,12 @@ def test_read_mesh_component_matches_full_read():
f.read_mesh_component(mesh, component, 1, size // 2),
full[component][1 : size // 2],
)


def test_read_cpu_mesh_missing_labels_and_coords():
with pytest.warns(
UserWarning,
match="Mesh 'grid_cpu_rank' has 2 dimensions but only 1 labels and "
"1 units in the SDF file, the missing ones are set to ''",
):
SDFFile(str(TEST_FILES_MISSING_LABELS_AND_UNITS / "0000.sdf"))
13 changes: 13 additions & 0 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
TEST_3D_DIST_FN = download.fetch_dataset("test_dist_fn")
TEST_2D_PARTICLE_DATA = download.fetch_dataset("test_two_probes_2D")
TEST_2D_MOVING_WINDOW = download.fetch_dataset("test_files_2D_moving_window")
TEST_FILES_MISSING_LABELS_AND_UNITS = download.fetch_dataset(
"test_missing_labels_and_units"
)


# Type hinting support
Expand Down Expand Up @@ -852,3 +855,13 @@ def test_lazy_indexing_matches_eager(indexers):
npt.assert_array_equal(
lazy[name].isel(indexers).values, expected.isel(indexers).values
)


@pytest.mark.parametrize("xrlib", [xr, sdfxr])
def test_load_missing_labels_and_coords_warns_user(xrlib: XRLibrary):
with pytest.warns(
UserWarning,
match="Mesh 'grid_cpu_rank' has 2 dimensions but only 1 labels and "
"1 units in the SDF file, the missing ones are set to ''",
):
xrlib.open_dataset(TEST_FILES_MISSING_LABELS_AND_UNITS / "0000.sdf")
Loading