Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add trim_cube process #278

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions openeo_processes_dask/process_implementations/cubes/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ def create_data_cube() -> RasterCube:
return xr.DataArray()


def trim_cube(data) -> RasterCube:
for dim in data.dims:
if (
dim in data.openeo.temporal_dims
or dim in data.openeo.band_dims
or dim in data.openeo.other_dims
):
values = data[dim].values
other_dims = [d for d in data.dims if d != dim]
available_data = values[(np.isnan(data)).all(dim=other_dims) == 0]
if len(available_data) == 0:
raise ValueError(f"Data contains NaN values only. ")
data = data.sel({dim: available_data})

return data


def dimension_labels(data: RasterCube, dimension: str) -> ArrayLike:
if dimension not in data.dims:
raise DimensionNotAvailable(
Expand Down
20 changes: 20 additions & 0 deletions tests/test_dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
drop_dimension,
rename_dimension,
rename_labels,
trim_cube,
)
from openeo_processes_dask.process_implementations.exceptions import (
DimensionLabelCountMismatch,
Expand Down Expand Up @@ -124,3 +125,22 @@ def test_rename_labels(temporal_interval, bounding_box, random_raster_data):
dimension="bands",
target=["B02", "B03", "B04", "B05", "B08", "B11", "B12"],
)


@pytest.mark.parametrize("size", [(30, 30, 20, 4)])
@pytest.mark.parametrize("dtype", [np.float32])
def test_trim_cube(temporal_interval, bounding_box, random_raster_data):
input_cube = create_fake_rastercube(
data=random_raster_data,
spatial_extent=bounding_box,
temporal_extent=temporal_interval,
bands=["B02", "B03", "B04", "B08"],
backend="dask",
)
input_cube[:, :, :, 2] = np.zeros((30, 30, 20)) * np.nan
output_cube = trim_cube(input_cube)
assert output_cube.shape == (30, 30, 20, 3)

all_nan = input_cube * np.nan
with pytest.raises(ValueError):
output_cube = trim_cube(all_nan)
Loading