diff --git a/.github/workflows/build_publish.yml b/.github/workflows/build_publish.yml index dd480e4..f27193e 100644 --- a/.github/workflows/build_publish.yml +++ b/.github/workflows/build_publish.yml @@ -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 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index bccb302..33fc414 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 diff --git a/src/sdf_xarray/csdf.pxd b/src/sdf_xarray/csdf.pxd index 876279f..e2fca32 100644 --- a/src/sdf_xarray/csdf.pxd +++ b/src/sdf_xarray/csdf.pxd @@ -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 diff --git a/src/sdf_xarray/download.py b/src/sdf_xarray/download.py index 9c5cfbf..0345067 100644 --- a/src/sdf_xarray/download.py +++ b/src/sdf_xarray/download.py @@ -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, ) diff --git a/src/sdf_xarray/sdf_interface.pyx b/src/sdf_xarray/sdf_interface.pyx index 30244af..ee0d8b3 100644 --- a/src/sdf_xarray/sdf_interface.pyx +++ b/src/sdf_xarray/sdf_interface.pyx @@ -3,6 +3,7 @@ cimport csdf import dataclasses import re import time +import warnings from libc.string cimport memcpy @@ -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] @@ -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)) diff --git a/tests/test_cython.py b/tests/test_cython.py index 1f23dff..ac670c5 100644 --- a/tests/test_cython.py +++ b/tests/test_cython.py @@ -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(): @@ -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")) diff --git a/tests/test_dataset.py b/tests/test_dataset.py index 2974baf..042b6e2 100644 --- a/tests/test_dataset.py +++ b/tests/test_dataset.py @@ -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 @@ -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")