Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
37 changes: 15 additions & 22 deletions eodag/plugins/search/build_search_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ def query(
collection = prep.collection
if not collection:
collection = kwargs.get("collection")
kwargs = self._preprocess_search_params(kwargs, collection)
kwargs = self._preprocess_search_params(kwargs)
result = super().query(prep, **kwargs)
if prep.count and not result.number_matched:
result.number_matched = 1
Expand Down Expand Up @@ -529,9 +529,7 @@ def build_query_string(
collection=collection, query_dict=ordered_kwargs
)

def _preprocess_search_params(
self, params: dict[str, Any], collection: Optional[str]
) -> dict[str, Any]:
def _preprocess_search_params(self, params: dict[str, Any]) -> dict[str, Any]:
"""Preprocess search parameters before making a request to the CDS API.

This method is responsible for checking and updating the provided search parameters
Expand All @@ -540,7 +538,6 @@ def _preprocess_search_params(
in the input parameters, default values or values from the configuration are used.

:param params: Search parameters to be preprocessed.
:param collection: (optional) collection id
"""

_dc_qs = params.get("_dc_qs")
Expand Down Expand Up @@ -572,17 +569,6 @@ def _preprocess_search_params(
params[START] = format_date(start_date)
params[END] = format_date(end_date)

# start_datetime / computed from "date", "time", "year", "month", "day"
indirects = self._preprocess_indirect_date_parameters(params)
if params.get(START) is None:
value = indirects.get("start_datetime", None)
if value is not None:
params[START] = value
if params.get(END) is None:
value = indirects.get("end_datetime", None)
if value is not None:
params[END] = value

# adapt end date if it is midnight
if END in params:
end_date_excluded = getattr(self.config, "end_date_excluded", True)
Expand Down Expand Up @@ -649,9 +635,9 @@ def _preprocess_indirect_date_parameters(self, params: dict[str, Any]) -> dict:
if year is not None:
indirects["year"] = year
if month is not None:
indirects["month"] = month
indirects["month"] = [f"{int(m):02d}" for m in month]
if day is not None:
indirects["day"] = day
indirects["day"] = [f"{int(d):02d}" for d in day]

# Compute date range from "date" param (takes precedence)
date = params.get("date", None)
Expand All @@ -670,7 +656,10 @@ def _preprocess_indirect_date_parameters(self, params: dict[str, Any]) -> dict:

# Compute date range from year/month/day/time params
start, end = compute_date_range_from_params(
year=year, month=month, day=day, time=time
year=year,
month=indirects.get("month"),
day=indirects.get("day"),
time=time,
)
if start is not None:
indirects["start_datetime"] = start
Expand Down Expand Up @@ -738,9 +727,7 @@ def discover_queryables(

# extract default datetime and convert geometry
try:
processed_filters = self._preprocess_search_params(
deepcopy(filters), collection
)
processed_filters = self._preprocess_search_params(deepcopy(filters))
except Exception as e:
raise ValidationError(e.args[0]) from e

Expand Down Expand Up @@ -1262,6 +1249,12 @@ def normalize_results(
**{k: v for k, v in kwargs.items() if v is not None},
}

# start_datetime / computed from "date", "time", "year", "month", "day"
indirects = self._preprocess_indirect_date_parameters(result_data)
for key in (START, END):
if result_data.get(key) is None and indirects.get(key) is not None:
result_data[key] = indirects[key]

properties = properties_from_json(
result_data,
self.config.metadata_mapping,
Expand Down
24 changes: 24 additions & 0 deletions tests/units/test_search_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2799,6 +2799,30 @@ def test_plugins_search_ecmwfsearch_with_year_month_day_filter(self):
eoproduct.properties["ecmwf:day"],
["20", "21"],
)
# month with one digit
results = self.search_plugin.query(
prep=PreparedSearch(),
collection="ERA5_SL",
**{
"ecmwf:year": "2020",
"ecmwf:month": ["2"],
"ecmwf:day": ["20", "21"],
"ecmwf:time": ["01:00"],
},
)
eoproduct = results.data[0]
self.assertEqual(
eoproduct.properties["start_datetime"],
"2020-02-20T01:00:00.000Z",
)
self.assertEqual(
eoproduct.properties["end_datetime"],
"2020-02-21T01:00:00.000Z",
)
self.assertEqual(
eoproduct.properties["ecmwf:month"],
["2"],
)

def test_plugins_search_ecmwfsearch_collection_with_alias(self):
"""alias of collection must be used in search result"""
Expand Down
Loading