data.core.metadata
Classes
Base class to represent metadata. |
|
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 toget(). 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 emptystrall 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:
dictofdatatime.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 keydefault (
value) – Specify the default value forkey. Returned whenkeyis not found or its value is a missing value and raise_on_missing isFalse.astype (
type as str,intorfloat) – Return/access type forkey. When it is supportedastypeis passed to the underlying metadata accessor as an option. Otherwise the value is cast toastypeafter it is taken from the accessor.raise_on_missing (
bool) – When it is True raises an exception ifkeyis not found or it has a missing value.
- Returns:
Returns the
keyvalue. Returnsdefaultifkeyis not found or it has a missing value andraise_on_missingis False.- Return type:
value- Raises:
KeyError – If
raise_on_missingis True andkeyis 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:
Iterableof(key,value)pairs
- abstract keys()
Return the metadata keys.
- Return type:
Iterableofstr
- abstract ls_keys()
Return the keys to be used with the
ls()method.
- abstract namespaces()
Return the available namespaces.
- Return type:
listofstr
- abstract override(*args, **kwargs)
Change the metadata values and return a new object.
overrideaccepts anotherMetadataor 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:
MetadataMetadata 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 emptystrall 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:
dictofdatatime.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 keydefault (
value) – Specify the default value forkey. Returned whenkeyis not found or its value is a missing value and raise_on_missing isFalse.astype (
type as str,intorfloat) – Return/access type forkey. When it is supportedastypeis passed to the underlying metadata accessor as an option. Otherwise the value is cast toastypeafter it is taken from the accessor.raise_on_missing (
bool) – When it is True raises an exception ifkeyis not found or it has a missing value.
- Returns:
Returns the
keyvalue. Returnsdefaultifkeyis not found or it has a missing value andraise_on_missingis False.- Return type:
value- Raises:
KeyError – If
raise_on_missingis True andkeyis 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:
Iterableof(key,value)pairs
- keys()
Return the metadata keys.
- Return type:
Iterableofstr
- ls_keys()
Return the keys to be used with the
ls()method.
- namespaces()
Return the available namespaces.
- Return type:
listofstr
- override(*args, **kwargs)
Change the metadata values and return a new object.
overrideaccepts anotherMetadataor 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()