-
Notifications
You must be signed in to change notification settings - Fork 8
/
noaa_api_v2.py
72 lines (60 loc) · 2.58 KB
/
noaa_api_v2.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
# NOAA API V2
# Documentation can be found at
# http://www.ncdc.noaa.gov/cdo-web/webservices/v2
import requests
class NOAAData(object):
def __init__(self, token):
# NOAA API Endpoint
self.url = 'https://www.ncdc.noaa.gov/cdo-web/api/v2/'
self.h = dict(token=token)
def poll_api(self, req_type, payload):
# Initiate http request - kwargs are constructed into a dict and passed as optional parameters
# Ex (limit=100, sortorder='desc', startdate='1970-10-03', etc)
r = requests.get(self.url + req_type, headers=self.h, params=payload)
if r.status_code != 200: # Handle erroneous requests
print("Error: " + str(r.status_code))
else:
r = r.json()
try:
return r['results'] # Most JSON results are nested under 'results' key
except KeyError:
return r # for non-nested results, return the entire JSON string
# Fetch available datasets
# http://www.ncdc.noaa.gov/cdo-web/webservices/v2#datasets
def datasets(self, **kwargs):
req_type = 'datasets'
return self.poll_api(req_type, kwargs)
# Fetch data categories
# http://www.ncdc.noaa.gov/cdo-web/webservices/v2#dataCategories
def data_categories(self, **kwargs):
req_type = 'datacategories'
return self.poll_api(req_type, kwargs)
# Fetch data types
# http://www.ncdc.noaa.gov/cdo-web/webservices/v2#dataTypes
def data_types(self, **kwargs):
req_type = 'datatypes'
return self.poll_api(req_type, kwargs)
# Fetch available location categories
# http://www.ncdc.noaa.gov/cdo-web/webservices/v2#locationCategories
def location_categories(self, **kwargs):
req_type = 'locationcategories'
return self.poll_api(req_type, kwargs)
# Fetch all available locations
# http://www.ncdc.noaa.gov/cdo-web/webservices/v2#locations
def locations(self, **kwargs):
req_type = 'locations'
return self.poll_api(req_type, kwargs)
# Fetch All available stations
# http://www.ncdc.noaa.gov/cdo-web/webservices/v2#stations
def stations(self, h, p, **kwargs):
req_type = 'stations'
return self.poll_api(req_type, kwargs)
# Fetch information about specific dataset
def dataset_spec(self, set_code, **kwargs):
req_type = 'datacategories/' + set_code
return self.poll_api(req_type, kwargs)
# Fetch data
# http://www.ncdc.noaa.gov/cdo-web/webservices/v2#data
def fetch_data(self, **kwargs):
req_type = 'data'
return self.poll_api(req_type, kwargs)