Skip to content

feat(binaryfile): add write methods#2722

Open
wpbonelli wants to merge 21 commits intomodflowpy:developfrom
wpbonelli:binary-write
Open

feat(binaryfile): add write methods#2722
wpbonelli wants to merge 21 commits intomodflowpy:developfrom
wpbonelli:binary-write

Conversation

@wpbonelli
Copy link
Member

@wpbonelli wpbonelli commented Feb 27, 2026

Support writing binary head, budget and grid files.

Class methods

Add write() classmethods to

  • HeadFile
  • CellBudgetFile

These write a new file with the given data and return an instance with it open.

Head files

HeadFile.write(
    'output.hds',
    # totim/pertim inferred (dt = 1.0 / time step)
    data={
        (1, 1): h_t1,
        (1, 2): h_t2,
    }
)

# alternative syntax
HeadFile.write('output.hds', data=[
    {'data': h_t2, 'kstp': 1, 'kper': 1, 'totim': 10.0, 'pertim': 10.0},
    {'data': h_t2, 'kstp': 1, 'kper': 2, 'totim': 20.0, 'pertim': 10.0},
])

Budget files

# just face flows
CellBudgetFile.write(
    'output.cbc',
    text='FLOW-JA-FACE',
    nlay=3,
    nrow=10,
    ncol=20,
    data={
        (1, 1): q_t1,
        (1, 2): q_t2,
    }
)

# multiple variables
CellBudgetFile.write(
    'output.cbc',
    data=[
        {'data': q_t1, 'kstp': 1, 'kper': 1, 'totim': 10.0,
         'text': 'FLOW-JA-FACE'},
        {'data': ...},
        ...
    ]
)

If only face flows are provided, the grid shape must be specified with nlay/nrow/ncol, nlay/ncpl, or nnodes. If grid-shaped variables are provided, the grid's shape will be inferred.

Instance methods

Add instance export() methods to

  • MfGrdFile
  • HeadFile
  • CellBudgetFile

These copy the contents of an open file to another.

Grid files

from flopy.mf6.utils.binarygrid_util import MfGrdFile

grb = MfGrdFile("model.grb")
grb.export("copy.grb") # copy to another file
grb.export("diff_prec.grb", precision="single") # different precision

Head/budget files

from flopy.utils.binaryfile import HeadFile, CellBudgetFile

# head file
hds = HeadFile("model.hds")
hds.export("copy.hds")  # copy to another file
hds.export("filtered.hds", kstpkper=[(1, 0), (1, 1)])  # filter time steps
hds.export("diff_prec.hds", precision="single")  # different precision

# budget file
cbc = CellBudgetFile("model.cbc")
cbc.export("copy.cbc")
cbc.export("flowja.cbc", text="FLOW-JA-FACE")
cbc.export("bndpkgs.cbc", text=["STORAGE", "CONSTANT HEAD"])
cbc.export("filtered.cbc", kstpkper=[(1, 0)], text="FLOW-JA-FACE")

Close #2717

@wpbonelli wpbonelli added this to the 3.11 milestone Feb 27, 2026
@codecov
Copy link

codecov bot commented Feb 27, 2026

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff             @@
##           develop   #2722      +/-   ##
==========================================
- Coverage     55.5%   36.0%   -19.6%     
==========================================
  Files          644     361     -283     
  Lines       124135   66411   -57724     
==========================================
- Hits         68947   23921   -45026     
+ Misses       55188   42490   -12698     

see 560 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 February 27, 2026 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feature: support writing MF6 binary output files

1 participant