-
-
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 18 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,97 @@ | ||
# | ||
# Copyright 2017-2018 European Centre for Medium-Range Weather Forecasts. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# Authors: | ||
# Alessandro Amici - B-Open - https://bopen.eu | ||
# | ||
|
||
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): | ||
alexamici marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really object to this notice, but we don't include it in other source code files for xarray so it looks a little out of place (perhaps we should?). Everything is Apache 2 licensed already, and owned by contributors (or whoever they assign it to, such as an employer).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how to handle this one. ECMWF is quite sensitive to license and IPR matters so I added the licence boilerplate to all cfgrib files and later I simply copied the existing backend code as part of the PR.
I'm the material author of the code and my name will appear in the contributors, but I've been working fully funded by ECMWF as an external contractor, so it looks like proper attribution would be lost in this case.
I need to ask @StephanSiemen if they object to removing the copyright notice or what else they propose. To my knowledge this is the first contribution to an external Open Source project funded by ECMWF this way and we are learning how to handle these kind of details as we go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It says "Copyright 2014-2018, xarray Developers" in our README.rst file, which was modeled off projects like NumPy: https://github.com/numpy/numpy/blob/master/LICENSE.txt
I see now that our LICENSE file just has the original Apache license text. Perhaps we should add in the more specific "Copyright xarray developers" line, like a project like TensorFlow: https://github.com/tensorflow/tensorflow/blob/master/LICENSE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexamici (and ECMWF by proxy I suppose) - first, I want to make sure you understand that I'm very encouraged by your recent developments of cfgrib as an open source package and your contributions to xarray. They are much appreciated and they stand to benefit a wide swath of the geoscience community.
That said, at this point, I'm somewhat against adding this copyright/license header for these reasons:
Now, I understand that it can be important for organizations to make visible their open source contributions so we want to make sure this sort of engagement can continue to happen. We've recently joined NUMFOCUS, in part to give us access to some proper legal advice when necessary. We can certainly solicit their advice here if we have technical questions.
It's probably worth pinging the rest of @pydata/xarray to get their thoughts here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexamici, @shoyer, @jhamman,
We at ECMWF are very happy for the copyright to be adjusted according to other contributions in xarray. Any acknowledgement of the contribution is much appreciated. Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like I was being overzealous :)
I removed the copyright notice and licence boilerplate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @alexamici and @StephanSiemen!