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

adding sum(experimental) and timeseries(experimental). syntax will change. #15

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
53 changes: 53 additions & 0 deletions src/anemoi/transform/filters/sum.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 . import filter_registry
from .base import SimpleFilter

LOG = logging.getLogger(__name__)


@filter_registry.register("sum")
class Sum(SimpleFilter):
"""A filter to sum some parameters"""

def __init__(
self,
*,
formula,
):
assert isinstance(formula, dict)
assert len(formula) == 1
self.name = list(formula.keys())[0]
self.args = list(formula.values())[0]
LOG.warning("Using the sum filter will be deprecated in the future. Please do not rely on it.")

def forward(self, data):
return self._transform(data, self.forward_transform, *self.args)

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

def forward_transform(self, *args):
"""Sum the fuel components to get the total fuel"""
total = None
for arg in args:
if total is None:
template = arg
total = template.to_numpy()
else:
total += arg.to_numpy()

yield self.new_field_from_numpy(total, template=template, param=self.name)

def backward_transform(self, data):
raise NotImplementedError("Sum is not reversible")
65 changes: 65 additions & 0 deletions src/anemoi/transform/filters/timeseries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# (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

import numpy as np

from . import filter_registry
from .base import SimpleFilter

LOG = logging.getLogger(__name__)

# class MyFilter(SuperSimpleFilter):
# def __init__(self, *, param):
# self.param = param
#
# def transform(date, tp, lsm):
# new = tp + lsm + self.data(self.param, date)
# return dict(q_500 = new)


@filter_registry.register("timeseries")
class Timeseries(SimpleFilter):
"""A source to add a timeseries depending on time but not on location"""

def __init__(self, *, netcdf=None, template_param="2t"):
if netcdf:
import xarray as xr

self.ds = xr.open_dataset(netcdf["path"]) # .to_dataframe()
LOG.warning("Using the timeseries filter will be deprecated in the future. Please do not rely on it.")

self.template_param = template_param

def forward(self, data):
return self._transform(
data,
self.forward_transform,
self.template_param,
)

def forward_transform(self, template):
"""Convert snow depth and snow density to snow cover"""
dt = template.metadata("valid_datetime")
template_array = template.to_numpy()

sel = self.ds.sel(time=dt)

for name in self.ds.data_vars:
value = sel[name].values
data = np.full_like(template_array, value)
yield self.new_field_from_numpy(data, template=template, param=name)

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

def backward_transform(self, sd, rsn):
raise NotImplementedError("SnowCover is not reversible")
Loading