-
Notifications
You must be signed in to change notification settings - Fork 583
/
Copy pathopenaq.py
538 lines (463 loc) · 19 KB
/
openaq.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
"""Simple openaq to only depend on json, math, and requests (no dfs/plots)."""
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import requests
import math
class ApiError(Exception):
pass
class API(object):
"""Generic API wrapper object.
"""
def __init__(self, **kwargs):
self._key = kwargs.pop('key', '')
self._pswd = kwargs.pop('pswd', '')
self._version = kwargs.pop('version', None)
self._baseurl = kwargs.pop('baseurl', None)
self._headers = {'content-type': 'application/json'}
def _make_url(self, endpoint, **kwargs):
"""Internal method to create a url from an endpoint.
:param endpoint: Endpoint for an API call
:type endpoint: string
:returns: url
"""
endpoint = "{}/{}/{}".format(self._baseurl, self._version, endpoint)
extra = []
for key, value in kwargs.items():
if isinstance(value, list) or isinstance(value, tuple):
#value = ','.join(value)
for v in value:
extra.append("{}={}".format(key, v))
else:
extra.append("{}={}".format(key, value))
if len(extra) > 0:
endpoint = '?'.join([endpoint, '&'.join(extra)])
return endpoint
def _send(self, endpoint, method='GET', **kwargs):
"""Make an API call of any method
:param endpoint: API endpoint
:param method: API call type. Options are PUT, POST, GET, DELETE
:type endpoint: string
:type method: string
:returns: (status_code, json_response)
:raises ApiError: raises an exception
"""
auth = (self._key, self._pswd)
url = self._make_url(endpoint, **kwargs)
if method == 'GET':
resp = requests.get(url, auth=auth, headers=self._headers)
else:
raise ApiError("Invalid Method")
if resp.status_code != 200:
raise ApiError("A bad request was made: {}".format(resp.status_code))
res = resp.json()
# Add a 'pages' attribute to the meta data
try:
res['meta']['pages'] = math.ceil(res['meta']['found'] / res['meta']['limit'])
except:
pass
return resp.status_code, res
def _get(self, url, **kwargs):
return self._send(url, 'GET', **kwargs)
class OpenAQ(API):
"""Create an instance of the OpenAQ API
"""
def __init__(self, version='v1', **kwargs):
"""Initialize the OpenAQ instance.
:param version: API version.
:param kwargs: API options.
:type version: string
:type kwargs: dictionary
"""
self._baseurl = 'https://api.openaq.org'
super(OpenAQ, self).__init__(version=version, baseurl=self._baseurl)
def cities(self, **kwargs):
"""Returns a listing of cities within the platform.
:param country: limit results by a certain country
:param limit: limit results in the query. Default is 100. Max is 10000.
:param page: paginate through the results. Default is 1.
:param order_by: order by one or more fields (ex. order_by=['country', 'locations']). Default value is 'country'
:param sort: define the sort order for one or more fields (ex. sort='desc')
:return: dictionary containing the *city*, *country*, *count*, and number of *locations*
:type country: 2-digit ISO code
:type limit: number
:type order_by: string or list of strings
:type sort: string
:type page: number
:type country: string or array of strings
:type df: bool
:type index: string
:Example:
>>> import openaq
>>> api = openaq.OpenAQ()
>>> status, resp = api.cities()
>>> resp['results']
[
{
"city": "Amsterdam",
"country": "NL",
"count": 21301,
"locations": 14
},
{
"city": "Badhoevedorp",
"country": "NL",
"count": 2326,
"locations": 1
},
...
]
"""
return self._get('cities', **kwargs)
def countries(self, **kwargs):
"""Returns a listing of all countries within the platform
:param order_by: order by one or more fields (ex. order_by=['cities', 'locations']). Default value is 'country'
:param sort: define the sort order for one or more fields (ex. sort='desc')
:param limit: change the number of results returned. Max is 10000. Default is 100.
:param page: paginate through results. Default is 1.
:type order_by: string or list
:type sort: string
:type limit: int
:type page: int
:type df: bool
:type index: string
:return: dictionary containing the *code*, *name*, *count*, *cities*, and number of *locations*.
:Example:
>>> import openaq
>>> api = openaq.OpenAQ()
>>> status, resp = api.countries()
>>> resp['results']
[
{
"cities": 174,
"code": "AT",
"count": 121987,
"locations": 174,
"name": "Austria"
},
{
"cities": 28,
"code": "AU",
"count": 1066179,
"locations": 28,
"name": "Australia",
},
...
]
"""
return self._get('countries', **kwargs)
def latest(self, **kwargs):
"""Provides the latest value of each parameter for each location
:param city: limit results by a certain city. Defaults to ``None``.
:param country: limit results by a certain country. Should be a 2-digit
ISO country code. Defaults to ``None``.
:param location: limit results by a city. Defaults to ``None``.
:param parameter: limit results by a specific parameter. Options include [
pm25, pm10, so2, co, no2, o3, bc]
:param has_geo: filter items that do or do not have geographic information.
:param coordinates: center point (`lat`, `long`) used to get measurements within a
certain area. (Ex: coordinates=40.23,34.17)
:param radius: radius (in meters) used to get measurements. Must be used with coordinates.
Default value is 2500.
:param limit: change the number of results returned. Max is 10000. Default is 100.
:param page: paginate through the results.
:type city: string
:type country: string
:type location: string
:type parameter: string
:type has_geo: bool
:type coordinates: string
:type radius: int
:type limit: int
:type page: int
:type df: bool
:type index: string
:return: dictionary containing the *location*, *country*, *city*, and number of *measurements*
:Example:
>>> import openaq
>>> api = openaq.OpenAQ()
>>> status, resp = api.latest()
>>> resp['results']
[
{
"location": "Punjabi Bagh",
"city": "Delhi",
"country": "IN",
"measurements": [
{
"parameter": "so2",
"value": 7.8,
"unit": "ug/m3",
"lastUpdated": "2015-07-24T11:30:00.000Z"
},
{
"parameter": "co",
"value": 1.3,
"unit": "mg/m3",
"lastUpdated": "2015-07-24T11:30:00.000Z"
},
...
]
...
}
]
"""
return self._get('latest', **kwargs)
def locations(self, **kwargs):
"""Provides metadata about distinct measurement locations
:param city: Limit results by one or more cities. Defaults to ``None``. Can define as a single city
(ex. city='Delhi'), a list of cities (ex. city=['Delhi', 'Mumbai']), or as a tuple
(ex. city=('Delhi', 'Mumbai')).
:param country: Limit results by one or more countries. Should be a 2-digit
ISO country code as a string, a list, or a tuple. See `city` for details.
:param location: Limit results by one or more locations.
:param parameter: Limit results by one or more parameters. Options include [
pm25, pm10, so2, co, no2, o3, bc]
:param has_geo: Filter items that do or do not have geographic information.
:param coordinates: center point (`lat`, `long`) used to get measurements within a
certain area. (Ex: coordinates=40.23,34.17)
:param nearest: get the X nearest number of locations to `coordinates`. Must be used
with coordinates. Wins over `radius` if both are present. Will add the
`distance` property to locations.
:param radius: radius (in meters) used to get measurements. Must be used with coordinates.
Default value is 2500.
:param order_by: order by one or more fields (ex. order_by=['country', 'count']). Default value is 'location'
:param sort: define the sort order for one or more fields (ex. sort='desc')
:param limit: change the number of results returned. Max is 10000. Default is 100.
:param page: paginate through the results.
:type city: string, array, or tuple
:type country: string, array, or tuple
:type location: string, array, or tuple
:type parameter: string, array, or tuple
:type has_geo: bool
:type coordinates: string
:type nearest: int
:type radius: int
:type order_by: string or list
:type sort: string
:type limit: int
:type page: int
:type df: bool
:type index: string
:return: a dictionary containing the *location*, *country*, *city*, *count*, *distance*,
*sourceName*, *sourceNames*, *firstUpdated*, *lastUpdated*, *parameters*, and *coordinates*
:Example:
>>> import openaq
>>> api = openaq.OpenAQ()
>>> status, resp = api.locations()
>>> resp['results']
[
{
"count": 4242,
"sourceName": "Australia - New South Wales",
"firstUpdated": "2015-07-24T11:30:00.000Z",
"lastUpdated": "2015-07-24T11:30:00.000Z",
"parameters": [
"pm25",
"pm10",
"so2",
"co",
"no2",
"o3"
],
"country": "AU",
"city": "Central Coast",
"location": "wyong"
},
...
]
"""
return self._get('locations', **kwargs)
def measurements(self, **kwargs):
"""Provides data about individual measurements
:param city: Limit results by a certain city. Defaults to ``None``.
:param country: Limit results by a certain country. Should be a 2-digit
ISO country code. Defaults to ``None``.
:param location: Limit results by a city. Defaults to ``None``.
:param parameter: Limit results by one or more parameters. Options include [
pm25, pm10, so2, co, no2, o3, bc]
:param has_geo: Filter items that do or do not have geographic information.
:param coordinates: center point (`lat`, `long`) used to get measurements within a
certain area. (Ex: coordinates=40.23,34.17)
:param radius: radius (in meters) used to get measurements. Must be used with `coordinates`.
Default value is 2500.
:param value_from: Show results above a value threshold. Must be used with `parameter`.
:param value_to: Show results below a value threshold. Must be used with `parameter`.
:param date_from: Show results after a certain date. Format should be ``Y-M-D``.
:param date_to: Show results before a certain date. Format should be ``Y-M-D``.
:param sort: The sort order (``asc`` or ``desc``). Must be used with `order_by`.
:param order_by: Field to sort by. Must be used with **sort**.
:param include_fields: Include additional fields in the output. Allowed values are: *attribution*,
*averagingPeriod*, and *sourceName*.
:param limit: Change the number of results returned. Max is 10000 and default is 100.
:param page: Paginate through the results
:type city: string
:type country: string
:type location: string
:type parameter: string, array, or tuple
:type has_geo: bool
:type coordinates: string
:type radius: int
:type value_from: number
:type value_to: number
:type date_from: date
:type date_to: date
:type sort: string
:type order_by: string
:type include_fields: array
:type limit: number
:type page: number
:type df: bool
:type index: string
:return: a dictionary containing the *date*, *parameter*, *value*, *unit*,
*location*, *country*, *city*, *coordinates*, and *sourceName*.
:Example:
>>> import openaq
>>> api = openaq.OpenAQ()
>>> status, resp = api.measurements(city = 'Delhi')
>>> resp['results']
{
"parameter": "Ammonia",
"date": {
"utc": "2015-07-16T20:30:00.000Z",
'local': "2015-07-16T18:30:00.000-02:00"
},
"value": "72.9",
"unit": "ug/m3",
"location": "Anand Vihar",
"country": "IN",
"city": "Delhi",
"coordinates": {
"latitude": 43.34,
"longitude": 23.04
},
"attribution": {
"name": "SINCA",
"url": "http://sinca.mma.gob.cl/"
},
{
"name": "Ministerio del Medio Ambiente"
}
...
}
"""
return self._get('measurements', **kwargs)
def fetches(self, **kwargs):
"""Provides data about individual fetch operations that are used to populate
data in the platform.
:param order_by: order by one or more fields (ex. order_by=['timeEnded', 'count']). Default value is 'country'
:param sort: define the sort order for one or more fields (ex. sort='desc')
:param limit: change the number of results returned. Max is 10000. Default is 100.
:param page: paginate through the results. Default is 1.
:type order_by: string or list
:type sort: string
:type limit: int
:type page: int
:return: dictionary containing the *timeStarted*, *timeEnded*, *count*, and *results*
:Example:
>>> import openaq
>>> api = openaq.OpenAQ()
>>> status, resp = api.fetches()
>>> resp
{
"meta": {
"name": "openaq-api",
"license":
"website":
"page": 1,
"limit": 100,
"found": 3,
"pages": 1
},
"results": [
{
"count": 0,
"results": [
{
"message": "New measurements inserted for Mandir Marg: 1",
"failures": {},
"count": 0,
"duration": 0.153,
"sourceName": "Mandir Marg"
},
{
"message": "New measurements inserted for Sao Paulo: 1898",
"failures": {},
"count": 1898,
"duration": 16.918,
"sourceName": "Sao Paulo"
},
...
],
"timeStarted": "2016-02-07T15:25:04.603Z",
"timeEnded": "2016-02-07T15:25:04.793Z",
}
]
}
"""
return self._get('fetches', **kwargs)
def parameters(self, **kwargs):
"""
Provides a simple listing of parameters within the platform.
:param order_by: order by one or more fields (ex. order_by=['preferredUnit', 'id']). Default value is 'country'
:param sort: define the sort order for one or more fields (ex. sort='desc')
:type order_by: string or list
:type sort: string
:return: a dictionary containing the *id*, *name*, *description*, and
*preferredUnit*.
:Example:
>>> import openaq
>>> api = openaq.OpenAQ()
>>> status, resp = api.parameters()
>>> resp['results']
[
{
"id": "pm25",
"name": "PM2.5",
"description": "Particulate matter less than 2.5 micrometers in diameter",
"preferredUnit": "ug/m3"
}
...
]
"""
return self._get('parameters', **kwargs)
def sources(self, **kwargs):
"""
Provides a list of data sources.
:param order_by: order by one or more fields (ex. order_by=['active', 'country']). Default value is 'country'
:param sort: define the sort order for one or more fields (ex. sort='desc')
:param limit: Change the number of results returned.
:param page: Paginate through the results
:type limit: number
:type page: number
:type df: bool
:type index: string
:type order_by: string or list
:type sort: string
:return: a dictionary containing the *url*, *adapter*, *name*, *city*,
*country*, *description*, *resolution*, *sourceURL*, *contacts*, and *active*.
:Example:
>>> import openaq
>>> api = openaq.OpenAQ()
>>> status, resp = api.sources()
>>> resp['results']
[
{
"url": "http://airquality.environment.nsw.gov.au/aquisnetnswphp/getPage.php?reportid=2",
"adapter": "nsw",
"name": "Australia - New South Wales",
"city": "",
"country": "AU",
"description": "Measurements from the Office of Environment & Heritage of the New South Wales government.",
"resolution": "1 hr",
"sourceURL": "http://www.environment.nsw.gov.au/AQMS/hourlydata.htm",
"contacts": [
]
}
...
]
"""
return self._get('sources', **kwargs)
def __repr__(self):
return "OpenAQ API"