diff --git a/hydromt_sfincs/sfincs.py b/hydromt_sfincs/sfincs.py index c89a1161..19e47866 100644 --- a/hydromt_sfincs/sfincs.py +++ b/hydromt_sfincs/sfincs.py @@ -3189,6 +3189,7 @@ def read_forcing(self, data_vars: List = None): for name in dvars_2d: fname, rename = self._FORCING_NET[name] fn = self.get_config(f"{fname}file", abs_path=True) + ds = None if fn is None or not isfile(fn): if fn is not None: self.logger.warning(f"{name}file not found at {fn}") @@ -3196,7 +3197,7 @@ def read_forcing(self, data_vars: List = None): elif name in ["netbndbzsbzi", "netsrcdis"]: ds = GeoDataset.from_netcdf(fn, crs=self.crs, chunks="auto") else: - ds = xr.load_dataset(fn, chunks="auto") + ds = xr.open_dataset(fn, chunks="auto") rename = {k: v for k, v in rename.items() if k in ds} if len(rename) > 0: @@ -3205,6 +3206,9 @@ def read_forcing(self, data_vars: List = None): else: logger.warning(f"No forcing variables found in {fname}file") + if ds is not None: + ds.close() + def write_forcing(self, data_vars: Union[List, str] = None, fmt: str = "%7.2f"): """Write forcing to ascii or netcdf (netampr) files. Filenames are based on the `config` attribute.