modflowpy / flopy

A Python package to create, run, and post-process MODFLOW-based models.
https://flopy.readthedocs.io
Other
506 stars 306 forks source link

fix(binaryread): raise/handle EOFError, deprecate vartype=str #2226

Closed mwtoews closed 2 months ago

mwtoews commented 2 months ago

This fixes issues while reading some binaryfiles with auto precision detection, and also modernizes a few aspects of flopy.utils.binaryfile left-over from python2.

There are two changes to flopy.utils.binaryfile.binaryread():

  1. Raises EOFError if attempting to read data beyond the end-of-file
  2. Deprecate vartype=str, since bytes is the the return type with Python3

Other refactors:


Examples of issues

CellBudgetFile

Reading a cell budget file that contains imeth=5 with precision="auto" (the default):

python -c "from flopy.utils import *; print(CellBudgetFile('examples/data/preserve_unitnums/testsfr2.ghb.cbc').headers)"

previously this will hang indefinitely since it is attempting to read data at the end of the file without raising any exception. This example is fixed with this PR.

HeadFile

And this is not so much a fix, but a less confusing error:

python -c "from flopy.utils import *; HeadFile('examples/data/mf6/create_tests/test_transport/expected_output/gwt_mst03.ucn')"

the exception is changed from:

IndexError: index 0 is out of bounds for axis 0 with size 0

to:

EOFError

I have some pending changes to resolve this issue, due after this one.

codecov[bot] commented 2 months ago

Codecov Report

Attention: Patch coverage is 96.07843% with 2 lines in your changes missing coverage. Please review.

Project coverage is 70.8%. Comparing base (e2d16df) to head (ac52c31).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## develop #2226 +/- ## ========================================= + Coverage 68.0% 70.8% +2.8% ========================================= Files 294 294 Lines 58880 59026 +146 ========================================= + Hits 40061 41826 +1765 + Misses 18819 17200 -1619 ``` | [Files](https://app.codecov.io/gh/modflowpy/flopy/pull/2226?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=modflowpy) | Coverage Δ | | |---|---|---| | [flopy/utils/binaryfile.py](https://app.codecov.io/gh/modflowpy/flopy/pull/2226?src=pr&el=tree&filepath=flopy%2Futils%2Fbinaryfile.py&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=modflowpy#diff-ZmxvcHkvdXRpbHMvYmluYXJ5ZmlsZS5weQ==) | `81.5% <96.0%> (+1.7%)` | :arrow_up: | ... and [212 files with indirect coverage changes](https://app.codecov.io/gh/modflowpy/flopy/pull/2226/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=modflowpy)
langevin-usgs commented 2 months ago

Thanks for this @mwtoews. The binary readers have been needing some updates. Appreciate you taking it on. Time to get past the python2 hangover.