Skip to content

Commit

Permalink
fix(modflow): dataframe support was patchy in a few packages
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Apr 4, 2024
1 parent 4c44cb0 commit 54bdf7b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
7 changes: 1 addition & 6 deletions .docs/Notebooks/mf6_mnw2_tutorial01.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@
)
node_data

# #### convert the DataFrame to a recarray for compatibility with flopy

node_data = node_data.to_records()
node_data

# ### Stress period information
# (could also be developed externally)

Expand All @@ -97,7 +92,7 @@
stress_period_data

pers = stress_period_data.groupby("per")
stress_period_data = {i: pers.get_group(i).to_records() for i in [0, 1]}
stress_period_data = {i: pers.get_group(i) for i in [0, 1]}
stress_period_data

# ### Make ``ModflowMnw2`` package object
Expand Down
14 changes: 11 additions & 3 deletions autotest/test_modflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -915,11 +915,19 @@ def test_bcs_check(function_tmpdir):
assert len(chk.summary_array) == 1

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"],
)

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={
0: [[0, 0, 0, 101, 10, 100], [0, 0, 1, 80, 10, 90]]
},
stress_period_data=riv_spd,
# stress_period_data={
# 0: [[0, 0, 0, 101, 10, 100], [0, 0, 1, 80, 10, 90]]
# },
)
chk = ghb.check()
assert chk.summary_array["desc"][0] == "BC in inactive cell"
Expand Down
6 changes: 6 additions & 0 deletions flopy/modflow/mfmnw2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,12 @@ def __init__(
dtype=self.get_default_spd_dtype(structured=self.structured),
)
if stress_period_data is not None:
stress_period_data = {
per: sp.to_records(index=False)
if isinstance(sp, pd.DataFrame)
else sp
for per, sp in stress_period_data.items()
}
for per, data in stress_period_data.items():
spd = ModflowMnw2.get_empty_stress_period_data(
len(data), aux_names=aux
Expand Down
9 changes: 6 additions & 3 deletions flopy/modflow/mfriv.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"""

import numpy as np
import pandas as pd

from ..pakbase import Package
from ..utils import MfList
Expand Down Expand Up @@ -194,9 +195,11 @@ def check(self, f=None, verbose=True, level=1, checktype=None):
chk = self._get_check(f, verbose, level, checktype)
chk.summary_array = basechk.summary_array

for per in self.stress_period_data.data.keys():
if isinstance(self.stress_period_data.data[per], np.recarray):
spd = self.stress_period_data.data[per]
for per, data in self.stress_period_data.data.items():
if isinstance(data, (np.recarray, pd.DataFrame)):
if isinstance(data, pd.DataFrame):
data = data.to_records(index=False).astype(self.dtype)
spd = data
inds = (
(spd.k, spd.i, spd.j)
if self.parent.structured
Expand Down
4 changes: 3 additions & 1 deletion flopy/utils/util_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,9 @@ def __cast_ndarray(self, kper, d):
self.__vtype[kper] = np.recarray

def __cast_dataframe(self, kper, d):
self.__cast_recarray(kper, d.to_records(index=False))
self.__cast_recarray(
kper, d.to_records(index=False).astype(self.dtype)
)

def get_dataframe(self, squeeze=False):
"""
Expand Down

0 comments on commit 54bdf7b

Please sign in to comment.