Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(datafile): ignore "text" parameter, add attributes from file #2231

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion autotest/test_binaryfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
See also test_cellbudgetfile.py for similar tests.
"""

import warnings
from itertools import repeat

import numpy as np
Expand Down Expand Up @@ -104,6 +105,8 @@ def test_headfile_build_index(example_data_path):
assert hds.ncol == 20
assert hds.nlay == 3
assert not hasattr(hds, "nper")
assert hds.text == "head"
assert hds.text_bytes == b"HEAD".rjust(16)
assert hds.totalbytes == 10_676_004
assert len(hds.recordarray) == 3291
assert type(hds.recordarray) == np.ndarray
Expand Down Expand Up @@ -150,7 +153,80 @@ def test_headfile_build_index(example_data_path):
)


def test_concentration_build_index(example_data_path):
@pytest.mark.parametrize(
"pth, expected",
[
pytest.param(
"mf6-freyberg/freyberg.hds",
{
"precision": "double",
"nlay, nrow, ncol": (1, 40, 20),
"text": "head",
"text_bytes": b"HEAD".ljust(16),
"len(obj)": 1,
},
id="freyberg.hds",
),
pytest.param(
"mf6/create_tests/test_transport/expected_output/gwt_mst03.ucn",
{
"precision": "double",
"nlay, nrow, ncol": (1, 1, 1),
"text": "concentration",
"text_bytes": b"CONCENTRATION".ljust(16),
"len(obj)": 28,
},
id="gwt_mst03.ucn",
),
pytest.param(
"mfusg_test/03A_conduit_unconfined/output/ex3A.cln.hds",
{
"precision": "single",
"nlay, nrow, ncol": (1, 1, 2),
"text": "cln_heads",
"text_bytes": b"CLN HEADS".rjust(16),
"len(obj)": 1,
},
id="ex3A.cln.hds",
),
pytest.param(
"mfusg_test/03A_conduit_unconfined/output/ex3A.ddn",
{
"precision": "single",
"nlay, nrow, ncol": (2, 100, 100),
"text": "drawdown",
"text_bytes": b"DRAWDOWN".rjust(16),
"len(obj)": 2,
},
id="ex3A.ddn",
),
],
)
def test_headfile_examples(example_data_path, pth, expected):
with HeadFile(example_data_path / pth) as obj:
assert obj.precision == expected["precision"]
assert (obj.nlay, obj.nrow, obj.ncol) == expected["nlay, nrow, ncol"]
assert obj.text == expected["text"]
assert obj.text_bytes == expected["text_bytes"]
assert len(obj) == expected["len(obj)"]


@pytest.mark.parametrize(
"pth",
[
"mt3d_test/mf96mt3d/P01/case1b/MT3D001.UCN",
"unstructured/headu.githds",
],
)
def test_not_headfile(example_data_path, pth):
# These examples pass get_headfile_precision, but are not HeadFiles
with pytest.raises(ValueError, match="cannot read file with HeadFile"):
with warnings.catch_warnings():
warnings.simplefilter("ignore")
HeadFile(example_data_path / pth)


def test_ucnfile_build_index(example_data_path):
# test low-level BinaryLayerFile._build_index() method with UCN file
pth = example_data_path / "mt3d_test/mf2005mt3d/P07/MT3D001.UCN"
with UcnFile(pth) as ucn:
Expand All @@ -159,6 +235,8 @@ def test_concentration_build_index(example_data_path):
assert ucn.ncol == 21
assert ucn.nlay == 8
assert not hasattr(ucn, "nper")
assert ucn.text == "concentration"
assert ucn.text_bytes == b"CONCENTRATION".ljust(16)
assert ucn.totalbytes == 10_432
assert len(ucn.recordarray) == 8
assert type(ucn.recordarray) == np.ndarray
Expand Down Expand Up @@ -296,6 +374,8 @@ def test_headu_file_data(function_tmpdir, example_data_path):
headobj = HeadUFile(fname)
assert isinstance(headobj, HeadUFile)
assert headobj.nlay == 3
assert headobj.text == "headu"
assert headobj.text_bytes == b"HEADU".rjust(16)

# ensure recordarray is has correct data
ra = headobj.recordarray
Expand Down
1 change: 1 addition & 0 deletions autotest/test_formattedfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def test_headfile_build_index(example_data_path):
assert hds.ncol == 10
assert hds.nlay == 1
assert not hasattr(hds, "nper")
assert hds.text == "head"
assert hds.totalbytes == 1613
assert len(hds.recordarray) == 1
assert type(hds.recordarray) == np.ndarray
Expand Down
15 changes: 9 additions & 6 deletions flopy/export/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ def _add_output_nc_variable(
)
array[:] = np.nan

if isinstance(text, bytes):
text = text.decode("ascii")

if isinstance(out_obj, ZBNetOutput):
a = np.asarray(out_obj.zone_array, dtype=np.float32)
if mask_array3d is not None:
Expand All @@ -179,7 +182,7 @@ def _add_output_nc_variable(
else:
a = out_obj.get_data(totim=t)
except Exception as e:
nme = var_name + text.decode().strip().lower()
nme = var_name + text
estr = f"error getting data for {nme} at time {t}:{e!s}"
if logger:
logger.warn(estr)
Expand All @@ -191,7 +194,7 @@ def _add_output_nc_variable(
try:
array[i, :, :, :] = a.astype(np.float32)
except Exception as e:
nme = var_name + text.decode().strip().lower()
nme = var_name + text
estr = f"error assigning {nme} data to array for time {t}:{e!s}"
if logger:
logger.warn(estr)
Expand All @@ -209,7 +212,7 @@ def _add_output_nc_variable(

if isinstance(nc, dict):
if text:
var_name = text.decode().strip().lower()
var_name = text
nc[var_name] = array
return nc

Expand All @@ -219,7 +222,7 @@ def _add_output_nc_variable(
precision_str = "f4"

if text:
var_name = text.decode().strip().lower()
var_name = text
attribs = {"long_name": var_name}
attribs["coordinates"] = "time layer latitude longitude"
attribs["min"] = mn
Expand Down Expand Up @@ -434,7 +437,7 @@ def output_helper(
times,
shape3d,
out_obj,
"concentration",
out_obj.text,
logger=logger,
mask_vals=mask_vals,
mask_array3d=mask_array3d,
Expand All @@ -446,7 +449,7 @@ def output_helper(
times,
shape3d,
out_obj,
out_obj.text.decode(),
out_obj.text,
logger=logger,
mask_vals=mask_vals,
mask_array3d=mask_array3d,
Expand Down
4 changes: 1 addition & 3 deletions flopy/export/vtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -1228,14 +1228,12 @@ def add_heads(self, hds, kstpkper=None, masked_values=None):
kstpkpers = hds.get_kstpkper()
self._totim = {ki: time for (ki, time) in zip(kstpkpers, times)}

text = hds.text.decode()

d = dict()
for ki in kstpkper:
d[ki] = hds.get_data(ki)

self.__transient_output_data = False
self.add_transient_array(d, name=text, masked_values=masked_values)
self.add_transient_array(d, name=hds.text, masked_values=masked_values)
self.__transient_output_data = True

def add_cell_budget(
Expand Down
6 changes: 2 additions & 4 deletions flopy/mf6/utils/binaryfile_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def _get_binary_file_object(self, path, bintype, key):

elif bintype == "DDN":
try:
return bf.HeadFile(path, text="drawdown", precision="double")
return bf.HeadFile(path, precision="double")
except AssertionError:
raise AssertionError(f"{self.dataDict[key]} does not exist")

Expand Down Expand Up @@ -333,9 +333,7 @@ def _setbinarykeys(self, binarypathdict):

elif key[1] == "DDN":
try:
readddn = bf.HeadFile(
path, text="drawdown", precision="double"
)
readddn = bf.HeadFile(path, precision="double")
self.dataDict[(key[0], key[1], "DRAWDOWN")] = path
readddn.close()

Expand Down
Loading
Loading