|
11 | 11 | from ..core.utils import ( |
12 | 12 | FrozenDict, |
13 | 13 | is_remote_uri, |
| 14 | + module_available, |
14 | 15 | read_magic_number_from_file, |
15 | 16 | try_read_magic_number_from_file_or_path, |
16 | 17 | ) |
|
33 | 34 | ) |
34 | 35 | from .store import StoreBackendEntrypoint |
35 | 36 |
|
36 | | -try: |
37 | | - import h5netcdf |
38 | | - |
39 | | - has_h5netcdf = True |
40 | | -except ImportError: |
41 | | - # Except a base ImportError (not ModuleNotFoundError) to catch usecases |
42 | | - # where errors have mismatched versions of c-dependencies. This can happen |
43 | | - # when developers are making changes them. |
44 | | - has_h5netcdf = False |
45 | | - |
46 | 37 |
|
47 | 38 | class H5NetCDFArrayWrapper(BaseNetCDF4Array): |
48 | 39 | def get_array(self, needs_lock=True): |
@@ -110,6 +101,7 @@ class H5NetCDFStore(WritableCFDataStore): |
110 | 101 | ) |
111 | 102 |
|
112 | 103 | def __init__(self, manager, group=None, mode=None, lock=HDF5_LOCK, autoclose=False): |
| 104 | + import h5netcdf |
113 | 105 |
|
114 | 106 | if isinstance(manager, (h5netcdf.File, h5netcdf.Group)): |
115 | 107 | if group is None: |
@@ -147,6 +139,7 @@ def open( |
147 | 139 | phony_dims=None, |
148 | 140 | decode_vlen_strings=True, |
149 | 141 | ): |
| 142 | + import h5netcdf |
150 | 143 |
|
151 | 144 | if isinstance(filename, bytes): |
152 | 145 | raise ValueError( |
@@ -237,12 +230,16 @@ def get_attrs(self): |
237 | 230 | return FrozenDict(_read_attributes(self.ds)) |
238 | 231 |
|
239 | 232 | def get_dimensions(self): |
| 233 | + import h5netcdf |
| 234 | + |
240 | 235 | if Version(h5netcdf.__version__) >= Version("0.14.0.dev0"): |
241 | 236 | return FrozenDict((k, len(v)) for k, v in self.ds.dimensions.items()) |
242 | 237 | else: |
243 | 238 | return self.ds.dimensions |
244 | 239 |
|
245 | 240 | def get_encoding(self): |
| 241 | + import h5netcdf |
| 242 | + |
246 | 243 | if Version(h5netcdf.__version__) >= Version("0.14.0.dev0"): |
247 | 244 | return { |
248 | 245 | "unlimited_dims": { |
@@ -373,7 +370,7 @@ class H5netcdfBackendEntrypoint(BackendEntrypoint): |
373 | 370 | backends.ScipyBackendEntrypoint |
374 | 371 | """ |
375 | 372 |
|
376 | | - available = has_h5netcdf |
| 373 | + available = module_available("h5netcdf") |
377 | 374 | description = ( |
378 | 375 | "Open netCDF (.nc, .nc4 and .cdf) and most HDF5 files using h5netcdf in Xarray" |
379 | 376 | ) |
|
0 commit comments