Skip to content

Commit

Permalink
Added an automatic converter for old queries
Browse files Browse the repository at this point in the history
  • Loading branch information
GermanoGuerrini committed Feb 14, 2024
1 parent f0bc08e commit 7dca966
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
7 changes: 7 additions & 0 deletions hda/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import requests
from tqdm import tqdm

from hda.utils import convert

BROKER_URL = "https://gateway.prod.wekeo2.eu/hda-broker/"
ITEMS_PER_PAGE = 100

Expand Down Expand Up @@ -621,6 +623,11 @@ def search(self, query, limit=None):
:return: An :class:`hda.api.SearchResults` instance
"""
# Users can pass in a query in v1 format and we try to convert it
# into the new one. If the query is already in v2 format, this is
# a no-op.
# This might be removed in future version.
query = convert(query)
assert "dataset_id" in query, "Missing dataset_id, check your query"
self.accept_tac(query["dataset_id"])
results = SearchPaginator(self.post).run(query=query, limit=limit)
Expand Down
81 changes: 81 additions & 0 deletions hda/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import logging

logger = logging.getLogger(__name__)


def convert(query):
"""Converts a query in HDA v1 format into an HDA v2 one.
Might not work in every case.
If a v2 query is passed in, it is returned as is."""
dataset_id = query.get("datasetId")

if dataset_id is None and "dataset_id" in query:
return query

logger.warning(
"The submitted query is still in HDA v1 format. "
"It will be automatically converted, if possible, "
"but it is recommended to update it manually."
)

# Modify the JSON query according to the new structure
new_query = {"dataset_id": dataset_id}

# Check if the original JSON has the "dateRangeSelectValues" field
if "dateRangeSelectValues" in query:
# Loop through the dateRangeSelectValues and add fields to new_query
for date_range_value in query["dateRangeSelectValues"]:
name = date_range_value["name"].lower()
start = date_range_value["start"]
end = date_range_value["end"]

if dataset_id.startswith("EO:ECMWF:DAT:CAMS_"):
new_query["dtstart"] = start
new_query["dtend"] = end
elif dataset_id.startswith("EO:CLMS:DAT:CGLS"):
new_query["start"] = start
new_query["end"] = end
elif dataset_id.startswith("EO:MO:DAT:"):
new_query["min_date"] = start
new_query["max_date"] = end
elif dataset_id.startswith("EO:EUM:DAT:"):
new_query["dtstart"] = start
new_query["dtend"] = end
elif dataset_id.startswith("EO:EEA:DAT:CLMS_HRVPP"):
new_query["start"] = start
new_query["end"] = end
elif dataset_id.startswith("EO:CNES:DAT:SWH"):
new_query["creationDateStart"] = start
new_query["creationDateEnd"] = end
else:
new_query[f"{name}_start"] = start
new_query[f"{name}_end"] = end

if "boundingBoxValues" in query:
new_query["bbox"] = query["boundingBoxValues"][0]["bbox"]

# Add a "test" field for stringInputValues
if "stringInputValues" in query:
# Loop through the stringInputValues and add fields to new_query
for string_input_value in query["stringInputValues"]:
name = string_input_value["name"]
value = string_input_value["value"]
new_query[name] = value

# Add a "test" field for stringChoiceValues
if "stringChoiceValues" in query:
# Loop through the stringChoiceValues and add fields to new_query
for string_choice_value in query["stringChoiceValues"]:
name = string_choice_value["name"]
value = string_choice_value["value"]
new_query[name] = value

# Add a "test" field for multiStringSelectValues
if "multiStringSelectValues" in query:
# Loop through the stringChoiceValues and add fields to new_query
for multi_string_select_value in query["multiStringSelectValues"]:
name = multi_string_select_value["name"]
value = multi_string_select_value["value"]
new_query[name] = value

return new_query

0 comments on commit 7dca966

Please sign in to comment.