Skip to content

Commit

Permalink
refactor: deprecate unused flopy.utils.binaryfile.binaryread_struct
Browse files Browse the repository at this point in the history
  • Loading branch information
mwtoews committed May 30, 2024
1 parent 31955a7 commit 155b222
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
17 changes: 17 additions & 0 deletions autotest/test_binaryfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,20 @@ def test_read_mf6_budgetfile(example_data_path):
assert len(rch_zone_1) == 120 * 3 + 1
assert len(rch_zone_2) == 120 * 3 + 1
assert len(rch_zone_3) == 120 * 3 + 1


def test_deprecated_binaryread_struct(example_data_path):
pth = example_data_path / "freyberg" / "freyberg.githds"
with open(pth, "rb") as fp:
with pytest.deprecated_call():
res = flopy.utils.binaryfile.binaryread_struct(fp, np.int32, 2)
np.testing.assert_array_equal(res, np.array([1, 1], np.int32))
with pytest.deprecated_call():
res = flopy.utils.binaryfile.binaryread_struct(fp, np.float32, 2)
np.testing.assert_array_equal(res, np.array([10, 10], np.float32))
with pytest.deprecated_call():
res = flopy.utils.binaryfile.binaryread_struct(fp, str)
assert res == b" HEAD"
with pytest.deprecated_call():
res = flopy.utils.binaryfile.binaryread_struct(fp, np.int32)
assert res == 20
30 changes: 24 additions & 6 deletions flopy/utils/binaryfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,16 @@ def binaryread_struct(file, vartype, shape=(1,), charlen=16):
cannot be returned, only multi-character strings. Shape has no
affect on strings.
.. deprecated:: 3.8.0
Use :meth:`binaryread` instead.
"""
import struct

import numpy as np
warnings.warn(
"binaryread_struct() is deprecated; use binaryread() instead.",
DeprecationWarning,
)

# store the mapping from type to struct format (fmt)
typefmtd = {np.int32: "i", np.float32: "f", np.float64: "d"}
Expand Down Expand Up @@ -306,8 +312,22 @@ def binaryread_struct(file, vartype, shape=(1,), charlen=16):

def binaryread(file, vartype, shape=(1,), charlen=16):
"""
Uses numpy to read from binary file. This was found to be faster than the
struct approach and is used as the default.
Read text, a scalar value, or an array of values from a binary file.
Parameters
----------
file : file object
is an open file object
vartype : type
is the return variable type: str, numpy.int32, numpy.float32,
or numpy.float64
shape : tuple
is the shape of the returned array (shape(1, ) returns a single
value) for example, shape = (nlay, nrow, ncol)
charlen : int
is the length of the text string. Note that string arrays
cannot be returned, only multi-character strings. Shape has no
affect on strings.
"""

Expand All @@ -318,9 +338,7 @@ def binaryread(file, vartype, shape=(1,), charlen=16):
# find the number of values
nval = np.prod(shape)
result = np.fromfile(file, vartype, nval)
if nval == 1:
result = result # [0]
else:
if nval != 1:
result = np.reshape(result, shape)
return result

Expand Down

0 comments on commit 155b222

Please sign in to comment.