Skip to content

Commit

Permalink
Add regrid filter
Browse files Browse the repository at this point in the history
  • Loading branch information
b8raoult committed Dec 15, 2024
1 parent 638cb24 commit 261e29c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/anemoi/transform/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ def __init__(self, field, **kwargs):
def metadata(self, *args, **kwargs):

if kwargs.get("namespace"):
assert kwargs.get("namespace") == "mars", kwargs
assert len(args) == 0, (args, kwargs)
mars = self._field.metadata(**kwargs).copy()
for k in list(mars.keys()):
Expand Down
53 changes: 53 additions & 0 deletions src/anemoi/transform/filters/regrid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# (C) Copyright 2024 Anemoi contributors.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
#
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.


import logging

from ..fields import new_field_from_numpy
from ..fields import new_fieldlist_from_list
from ..filter import Filter
from . import filter_registry

LOG = logging.getLogger(__name__)


@filter_registry.register("regrid")
class RegridFilter(Filter):
"""A filter to regrid fields using earthkit-regrid."""

def __init__(self, *, in_grid=None, out_grid=None, method="linear"):
self.in_grid = in_grid
self.out_grid = out_grid
self.method = method

def forward(self, data):
return self._interpolate(data, self.in_grid, self.out_grid, self.method)

def backward(self, data):
return self._interpolate(data, self.out_grid, self.in_grid, self.method)

def _interpolate(self, data, in_grid, out_grid, method):
from earthkit.regrid import interpolate

result = []
for field in data:
result.append(
new_field_from_numpy(
interpolate(
field.to_numpy(flatten=True),
in_grid={"grid": in_grid},
out_grid={"grid": out_grid},
method=method,
),
template=field,
)
)

return new_fieldlist_from_list(result)

0 comments on commit 261e29c

Please sign in to comment.