Commit e6951c4
authored
feat(binaryfile): add write methods (#2722)
Support writing binary head, budget and grid files, both brand-new via classmethods, and copying existing files to new paths via instance methods.
Add write() classmethods to HeadFile and CellBudgetFile. These write a new file with the given data and return an instance with it open.
There are a few syntax variants for head files.
# dict keyed by (kstp, kper)
hds = HeadFile.write(
'output.hds',
# totim/pertim inferred (dt = 1.0 / time step)
data={
(1, 1): h_t1,
(1, 2): h_t2,
}
)
# list
hds = 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},
])
# array/list with time as first dimension
# defaults to sequential stress periods: (1,1), (1,2), (1,3), ...
heads = [h_t1, h_t2, h_t3] # or np.array([h_t1, h_t2, h_t3])
hds = HeadFile.write('output.hds', heads)
# array/list with custom tdis
hds = HeadFile.write('output.hds', heads, kstpkper=[(1, 1), (2, 1), (3, 1)])
Again there are variants for budget files. The typical case is to use data with a list, where each entry has "text", "kper", "kstp", and "data" entries, and "data" is an array, typically grid-shaped. But it can be convenient to use text and pass a time-indexed dictionary to data to create a file with a single variable.
# just face flows, dict keyed by (kstp, kper)
cbc = 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, list
cbc = CellBudgetFile.write(
'output.cbc',
data=[
{'data': q_t1, 'kstp': 1, 'kper': 1, 'totim': 10.0,
'text': 'FLOW-JA-FACE'},
{'data': ...},
...
]
)
# array/list with time dimension (grid-shaped data like storage)
# defaults to sequential stress periods: (1,1), (1,2), (1,3), ...
# grid dimensions inferred from array shape
storage = [s_t1, s_t2, s_t3] # nlay x nrow x ncol arrays
cbc = CellBudgetFile.write('output.cbc', storage, text='STORAGE')
# for face flows, grid dimensions are required since data is 1D
flows = [q_t1, q_t2] # 1D arrays
cbc = CellBudgetFile.write(
'output.cbc',
flows,
text='FLOW-JA-FACE',
nlay=3, nrow=10, ncol=20
)
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, and CellBudgetFile. These copy the contents of an open file to another, optionally filtering by variable and/or time step, or changing the precision. There is no write() method for MfGrdFile, as its signature would have been long and complicated to accommodate all grid types.
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
from flopy.utils.binaryfile import HeadFile, CellBudgetFile
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
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")1 parent cbc6e86 commit e6951c4
7 files changed
Lines changed: 2643 additions & 4 deletions
File tree
- autotest
- flopy
- mf6/utils
- utils
- binaryfile
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
0 commit comments