-
Notifications
You must be signed in to change notification settings - Fork 14
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
Overwriting files that were lazily loaded results in permission error… #241
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
from os.path import join, dirname, abspath | ||
import numpy as np | ||
import os | ||
from pyproj import CRS | ||
import shutil | ||
|
||
from hydromt_sfincs import utils | ||
from hydromt_sfincs.quadtree import QuadtreeGrid | ||
|
||
TESTDATADIR = join(dirname(abspath(__file__)), "data") | ||
|
@@ -35,3 +38,34 @@ def test_quadtree_io(tmpdir): | |
assert np.sum(qtr2.data["msk"].values) == 4298 | ||
# assert the dep variable is the same | ||
assert np.sum(qtr.data["dep"].values) == np.sum(qtr2.data["dep"].values) | ||
|
||
|
||
def test_overwrite_quadtree_nc(tmpdir): | ||
ncfile = join(TESTDATADIR, "sfincs_test_quadtree", "sfincs.nc") | ||
nc_copy = join(str(tmpdir), "sfincs.nc") | ||
|
||
# Create file + copy | ||
shutil.copy(ncfile, nc_copy) | ||
|
||
# Open the copy with xu_open_dataset | ||
# This opens the file lazily | ||
ds = utils.xu_open_dataset(nc_copy) | ||
|
||
# Convert to dataset | ||
ds = ds.ugrid.to_dataset() | ||
Comment on lines
+52
to
+55
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here you are overwriting the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty sure this happens in more places in the code, I could change that. |
||
|
||
# Try to write | ||
# NOTE this should fail because it still has lazy references to the file | ||
try: | ||
ds.to_netcdf(nc_copy) | ||
except PermissionError: | ||
pass | ||
Comment on lines
+59
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
use with pytest.raises(PermissionError) as e:
ds.to_netcdf(nc_copy)
assert .... in str(e) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good one, I was looking for something like that, but then forgot |
||
|
||
# Now perform the check and lazy loading check | ||
utils.check_exists_and_lazy(ds, nc_copy) | ||
|
||
# Try to overwrite the file | ||
ds.to_netcdf(nc_copy) | ||
|
||
# Remove the copied file | ||
os.remove(nc_copy) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure to call
ds.close()
the file after loading the contentsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We discussed earlier that ds.close() does not work for xugrid.UgridDatasets right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, I keep forgetting.
Unfortunately, for the same reason
ds.load()
will also not work then :s