Skip to content

Commit

Permalink
add remove_nans
Browse files Browse the repository at this point in the history
  • Loading branch information
b8raoult committed Feb 10, 2025
1 parent 0277476 commit f103e08
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
5 changes: 1 addition & 4 deletions src/anemoi/transform/filters/cos_sin_mean_wave_direction.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ def __init__(
mean_wave_direction="mwd",
cos_mean_wave_direction="cos_mwd",
sin_mean_wave_direction="sin_mwd",
radians=False,
):
self.mean_wave_direction = mean_wave_direction
self.cos_mean_wave_direction = cos_mean_wave_direction
self.sin_mean_wave_direction = sin_mean_wave_direction
self.radians = radians

def forward(self, data):
return self._transform(
Expand All @@ -46,8 +44,7 @@ def backward(self, data):
def forward_transform(self, mwd):

data = mwd.to_numpy()
if not self.radians:
data = np.deg2rad(data)
data = np.deg2rad(data)

yield self.new_field_from_numpy(np.cos(data), template=mwd, param=self.cos_mean_wave_direction)
yield self.new_field_from_numpy(np.sin(data), template=mwd, param=self.sin_mean_wave_direction)
Expand Down
66 changes: 66 additions & 0 deletions src/anemoi/transform/filters/remove_nans.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# (C) Copyright 2025 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

import tqdm

from ..fields import new_field_from_latitudes_longitudes
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("remove_nans")
class RemoveNaNs(Filter):
"""A filter to regrid fields using earthkit-regrid."""

def __init__(self, *, method="mask", check=False):
self.method = method
self.check = check

assert method == "mask", f"Method {method} not implemented"
assert not check, "Check not implemented"

self._mask = None
self._latitudes = None
self._longitudes = None

def forward(self, fields):
import numpy as np

if self._mask is None:
first = fields[0]
data = first.to_numpy(flatten=True)
self._mask = ~np.isnan(data)

latitudes, longitudes = first.grid_points()
self._latitudes = latitudes[self._mask]
self._longitudes = longitudes[self._mask]

result = []
for field in tqdm.tqdm(fields, desc="Remove NaNs"):

data = field.to_numpy(flatten=True)
result.append(
new_field_from_latitudes_longitudes(
new_field_from_numpy(data[self._mask], template=field),
latitudes=self._latitudes,
longitudes=self._longitudes,
)
)

return new_fieldlist_from_list(result)

def backward(self, data):
raise NotImplementedError("IconRefinement is not reversible")

0 comments on commit f103e08

Please sign in to comment.