Skip to content

Commit

Permalink
add mask
Browse files Browse the repository at this point in the history
  • Loading branch information
clausmichele committed Oct 9, 2023
1 parent 6366e4d commit 5785811
Showing 1 changed file with 9 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import logging
from typing import Callable

Check warning on line 2 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#L1-L2

Added lines #L1 - L2 were not covered by tests

import numpy as np

Check warning on line 4 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#L4

Added line #L4 was not covered by tests

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 (

Check warning on line 8 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#L6-L8

Added lines #L6 - L8 were not covered by tests
DimensionLabelCountMismatch,
DimensionMismatch,
LabelMismatch,
)
from openeo_processes_dask.process_implementations.logic import _not

Check warning on line 13 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#L13

Added line #L13 was not covered by tests

logger = logging.getLogger(__name__)

Check warning on line 15 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#L15

Added line #L15 was not covered by tests

__all__ = ["mask"]

Check warning on line 17 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#L17

Added line #L17 was not covered by tests


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#L20-L22

Added lines #L20 - 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
Expand Down Expand Up @@ -46,41 +54,4 @@ def mask(data: RasterCube, mask: RasterCube, replacement=None) -> RasterCube:
f"data and mask temporal dimensions do no match: data has temporal dimensions ({data_temporal_dims}) and mask {mask_temporal_dims}"
)

#

# if data.openeo.band_dims[0] == "bands"
# if data.openeo.temporal_dims[0] == mask.openeo.temporal_dims[0]
assert output_cube.openeo.spatial_dims == ("y", "x")
assert output_cube.openeo.other_dims[0] == "other"

maskSource = node.arguments["mask"]["from_node"]
dataSource = node.arguments["data"]["from_node"]
# If the mask has a variable dimension, it will keep only the values of the input with the same variable name.
# Solution is to take the min over the variable dim to drop that dimension. (Problems if there are more than 1 band/variable)
if (
"variable" in self.partialResults[maskSource].dims
and len(self.partialResults[maskSource]["variable"]) == 1
):
mask = self.partialResults[maskSource].min(dim="variable")
else:
mask = self.partialResults[maskSource]
self.partialResults[node.id] = self.partialResults[dataSource].where(
np.logical_not(mask)
)
if "replacement" in node.arguments and node.arguments["replacement"] is not None:
burnValue = node.arguments["replacement"]
if isinstance(burnValue, int) or isinstance(burnValue, float):
self.partialResults[node.id] = self.partialResults[node.id].fillna(
burnValue
) # Replace the na with the burnValue

if dimension not in data.dims:
raise DimensionNotAvailable(
f"Provided dimension ({dimension}) not found in data.dims: {data.dims}"
)

labels = data[dimension].values
label_mask = condition(x=labels)
label = labels[label_mask]
data = data.sel(**{dimension: label})
return data
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

0 comments on commit 5785811

Please sign in to comment.