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

feat: add mask and fix resample_cube_spatial #165

Merged
merged 23 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
from .general import *
from .indices import *
from .load import *
from .mask import *
from .merge import *
from .reduce import *
57 changes: 57 additions & 0 deletions openeo_processes_dask/process_implementations/cubes/mask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import logging
from typing import Callable

import numpy as np

from openeo_processes_dask.process_implementations.cubes.utils import notnull
from openeo_processes_dask.process_implementations.data_model import RasterCube
from openeo_processes_dask.process_implementations.exceptions import (
DimensionLabelCountMismatch,
DimensionMismatch,
LabelMismatch,
)
from openeo_processes_dask.process_implementations.logic import _not

logger = logging.getLogger(__name__)

__all__ = ["mask"]


def mask(data: RasterCube, mask: RasterCube, replacement=None) -> RasterCube:
if replacement is None:
replacement = np.nan

Check warning on line 22 in openeo_processes_dask/process_implementations/cubes/mask.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/cubes/mask.py#L21-L22

Added lines #L21 - L22 were not covered by tests

# Check if spatial dimensions have the same name
data_spatial_dims = data.openeo.spatial_dims
mask_spatial_dims = mask.openeo.spatial_dims
if not set(data_spatial_dims) == set(mask_spatial_dims):
raise DimensionMismatch(

Check warning on line 28 in openeo_processes_dask/process_implementations/cubes/mask.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/cubes/mask.py#L25-L28

Added lines #L25 - L28 were not covered by tests
f"data and mask spatial dimensions do no match: data has spatial dimensions ({data_spatial_dims}) and mask {mask_spatial_dims}"
)
# Check if spatial labels correspond
for n in data.openeo.spatial_dims:
data_spatial_labels = data[n].values
mask_spatial_labels = mask[n].values
data_n_labels = len(data_spatial_labels)
mask_n_labels = len(mask_spatial_labels)

Check warning on line 36 in openeo_processes_dask/process_implementations/cubes/mask.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/cubes/mask.py#L32-L36

Added lines #L32 - L36 were not covered by tests

if not data_n_labels == mask_n_labels:
raise DimensionLabelCountMismatch(

Check warning on line 39 in openeo_processes_dask/process_implementations/cubes/mask.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/cubes/mask.py#L38-L39

Added lines #L38 - L39 were not covered by tests
f"data and mask spatial dimension labels don't match: data has ({data_n_labels}) labels and mask has {mask_n_labels} for dimension {n}."
)
if not all(data_spatial_labels == mask_spatial_labels):
raise LabelMismatch(

Check warning on line 43 in openeo_processes_dask/process_implementations/cubes/mask.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/cubes/mask.py#L42-L43

Added lines #L42 - L43 were not covered by tests
f"data and mask spatial dimension labels don't match for dimension {n}, i.e. the coordinate values are different."
)
# Check if temporal dimensions are present and check the names
data_temporal_dims = data.openeo.temporal_dims
mask_temporal_dims = mask.openeo.temporal_dims

Check warning on line 48 in openeo_processes_dask/process_implementations/cubes/mask.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/cubes/mask.py#L47-L48

Added lines #L47 - L48 were not covered by tests

if not set(data_temporal_dims) == set(mask_temporal_dims):

Check warning on line 50 in openeo_processes_dask/process_implementations/cubes/mask.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/cubes/mask.py#L50

Added line #L50 was not covered by tests
# To continue with a valid case, mask shouldn't have a temporal dimension, so that the mask will be applied to all the temporal labels
if len(mask_temporal_dims) != 0:
raise DimensionMismatch(

Check warning on line 53 in openeo_processes_dask/process_implementations/cubes/mask.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/cubes/mask.py#L52-L53

Added lines #L52 - L53 were not covered by tests
f"data and mask temporal dimensions do no match: data has temporal dimensions ({data_temporal_dims}) and mask {mask_temporal_dims}"
)

return data.where(_not(mask), replacement)

Check warning on line 57 in openeo_processes_dask/process_implementations/cubes/mask.py

View check run for this annotation

Codecov / codecov/patch

openeo_processes_dask/process_implementations/cubes/mask.py#L57

Added line #L57 was not covered by tests
8 changes: 8 additions & 0 deletions openeo_processes_dask/process_implementations/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,11 @@ class RedBandAmbiguous(OpenEOException):

class BandExists(OpenEOException):
pass


class DimensionMismatch(OpenEOException):
pass


class LabelMismatch(OpenEOException):
pass
2 changes: 1 addition & 1 deletion openeo_processes_dask/specs/openeo-processes
clausmichele marked this conversation as resolved.
Show resolved Hide resolved