MCPcopy
hub / github.com/pydata/xarray / guess_can_open

Method guess_can_open

xarray/backends/netCDF4_.py:708–747  ·  view source on GitHub ↗
(self, filename_or_obj: T_PathFileOrDataStore)

Source from the content-addressed store, hash-verified

706 supports_groups = True
707
708 def guess_can_open(self, filename_or_obj: T_PathFileOrDataStore) -> bool:
709 # Helper to check if magic number is netCDF or HDF5
710 def _is_netcdf_magic(magic: bytes) -> bool:
711 return magic.startswith((b"CDF", b"\211HDF\r\n\032\n"))
712
713 # Helper to check if extension is netCDF
714 def _has_netcdf_ext(path: str | os.PathLike, is_remote: bool = False) -> bool:
715 path = str(path).rstrip("/")
716 # For remote URIs, strip query parameters and fragments
717 if is_remote:
718 path = strip_uri_params(path)
719 _, ext = os.path.splitext(path)
720 return ext in {".nc", ".nc4", ".cdf"}
721
722 if isinstance(filename_or_obj, str):
723 if is_remote_uri(filename_or_obj):
724 # For remote URIs, check extension (accounting for query params/fragments)
725 # Remote netcdf-c can handle both regular URLs and DAP URLs
726 if _has_netcdf_ext(filename_or_obj, is_remote=True):
727 return True
728 elif "zarr" in filename_or_obj.lower():
729 return False
730 # return true for non-zarr URLs so we don't have a breaking change for people relying on this
731 # netcdf backend guessing true for all remote sources.
732 # TODO: emit a warning here about deprecation of this behavior
733 # https://github.com/pydata/xarray/pull/10931
734 return True
735
736 if isinstance(filename_or_obj, str | os.PathLike):
737 # For local paths, check magic number first, then extension
738 magic_number = try_read_magic_number_from_path(filename_or_obj)
739 if magic_number is not None:
740 return _is_netcdf_magic(magic_number)
741 # No magic number available, fallback to extension
742 return _has_netcdf_ext(filename_or_obj)
743
744 if isinstance(filename_or_obj, bytes | memoryview):
745 return _is_netcdf_magic(bytes(filename_or_obj[:8]))
746
747 return False
748
749 def open_dataset(
750 self,

Callers 1

test_netcdf4_entrypointFunction · 0.95

Calls 3

is_remote_uriFunction · 0.90
lowerMethod · 0.80

Tested by 1

test_netcdf4_entrypointFunction · 0.76