Skip to content

Commit fb14357

Browse files
committed
refactor(datafile): ignore "text" parameter, add attributes from file
1 parent e2a85a3 commit fb14357

File tree

7 files changed

+145
-55
lines changed

7 files changed

+145
-55
lines changed

autotest/test_binaryfile.py

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
See also test_cellbudgetfile.py for similar tests.
44
"""
55

6+
import warnings
67
from itertools import repeat
78

89
import numpy as np
@@ -104,6 +105,8 @@ def test_headfile_build_index(example_data_path):
104105
assert hds.ncol == 20
105106
assert hds.nlay == 3
106107
assert not hasattr(hds, "nper")
108+
assert hds.text == "head"
109+
assert hds.text_bytes == b"HEAD".rjust(16)
107110
assert hds.totalbytes == 10_676_004
108111
assert len(hds.recordarray) == 3291
109112
assert type(hds.recordarray) == np.ndarray
@@ -150,7 +153,80 @@ def test_headfile_build_index(example_data_path):
150153
)
151154

152155

153-
def test_concentration_build_index(example_data_path):
156+
@pytest.mark.parametrize(
157+
"pth, expected",
158+
[
159+
pytest.param(
160+
"mf6-freyberg/freyberg.hds",
161+
{
162+
"precision": "double",
163+
"nlay, nrow, ncol": (1, 40, 20),
164+
"text": "head",
165+
"text_bytes": b"HEAD".ljust(16),
166+
"len(obj)": 1,
167+
},
168+
id="freyberg.hds",
169+
),
170+
pytest.param(
171+
"mf6/create_tests/test_transport/expected_output/gwt_mst03.ucn",
172+
{
173+
"precision": "double",
174+
"nlay, nrow, ncol": (1, 1, 1),
175+
"text": "concentration",
176+
"text_bytes": b"CONCENTRATION".ljust(16),
177+
"len(obj)": 28,
178+
},
179+
id="gwt_mst03.ucn",
180+
),
181+
pytest.param(
182+
"mfusg_test/03A_conduit_unconfined/output/ex3A.cln.hds",
183+
{
184+
"precision": "single",
185+
"nlay, nrow, ncol": (1, 1, 2),
186+
"text": "cln_heads",
187+
"text_bytes": b"CLN HEADS".rjust(16),
188+
"len(obj)": 1,
189+
},
190+
id="ex3A.cln.hds",
191+
),
192+
pytest.param(
193+
"mfusg_test/03A_conduit_unconfined/output/ex3A.ddn",
194+
{
195+
"precision": "single",
196+
"nlay, nrow, ncol": (2, 100, 100),
197+
"text": "drawdown",
198+
"text_bytes": b"DRAWDOWN".rjust(16),
199+
"len(obj)": 2,
200+
},
201+
id="ex3A.ddn",
202+
),
203+
],
204+
)
205+
def test_headfile_examples(example_data_path, pth, expected):
206+
with HeadFile(example_data_path / pth) as obj:
207+
assert obj.precision == expected["precision"]
208+
assert (obj.nlay, obj.nrow, obj.ncol) == expected["nlay, nrow, ncol"]
209+
assert obj.text == expected["text"]
210+
assert obj.text_bytes == expected["text_bytes"]
211+
assert len(obj) == expected["len(obj)"]
212+
213+
214+
@pytest.mark.parametrize(
215+
"pth",
216+
[
217+
"mt3d_test/mf96mt3d/P01/case1b/MT3D001.UCN",
218+
"unstructured/headu.githds",
219+
],
220+
)
221+
def test_not_headfile(example_data_path, pth):
222+
# These examples pass get_headfile_precision, but are not HeadFiles
223+
with pytest.raises(ValueError, match="cannot read file with HeadFile"):
224+
with warnings.catch_warnings():
225+
warnings.simplefilter("ignore")
226+
HeadFile(example_data_path / pth)
227+
228+
229+
def test_ucnfile_build_index(example_data_path):
154230
# test low-level BinaryLayerFile._build_index() method with UCN file
155231
pth = example_data_path / "mt3d_test/mf2005mt3d/P07/MT3D001.UCN"
156232
with UcnFile(pth) as ucn:
@@ -159,6 +235,8 @@ def test_concentration_build_index(example_data_path):
159235
assert ucn.ncol == 21
160236
assert ucn.nlay == 8
161237
assert not hasattr(ucn, "nper")
238+
assert ucn.text == "concentration"
239+
assert ucn.text_bytes == b"CONCENTRATION".ljust(16)
162240
assert ucn.totalbytes == 10_432
163241
assert len(ucn.recordarray) == 8
164242
assert type(ucn.recordarray) == np.ndarray
@@ -296,6 +374,8 @@ def test_headu_file_data(function_tmpdir, example_data_path):
296374
headobj = HeadUFile(fname)
297375
assert isinstance(headobj, HeadUFile)
298376
assert headobj.nlay == 3
377+
assert headobj.text == "headu"
378+
assert headobj.text_bytes == b"HEADU".rjust(16)
299379

300380
# ensure recordarray is has correct data
301381
ra = headobj.recordarray

autotest/test_formattedfile.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def test_headfile_build_index(example_data_path):
2121
assert hds.ncol == 10
2222
assert hds.nlay == 1
2323
assert not hasattr(hds, "nper")
24+
assert hds.text == "head"
2425
assert hds.totalbytes == 1613
2526
assert len(hds.recordarray) == 1
2627
assert type(hds.recordarray) == np.ndarray

flopy/export/utils.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ def _add_output_nc_variable(
161161
)
162162
array[:] = np.nan
163163

164+
if isinstance(text, bytes):
165+
text = text.decode("ascii")
166+
164167
if isinstance(out_obj, ZBNetOutput):
165168
a = np.asarray(out_obj.zone_array, dtype=np.float32)
166169
if mask_array3d is not None:
@@ -179,7 +182,7 @@ def _add_output_nc_variable(
179182
else:
180183
a = out_obj.get_data(totim=t)
181184
except Exception as e:
182-
nme = var_name + text.decode().strip().lower()
185+
nme = var_name + text
183186
estr = f"error getting data for {nme} at time {t}:{e!s}"
184187
if logger:
185188
logger.warn(estr)
@@ -191,7 +194,7 @@ def _add_output_nc_variable(
191194
try:
192195
array[i, :, :, :] = a.astype(np.float32)
193196
except Exception as e:
194-
nme = var_name + text.decode().strip().lower()
197+
nme = var_name + text
195198
estr = f"error assigning {nme} data to array for time {t}:{e!s}"
196199
if logger:
197200
logger.warn(estr)
@@ -209,7 +212,7 @@ def _add_output_nc_variable(
209212

210213
if isinstance(nc, dict):
211214
if text:
212-
var_name = text.decode().strip().lower()
215+
var_name = text
213216
nc[var_name] = array
214217
return nc
215218

@@ -219,7 +222,7 @@ def _add_output_nc_variable(
219222
precision_str = "f4"
220223

221224
if text:
222-
var_name = text.decode().strip().lower()
225+
var_name = text
223226
attribs = {"long_name": var_name}
224227
attribs["coordinates"] = "time layer latitude longitude"
225228
attribs["min"] = mn
@@ -434,7 +437,7 @@ def output_helper(
434437
times,
435438
shape3d,
436439
out_obj,
437-
"concentration",
440+
out_obj.text,
438441
logger=logger,
439442
mask_vals=mask_vals,
440443
mask_array3d=mask_array3d,
@@ -446,7 +449,7 @@ def output_helper(
446449
times,
447450
shape3d,
448451
out_obj,
449-
out_obj.text.decode(),
452+
out_obj.text,
450453
logger=logger,
451454
mask_vals=mask_vals,
452455
mask_array3d=mask_array3d,

flopy/export/vtk.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,14 +1228,12 @@ def add_heads(self, hds, kstpkper=None, masked_values=None):
12281228
kstpkpers = hds.get_kstpkper()
12291229
self._totim = {ki: time for (ki, time) in zip(kstpkpers, times)}
12301230

1231-
text = hds.text.decode()
1232-
12331231
d = dict()
12341232
for ki in kstpkper:
12351233
d[ki] = hds.get_data(ki)
12361234

12371235
self.__transient_output_data = False
1238-
self.add_transient_array(d, name=text, masked_values=masked_values)
1236+
self.add_transient_array(d, name=hds.text, masked_values=masked_values)
12391237
self.__transient_output_data = True
12401238

12411239
def add_cell_budget(

flopy/mf6/utils/binaryfile_utils.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def _get_binary_file_object(self, path, bintype, key):
192192

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

@@ -333,9 +333,7 @@ def _setbinarykeys(self, binarypathdict):
333333

334334
elif key[1] == "DDN":
335335
try:
336-
readddn = bf.HeadFile(
337-
path, text="drawdown", precision="double"
338-
)
336+
readddn = bf.HeadFile(path, precision="double")
339337
self.dataDict[(key[0], key[1], "DRAWDOWN")] = path
340338
readddn.close()
341339

0 commit comments

Comments
 (0)