Skip to content

Commit

Permalink
Add a filter_by_keys kwargs to open_dataset to filter the GRIB file i…
Browse files Browse the repository at this point in the history
…ndex before building the cube. See #2.
  • Loading branch information
alexamici committed Jul 25, 2018
1 parent 08914eb commit 9de74ef
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion cfgrib/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,9 @@ def dict_merge(master, update):
def build_dataset_components(
stream,
encode_parameter=False, encode_time=False, encode_vertical=False, encode_geography=False,
filter_by_keys={},
):
index = stream.index(ALL_KEYS)
index = stream.index(ALL_KEYS).subindex(filter_by_keys)
param_ids = index['paramId']
dimensions = collections.OrderedDict()
variables = collections.OrderedDict()
Expand Down Expand Up @@ -387,6 +388,7 @@ class Dataset(object):
encode_time = attr.attrib(default=True)
encode_vertical = attr.attrib(default=True)
encode_geography = attr.attrib(default=True)
filter_by_keys = attr.attrib(default={}, type=T.Dict[str, T.Any])

@classmethod
def frompath(cls, path, mode='r', **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions cfgrib/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ def getone(self, item):
raise ValueError("not one value for %r: %r" % (item, len(values)))
return values[0]

def subindex(self, dict_query={}, **query):
query.update(dict_query)
def subindex(self, filter_by_keys={}, **query):
query.update(filter_by_keys)
raw_query = [(self.index_keys.index(k), v) for k, v in query.items()]
offsets = collections.OrderedDict()
for header_values in self.offsets:
Expand Down
9 changes: 6 additions & 3 deletions cfgrib/xarray_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,15 @@ def get_encoding(self):
return encoding


def open_dataset(path, flavour_name='ecmwf', **kwargs):
overrides = {}
def open_dataset(path, flavour_name='ecmwf', filter_by_keys={}, **kwargs):
overrides = {
'flavour_name': flavour_name,
'filter_by_keys': filter_by_keys,
}
for k in list(kwargs): # copy to allow the .pop()
if k.startswith('encode_'):
overrides[k] = kwargs.pop(k)
store = GribDataStore.frompath(path, flavour_name=flavour_name, **overrides)
store = GribDataStore.frompath(path, **overrides)
return _open_dataset(store, **kwargs)


Expand Down

0 comments on commit 9de74ef

Please sign in to comment.