Skip to content

Commit

Permalink
Merge branch 'release/0.21.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
floriankrb committed Feb 26, 2024
2 parents 78d26db + 26f776a commit 70836d0
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
5 changes: 1 addition & 4 deletions climetlab/readers/netcdf/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def to_bounding_box(self):
)

@cached_property
def projection(self):
def grid_mapping(self):
def tidy(x):
if isinstance(x, np.ndarray):
return x.tolist()
Expand All @@ -105,9 +105,6 @@ def tidy(x):
def grid_points(self):
return DataSet(self.owner.xr_dataset).grid_points(self.variable)

def grid_points_xy(self):
return DataSet(self.owner.xr_dataset).grid_points_xy(self.variable)

def to_numpy(self, reshape=True, dtype=None):
dimensions = dict((s.name, s.index) for s in self.slices)
values = self.owner.xr_dataset[self.variable].isel(dimensions).values
Expand Down
31 changes: 31 additions & 0 deletions climetlab/sources/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,37 @@ def cos_solar_zenith_angle(self, date):
)
return result.flatten()

def __getattr__(self, name):
if not "+" in name and not "-" in name:
# If we are here, we are looking for a method that does not exist,
# it has to be a method with a time delta.
raise AttributeError(name)
if "+" in name:
fname, delta = name.split("+")
sign = 1
if "-" in name:
fname, delta = name.split("-")
sign = -1
method = getattr(self, fname)

if delta.endswith("h"):
factor = 60
elif delta.endswith("d"):
factor = 24 * 60
else:
raise ValueError(f"Invalid time delta {delta} in {name}")

delta = delta[:-1]
delta = int(delta)
delta = datetime.timedelta(minutes=delta) * factor * sign

def wrapper(date):
date = date + delta
value = method(date)
return value

return wrapper


class ConstantField:
def __init__(
Expand Down
2 changes: 1 addition & 1 deletion climetlab/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.21.0
0.21.1
21 changes: 21 additions & 0 deletions tests/sources/test_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#

import datetime
import numpy as np

from climetlab import load_source
from climetlab.testing import climetlab_file
Expand Down Expand Up @@ -86,6 +87,26 @@ def test_constant_2():
assert len(ds) == len(params) * len(dates) * ntimes


def test_constant_3():
sample = load_source("file", climetlab_file("docs/examples/test.grib"))

date = sample[0].datetime()
date_plus_6h = date + datetime.timedelta(hours=6)
a = load_source("constants", sample, date=date_plus_6h, param="insolation")
b = load_source("constants", sample, date=date, param="insolation+6h")
assert np.all(a.to_numpy() == b.to_numpy())
assert a.unique_values("param")["param"] == ("insolation",)
assert b.unique_values("param")["param"] == ("insolation+6h",)

date = sample[0].datetime()
date_minus_30d = date + datetime.timedelta(days=-30)
a = load_source("constants", sample, date=date_minus_30d, param="insolation")
b = load_source("constants", sample, date=date, param="insolation-30d")
assert np.all(a.to_numpy() == b.to_numpy())
assert a.unique_values("param")["param"] == ("insolation",)
assert b.unique_values("param")["param"] == ("insolation-30d",)


if __name__ == "__main__":
from climetlab.testing import main

Expand Down

0 comments on commit 70836d0

Please sign in to comment.