Skip to content
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

Alter field metadata #493

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions src/earthkit/data/core/fieldlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,6 @@ def index(self, key):
class Field(Base):
r"""Represent a Field."""

def __init__(
self,
metadata=None,
):
self.__metadata = metadata

@abstractmethod
def _values(self, dtype=None):
r"""Return the raw values extracted from the underlying storage format
Expand Down Expand Up @@ -113,12 +107,10 @@ def values(self):
return self._flatten(self._values())

@property
@abstractmethod
def _metadata(self):
r"""Metadata: Get the object representing the field's metadata."""
if self.__metadata is None:
# TODO: remove this legacy method
self.__metadata = self._make_metadata()
return self.__metadata
self._not_implemented()

def to_numpy(self, flatten=False, dtype=None, index=None):
r"""Return the values stored in the field as an ndarray.
Expand Down Expand Up @@ -637,6 +629,16 @@ def metadata(self, *keys, astype=None, remapping=None, patches=None, **kwargs):
else:
return self._metadata.as_namespace(None)

@abstractmethod
def copy(self, **kwargs):
r"""Return a copy of the field.

Returns
-------
:obj:`Field`
"""
self._not_implemented()

def dump(self, namespace=all, **kwargs):
r"""Generate dump with all the metadata keys belonging to ``namespace``.

Expand Down Expand Up @@ -668,6 +670,28 @@ def dump(self, namespace=all, **kwargs):
"""
return self._metadata.dump(namespace=namespace, **kwargs)

def save(self, filename, append=False, **kwargs):
r"""Write the field into a file.

Parameters
----------
filename: str, optional
The target file path, if not defined attempts will be made to detect the filename
append: bool, optional
When it is true append data to the target file. Otherwise
the target file be overwritten if already exists. Default is False
**kwargs: dict, optional
Other keyword arguments passed to :obj:`write`.

See Also
--------
:obj:`write`

"""
flag = "wb" if not append else "ab"
with open(filename, flag) as f:
self.write(f, **kwargs)

def __getitem__(self, key):
"""Return the value of the metadata ``key``."""
return self._metadata.get(key)
Expand Down
Loading
Loading