Repository navigation
grid accessor - #487
Merged
Merged
grid accessor#487
Conversation
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>
Merged
wpbonelli
marked this pull request as ready for review
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.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Add a
.gridaccessor on DataArrays and Datasets. Addresses #208 and corresponds to xugrid's.ugrid. For now its only method issel_world(x, y, **kwargs), the same as the free function from #485:da.grid.sel_world(x, y).