Skip to content

Commit

Permalink
feat(MfList): support kper field in stress period data (#2179)
Browse files Browse the repository at this point in the history
ndarray is treated as before — only named data structures (recarray and dataframe) are considered, to avoid questions about where to put the kper column
  • Loading branch information
wpbonelli authored May 4, 2024
1 parent 8e16aab commit 7122219
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
9 changes: 2 additions & 7 deletions autotest/test_modflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,17 +917,12 @@ def test_bcs_check(function_tmpdir):
ghb = ModflowGhb(mf, stress_period_data={0: [0, 0, 0, 100, 1]})
riv_spd = pd.DataFrame(
[[0, 0, 0, 0, 101.0, 10.0, 100.0], [0, 0, 0, 1, 80.0, 10.0, 90.0]],
columns=["per", "k", "i", "j", "stage", "cond", "rbot"],
columns=["kper", "k", "i", "j", "stage", "cond", "rbot"],
)

pers = riv_spd.groupby("per")
riv_spd = {i: pers.get_group(i).drop("per", axis=1) for i in [0]}
riv = ModflowRiv(
mf,
stress_period_data=riv_spd,
# stress_period_data={
# 0: [[0, 0, 0, 101, 10, 100], [0, 0, 1, 80, 10, 90]]
# },
stress_period_data=riv_spd.to_records(index=False),
)
chk = ghb.check()
assert chk.summary_array["desc"][0] == "BC in inactive cell"
Expand Down
23 changes: 18 additions & 5 deletions flopy/utils/util_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,16 @@ def fmt_string(self):
fmt_string = "".join(fmts)
return fmt_string

def __cast_tabular(self, data):
data = pd.DataFrame(data)
if "kper" in data.dtypes:
groups = data.groupby("kper")
data = {kper: group.drop("kper", axis=1) for kper, group in groups}
for kper, d in data.items():
self.__cast_dataframe(kper, d)
else:
self.__cast_dataframe(0, data)

# Private method to cast the data argument
# Should only be called by the constructor
def __cast_data(self, data):
Expand Down Expand Up @@ -350,15 +360,18 @@ def __cast_data(self, data):
f"{type(d)} at kper {kper}"
)

# A single recarray - same MfList for all stress periods
# A single dataframe
elif isinstance(data, pd.DataFrame):
self.__cast_tabular(data)

# A single recarray
elif isinstance(data, np.recarray):
self.__cast_recarray(0, data)
self.__cast_tabular(data)

# A single ndarray
elif isinstance(data, np.ndarray):
self.__cast_ndarray(0, data)
# A single dataframe
elif isinstance(data, pd.DataFrame):
self.__cast_dataframe(0, data)

# A single filename
elif isinstance(data, str):
self.__cast_str(0, data)
Expand Down

0 comments on commit 7122219

Please sign in to comment.