From 83ec6999624cb532489f819b62bceca90cb0239e Mon Sep 17 00:00:00 2001 From: David Huard Date: Thu, 1 Jun 2023 11:16:12 -0400 Subject: [PATCH] Store Dataset in OutputReader class to avoir calling open_dataset repeatedly --- HISTORY.rst | 5 +++++ ravenpy/ravenpy.py | 21 +++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index dfab05ca..572fd0cf 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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 diff --git a/ravenpy/ravenpy.py b/ravenpy/ravenpy.py index 5459f817..582d63b1 100644 --- a/ravenpy/ravenpy.py +++ b/ravenpy/ravenpy.py @@ -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"] @@ -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]: