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

feat(MfList): support kper field in stress period data #2179

Merged
merged 1 commit into from
May 4, 2024
Merged
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
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
Loading