|
5 | 5 | from ..core import indexing
|
6 | 6 | from ..core.utils import Frozen, FrozenDict, close_on_error
|
7 | 7 | from ..core.variable import Variable
|
8 |
| -from .common import AbstractDataStore, BackendArray, BackendEntrypoint |
| 8 | +from .common import ( |
| 9 | + BACKEND_ENTRYPOINTS, |
| 10 | + AbstractDataStore, |
| 11 | + BackendArray, |
| 12 | + BackendEntrypoint, |
| 13 | +) |
9 | 14 | from .locks import SerializableLock, ensure_lock
|
10 |
| -from .store import open_backend_dataset_store |
| 15 | +from .store import StoreBackendEntrypoint |
| 16 | + |
| 17 | +try: |
| 18 | + import cfgrib |
| 19 | + |
| 20 | + has_cfgrib = True |
| 21 | +except ModuleNotFoundError: |
| 22 | + has_cfgrib = False |
| 23 | + |
11 | 24 |
|
12 | 25 | # FIXME: Add a dedicated lock, even if ecCodes is supposed to be thread-safe
|
13 | 26 | # in most circumstances. See:
|
@@ -38,7 +51,6 @@ class CfGribDataStore(AbstractDataStore):
|
38 | 51 | """
|
39 | 52 |
|
40 | 53 | def __init__(self, filename, lock=None, **backend_kwargs):
|
41 |
| - import cfgrib |
42 | 54 |
|
43 | 55 | if lock is None:
|
44 | 56 | lock = ECCODES_LOCK
|
@@ -74,58 +86,58 @@ def get_encoding(self):
|
74 | 86 | return encoding
|
75 | 87 |
|
76 | 88 |
|
77 |
| -def guess_can_open_cfgrib(store_spec): |
78 |
| - try: |
79 |
| - _, ext = os.path.splitext(store_spec) |
80 |
| - except TypeError: |
81 |
| - return False |
82 |
| - return ext in {".grib", ".grib2", ".grb", ".grb2"} |
83 |
| - |
84 |
| - |
85 |
| -def open_backend_dataset_cfgrib( |
86 |
| - filename_or_obj, |
87 |
| - *, |
88 |
| - mask_and_scale=True, |
89 |
| - decode_times=None, |
90 |
| - concat_characters=None, |
91 |
| - decode_coords=None, |
92 |
| - drop_variables=None, |
93 |
| - use_cftime=None, |
94 |
| - decode_timedelta=None, |
95 |
| - lock=None, |
96 |
| - indexpath="{path}.{short_hash}.idx", |
97 |
| - filter_by_keys={}, |
98 |
| - read_keys=[], |
99 |
| - encode_cf=("parameter", "time", "geography", "vertical"), |
100 |
| - squeeze=True, |
101 |
| - time_dims=("time", "step"), |
102 |
| -): |
103 |
| - |
104 |
| - store = CfGribDataStore( |
| 89 | +class CfgribfBackendEntrypoint(BackendEntrypoint): |
| 90 | + def guess_can_open(self, store_spec): |
| 91 | + try: |
| 92 | + _, ext = os.path.splitext(store_spec) |
| 93 | + except TypeError: |
| 94 | + return False |
| 95 | + return ext in {".grib", ".grib2", ".grb", ".grb2"} |
| 96 | + |
| 97 | + def open_dataset( |
| 98 | + self, |
105 | 99 | filename_or_obj,
|
106 |
| - indexpath=indexpath, |
107 |
| - filter_by_keys=filter_by_keys, |
108 |
| - read_keys=read_keys, |
109 |
| - encode_cf=encode_cf, |
110 |
| - squeeze=squeeze, |
111 |
| - time_dims=time_dims, |
112 |
| - lock=lock, |
113 |
| - ) |
114 |
| - |
115 |
| - with close_on_error(store): |
116 |
| - ds = open_backend_dataset_store( |
117 |
| - store, |
118 |
| - mask_and_scale=mask_and_scale, |
119 |
| - decode_times=decode_times, |
120 |
| - concat_characters=concat_characters, |
121 |
| - decode_coords=decode_coords, |
122 |
| - drop_variables=drop_variables, |
123 |
| - use_cftime=use_cftime, |
124 |
| - decode_timedelta=decode_timedelta, |
| 100 | + *, |
| 101 | + mask_and_scale=True, |
| 102 | + decode_times=None, |
| 103 | + concat_characters=None, |
| 104 | + decode_coords=None, |
| 105 | + drop_variables=None, |
| 106 | + use_cftime=None, |
| 107 | + decode_timedelta=None, |
| 108 | + lock=None, |
| 109 | + indexpath="{path}.{short_hash}.idx", |
| 110 | + filter_by_keys={}, |
| 111 | + read_keys=[], |
| 112 | + encode_cf=("parameter", "time", "geography", "vertical"), |
| 113 | + squeeze=True, |
| 114 | + time_dims=("time", "step"), |
| 115 | + ): |
| 116 | + |
| 117 | + store = CfGribDataStore( |
| 118 | + filename_or_obj, |
| 119 | + indexpath=indexpath, |
| 120 | + filter_by_keys=filter_by_keys, |
| 121 | + read_keys=read_keys, |
| 122 | + encode_cf=encode_cf, |
| 123 | + squeeze=squeeze, |
| 124 | + time_dims=time_dims, |
| 125 | + lock=lock, |
125 | 126 | )
|
126 |
| - return ds |
127 |
| - |
128 |
| - |
129 |
| -cfgrib_backend = BackendEntrypoint( |
130 |
| - open_dataset=open_backend_dataset_cfgrib, guess_can_open=guess_can_open_cfgrib |
131 |
| -) |
| 127 | + store_entrypoint = StoreBackendEntrypoint() |
| 128 | + with close_on_error(store): |
| 129 | + ds = store_entrypoint.open_dataset( |
| 130 | + store, |
| 131 | + mask_and_scale=mask_and_scale, |
| 132 | + decode_times=decode_times, |
| 133 | + concat_characters=concat_characters, |
| 134 | + decode_coords=decode_coords, |
| 135 | + drop_variables=drop_variables, |
| 136 | + use_cftime=use_cftime, |
| 137 | + decode_timedelta=decode_timedelta, |
| 138 | + ) |
| 139 | + return ds |
| 140 | + |
| 141 | + |
| 142 | +if has_cfgrib: |
| 143 | + BACKEND_ENTRYPOINTS["cfgrib"] = CfgribfBackendEntrypoint |
0 commit comments