Skip to content

Commit 19cd49c

Browse files
committed
use setdefault, hoist available_terms outside loop, remove redundant conditions
1 parent ac7c6ff commit 19cd49c

2 files changed

Lines changed: 23 additions & 55 deletions

File tree

flopy/utils/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
get_modflow = get_modflow_module.run_main
3232
from .classic_to_mf6 import (
3333
ClassicMfToMf6Converter,
34-
NwtToMf6Converter, # backward-compatible alias
3534
get_icelltype_from_laycon,
3635
get_icelltype_from_laytyp,
3736
)

flopy/utils/classic_to_mf6.py

Lines changed: 23 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,9 @@ def _build_budget_header_lookup(self):
527527
for that time step. The compact budget header stores delt, pertim,
528528
and totim directly, so no arithmetic is needed to recover them.
529529
"""
530-
seen = set()
531530
result = {}
532531
for rec in self.cbc_obj.recordarray:
533-
key = (int(rec["kstp"]) - 1, int(rec["kper"]) - 1)
534-
if key not in seen:
535-
result[key] = rec
536-
seen.add(key)
532+
result.setdefault((int(rec["kstp"]) - 1, int(rec["kper"]) - 1), rec)
537533
return result
538534

539535
def _write_budget(self, filename, precision, verbose):
@@ -548,6 +544,13 @@ def _write_budget(self, filename, precision, verbose):
548544
records = []
549545
header_lookup = self._build_budget_header_lookup()
550546

547+
# for 1D/2D models, some face-flow directions may be absent
548+
available_terms = {
549+
t.decode().strip() for t in self.cbc_obj.get_unique_record_names()
550+
}
551+
if verbose:
552+
print(f" Available budget terms: {available_terms}")
553+
551554
for kstpkper in self.kstpkper:
552555
kstp, kper = kstpkper
553556
rec = header_lookup[kstpkper]
@@ -560,55 +563,21 @@ def _write_budget(self, filename, precision, verbose):
560563

561564
head = self.hds_obj.get_data(kstpkper=kstpkper)
562565

563-
if verbose:
564-
print(
565-
f" Available budget terms: "
566-
f"{self.cbc_obj.get_unique_record_names()}"
567-
)
568-
569-
# for 1D/2D models, some directions may be missing
570-
available_terms = [
571-
t.decode().strip() for t in self.cbc_obj.get_unique_record_names()
572-
]
573-
574-
if "FLOW RIGHT FACE" in available_terms:
575-
frf_data = self.cbc_obj.get_data(
576-
text="FLOW RIGHT FACE", kstpkper=kstpkper
577-
)
578-
if verbose:
579-
print(
580-
f" FLOW RIGHT FACE: {type(frf_data)}, "
581-
f"len={len(frf_data) if frf_data else 0}"
582-
)
583-
frf = frf_data[0] if frf_data and len(frf_data) > 0 else None
584-
else:
585-
frf = None
586-
587-
if "FLOW FRONT FACE" in available_terms:
588-
fff_data = self.cbc_obj.get_data(
589-
text="FLOW FRONT FACE", kstpkper=kstpkper
590-
)
591-
if verbose:
592-
print(
593-
f" FLOW FRONT FACE: {type(fff_data)}, "
594-
f"len={len(fff_data) if fff_data else 0}"
595-
)
596-
fff = fff_data[0] if fff_data and len(fff_data) > 0 else None
597-
else:
598-
fff = None
599-
600-
if "FLOW LOWER FACE" in available_terms:
601-
flf_data = self.cbc_obj.get_data(
602-
text="FLOW LOWER FACE", kstpkper=kstpkper
603-
)
604-
if verbose:
605-
print(
606-
f" FLOW LOWER FACE: {type(flf_data)}, "
607-
f"len={len(flf_data) if flf_data else 0}"
608-
)
609-
flf = flf_data[0] if flf_data and len(flf_data) > 0 else None
610-
else:
611-
flf = None
566+
frf = (
567+
self.cbc_obj.get_data(text="FLOW RIGHT FACE", kstpkper=kstpkper)[0]
568+
if "FLOW RIGHT FACE" in available_terms
569+
else None
570+
)
571+
fff = (
572+
self.cbc_obj.get_data(text="FLOW FRONT FACE", kstpkper=kstpkper)[0]
573+
if "FLOW FRONT FACE" in available_terms
574+
else None
575+
)
576+
flf = (
577+
self.cbc_obj.get_data(text="FLOW LOWER FACE", kstpkper=kstpkper)[0]
578+
if "FLOW LOWER FACE" in available_terms
579+
else None
580+
)
612581

613582
if frf is None and fff is None and flf is None:
614583
raise ValueError("No face flows found in budget file")

0 commit comments

Comments
 (0)