-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistoricaldata.py
More file actions
124 lines (111 loc) · 3.95 KB
/
historicaldata.py
File metadata and controls
124 lines (111 loc) · 3.95 KB
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
import requests
import json
from historygraph import HistoryGraph
class HistoricalData:
def __init__(self):
self.token = "xwPDPxAMOcpejWpbvJbGJddMEQUBPHlO"
self.url = 'https://www.ncdc.noaa.gov/cdo-web/api/v2/'
self.header = dict(token=self.token)
self.dataType = ''
self.location = ''
self.locationType = ''
self.units = 'standard'
self.startDate = ''
self.endDate = ''
self.limit = '1000'
self.locCats = []
self.dataSets = []
self.dataCats = []
self.dataList = []
self.isValid = True
def setLimit(self, limit):
if int(limit) < 1000 or limit > 0:
self.limit = limit
else:
limit = '1000'
def setLocationType(self, locType):
self.locationType = locType
def setLocation(self, loc):
self.location = loc
def setStartDate(self, year, month, day):
if year and month and day != '':
if int(year) < 1970:
y = '1970'
else:
y = year
if int(month) < 1 or int(month) > 12:
m = '1'
else:
m = month
if int(day) < 0 or int(day) > 31:
d = '1'
else:
d = day
self.startDate = y + '-' + m + '-' + d
def setEndDate(self, year, month, day):
if year and month and day != '':
if int(year) < 1970:
y = '1970'
else:
y = year
if int(month) < 1 or int(month) > 12:
m = '1'
else:
m = month
if int(day) < 0 or int(day) > 31:
d = '1'
else:
d = day
self.endDate = y + '-' + m + '-' + d
def getDataSets(self):
dSetUrl = self.url+'datasets?limit='+self.limit+'&locationid='+self.locationType+':'+self.location
r = requests.get(dSetUrl, headers=self.header)
if r.status_code == 200:
d = r.json()
if 'results' in d:
self.isValid = True
for item in d['results']:
self.dataSets.append(item['name'])
else:
self.isValid = False
else:
self.isValid = False
def getDataCategories(self):
dSetUrl = self.url+'datacategories?limit='+self.limit+'&locationid='+self.locationType+':'+self.location
r = requests.get(dSetUrl, headers=self.header)
if r.status_code == 200:
d = r.json()
if 'results' in d:
self.isValid = True
for item in d['results']:
self.dataCats.append([item['name'], item['id']])
else:
self.isValid = False
else:
self.isValid = False
def getData(self):
dSetUrl = self.url+'data?datasetid=GHCND&datatypeid='+self.dataType+'&limit='+self.limit+'&locationid='+self.locationType+':'+self.location+'&startdate='+self.startDate+'&enddate='+self.endDate+'&units='+self.units
r = requests.get(dSetUrl, headers=self.header)
if r.status_code == 200:
d = r.json()
if 'results' in d:
self.isValid = True
for item in d['results']:
self.dataList.append([item['value'], item['date']])
else:
self.isValid = False
else:
self.isValid = False
def getLocationCategories(self):
fullUrl = self.url+'locationcategories?limit='+self.limit
r = requests.get(fullUrl, headers=self.header)
if r.status_code == 200:
d = r.json()
if 'results' in d:
self.isValid = True
for item in d['results']:
self.locCats.append([item['name'], item['id']])
else:
self.isValid = False
else:
self.isValid = False