Skip to content

Commit

Permalink
update changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
b8raoult committed Dec 18, 2024
1 parent 192e084 commit f031721
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Keep it human-readable, your future self will thank you!

## [Unreleased](https://github.com/ecmwf/anemoi-utils/transform/0.0.5...HEAD/compare/0.1.0...HEAD)

### Added

- Add regrid filter

## [0.1.0](https://github.com/ecmwf/anemoi-utils/transform/0.0.5...HEAD/compare/0.0.8...0.1.0) - 2024-11-18

## [0.0.8](https://github.com/ecmwf/anemoi-utils/transform/0.0.5...HEAD/compare/0.0.5...0.0.8) - 2024-10-26
Expand Down
27 changes: 9 additions & 18 deletions src/anemoi/transform/filters/rescale.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from . import filter_registry
from .base import SimpleFilter


class Rescale(SimpleFilter):
"""A filter to rescale a parameter from a scale and an offset, and back.
"""
"""A filter to rescale a parameter from a scale and an offset, and back."""

def __init__(
self,
Expand All @@ -27,11 +27,7 @@ def __init__(
self.param = param

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

def backward(self, data):
return self._transform(
Expand All @@ -46,26 +42,21 @@ def forward_transform(self, x):
rescaled = x.to_numpy() * self.scale + self.offset

yield self.new_field_from_numpy(rescaled, template=x, param=self.param)

def backward_transform(self, x):
"""ax+b to x"""

descaled = (x.to_numpy() - self.offset)/ self.scale
descaled = (x.to_numpy() - self.offset) / self.scale

yield self.new_field_from_numpy(descaled, template=x, param=self.param)


class Convert(Rescale):
"""A filter to convert a parameter in a given unit to another unit, and back.
"""
"""A filter to convert a parameter in a given unit to another unit, and back."""

def __init__(
self,
*,
unit_in,
unit_out,
param
):
def __init__(self, *, unit_in, unit_out, param):
from cfunits import Units

u0 = Units(unit_in)
u1 = Units(unit_out)
x1, x2 = 0.0, 1.0
Expand Down
16 changes: 6 additions & 10 deletions tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
# nor does it submit to any jurisdiction.


from anemoi.transform.filters.rescale import Rescale, Convert
import earthkit.data as ekd
from pytest import approx

from anemoi.transform.filters.rescale import Convert
from anemoi.transform.filters.rescale import Rescale


def test_rescale():
# rescale from K to °C
temp = ekd.from_source(
"mars", {"param": "2t", "levtype": "sfc", "dates": ["2023-11-17 00:00:00"]}
)
temp = ekd.from_source("mars", {"param": "2t", "levtype": "sfc", "dates": ["2023-11-17 00:00:00"]})
fieldlist = temp.to_fieldlist()
k_to_deg = Rescale(scale=1.0, offset=-273.15, param="2t")
rescaled = k_to_deg.forward(fieldlist)
Expand All @@ -31,9 +31,7 @@ def test_rescale():
deg_to_far = Rescale(scale=9 / 5, offset=32, param="2t")
rescaled_farheneit = deg_to_far.forward(rescaled)
assert rescaled_farheneit[0].values.min() == 9 / 5 * rescaled[0].values.min() + 32
assert rescaled_farheneit[0].values.std() == approx(
(9 / 5) * rescaled[0].values.std()
)
assert rescaled_farheneit[0].values.std() == approx((9 / 5) * rescaled[0].values.std())
# rescale from F to K
rescaled_back = k_to_deg.backward(deg_to_far.backward(rescaled_farheneit))
assert rescaled_back[0].values.min() == temp.values.min()
Expand All @@ -42,9 +40,7 @@ def test_rescale():

def test_convert():
# rescale from K to °C
temp = ekd.from_source(
"mars", {"param": "2t", "levtype": "sfc", "dates": ["2023-11-17 00:00:00"]}
)
temp = ekd.from_source("mars", {"param": "2t", "levtype": "sfc", "dates": ["2023-11-17 00:00:00"]})
fieldlist = temp.to_fieldlist()
k_to_deg = Convert(unit_in="K", unit_out="degC", param="2t")
rescaled = k_to_deg.forward(fieldlist)
Expand Down

0 comments on commit f031721

Please sign in to comment.