-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Add a GRIB backend via ECMWF cfgrib / ecCodes #2476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
shoyer
merged 29 commits into
pydata:master
from
alexamici:feature/grib-support-via-cfgrib
Oct 17, 2018
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
72606f7
Integration of ECMWF cfgrib driver to read GRIB files into xarray.
alexamici 71fcbe7
Remove all coordinate renaming from the cfgrib backend.
alexamici 6faa7b9
Move flavour selection to `cfgrib.Dataset.from_path`.
alexamici 1469a0e
Sync xarray backend import style with xarray.
alexamici 12811e8
Make use of the new xarray.backends.FileCachingManager.
alexamici a4409b6
Add just-in-case locking for ecCodes.
alexamici 80b8788
Explicitly assign attributes to CfGribArrayWrapper
alexamici 9dfd660
Add missing locking in CfGribArrayWrapper and use explicit_indexing_a…
alexamici edc4e85
Add a comment about the ugly work-around needed for filter_by_keys.
alexamici 9b5335a
Declare correct indexing support.
alexamici 186a504
Merge branch 'upstream' into feature/grib-support-via-cfgrib
alexamici 485a409
Add TestCfGrib test class.
alexamici 81f18c2
cfgrib doesn't store a file reference so no need for CachingFileManager.
alexamici 5dedb3f
Add cfgrib testing to Travis-CI.
alexamici 831ae4f
Naming.
alexamici 6372e6e
Fix line lengths and get to 100% coverage.
alexamici 8e9b2e3
Add reference to *cfgrib* engine in inline docs.
alexamici 07b9469
First cut of the documentation.
alexamici 340720a
Tentative test cfgrib under dask.distributed.
alexamici 4d84f70
Better integration test.
alexamici 0b027db
Remove explicit copyright and license boilerplate to harmonise with o…
alexamici a4ead54
Add a usage example.
alexamici ec80d86
Fix code style.
alexamici f30b7d0
Fix doc style.
alexamici 223d25c
Fix docs testing. The example.grib file is not accessible.
alexamici 2ef993f
Merge remote-tracking branch 'upstream/master' into feature/grib-supp…
alexamici bbf01e3
Fix merge in docs.
alexamici da2b9dd
Fix merge in docs.
alexamici eda96a4
Fix doc style.
alexamici File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from __future__ import absolute_import, division, print_function | ||
|
||
import numpy as np | ||
|
||
from .. import Variable | ||
from ..core import indexing | ||
from ..core.utils import Frozen, FrozenOrderedDict | ||
from .common import AbstractDataStore, BackendArray | ||
from .locks import ensure_lock, SerializableLock | ||
|
||
# FIXME: Add a dedicated lock, even if ecCodes is supposed to be thread-safe | ||
# in most circumstances. See: | ||
# https://confluence.ecmwf.int/display/ECC/Frequently+Asked+Questions | ||
ECCODES_LOCK = SerializableLock() | ||
|
||
|
||
class CfGribArrayWrapper(BackendArray): | ||
def __init__(self, datastore, array): | ||
self.datastore = datastore | ||
self.shape = array.shape | ||
self.dtype = array.dtype | ||
self.array = array | ||
alexamici marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def __getitem__(self, key): | ||
return indexing.explicit_indexing_adapter( | ||
key, self.shape, indexing.IndexingSupport.OUTER, self._getitem) | ||
|
||
def _getitem(self, key): | ||
with self.datastore.lock: | ||
return self.array[key] | ||
|
||
|
||
class CfGribDataStore(AbstractDataStore): | ||
""" | ||
Implements the ``xr.AbstractDataStore`` read-only API for a GRIB file. | ||
""" | ||
def __init__(self, filename, lock=None, **backend_kwargs): | ||
import cfgrib | ||
if lock is None: | ||
lock = ECCODES_LOCK | ||
self.lock = ensure_lock(lock) | ||
|
||
# NOTE: filter_by_keys is a dict, but CachingFileManager only accepts | ||
# hashable types. | ||
if 'filter_by_keys' in backend_kwargs: | ||
filter_by_keys_items = backend_kwargs['filter_by_keys'].items() | ||
backend_kwargs['filter_by_keys'] = tuple(filter_by_keys_items) | ||
|
||
self.ds = cfgrib.open_file(filename, mode='r', **backend_kwargs) | ||
|
||
def open_store_variable(self, name, var): | ||
if isinstance(var.data, np.ndarray): | ||
data = var.data | ||
else: | ||
wrapped_array = CfGribArrayWrapper(self, var.data) | ||
data = indexing.LazilyOuterIndexedArray(wrapped_array) | ||
|
||
encoding = self.ds.encoding.copy() | ||
encoding['original_shape'] = var.data.shape | ||
|
||
return Variable(var.dimensions, data, var.attributes, encoding) | ||
|
||
def get_variables(self): | ||
return FrozenOrderedDict((k, self.open_store_variable(k, v)) | ||
for k, v in self.ds.variables.items()) | ||
|
||
def get_attrs(self): | ||
return Frozen(self.ds.attributes) | ||
|
||
def get_dimensions(self): | ||
return Frozen(self.ds.dimensions) | ||
|
||
def get_encoding(self): | ||
dims = self.get_dimensions() | ||
encoding = { | ||
'unlimited_dims': {k for k, v in dims.items() if v is None}, | ||
} | ||
return encoding |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.