-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2017-09-09-hurricane_irma.py
610 lines (442 loc) · 15.4 KB
/
2017-09-09-hurricane_irma.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
# coding: utf-8
# # Exploring the NHC Advisories and Sea Surface Height during Hurricane Irma
#
#
# This notebook aims to demonstrate how to create a simple interactive GIS map with the National Hurricane Center predictions [1] and the observed sea surface height from CO-OPS [2].
#
#
# See https://tidesandcurrents.noaa.gov/quicklook/view.html?name=IRMA for the latest information on Irma.
#
# 1. http://www.nhc.noaa.gov/gis/
# 2. https://opendap.co-ops.nos.noaa.gov/ioos-dif-sos/
#
#
# First we have to download the National Hurricane Center (NHC) GIS 5 day predictions data for Irma.
#
# NHC codes storms are coded with 8 letter names:
# - 2 char for region `al` → Atlantic
# - 2 char for number `11` is Irma
# - and 4 char for year, `2017`
#
# Browse http://www.nhc.noaa.gov/gis/archive_wsurge.php?year=2017 to find other hurricanes code.
# In[ ]:
code = 'al112017'
# code = 'al032018'
hurricane = '{}_5day'.format(code)
# In[ ]:
import os
import sys
try:
from urllib.request import urlopen, urlretrieve
except Exception:
from urllib import urlopen, urlretrieve
import lxml.html
def url_lister(url):
urls = []
connection = urlopen(url)
dom = lxml.html.fromstring(connection.read())
for link in dom.xpath('//a/@href'):
urls.append(link)
return urls
def download(url, path):
sys.stdout.write(fname + '\n')
if not os.path.isfile(path):
urlretrieve(
url,
filename=path,
reporthook=progress_hook(sys.stdout)
)
sys.stdout.write('\n')
sys.stdout.flush()
def progress_hook(out):
"""
Return a progress hook function, suitable for passing to
urllib.retrieve, that writes to the file object *out*.
"""
def it(n, bs, ts):
got = n * bs
if ts < 0:
outof = ''
else:
# On the last block n*bs can exceed ts, so we clamp it
# to avoid awkward questions.
got = min(got, ts)
outof = '/%d [%d%%]' % (ts, 100 * got // ts)
out.write("\r %d%s" % (got, outof))
out.flush()
return it
# In[ ]:
nhc = 'http://www.nhc.noaa.gov/gis/forecast/archive/'
# We don't need the latest file b/c that is redundant to the latest number.
fnames = [
fname for fname in url_lister(nhc)
if fname.startswith(hurricane) and 'latest' not in fname
]
# In[ ]:
base = os.path.abspath(
os.path.join(os.path.curdir, 'data', hurricane)
)
if not os.path.exists(base):
os.makedirs(base)
for fname in fnames:
url = '{}/{}'.format(nhc, fname)
path = os.path.join(base, fname)
download(url, path)
# In the cells below we use `geopandas` to load the data we just downloaded. We will use only the prediction cone (`png`) and the track points (`pts`), but feel free to explore this data further, there is plenty more there.
# In[ ]:
import os
# Only needed to run on binder!
# See https://gitter.im/binder-project/binder?at=59bc2498c101bc4e3acfc9f1
os.environ['CPL_ZIP_ENCODING'] = 'UTF-8'
# In[ ]:
from glob import glob
import geopandas
cones, points = [], []
for fname in sorted(glob(os.path.join(base, '{}_*.zip'.format(hurricane)))):
number = os.path.splitext(os.path.split(fname)[-1])[0].split('_')[-1]
pgn = geopandas.read_file(
'/{}-{}_5day_pgn.shp'.format(code, number),
vfs='zip://{}'.format(fname)
)
cones.append(pgn)
pts = geopandas.read_file(
'/{}-{}_5day_pts.shp'.format(code, number),
vfs='zip://{}'.format(fname)
)
# Only the first "obsevartion."
points.append(pts.iloc[0])
# Let's create a color code for the point track.
# In[ ]:
colors = {
'Subtropical Depression': 'yellow',
'Tropical Depression': 'yellow',
'Tropical Storm': 'orange',
'Subtropical Storm': 'orange',
'Hurricane': 'red',
'Major Hurricane': 'crimson'
}
# Now we can get all the information we need from those GIS files. Let's start with the event dates.
# In[ ]:
import arrow
# We are ignoring the timezone, like AST (Atlantic Time Standar) b/c
# those are not a unique identifiers and we cannot disambiguate.
start = points[0]['FLDATELBL']
end = points[-1]['FLDATELBL']
start = arrow.get(start, 'YYYY-MM-DD h:mm A ddd').naive
end = arrow.get(end, 'YYYY-MM-DD h:mm A ddd').naive
# And the bounding box to search the data.
# In[ ]:
from shapely.geometry import LineString
from shapely.ops import cascaded_union
last_cone = cones[-1]['geometry'].iloc[0]
track = LineString([point['geometry'] for point in points])
polygon = cascaded_union([last_cone, track])
# Add a buffer to find the stations along the track.
bbox = polygon.buffer(2).bounds
# Note that the bounding box is derived from the track and the latest prediction cone.
# In[ ]:
strbbox = ', '.join(format(v, '.2f') for v in bbox)
print('bbox: {}\nstart: {}\n end: {}'.format(strbbox, start, end))
# Now we need to build a filter with those parameters to find the observations along the Hurricane path. We still need to specify:
#
# - the units for the observations;
# - and the SOS name for the variables of interest.
#
# Next, we can use `pyoos` to assemble a collector to download the data into a pandas `DataFrame`.
# In[ ]:
import cf_units
from ioos_tools.ioos import collector2table
import pandas as pd
from pyoos.collectors.coops.coops_sos import CoopsSos
from retrying import retry
# We need to retry in case of failure b/c the server cannot handle
# the high traffic during events like Irma.
@retry(stop_max_attempt_number=5, wait_fixed=3000)
def get_coops(start, end, sos_name, units, bbox, verbose=False):
collector = CoopsSos()
collector.set_bbox(bbox)
collector.end_time = end
collector.start_time = start
collector.variables = [sos_name]
ofrs = collector.server.offerings
title = collector.server.identification.title
config = dict(
units=units,
sos_name=sos_name,
)
data = collector2table(
collector=collector,
config=config,
col='{} ({})'.format(sos_name, units.format(cf_units.UT_ISO_8859_1))
)
# Clean the table.
table = dict(
station_name=[s._metadata.get('station_name') for s in data],
station_code=[s._metadata.get('station_code') for s in data],
sensor=[s._metadata.get('sensor') for s in data],
lon=[s._metadata.get('lon') for s in data],
lat=[s._metadata.get('lat') for s in data],
depth=[s._metadata.get('depth', 'NA') for s in data],
)
table = pd.DataFrame(table).set_index('station_name')
if verbose:
print('Collector offerings')
print('{}: {} offerings'.format(title, len(ofrs)))
return data, table
# In[ ]:
ssh, ssh_table = get_coops(
start=start,
end=end,
sos_name='water_surface_height_above_reference_datum',
units=cf_units.Unit('meters'),
bbox=bbox,
)
ssh_table
# In[ ]:
wind_speed, wind_speed_table = get_coops(
start=start,
end=end,
sos_name='wind_speed',
units=cf_units.Unit('m/s'),
bbox=bbox,
)
wind_speed_table
# For simplicity we will use only the stations that have both wind speed and sea surface height and reject those that have only one or the other.
# In[ ]:
common = set(ssh_table['station_code']).intersection(wind_speed_table['station_code'])
# In[ ]:
ssh_obs, win_obs = [], []
for station in common:
ssh_obs.extend([obs for obs in ssh if obs._metadata['station_code'] == station])
win_obs.extend([obs for obs in wind_speed if obs._metadata['station_code'] == station])
# In[ ]:
index = pd.date_range(
start=start.replace(tzinfo=None),
end=end.replace(tzinfo=None),
freq='15min'
)
# Re-index and rename series.
ssh_observations = []
for series in ssh_obs:
_metadata = series._metadata
obs = series.reindex(index=index, limit=1, method='nearest')
obs._metadata = _metadata
obs.name = _metadata['station_name']
ssh_observations.append(obs)
winds_observations = []
for series in win_obs:
_metadata = series._metadata
obs = series.reindex(index=index, limit=1, method='nearest')
obs._metadata = _metadata
obs.name = _metadata['station_name']
winds_observations.append(obs)
# Let's take a look at some stations to see if the data is OK. Below we have a station in Naples, FL along the Gulf of Mexico.
# In[ ]:
get_ipython().run_line_magic('matplotlib', 'inline')
import matplotlib.pyplot as plt
try:
station = '8725110'
w = [obs for obs in winds_observations if obs._metadata['station_code'] == station][0]
s = [obs for obs in ssh_observations if obs._metadata['station_code'] == station][0]
fig, ax = plt.subplots(figsize=(17, 3.75))
s['2017-9-10':].plot(ax=ax, label='Sea surface height (m)', color='#0000ff')
ax1 = w['2017-9-10':].plot(ax=ax, label='Wind speed (m/s)', color='#9900cc', secondary_y=True)
ax.set_title(w._metadata['station_name'])
lines = ax.get_lines() + ax.right_ax.get_lines()
ax.legend(lines, [l.get_label() for l in lines], loc='upper left')
ax.axhline(0, color='black')
ax.set_ylabel('Sea surface height (m)', color='#0000ff')
ax.right_ax.set_ylabel('Wind speed (m/s)', color='#9900cc')
ax1.annotate(
'Eye of the hurricane',
xy=(w['2017-9-10':].argmin().to_pydatetime(), w['2017-9-10':].min()),
xytext=(5, 10),
textcoords='offset points',
arrowprops=dict(
arrowstyle='simple',
facecolor='crimson'
),
)
ax.grid(True)
except Exception:
print('Cannot find station {}'.format(station))
# We can observe the sea level retreating around 10-Sep 9:00 and then a significant surge after 19:00.
# The lower winds at beginning of the surge is probably the eye of the hurricane.
#
# For our interactive map we will use [`bokeh`](https://bokeh.pydata.org/en/latest) HTML plots instead of the usual raster [`matplotlib`](https://matplotlib.org) ones to enhance the user experience when exploring the graphs.
# In[ ]:
from bokeh.resources import CDN
from bokeh.plotting import figure
from bokeh.embed import file_html
from bokeh.models import Range1d, LinearAxis, HoverTool
from folium import IFrame
# Plot defaults.
tools = "pan,box_zoom,reset"
width, height = 750, 250
def make_plot(ssh, wind):
p = figure(toolbar_location='above',
x_axis_type='datetime',
width=width,
height=height,
tools=tools,
title=ssh.name)
p.yaxis.axis_label = 'wind speed (m/s)'
l0 = p.line(
x=wind.index,
y=wind.values,
line_width=5,
line_cap='round',
line_join='round',
legend='wind speed (m/s)',
color='#9900cc',
alpha=0.5,
)
p.extra_y_ranges = {}
p.extra_y_ranges['y2'] = Range1d(
start=-1,
end=3.5
)
p.add_layout(
LinearAxis(
y_range_name='y2',
axis_label='ssh (m)'),
'right'
)
l1 = p.line(
x=ssh.index,
y=ssh.values,
line_width=5,
line_cap='round',
line_join='round',
legend='ssh (m)',
color='#0000ff',
alpha=0.5,
y_range_name='y2',
)
p.legend.location = 'top_left'
p.add_tools(
HoverTool(
tooltips=[
('wind speed (m/s)', '@y'),
],
renderers=[l0],
),
HoverTool(
tooltips=[
('ssh (m)', '@y'),
],
renderers=[l1],
),
)
return p
def make_marker(p, location, fname):
html = file_html(p, CDN, fname)
iframe = IFrame(html, width=width+45, height=height+80)
popup = folium.Popup(iframe, max_width=2650)
icon = folium.Icon(color='green', icon='stats')
marker = folium.Marker(location=location,
popup=popup,
icon=icon)
return marker
# Here is the final result. Explore the map by clicking on the map features plotted!
# In[ ]:
import folium
from folium.plugins import Fullscreen, MarkerCluster
from ioos_tools.ioos import get_coordinates
lon = track.centroid.x
lat = track.centroid.y
m = folium.Map(location=[lat, lon], tiles='OpenStreetMap', zoom_start=4)
Fullscreen(position='topright', force_separate_button=True).add_to(m)
marker_cluster0 = MarkerCluster(name='Observations')
marker_cluster1 = MarkerCluster(name='Past predictions')
marker_cluster0.add_to(m)
marker_cluster1.add_to(m)
url = 'http://oos.soest.hawaii.edu/thredds/wms/hioos/satellite/dhw_5km'
w0 = folium.WmsTileLayer(
url,
name='Sea Surface Temperature',
fmt='image/png',
layers='CRW_SST',
attr='PacIOOS TDS',
overlay=True,
transparent=True)
w0.add_to(m)
url = 'http://hfrnet.ucsd.edu/thredds/wms/HFRNet/USEGC/6km/hourly/RTV'
w1 = folium.WmsTileLayer(
url,
name='HF Radar',
fmt='image/png',
layers='surface_sea_water_velocity',
attr='HFRNet',
overlay=True,
transparent=True)
w1.add_to(m)
def style_function(feature):
return {
'fillOpacity': 0,
'color': 'black',
'stroke': 1,
'weight': 0.5,
'opacity': 0.2,
}
# Latest cone prediction.
latest = cones[-1]
folium.GeoJson(
data=latest.__geo_interface__,
name='Cone prediction as of {}'.format(latest['ADVDATE'].values[0]),
).add_to(m)
# Past cone predictions.
for cone in cones[:-1]:
folium.GeoJson(
data=cone.__geo_interface__,
style_function=style_function,
).add_to(marker_cluster1)
# Latest points prediction.
for k, row in pts.iterrows():
date = row['FLDATELBL']
hclass = row['TCDVLP']
location = row['LAT'], row['LON']
popup = '{}<br>{}'.format(date, hclass)
folium.CircleMarker(
location=location,
radius=10,
fill=True,
color=colors[hclass],
popup=popup,
).add_to(m)
# All the points along the track.
for point in points:
date = point['FLDATELBL']
hclass = point['TCDVLP']
location = point['LAT'], point['LON']
popup = '{}<br>{}'.format(date, hclass)
folium.CircleMarker(
location=location,
radius=5,
fill=True,
color=colors[hclass],
popup=popup,
).add_to(m)
# Observations.
for ssh, wind in zip(ssh_observations, winds_observations):
fname = ssh._metadata['station_code']
location = ssh._metadata['lat'], ssh._metadata['lon']
p = make_plot(ssh, wind)
marker = make_marker(p, location=location, fname=fname)
marker.add_to(marker_cluster0)
folium.LayerControl().add_to(m)
p = folium.PolyLine(get_coordinates(bbox),
color='#009933',
weight=1,
opacity=0.2)
p.add_to(m)
# In[ ]:
def embed_map(m):
from IPython.display import HTML
m.save('index.html')
with open('index.html') as f:
html = f.read()
iframe = '<iframe srcdoc="{srcdoc}" style="width: 100%; height: 750px; border: none"></iframe>'
srcdoc = html.replace('"', '"')
return HTML(iframe.format(srcdoc=srcdoc))
embed_map(m)