Repository navigation
support xarray conversion for list-based stress packages - #476
Merged
Merged
Conversation
Package.to_xarray() raised for a list package (CHD, WEL, ...). Put each column on the grid instead, a (per, ...grid) variable per column and per aux name, FILL_DNODATA where a period has no boundary, carrying periods forward as MF6 does. UZF finds its cells through packagedata. Package.from_xarray() is the inverse. What has no grid form raises, pointing to to_dataframe(): two rows in a cell, time-series values, keystring settings (SFR, LAK, MAW, ...), and rows without exactly one cellid. Boundnames are dropped with a warning. attrs_to_dataset still keeps the rows as objects in the dataset's attrs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Rework to_xarray() for list packages so it needs no per-package code: - Rows are dense along their key: the grid for a cellid, the referenced table's sizing dim for a feature number (SFR's nreaches, UZF's nuzfcells), or with grid=True the grid through that table's cellid. - Keystring packages (SFR, LAK, MAW, LKT, TVK, ...) get a variable per setting, carried forward per setting, with a setting's own index (SFR's idv) as a dim and named settings (AUXILIARY) as a variable per name. LAK's settings land on their own keys' dims (nlakes, noutlets). - Array columns get a dim named for their size (EVT's nseg-1). - Boundnames are a coordinate; a time-series value is NaN with its series named in a <variable>_ts coordinate. - Two rows at a position raise, or duplicates="sum"/"first"/"last" merges them. - sparse=True keeps every row; it's the only form of rows with no single key (HFB, MVR, OC). from_xarray() inverts either form. Grid dim names come from one table, GRID_DIMS, shared with Package.to_dataarray. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Since the field functions (modflowpy#463, modflowpy#466), a field records its DFN type as metadata["type"] ("cellid", "array"), not as cellid=/array= flags, and a list's sizing dim is its shape, not dim=. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A column an option turns on (the DFN's when, e.g. EVT's petm0 with SURF_RATE_SPECIFIED) is no longer marked optional, so reading rows back from the xarray form treated it as required. Use _may_be_absent, as item.py does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Say that an entry is one row (an Item), not named row because that's the grid's dim, and that a column named like a Dataset method (CHD's head) needs ds["head"]. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Package.to_xarray()used to raise for list-based stress packages. Support stress period data conversion to an xarray dataset, in one of two forms, dense or sparse.Dense
The dense form is the default, where each field becomes a
(per, ...)variable, laid out along the rows' primary key, which can be:grid=Trueit goes on the grid instead, via cellidThe typical case for this is a variable living on the grid, e.g. CHD
Details:
<var>_tscoordinate (e.g.head_ts).Sparse
sparse=Truekeeps every row, so there are no duplicates to resolve and the round trip is exact. Rows go along anentrydim spanning all periods, with each row's period, key and keystring setting as coordinates, a cellid split by grid dim. Theperiodsattr lists the periods with a block, so an empty block and no block stay distinct. Packages whose rows have no single key (HFB's two cellids, MVR, OC) have only this form. E.g. CHDRound trip conversion
Package.from_xarray()inverts either form. From sparse it's exact. From dense, it writes a period block only where a period differs from the previous one, and a keystring row only where a setting changes, so period data become sparse. That gives the same model, not the same rows: row order and settings that repeat a value aren't kept.