Skip to content

fix(binaryfile): export all records in CellBudgetFile.export() - #2893

Merged
wpbonelli merged 4 commits into
modflowpy:developfrom
wpbonelli:fix-cbc-export-multi-package
Nov 16, 2026
Merged

wpbonelli merged 4 commits into
modflowpy:developfrom
wpbonelli:fix-cbc-export-multi-package

Conversation

@wpbonelli

@wpbonelli wpbonelli commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

CellBudgetFile.export() writes at most one record per budget text per time step, so it silently drops every package after the first when a file has more than one package of the same type. I overlooked this in #2722.

Go through records in file order, keeping those that match text and kstpkper, and write each one from its own header and data. Before, headers and data came from separate lookups that only matched because both took the first result. Consequences:

  • All matching records are exported, in file order. Before, if text was a list, records within each time step were written in the order of that list rather than the order of the file.
  • Budget text, model names, package names are copied from the header. Before, text was re-padded with spaces on the right instead of the left. Flopy and MF6 loading logic doesnt depend on the padding, so only custom code should notice this..
  • imeth is read from the header rather than guessed from the data

wpbonelli and others added 4 commits November 16, 2026 12:32
export() wrote only the first record per budget text per time step, so
budget files with multiple packages of the same type (e.g. several RCH
packages) lost all but the first package's records. Records were also
written grouped by the requested text order rather than in file order,
and imeth was inferred from the data type rather than taken from the
header.

Iterate over records in file order, as reverse() does, and write each
record from its own header and data. Copy text and package names from
the header verbatim, preserving their justification. Raise
NotImplementedError for imeth values other than 0, 1 and 6 instead of
writing them incorrectly.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The previous commit raised NotImplementedError for imeth values other
than 0, 1 and 6, which broke exporting MF2005/MFUSG compact budget
files with list-based packages (imeth 2 and 5). These were previously
exported as imeth 6, and still are. imeth 3 and 4 records, which were
previously written incorrectly, are now expanded to full 3D arrays and
written as imeth 1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Leave imeth 3 and 4 records as they were exported before, as imeth 1,
so this change stays limited to exporting multiple packages. Writing
MF2005 compact records (imeth 2-5) unchanged is left for a follow-up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The text subset test passed text in file order, so it didn't check that
records are written in file order regardless of the order of text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wpbonelli wpbonelli added this to the 3.12.1 milestone Nov 16, 2026
@wpbonelli wpbonelli added the bug label Nov 16, 2026
@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.62500% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.0%. Comparing base (556c088) to head (027c1cd).
⚠️ Report is 264 commits behind head on develop.

Files with missing lines Patch % Lines
flopy/utils/binaryfile/__init__.py 90.6% 6 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2893      +/-   ##
===========================================
+ Coverage     55.5%    72.0%   +16.5%     
===========================================
  Files          644      659      +15     
  Lines       124135   132478    +8343     
===========================================
+ Hits         68947    95488   +26541     
+ Misses       55188    36990   -18198     
Files with missing lines Coverage Δ
flopy/utils/binaryfile/__init__.py 74.8% <90.6%> (-9.7%) ⬇️

... and 584 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@wpbonelli
wpbonelli marked this pull request as ready for review November 16, 2026 22:21
@wpbonelli
wpbonelli merged commit 61b3d28 into modflowpy:develop Nov 16, 2026
22 of 26 checks passed
@wpbonelli
wpbonelli deleted the fix-cbc-export-multi-package branch November 16, 2026 22:22
roh-sgm added a commit to roh-sgm/flopy that referenced this pull request Nov 19, 2026
Integrate modflowpy/flopy develop at
996687f (112 commits since merge base
6888032) into the USG-Transport branch.
The merged tree was built and reviewed separately. Conflicts (17 files,
50 blocks) were resolved block by block, without a global ours/theirs
strategy.

Upstream changes kept in conflicted files:
- util_list: additional_data branch for MNW1 (modflowpy#2752). The CLN branch
  keeps the fork behaviour (ITMPCLN written as 0 when there is no CLN
  data).
- mfusg fmt_string: blank separator in free format (modflowpy#2789).
- RCH selev/iznrch test (modflowpy#2763) and .gitignore entries.
All other upstream changes merged without conflict, including the
CellBudgetFile.export() fixes (modflowpy#2893, modflowpy#2894). The fork's label filtering
in BinaryLayerFile and HeadUFile is kept; it does not overlap those
changes.

Fork behaviour kept in conflicted files:
- mfusglpf: Richards arrays written when BAS RICHARDS turns LAYCON
  into 5 (already includes the modflowpy#2812 fix).
- mfusgoc: ATS options written through the ATS1RP conversion (covers the
  modflowpy#2770 fix).
- mfusgrch: INCONC written as a keyword without a value.
- mfusg/__init__.py, USG-Transport tests and transport tutorials: fork
  versions (engine selector also accepts mfusgt).

Deliberate divergence from upstream: flopy/mf6/modflow/mfpts.py and
flopy/mf6/utils/reference.py, removed upstream (modflowpy#2834, modflowpy#2795), are kept
unchanged in this merge. Nothing imports them any more and ModflowPts is
no longer exported by flopy.mf6. Removing them is a separate decision.

Validation was local only (macOS arm64). Pytest subsets of the merged
tree show no outcome change against the fork for common test IDs.
Differences against upstream are known open items (STR, HFB check,
free_format_npl). CellBudgetFile.export() was checked against an
independent reader on synthetic files (imeth 0 to 6, auxiliary
variables, repeated labels, single and double precision) and on
USG-Transport budget files. USG-Transport engine cases pass with local
2.8.1 and 2.7.0 builds, and binary outputs match the fork. Not
validated: CI workflows (pixi and USG-Transport steps), notebooks,
Windows and Linux, MF2005/MF6 engine runs (including the upstream
multiple-package export tests), and Voronoi tests that need scipy.
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