Skip to content

attach cell elevations, support z selection - #490

Merged
wpbonelli merged 8 commits into
modflowpy:developfrom
wpbonelli:z-attach
Oct 19, 2026
Merged

wpbonelli merged 8 commits into
modflowpy:developfrom
wpbonelli:z-attach

Conversation

@wpbonelli

@wpbonelli wpbonelli commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Wire #488's elevation selection up to the .grid accessor (#487) to support cell selection by elevation. Builds on #488. Addresses #207.

Method Gives Where there's no cell
.grid.sel(x=..., y=..., z=...) the cell(s) containing the point(s) KeyError
.grid.sel_world(x, y, z) same, with world x/y KeyError
.grid.sel_z(z) a horizontal slice at elevation z NaN
.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 vertical column containing a point: 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)  # the same, on an unrotated grid

x/y are model coordinates, relative to the grid's origin and rotation, as with .sel. .grid.sel_world takes world coordinates (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 in flopy 3.

Plain .sel(z=...) won't work, because z varies from column to column, so xarray can't index it.

Why not?

xarray's .sel only works through an index, and z can't have a default one: cell tops and bottoms vary from column to column, so it's on (layer, row, col), and the default index only takes 1-D coordinates. So .sel(z=...) raises KeyError, alone or with x and y.

Slices and ranges

Layer surfaces often slope, so a given elevation can be in different layers at different places in the grid. To slice laterally, use sel_z or mask_z:

head.grid.sel_z(15.0)
head.where(head.grid.mask_z(12.0, 18.0))  # only the cells between 12 and 18 m

Multi-point selection

Passing lists to an xarray selection returns the cartesian product of coordinates, so e.g. 2 x's and 2 y's give 4 vertical 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.g. well), and the result is on it instead of row/col:

px = xr.DataArray([5.0, 25.0], dims="point")
py = xr.DataArray([5.0, 35.0], dims="point")
pz = xr.DataArray([0.0, 24.0], dims="point")
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 range of world x is a diagonal 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.

wpbonelli and others added 4 commits October 18, 2026 04:54
Arrays on a grid's layers now carry z, z_top and z_bot coordinates from the discretization's top/botm: package arrays, StructuredGrid/VertexGrid datasets and arrays, and DIS/DISV heads and budgets from the grb. vertical.z_coords builds all three, so the centres can't disagree with the bounds; it replaces layer_bounds. sel_world takes an optional z, which picks the cell containing that elevation from the vertical column and raises KeyError if there's none, and the grid accessor gets sel_z and mask_z. DISV face-flow indices on edges no longer take the cells' coordinates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…, elevation, rotated grids

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ask on x_world/y_world or pass point DataArrays

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…he grid classes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wpbonelli wpbonelli added this to the MVP milestone Oct 19, 2026
@wpbonelli wpbonelli added the enhancement New feature or request label Oct 19, 2026
wpbonelli and others added 4 commits October 19, 2026 05:49
…rray's values

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…arbitrary

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ertical column left

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wpbonelli
wpbonelli marked this pull request as ready for review October 19, 2026 13:46
@wpbonelli
wpbonelli merged commit b407f81 into modflowpy:develop Oct 19, 2026
17 checks passed
@wpbonelli
wpbonelli deleted the z-attach branch October 19, 2026 13:47
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