Skip to content

Commit

Permalink
Utilize xarray's set_close consistently. Fixes #309
Browse files Browse the repository at this point in the history
  • Loading branch information
Huite committed Jan 31, 2025
1 parent 508e54e commit cb5ab73
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
20 changes: 16 additions & 4 deletions tests/test_ugrid_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_dunder_forward(self):
assert isinstance(float(self.uda[0]), float)

def test_close(self, tmp_path):
path = tmp_path / "locktest.nc"
path = tmp_path / "dataarray-closetest.nc"
self.uda.ugrid.to_netcdf(path)
back = xugrid.open_dataarray(path)
with pytest.raises(PermissionError):
Expand Down Expand Up @@ -323,8 +323,10 @@ def test_is_geographic(self):
assert result.grid.is_geographic is False

def test_to_geodataframe(self):
uda1 = self.uda.copy()
uda1.ugrid.obj.name = None
with pytest.raises(ValueError, match="unable to convert unnamed"):
self.uda.ugrid.to_geodataframe()
uda1.ugrid.to_geodataframe()
uda2 = self.uda.copy()
uda2.ugrid.obj.name = "test"
uda2.ugrid.set_crs(epsg=28992)
Expand Down Expand Up @@ -499,6 +501,16 @@ def test_init_from_dataset_only(self):
def test_repr(self):
assert self.uds.__repr__() == self.uds.obj.__repr__()

def test_close(self, tmp_path):
path = tmp_path / "dataset-closetest.nc"
self.uds.ugrid.to_netcdf(path)
back = xugrid.open_dataset(path)
with pytest.raises(PermissionError):
os.remove(path)
# Should close succesfully after closing.
back.close()
os.remove(path)

def test_getitem(self):
assert "a" in self.uds
assert "b" in self.uds
Expand Down Expand Up @@ -639,7 +651,7 @@ def test_intersect_line(self):
actual = self.uds.ugrid.intersect_line(start=p0, end=p1)
sqrt2 = np.sqrt(2.0)
assert isinstance(actual, xr.Dataset)
assert actual.dims == {"mesh2d_nFaces": 2}
assert actual.sizes == {"mesh2d_nFaces": 2}
assert np.allclose(actual["mesh2d_x"], [0.5, 1.25])
assert np.allclose(actual["mesh2d_y"], [0.5, 1.25])
assert np.allclose(actual["mesh2d_s"], [0.5 * sqrt2, 1.25 * sqrt2])
Expand All @@ -656,7 +668,7 @@ def test_intersect_linestring(self):
)
actual = self.uds.ugrid.intersect_linestring(linestring)
assert isinstance(actual, xr.Dataset)
assert actual.dims == {"mesh2d_nFaces": 4}
assert actual.sizes == {"mesh2d_nFaces": 4}
assert np.allclose(actual["mesh2d_x"], [0.75, 1.25, 1.5, 1.5])
assert np.allclose(actual["mesh2d_y"], [0.5, 0.5, 0.75, 1.25])
assert np.allclose(actual["mesh2d_s"], [0.25, 0.75, 1.25, 1.75])
Expand Down
14 changes: 5 additions & 9 deletions xugrid/core/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,12 @@ def __init__(self, obj: xr.DataArray, grid: UgridType):

self._grid = grid
self._obj = assign_ugrid_coords(obj, [grid])
self._original = obj
self._obj.set_close(obj._close)

def __getattr__(self, attr):
result = getattr(self.obj, attr)
return maybe_xugrid(result, [self.grid])

def close(self):
self._original.close()

@property
def obj(self):
return self._obj
Expand Down Expand Up @@ -349,11 +346,10 @@ def __init__(

self._grids = grids
self._obj = assign_ugrid_coords(ds, grids)
# store a reference to the original such that we can close the file handle.
self._original = original

def close(self):
self._original.close()
# We've created a new object; the file handle will be associated with the original.
# set_close makes sure that when close is called on the UgridDataset, that the
# file will actually be closed (via the original).
self._obj.set_close(original._close)

@property
def obj(self):
Expand Down

0 comments on commit cb5ab73

Please sign in to comment.