-
Notifications
You must be signed in to change notification settings - Fork 1
/
shread_dash.py
652 lines (615 loc) · 23.5 KB
/
shread_dash.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
"""
Created on Wed Dec 23 16:35:45 2020
Snow-Hydrology Repo for Evaluation, Analysis, and Decision-making Dashboard (shread_dash.py)
This repository contains a series of batch scripts and python codes to run the Snow-Hydrology Repo for Evaluation,
Analysis, and Decision-making Dashboard (or "SHREAD Dash"). SHREAD plot has two main components: the database and
the dashboard. The database relies on a series of retrieval scripts (/database/) that retrieve hydrometeorological
data from online and store the data in local databases. Part of the retrieval process is dependent on the SHREAD
repository (https://github.com/tclarkin/shread), The databases are built in SQLite. The dashboard (this script) is
built with Dash by Plotly and is configured to run locally.
@author: tclarkin, buriona (2020-2022)
"""
### Import Dependencies & Define Functions
import os
import json
import datetime as dt
import dash
import pandas as pd
import dash_core_components as dcc
import dash_html_components as html
import dash_bootstrap_components as dbc
from dash.dependencies import Input, Output, State
import database
from database import basin_list
from database import start_date, end_date, dust_disable
from database import snotel_list,usgs_list,csas_list,forecast_list
from database import sloperange, elevrange, aspectdict, elevdict, slopedict
from plot_lib.utils import get_plot_config
from plot_lib.snow_plot import get_snow_plot
from plot_lib.met_plot import get_met_plot
from plot_lib.flow_plot import get_flow_plot
from plot_lib.csas_plot import get_csas_plot
#from plot_lib.test_ndfd_plot import get_test_plot
### Configure app ###
# Initialize dash
app = database.app
app_dir = os.path.dirname(os.path.realpath(__file__))
# Load and configure presets (from presets.csv) for nav bar
res_dir = os.path.join(app_dir, 'resources')
try:
presets = pd.read_csv(os.path.join(res_dir, "presets.csv"))
except FileNotFoundError:
presets = pd.DataFrame()
for col in ["snotels","usgss","csass","elevations","aspects","slopes","frcst"]:
presets[col] = presets[col].apply(lambda x: json.loads(x))
presets.index = presets.id
preset_options = list()
input_options = list()
for p in presets.index:
preset_options.append(dbc.DropdownMenuItem(presets.loc[p,"name"], id=p))
input_options.append(Input(p,"n_clicks"))
# Define navbar layout
def get_navbar():
return dbc.Navbar(
[
dbc.Col(
html.Img(
src=app.get_asset_url('BofR-vert-cmyk.png'),
className='img-fluid'
),
width=1
),
dbc.Col(
html.Div(
[
html.H1(['WCAO Dashboard'])
]
),
width=5
),
dbc.Col(html.Div(
[
dbc.DropdownMenu(
label="Time Window Presets",
color="light",
children=
[
dbc.DropdownMenuItem("Now", id="set_now"),
dbc.DropdownMenuItem("2023", id="2023_window"),
dbc.DropdownMenuItem("2022", id="2022_window"),
dbc.DropdownMenuItem("2021", id="2021_window"),
],
)
]
),
width=2
),
dbc.Col(html.Div(
[
dbc.DropdownMenu(
label="Managed Basin Presets",
color="light",
children=preset_options,
)
]
),
width=2
),
dbc.Col(html.Div(
[
dbc.DropdownMenu(
label="Report Error",
color="light",
children=
[
dbc.Alert(
[
"Please report errors to ",
html.A("[email protected]",href="mailto:[email protected]",className="alert-link"),
],
color = "warning"
)
]
)
]
),
width=2
)
],
className='mb-4'
)
# Define dashboard layout
def get_layout():
return html.Div(
className="mx-2",
children=[
dbc.Row(
[
get_navbar()
]
),
dbc.Row(
[
dbc.Col(
[
dbc.FormGroup(
[
html.H4('Select basin:'),
dcc.Dropdown(
id='basin',
options=basin_list,
placeholder="Select basin",
value=[],
multi=False),
dbc.Label('Set basin filters:'),
dcc.RangeSlider(
id='elevations',
min=elevrange[0],
max=elevrange[1],
step=1,
allowCross=False,
marks=elevdict,
value=[elevrange[0], elevrange[1]]
),
dcc.RangeSlider(
id='slopes',
min=sloperange[0],
max=sloperange[1],
step=1,
allowCross=False,
marks=slopedict,
value=[sloperange[0], sloperange[1]]
),
dcc.RangeSlider(
id='aspects',
min=-90,
max=360,
step=45,
allowCross=False,
marks=aspectdict,
value=[0, 360]
),
html.Div(
id='mean_elevation'
)
]
),
]
),
dbc.Col(dbc.FormGroup(
[
html.H4('Select time options:'),
html.Div(html.P()),
dcc.DatePickerRange(
id='date_selection',
start_date=start_date,
end_date=end_date,
),
html.Div(html.P()),
dbc.RadioItems(
id='dtype',
options=[{'label': "Daily", 'value': "dv"},
{'label': "Instantaneous", 'value': "iv"}],
value='dv',
inline=True
),
# dbc.Checkbox(
# id='plot_forecast',
# ),
# dbc.Label(
# "Include Forecast Data",
# style=dict(marginLeft=10),
# html_for="plot_forecast",
# ),
html.Div(html.P()),
dbc.Label('Select forecast variables:'),
dcc.Dropdown(
id='forecast_sel',
options=forecast_list,
placeholder="Select NDFD variables",
value=[],
multi=True),
]
)),
dbc.Col(dbc.FormGroup(
[
html.H4('Select other options:'),
dbc.Checkbox(
id='offline',
checked=True,
),
dbc.Label(
"Offline Mode",
style=dict(marginLeft=10),
html_for="offline",
),
dbc.RadioItems(
id='stype',
options=[{'label': "SWE", 'value': "swe"},
{'label': "Snow Depth", 'value': "sd"}],
value="sd",
inline=True
),
html.Div(html.P()),
dbc.Checkbox(
id='plot_dust',
disabled=dust_disable,
),
dbc.Label(
"Include CSAS Dust Layers",
style=dict(marginLeft=10),
html_for="plot_dust"
),
html.Div(html.B()),
dbc.Checkbox(
id='plot_albedo_snow',
),
dbc.Label(
"Plot CSAS Albedo on Snow Plot",
style=dict(marginLeft=10),
html_for="plot_albedo_snow",
),
html.Div(html.B()),
dbc.Checkbox(
id='plot_albedo_met',
),
dbc.Label(
"Plot CSAS Albedo on Meteo Plot",
style=dict(marginLeft=10),
html_for="plot_albedo_met",
),
html.Div(html.B()),
dbc.Checkbox(
id='plot_albedo_flow',
),
dbc.Label(
"Plot CSAS Albedo on Flow Plot",
style=dict(marginLeft=10),
html_for="plot_albedo_flow",
),
html.Div(html.B()),
dbc.Checkbox(
id='plot_albedo_csas',
),
dbc.Label(
"Plot CSAS Albedo on CSAS Plot",
style=dict(marginLeft=10),
html_for="plot_albedo_csas",
)
]
))
]
),
dbc.Row(
[
dbc.Col(dbc.FormGroup(
[
dbc.Label('Select point observation sites:'),
dcc.Dropdown(
id='snotel_sel',
options=snotel_list,
placeholder="Select SNOTEL sites",
value=[],
multi=True),
html.Div(html.B()),
dcc.Dropdown(
id='usgs_sel',
options=usgs_list,
placeholder="Select FLOW gages",
value=[],
multi=True),
html.Div(html.B()),
dcc.Dropdown(
id='csas_sel',
options=csas_list,
placeholder="Select CSAS study plots",
value=[],
multi=True
)
]
))
]
),
## TODO: Change to figure with subplots.
dbc.Row(
[
dbc.Col(dbc.FormGroup(
[
dcc.Graph(
id='snow_plot',
config=get_plot_config("dashboard_snow.jpg"),
),
]
))
]
),
dbc.Row(
[
dbc.Col(dbc.FormGroup(
[
dcc.Graph(
id='met_plot',
config = get_plot_config("dashboard_met.jpg")
),
]
))
]
),
dbc.Row(
[
dbc.Col(dbc.FormGroup(
[
dcc.Graph(
id='flow_plot',
config = get_plot_config("dashboard_flow.jpg")
),
]
))
]
),
dbc.Row(
[
dbc.Col(dbc.FormGroup(
[
dcc.Graph(
id='csas_plot',
config=get_plot_config("dashboard_csas.jpg")
),
]
))
]
),
# dbc.Row(
# [
# dbc.Col(dbc.FormGroup(
# [
# dcc.Graph(
# id='test_plot',
# config=get_plot_config("dashboard_test.jpg")
# ),
# ]
# ))
# ]
# ),
]
)
# Call dashboard layout into app
app.layout = get_layout()
### Configure callback functions ###
@app.callback(
Output('forecast_sel', 'disabled'),
[Input('date_selection', 'end_date')]
)
def disable_forecast(end_date):
"""
:description: this function disables forecast data if time window doesn't exctend to future.
:param end_date: the end date selected
:return: series of booleans (True/False)
"""
end_date = dt.datetime.strptime(end_date, "%Y-%m-%d")
today = dt.datetime.now()
#print(today)
if end_date<today:
print("forecasts disabled.")
return(True)
else:
return(False)
@app.callback(
Output('basin', 'value'),
Output('dtype', 'value'),
Output('forecast_sel', 'value'),
Output('plot_albedo_flow', 'value'),
Output('stype', 'value'),
Output('plot_dust', 'value'),
Output('plot_albedo_met', 'value'),
Output('snotel_sel', 'value'),
Output('usgs_sel', 'value'),
Output('csas_sel', 'value'),
Output('elevations', 'value'),
Output('aspects', 'value'),
Output('slopes', 'value'),
input_options
)
def load_presets(a,b,c,d,e,f):
"""
:description: this function applies user specified presets based on dropdown menu clicks
:return: user specified presets, as defined below.
"""
changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
id = changed_id.split(".")[0]
#print(changed_id)
if id not in presets.index:
id = presets.index[0]
# basins
basins = presets.loc[id,"basins"]
dtypes = presets.loc[id,"dtypes"]
frcst = presets.loc[id,"frcst"]
albedo_flow = presets.loc[id,"albedo_flow"]
stypes = presets.loc[id,"stypes"]
dusts = presets.loc[id,"dusts"]
albedo_met = presets.loc[id,"albedo_met"]
snotels = presets.loc[id,"snotels"]
usgss = presets.loc[id,"usgss"]
csass = presets.loc[id,"csass"]
elevations = presets.loc[id,"elevations"]
aspects = presets.loc[id,"aspects"]
slopes = presets.loc[id,"slopes"]
return(basins,dtypes,frcst,albedo_flow,stypes,dusts,albedo_met,snotels,usgss,csass,elevations,aspects,slopes)
@app.callback(
Output('date_selection', 'start_date'),
Output('date_selection', 'end_date'),
[Input('set_now', 'n_clicks'),
Input('2021_window', 'n_clicks'),
Input('2022_window', 'n_clicks'),
Input('2023_window', 'n_clicks'),
State('date_selection', 'start_date'),
State('date_selection', 'end_date')]
)
def load_preset_dates(a,b,c,d,start,end):
"""
:description: this function applies user specified dates based on dropdown menu clicks
:return: user specified dates
"""
changed_id = [p['prop_id'] for p in dash.callback_context.triggered][0]
if '2021_window' in changed_id:
start_date = "2021-01-01"
end_date = "2021-07-01"
elif '2022_window' in changed_id:
start_date = "2021-11-01"
end_date = dt.datetime.now().date() + dt.timedelta(days=10)
elif '2023_window' in changed_id:
start_date = "2022-11-01"
end_date = dt.datetime.now().date() + dt.timedelta(days=10)
else:
start_date = dt.datetime.now().date() - dt.timedelta(days=10)
start_date = dt.datetime.strftime(start_date, "%Y-%m-%d")
end_date = dt.datetime.now().date() + dt.timedelta(days=10)
end_date = dt.datetime.strftime(end_date, "%Y-%m-%d")
return(start_date,end_date)
@app.callback(
Output('snow_plot', 'figure'),
Output('mean_elevation', 'children'),
[
Input('basin', 'value'),
Input('stype', 'value'),
Input('elevations', 'value'),
Input('aspects', 'value'),
Input('slopes', 'value'),
Input('date_selection', 'start_date'),
Input('date_selection', 'end_date'),
Input('dtype', 'value'),
Input('snotel_sel', 'value'),
Input('csas_sel','value'),
Input('forecast_sel','value'),
Input('plot_albedo_snow','checked'),
Input('offline','checked'),
State('snow_plot', 'figure'),
State('mean_elevation', 'children'),
])
def update_snow_plot(basin, stype, elrange, aspects, slopes, start_date,
end_date, dtype,snotel_sel,csas_sel,forecast_sel,plot_albedo,offline,fig,basin_stats):
fcst_update = True
ctx = dash.callback_context.triggered[0]['prop_id'].split('.')[0]
if ctx == "forecast_sel":
fcst_update = False
for fcst in forecast_sel:
if fcst in ["snow","rhm","sky"]:
fcst_update = True
else:
continue
if fcst_update:
fig, basin_stats = get_snow_plot(
basin, stype, elrange, aspects, slopes, start_date,
end_date, dtype,snotel_sel,csas_sel,forecast_sel,plot_albedo,
offline
)
return fig, basin_stats
@app.callback(
Output('met_plot', 'figure'),
[
Input('basin', 'value'),
Input('elevations', 'value'),
Input('aspects', 'value'),
Input('slopes', 'value'),
Input('date_selection', 'start_date'),
Input('date_selection', 'end_date'),
Input('snotel_sel', 'value'),
Input('csas_sel','value'),
Input('plot_albedo_met','checked'),
Input('dtype', 'value'),
Input('forecast_sel','value'),
Input('offline','checked'),
State('met_plot', 'figure')
])
def update_met_plot(basin, elrange, aspects, slopes, start_date,
end_date, snotel_sel, csas_sel, plot_albedo, dtype,
forecast_sel,offline,fig):
fcst_update = True
ctx = dash.callback_context.triggered[0]['prop_id'].split('.')[0]
if ctx == "forecast_sel":
fcst_update = False
for fcst in forecast_sel:
if fcst in ["mint","maxt","qpf","pop12"]:
fcst_update = True
else:
continue
if fcst_update:
fig = get_met_plot(
basin, elrange, aspects, slopes, start_date,
end_date, snotel_sel, csas_sel, plot_albedo, dtype,
forecast_sel,offline
)
return fig
@app.callback(
Output('flow_plot', 'figure'),
[
Input('usgs_sel', 'value'),
Input('dtype', 'value'),
Input('forecast_sel', 'value'),
Input('date_selection', 'start_date'),
Input('date_selection', 'end_date'),
Input('csas_sel','value'),
Input('plot_albedo_flow','checked'),
Input('offline','checked'),
State('flow_plot', 'figure'),
])
def update_flow_plot(usgs_sel, dtype, forecast_sel, start_date, end_date,
csas_sel, plot_albedo,
offline,fig):
fcst_update = True
ctx = dash.callback_context.triggered[0]['prop_id'].split('.')[0]
if ctx == "forecast_sel":
fcst_update = False
for fcst in forecast_sel:
if fcst in ["flow"]:
fcst_update = True
else:
continue
if fcst_update:
fig = get_flow_plot(
usgs_sel, dtype, forecast_sel, start_date, end_date, csas_sel, plot_albedo,
offline
)
return fig
@app.callback(
Output('csas_plot', 'figure'),
[
Input('date_selection', 'start_date'),
Input('date_selection', 'end_date'),
Input('plot_dust',"checked"),
Input('csas_sel', 'value'),
Input('dtype', 'value'),
Input('plot_albedo_csas','checked'),
Input('offline','checked'),
])
def update_csas_plot(start_date, end_date, plot_dust, csas_sel, dtype, albedo,offline):
fig = get_csas_plot(start_date, end_date, plot_dust, csas_sel, dtype, albedo,offline)
return fig
# @app.callback(
# Output('test_plot', 'figure'),
# [
# Input('forecast_sel', 'value'),
# Input('forecast_sel','value'),
# Input('basin', 'value'),
# Input('elevations', 'value'),
# Input('aspects', 'value'),
# Input('slopes', 'value'),
# Input('date_selection', 'start_date'),
# Input('date_selection', 'end_date'),
# ])
# def update_test_plot(forecast_sel,forecast_sel,basin,elrange,aspects,slopes,start_date,end_date):
#
# fig = get_test_plot(forecast_sel,
# forecast_sel,basin,elrange,aspects,slopes,start_date,end_date
# )
# return fig
### LAUNCH DASHBOARD ###
if __name__ == '__main__':
port = int(os.environ.get('PORT',5001))
app.server.run(debug=True,port=5001)
@app.after_request
def after_request(response):
for query in get_debug_queries():
if query.duration >= 0:
print(query.statement, query.parameters, query.duration, query.context)
return response