Return the default netCDF library to use for writing a netCDF file.
(
path_or_file: str | IOBase | None,
format: T_NetcdfTypes | None,
)
| 163 | |
| 164 | |
| 165 | def get_default_netcdf_write_engine( |
| 166 | path_or_file: str | IOBase | None, |
| 167 | format: T_NetcdfTypes | None, |
| 168 | ) -> Literal["netcdf4", "h5netcdf", "scipy"]: |
| 169 | """Return the default netCDF library to use for writing a netCDF file.""" |
| 170 | |
| 171 | module_names = { |
| 172 | "netcdf4": "netCDF4", |
| 173 | "scipy": "scipy", |
| 174 | "h5netcdf": "h5netcdf", |
| 175 | } |
| 176 | candidates = list(OPTIONS["netcdf_engine_order"]) |
| 177 | |
| 178 | if format is not None: |
| 179 | format = format.upper() # type: ignore[assignment] |
| 180 | if format not in { |
| 181 | "NETCDF4", |
| 182 | "NETCDF4_CLASSIC", |
| 183 | "NETCDF3_64BIT", |
| 184 | "NETCDF3_CLASSIC", |
| 185 | }: |
| 186 | raise ValueError(f"unexpected {format=}") |
| 187 | # TODO: allow format='NETCDF4_CLASSIC' to default to using h5netcdf, |
| 188 | # when the oldest supported version of h5netcdf supports it: |
| 189 | # https://github.com/h5netcdf/h5netcdf/pull/283 |
| 190 | if format != "NETCDF4": |
| 191 | candidates.remove("h5netcdf") |
| 192 | if format not in {"NETCDF3_64BIT", "NETCDF3_CLASSIC"}: |
| 193 | candidates.remove("scipy") |
| 194 | |
| 195 | nczarr_mode = isinstance(path_or_file, str) and path_or_file.endswith( |
| 196 | "#mode=nczarr" |
| 197 | ) |
| 198 | if nczarr_mode: |
| 199 | candidates[:] = ["netcdf4"] |
| 200 | |
| 201 | if isinstance(path_or_file, IOBase): |
| 202 | candidates.remove("netcdf4") |
| 203 | |
| 204 | for engine in candidates: |
| 205 | module_name = module_names[engine] |
| 206 | if importlib.util.find_spec(module_name) is not None: |
| 207 | return engine |
| 208 | |
| 209 | if nczarr_mode: |
| 210 | format_str = " in NCZarr format" |
| 211 | else: |
| 212 | format_str = f" with {format=}" if format is not None else "" |
| 213 | libraries = ", ".join(module_names[c] for c in candidates) |
| 214 | raise ValueError( |
| 215 | f"cannot write NetCDF files{format_str} because none of the suitable " |
| 216 | f"backend libraries ({libraries}) are installed" |
| 217 | ) |
| 218 | |
| 219 | |
| 220 | def _sanitize_unlimited_dims(dataset, unlimited_dims): |
searching dependent graphs…