Skip to content

Commit cfdedbc

Browse files
authored
refactor(datafile): deprecate list_records() and other list_ methods (#2232)
Now that data files have a .headers data frame property (#2221), there is no need to have methods that print parts of this information to stdout (they return None). This PR deprecates the following: * list_records(): use headers instead * list_unique_records(): use headers[["text", "imeth"]].drop_duplicates() instead * list_unique_packages(to=True/False): use headers.paknam.drop_duplicates() or headers.paknam2.unique() (there are a few ways) The last two only apply to CellBudgetFile. This PR does not apply to flopy.mf6.utils.mfobservation.list_records().
1 parent ae388ef commit cfdedbc

12 files changed

+66
-26
lines changed

.docs/Notebooks/mf6_simple_model_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020

2121
# ### Setup the Notebook Environment
2222

23-
import os
24-
2523
# +
24+
import os
2625
import sys
2726
from pprint import pformat
2827
from tempfile import TemporaryDirectory
@@ -272,8 +271,9 @@
272271
# read the cell budget file
273272
fname = os.path.join(workspace, f"{name}.cbb")
274273
cbb = flopy.utils.CellBudgetFile(fname, precision="double")
275-
cbb.list_records()
274+
cbb.headers.T
276275

276+
# +
277277
flowja = cbb.get_data(text="FLOW-JA-FACE")[0][0, 0, :]
278278
chdflow = cbb.get_data(text="CHD")[0]
279279
# -

.docs/Notebooks/mfusg_conduit_examples.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@
104104
# +
105105
cbb_file = os.path.join(mf.model_ws, "ex3.clncbb")
106106
cbb = flopy.utils.CellBudgetFile(cbb_file)
107-
# cbb.list_records()
107+
cbb.headers
108108

109+
# +
109110
simflow = cbb.get_data(kstpkper=(0, 0), text="GWF")[0]
110111
for i in range(nper - 1):
111112
simflow = np.append(
@@ -297,8 +298,9 @@
297298
# +
298299
cbb_file = os.path.join(mf.model_ws, f"{modelname}.clncb")
299300
cbb = flopy.utils.CellBudgetFile(cbb_file)
300-
# cbb.list_records()
301+
cbb.headers
301302

303+
# +
302304
simflow = cbb.get_data(kstpkper=(0, 0), text="GWF")[0]
303305
for i in range(nper - 1):
304306
simflow = np.append(
@@ -392,8 +394,9 @@
392394
# +
393395
cbb_file = os.path.join(mf.model_ws, f"{modelname}.clncb")
394396
cbb = flopy.utils.CellBudgetFile(cbb_file)
395-
# cbb.list_records()
397+
cbb.headers
396398

399+
# +
397400
simflow = cbb.get_data(kstpkper=(0, 0), text="GWF")[0]
398401
for i in range(nper - 1):
399402
simflow = np.append(
@@ -490,8 +493,9 @@
490493
# +
491494
cbb_file = os.path.join(mf.model_ws, f"{modelname}.clncb")
492495
cbb = flopy.utils.CellBudgetFile(cbb_file)
493-
# cbb.list_records()
496+
cbb.headers
494497

498+
# +
495499
simflow = cbb.get_data(kstpkper=(0, 0), text="GWF")[0]
496500
for i in range(nper - 1):
497501
simflow = np.append(
@@ -575,8 +579,9 @@
575579
# +
576580
cbb_file = os.path.join(mf.model_ws, f"{modelname}.clncb")
577581
cbb = flopy.utils.CellBudgetFile(cbb_file)
578-
# cbb.list_records()
582+
cbb.headers
579583

584+
# +
580585
simflow = cbb.get_data(kstpkper=(0, 0), text="GWF")[0]
581586
for i in range(nper - 1):
582587
simflow = np.append(

.docs/Notebooks/sfrpackage_example.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import glob
3434
import os
3535
import shutil
36-
37-
# +
3836
import sys
3937
from pprint import pformat
4038
from tempfile import TemporaryDirectory
@@ -237,7 +235,7 @@
237235

238236
bpth = os.path.join(path, "test1ss.cbc")
239237
cbbobj = bf.CellBudgetFile(bpth)
240-
cbbobj.list_records()
238+
cbbobj.headers
241239

242240
sfrleak = cbbobj.get_data(text=" STREAM LEAKAGE")[0]
243241
sfrleak[sfrleak == 0] = np.nan # remove zero values

.docs/Notebooks/uzf_example.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@
238238
avail = os.path.isfile(fpth)
239239
if avail:
240240
uzfbdobjct = flopy.utils.CellBudgetFile(fpth)
241-
uzfbdobjct.list_records()
242241
else:
243242
print(f'"{fpth}" is not available')
244243

autotest/regression/test_mf6.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3687,7 +3687,6 @@ def test001a_tharmonic(function_tmpdir, example_data_path):
36873687

36883688
# get expected results
36893689
budget_obj = CellBudgetFile(expected_cbc_file_a, precision="auto")
3690-
budget_obj.list_records()
36913690
budget_frf_valid = np.array(
36923691
budget_obj.get_data(text=" FLOW JA FACE", full3D=True)
36933692
)
@@ -4464,7 +4463,6 @@ def test006_2models_mvr(function_tmpdir, example_data_path):
44644463
expected_cbc_file_a,
44654464
precision="double",
44664465
)
4467-
budget_obj.list_records()
44684466

44694467
# test getting models
44704468
model_dict = sim.model_dict

autotest/test_binaryfile.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ def test_headfile_build_index(example_data_path):
131131
assert hds.kstpkper == [(1, kper + 1) for kper in range(1097)]
132132
np.testing.assert_array_equal(hds.iposarray, np.arange(3291) * 3244 + 44)
133133
assert hds.iposarray.dtype == np.int64
134+
with pytest.deprecated_call(match="use headers instead"):
135+
assert hds.list_records() is None
134136
# check first and last row of data frame
135137
pd.testing.assert_frame_equal(
136138
hds.headers.iloc[[0, -1]],
@@ -186,6 +188,8 @@ def test_concentration_build_index(example_data_path):
186188
assert ucn.kstpkper == [(1, 1)]
187189
np.testing.assert_array_equal(ucn.iposarray, np.arange(8) * 1304 + 44)
188190
assert ucn.iposarray.dtype == np.int64
191+
with pytest.deprecated_call(match="use headers instead"):
192+
assert ucn.list_records() is None
189193
# check first and last row of data frame
190194
pd.testing.assert_frame_equal(
191195
ucn.headers.iloc[[0, -1]],

autotest/test_cbc_full3D.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def cbc_eval_size(cbcobj, nnodes, shape3d):
8686
def cbc_eval_data(cbcobj, shape3d):
8787
cbc_pth = cbcobj.filename
8888
print(f"{cbc_pth}:\n")
89-
cbcobj.list_unique_records()
89+
print(cbcobj.headers[["text", "imeth"]].drop_duplicates())
9090

9191
names = cbcobj.get_unique_record_names(decode=True)
9292
times = cbcobj.get_times()

autotest/test_cellbudgetfile.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,10 +400,14 @@ def test_cellbudgetfile_position(function_tmpdir, zonbud_model_path):
400400

401401
v2 = CellBudgetFile(opth, verbose=True)
402402

403-
try:
404-
v2.list_records()
405-
except:
406-
assert False, f"could not list records on {opth}"
403+
with pytest.deprecated_call(match="use headers instead"):
404+
assert v2.list_records() is None
405+
with pytest.deprecated_call(match=r"drop_duplicates\(\) instead"):
406+
assert v2.list_unique_records() is None
407+
with pytest.deprecated_call(match=r"drop_duplicates\(\) instead"):
408+
assert v2.list_unique_packages(True) is None
409+
with pytest.deprecated_call(match=r"drop_duplicates\(\) instead"):
410+
assert v2.list_unique_packages(False) is None
407411

408412
names = v2.get_unique_record_names(decode=True)
409413

autotest/test_formattedfile.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ def test_headfile_build_index(example_data_path):
4444
assert hds.kstpkper == [(50, 1)]
4545
np.testing.assert_array_equal(hds.iposarray, [98])
4646
assert hds.iposarray.dtype == np.int64
47+
with pytest.deprecated_call(match="use headers instead"):
48+
assert hds.list_records() is None
4749
pd.testing.assert_frame_equal(
4850
hds.headers,
4951
pd.DataFrame(

flopy/utils/binaryfile.py

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -631,11 +631,11 @@ class HeadFile(BinaryLayerFile):
631631
632632
>>> import flopy.utils.binaryfile as bf
633633
>>> hdobj = bf.HeadFile('model.hds', precision='single')
634-
>>> hdobj.list_records()
634+
>>> hdobj.headers
635635
>>> rec = hdobj.get_data(kstpkper=(0, 49))
636636
637637
>>> ddnobj = bf.HeadFile('model.ddn', text='drawdown', precision='single')
638-
>>> ddnobj.list_records()
638+
>>> ddnobj.headers
639639
>>> rec = ddnobj.get_data(totim=100.)
640640
641641
"""
@@ -784,7 +784,7 @@ class UcnFile(BinaryLayerFile):
784784
785785
>>> import flopy.utils.binaryfile as bf
786786
>>> ucnobj = bf.UcnFile('MT3D001.UCN', precision='single')
787-
>>> ucnobj.list_records()
787+
>>> ucnobj.headers
788788
>>> rec = ucnobj.get_data(kstpkper=(0, 0))
789789
790790
"""
@@ -851,7 +851,7 @@ class HeadUFile(BinaryLayerFile):
851851
852852
>>> import flopy.utils.binaryfile as bf
853853
>>> hdobj = bf.HeadUFile('model.hds')
854-
>>> hdobj.list_records()
854+
>>> hdobj.headers
855855
>>> usgheads = hdobj.get_data(kstpkper=(0, 49))
856856
857857
"""
@@ -1001,7 +1001,7 @@ class CellBudgetFile:
10011001
10021002
>>> import flopy.utils.binaryfile as bf
10031003
>>> cbb = bf.CellBudgetFile('mymodel.cbb')
1004-
>>> cbb.list_records()
1004+
>>> cbb.headers
10051005
>>> rec = cbb.get_data(kstpkper=(0,0), text='RIVER LEAKAGE')
10061006
10071007
"""
@@ -1458,7 +1458,14 @@ def _find_paknam(self, paknam, to=False):
14581458
def list_records(self):
14591459
"""
14601460
Print a list of all of the records in the file
1461+
1462+
.. deprecated:: 3.8.0
1463+
Use :attr:`headers` instead.
14611464
"""
1465+
warnings.warn(
1466+
"list_records() is deprecated; use headers instead.",
1467+
DeprecationWarning,
1468+
)
14621469
for rec in self.recordarray:
14631470
if isinstance(rec, bytes):
14641471
rec = rec.decode()
@@ -1467,7 +1474,15 @@ def list_records(self):
14671474
def list_unique_records(self):
14681475
"""
14691476
Print a list of unique record names
1477+
1478+
.. deprecated:: 3.8.0
1479+
Use `headers[["text", "imeth"]].drop_duplicates()` instead.
14701480
"""
1481+
warnings.warn(
1482+
"list_unique_records() is deprecated; use "
1483+
'headers[["text", "imeth"]].drop_duplicates() instead.',
1484+
DeprecationWarning,
1485+
)
14711486
print("RECORD IMETH")
14721487
print(22 * "-")
14731488
for rec, imeth in zip(self.textlist, self.imethlist):
@@ -1478,7 +1493,17 @@ def list_unique_records(self):
14781493
def list_unique_packages(self, to=False):
14791494
"""
14801495
Print a list of unique package names
1496+
1497+
.. deprecated:: 3.8.0
1498+
Use `headers.paknam.drop_duplicates()` or
1499+
`headers.paknam2.drop_duplicates()` instead.
14811500
"""
1501+
warnings.warn(
1502+
"list_unique_packages() is deprecated; use "
1503+
"headers.paknam.drop_duplicates() or "
1504+
"headers.paknam2.drop_duplicates() instead",
1505+
DeprecationWarning,
1506+
)
14821507
for rec in self._unique_package_names(to):
14831508
if isinstance(rec, bytes):
14841509
rec = rec.decode()

flopy/utils/datafile.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,14 @@ def _build_index(self):
430430
def list_records(self):
431431
"""
432432
Print a list of all of the records in the file
433-
obj.list_records()
434433
434+
.. deprecated:: 3.8.0
435+
Use :attr:`headers` instead.
435436
"""
437+
warnings.warn(
438+
"list_records() is deprecated; use headers instead.",
439+
DeprecationWarning,
440+
)
436441
for header in self.recordarray:
437442
print(header)
438443
return

flopy/utils/formattedfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ class FormattedHeadFile(FormattedLayerFile):
361361
362362
>>> import flopy.utils.formattedfile as ff
363363
>>> hdobj = ff.FormattedHeadFile('model.fhd', precision='single')
364-
>>> hdobj.list_records()
364+
>>> hdobj.headers
365365
>>> rec = hdobj.get_data(kstpkper=(0, 49))
366366
>>> rec2 = ddnobj.get_data(totim=100.)
367367

0 commit comments

Comments
 (0)