Skip to content

Respect CF coordinate metadata in UGRID - #1893

Open
rajeeja wants to merge 5 commits into
mainfrom
rajeeja/ugrid-coordinates-901
Open

rajeeja wants to merge 5 commits into
mainfrom
rajeeja/ugrid-coordinates-901

Conversation

@rajeeja

@rajeeja rajeeja commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Closes #901.
Select longitude and latitude by CF standard_name. Projected-only coordinates still load with the existing warning, and geographic face_coordinates and edge_coordinates from the file are now used as given.

@erogluorhan erogluorhan 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.

Choosing lon/lat by standard_name is a good fix, and a file with node_coordinates = "y x" now reads correctly. But this PR rejects projected UGRID files, which #1525 made loadable and docs/user-guide/grid-formats.rst ("Projected (Non-Spherical) Coordinates") documents as working for loading and plotting. #901 asked for that support, not for an error, so I don't think this should close #901.

  • Full suite on the PR: 1295 passed. None of the existing projected tests reach _read_ugrid (test_projected.py builds ux.Grid(ds) directly), so nothing noticed the change in behavior.
  • Opened small UGRID files with ux.open_grid(path) on main and on the PR (results in the inline comments).
  • Mutation: reducing the degree-units list to {""} still passes the whole suite, so the unit check is untested.

Before merge: keep projected node coordinates loading (or get a maintainer decision to drop #1525's behavior and update the docs), and fix the unit list.

Comment thread uxarray/io/_ugrid.py Resolved
Comment thread uxarray/io/_ugrid.py Resolved
Comment thread uxarray/io/_ugrid.py
)
else:
# Projected centers may be retained, but are not spherical coordinates.
continue

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.

(non-blocking) Behavior change worth putting in the description: face_coordinates/edge_coordinates are now always used. On main they were renamed only if face_lon/face_lat already existed, which in practice left them unused. For a file with face_coordinates="cx cy" (cx=0.3), face_lon changes from the computed centroid (0.333) to 0.3. I think this is right, but it should be stated.

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.

Code is fine; just the description note left.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unresolved coordinate-handling defects can misinterpret radians or projected values as degrees, trigger geometry recursion, and reject previously supported geographic inputs.

5 open issues
What changed in this PR

Updates UXarray’s UGRID reader to select geographic coordinates using CF metadata rather than assuming coordinate order, addressing the projected-coordinate concern in #901.

Changes:

  • Selects node, edge and face longitude/latitude by standard_name, with a legacy positional fallback.
  • Rejects node pairs identified as projected by standard names or units, while retaining unselected coordinate variables.
  • Adds tests for reordered coordinates, chunked loading, projected inputs, mixed coordinate sets and legacy files.
File Description
uxarray/​io/​_ugrid.py Adds metadata-based coordinate selection and projected-node validation.
test/​io/​test_ugrid_coordinates.py Tests geographic selection, projected-coordinate handling and legacy compatibility.

Review effort: Balanced

Comment thread uxarray/io/_ugrid.py
Comment on lines +24 to +26
geographic = {ds[name].attrs.get("standard_name"): name for name in names}
if "longitude" in geographic and "latitude" in geographic:
lon_name, lat_name = geographic["longitude"], geographic["latitude"]
Comment thread uxarray/io/_ugrid.py Outdated
"degree_n",
"radian",
"radians",
}
Comment thread uxarray/io/_ugrid.py
Comment on lines +58 to +59
coord_dict[lon_name] = f"{location}_lon"
coord_dict[lat_name] = f"{location}_lat"
Comment thread uxarray/io/_ugrid.py Outdated
if "longitude" in geographic and "latitude" in geographic:
lon_name, lat_name = geographic["longitude"], geographic["latitude"]
elif len(names) == 2 and all(
not ds[name].attrs.get("standard_name")
Comment thread uxarray/io/_ugrid.py Outdated
and ds[name].attrs.get("units", "").lower()
in {
"",
"degree",
@rajeeja

rajeeja commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

@erogluorhan thanks for the review, all three points are addressed in e6bc9d0. I kept Closes #901 because the change the issue proposes is selecting longitude and latitude by standard_name, and projected files load as before. Native projected geometry is still unsupported, so let me know if you want #901 kept open, and I'll change the PR to Related to #901.

@erogluorhan erogluorhan 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.

Thanks @rajeeja, all of my points seem fixed, Closes #901 is fine with me after your clarification.

I am approving it now. Please see a few tiny points below though:

Please update the description: it still says projected coordinates are rejected.

Separately, and not from this PR: Claude found that face_areas on a projected grid returns a meaningless value with no error (a UTM triangle of 5000 m² gives 0.855, same on main). Could you open an issue to make geometry on projected grids raise?

…ates-901

# Conflicts:
#	uxarray/io/_ugrid.py
@rajeeja
rajeeja requested a review from erogluorhan November 24, 2026 06:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support projection coordinates in _read_ugrid

3 participants