Skip to content
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
13 changes: 13 additions & 0 deletions src/earthkit/data/field/component/geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,19 @@ def area(self) -> tuple:
def grid_type(self) -> str | None:
return None

def set(self, *args, shape_hint=None, **kwargs) -> "GeographyBase":
"""Return a new GeographyBase object with updated values."""
kwargs = self._normalise_set_kwargs(*args, **kwargs)
keys = set(kwargs.keys())

if keys == {"shape"}:
return self.from_dict(**kwargs)

if keys == {"grid_spec"} or keys == {"latitudes", "longitudes"}:
return super().from_dict(kwargs, shape_hint=shape_hint)

raise ValueError(f"Invalid {keys=} for Geography specification")

@classmethod
def from_dict(cls, d) -> "EmptyGeography":
if not isinstance(d, dict):
Expand Down
28 changes: 28 additions & 0 deletions tests/list_of_dicts/test_lod_geography.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,34 @@ def test_lod_no_latlon(
assert ds[0].geography.latlons() == (None, None)


def test_lod_geo_set_on_nongeo_1():
from earthkit.data import Field

f = Field.from_dict({"parameter": {"variable": "t", "units": "K"}, "values": np.array([1.0, 2.0, 3.0])})

lats = np.array([10.0, 20.0, 30.0])
lons = np.array([100.0, 110.0, 120.0])

f_new = f.set({"geography.latitudes": lats, "geography.longitudes": lons})

assert np.allclose(f_new.geography.latitudes(), lats)
assert np.allclose(f_new.geography.longitudes(), lons)


def test_lod_geo_set_on_nongeo_2():
from earthkit.data import Field

f = Field.from_dict({"parameter": {"variable": "t", "units": "K"}, "values": np.array([1.0, 2.0, 3.0])})

lats = np.array([10.0, 20.0, 30.0])
lons = np.array([100.0, 110.0, 120.0])

f_new = f.set(geography={"latitudes": np.array([10.0, 20.0, 30.0]), "longitudes": np.array([100.0, 110.0, 120.0])})

assert np.allclose(f_new.geography.latitudes(), lats)
assert np.allclose(f_new.geography.longitudes(), lons)


if __name__ == "__main__":
from earthkit.data.utils.testing import main

Expand Down
Loading