-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
307 lines (262 loc) · 9.47 KB
/
app.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import dash
import dash_html_components as html
import dash_core_components as dcc
from dash.dependencies import Input, Output
import datetime as dt
import itertools
import numpy as np
import matplotlib.tri as tri
import import_data
import base64
with open("./assets/map.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode()
#add the prefix that plotly will want when using the string as source
encoded_image = "data:image/png;base64," + encoded_string
compuestos = {"Dióxido de Azufre": 1, "Monóxido de Carbono" : 6, "Monóxido de Nitrógeno": 7, "Dióxido de Nitrógeno": 8, "Partículas < 2.5 µm": 9, "Partículas < 10 µm": 10, "Óxidos de Nitrógeno": 12, "Ozono": 14, "Tolueno": 20, "Benceno": 30, "Etilbenceno": 35, "Metaxileno": 37, "Paraxileno": 38, "Ortoxileno": 39}
horas = {"1 AM": "H01", "2 AM": "H02", "3 AM": "H03", "4 AM": "H04", "5 AM": "H05", "6 AM": "H06", "7 AM": "H07", "8 AM": "H08", "9 AM": "H09", "10 AM": "H10", "11 AM": "H11", "12 AM": "H12", "1 PM": "H13", "2 PM": "H14", "3 PM": "H15", "4 PM": "H16", "5 PM": "H17", "6 PM": "H18", "7 PM": "H19", "8 PM": "H20", "9 PM": "H21", "10 PM": "H22", "11 PM": "H23", "12 PM": "H24"}
data = import_data.generar_datos()
# Obtenemos la última hora con datos distintos de 0
for i in range(len(horas)):
if(sum(data[horas[list(horas.keys())[i]]].values) != 0):
latest = i
def actualizar_datos(tiempo, variable):
global datos, width, height, z, x, y, x_mean, y_mean
datos = data.iloc[np.where(data.MAGNITUD == variable)[0], :]
width = np.max(datos["lon"])-np.min(datos["lon"])
height = np.max(datos["lat"])-np.min(datos["lat"])
z = datos[tiempo].values
if variable == 6:
z = z * 1000
x = datos["lon"].values
y = datos["lat"].values
y_mean = np.mean(y)
x_mean = np.mean(x)
def interpolation(x, y, z):
ngridx = 200
ngridy = 200
xi = np.linspace(np.min(x), np.max(x), ngridx)
yi = np.linspace(np.min(y), np.max(y), ngridy)
triang = tri.Triangulation(x, y)
interpolator = tri.LinearTriInterpolator(triang, z)
Xi, Yi = np.meshgrid(xi, yi)
zi = interpolator(Xi, Yi)
return(xi, yi, zi)
app = dash.Dash()
app.scripts.config.serve_locally=True
app.css.append_css({"external_url": "https://codepen.io/chriddyp/pen/dZVMbK.css"})
server = app.server
styles = {
'pre': {
'border': 'thin lightgrey solid',
'overflowX': 'scroll'
},
'header': {
'height': '60px',
'line-height': '60px',
'padding': '5px',
'padding-left': '10px',
'backgroundColor': '#333333'
},
'title': {
'font-size': '26px',
'padding-left:': '10px',
'color': '#ffffff'
},
'logo': {
'float': 'right',
'padding-left': '15px',
'padding': '10px'
},
'plot': {
'border-width': '10px',
'border-left-style': 'solid',
'color': '#ffffff',
'backgroundColor': '#333333',
'padding' : '10px'
}
}
colors = {
'background': '#333333',
'text': '#ffffff',
'text2': '#706f6f'
}
app.layout = html.Div(style = {}, children =[
html.Div(
style = styles['header'], children = [
html.Div(
className='six columns',
children = html.Div(style = styles['title'], children = 'Air Quality Madrid' )
),
html.Div(
className='six columns',
children=html.Div(
style = styles['logo'],
children=[html.Img(src='https://www.madrid.es/assets/images/logo-madrid.png')])
)
]),
html.Div(
children = [
html.Div(
style = {'color': colors['text'], 'backgroundColor': colors['background'], 'padding' : '10px'},
className='two columns',
children = [
html.H5("Compuesto (µg/m³)"),
html.Div(
style = {'color': colors['background']},
children =
dcc.Dropdown(
id='variable',
options=[{'label': i, "value": compuestos[i]} for i in compuestos],
value=8,
)
),
html.H5("Hora"),
html.Div(
style = {'color': colors['background']},
children =
dcc.Dropdown(
id='time')
),
html.Div([
html.Div(id='live-update-text'),
dcc.Interval(
id='interval-component',
interval=1000*60*30, # in milliseconds
n_intervals=0
)
]),
html.Br(),
html.A('Sobre esta app y los datos', href='https://github.com/crossvalidados/AQ_Madrid/blob/master/README.md', target="_blank"),
html.Br(),
html.A('Código fuente', href='https://github.com/crossvalidados/AQ_Madrid', target="_blank"),
html.H6("Autores:", style={'color': colors['text2'], 'font-size': '1em'}),
html.H6("Manuel Bajo y Kevin Craig", style={'color': colors['text2'], 'font-size': '1em'})
]),
html.Div(
className='five columns',
style = styles['plot'],
children = [
html.H5("Mapa estaciones"),
dcc.Graph(id='map', animate=True)
]
),
html.Div(
className='five columns',
style = styles['plot'],
children = [
html.H5("Mapa interpolación"),
html.Div(
children = dcc.Graph(id='cont'))
]
)
])
])
@app.callback(Output('live-update-text', 'children'),
[Input('interval-component', 'n_intervals')])
def update_metrics(n):
global data
data = import_data.generar_datos()
style = {'padding': '5px', 'fontSize': '16px'}
return[]
@app.callback(Output('time', 'options'),
[Input('interval-component', 'n_intervals')])
def update_date_dropdown(name):
global latest
for i in range(len(horas)):
if(sum(data[horas[list(horas.keys())[i]]].values) != 0):
latest = i
return [{'label': i, "value": horas[i]} for i in dict(itertools.islice(horas.items(), latest + 1))]
@app.callback(Output('time', 'value'),
[Input('time', 'options')])
def update_value_dropdown(available_options):
return available_options[-1]['value']
@app.callback(
dash.dependencies.Output('cont', 'figure'),
[dash.dependencies.Input('time', 'value'),
dash.dependencies.Input('interval-component', 'n_intervals'),
dash.dependencies.Input('variable', 'value')])
def display_stores_over_time(time, n, variable):
actualizar_datos(time, variable)
xi, yi, zi = interpolation(x, y, z)
return {
'data': [
{
'x': xi,
'y': yi,
'z': zi,
'name': 'Open Date',
'type': 'contour',
'opacity': 0.35,
'line': {'width': 0},
'contours': {'coloring': 'fill'},
'connectgaps': True
}
],
'layout': {
'margin': {'l': 40, 'r': 20, 't': 10, 'b': 30},
'xaxis': dict(
autorange=True,
title='Longitud',
showgrid=False,
zeroline=False,
showline=False,
),
'yaxis': dict(
autorange=True,
title = 'Latitud',
showgrid=False,
tickangle=270,
zeroline=False,
showline=False,
),
'images': [dict(
source=encoded_image,
xref= "x",
yref= "y",
x= np.min(datos["lon"]) - 0.05*width,
y= np.max(datos["lat"]) + 0.05*height,
sizex= width + 0.1*width,
sizey= height + 0.1*height,
sizing= "stretch",
opacity= 1,
layer= "below")]
}
}
@app.callback(
dash.dependencies.Output('map', 'figure'),
[dash.dependencies.Input('time', 'value'),
dash.dependencies.Input('interval-component', 'n_intervals'),
dash.dependencies.Input('variable', 'value')])
def display_map(time, n, variable):
actualizar_datos(time, variable)
estacion = datos.iloc[np.where(datos.MAGNITUD == variable)[0], :]["ESTACIÓN"].values
return {
'data': [{
'lat': y.flatten(),
'lon': x.flatten(),
'type': 'scattermapbox',
'marker': {'size': 8, 'opacity': 0.8},
'text': estacion + ': ' + [str(i) for i in z],
'hoverinfo': "text",
'selected': {
'marker': {'color': '#85144b'}
}
}],
'layout': {
'mapbox': {
'center': {
'lat': y_mean,
'lon': x_mean
},
'zoom': 10.5,
'pitch': 0,
'accesstoken': 'pk.eyJ1IjoibWJham9idWUiLCJhIjoiY2pyeTFuMWRrMHFwOTQ5b2E5b2E3Y3NleiJ9.0UXhwZBeHtsd7SPe_0E0QQ'
},
'margin': {'l': 0, 'r': 0, 't': 0, 'b': 0}
}
}
if __name__ == '__main__':
app.run_server(debug=True)