forked from gunja/influx2-weather
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironmentcanada.py
139 lines (102 loc) · 5.22 KB
/
environmentcanada.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import configparser, json, sys, untangle, urllib.request
#import data_gc_ca_api
#from weathergc import Forecast
config = configparser.ConfigParser()
config.read('ec-config.ini')
siteCode = config['ENVIRONMENTCANADA']['SiteCode']
provinceCode = config['ENVIRONMENTCANADA']['Province']
def getWeatherData(siteID, province):
#check that the location that has been submitted is a valid location
try:
ECLocationList = untangle.parse('http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/siteList.xml')
except urllib.error.HTTPError as e:
print("Unable to retrieve Environment Canada Weather Station List: {}".format(e))
except:
e = sys.exc_info()[0]
print("Unable to retrieve Environment Canada Weather Station List: {}".format(e))
return None
#print (ECLocationList.siteList)
#loop through each of the sites in the list of available sites to confirm that the requested site is available
for item in ECLocationList.siteList.site:
#print(item['code'])
#print(item.provinceCode.cdata)
#if this site in the list is the same as the site that is asking for
if siteID == item['code'] and province == item.provinceCode.cdata:
break
#the site was not one the list
else:
print('Unable to find station `{}` in province `{}` on list of available stations'.format(siteID, province))
return None
#lookup the weather for the requested location
try:
locationWeather = untangle.parse('http://dd.weatheroffice.ec.gc.ca/citypage_weather/xml/' + province + '/' + siteID + '_e.xml')
except urllib.error.HTTPError as e:
print("Unable to retrieve Environment Canada Weather for station {}: {}".format(siteID, e))
except:
e = sys.exc_info()[0]
print("Unable to retrieve Environment Canada Weather for station {}: {}".format(siteID, e))
return None
#print(locationWeather.siteData.currentConditions)
try:
formattedData = formatData(locationWeather.siteData.currentConditions)
return formattedData
except ValueError as e:
print("Unable to format Environment Canada Weather data: {}".format(e))
return None
except KeyError as e:
print("Unable to retrieve Weather Underground Weather info: {}".format(e))
return None
except TypeError as e:
print("Unable to format Environment Canada Weather data: {}".format(e))
return None
except AttributeError as e:
print("Unable to format Environment Canada Weather data: {}".format(e))
return None
except:
e = sys.exc_info()[0]
print("Unable to format Environment Canada Weather data: {}".format(e))
return None
def formatData(data):
#print(data.station['code'])
#print(type(data.pressure.cdata))
#TODO: check that values are not empty (they seem to all start as stings) before converting to floats (really doesnt like that)
json_data = [
{
"measurement": "environmentcanada",
"tags": {
"stationCode": str(data.station['code'])
},
"fields":
{
# 'pressure': ,
# 'pressure_tendency': str(data.pressure['tendency']),
# 'pressure_change': float(data.pressure['change']),
# #'visibility': float(data.visibility.cdata),
# 'relativeHumidity':float(data.relativeHumidity.cdata),
# 'wind_speed': float(data.wind.speed.cdata),
# 'wind_direction': str(data.wind.direction.cdata),
# 'wind_bearing': float(data.wind.bearing.cdata)
}
}
]
#print(str(data.condition.cdata))
if data.dateTime[1].textSummary.cdata: json_data[0]['fields']['observation'] = str(data.dateTime[1].textSummary.cdata)
if data.condition.cdata: json_data[0]['fields']['condition'] = str(data.condition.cdata)
if data.temperature.cdata: json_data[0]['fields']['temperature'] = float(data.temperature.cdata)
if data.dewpoint.cdata: json_data[0]['fields']['dewpoint'] = float(data.dewpoint.cdata)
if data.pressure.cdata: json_data[0]['fields']['pressure'] = float(data.pressure.cdata)
if data.pressure['tendency']: json_data[0]['fields']['pressure_tendency'] = str(data.pressure['tendency'])
if data.pressure['change']: json_data[0]['fields']['pressure_change'] = float(data.pressure['change'])
if data.relativeHumidity.cdata: json_data[0]['fields']['relativeHumidity'] = float(data.relativeHumidity.cdata)
if data.dateTime[1].textSummary.cdata: json_data[0]['fields']['wind_speed'] = float(data.wind.speed.cdata)
if data.dateTime[1].textSummary.cdata: json_data[0]['fields']['wind_direction'] = str(data.wind.direction.cdata)
if data.dateTime[1].textSummary.cdata: json_data[0]['fields']['wind_bearing'] = float(data.wind.bearing.cdata)
#print(type(json_data))
print(json_data)
return json_data
def main():
weatherdata = getWeatherData(siteCode, provinceCode)
#print(weatherdata)
return weatherdata
if __name__ == '__main__':
main()