|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +import xarray as xr |
| 4 | + |
| 5 | +from . import parameterized, requires_dask |
| 6 | + |
| 7 | +ntime = 365 * 30 |
| 8 | +nx = 50 |
| 9 | +ny = 50 |
| 10 | + |
| 11 | +rng = np.random.default_rng(0) |
| 12 | + |
| 13 | + |
| 14 | +class Align: |
| 15 | + def setup(self, *args, **kwargs): |
| 16 | + data = rng.standard_normal((ntime, nx, ny)) |
| 17 | + self.ds = xr.Dataset( |
| 18 | + {"temperature": (("time", "x", "y"), data)}, |
| 19 | + coords={ |
| 20 | + "time": xr.date_range("2000", periods=ntime), |
| 21 | + "x": np.arange(nx), |
| 22 | + "y": np.arange(ny), |
| 23 | + }, |
| 24 | + ) |
| 25 | + self.year = self.ds.time.dt.year |
| 26 | + self.idx = np.unique(rng.integers(low=0, high=ntime, size=ntime // 2)) |
| 27 | + self.year_subset = self.year.isel(time=self.idx) |
| 28 | + |
| 29 | + @parameterized(["join"], [("outer", "inner", "left", "right", "exact", "override")]) |
| 30 | + def time_already_aligned(self, join): |
| 31 | + xr.align(self.ds, self.year, join=join) |
| 32 | + |
| 33 | + @parameterized(["join"], [("outer", "inner", "left", "right")]) |
| 34 | + def time_not_aligned(self, join): |
| 35 | + xr.align(self.ds, self.year[-100:], join=join) |
| 36 | + |
| 37 | + @parameterized(["join"], [("outer", "inner", "left", "right")]) |
| 38 | + def time_not_aligned_random_integers(self, join): |
| 39 | + xr.align(self.ds, self.year_subset, join=join) |
| 40 | + |
| 41 | + |
| 42 | +class AlignCFTime(Align): |
| 43 | + def setup(self, *args, **kwargs): |
| 44 | + super().setup() |
| 45 | + self.ds["time"] = xr.date_range("2000", periods=ntime, calendar="noleap") |
| 46 | + self.year = self.ds.time.dt.year |
| 47 | + self.year_subset = self.year.isel(time=self.idx) |
| 48 | + |
| 49 | + |
| 50 | +class AlignDask(Align): |
| 51 | + def setup(self, *args, **kwargs): |
| 52 | + requires_dask() |
| 53 | + super().setup() |
| 54 | + self.ds = self.ds.chunk({"time": 100}) |
0 commit comments