-
Notifications
You must be signed in to change notification settings - Fork 55
Merge coincident pole and antimeridian nodes in structured grids #1690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rajeeja
wants to merge
5
commits into
main
Choose a base branch
from
rajeeja/structured-coincident-nodes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1b6224e
Merge coincident nodes in structured grids on the sphere
rajeeja 3b1016e
Keep tol in degrees when matching nodes on the sphere
rajeeja 70cade5
Address review: derive tol from ERROR_TOLERANCE, reuse xyz helper, cl…
rajeeja dde8a18
Upcast lon/lat to float64 before coincidence detection
rajeeja 2aceeab
Merge branch 'main' into rajeeja/structured-coincident-nodes
rajeeja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,17 @@ | ||
| import numpy as np | ||
| import xarray as xr | ||
|
|
||
| from uxarray.constants import INT_DTYPE | ||
| from uxarray.constants import ERROR_TOLERANCE, INT_DTYPE, INT_FILL_VALUE | ||
| from uxarray.conventions import ugrid | ||
| from uxarray.grid.coordinates import _lonlat_rad_to_xyz | ||
|
|
||
| # ``ERROR_TOLERANCE`` is defined as a Cartesian distance on the unit sphere; convert | ||
| # it to the angular tolerance `tol` is documented in so the default tracks the same | ||
| # precision assumption used everywhere else in the codebase. | ||
| _DEFAULT_STRUCTURED_TOL_DEG = np.rad2deg(2.0 * np.arcsin(ERROR_TOLERANCE / 2.0)) | ||
|
|
||
| def _read_structured_grid(lon, lat, tol=1e-10): | ||
|
|
||
| def _read_structured_grid(lon, lat, tol=_DEFAULT_STRUCTURED_TOL_DEG): | ||
| """ | ||
| Constructs an unstructured grid dataset from structured longitude and latitude coordinates. | ||
|
|
||
|
|
@@ -21,7 +27,9 @@ def _read_structured_grid(lon, lat, tol=1e-10): | |
| lat : array_like | ||
| 1D array of latitude coordinates in degrees. | ||
| tol : float, optional | ||
| Tolerance for considering nodes as identical (default is `1e-10`). | ||
| Tolerance in degrees for considering nodes as identical. Defaults to the angle | ||
| whose chord length on the unit sphere equals ``uxarray.constants.ERROR_TOLERANCE``, | ||
| matching the precision assumption used elsewhere in the codebase. | ||
|
|
||
| Returns | ||
| ------- | ||
|
|
@@ -42,6 +50,13 @@ def _read_structured_grid(lon, lat, tol=1e-10): | |
|
|
||
| out_ds = xr.Dataset() | ||
|
|
||
| # Coincidence detection below relies on float64 precision (~1e-16); real-world | ||
| # datasets often store lon/lat as float32 (~1e-7), which silently propagates | ||
| # through this pipeline and causes pole/antimeridian merges to fail or merge | ||
| # only partially, regardless of ``tol``. | ||
| lon = np.asarray(lon, dtype=np.float64) | ||
| lat = np.asarray(lat, dtype=np.float64) | ||
|
|
||
| sorted_indices = np.argsort(lon) | ||
| lon = lon[sorted_indices] | ||
|
|
||
|
|
@@ -79,11 +94,22 @@ def _read_structured_grid(lon, lat, tol=1e-10): | |
| # Stack longitude and latitude for processing | ||
| nodes = np.column_stack((node_lon, node_lat)) | ||
|
|
||
| # Match nodes on the sphere rather than in the lon/lat plane, so that the poles | ||
| # (many lon values, one point) and the antimeridian seam (lon differing by 360) | ||
| # are recognized as coincident. | ||
| lon_rad = np.deg2rad(node_lon) | ||
| lat_rad = np.deg2rad(node_lat) | ||
| node_xyz = np.column_stack(_lonlat_rad_to_xyz(lon_rad, lat_rad)) | ||
|
|
||
| # Build KDTree | ||
| tree = KDTree(nodes) | ||
| tree = KDTree(node_xyz) | ||
|
|
||
| # ``tol`` is an angle in degrees; on the unit sphere the matching radius is the | ||
| # chord subtended by that angle, so the threshold keeps its documented meaning. | ||
| chord_tol = 2.0 * np.sin(np.deg2rad(tol) / 2.0) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This conversion is now redundant with the changes to default |
||
|
|
||
| # Find all pairs of nodes within the tolerance | ||
| pairs = tree.query_pairs(r=tol) | ||
| pairs = tree.query_pairs(r=chord_tol) | ||
|
|
||
| n_nodes = len(nodes) | ||
| if pairs: | ||
|
|
@@ -138,6 +164,34 @@ def _read_structured_grid(lon, lat, tol=1e-10): | |
| # Stack the node indices to form face_node_connectivity | ||
| face_node_conn = np.vstack((n1, n2, n3, n4), dtype=INT_DTYPE).T | ||
|
|
||
| # No new faces are created here -- this only shrinks the width of existing rows | ||
| # in face_node_conn for faces that became degenerate after the pole merge above. | ||
| # | ||
| # A face touching the pole is built from 2 distinct edge-longitudes at the pole | ||
| # latitude, e.g. corners (n1, n2, n3, n4) = (A, P, P, B), where P is the single | ||
| # merged pole node that both pole-row corners now point to (n2 == n3). That is a | ||
| # triangle A-P-B stored as a 4-column quad with one corner repeated, so: | ||
| # 1. `keep` marks, per face, which corners differ from their cyclic predecessor | ||
| # (n2 == n3 above means the P at position 2 is dropped from that row). | ||
| # 2. The kept corners are pushed to the front of each row (`order`), giving | ||
| # (A, P, B, B) instead of (A, P, P, B) -- still 4 columns, but the last | ||
| # column is now the padding slot for a 3-node face. | ||
| # 3. `n_max_face_nodes` is the largest node count any face still needs (3 here, | ||
| # unless some other face in the grid still has 4 distinct corners, in which | ||
| # case nothing is trimmed and this is a no-op). Columns beyond each face's | ||
| # own count are set to `INT_FILL_VALUE`, giving (A, P, B, FILL). | ||
| keep = face_node_conn != np.roll(face_node_conn, 1, axis=1) | ||
| if not keep.all(): | ||
| n_nodes_per_face = keep.sum(axis=1) | ||
| order = np.argsort(~keep, axis=1, kind="stable") | ||
| compacted = np.take_along_axis(face_node_conn, order, axis=1) | ||
| n_max_face_nodes = n_nodes_per_face.max() | ||
| compacted = compacted[:, :n_max_face_nodes] | ||
| compacted[np.arange(n_max_face_nodes) >= n_nodes_per_face[:, None]] = ( | ||
| INT_FILL_VALUE | ||
| ) | ||
| face_node_conn = compacted | ||
|
|
||
| out_ds["node_lon"] = xr.DataArray( | ||
| data=unique_node_lon, dims=ugrid.NODE_DIM, attrs=ugrid.NODE_LON_ATTRS | ||
| ) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor but please move numpy import to top of file to match style with other testing suite files. Similar for
import numpycall from test below.