Skip to content

world-coordinate and containing-cell selection on structured grids - #485

Merged
wpbonelli merged 5 commits into
modflowpy:developfrom
wpbonelli:rotated-world
Oct 17, 2026
Merged

wpbonelli merged 5 commits into
modflowpy:developfrom
wpbonelli:rotated-world

Conversation

@wpbonelli

@wpbonelli wpbonelli commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #469. On a rotated DIS grid, x/y are local: zero-based, in the grid's frame, as MF6 writes them to netCDF. The rotation lives in the projection coordinate. This PR adds world coordinates and world selection on top of that

Relation to other work

  • Xarray structured grid index #207 asked for a structured grid index so DIS data can be selected by layer/row/column
  • rework grid dims and coordinates #469 covered layer/row/column: the dims are layer/row/col, so isel works directly and no custom index is needed for it
  • This PR covers the point queries: CellIndex is the structured grid index, without a method sel gives the containing cell, and method="nearest" gives the nearest center. Rotation, which the design doc didn't consider, is handled as an overlay of world coordinates rather than by world-valued x/y.
  • As in flopy3, a point on an edge between cells goes to the lower-numbered cell.
  • Still open under Xarray structured grid index #207: selecting by z (vertical profiles, elevation ranges), spatial selection on DISV/DISU, and radius queries. The .grid accessor (Xarray (structured) grid accessor #208) comes next and will expose sel_world and the rest

wpbonelli and others added 4 commits October 17, 2026 07:19
Arrays on a rotated structured grid, whose x/y are local, get the cells'
world coordinates as unindexed x_world/y_world on (row, col), computed
from the projection coordinate. sel_world selects by world coordinates,
converting to local through to_local, the inverse of to_world; on other
grids it's sel. With a CRS, projection has crs_wkt (wrapped with the
rotation if rotated) and grid_mapping_name, as MF6 writes them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The grb has no CRS, so take crs_wkt, grid_mapping_name and the rest
from the file's projection variable, keeping the grb's placement.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
sel_world without a method missed even a cell's own world centre on a
rotated grid, since rotating to local coordinates rounds. Match to
within a billionth of the grid's extent instead; other points still
raise, as with sel. Test that projection and the world coordinates
survive concat and a netCDF round trip, and note that combining arrays
on differently placed grids keeps the first's projection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Index x/y with CellIndex, a PandasIndex that also keeps each cell's
edges, from the dx/dy cell sizes, so sel without a method gives the
cell containing the point rather than needing its centre. Edges go to
the cell east or north, points outside raise, and method="nearest"
still picks the nearest centre. Package arrays and a StructuredGrid's
dataset get dx/dy, as hds/cbc output has, and sel_world no longer needs
a tolerance for rotation rounding.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wpbonelli wpbonelli added this to the MMP milestone Oct 17, 2026
@wpbonelli wpbonelli added the enhancement New feature or request label Oct 17, 2026
A point on the edge between two cells now gets the one with the lower
index (west or north), as with flopy's StructuredGrid.intersect, rather
than the east or north one. Ties are within the edge tolerance, so a
point rounded a hair off an edge (e.g. by rotation) goes to the same
cell as one exactly on it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wpbonelli
wpbonelli marked this pull request as ready for review October 17, 2026 14:44
@wpbonelli
wpbonelli merged commit b120ef1 into modflowpy:develop Oct 17, 2026
17 checks passed
@wpbonelli
wpbonelli deleted the rotated-world branch October 17, 2026 14:58
wpbonelli added a commit to wpbonelli/pyphoenix-project that referenced this pull request Oct 17, 2026
as modflowpy#208 and modflowpy#485 have it, and alongside xugrid's ugrid

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
wpbonelli added a commit that referenced this pull request Oct 17, 2026
Follow-up to #485. CellIndex used PandasIndex's join and concat, which build a new index without the cell edges. So after a + b on overlapping grids, xr.align, or concat along row/col, sel(x=, y=) without a method needed exact centers again.

* join and concat keep each cell's edges, in any order
* aligning grids with a cell at the same centre but different edges raises ValueError
* index_xy skips a scalar x or y (e.g. a single row) instead of failing
wpbonelli added a commit that referenced this pull request Oct 17, 2026
Add a .grid accessor on DataArrays and Datasets. Addresses #208 and corresponds to xugrid's .ugrid. For now its only method is sel_world(x, y, **kwargs), the same as the free function from #485: da.grid.sel_world(x, y).
wpbonelli added a commit that referenced this pull request Oct 17, 2026
* layer_bounds(top, botm) builds (z_top, z_bot) with the same shape as botm
* layer_at(z, z_top, z_bot) gives the layer
* sel_z(obj, z, outside="nan") selects the cell containing z. vertical columns where z is outside the model are NaN, or raise KeyError with outside="raise", dask-backed arrays stay lazy
* mask_z(obj, zmin, zmax, how="overlap" | "within" | "center") is a boolean mask over cells

An elevation on a layer boundary goes to the upper layer, as in flopy3's intersect and #485's x/y edges. Bounds come from z_top/z_bot coordinates or are passed in, nothing attaches them yet, soon this will be wired up to #487
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant