I have a working model with output. I'm trying to take advantage of flopy in interrogating the results. In specific, I would like to be able to generate (on the fly) zone budget information for specific cells. I can of course go to the trouble of building the zone files which I am interested in, but I was hoping to be able to extract the results as I need them without having to go through that intermediate step.
I'm looking for a workflow that I can use in Python to extract net flow information from the CBB (already generated) for specific cells only in the Python console (would rather avoid the legacy style of generating zone files, importing, then extracting).
EDIT
Ran into problems trying to get the utility to work at all. Here's the latest attempt. Can't tell if flopy is choking on the CBB itself or if the zone I've provided is giving it problems.
The model is 7 layers, 196 Rows, 241 Columns. I am trying to extract Layer=3, Row=58, Col=30. The list object I created for the zone is here:
zon_lst = []
for lay in range(7):
for row in range(196):
for col in range(241):
if lay == 2 and row == 57 and col == 29:
zon_lst.append(1)
else:
zon_lst.append(0)
I then wrote this to a file using the following:
def chunk_list(alist, n):
for i in range(0, len(alist), n):
yield alist[i:i + n]
def zon_gen(mylst, rows, cols, file_cols):
# Assumes integers for zonation file
frmt = '{:>3}'
list_by_lay = chunk_list(mylst, rows * cols)
astr = '\n'.join(['\n'.join([''.join([frmt.format(i) for i in seq]) for seq in chunk_list(layer, file_cols)]) for layer in list_by_lay])
return astr
zon_str = zon_gen(zon_lst, 196, 241, 10)
with open('t_26.zon', 'w+') as file:
file.write('# For Scoping Work\n')
file.write('1\n')
file.write('t_26\n')
file.write('INTERNAL 1 (10I3) 1\n')
file.write(zon_str)
Then I build my modflow model for the zone budget class/methods:
import flopy
mf = flopy.modflow.Modflow(modelname='t_26_scope', version='mf2k')
zon = flopy.modflow.ModflowZon.load(r"t_26.zon",mf,nrow=196, ncol=241)
zb = flopy.utils.zonbud.ZoneBudget(r"P2Rv8.2_1000yr.cbb", zon)
All of this runs fine up until the very last command, where I get the following error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\ProgramData\Miniconda3\envs\ca_cie\lib\site-packages\flopy\utils\zonbud.py", line 53, in __init__
self.cbc = CellBudgetFile(cbc_file)
File "C:\ProgramData\Miniconda3\envs\ca_cie\lib\site-packages\flopy\utils\binaryfile.py", line 618, in __init__
self._build_index()
File "C:\ProgramData\Miniconda3\envs\ca_cie\lib\site-packages\flopy\utils\binaryfile.py", line 708, in _build_index
self._skip_record(header)
File "C:\ProgramData\Miniconda3\envs\ca_cie\lib\site-packages\flopy\utils\binaryfile.py", line 768, in _skip_record
self.file.seek(nbytes, 1)
OSError: [Errno 22] Invalid argument
Please bare in mind that I am still interested in skipping the file writing portion. I included it to show my work up to this point