-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathECClimateData.py
304 lines (250 loc) · 9.84 KB
/
ECClimateData.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python
"""
Retrieve and process Environment Canada climate data from their web
site.
Exception Classes:
- `ECClimateDataError`
- `UnknownParameterError`
- `InvalidNoneParameterError`
- `IncorrectStationError`
- `UnknownParameterError`
Functions:
- `get_met_data()`: Return a list of string of meteorological data for
the specified station.
Tests and Examples
------------------
Create an instance and check the default property values:
>>> ecdata = ECClimateData()
>>> ecdata.site
'http://www.climate.weatheroffice.ec.gc.ca/climateData/bulkdata_e.html'
>>> assert(date(year=ecdata.year, month=ecdata.month, day=ecdata.day)
... == date.today() - timedelta(days=1))
>>> (ecdata.type, ecdata.format)
('hly', 'xml')
>>> print ecdata.stns['yvr']['name'], ecdata.stns['yvr']['stationID']
VANCOUVER INT'L A 889
Get 1 line of met data:
>>> ecdata.site = 'test.xml'
>>> print ecdata.get_met_data('yvr', year=2007, month=1, day=1)[0]
2007 1 1 0 5.5 80.00 10
Get 1 line of wind data:
>>> ecdata.site = 'wind.xml'
>>> print ecdata.get_wind_data('sandheads', 55, year=2006, month=4, day=1)[0]
Test exceptions:
>>> ecdata.get_met_data('yvr', spam=42)
Traceback (most recent call last):
...
UnknownParameterError: ('spam', 42)
>>> ecdata.get_met_data('yvr', year=None)
Traceback (most recent call last):
...
InvalidNoneParameterError
>>> ecdata.site = 'http://www.climate.weatheroffice.ec.gc'
>>> ecdata.get_met_data('yvr')
Traceback (most recent call last):
...
IOError: [Errno 2] No such file or directory: 'http://www.climate.weatheroffice.ec.gc'
"""
__author__ = 'Doug Latornell'
__version__ = '$Id$'
__docformat__ = 'restructuredtext'
from datetime import date, datetime, timedelta
import math
import sys
import urllib
import xml.dom.minidom
class ECClimateData:
"""Retrieve and process Environment Canada climate data from their
web site.
"""
def __init__(self):
"""
Initialize an `ECClimateData` object; set default property values.
"""
self.site = ('http://www.climate.weatheroffice.ec.gc.ca/'
+ 'climateData/bulkdata_e.html')
"""Root of URL to query for data."""
yesterday = datetime.now() - timedelta(days=1)
self.year = yesterday.year
"""Year to get data for."""
self.month = yesterday.month
"""Month to get data for."""
self.day = yesterday.day
"""Day to get data for."""
self.type = 'hly'
"""Type of data to get (frequnecy)."""
self.format = 'xml'
"""Format to retrieve data in."""
self.stns = dict(yvr=dict(name="VANCOUVER INT'L A",
stationID=889),
sandheads=dict(name="SANDHEADS CS",
stationID=6831))
"""Mapping of common station names to official names, and
station ID numbers."""
def get_met_data(self, stn, **kwargs):
"""Return a list of string of meteorological data for the
specified station.
Each list element is a string of the form:
'yyyy mm dd hh temperature humidity prelim_cloud_fraction'
"""
try:
dom = self._get_dom(stn, kwargs)
except:
raise
self._check_stn(stn, dom)
stndata = dom.getElementsByTagName('stationdata')
data = []
for node in stndata:
tempdata = node.getElementsByTagName('temp')[0]
humidity = node.getElementsByTagName('relhum')[0]
weather = node.getElementsByTagName('weather')[0]
weather = self._getText(weather.childNodes)
try:
data.append(' '.join((node.getAttribute('year'),
node.getAttribute('month'),
node.getAttribute('day'),
node.getAttribute('hour'),
self._getText(tempdata.childNodes),
self._getText(humidity.childNodes),
self._est_cloudfrac(weather))))
except:
raise
return data
def _est_cloudfrac(self, weather):
"""Map weather description to preliminary cloud fraction value.
Weather description is a text string. We return a
corresponding preliminary cloud fraction value in the rannge 0
to 10.
"""
cloudfrac_map = {'clear':0,
'cloudy':10,
'drizzle':10,
'fog':10,
'mainly clear':4,
'moderate rain':10,
'moderate snow':10,
'mostly cloudy':7,
'rain':10,
'rain,fog':10,
'rain showers':10,
'rain showers,snow showers':10,
'snow':10,
'snow grains':10,
'snow showers':10,
}
try:
return unicode(cloudfrac_map[weather.lower()])
except KeyError:
raise UnknownWeatherDescriptionError, weather
def get_wind_data(self, stn, angle, **kwargs):
"""Return a list of string of wind data for the specified
station.
Each list element is a string of the form:
'yyyy mm dd hh u_windspeed v_windspeed'
The wind speeds are in m/s. The +u direction is to the east,
and the +v direction is to the north (oceanographic
convention). The components are rotated through the specified
angle (clockwise).
"""
try:
dom = self._get_dom(stn, kwargs)
except:
raise
self._check_stn(stn, dom)
stndata = dom.getElementsByTagName('stationdata')
data = []
for node in stndata:
windspd = node.getElementsByTagName('windspd')[0]
windspd = float(self._getText(windspd.childNodes))
winddir = node.getElementsByTagName('winddir')[0]
winddir = float(self._getText(winddir.childNodes))
wind = self._calc_wind(windspd, winddir, angle)
try:
data.append(' '.join((node.getAttribute('year'),
node.getAttribute('month'),
node.getAttribute('day'),
node.getAttribute('hour'),
wind[0], wind[1])))
# unicode(windspd), unicode(winddir))))
except:
raise
return data
def _calc_wind(self, windspd, winddir, angle):
"""
"""
# Convert wind speed from km/hr to m/s, and direction from 10s
# of degrees to degrees
windspd = windspd * 1000 / 3600
winddir = winddir * 10
# Decompose the wind into components from the east and north
u = windspd * math.sin(winddir * (math.pi / 180))
v = windspd * math.cos(winddir * (math.pi / 180))
# Calculate rotated wind components
theta = -angle * math.pi / 180
u = u * math.cos(theta) - v * math.sin(theta)
v = u * math.sin(theta) + v * math.cos(theta)
# Change wind to "from" direction
u = -u
v = -v
return unicode(u), unicode(v)
def _get_dom(self, stn, kwargs):
"""Return an xml.dom.minidom object contining the specified
climate data.
For testing purposes a local file may be specified. It will
be opened iff the opening the url fails.
"""
# URL parameters and values
param_map = dict(stationID=self.stns[stn]['stationID'],
year=self.year, month=self.month, day=self.day,
type=self.type, format=self.format)
# Process args into parameter map
for k in kwargs:
if k in param_map:
if kwargs[k] is None:
if kwargs[k] == 'day':
del param_map[k]
else:
raise InvalidNoneParameterError, kwargs[k]
else:
param_map[k] = kwargs[k]
else:
raise UnknownParameterError, (k, kwargs[k])
params = urllib.urlencode(param_map)
# Open the URL (or test file), an dparse it into a minidom
# object
try:
f = urllib.urlopen("%s?%s" % (self.site, params))
except:
try:
f = open(self.site, 'r')
except:
raise
dom = xml.dom.minidom.parse(f)
f.close()
return dom
def _check_stn(self, stn, dom):
"""Confirm that the data returned is for the requested
station.
"""
stninfo = dom.getElementsByTagName('stationinformation')
stnname = stninfo[0].getElementsByTagName('name')[0].childNodes
if self._getText(stnname) != self.stns[stn]['name']:
raise IncorrectStationError, (stn, self._getText(stnname))
def _getText(self, nodelist):
"""Return the text from the specified nodes as a string."""
rc = ''
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc
class ECClimateDataError(Exception): pass
class UnknownParameterError(ECClimateDataError): pass
class InvalidNoneParameterError(ECClimateDataError): pass
class IncorrectStationError(ECClimateDataError): pass
class UnknownWeatherDescriptionError(ECClimateDataError): pass
def _test():
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()
# end of file