Skip to content

Commit

Permalink
Add function for valid station_id
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Aug 7, 2020
1 parent b268270 commit a57b233
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ temperature_tomorrow = dwd_weather.get_forecast_temperature(time_tomorrow)
All methods return their values as string. The datetime value has to be in UTC. If no data is available for this datetime, None will be returned. With the optional boolean `shouldUpdate` an automated check for new updates can be prevented by setting this parameter to `False`. Otherwise data is updated if new data is available with every function call.

```python
dwdforecast.is_valid_station_id(station_id) #Checks if given station_id is valid

dwdforecast.get_nearest_station_id(latitude, longitude) #Returns nearest Station-ID for the coordinates. latitude and longitude expect float values.

get_station_name(optional bool shouldUpdate) #Return Station name
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="simple_dwd_weatherforecast",
version="0.9.12",
version="0.9.13",
author="Max Fermor",
description="A simple tool to retrieve weather forecast from DWD OpenData",
long_description=long_description,
Expand Down
19 changes: 12 additions & 7 deletions simple_dwd_weatherforecast/dwdforecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@

from .stations import stations

def is_valid_station_id(station_id: str):
for line in stations.splitlines():
if (len(line) > 0 and line[0].isdigit()):
if line[12:18].strip() == station_id:
return True
return False

def get_nearest_station_id(lat: float, lon: float):
result = ""
distance = 99999999

for line in stations.splitlines():
if (line.startswith('1') or line.startswith('2') or line.startswith('3') \
or line.startswith('4') or line.startswith('5') or line.startswith('6') \
or line.startswith('7') or line.startswith('8') or line.startswith('9') \
or line.startswith('0')):
if (len(line) > 0 and line[0].isdigit()):
_lat = float(line[45:51].strip())
_lon = float(line[52:59].strip())
distance_temp = get_distance(lat, lon, _lat, _lon)
Expand Down Expand Up @@ -72,8 +74,11 @@ class Weather:
"95": ("lightning-rainy", 1),
}

def __init__(self, stationid):
self.station_id = stationid
def __init__(self, station_id):
if is_valid_station_id(station_id):
self.station_id = station_id
else:
raise ValueError("Not a valid station_id")

def get_station_name(self, shouldUpdate=True):
if self.station_name == '' and shouldUpdate:
Expand Down

0 comments on commit a57b233

Please sign in to comment.