Skip to content

Commit ef3e07c

Browse files
committed
no query params for h5netcdf
1 parent d2334e4 commit ef3e07c

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

xarray/backends/h5netcdf_.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -462,19 +462,13 @@ class H5netcdfBackendEntrypoint(BackendEntrypoint):
462462
supports_groups = True
463463

464464
def guess_can_open(self, filename_or_obj: T_PathFileOrDataStore) -> bool:
465-
from xarray.core.utils import is_remote_uri, strip_uri_params
466-
467465
filename_or_obj = _normalize_filename_or_obj(filename_or_obj)
468466
magic_number = try_read_magic_number_from_file_or_path(filename_or_obj)
469467
if magic_number is not None:
470468
return magic_number.startswith(b"\211HDF\r\n\032\n")
471469

472470
if isinstance(filename_or_obj, str | os.PathLike):
473-
path = str(filename_or_obj)
474-
# For remote URIs, strip query parameters and fragments before checking extension
475-
if isinstance(filename_or_obj, str) and is_remote_uri(path):
476-
path = strip_uri_params(path)
477-
_, ext = os.path.splitext(path)
471+
_, ext = os.path.splitext(str(filename_or_obj))
478472
return ext in {".nc", ".nc4", ".cdf"}
479473

480474
return False

xarray/tests/test_backends.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7281,27 +7281,29 @@ def test_remote_url_backend_auto_detection() -> None:
72817281
("https://example.com/store.zarr", "zarr"),
72827282
("http://example.com/data.zarr/", "zarr"),
72837283
("s3://bucket/path/to/data.zarr", "zarr"),
7284-
# Remote netCDF URLs (non-DAP) - h5netcdf wins (first in order)
7284+
# Remote netCDF URLs (non-DAP) - h5netcdf wins (first in order, no query params)
72857285
("https://example.com/file.nc", "h5netcdf"),
72867286
("http://example.com/data.nc4", "h5netcdf"),
72877287
("https://example.com/test.cdf", "h5netcdf"),
7288-
("https://example.com/data.nc?var=temperature&time=0", "h5netcdf"),
7289-
# DAP URLs with query parameters - h5netcdf wins (has .nc4 ext, first in order)
7288+
("s3://bucket/path/to/data.nc", "h5netcdf"),
7289+
# Remote netCDF URLs with query params - netcdf4 wins
7290+
# Note: Query params are typically indicative of DAP URLs (e.g., OPeNDAP constraint expressions),
7291+
# so we prefer netcdf4 (which has DAP support) over h5netcdf (which doesn't)
7292+
("https://example.com/data.nc?var=temperature&time=0", "netcdf4"),
72907293
(
72917294
"http://test.opendap.org/opendap/dap4/StaggeredGrid.nc4?dap4.ce=/time[0:1:0]",
7292-
"h5netcdf",
7295+
"netcdf4",
72937296
),
72947297
# DAP URLs without extensions - pydap wins
72957298
("dap2://opendap.earthdata.nasa.gov/collections/dataset", "pydap"),
72967299
("dap4://opendap.earthdata.nasa.gov/collections/dataset", "pydap"),
72977300
("DAP2://example.com/dataset", "pydap"), # uppercase scheme
72987301
("DAP4://example.com/dataset", "pydap"), # uppercase scheme
72997302
("https://example.com/services/DAP2/dataset", "pydap"), # uppercase in path
7300-
# DAP URLs with .nc extensions - h5netcdf wins (first in order)
7303+
# DAP URLs with .nc extensions (no query params) - h5netcdf wins (first in order)
73017304
("http://test.opendap.org/opendap/dap4/StaggeredGrid.nc4", "h5netcdf"),
73027305
("https://example.com/DAP4/data.nc", "h5netcdf"),
73037306
("http://example.com/data/Dap4/file.nc", "h5netcdf"),
7304-
("s3://bucket/path/to/data.nc", "h5netcdf"),
73057307
]
73067308

73077309
for url, expected_backend in test_cases:

0 commit comments

Comments
 (0)