data.core.metadata

Classes

Metadata

Base class to represent metadata.

RawMetadata

Metadata implementation based on key/value pairs.

Module Contents

class data.core.metadata.Metadata

Base class to represent metadata.

Metadata is a dict-like immutable object. Modification is possible with override, which always creates a new object.

Implemented in subclasses: RawMetadata, GribMetadata.

Parameters:

cache (bool, dict-like, optional) – Enable caching of all the calls to get(). Default is False. The cache is attached to the instance.

Examples

abstract as_namespace(namespace=None)

Return all the keys/values from a namespace.

Parameters:

namespace (str, None) – The namespace. When namespace is None or an empty str all the available keys/values are returned.

Returns:

All the keys/values from the namespace.

Return type:

dict

abstract base_datetime()
abstract data_format()

Return the underlying data format.

Return type:

str

abstract datetime()

Return the date and time of the field.

Returns:

Dict with items “base_time” and “valid_time”.

Return type:

dict of datatime.datetime

Examples

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "tests/data/t_time_series.grib")
>>> ds[4].datetime()
{'base_time': datetime.datetime(2020, 12, 21, 12, 0),
'valid_time': datetime.datetime(2020, 12, 21, 18, 0)}
abstract describe_keys()

Return the keys to be used with the describe() method.

abstract dump(**kwargs)

Generate a dump from the metadata content.

abstract property geography

Get geography description.

If it is not available None is returned.

Type:

Geography

abstract get(key, default=None, *, astype=None, raise_on_missing=False)

Return the value for key.

Parameters:
  • key (str) – Metadata key

  • default (value) – Specify the default value for key. Returned when key is not found or its value is a missing value and raise_on_missing is False.

  • astype (type as str, int or float) – Return/access type for key. When it is supported astype is passed to the underlying metadata accessor as an option. Otherwise the value is cast to astype after it is taken from the accessor.

  • raise_on_missing (bool) – When it is True raises an exception if key is not found or it has a missing value.

Returns:

Returns the key value. Returns default if key is not found or it has a missing value and raise_on_missing is False.

Return type:

value

Raises:

KeyError – If raise_on_missing is True and key is not found or it has a missing value.

abstract index_keys()

Return the keys to be used with the indices() method.

abstract items()

Return the metadata items.

Return type:

Iterable of (key,value) pairs

abstract keys()

Return the metadata keys.

Return type:

Iterable of str

abstract ls_keys()

Return the keys to be used with the ls() method.

abstract namespaces()

Return the available namespaces.

Return type:

list of str

abstract override(*args, **kwargs)

Change the metadata values and return a new object.

override accepts another Metadata or a dict or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the metadata is then updated with those key/value pairs.

Examples

>>> other = RawMetadata({"key1": 1, "key2": 2})
>>> m1 = m.override(other)
>>> m1 = m.override({"key1": 1, "key2": 2})
>>> m1 = m.override([("key1", 1), ("key2", 2)])
>>> m1 = m.override(key1=1, key2=2)
abstract valid_datetime()
class data.core.metadata.RawMetadata(*args, **kwargs)

Bases: Metadata

Metadata implementation based on key/value pairs.

>>> from earthkit.data.core.metadata import RawMetadata
>>> md = RawMetadata({"shortName": "2t", "perturbationNumber": 5})
>>> md = RawMetadata([("shortName", "2t"), ("perturbationNumber", 5)])
>>> md = RawMetadata(shortName="2t", perturbationNumber=5)
>>> md
RawMetadata({'shortName': '2t', 'perturbationNumber': 5})
>>> md2 = RawMetadata(md)
>>> md2
RawMetadata({'shortName': '2t', 'perturbationNumber': 5})

Value access:

>>> "shortName" in md
True
>>> md["shortName"]
'2t'
>>> md.get("shortName")
'2t'
>>> "step" in md
False
>>> md.get("step")
>>> md.get("step", 0)
0

Examples

as_namespace(namespace)

Return all the keys/values from a namespace.

Parameters:

namespace (str, None) – The namespace. When namespace is None or an empty str all the available keys/values are returned.

Returns:

All the keys/values from the namespace.

Return type:

dict

base_datetime()
data_format()

Return the underlying data format.

Return type:

str

datetime()

Return the date and time of the field.

Returns:

Dict with items “base_time” and “valid_time”.

Return type:

dict of datatime.datetime

Examples

>>> import earthkit.data
>>> ds = earthkit.data.from_source("file", "tests/data/t_time_series.grib")
>>> ds[4].datetime()
{'base_time': datetime.datetime(2020, 12, 21, 12, 0),
'valid_time': datetime.datetime(2020, 12, 21, 18, 0)}
describe_keys()

Return the keys to be used with the describe() method.

dump(**kwargs)

Generate a dump from the metadata content.

property geography

Get geography description.

If it is not available None is returned.

Type:

Geography

get(key, default=None, *, astype=None, raise_on_missing=False)

Return the value for key.

Parameters:
  • key (str) – Metadata key

  • default (value) – Specify the default value for key. Returned when key is not found or its value is a missing value and raise_on_missing is False.

  • astype (type as str, int or float) – Return/access type for key. When it is supported astype is passed to the underlying metadata accessor as an option. Otherwise the value is cast to astype after it is taken from the accessor.

  • raise_on_missing (bool) – When it is True raises an exception if key is not found or it has a missing value.

Returns:

Returns the key value. Returns default if key is not found or it has a missing value and raise_on_missing is False.

Return type:

value

Raises:

KeyError – If raise_on_missing is True and key is not found or it has a missing value.

index_keys()

Return the keys to be used with the indices() method.

items()

Return the metadata items.

Return type:

Iterable of (key,value) pairs

keys()

Return the metadata keys.

Return type:

Iterable of str

ls_keys()

Return the keys to be used with the ls() method.

namespaces()

Return the available namespaces.

Return type:

list of str

override(*args, **kwargs)

Change the metadata values and return a new object.

override accepts another Metadata or a dict or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the metadata is then updated with those key/value pairs.

Examples

>>> other = RawMetadata({"key1": 1, "key2": 2})
>>> m1 = m.override(other)
>>> m1 = m.override({"key1": 1, "key2": 2})
>>> m1 = m.override([("key1", 1), ("key2", 2)])
>>> m1 = m.override(key1=1, key2=2)
valid_datetime()