-
Notifications
You must be signed in to change notification settings - Fork 0
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
lukas-phaf
wants to merge
1
commit into
main
Choose a base branch
from
locations-small
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 | ||
|
@@ -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 = [ | ||
|
@@ -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. | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.