Skip to content

Check SGRID node dimensions and coordinates against the dataset - #3035

Merged
VeckoTheGecko merged 2 commits into
Parcels-code:mainfrom
VeckoTheGecko:sgrid-ds
Nov 4, 2026
Merged

VeckoTheGecko merged 2 commits into
Parcels-code:mainfrom
VeckoTheGecko:sgrid-ds

Conversation

@VeckoTheGecko

Copy link
Copy Markdown
Contributor

Description

There was some existing tooling for checking that SGRID metadata was consistent with the underlying dataset (this logic was in a function assert_metadata_ds_consistency). This consistency check was limited to the padding time and number of observations along dimensions (i.e., a padding of None means that there is one less edge than nodes, etc.).

This PR extends the consistency checks to also ensure:

This PR also makes the function assert_metadata_ds_consistency a method on the accessor, making it more accesible to the rest of the code.

minor changes

SGridDatasetInconsistency now subclasses ValueError.

Also fix croco_to_sgrid node_dimensions (xi_rho, eta_rho), and update the example datasets to use per-axis degree units and to define node dimension coordinates in the sgrid_dataset strategy.

Checklist

  • Closes None
  • Tests added
  • This PR targets the correct branch (main for normal development, v3-support for v3 support)

AI Disclosure

  • This PR contains AI-generated content.
    • I have tested any AI-generated content in my PR.
    • I take responsibility for any AI-generated content in my PR.
    • Describe how you used it (e.g., by pasting your prompt): Explicit instructions provided to Claude Opus 5.5

Add ds.sgrid.assert_metadata_ds_consistency() which, in addition to the
existing face/node size check, asserts that:
- node_dimensions coordinate variables have axis attributes X then Y
- node_coordinates exist, are defined on the node dimensions (1D coords
  along their own axis), and have longitude/latitude units in X then Y
  order when in degrees

SGridDatasetInconsistency now subclasses ValueError.

Also fix croco_to_sgrid node_dimensions (xi_rho, eta_rho), and update the
example datasets to use per-axis degree units and to define node
dimension coordinates in the sgrid_dataset strategy.

@VeckoTheGecko VeckoTheGecko left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some changes to the test suite/data generation was needed to attach this new metadata. There is also a change to convert.croco_to_sgrid.

There's no change to the docs - everything works like before.

max_lat = 90.0 if mesh_str == "spherical" else 1e6
unit = "degrees" if mesh_str == "spherical" else "m"
x_unit = "degrees_east" if mesh_str == "spherical" else "m"
y_unit = "degrees_north" if mesh_str == "spherical" else "m"

@VeckoTheGecko VeckoTheGecko Nov 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change isn't strictly necessary, but I think it being more explicit is nice

Comment on lines +177 to +184
# A 1D node coordinate (i.e., a rectilinear grid) must lie along its own axis. If one of the node
# dimensions has been indexed away (e.g., via isel) a 2D coordinate can legitimately be 1D along the other
if both_node_dims_in_ds and da.ndim == 1 and da.dims != (node_dim,):
raise SGridDatasetInconsistency(
f"Node coordinate {coord!r} is 1D with dims {da.dims}, but as entry {expected_axis!r} in "
f"node_coordinates={metadata.node_coordinates!r} it is expected to be defined on node dimension {node_dim!r}. "
f'HINT: Make sure node_coordinates in the SGRID metadata is ordered as "X" then "Y".'
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to #2054

)


def _assert_node_coordinates_well_defined(ds: xr.Dataset, metadata: SGrid2DMetadata):

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function is the main thing worth reviewing @erikvansebille

@erikvansebille erikvansebille left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good - thanks for adding this code to check the SGrid consistency!

@VeckoTheGecko
VeckoTheGecko merged commit 1af6d0a into Parcels-code:main Nov 4, 2026
22 checks passed
@github-project-automation github-project-automation Bot moved this from Backlog to Done in Parcels development Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants