Repository navigation
attach cell elevations, support z selection - #490
Merged
Merged
Conversation
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>
…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
marked this pull request as ready for review
October 19, 2026 13:46
This was referenced Oct 19, 2026
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.
Wire #488's elevation selection up to the
.gridaccessor (#487) to support cell selection by elevation. Builds on #488. Addresses #207..grid.sel(x=..., y=..., z=...)KeyError.grid.sel_world(x, y, z)KeyError.grid.sel_z(z).grid.mask_z(zmin, zmax).whereElevation coordinates
Arrays on a layered grid now have
z_top,z_botandzcoordinates: cell top, bottom and center. This includes package arrays,StructuredGrid/VertexGrid, and DIS/DISV heads and budgets from the binary grid file. DIS/DISVtopgets the top layer's coordinates, since it's those cells' tops. The coordinates havelong_names ("cell top elevation" etc.), to distinguish them from the array's values, e.g. heads.Point-wise selection
Selecting by
xandyalready gives the vertical column containing a point: the cells at one row and column, through all layers..grid.seltakes the same arguments as.sel, plusz, and gives the one cell at that elevation:x/yare model coordinates, relative to the grid's origin and rotation, as with.sel..grid.sel_worldtakes world coordinates (thex_world/y_worldcoordinates). The two are the same unless the grid is offset or rotated.If the point is above the top or below the bottom, a
KeyErroris 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, becausezvaries from column to column, so xarray can't index it.Why not?
xarray's
.selonly works through an index, andzcan'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=...)raisesKeyError, alone or withxandy.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_zormask_z:Multi-point selection
Passing lists to an xarray selection returns the cartesian product of coordinates, so e.g. 2
x's and 2y's give 4 vertical columns:To select only one cell per point instead, pass
DataArrayswith a shared dim. The dim's name is arbitrary (pointhere,pointsin xarray's own examples, or e.g.well), and the result is on it instead ofrow/col: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
ValueErrorin.grid.sel_world. For ranges, mask onx_world/y_world, e.g.da.where(da.x_world >= 1020); for several points, passDataArrays as above.