From 1091de53569752e1191ea5a0832d40c68cf0b505 Mon Sep 17 00:00:00 2001 From: Matthias Mohr Date: Mon, 16 Sep 2024 15:02:29 +0200 Subject: [PATCH] Support date-only, add TemporalExtentEmpty exception (#211) * Support date-only check whether the temporal extent is empty * add test * update specs * allow temporal interval * allow temporal interval --------- Co-authored-by: ValentinaHutter --- .../process_implementations/cubes/_filter.py | 29 +++++++++++++++---- openeo_processes_dask/specs/openeo-processes | 2 +- tests/test_filter.py | 7 +++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/openeo_processes_dask/process_implementations/cubes/_filter.py b/openeo_processes_dask/process_implementations/cubes/_filter.py index 60939cb3..6fa1f566 100644 --- a/openeo_processes_dask/process_implementations/cubes/_filter.py +++ b/openeo_processes_dask/process_implementations/cubes/_filter.py @@ -21,6 +21,7 @@ BandFilterParameterMissing, DimensionMissing, DimensionNotAvailable, + TemporalExtentEmpty, TooManyDimensions, ) @@ -69,15 +70,33 @@ def filter_temporal( # https://github.com/numpy/numpy/issues/23904 with warnings.catch_warnings(): warnings.filterwarnings("ignore", category=DeprecationWarning) - start_time = extent[0] - if start_time is not None: + if isinstance(extent, TemporalInterval): + start_time = extent.start + end_time = extent.end + else: + start_time = extent[0] + end_time = extent[1] + + if isinstance(start_time, str): + start_time = np.datetime64(start_time) + elif start_time is not None: start_time = start_time.to_numpy() - end_time = extent[1] - if end_time is not None: - end_time = extent[1].to_numpy() - np.timedelta64(1, "ms") + + if isinstance(end_time, str): + end_time = np.datetime64(end_time) + elif end_time is not None: + end_time = end_time.to_numpy() + # The second element is the end of the temporal interval. # The specified instance in time is excluded from the interval. # See https://processes.openeo.org/#filter_temporal + if end_time is not None: + end_time -= np.timedelta64(1, "ms") + + if start_time is not None and end_time is not None and end_time < start_time: + raise TemporalExtentEmpty( + "The temporal extent is empty. The second instant in time must always be greater/later than the first instant in time." + ) data = data.where(~np.isnat(data[applicable_temporal_dimension]), drop=True) filtered = data.loc[ diff --git a/openeo_processes_dask/specs/openeo-processes b/openeo_processes_dask/specs/openeo-processes index 66cfff4a..afa9c6b0 160000 --- a/openeo_processes_dask/specs/openeo-processes +++ b/openeo_processes_dask/specs/openeo-processes @@ -1 +1 @@ -Subproject commit 66cfff4a7c5111fda75afd7acad557bfae7bb38a +Subproject commit afa9c6b00e57481383c35c81a9d59e42344c3624 diff --git a/tests/test_filter.py b/tests/test_filter.py index 2b4c2f67..9b29906d 100644 --- a/tests/test_filter.py +++ b/tests/test_filter.py @@ -12,6 +12,7 @@ from openeo_processes_dask.process_implementations.cubes.reduce import reduce_dimension from openeo_processes_dask.process_implementations.exceptions import ( DimensionNotAvailable, + TemporalExtentEmpty, ) from tests.general_checks import general_output_checks from tests.mockdata import create_fake_rastercube @@ -50,6 +51,12 @@ def test_filter_temporal(temporal_interval, bounding_box, random_raster_data): data=input_cube, extent=temporal_interval_part, dimension="immissing" ) + with pytest.raises(TemporalExtentEmpty): + filter_temporal( + data=input_cube, + extent=["2018-05-31T23:59:59", "2018-05-15T00:00:00"], + ) + temporal_interval_open = TemporalInterval.parse_obj([None, "2018-05-03T00:00:00"]) output_cube = filter_temporal(data=input_cube, extent=temporal_interval_open)