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

Mere support for climateData API #3

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
17 changes: 13 additions & 4 deletions dmi_open_data/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@
class DMIOpenDataClient:
_base_url = "https://dmigw.govcloud.dk/{version}/{api}"

def __init__(self, api_key: str, version: str = "v2"):
def __init__(self, api_key: str, api_name:str = "metObs", version: str = "v2"):
if api_key is None:
raise ValueError(f"Invalid value for `api_key`: {api_key}")
if api_name not in ("climateData", "metObs"):
raise NotImplementedError(f"Following api is not supported yet: {api_name}")
if version == "v1":
raise ValueError(f"DMI metObs v1 not longer supported")
if version not in ["v2"]:
raise ValueError(f"API version {version} not supported")

self.api_key = api_key
self.api_name = api_name
self.version = version

def base_url(self, api: str):
Expand Down Expand Up @@ -47,7 +50,7 @@ def _query(self, api: str, service: str, params: Dict[str, Any], **kwargs):
return res.json()

def get_stations(
self, limit: Optional[int] = 10000, offset: Optional[int] = 0
self, limit: Optional[int] = 10000, offset: Optional[int] = 0
perabrahamsen marked this conversation as resolved.
Show resolved Hide resolved
) -> List[Dict[str, Any]]:
"""Get DMI stations.

Expand All @@ -61,7 +64,7 @@ def get_stations(
List[Dict[str, Any]]: List of DMI stations.
"""
res = self._query(
api="metObs",
api=self.api_name,
service="collections/station/items",
params={
"limit": limit,
Expand Down Expand Up @@ -190,7 +193,8 @@ def get_parameter(parameter_id: str) -> Parameter:
return Parameter(parameter_id)

def get_closest_station(
self, latitude: float, longitude: float
self, latitude: float, longitude: float,
pars = []
perabrahamsen marked this conversation as resolved.
Show resolved Hide resolved
) -> List[Dict[str, Any]]:
"""Get closest weather station from given coordinates.

Expand All @@ -203,6 +207,7 @@ def get_closest_station(
"""
stations = self.get_stations()
closest_station, closests_dist = None, 1e10
want_pars = set (pars)
perabrahamsen marked this conversation as resolved.
Show resolved Hide resolved
for station in stations:
coordinates = station.get("geometry", {}).get("coordinates")
if coordinates is None or len(coordinates) < 2:
Expand All @@ -211,6 +216,10 @@ def get_closest_station(
if lat is None or lon is None:
continue

has_pars = set (station['properties']['parameterId'])
perabrahamsen marked this conversation as resolved.
Show resolved Hide resolved
if (not want_pars.issubset (has_pars)):
perabrahamsen marked this conversation as resolved.
Show resolved Hide resolved
continue

# Calculate distance
dist = distance(
lat1=latitude,
Expand Down
1 change: 1 addition & 0 deletions dmi_open_data/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class ClimateDataParameter(Enum):
MaxPressure = "max_pressure"
MinPressure = "min_pressure"
BrightSunshine = "bright_sunshine"
MeanRadiation = "mean_radiation"
AccPrecip = "acc_precip"
MaxPrecip_24h = "max_precip_24h"
AccPrecipPast12h = "acc_precip_past12h"
Expand Down