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

Store Dataset in OutputReader class to avoid calling open_dataset repeatedly #289

Merged
merged 2 commits into from
Jun 1, 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
5 changes: 5 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
History
=======

0.12.1 (2023-06-01)
-------------------

* Avoid repeatedly calling `xr.open_dataset` in `OutputReader`'s `hydrograph` and `storage` properties. This seems to cause kernel failures in Jupyter notebooks.

0.12.0 (2023-05-25)
-------------------
This release includes major breaking changes. It completely overhauls how models are defined, and how to run
Expand Down
21 changes: 15 additions & 6 deletions ravenpy/ravenpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ def __init__(
self._run_name = run_name
self._path = Path(path) if path else Path.cwd()
self._files = parsers.output_files(self._run_name, self._path)
self._nc_hydrograph = None
self._nc_storage = None

# TODO: Check if no files are found. Otherwise we get cryptic errors.
# if self._files["hydrograph"]
Expand All @@ -152,15 +154,22 @@ def diagnostics(self) -> Optional[dict]:
@property
def hydrograph(self) -> xr.Dataset:
"""Return the hydrograph."""
h = self.files.get("hydrograph")
if h:
return parsers.parse_nc(h)
if self._nc_hydrograph is None:
h = self.files.get("hydrograph")
if h:
self._nc_hydrograph = parsers.parse_nc(h)

return self._nc_hydrograph

@property
def storage(self) -> xr.Dataset:
s = self.files.get("storage")
if s:
return parsers.parse_nc(s)
"""Return the storage variables."""
if self._nc_storage is None:
s = self.files.get("storage")
if s:
self._nc_storage = parsers.parse_nc(s)

return self._nc_storage

@property
def messages(self) -> Optional[str]:
Expand Down