/
update.py
411 lines (375 loc) · 15.1 KB
/
update.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
# -*- coding: utf-8 -*-
"""
Scientific units used are as follows,
Coordinates (Lat, Lon) : Decimal Degrees (DD)
Timestamp : Python Datetime
Barometric pressure : mb
Wind Intensity: Knots
"""
from curses import meta
from distutils.command.upload import upload
import xmltodict
import requests
from datetime import datetime
import dateutil.parser
from pytz import timezone
import zipfile
import io
from bs4 import BeautifulSoup
import pandas as pd
import hashlib
import db
import sqlalchemy
from sqlalchemy import MetaData, Table
import json
def past_track(link):
'''
From a KMZ file of a storm in the NHC format, we extract the history
Parameters
----------
link string
The network link or downloadable KMZ href file
Returns
-------
dict
'''
kmz = requests.get(link)
uncompressed = zipfile.ZipFile(io.BytesIO(kmz.content))
# get the kml name
for name in uncompressed.namelist():
# all kml file names begin with al, e.g. 'al202020.kml'
if name[:2] == 'al':
file_name = name
# read the contents of the kml file in the archive
kml = xmltodict.parse(uncompressed.read(file_name))
kml['results'] = []
for attribute in kml['kml']['Document']['Folder']:
if attribute['name'] == 'Data':
for entry in attribute['Placemark']:
# parse time information
time = datetime.strptime(entry['atcfdtg'],
'%Y%m%d%H').replace(
tzinfo=timezone('UTC'))
# add to results
kml['results'].append({
'time' : time,
'wind' : float(entry['intensity']),
'lat' : float(entry['lat']),
'lon' : float(entry['lon']),
'pressure' : float(entry['minSeaLevelPres'])
})
print(kml['results'][-1])
return kml
def nhc() :
'''
Runs the NHC update and populates current Atlantic storms
Returns
-------
array of dict
Each dictionary is in the following form,
{
"storm" : string # the storm ID from the NHC
"metadata" : dict # the kml files used to create the results
"entries" : array of dict # The data for the storm in the form,
{
'time' : Datetime,
'wind' : Knots,
'lat' : Decimal Degrees,
'lon' : Decimal Degrees,
'pressure' : Barometric pressure (mb)
}
}
'''
# this link can be reused to download the most recent data
static_link = 'https://www.nhc.noaa.gov/gis/kml/nhc_active.kml'
# common timezones for parsing with dateutil. offset by seconds
timezones = {
"ADT": 4 * 3600,
"AST": 3 * 3600,
"CDT": -5 * 3600,
"CST": -6 * 3600,
"CT": -6 * 3600,
"EDT": -4 * 3600,
"EST": -5 * 3600,
"ET": -5 * 3600,
"GMT": 0 * 3600,
"PST": -8 * 3600,
"PT": -8 * 3600,
"UTC": 0 * 3600,
"Z": 0 * 3600,
}
# create data structure as dictionary
request = requests.get(static_link)
data = xmltodict.parse(request.text)
results = []
# return if no storms
if 'Folder' not in data['kml']['Document'].keys() :
return results
# parse in storms
for folder in data['kml']['Document']['Folder']:
# the id's that start with 'at' are the storms we are interested in
# others can include 'wsp' for wind speed probabilities
if folder['@id'][:2] == 'at':
# some storms don't have any data because they are so weak
if not 'ExtendedData' in folder.keys():
continue
# storm data structure
storm = {
'metadata': folder,
'entries': []
}
entry = {}
for attribute in folder['ExtendedData'][1]:
if attribute == 'tc:atcfID': # NHC Storm ID
storm['id'] = folder['ExtendedData'][1][attribute]
elif attribute == 'tc:name': # Human readable name
storm['name'] = folder['ExtendedData'][1][attribute]
print(folder['ExtendedData'][1][attribute])
elif attribute == 'tc:centerLat': # Latitude
entry['lat'] = float(folder['ExtendedData'][1][attribute])
elif attribute == 'tc:centerLon': # Longitude
entry['lon'] = float(folder['ExtendedData'][1][attribute])
elif attribute == 'tc:dateTime': # Timestamp
entry['time'] = dateutil.parser.parse(
folder['ExtendedData'][1][attribute],
tzinfos=timezones)
elif attribute == 'tc:minimumPressure': # Barometric pressure
entry['pressure'] = float(folder['ExtendedData'][1]
[attribute].split(' ')[0])
elif attribute == 'tc:maxSustainedWind': # Wind Intensity
# note that we are converting mph to knots
entry['wind'] = float(folder['ExtendedData'][1][attribute].
split(' ')[0]) / 1.151
print(storm['id'])
print(entry)
# add entry to storm
storm['entries'].append(entry)
# get network link and extract past history
for links in folder['NetworkLink']:
if links['@id'] == 'pasttrack':
kml = past_track(links['Link']['href'])
# add history to entries
storm['entries'].extend(kml['results'])
# add history to storm metadata
storm['metadata']['history'] = kml
# add to results
results.append(storm)
return results
def update_global_hwrf():
'''
Provides data based on current global storms based on the HWRF data
https://dtcenter.org/sites/default/files/community-code/hwrf/docs/users_guide/HWRF-UG-2018.pdf
- Page 154 column and data descriptions
Returns
-------
array of dict
Each dictionary is in the following form,
{
"id" : string,
}
'''
config = {
'url': 'https://www.emc.ncep.noaa.gov/gc_wmb/vxt/DECKS/',
'freq': 21600, # (6 hours) frequency, in seconds
'time_column': 'Last Change',
'column_names': ['basin', 'id', 'time', 'is_f', 'model', 'lead_time',
'lat', 'lon', 'wind', 'pressure', 'label', 'radii_threshold', 'radii_begin',
'wind_radii_1', 'wind_radii_2', 'wind_radii_3', 'wind_radii_4', 'isobar_pressure',
'isobar_radius', 'max_wind_radius', 'var_1', 'var_2', 'var_3', 'var_4', 'var_5',
'var_6', 'var_7', 'name', 'var_8'],
'column_buffer': 20,
}
# read in table from link, this is the only (first) table
update_table = pd.read_html(config['url'])[0]
# get relevant table
update_table['timestamp'] = [time.timestamp() for time in pd.to_datetime(update_table[config['time_column']], utc=True)]
# the table has many entries, so we only parse the most recent one
# according to the frequency. We use the most recent in a day
timestamp_threshold = datetime.now().timestamp() - (config['freq'] * 4)
data = update_table[update_table['timestamp'] > timestamp_threshold]
# construct links to applicable data
links = [config['url'] + fname for fname in data['File Name']]
'''
example,
['https://www.emc.ncep.noaa.gov/gc_wmb/vxt/DECKS/ash972023.dat',
'https://www.emc.ncep.noaa.gov/gc_wmb/vxt/DECKS/bsh972023.dat',
'https://www.emc.ncep.noaa.gov/gc_wmb/vxt/DECKS/ash162023.dat',
:
:
'https://www.emc.ncep.noaa.gov/gc_wmb/vxt/DECKS/bsh162023.dat',]
'''
# filter it to just the track, has a b in the first letter of the name
filtered_links = [link for link in links if link.split('/')[-1][0] == 'b']
# for each link, download it and append to an array
column_names = config['column_names'] + [f'unk_{i + 1}' for i in range(config['column_buffer'])]
active_storms = pd.concat([pd.read_csv(link, names=column_names, engine='python') for link in filtered_links],
ignore_index = True)
# trim buffered columns
active_storms = active_storms.dropna(axis=1, how='all')
# change data types of columns
active_storms['time'] = [datetime.strptime(str(time), '%Y%m%d%H').replace(tzinfo=timezone('utc')) for time in active_storms['time']]
def process_coord(c):
'''
The coordinates in the files are in a different
coordinate format like 262N. The data description
claims that we can divide by 10 and get the
decimal representation. This function tries to output
in decimal degrees. A positive value for North and East,
a negative value for South and West.
'''
value = float(c[:-1]) / 10
direction = c[-1:]
return value if direction in ['N', 'E'] else -value
active_storms['lat'] = [process_coord(c) for c in active_storms['lat']]
active_storms['lon'] = [process_coord(c) for c in active_storms['lon']]
active_storms['storm_id'] = [f'{ids[0]}{ids[1]}{ids[2].year}' for ids in zip(
active_storms['basin'],
active_storms['id'],
active_storms['time'])]
# drop duplicates that might have some extra data
postprocessed_data = active_storms.drop_duplicates(
subset=['basin', 'id', 'time', 'model', 'lead_time', 'lat', 'lon', 'wind', 'pressure'])
# rename columns to match data structure
postprocessed_data = postprocessed_data.rename(columns = {'wind': 'int', 'id': '_id'})
postprocessed_data = postprocessed_data.rename(columns = {'storm_id': 'id'})
postprocessed_data['time'] = [timestamp.isoformat() for timestamp in postprocessed_data['time']]
return postprocessed_data
def update_global_rammb():
'''
Provides data based on current global storms based on the RAMMB data
Returns
-------
array of dict
Each dictionary is in the following form,
{
"id" : string,
"urls" : dict,
"data" : {
# note that this data depends on the data source
'track_history' : {
},
'forecast_track' : {
}
}
}
'''
config = {
'url' : 'http://rammb-data.cira.colostate.edu/tc_realtime/',
'ir_img_url' : 'http://rammb-data.cira.colostate.edu/tc_realtime/archive.asp?product=4kmirimg&storm_identifier=',
'base_url' : 'http://rammb-data.cira.colostate.edu'
}
page = requests.get(config['url'])
soup = BeautifulSoup(page.text, 'html.parser')
data = soup.findAll('div',attrs={'class':'basin_storms'})
storms = []
for div in data:
links = div.findAll('a')
for a in links:
storm = {
'id' : a.text[:8],
'urls' : {
'base' : config['url'] + a['href'],
'img_url' : config['ir_img_url'] + a.text[:8].lower()
}
}
print(f'[id]: {storm["id"]}')
print(f'[url]: {storm["urls"]["base"]}')
print(f'[img_url]: {storm["urls"]["img_url"]}')
# get dataframe from url
current_page = requests.get(storm['urls']['base'])
current_soup = BeautifulSoup(current_page.text, 'html.parser')
tables = current_soup.findAll('table')
# we manually input the table names because they're the same
# for every storm
has_forecast = len(tables) > 1
storm['data'] = {
'forecast_track' : pd.read_html(
str(tables[0]),
header = 0)[0].to_dict() if has_forecast else None,
'track_history' : pd.read_html(
str(tables[1]),
header = 0)[0].to_dict() if has_forecast else pd.read_html(
str(tables[0]),
header = 0)[0].to_dict()
}
print(f'[track_history] : {storm["data"]["track_history"]}')
print(f'[forecast_track] : {storm["data"]["forecast_track"]}')
# begin getting img url links
current_page = requests.get(storm['urls']['img_url'])
current_soup = BeautifulSoup(current_page.text, 'html.parser')
img_urls = [config['base_url'] + img_href['href'] for img_href in current_soup.findAll('table')[0].findAll('a')]
storm['urls']['img_urls'] = img_urls
print(f'[1st img_url]: {storm["urls"]["img_urls"][0]}')
storms.append(storm)
return storms
def update_global():
'''
This function decides which global ingestion to use
'''
return update_global_hwrf()
def data_to_hash(data) :
'''
Takes in a Pandas DataFrame and creates a MD5 hash
in order to quickly compare if we have the same data
'''
return hashlib.md5(json.dumps(data).encode()).hexdigest()
def upload_hash(data) :
'''
Checks if the data has already been ingested and returns a
False if it has. It returns the hash if it was successfully uploaded
References
----------
- https://docs.sqlalchemy.org/en/14/tutorial/data_insert.html
'''
hashx = data_to_hash(data)
results = db.query(
f'select hash from ingest_hash where hash = "{hashx}"'
)
if len(results) < 1 : # sqlalchemy 1.4.39
engine = db.get_engine('hurricane_live')
metadata = MetaData()
metadata.reflect(bind=engine)
table = metadata.tables['ingest_hash']
stmnt = table.insert().values(
hash = hashx,
data = {"ingest" : data},
time = datetime.now().isoformat()
)
db.query(q = (stmnt,), write = True)
return {
'hash' : hashx,
'unique' : (len(results) < 1)
}
def global_pipeline() :
data = update_global()
# generate data
hurricane_rows = data[['id', 'time', 'lat', 'lon', 'int']].to_dict('records')
# check if data is unique
hashx = upload_hash(hurricane_rows)
print(f'data hash: {hashx["hash"]}')
if hashx['unique'] :
# process the data into the live database
engine = db.get_engine('hurricane_live')
metadata = MetaData()
metadata.reflect(bind=engine)
table = metadata.tables['hurricane_live']
# reset live table
db.query(q = ('DELETE FROM hurricane_live',), write = True)
db.query(q = (table.insert(), hurricane_rows), write = True)
return {
'dataframe' : pd.DataFrame(hurricane_rows),
'hash' : hashx['hash'],
'unique' : hashx['unique']
}
def live_deltas():
'''
Returns a representation of the changes in the live data
'''
df = db.query('select data from ingest_hash')
deltas = []
prev = None
for row in df.iterrows() :
row['data']
return df
if __name__ == "__main__" :
update_global()