Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Change /locations logic #101

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
42 changes: 24 additions & 18 deletions api/routers/edr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# For developing: uvicorn main:app --reload
import logging
from collections import defaultdict
from typing import Annotated
from typing import DefaultDict
Expand All @@ -20,6 +21,7 @@
from geojson_pydantic import Feature
from geojson_pydantic import Point
from grpc_getter import get_obs_request
from grpc_getter import get_ts_ag_request
from shapely import buffer
from shapely import geometry
from shapely import wkt
Expand All @@ -29,6 +31,11 @@
from utilities import validate_bbox
from utilities import verify_parameter_names


logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)


router = APIRouter(prefix="/collections/observations")

response_fields_needed_for_data_api = [
Expand Down Expand Up @@ -56,17 +63,27 @@
async def get_locations(
bbox: Annotated[str | None, Query(example="5.0,52.0,6.0,52.1")] = None
) -> EDRFeatureCollection: # Hack to use string
# TODO: Remove duplication between `/collections` endpoint
# Get all unique parameters
ts_request = dstore.GetTSAGRequest(attrs=["parameter_name", "standard_name", "unit", "level", "period", "function"])
ts_response = await get_ts_ag_request(ts_request)
# logger.info(ts_response.ByteSize())

# Sadly, this is a different parameter as in the /locations endpoint, due to an error in the EDR spec
# See: https://github.com/opengeospatial/ogcapi-environmental-data-retrieval/issues/427
all_parameters: Dict[str, Parameter] = {}

for group in ts_response.groups:
ts = group.combo
parameter = make_parameter(ts)
all_parameters[ts.parameter_name] = parameter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible that there is a duplicate parameter name? If so should then an exception be raised that it already exists?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is probably the right way/place to do it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it is possible. make_parameter creates the definition from variables used for parameter_name.


ts_request = dstore.GetObsRequest(
temporal_latest=True,
included_response_fields=[
"parameter_name",
"platform",
"geo_point",
"standard_name",
"unit",
"level",
"period",
"function",
],
)
# Add spatial area to the time series request if bbox exists.
Expand All @@ -78,28 +95,17 @@ async def get_locations(
)

ts_response = await get_obs_request(ts_request)
# logger.info(ts_response.ByteSize())

platform_parameters: DefaultDict[str, Set[str]] = defaultdict(set)
platform_coordinates: Dict[str, Set[Tuple[float, float]]] = defaultdict(set)
all_parameters: Dict[str, Parameter] = {}
for obs in ts_response.observations:
parameter = make_parameter(obs.ts_mdata)
platform_parameters[obs.ts_mdata.platform].add(obs.ts_mdata.parameter_name)
# Take last point
platform_coordinates[obs.ts_mdata.platform].add(
(obs.obs_mdata[-1].geo_point.lon, obs.obs_mdata[-1].geo_point.lat)
)
# Check for inconsistent parameter definitions between stations
# TODO: How to handle those?
if obs.ts_mdata.parameter_name in all_parameters and all_parameters[obs.ts_mdata.parameter_name] != parameter:
raise HTTPException(
status_code=500,
detail={
"parameter": f"Parameter with name {obs.ts_mdata.parameter_name} "
f"has multiple definitions:\n{all_parameters[obs.ts_mdata.parameter_name]}\n{parameter}"
},
)
all_parameters[obs.ts_mdata.parameter_name] = parameter
# TODO: How to check for inconsistent parameter definitions between stations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this possible to do on lines 76-79? Because if a parameter_name exists that is not unique, it is likely impossible to distinguish between them for stations.


# Check for multiple coordinates on one station
for station_id in platform_parameters.keys():
Expand Down