|
| 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