Skip to content

Commit

Permalink
Support date-only, add TemporalExtentEmpty exception (#211)
Browse files Browse the repository at this point in the history
* Support date-only check whether the temporal extent is empty

* add test

* update specs

* allow temporal interval

* allow temporal interval

---------

Co-authored-by: ValentinaHutter <[email protected]>
  • Loading branch information
m-mohr and ValentinaHutter authored Sep 16, 2024
1 parent 8512783 commit 1091de5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
29 changes: 24 additions & 5 deletions openeo_processes_dask/process_implementations/cubes/_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
BandFilterParameterMissing,
DimensionMissing,
DimensionNotAvailable,
TemporalExtentEmpty,
TooManyDimensions,
)

Expand Down Expand Up @@ -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[
Expand Down
2 changes: 1 addition & 1 deletion openeo_processes_dask/specs/openeo-processes
7 changes: 7 additions & 0 deletions tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 1091de5

Please sign in to comment.