Skip to content
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

xugrid.open_dataset results in a locked netCDF file #309

Closed
Huite opened this issue Jan 23, 2025 · 1 comment
Closed

xugrid.open_dataset results in a locked netCDF file #309

Huite opened this issue Jan 23, 2025 · 1 comment

Comments

@Huite
Copy link
Collaborator

Huite commented Jan 23, 2025

Current implementation does this:

def open_dataset(*args, **kwargs):
    ds = xr.open_dataset(*args, **kwargs)
    return UgridDataset(ds) 

Internally, the UgridDataset constructor creates a new dataset, and so the ds reference is lost, making UgridDataset.close() a dud.

We should likely do something like:

class UgridDataset:
    def __init__(self, dataset):
        self._original = dataset
        # ... rest of initialization
    
    def close(self):
        self._original.close()

It might be possible that dataset is None, though.

Explicit indexes would also solve this, since we'd just use/return an ordinary xarray dataset.

@Huite
Copy link
Collaborator Author

Huite commented Jan 31, 2025

Funnily enough, I apparently looked into this in the past, and used xarray's set_close, which does exactly the right thing: it associates the close of a (new) object with another. It was part of the open_dataarray code, but not of the UgridDataset and UgridDataArray constructors. Using it consistently does the trick (and doesn't require a new close method).

@Huite Huite closed this as completed in cb5ab73 Jan 31, 2025
Huite added a commit that referenced this issue Jan 31, 2025
Fix #309. Use xarray's set_close consistently. Silence some additional xarray FutureWarnings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant