Skip to content

Commit

Permalink
Remove dead code (#1582)
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikmakait authored Nov 5, 2024
1 parent 2a06ffd commit 20a2770
Showing 1 changed file with 0 additions and 61 deletions.
61 changes: 0 additions & 61 deletions tests/geospatial/test_climatology.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,72 +9,11 @@
METHOD = "explicit"
"""

import numpy as np
import xarray as xr
from coiled.credentials.google import CoiledShippedCredentials

from tests.geospatial.workloads.climatology import highlevel_api, rechunk_map_blocks


def compute_hourly_climatology(
ds: xr.Dataset,
) -> xr.Dataset:
hours = xr.DataArray(range(0, 24, 6), dims=["hour"])
window_weights = create_window_weights(61)
return xr.concat(
[compute_rolling_mean(select_hour(ds, hour), window_weights) for hour in hours],
dim=hours,
)


def compute_rolling_mean(ds: xr.Dataset, window_weights: xr.DataArray) -> xr.Dataset:
window_size = len(window_weights)
half_window_size = window_size // 2 # For padding
ds = xr.concat(
[
replace_time_with_doy(ds.sel(time=str(y)))
for y in np.unique(ds.time.dt.year)
],
dim="year",
)
ds = ds.fillna(ds.sel(dayofyear=365))
ds = ds.pad(pad_width={"dayofyear": half_window_size}, mode="wrap")
ds = ds.rolling(dayofyear=window_size, center=True).construct("window")
ds = ds.weighted(window_weights).mean(dim=("window", "year"))
return ds.isel(dayofyear=slice(half_window_size, -half_window_size))


def create_window_weights(window_size: int) -> xr.DataArray:
"""Create linearly decaying window weights."""
assert window_size % 2 == 1, "Window size must be odd."
half_window_size = window_size // 2
window_weights = np.concatenate(
[
np.linspace(0, 1, half_window_size + 1),
np.linspace(1, 0, half_window_size + 1)[1:],
]
)
window_weights = window_weights / window_weights.mean()
window_weights = xr.DataArray(window_weights, dims=["window"])
return window_weights


def replace_time_with_doy(ds: xr.Dataset) -> xr.Dataset:
"""Replace time coordinate with days of year."""
return ds.assign_coords({"time": ds.time.dt.dayofyear}).rename(
{"time": "dayofyear"}
)


def select_hour(ds: xr.Dataset, hour: int) -> xr.Dataset:
"""Select given hour of day from dataset."""
# Select hour
ds = ds.isel(time=ds.time.dt.hour == hour)
# Adjust time dimension
ds = ds.assign_coords({"time": ds.time.astype("datetime64[D]")})
return ds


def test_rechunk_map_blocks(
gcs_url,
scale,
Expand Down

0 comments on commit 20a2770

Please sign in to comment.