Skip to content

feat: Add context manager support to DataSet (closes #67) - #70

Open
sahiljhawar wants to merge 1 commit into
GFZ:mainfrom
sahiljhawar:67-context-manager-for-dataset
Open

feat: Add context manager support to DataSet (closes #67)#70
sahiljhawar wants to merge 1 commit into
GFZ:mainfrom
sahiljhawar:67-context-manager-for-dataset

Conversation

@sahiljhawar

Copy link
Copy Markdown
Contributor
  • DataSet now supports __enter__/__exit__ (and __del__) alongside the existing direct-instantiation usage, so callers can choose with DataSet(...) as ds: when they need lazily opened .nc file handles released deterministically instead of relying on garbage collection.

  • close() closes any xr.Dataset groups opened by load_netcdf_data_lazy and tracked via a new _open_resources list, and also materializes any variable that is still a lazy, unmaterialized xr.Variable into a plain array. The latter fixes a real bug: variables cached but never individually accessed (e.g. loading "Flux" also caches "alpha_local" from the same file) would silently reopen the file via xarray's CachingFileManager on first access, even after close() had already run, leaving the file locked for writing.

  • Adds unit tests covering the context manager, close() idempotency, the reference-cycle/lazy-materialization regression, and the overwrite-while-open PermissionError this all stems from.

  • Adds a new tutorial notebook 6_dataset_context_manager.ipynb demonstrating both usage styles.

- DataSet now supports `__enter__`/`__exit__` (and `__del__`)
alongside the existing direct-instantiation usage, so
callers can choose `with DataSet(...) as ds:` when they need lazily
opened .nc file handles released deterministically instead of
relying on garbage collection.

- `close()` closes any `xr.Dataset` groups opened by `load_netcdf_data_lazy`
and tracked via a new `_open_resources` list, and also materializes any
variable that is still a lazy, unmaterialized `xr.Variable` into a
plain array. The latter fixes a real bug: variables cached but never
individually accessed (e.g. loading "Flux" also caches "alpha_local"
from the same file) would silently reopen the file via xarray's
`CachingFileManager` on first access, even after `close()` had already
run, leaving the file locked for writing.

- Adds unit tests covering the context manager, `close()` idempotency,
the reference-cycle/lazy-materialization regression, and the
overwrite-while-open `PermissionError` this all stems from.

- Adds a new tutorial notebook `6_dataset_context_manager.ipynb` demonstrating both
usage styles.
Copilot AI review requested due to automatic review settings July 25, 2026 23:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@sahiljhawar sahiljhawar linked an issue Jul 25, 2026 that may be closed by this pull request
@sahiljhawar
sahiljhawar requested a review from Copilot July 25, 2026 23:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

Successfully merging this pull request may close these issues.

context manager for dataset

2 participants