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

Small Model updates #534

Merged
merged 7 commits into from
Oct 6, 2023
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
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Added
- Added documentation for how to start your own plugin (PR #446)
- New raster method ``to_slippy_tiles``: tiling of a raster dataset according to the slippy tile structure for e.g., webviewers (PR #440).
- Support for http and other *filesystems* in path of data source (PR #515).
- ``set_forcing`` can now add pandas.DataFrame object to frocing. (PR #534)

Changed
-------
Expand All @@ -43,6 +44,7 @@ Fixed
-----
- when a model component (eg maps, forcing, grid) is updated using the set_ methods, it will first be read to avoid loosing data. (PR #460)
- open_geodataset with driver vector also works for other geometry type than points. (PR #509)
- overwrite model in update mode. (PR #534)

Deprecated
----------
Expand Down
16 changes: 13 additions & 3 deletions hydromt/models/model_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ def update(
'"model_out" directory required when updating in "read-only" mode'
)
self.read()
self.set_root(model_out, mode="w")
if forceful_overwrite:
self.set_root(model_out, mode="w+")
else:
self.set_root(model_out, mode="w")

# check if model has a region
if self.region is None:
Expand Down Expand Up @@ -1335,21 +1338,28 @@ def forcing(self) -> Dict[str, Union[xr.Dataset, xr.DataArray]]:

def set_forcing(
self,
data: Union[xr.DataArray, xr.Dataset],
data: Union[xr.DataArray, xr.Dataset, pd.DataFrame],
name: Optional[str] = None,
split_dataset: Optional[bool] = True,
):
"""Add data to forcing attribute.

Data can be xarray.DataArray, xarray.Dataset or pandas.DataFrame.
If pandas.DataFrame, indices should be the DataFrame index and the columns
the variable names. the DataFrame will then be converted to xr.Dataset using
:py:meth:`pandas.DataFrame.to_xarray` method.

Arguments
---------
data: xarray.Dataset or xarray.DataArray
data: xarray.Dataset or xarray.DataArray or pd.DataFrame
New forcing data to add
name: str, optional
Results name, required if data is xarray.Dataset is and split_dataset=False.
split_dataset: bool, optional
If True (default), split a Dataset to store each variable as a DataArray.
"""
if isinstance(data, pd.DataFrame):
data = data.to_xarray()
data_dict = _check_data(data, name, split_dataset)
for name in data_dict:
if name in self.forcing: # trigger init / read
Expand Down
3 changes: 3 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ def test_model_append(demda, df, tmpdir):
assert "dem" in mod1.maps
mod1.set_forcing(demda, name="dem1")
assert "dem" in mod1.forcing
mod1.set_forcing(df, name="df1", split_dataset=False)
assert "df1" in mod1.forcing
assert isinstance(mod1.forcing["df1"], xr.Dataset)
mod1.set_states(demda, name="dem1")
assert "dem" in mod1.states
mod1.set_geoms(demda.raster.box, name="dem1")
Expand Down
Loading