diff --git a/openeo_processes_dask/process_implementations/cubes/general.py b/openeo_processes_dask/process_implementations/cubes/general.py index 91ca5d6..04a2011 100644 --- a/openeo_processes_dask/process_implementations/cubes/general.py +++ b/openeo_processes_dask/process_implementations/cubes/general.py @@ -158,30 +158,44 @@ def rename_labels( raise DimensionNotAvailable( f"Provided dimension ({dimension}) not found in data.dims: {data_rename.dims}" ) - if source: + if len(source) > 0: if len(source) != len(target): raise Exception( f"LabelMismatch - The number of labels in the parameters `source` and `target` don't match." ) + time = False + if dimension in data.openeo.temporal_dims: + time = True + source_labels = data_rename[dimension].values + if time: + source_labels = np.array(source_labels, dtype="datetime64[s]") + elif np.issubdtype(source_labels.dtype, np.datetime64): + source_labels = source_labels.astype("datetime64[s]") + time = True if isinstance(source_labels, np.ndarray): source_labels = source_labels.tolist() if isinstance(target, np.ndarray): target = target.tolist() - + if time: + source = np.array(source, dtype="datetime64[s]") + if isinstance(source, np.ndarray): + if np.issubdtype(source.dtype, np.datetime64): + source = source.astype("datetime64[s]") + source = source.tolist() target_values = [] for label in source_labels: if label in target: raise Exception(f"LabelExists - A label with the specified name exists.") - if source: + if len(source) > 0: if label in source: target_values.append(target[source.index(label)]) else: target_values.append(label) - if not source: + if len(source) == 0: if len(source_labels) == len(target): data_rename[dimension] = target elif len(target) < len(source_labels): diff --git a/pyproject.toml b/pyproject.toml index 4f87115..8a2e8d3 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "openeo-processes-dask" -version = "2024.11.4" +version = "2024.11.5" description = "Python implementations of many OpenEO processes, dask-friendly by default." authors = ["Lukas Weidenholzer ", "Sean Hoyal ", "Valentina Hutter "] maintainers = ["EODC Staff "] diff --git a/tests/test_dimensions.py b/tests/test_dimensions.py index 37ae373..2210f92 100644 --- a/tests/test_dimensions.py +++ b/tests/test_dimensions.py @@ -1,13 +1,7 @@ import numpy as np import pytest -from openeo_processes_dask.process_implementations.cubes.general import ( - add_dimension, - drop_dimension, - rename_dimension, - rename_labels, - trim_cube, -) +from openeo_processes_dask.process_implementations.cubes.general import * from openeo_processes_dask.process_implementations.exceptions import ( DimensionLabelCountMismatch, DimensionNotAvailable, @@ -127,6 +121,29 @@ def test_rename_labels(temporal_interval, bounding_box, random_raster_data): ) +@pytest.mark.parametrize("size", [(30, 30, 2, 4)]) +@pytest.mark.parametrize("dtype", [np.float32]) +def test_rename_labels_time(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", + ) + + t_labels = dimension_labels(input_cube, dimension="t") + output_cube = rename_labels( + input_cube, dimension="t", source=t_labels, target=["first_date", "second_date"] + ) + assert "first_date" in output_cube["t"].values + + output_cube_2 = rename_labels( + input_cube, dimension="t", source=[t_labels[-1]], target=["second_date"] + ) + assert "second_date" in output_cube_2["t"].values + + @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):