Backend for netCDF files based on the netCDF4 package. It can open ".nc", ".nc4", ".cdf" files and will be chosen as default for these files. Additionally it can open valid HDF5 files, see https://h5netcdf.org/#invalid-netcdf-files for more info. It will not be detected as
| 678 | |
| 679 | |
| 680 | class NetCDF4BackendEntrypoint(BackendEntrypoint): |
| 681 | """ |
| 682 | Backend for netCDF files based on the netCDF4 package. |
| 683 | |
| 684 | It can open ".nc", ".nc4", ".cdf" files and will be chosen |
| 685 | as default for these files. |
| 686 | |
| 687 | Additionally it can open valid HDF5 files, see |
| 688 | https://h5netcdf.org/#invalid-netcdf-files for more info. |
| 689 | It will not be detected as valid backend for such files, so make |
| 690 | sure to specify ``engine="netcdf4"`` in ``open_dataset``. |
| 691 | |
| 692 | For more information about the underlying library, visit: |
| 693 | https://unidata.github.io/netcdf4-python |
| 694 | |
| 695 | See Also |
| 696 | -------- |
| 697 | backends.NetCDF4DataStore |
| 698 | backends.H5netcdfBackendEntrypoint |
| 699 | backends.ScipyBackendEntrypoint |
| 700 | """ |
| 701 | |
| 702 | description = ( |
| 703 | "Open netCDF (.nc, .nc4 and .cdf) and most HDF5 files using netCDF4 in Xarray" |
| 704 | ) |
| 705 | url = "https://docs.xarray.dev/en/stable/generated/xarray.backends.NetCDF4BackendEntrypoint.html" |
| 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 |
no outgoing calls
searching dependent graphs…