Skip to content

Commit

Permalink
Merge pull request #213 from ecmwf/feature/contants-fieldlist
Browse files Browse the repository at this point in the history
Feature/contants fieldlist
  • Loading branch information
sandorkertesz authored Oct 3, 2023
2 parents c03643a + 76082a7 commit 5757f27
Show file tree
Hide file tree
Showing 15 changed files with 1,237 additions and 3 deletions.
16 changes: 15 additions & 1 deletion earthkit/data/arguments/earthkit_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,17 @@ class IntType(_IntType, NonListMixin):


class IntListType(_IntType, ListMixin):
pass
def cast(self, value):
if isinstance(value, str) and "/" in value:
bits = value.split("/")
if len(bits) == 3 and bits[1].lower() == "to":
value = list(range(int(bits[0]), int(bits[2]) + 1, 1))

elif len(bits) == 5 and bits[1].lower() == "to" and bits[3].lower() == "by":
value = list(
range(int(bits[0]), int(bits[2]) + int(bits[4]), int(bits[4]))
)
return super().cast(value)


class IntSingleOrListType(_IntType, SingleOrListMixin):
Expand All @@ -194,6 +204,10 @@ class FloatSingleOrListType(_FloatType, SingleOrListMixin):

class _DateType(Type):
def _format(self, value, format):
if format == "datetime.datetime":
return value
if format == datetime.datetime:
return value
return value.strftime(format)

def include_args(self, decorator, args):
Expand Down
32 changes: 32 additions & 0 deletions earthkit/data/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import logging
from abc import abstractmethod
from collections import defaultdict

LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -97,6 +98,37 @@ def order_by(self, *args, **kwargs):
"""Reorder the elements of the object."""
self._not_implemented()

def unique_values(self, *coords, remapping=None, progress_bar=True):
"""
Given a list of metadata attributes, such as date, param, levels,
returns the list of unique values for each attributes
"""
from earthkit.data.core.order import build_remapping
from earthkit.data.utils import progress_bar

assert len(coords)
assert all(isinstance(k, str) for k in coords), coords

remapping = build_remapping(remapping)
iterable = self

if progress_bar:
iterable = progress_bar(
iterable=self,
desc=f"Finding coords in dataset for {coords}",
)

dic = defaultdict(dict)
for f in iterable:
metadata = remapping(f.metadata)
for k in coords:
v = metadata(k)
dic[k][v] = True

dic = {k: tuple(values.keys()) for k, values in dic.items()}

return dic

# @abstractmethod
# def to_points(self, *args, **kwargs):
# self._not_implemented()
Expand Down
5 changes: 3 additions & 2 deletions earthkit/data/core/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,11 @@ def _get_internal_key(self, key, astype=None, default=None, raise_on_missing=Tru
def _get_custom_key(self, key, default=None, raise_on_missing=True):
try:
if key == DATETIME:
return True, self.datetime().get("valid_time")
if key not in self:
return True, self.datetime().get("valid_time")
except Exception as e:
if not raise_on_missing:
return default
return True, default
else:
raise KeyError(f"{key}, reason={e}")
return False, None
Expand Down
Loading

0 comments on commit 5757f27

Please sign in to comment.