refactor(datafile): use len(obj) rather than obj.get_nrecords()#2215
Conversation
|
I'm in favor of |
Agreed
Maybe a future flopy could consider static routines to read model outputs e.g. flopy.read_binary_file("path/to/file.cbc", data="FLOW RIGHT FACE")File classes could live on under the hood, e.g. for indexing/caching, precision detection, etc, but it seems ideal to try to hide this and return one table at a time? The user would still need to know which sort of data can be found in which output file, but even that seems like it could be hidden with suitable shortcuts on In the meantime I like |
|
Reply to a few points of discussion...
Xref to #2221 which adds a
With CellBudget files, a somewhat expensive component is to build an index of headers, which is ideally done once, then re-used to get data from more than one component. This process wouldn't work well with static routines.
I was considering keeping/adding |
|
@mwtoews can you resolve the |
5025802 to
2e97b8e
Compare
This PR has a few aims related to data files, including FormattedHeadFile, HeadFile and CellBudgetFile.
These files have a "number of records" property that was implemented with
get_nrecords(). This "length of the object" measure is more naturally done with__len__, i.e.len(headsfile).It is advised to prefer the
len()approach, so instances ofget_nrecords()for these files show a DeprecationWarning.The CellBudgetFile also has a
.nrecordsproperty. It is also advised to show a DeprecationWarning with this property.This PR also fixes a bug with
get_nrecords()shown here:flopy/flopy/utils/datafile.py
Lines 427 to 430 in ea3e475
the bug is that
recordarrayis a structured array (np.ndarray), not a record array, so this always silently returns 0. This bug does not apply to CellBudgetFile, which worked fine and matchesobj.nrecords.Fixing this bug caused this test to fail, since the for-loop was never activated. A "todo" note is added since the reversed header is not the same as the original header.
Another aim of this PR is to re-organize a few CellBudgetFile tests from test_binaryfile.py to test_cellbudgetfile.py. Most of this is copied with perhaps minor simplifications.
Note that none of these changes apply to
flopy.utils.swroutputfile.SwrBudget.get_nrecords(), which returns a tuple.There is also still room for discussion if
get_nrecords()or a dynamic.nrecordsproperty should be preferred overlen(obj). This PR can be adjusted accordingly. Opinions welcome!