-
Notifications
You must be signed in to change notification settings - Fork 0
/
step4_api.py
155 lines (124 loc) · 5.91 KB
/
step4_api.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
'''
Function extracts rain data for a single day. Will return the average rainfall in quadrants whereby
the center is defined by the user. Will also return the average rainfall against time for each quadrant.
Inputs:
date: date of interest
string
easting_lim, northing_lim: easting and northing of the specified center
int or float
latlong:
Boolean
If True, the easting_lim and northing_lim are input as lattitude and longitude.
'''
import requests
import json
# import tool
from flood_tool import geo
from math import sqrt
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
def historic_API(date, easting_lim, northing_lim, latlong=False):
url = 'https://environment.data.gov.uk/flood-monitoring/archive/readings-full-' + date + '.csv'
data_csv = pd.read_csv(url)
df=pd.DataFrame(data=data_csv)
df = df.groupby('parameter')
df = df.get_group('rainfall')
df = df.reset_index()
if latlong == True:
easting_lim, northing_lim = geo.get_easting_northing_from_lat_long(easting_lim, northing_lim)
values = []
dates = []
stations = []
northing = []
easting = []
station_list = []
northeast = []
for row in range(0, len(df)):
historic_values = df.loc[row, 'value']
historic_date = df.loc[row, 'dateTime']
station = df.loc[row, 'stationReference']
historic_values = pd.to_numeric(historic_values, errors='coerce')
if isinstance(historic_values, float) == False:
continue
if station in station_list:
stations.append(station)
northing.append(northing[station_list.index(station)])
easting.append(easting[station_list.index(station)])
else:
station_list.append(station)
station_url = 'https://environment.data.gov.uk/flood-monitoring/id/stations?parameter=rainfall&stationReference=' + str(station)
coordinates = requests.get(station_url)
coordinates = json.loads(coordinates.text)
north = coordinates.get('items')[0].get('northing')
east = coordinates.get('items')[0].get('easting')
stations.append(station)
northing.append(north)
easting.append(east)
values.append(historic_values)
dates.append(historic_date)
historic_rain = pd.DataFrame({'dates':dates[:], 'station':stations[:], 'northing':northing[:], 'easting':easting[:], 'values':values[:]})
historic_rain['dates'] = historic_rain['dates'].map(lambda x: x.rstrip('Z'))
# for line in historic_rain['dates']:
# if ':01T' in historic_rain['dates']:
# historic_rain['dates'][row] = historic_rain['dates'][row].str.replace(':01T', ':00T')
historic_rain['date'], historic_rain['time'] = historic_rain['dates'].str.split('T', 1).str
# pattern = '*:01'
# for line in historic_rain['time']:
# if fnmatch(line, pattern):
# historic_rain.dates.loc[row] = historic_rain.dates.loc[row].replace('*$.:01', ':00', regex=True)
# # historic_rain['dates'][index=row] = historic_rain['dates'][index=row].replace(r'*$.:01', ':00')
historic_rain = historic_rain.drop('dates', axis=1).drop('date', axis=1).sort_values(by='time', ascending=True)
northeast = historic_rain.loc[(historic_rain.northing > northing_lim) & (historic_rain.easting > easting_lim)]
northeast_averageT = northeast.groupby('time')['values'].mean().reset_index()
northeast_average = northeast['values'].mean()
southeast = historic_rain.loc[(historic_rain.northing < northing_lim) & (historic_rain.easting > easting_lim)]
southeast_average = southeast['values'].mean()
southeast_averageT = southeast.groupby('time')['values'].mean().reset_index()
northwest = historic_rain.loc[(historic_rain.northing > northing_lim) & (historic_rain.easting < easting_lim)]
northwest_average = northwest['values'].mean()
northwest_averageT = northwest.groupby('time')['values'].mean().reset_index()
southwest = historic_rain.loc[(historic_rain.northing < northing_lim) & (historic_rain.easting < easting_lim)]
southwest_average = southwest['values'].mean()
southwest_averageT = southwest.groupby('time')['values'].mean().reset_index()
plt.figure(1)
plt.title('Average rain per quadrant' + date)
scale_ls = range(4)
index_ls = ['NE', 'SE', 'NW', 'SW']
area_data=[northeast_average, southeast_average, northwest_average, southwest_average]
plt.xticks(scale_ls, index_ls)
plt.xlabel('Quadrant')
plt.ylabel('Average Rainfall (mm)')
plt.bar(scale_ls,area_data )
plt.savefig('quadrant.png')
plt.show()
plt.subplots()
plt.title('England Rainfall Against Time for ' + date)
plt.subplot(2, 2, 1)
plt.plot(northwest_averageT['time'], northwest_averageT['values'])
plt.xlabel('Time')
plt.xticks(np.arange(0, len(northwest_averageT), 4), rotation=30)
plt.ylabel('Average Rainfall (mm)')
plt.title('North West')
plt.subplot(2, 2, 2)
plt.plot(northeast_averageT['time'], northeast_averageT['values'])
plt.xlabel('Time')
plt.xticks(np.arange(0, len(northeast_averageT), 4), rotation=30)
plt.ylabel('Average Rainfall (mm)')
plt.title('North East')
plt.subplot(223)
plt.plot(southwest_averageT['time'], southwest_averageT['values'])
plt.xlabel('Time')
plt.xticks(np.arange(0, len(southwest_averageT), 4), rotation=30)
plt.ylabel('Average Rainfall (mm)')
plt.title('South West')
plt.subplot(224)
plt.plot(southeast_averageT['time'], southeast_averageT['values'])
plt.xlabel('Time')
plt.xticks(np.arange(0, len(southeast_averageT), 4), rotation=30)
plt.ylabel('Average Rainfall (mm)')
plt.title('South East')
plt.subplots_adjust(left=0.1, bottom=0.05, top=0.9, right=0.95, hspace=0.5)
plt.savefig('quadrant_detail.png')
plt.show()
historic_API('2019-01-03', 406689, 286822)