Skip to content

Commit

Permalink
Alter field values
Browse files Browse the repository at this point in the history
  • Loading branch information
sandorkertesz committed Oct 23, 2024
1 parent 527ce1b commit 07d9d6d
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 23 deletions.
14 changes: 12 additions & 2 deletions src/earthkit/data/core/fieldlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ def _attributes(self, names, remapping=None, joiner=None, default=None):

# return {name: metadata(name) for name in names}

def to_field(self, array_backend=None, **kwargs):
def to_field(self, flatten=False, dtype=None, array_backend=None, values=None, **kwargs):
r"""Convert to a new :class:`Field`.
Parameters
Expand All @@ -748,7 +748,17 @@ def to_field(self, array_backend=None, **kwargs):
"""
from earthkit.data.sources.array_list import ArrayField

return ArrayField(self.to_array(array_backend=array_backend, **kwargs), self._metadata.override())
if values is None:
values = self.to_array(
flatten=flatten,
dtype=dtype,
array_backend=array_backend,
)

return ArrayField(
values,
self._metadata.override(**kwargs),
)

@staticmethod
def _flatten(v):
Expand Down
28 changes: 28 additions & 0 deletions src/earthkit/data/indexing/fieldlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,33 @@ def _metadata(self):
return self.__metadata


class NewFieldWrapper:
def __init__(self, field, values=None, **kwargs):
self._field = field
self.__values = values

if kwargs:
from earthkit.data.core.metadata import WrappedMetadata

self.__metadata = WrappedMetadata(field._metadata, extra=kwargs, owner=field)
else:
self.__metadata = field._metadata

def _values(self, dtype=None):
if self.__values is None:
return self._field._values(dtype=dtype)
else:
if dtype is None:
return self.__values
return self.__values.astype(dtype)

@property
def _metadata(self):
return self.__metadata

def _has_new_values(self):
return self.__values is not None


# For backwards compatibility
FieldArray = SimpleFieldList
8 changes: 4 additions & 4 deletions src/earthkit/data/readers/grib/codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import numpy as np

from earthkit.data.core.fieldlist import Field
from earthkit.data.indexing.fieldlist import NewFieldMetadataWrapper
from earthkit.data.indexing.fieldlist import NewFieldWrapper
from earthkit.data.readers.grib.metadata import GribFieldMetadata
from earthkit.data.utils.message import CodesHandle
from earthkit.data.utils.message import CodesMessagePositionIndex
Expand Down Expand Up @@ -326,12 +326,12 @@ def message(self):
return self.handle.get_buffer()

def copy(self, **kwargs):
return NewMetadataGribField(self, **kwargs)
return NewGribField(self, **kwargs)


class NewMetadataGribField(NewFieldMetadataWrapper, GribField):
class NewGribField(NewFieldWrapper, GribField):
def __init__(self, field, **kwargs):
NewFieldMetadataWrapper.__init__(self, field, **kwargs)
NewFieldWrapper.__init__(self, field, **kwargs)
self._handle = field._handle
GribField.__init__(
self,
Expand Down
8 changes: 4 additions & 4 deletions src/earthkit/data/readers/grib/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import eccodes

from earthkit.data.indexing.fieldlist import NewFieldMetadataWrapper
from earthkit.data.indexing.fieldlist import NewFieldWrapper
from earthkit.data.indexing.fieldlist import SimpleFieldList
from earthkit.data.readers import Reader
from earthkit.data.readers.grib.codes import GribCodesHandle
Expand Down Expand Up @@ -156,12 +156,12 @@ def from_buffer(buf):
)

def copy(self, **kwargs):
return NewMetadataGribFieldInMemory(self, **kwargs)
return NewGribFieldInMemory(self, **kwargs)


class NewMetadataGribFieldInMemory(NewFieldMetadataWrapper, GribFieldInMemory):
class NewGribFieldInMemory(NewFieldWrapper, GribFieldInMemory):
def __init__(self, field, **kwargs):
NewFieldMetadataWrapper.__init__(self, field, **kwargs)
NewFieldWrapper.__init__(self, field, **kwargs)
self._handle = field._handle
GribFieldInMemory.__init__(
self,
Expand Down
8 changes: 4 additions & 4 deletions src/earthkit/data/readers/netcdf/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from earthkit.data.core.geography import Geography
from earthkit.data.core.metadata import MetadataAccessor
from earthkit.data.core.metadata import RawMetadata
from earthkit.data.indexing.fieldlist import NewFieldMetadataWrapper
from earthkit.data.indexing.fieldlist import NewFieldWrapper
from earthkit.data.utils.bbox import BoundingBox
from earthkit.data.utils.dates import to_datetime

Expand Down Expand Up @@ -296,12 +296,12 @@ def tidy(x):
return tidy(self._ds[self._ds[self.variable].grid_mapping].attrs)

def copy(self, **kwargs):
return NewMetadataXarrayField(self, **kwargs)
return NewXarrayField(self, **kwargs)


class NewMetadataXarrayField(NewFieldMetadataWrapper, XArrayField):
class NewXarrayField(NewFieldWrapper, XArrayField):
def __init__(self, field, **kwargs):
NewFieldMetadataWrapper.__init__(self, field, **kwargs)
NewFieldWrapper.__init__(self, field, **kwargs)
XArrayField.__init__(self, field.ds, field.variable, field.slices, field.non_dim_coords)


Expand Down
8 changes: 4 additions & 4 deletions src/earthkit/data/sources/array_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import math

from earthkit.data.core.fieldlist import Field
from earthkit.data.indexing.fieldlist import NewFieldMetadataWrapper
from earthkit.data.indexing.fieldlist import NewFieldWrapper
from earthkit.data.utils.array import array_namespace

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -96,12 +96,12 @@ def __setstate__(self, state: dict):
self.__metadata = state.pop("_metadata")

def copy(self, **kwargs):
return NewMetadataArrayField(self, **kwargs)
return NewArrayField(self, **kwargs)


class NewMetadataArrayField(NewFieldMetadataWrapper, ArrayField):
class NewArrayField(NewFieldWrapper, ArrayField):
def __init__(self, field, **kwargs):
NewFieldMetadataWrapper.__init__(self, field, **kwargs)
NewFieldWrapper.__init__(self, field, **kwargs)
ArrayField.__init__(self, field._array, None)


Expand Down
8 changes: 4 additions & 4 deletions src/earthkit/data/sources/forcings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from earthkit.data.core.metadata import RawMetadata
from earthkit.data.decorators import cached_method
from earthkit.data.decorators import normalize
from earthkit.data.indexing.fieldlist import NewFieldMetadataWrapper
from earthkit.data.indexing.fieldlist import NewFieldWrapper
from earthkit.data.utils.dates import to_datetime

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -256,15 +256,15 @@ def _values(self, dtype=None):
return values

def copy(self, **kwargs):
return NewMetadataForcingField(self, **kwargs)
return NewForcingField(self, **kwargs)

def __repr__(self):
return "ForcingField(%s,%s,%s)" % (self.param, self.date, self.number)


class NewMetadataForcingField(NewFieldMetadataWrapper, ForcingField):
class NewForcingField(NewFieldWrapper, ForcingField):
def __init__(self, field, **kwargs):
NewFieldMetadataWrapper.__init__(self, field, **kwargs)
NewFieldWrapper.__init__(self, field, **kwargs)
ForcingField.__init__(self, field.maker, field.date, field.param, field.proc, number=field.number)


Expand Down
7 changes: 7 additions & 0 deletions src/earthkit/data/writers/grib.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def write(self, f, field, values=None, check_nans=True, bits_per_value=None):
if "generatingProcessIdentifier" not in md:
md["generatingProcessIdentifier"] = None

if values is None:
try:
if field._has_new_values():
values = field.values
except AttributeError:
pass

output.write(values, check_nans=check_nans, missing_value=field.handle.MISSING_VALUE, **md)


Expand Down
Loading

0 comments on commit 07d9d6d

Please sign in to comment.