Skip to content

rework grid dims and coordinates - #469

Merged
wpbonelli merged 12 commits into
modflowpy:developfrom
wpbonelli:grid-dims
Oct 15, 2026
Merged

wpbonelli merged 12 commits into
modflowpy:developfrom
wpbonelli:grid-dims

Conversation

@wpbonelli

@wpbonelli wpbonelli commented Oct 15, 2026 •

Copy link
Copy Markdown
Member

Rename grid dims returned by to_dataarray

  • DIS: (layer, row, col) instead of (layer, y, x)
  • DISV: (layer, cell)
  • DISU: (node,) (same as before)

Similar renaming for the head/budget readers:

  • DIS: (time, layer, row, col)
  • DISV: (time, layer, cell)

And add cell centers as x/y coordinates when the model has a discretization: on col/row for DIS (indexed, so sel(x=..., y=..., method="nearest") works), on cell/node for DISV/DISU. Standalone packages have no geometry, so they get none. Rotated DIS grids get local x/y, zero at the lower-left corner, as in MF6's netCDF output. Every structured array gets a scalar projection coordinate (CF grid-mapping style, named in its grid_mapping attr) whose attrs xorigin/yorigin/angrot place x/y in the world. DISV/DISU centres and vertices are rotated into world coordinates.

Reasons:

  • y/x seemed like world coordinates but weren't
  • the DFN names (nlay, nrow, ...) are awkward in selections (isel(nlay=0)), and xarray has no dim aliases, so name the dims for the axes and keep the DFN names for looking up dim sizes
  • cell rather than node for DISV, to avoid confusion with UGRID where "node" means vertex, and the other MF6 sense where it means 3d center of the cell. DISV inputs and outputs now match

NetCDF file handling is unchanged; the readers rename MF6's x/y/nmesh_face on load.

It's now necessary to explicitly pass x="x", y="y" to contour plots of DIS output.

@wpbonelli wpbonelli added this to the MVP milestone Oct 15, 2026
@wpbonelli wpbonelli added the bug Something isn't working label Oct 15, 2026
wpbonelli and others added 4 commits October 15, 2026 06:46
to_dataarray's dims are (nlay, nrow, ncol), (nlay, ncpl) or (nodes,),
the names the generated classes' shapes and utils/grid.py already use,
in place of (layer, y, x), (layer, face) and (node,). y and x read as
world coordinates, which these indices aren't.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
to_dataarray's dims are (layer, row, col), (layer, node) or (node,).
The DFNs' sizing names read badly in a selection (isel(nlay=0)), and
xarray has no dim aliases. A DISV layer's cell is a node, as in DISU.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
node means a vertex in UGRID, which xugrid users see alongside these.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Heads and budgets read from a DIS model are (time, layer, row, col),
as for package arrays, in place of (time, layer, y, x). x and y stay as
world coordinates on col and row, indexed so they select. No 1-based
layer coordinate on DIS or DISV output: as for inputs, a position is
the index. DISV output keeps xugrid's face dim, which its topology
needs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wpbonelli wpbonelli changed the title name grid dims for the grid's sizes improve grid dim naming Oct 15, 2026
@wpbonelli wpbonelli removed the bug Something isn't working label Oct 15, 2026
@wpbonelli
wpbonelli marked this pull request as ready for review October 15, 2026 13:51
wpbonelli and others added 8 commits October 15, 2026 07:02
DISV heads and budgets were on xugrid's default face dim, mesh2d_nFaces.
Name it cell, as on DISV input arrays; UGRID lets a mesh name its face
dim. Horizontal flows stay on the mesh's edges. The flow-ja-face
connectivity's n/m dim, which was also called cell, is now nm.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
StructuredGrid's k/i/j and VertexGrid's k/icpl coordinates held
0..n-1 along nlay/nrow/ncol/ncpl, which a position already gives
(isel), and k clashed with NPF's k, so a grid's dataset couldn't merge
with NPF's. node stays, as the only thing on the nodes dim. x/y/z
world coordinates are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
StructuredGrid's dims are (layer, row, col) and VertexGrid's (layer,
cell), as for package arrays, so a grid's dataset merges with a
package's. The global node coordinate goes too, as an index coordinate.
grid.dims gives the sizes by their DFN names for a package's dims=,
which the examples used dataset.sizes for.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
As for package arrays: UGRID's node is a vertex.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
to_dataarray assigns x on col and y on row for DIS (from delr/delc and
the origin), indexed so arrays select by them like DIS output does, and x
and y on the cells for DISV and DISU (from cell2d). A standalone package
has no geometry and gets none. Rotated grids get none for now.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
x/y are world coordinates everywhere, rotated about the grid's origin
by angrot as in MF6. DISV/DISU cell centres and DISV output vertices
are rotated. A rotated DIS grid's x/y are 2-D on (row, col), and not
indexed, since xarray can't index 2-D coordinates. The grb readers
used to ignore angrot, placing rotated output unrotated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A rotated DIS grid's rows and columns don't line up with world x and
y, so 2-D world x/y can't be indexed. Rather than commit to those, its
arrays get no x/y for now. A follow-up will give them local x/y in the
grid's own frame, as MF6's netCDF output has, with the rotation as
grid-mapping metadata. A rotated StructuredGrid's x/y are local,
matching to_xarray(). DISV/DISU centres and vertices stay rotated into
world coordinates.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A rotated DIS grid's x/y are in its own frame, zero at the lower-left
corner, as MF6 writes them to netCDF, and indexed like an unrotated
grid's. Structured arrays get a scalar projection coordinate, named in
their grid_mapping attr as in CF and our netCDF writer, whose attrs
(xorigin, yorigin, angrot) place x/y in the world.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wpbonelli wpbonelli changed the title improve grid dim naming rework grid dims and coordinates Oct 15, 2026
@wpbonelli
wpbonelli merged commit 88983a5 into modflowpy:develop Oct 15, 2026
17 checks passed
@wpbonelli
wpbonelli deleted the grid-dims branch October 15, 2026 17:34
wpbonelli added a commit that referenced this pull request Oct 17, 2026
)

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant