Skip to content

Commit

Permalink
Change /locations to first get all unique parameters, and then the ac…
Browse files Browse the repository at this point in the history
…tual stations, making the connection only by parameter_name.
  • Loading branch information
lukas-phaf committed Mar 27, 2024
1 parent b85a220 commit 1312be1
Showing 1 changed file with 24 additions and 18 deletions.
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

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

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

1 comment on commit 1312be1

@github-actions
Copy link

Choose a reason for hiding this comment

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

API Unit Test Coverage Report
FileStmtsMissCoverMissing
\_\_init\_\_.py00100% 
datastore_pb2.py584621%24–69
datastore_pb2_grpc.py432347%37–52, 85–87, 92–94, 99–101, 106–108, 112–136, 174, 191, 208, 225
grpc_getter.py201145%15–16, 20–23, 27–29, 33–35
locustfile.py15150%1–31
main.py34585%41, 51–52, 62–63
metadata_endpoints.py552555%42–51, 55, 72–151, 155
utilities.py683253%15, 33, 40, 62–65, 73–80, 85–92, 104–123
custom_geo_json
   edr_feature_collection.py60100% 
formatters
   \_\_init\_\_.py110100% 
   covjson.py53198%75
   geojson.py15193%42
routers
   \_\_init\_\_.py00100% 
   edr.py1112875%68–140, 225–226, 270–271
   feature.py451958%70–103, 113–118, 124–146
TOTAL53420661% 

API Unit Test Coverage Summary

Tests Skipped Failures Errors Time
17 0 💤 0 ❌ 0 🔥 2.119s ⏱️

Please sign in to comment.