-
Notifications
You must be signed in to change notification settings - Fork 0
/
getEARainfall_28dayAPI.py
256 lines (206 loc) · 9.12 KB
/
getEARainfall_28dayAPI.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
#! python3
# Steve Buss, 24 April 2017
# details here https://environment.data.gov.uk/flood-monitoring/doc/rainfall
import urllib
import urllib.request
import json
import csv
import datetime
import requests
import numpy as np
from datetime import date
from datetime import time
from datetime import datetime
from datetime import timedelta
from dateutil.relativedelta import relativedelta
import pandas as pd
import pprint
import sys
##import iso8601
from dateutil.parser import parse
from io import BytesIO
maxDailyDataPoints = 96
def getData(url):
response=BytesIO()
f = urllib.request.urlopen(url)
response = f.read()
strResponse = response.decode('utf-8') # json.loads object must be a string, not bytes
dicResponse = json.loads(strResponse)
return dicResponse
def getStationRef(long,lat):
url=('http://environment.data.gov.uk/flood-monitoring/id/stations?parameter=rainfall&long='
+long+'&lat='+lat+'&dist=10')
stationRef=getData(url)['items'][0]['stationReference']
return stationRef
def writeLocations(dic):
# dic is actually a list of dictionaries
with open(monitoringPointsFilename, 'w') as f:
# lineterminator='\n' is required to prevent empty lines between data
w = csv.writer(f, delimiter=',', lineterminator='\n')
w.writerow(["StationRef", "x", "y"])
for item in dic['items']:
# each str is wrapped in [] so that writerow doesn't
# put commas between all the characters
w.writerow(
[str(item['stationReference'])] +
[str(item['easting'])] +
[str(item['northing'])]
)
return
def writeTimeSeries(lst, filename):
# see comments on formatting in writePoints
timeSeries = []
with open(filename, 'w') as f:
w = csv.writer(f, delimiter=',', lineterminator='\n')
w.writerow(["Date", "Time", "Value"])
for item in lst['items']:
e = {}
dt = parse(item['dateTime'])
e['date'] = dt.date()
e['time'] = dt.time()
e['value'] = item['value']
timeSeries.append(e)
w.writerow(
[str(e['date'])] +
[str(e['time'])] +
[str(e['value'])]
)
return timeSeries
def writeAggregateTimeSeries(lst, filename):
# see comments on formatting in writePoints
with open(filename, 'w') as f:
# TO DO write some headers for this file
w = csv.writer(f, delimiter=',', lineterminator='\n')
w.writerow(["Date", "Value", "PercentComplete"])
for item in lst:
w.writerow(
[str(item['date'])] +
[str(item['value'])] +
[str(item['percent'])]
)
return
def aggregateTimeSeries(listData, filename):
# pandas would do this more efficiently, I'm sure
# the dates need to be sorted
daysCount = 1 # number of days in the dataset
dataCount = 0 # number of dataPoints in each day
daySum = 0 # daily total
a = []
d = listData[0]['date']
for dt in listData:
if d == dt['date']:
dataCount += 1
daySum += dt['value']
lastDate = d
else:
a.append({'date': lastDate, 'value': daySum,
'percent': 100*dataCount/maxDailyDataPoints})
daysCount =+ 1
dataCount = 1
daySum = dt['value']
d = dt['date']
lastDate = d
a.append({'date': lastDate, 'value': daySum,
'percent': 100*dataCount/maxDailyDataPoints}) # write the last lot of data not part of a complete day
writeAggregateTimeSeries(a, filename)
return
def compileData(pointID):
rawFilename = 'RainDataRaw_' + pointID + '.csv'
aggFilename = 'RainDataAggreagate_' + pointID + '.csv'
url = ('http://environment.data.gov.uk/flood-monitoring/id/stations/'
+ pointID + '/readings?_sorted&_limit=10000')
d = writeTimeSeries(getData(url), rawFilename)
da = aggregateTimeSeries(d, aggFilename)
### not yet implemented
# return the value for a given date in the time series
# return the last n days rainfall
# change aggregation from 00:00 to 23:59 to 09:00 to 08:59 (or whatever)
return da
def compileDatafromlist(folder, rundate):
# function interrogates CSV reference file which has list of boreholes and the nearest three qauges
# tries to get rainfall data for each gauge in turn
# writes aggregate to CSV for last 28 days
datestr = rundate.strftime("%d-%m-%Y")
data = pd.read_csv(reference_filename, converters={'EA Rain Gauge': lambda x: str(x)})
x = len(data.index)
a = 0
print ("An attempt will be made to obtain gauge data for "
+ str(int(x/3)) + " boreholes")
while a < x:
# try nearest gauge to borehole
try:
gaugeid = data.loc[a,"EA Rain Gauge"]
boreholename = data.loc[a,"Model Borehole ID"]
rank = data.loc[a,"Rank"]
rawFilename = (folder + 'RainDataRaw_' + boreholename
+ '_' + gaugeid + '_' + datestr + '.csv')
aggFilename = (folder + 'RainDataAggregate_' + boreholename
+ '_' + gaugeid + '_' + datestr + '.csv')
url = ('http://environment.data.gov.uk/flood-monitoring/id/stations/'
+ gaugeid + '/readings?_sorted&_limit=10000')
d = writeTimeSeries(getData(url), rawFilename)
da = aggregateTimeSeries(d, aggFilename)
print('Rainfall data for ' +
str(boreholename) +
' is from ' + str(gaugeid) +
', Rank ' + str(rank))
a += 3
# if error, try next nearest gauge
except (IndexError,KeyError):
try:
a += 1
gaugeid = data.loc[a,"EA Rain Gauge"]
boreholename = data.loc[a,"Model Borehole ID"]
rank = data.loc[a,"Rank"]
rawFilename = (folder + 'RainDataRaw_' + boreholename
+ '_' + gaugeid + '_' + datestr + '.csv')
aggFilename = (folder + 'RainDataAggregate_' + boreholename
+ '_' + gaugeid + '_' + datestr + '.csv')
url = ('http://environment.data.gov.uk/flood-monitoring/id/stations/'
+ gaugeid + '/readings?_sorted&_limit=10000')
d = writeTimeSeries(getData(url), rawFilename)
da = aggregateTimeSeries(d, aggFilename)
print('Rainfall data for ' +
str(boreholename) + ' is from ' +
str(gaugeid) +
', Rank ' + str(rank))
a += 2
# if error, try next nearest gauge
except (IndexError,KeyError):
try:
a += 1
gaugeid = data.loc[a,"EA Rain Gauge"]
boreholename = data.loc[a,"Model Borehole ID"]
rank = data.loc[a,"Rank"]
rawFilename = (folder + 'RainDataRaw_' + boreholename
+ '_' + gaugeid + '_' + datestr + '.csv')
aggFilename = (folder + 'RainDataAggregate_' + boreholename
+ '_' + gaugeid + '_' + datestr + '.csv')
url = ('http://environment.data.gov.uk/flood-monitoring/id/stations/'
+ gaugeid + '/readings?_sorted&_limit=10000')
d = writeTimeSeries(getData(url), rawFilename)
da = aggregateTimeSeries(d, aggFilename)
print('Rainfall data for ' +
str(boreholename) +
' is from ' + str(gaugeid) +
', Rank ' + str(rank))
a += 1
# if error for all three gauges, quit
except (IndexError,KeyError):
print('All three gauges provide no data for ' +
str(boreholename))
a += 1
return
def main():
global reference_filename
reference_filename = "NearestThreeGauges_v1_ForecastBoreholes.csv"
print("Connecting...")
print("Using " + str(reference_filename) + " file as reference")
folder = ('F:\\GWFForecast\\044 GWF forecasting\\' +
'EA rainfall gauge API Outputs\\EAgauges_28day_historical\\')
rundate = date.today()
compileDatafromlist(folder, rundate)
print("...Done.")
#main()
if __name__ == "__main__":
sys.exit(main())