Skip to content

Commit 32729f6

Browse files
committed
experimental "timeseries" filter. Syntax is bad, and will change
1 parent aa6ec0a commit 32729f6

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

src/anemoi/transform/filters/sum.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@
88
# nor does it submit to any jurisdiction.
99

1010

11+
import logging
12+
1113
from . import filter_registry
1214
from .base import SimpleFilter
1315

16+
LOG = logging.getLogger(__name__)
17+
1418

1519
@filter_registry.register("sum")
1620
class Sum(SimpleFilter):
@@ -25,6 +29,7 @@ def __init__(
2529
assert len(formula) == 1
2630
self.name = list(formula.keys())[0]
2731
self.args = list(formula.values())[0]
32+
LOG.warning("Using the sum filter will be deprecated in the future. Please do not rely on it.")
2833

2934
def forward(self, data):
3035
return self._transform(data, self.forward_transform, *self.args)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# (C) Copyright 2024 Anemoi contributors.
2+
#
3+
# This software is licensed under the terms of the Apache Licence Version 2.0
4+
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
5+
#
6+
# In applying this licence, ECMWF does not waive the privileges and immunities
7+
# granted to it by virtue of its status as an intergovernmental organisation
8+
# nor does it submit to any jurisdiction.
9+
10+
11+
import logging
12+
13+
import numpy as np
14+
15+
from . import filter_registry
16+
from .base import SimpleFilter
17+
18+
LOG = logging.getLogger(__name__)
19+
20+
# class MyFilter(SuperSimpleFilter):
21+
# def __init__(self, *, param):
22+
# self.param = param
23+
#
24+
# def transform(date, tp, lsm):
25+
# new = tp + lsm + self.data(self.param, date)
26+
# return dict(q_500 = new)
27+
28+
29+
@filter_registry.register("timeseries")
30+
class Timeseries(SimpleFilter):
31+
"""A source to add a timeseries depending on time but not on location"""
32+
33+
def __init__(self, *, netcdf=None, template_param="2t"):
34+
if netcdf:
35+
import xarray as xr
36+
37+
self.ds = xr.open_dataset(netcdf["path"]) # .to_dataframe()
38+
LOG.warning("Using the timeseries filter will be deprecated in the future. Please do not rely on it.")
39+
40+
self.template_param = template_param
41+
42+
def forward(self, data):
43+
return self._transform(
44+
data,
45+
self.forward_transform,
46+
self.template_param,
47+
)
48+
49+
def forward_transform(self, template):
50+
"""Convert snow depth and snow density to snow cover"""
51+
dt = template.metadata("valid_datetime")
52+
template_array = template.to_numpy()
53+
54+
sel = self.ds.sel(time=dt)
55+
56+
for name in self.ds.data_vars:
57+
value = sel[name].values
58+
data = np.full_like(template_array, value)
59+
yield self.new_field_from_numpy(data, template=template, param=name)
60+
61+
def backward(self, data):
62+
raise NotImplementedError("SnowCover is not reversible")
63+
64+
def backward_transform(self, sd, rsn):
65+
raise NotImplementedError("SnowCover is not reversible")

0 commit comments

Comments
 (0)