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 authored May 31, 2024
1 parent 31955a7 commit bbabf86
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
32 changes: 32 additions & 0 deletions autotest/test_binaryfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,38 @@ def zonbud_model_path(example_data_path):
return example_data_path / "zonbud_examples"


def test_binaryread(example_data_path):
# test low-level binaryread() method
pth = example_data_path / "freyberg" / "freyberg.githds"
with open(pth, "rb") as fp:
res = flopy.utils.binaryfile.binaryread(fp, np.int32, 2)
np.testing.assert_array_equal(res, np.array([1, 1], np.int32))
res = flopy.utils.binaryfile.binaryread(fp, np.float32, 2)
np.testing.assert_array_equal(res, np.array([10, 10], np.float32))
res = flopy.utils.binaryfile.binaryread(fp, str)
assert res == b" HEAD"
res = flopy.utils.binaryfile.binaryread(fp, np.int32)
assert res == 20


def test_deprecated_binaryread_struct(example_data_path):
# similar to test_binaryread(), but check the calls are deprecated
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


def test_binaryfile_writeread(function_tmpdir, nwt_model_path):
model = "Pr3_MFNWT_lower.nam"
ml = flopy.modflow.Modflow.load(
Expand Down
32 changes: 25 additions & 7 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,21 +312,33 @@ 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, default (1,)
is the shape of the returned array (shape(1, ) returns a single
value) for example, shape = (nlay, nrow, ncol)
charlen : int, default 16
is the length of the text string. Note that string arrays
cannot be returned, only multi-character strings. Shape has no
affect on strings.
"""

# read a string variable of length charlen
if vartype == str:
result = file.read(charlen * 1)
result = file.read(charlen)
else:
# 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 bbabf86

Please sign in to comment.