Skip to content

Commit

Permalink
Makes functions more robust to errors
Browse files Browse the repository at this point in the history
  • Loading branch information
FL550 committed Aug 6, 2020
1 parent 134fab4 commit 5df426f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 30 deletions.
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.10",
version="0.9.11",
author="Max Fermor",
description="A simple tool to retrieve weather forecast from DWD OpenData",
long_description=long_description,
Expand Down
91 changes: 62 additions & 29 deletions simple_dwd_weatherforecast/dwdforecast.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,14 @@ def get_daily_temp_max(self, timestamp: datetime, shouldUpdate = True):
if list(self.forecast_data.keys())[0] < self.strip_to_hour_str(
timestamp) < list(self.forecast_data.keys())[-1]:
weather_data = self.get_day_values(timestamp)
temp = -9999999
temp = None
for item in weather_data:
temp_new = item["temp"]
if temp_new > temp:
temp = temp_new
if temp_new:
if not temp:
temp = -9999999
if temp_new > temp:
temp = temp_new
return str(temp)
return None

Expand All @@ -204,11 +207,14 @@ def get_daily_temp_min(self, timestamp: datetime, shouldUpdate = True):
if list(self.forecast_data.keys())[0] < self.strip_to_hour_str(
timestamp) < list(self.forecast_data.keys())[-1]:
weather_data = self.get_day_values(timestamp)
temp = 9999999
temp = None
for item in weather_data:
temp_new = item["temp"]
if temp_new < temp:
temp = temp_new
if temp_new:
if not temp:
temp = 9999999
if temp_new < temp:
temp = temp_new
return str(temp)
return None

Expand All @@ -218,9 +224,13 @@ def get_daily_precipitation(self, timestamp: datetime, shouldUpdate = True):
if list(self.forecast_data.keys())[0] < self.strip_to_hour_str(
timestamp) < list(self.forecast_data.keys())[-1]:
weather_data = self.get_day_values(timestamp)
precipitation = 0.0
precipitation = None
for item in weather_data:
precipitation += float(item["prec_sum"])
value = item["prec_sum"]
if (value):
if not precipitation:
precipitation = 0.0
precipitation += float(value)
return str(precipitation)
return None

Expand All @@ -230,11 +240,15 @@ def get_daily_precipitation_probability(self, timestamp: datetime, shouldUpdate
if list(self.forecast_data.keys())[0] < self.strip_to_hour_str(
timestamp) < list(self.forecast_data.keys())[-1]:
weather_data = self.get_day_values(timestamp)
prec_prop = 0.0
prec_prop = None
for item in weather_data:
prec_prop_new = float(item["prec_prop"])
if prec_prop_new > prec_prop:
prec_prop = prec_prop_new
value = item["prec_prop"]
if value:
if not prec_prop:
prec_prop = 0.0
prec_prop_new = float(value)
if prec_prop_new > prec_prop:
prec_prop = prec_prop_new
return str(int(prec_prop))
return None

Expand Down Expand Up @@ -297,64 +311,83 @@ def parse_kml(self, kml):
namespaces=namespaces)[0].text
temperatures = []
for elem in result.split():
temperatures.append(round(float(elem) - 273.15, 2))
if (elem!="-"):
temperatures.append(round(float(elem) - 273.15, 2))
else:
temperatures.append(None)

result = tree.xpath(
'//kml:ExtendedData/dwd:Forecast[@dwd:elementName="PPPP"]/dwd:value',
namespaces=namespaces)[0].text
pressure = []
for elem in result.split():
pressure.append(float(elem) / 100)

if (elem!="-"):
pressure.append(float(elem) / 100)
else:
pressure.append(None)
result = tree.xpath(
'//kml:ExtendedData/dwd:Forecast[@dwd:elementName="DD"]/dwd:value',
namespaces=namespaces)[0].text
wind_dir = []
for elem in result.split():
wind_dir.append(elem)

if (elem!="-"):
wind_dir.append(elem)
else:
wind_dir.append(None)
result = tree.xpath(
'//kml:ExtendedData/dwd:Forecast[@dwd:elementName="FF"]/dwd:value',
namespaces=namespaces)[0].text
wind_speed = []
for elem in result.split():
wind_speed.append(elem)

if (elem!="-"):
wind_speed.append(elem)
else:
wind_speed.append(None)
result = tree.xpath(
'//kml:ExtendedData/dwd:Forecast[@dwd:elementName="RR1c"]/dwd:value',
namespaces=namespaces)[0].text
prec_sum = []
for elem in result.split():
prec_sum.append(elem)

if (elem!="-"):
prec_sum.append(elem)
else:
prec_sum.append(None)
result = tree.xpath(
'//kml:ExtendedData/dwd:Forecast[@dwd:elementName="wwP"]/dwd:value',
namespaces=namespaces)[0].text
prec_prop = []
for elem in result.split():
prec_prop.append(elem)

if (elem!="-"):
prec_prop.append(elem)
else:
prec_prop.append(None)
result = tree.xpath(
'//kml:ExtendedData/dwd:Forecast[@dwd:elementName="N"]/dwd:value',
namespaces=namespaces)[0].text
cloud_cov = []
for elem in result.split():
cloud_cov.append(elem)

if (elem!="-"):
cloud_cov.append(elem)
else:
cloud_cov.append(None)
result = tree.xpath(
'//kml:ExtendedData/dwd:Forecast[@dwd:elementName="VV"]/dwd:value',
namespaces=namespaces)[0].text
visibility = []
for elem in result.split():
visibility.append(elem)

if (elem!="-"):
visibility.append(elem)
else:
visibility.append(None)
result = tree.xpath(
'//kml:ExtendedData/dwd:Forecast[@dwd:elementName="SunD1"]/dwd:value',
namespaces=namespaces)[0].text
sun_dur = []
for elem in result.split():
sun_dur.append(round(float(elem) / 60))

if (elem!="-"):
sun_dur.append(round(float(elem) / 60))
else:
sun_dur.append(None)
merged_list = {}
for i in range(len(timesteps)):
item = {
Expand Down

0 comments on commit 5df426f

Please sign in to comment.