Skip to content

Commit

Permalink
Merge pull request #289 from CSHS-CWRA/fix-288
Browse files Browse the repository at this point in the history
Store Dataset in OutputReader class to avoid calling open_dataset repeatedly
  • Loading branch information
Zeitsperre authored Jun 1, 2023
2 parents c17976e + 7006e9a commit 7b9b55e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
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

0 comments on commit 7b9b55e

Please sign in to comment.