Skip to content

grid accessor - #487

Merged
wpbonelli merged 2 commits into
modflowpy:developfrom
wpbonelli:grid-accessor
Oct 17, 2026
Merged

wpbonelli merged 2 commits into
modflowpy:developfrom
wpbonelli:grid-accessor

Conversation

@wpbonelli

Copy link
Copy Markdown
Member

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 and others added 2 commits October 17, 2026 08:01
da.mf6.sel_world(x, y) and ds.mf6.sel_world(x, y) call sel_world.
Registered when flopy4.mf6 is imported; the grid utilities load on
first use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
as modflowpy#208 and modflowpy#485 have it, and alongside xugrid's ugrid

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wpbonelli wpbonelli added this to the MVP milestone Oct 17, 2026
@wpbonelli wpbonelli added the enhancement New feature or request label Oct 17, 2026
@wpbonelli wpbonelli mentioned this pull request Oct 17, 2026
@wpbonelli
wpbonelli marked this pull request as ready for review October 17, 2026 15:45
@wpbonelli
wpbonelli merged commit 9895fb2 into modflowpy:develop Oct 17, 2026
17 checks passed
@wpbonelli
wpbonelli deleted the grid-accessor branch October 17, 2026 15:45
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
wpbonelli added a commit that referenced this pull request Oct 19, 2026
Wire #488's elevation selection up to the grid accessor (#487) to support cell selection by elevation. Builds on #488. Addresses #207.

- grid.sel(x=..., y=..., z=...): the cell(s) containing the point(s), KeyError where there's no cell
- grid.sel_world(x, y, z): same, with world x/y
- grid.sel_z(z): a horizontal slice at elevation z, NaN where there's no cell
- grid.mask_z(zmin, zmax): a boolean mask, for where()

Elevation coordinates
---------------------

Arrays on a layered grid now have z_top, z_bot and z coordinates: cell top, bottom and center. This includes package arrays, StructuredGrid/VertexGrid, and DIS/DISV heads and budgets from the binary grid file. DIS/DISV top gets the top layer's coordinates, since it's those cells' tops. The coordinates have long_names ("cell top elevation" etc.), to distinguish them from the array's values, e.g. heads.

    head = gwf.output.head
    head.where(head.z_bot >= 10.0)  # only cells entirely above 10

Point-wise selection
--------------------

Selecting by x and y already gives the vertiint: the cells at one row and column, through all layers. grid.sel takes the same arguments as sel, plus z, and gives the one cell at that elevation:

    head.grid.sel(x=12.0, y=33.0, z=15.0)
    head.grid.sel_world(12.0, 33.0, 15.0)  # grid

x/y are model coordinates, relative to the g as with sel. grid.sel_world takes worldcoordinates (the x_world/y_world coordinates). The two are the same unless the grid is offset or rotated.

If the point is above the top or below the bottom, a KeyError is raised. An elevation exactly on the boundary
between two layers goes to the upper one, as

Plain sel(z=...) won't work, because z varieo xarray can't index it. xarray's sel onlyworks through an index, and z can't have a default one: it's on (layer, row, col), and the default index only takes
1-D coordinates. So sel(z=...) raises KeyErr

Slices and ranges
-----------------

Layer surfaces often slope, so a given elevaayers at different places in the grid. Toslice laterally, use sel_z or mask_z:

    head.grid.sel_z(15.0)
    head.where(head.grid.mask_z(12.0, 18.0)) 12 and 18 m

Multi-point selection
--------------------

Passing lists to an xarray selection returnscoordinates, so e.g. 2 x's and 2 y's give 4vertical columns:

    head.sel(x=[5.0, 25.0], y=[5.0, 35.0])  # 2 rows x 2 columns, each through all layers

To select only one cell per point instead, pass DataArrays with a shared dim. The dim's name is arbitrary (point
here, points in xarray's own examples, or e.s on it instead of row/col:

    px = xr.DataArray([5.0, 25.0], dims="poi
    py = xr.DataArray([5.0, 35.0], dims="point")
    pz = xr.DataArray([0.0, 24.0], dims="poi
    head.grid.sel(x=px, y=py, z=pz)  # 2 cells, along the point dim

Rotated grids
-------------

On a rotated grid, columns don't run along world x, nor rows along world y, so a list of world x's and y's doesn't pick out a set of rows and columns, and a raal band, not a range of columns. Both raise a ValueError in grid.sel_world. For ranges, mask on x_world/y_world, e.g. da.where(da.x_world >= 1020); for several
points, pass DataArrays as above.
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