Skip to content

fix(binaryfile): write MF2005 compact records as-is in CellBudgetFile.export() - #2894

Merged
wpbonelli merged 2 commits into
modflowpy:developfrom
wpbonelli:cbc-export-preserve-imeth
Nov 17, 2026
Merged

wpbonelli merged 2 commits into
modflowpy:developfrom
wpbonelli:cbc-export-preserve-imeth

Conversation

@wpbonelli

Copy link
Copy Markdown
Member

#2722 had export() guess each record's imeth from the shape of its data, so MF2005 compact records came out in MF6 form:

  • imeth 2 and 5 lists became imeth 6, with blank model and package names and node2=0
  • imeth 3 and 4 records became imeth 1 arrays with fewer values than their headers said

This was all a consequence of the original motivation described in #2717, which was

converting outputs from non-MF6 flow models to MF6 format, so MF6 transport or tracking models can consume them

But a binary file .export() method is not the place to do this. IMO, export should be faithful to the original and conversion should be a separate piece of functionality, built on .write() instead. This is essentially what the MF6 tests already do. Oops.

So, make export() write compact imeth 2–5 records as they appear in the original file. This is what reverse() already does, incidentally. The one exception is imeth 0, which becomes the equivalent but compact imeth 1.

And fix 2 more bugs from #2722 which this work turned up

  • With the default precision="auto", single-precision files were written as double, fix it
  • Headers used the file's grid shape instead of each record's own dimensions, which made MFUSG CLN exports unreadable

@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

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.8%. Comparing base (556c088) to head (a320dd0).
⚠️ Report is 265 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #2894      +/-   ##
===========================================
+ Coverage     55.5%    73.8%   +18.3%     
===========================================
  Files          644      659      +15     
  Lines       124135   132488    +8353     
===========================================
+ Hits         68947    97875   +28928     
+ Misses       55188    34613   -20575     
Files with missing lines Coverage Δ
flopy/utils/binaryfile/__init__.py 75.2% <100.0%> (-9.3%) ⬇️

... and 585 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:24
wpbonelli and others added 2 commits November 16, 2026 14:25
With the default precision="auto", export() read self.precision as
"auto" and, since it wasn't "single", wrote double precision even for
single precision files. Use the detected realtype instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
export() writes a subset of the file in its own format, as reverse()
does, so write imeth 1-6 records as they appear in the source. imeth 0
(non-compact full array) is still written as the equivalent imeth 1.

Previously imeth 2 and 5 lists were converted to imeth 6 with blank
model/package names and node2 = 0, and imeth 3 and 4 records were
written as imeth 1 with too few values for the header. Reading back an
exported MF2005 file now gives list records with node and q (and aux),
like the source, instead of node, node2 and q.

Also write each record's own ncol, nrow and nlay instead of the
file-level grid shape. They differ for e.g. MFUSG CLN flows, where
export() wrote more values than the header said, so the output could
not be read back. This also covers FLOW-JA-FACE without a special case.

Exporting a whole MF2005 or MFUSG compact file now reproduces it byte
for byte.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wpbonelli
wpbonelli force-pushed the cbc-export-preserve-imeth branch from 8b64750 to a320dd0 Compare November 16, 2026 22:32
@wpbonelli
wpbonelli merged commit 996687f into modflowpy:develop Nov 17, 2026
21 checks passed
@wpbonelli
wpbonelli deleted the cbc-export-preserve-imeth branch November 17, 2026 11:19
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