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

Issue 105 deal with multiple locations #108

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,5 @@ datastore_pb2.py
datastore_pb2_grpc.py
# Ignore all the copied protobuf directories, as the root contains the source of truth.
**/protobuf
# Ignore nvim styling files
.stylua.toml
26 changes: 19 additions & 7 deletions api/routers/edr.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from utilities import split_and_strip
from utilities import validate_bbox
from utilities import verify_parameter_names
from utilities import calculate_largest_postition_deviation

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

Expand Down Expand Up @@ -97,6 +98,7 @@ async def get_locations(
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:
Expand All @@ -112,13 +114,23 @@ async def get_locations(
# Check for multiple coordinates or names on one station
for station_id in platform_parameters.keys():
if len(platform_coordinates[station_id]) > 1:
raise HTTPException(
status_code=500,
detail={
"coordinates": f"Station with id `{station_id} "
f"has multiple coordinates: {platform_coordinates[station_id]}"
},
)
if (
calculate_largest_postition_deviation(platform_coordinates[station_id]) < 1e-4
): # all coordinates are within 1e-4 degrees (roughly 10 m)
platform_coordinates[station_id] = {
sorted(
[i for i in platform_coordinates[station_id]],
key=lambda x: (len(str(x[0])), len(str(x[1])), x[0], x[1]),
Teddy-1000 marked this conversation as resolved.
Show resolved Hide resolved
)[-1]
}
else:
raise HTTPException(
status_code=500,
detail={
"coordinates": f"Station with id `{station_id} "
f"has multiple coordinates: {platform_coordinates[station_id]}"
Teddy-1000 marked this conversation as resolved.
Show resolved Hide resolved
},
)
if len(platform_names[station_id]) > 1:
raise HTTPException(
status_code=500,
Expand Down
13 changes: 12 additions & 1 deletion api/utilities.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from datetime import datetime
from datetime import timedelta
from typing import Tuple
from itertools import combinations

import datastore_pb2 as dstore
from fastapi import HTTPException
Expand All @@ -10,7 +11,9 @@
from pydantic import TypeAdapter


def get_datetime_range(datetime_string: str | None) -> Tuple[Timestamp, Timestamp] | None:
def get_datetime_range(
datetime_string: str | None,
) -> Tuple[Timestamp, Timestamp] | None:
Teddy-1000 marked this conversation as resolved.
Show resolved Hide resolved
if not datetime_string:
return None

Expand Down Expand Up @@ -121,3 +124,11 @@ def validate_bbox(bbox: str) -> Tuple[float, float, float, float]:
raise HTTPException(status_code=400, detail=errors)

return left, bottom, right, top


def calculate_largest_postition_deviation(positions: list) -> float:
largest_deviation = 0
for i, j in combinations(positions, 2):
if (dist := ((i[0] - j[0]) ** 2) + ((i[1] - j[1]) ** 2) ** 0.5) > largest_deviation:
largest_deviation = dist
return largest_deviation
5 changes: 5 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@
ignore = E221,E226,E228,E241,W503
max-line-length = 120
exclude = ingest/src/esoh/*pb2*.py
[isort]
profile = black
[black]
line-length = 120
Teddy-1000 marked this conversation as resolved.
Show resolved Hide resolved
exclude = *pb2*.py