-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Mostly restore netcdf backend behavior with URLs
#10931
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1f91b70
backend resolution - revert to netcdf4 eagerly grabbing URL + FutureW…
ianhi f2df3e0
better warning message
ianhi 5ba3900
add whats new
ianhi a03cf17
don't emit warning yet
ianhi 40a1c5b
Merge branch 'main' into backends-urls
ianhi df7f74d
don't add new behavior to netcdf4 backend
ianhi 2e1752d
numpydocify
ianhi a4790a0
Merge branch 'main' into backends-urls
ianhi 15c3e73
Apply suggestion from @ianhi
ianhi 70dd54d
Apply suggestion from @ianhi
ianhi bc5bc05
simplify
ianhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,6 +49,7 @@ | |
| from xarray.core.utils import ( | ||
| FrozenDict, | ||
| close_on_error, | ||
| emit_user_level_warning, | ||
| is_remote_uri, | ||
| strip_uri_params, | ||
| try_read_magic_number_from_path, | ||
|
|
@@ -463,6 +464,15 @@ def open( | |
| if isinstance(filename, os.PathLike): | ||
| filename = os.fspath(filename) | ||
|
|
||
| # Replace DAP protocol prefixes with https:// - netCDF4 library can't handle them | ||
| # These prefixes may be added by users to explicitly indicate DAP protocol | ||
| # Following pydap's convention, we convert to https:// | ||
| # See: https://github.com/pydap/pydap/blob/0a2b0892611abaf0a9762ffd4f2f082cb8e497c2/src/pydap/handlers/dap.py#L103-L107 | ||
| if isinstance(filename, str): | ||
| filename_lower = filename.lower() | ||
| if filename_lower.startswith(("dap2://", "dap4://")): | ||
| filename = "https://" + filename[7:] | ||
|
|
||
| if isinstance(filename, IOBase): | ||
| raise TypeError( | ||
| f"file objects are not supported by the netCDF4 backend: {filename}" | ||
|
|
@@ -715,10 +725,36 @@ def _has_netcdf_ext(path: str | os.PathLike, is_remote: bool = False) -> bool: | |
| _, ext = os.path.splitext(path) | ||
| return ext in {".nc", ".nc4", ".cdf"} | ||
|
|
||
| if isinstance(filename_or_obj, str) and is_remote_uri(filename_or_obj): | ||
| # For remote URIs, check extension (accounting for query params/fragments) | ||
| # Remote netcdf-c can handle both regular URLs and DAP URLs | ||
| return _has_netcdf_ext(filename_or_obj, is_remote=True) | ||
| # Check for explicit DAP protocol indicators: | ||
| # 1. DAP scheme: dap2:// or dap4:// (case-insensitive, may not be recognized by is_remote_uri) | ||
| # 2. Remote URI with /dap2/ or /dap4/ in URL path (case-insensitive) | ||
| # Note: We intentionally do NOT check for .dap suffix as that would match | ||
| # file extensions like .dap which trigger downloads of binary data | ||
| if isinstance(filename_or_obj, str): | ||
| url_lower = filename_or_obj.lower() | ||
| from xarray.backends.common import _is_likely_dap_url | ||
|
|
||
| if _is_likely_dap_url(url_lower): | ||
| return True | ||
| elif is_remote_uri(filename_or_obj): | ||
|
ianhi marked this conversation as resolved.
Outdated
|
||
| # For remote URIs, check extension (accounting for query params/fragments) | ||
| # Remote netcdf-c can handle both regular URLs and DAP URLs | ||
| if _has_netcdf_ext(filename_or_obj, is_remote=True): | ||
| return True | ||
| elif "zarr" in url_lower: | ||
| return False | ||
|
ianhi marked this conversation as resolved.
|
||
| else: | ||
| # returning true so we don't have a breaking change for people relying on this | ||
| # netcdf backend guessing true for all remote sources. But emitting a deprecation | ||
| # warning that this behavior will go away as it presents problems for other backends | ||
| emit_user_level_warning( | ||
| f"The NetCDF4 backend is guessing that {filename_or_obj!r} is a NetCDF file. " | ||
| "In the future, xarray will require remote URLs to either have a .nc, .nc4, or .cdf " | ||
| "extension, use a DAP protocol (dap2://, dap4://, or /dap/ in the path), or specify " | ||
|
Collaborator
Author
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. this isn't strictly true, but the full truth is, i think, too verbose for this already long warning |
||
| "the backend explicitly using the 'engine' parameter (e.g., engine='netcdf4').", | ||
| FutureWarning, | ||
|
ianhi marked this conversation as resolved.
Outdated
|
||
| ) | ||
| return True | ||
|
|
||
| if isinstance(filename_or_obj, str | os.PathLike): | ||
| # For local paths, check magic number first, then extension | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.