-
Notifications
You must be signed in to change notification settings - Fork 0
/
gliderkmz.py
691 lines (579 loc) · 31.4 KB
/
gliderkmz.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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#!/usr/bin/env python
"""
Author: lgarzio and lnazzaro on 2/28/2024
Last modified: lgarzio on 11/7/2024
Generate glider .kmzs for either 1) all active deployments or 2) a user specified deployment
"""
import os
import argparse
import sys
import zipfile
import re
import pytz
from dateutil import parser
import datetime as dt
import pandas as pd
import numpy as np
import requests
import simplekml
import yaml
import geopy.distance
import math
from jinja2 import Environment, FileSystemLoader
from oceans.ocfis import uv2spdir, spdir2uv
from magnetic_field_calculator import MagneticFieldCalculator
pd.set_option('display.width', 320, "display.max_columns", 10)
def add_sensor_values(data_dict, sensor_name, sdf, thresholds=None):
"""
Find data from a sensor within a specific time range (-5 minutes from surface connect time thru +5 minutes from
surface disconnect time). Add the median of the values to the dictionary summaries
"""
if thresholds:
try:
sthresholds = thresholds[sensor_name]
except KeyError:
sthresholds = None
else:
sthresholds = None
cts = pd.to_datetime(data_dict['connect_ts'])
dcts = pd.to_datetime(data_dict['disconnect_ts'])
t0 = cts - pd.Timedelta(minutes=15)
try:
sdf_sel = sdf.loc[np.logical_and(sdf.ts >= t0, sdf.ts <= dcts)].sort_values(by='epoch_seconds')
sensor_value = format_float(np.array(sdf_sel.value)[-1]) # grab the latest value reported for this surfacing
if np.isnan(sensor_value):
sensor_value = None
bgcolor = 'BEA60E' # yellow BEA60E
else:
if sthresholds:
bgcolor = 'green'
if 'suspect_low' in sthresholds.keys() and sensor_value <= sthresholds['suspect_low']:
bgcolor = 'BEA60E' # yellow BEA60E
elif 'suspect_high' in sthresholds.keys() and sensor_value <= sthresholds['suspect_high']:
bgcolor = 'BEA60E' # yellow BEA60E
elif 'fail_low' in sthresholds.keys() and sensor_value <= sthresholds['fail_low']:
bgcolor = 'darkred' # yellow BEA60E
elif 'fail_high' in sthresholds.keys() and sensor_value >= sthresholds['fail_high']:
bgcolor = 'darkred' # yellow BEA60E
else:
bgcolor = 'BEA60E' # yellow BEA60E
except IndexError:
sensor_value = None
bgcolor = 'BEA60E' # yellow BEA60E
data_dict[sensor_name] = sensor_value
data_dict[f'{sensor_name}_bgcolor'] = bgcolor
def build_popup_dict(data):
"""
Build the dictionaries for the data that populates the pop-up text boxes
:param data: dictionary
"""
connect_ts = format_ts_epoch(data['connect_time_epoch'])
disconnect_ts = format_ts_epoch(data['disconnect_time_epoch'])
gps_connect_ts = format_ts_epoch(data['gps_timestamp_epoch'])
gps_connect_timedelta = dt.datetime.fromtimestamp(data['connect_time_epoch'], dt.UTC) - dt.datetime.fromtimestamp(data['gps_timestamp_epoch'], dt.UTC)
if gps_connect_timedelta.seconds >= 30*60: # 30 minutes (per Dave slack message 4/1/2024)
gps_bgcolor = 'darkred'
# elif gps_connect_timedelta.seconds > 10*6: # between 10 mins and fail (above)
# gps_bgcolor = 'BEA60E' # yellow BEA60E
else: # < suspect (or fail if not using suspect range)
gps_bgcolor = 'green'
segment_ewo = f"{format_int(data['segment_errors'])}/{format_int(data['segment_warnings'])}/{format_int(data['segment_oddities'])}"
mission_ewo = f"{format_int(data['mission_errors'])}/{format_int(data['mission_warnings'])}/{format_int(data['mission_oddities'])}"
total_ewo = f"{format_int(data['total_errors'])}/{format_int(data['total_warnings'])}/{format_int(data['total_oddities'])}"
try:
waypoint_range_km = data['waypoint_range_meters'] / 1000
except TypeError:
waypoint_range_km = None
popup_dict = dict(
connect_ts=connect_ts,
disconnect_ts=disconnect_ts,
gps_lat=format_coordinates_str(data['gps_lat'], 'lat'),
gps_lon=format_coordinates_str(data['gps_lon'], 'lon'),
gps_connect_ts=gps_connect_ts,
gps_bgcolor=gps_bgcolor,
reason=data['surface_reason'],
mission=data['mission'],
filename=data['filename'],
filename_8x3=data['the8x3_filename'],
dsvr_log=data['dsvr_log_name'],
segment_ewo=format_ewo(segment_ewo),
mission_ewo=format_ewo(mission_ewo),
total_ewo=format_ewo(total_ewo),
waypoint_lat=format_coordinates_str(data['waypoint_lat'], 'lat'),
waypoint_lon=format_coordinates_str(data['waypoint_lon'], 'lon'),
waypoint_range=format_float(waypoint_range_km),
waypoint_bearing=format_int(data['waypoint_bearing_degrees'])
)
return popup_dict
def calculate_compass_bearing(pointA, pointB):
"""
Calculates the bearing between two points.
From https://gist.github.com/jeromer/2005586?permalink_comment_id=4669453 and
https://towardsdatascience.com/calculating-the-bearing-between-two-geospatial-coordinates-66203f57e4b4
The formulae used is the following:
θ = atan2(sin(Δlong).cos(lat2),
cos(lat1).sin(lat2) − sin(lat1).cos(lat2).cos(Δlong))
:Parameters:
- `pointA: The tuple representing the latitude/longitude for the
first point. Latitude and longitude must be in decimal degrees
- `pointB: The tuple representing the latitude/longitude for the
second point. Latitude and longitude must be in decimal degrees
:Returns:
The bearing in degrees
:Returns Type:
float
"""
if (type(pointA) != tuple) or (type(pointB) != tuple):
raise TypeError("Only tuples are supported as arguments")
lat1 = math.radians(pointA[0])
lat2 = math.radians(pointB[0])
diffLong = math.radians(pointB[1] - pointA[1])
x = math.sin(diffLong) * math.cos(lat2)
y = math.cos(lat1) * math.sin(lat2) - (math.sin(lat1) * math.cos(lat2) * math.cos(diffLong))
initial_bearing = math.atan2(x, y)
# Now we have the initial bearing but math.atan2 return values
# from -180° to + 180° which is not what we want for a compass bearing
# The solution is to normalize the initial bearing as shown below
initial_bearing = math.degrees(initial_bearing)
compass_bearing = (initial_bearing + 360) % 360
return compass_bearing
def convert_kml_to_kmz(kml_file_path, kmz_file_path=None):
if kmz_file_path is None:
kmz_file_path = f'{os.path.splitext(kml_file_path)[0]}.kmz'
with zipfile.ZipFile(kmz_file_path, 'w', zipfile.ZIP_DEFLATED) as kmz:
# Define the arcname to be ‘doc.kml’ as per KMZ file specification
kmz.write(kml_file_path, arcname='doc.kml')
return kmz_file_path
def convert_coordinates(x):
"""
Convert lat/lon coordinates from nmea to decimal degrees
"""
try:
output = np.sign(x) * (np.floor(np.abs(x)/100) + np.mod(np.abs(x), 100) / 60)
except TypeError:
output = None
return output
def format_coordinates_str(x, version):
"""
Convert lat/lon coordinates from nmea to degrees decimal minutes string format
version: 'lat' or 'lon'
"""
try:
decdegrees = np.sign(x) * (np.floor(np.abs(x)/100) + np.mod(np.abs(x), 100) / 60)
if version == 'lat':
if x >= 0:
hemis = 'N'
else:
hemis = 'S'
elif version == 'lon':
if x >= 0:
hemis = 'E'
else:
hemis = 'W'
else:
ValueError('Incorrect version provided')
# convert from decimal degrees to degrees decimal minutes
minutes, degrees = math.modf(decdegrees)
output = f"{int(abs(degrees))}\N{DEGREE SIGN} {np.round(abs(minutes * 60), 3)}'{hemis}"
except TypeError:
output = None
return output
def format_ewo(ewo):
if ewo == 'None/None/None':
ewo = None
return ewo
def format_float(value):
# round float values to 2 decimal places
try:
value = np.round(value, 2)
except (ValueError, TypeError):
value = value
return value
def format_int(value):
try:
value = int(value)
except (ValueError, TypeError):
value = value
return value
def format_ts_epoch(timestamp):
return dt.datetime.fromtimestamp(timestamp, dt.UTC).strftime('%Y-%m-%d %H:%M')
def main(args):
loglevel = args.loglevel.upper() # TODO do something with logging?
deployment = args.deployment
kml_type = args.kml_type
templatedir = args.templatedir
configdir = args.configdir
savedir = args.savedir
sensor_list = ['m_battery', 'm_vacuum', 'm_water_vx', 'm_water_vy', 'm_gps_mag_var']
sensor_thresholds_yml = os.path.join(configdir, 'sensor_thresholds.yml')
with open(sensor_thresholds_yml) as f:
sensor_thresholds = yaml.safe_load(f)
glider_tails = 'https://rucool.marine.rutgers.edu/gliders/glider_tails/'
# load the templates
environment = Environment(loader=FileSystemLoader(templatedir))
template = environment.get_template('kml_template.kml')
format_template = environment.get_template('format_active_deployments_macro.kml')
deployment_template = environment.get_template('deployment_macro.kml')
track_template = environment.get_template('track_macro.kml')
surfacing_template = environment.get_template('surface_event_macro.kml')
text_box_template = environment.get_template('text_box_macro.kml')
# define filename
if kml_type == 'deployed':
ext = ''
else:
ext = f'_{kml_type.split("deployed_")[-1]}'
ts_now = dt.datetime.now(dt.UTC).strftime('%m/%d/%y %H:%M')
glider_api = 'https://marine.rutgers.edu/cool/data/gliders/api/'
if deployment == 'active': # 'deployed' 'deployed_ts' 'deployed_uv' 'deployed_uv_ts'
savefile = os.path.join(savedir, f'active_deployments{ext}.kml')
document_name = 'Active Deployments'
# load the active_deployments configuration file (generated with setup_active_deployments_yaml.py)
active_deployments_config = os.path.join(configdir, 'active_deployments.yml')
with open(active_deployments_config) as f:
ad_config = yaml.safe_load(f)
# make sure the config file is up-to-date
active_deployments_api = requests.get(f'{glider_api}deployments/?active').json()['data']
glider_deployments_api = []
for ad in active_deployments_api:
glider_deployments_api.append(ad['deployment_name'])
difference = list(set(glider_deployments_api).symmetric_difference(set(list(ad_config['deployments'].keys()))))
if len(difference) > 0:
raise ValueError('active_deployments.yml is not up-to-date')
glider_deployments = ad_config['deployments']
else:
glider_regex = re.compile(r'^(.*)-(\d{8}T\d{4})')
match = glider_regex.search(deployment)
if match:
try:
(glider, trajectory) = match.groups()
try:
trajectory_dt = parser.parse(trajectory).replace(tzinfo=pytz.UTC)
except ValueError as e:
# logger.error('Error parsing trajectory date {:s}: {:}'.format(trajectory, e))
print('need to enable logging')
except ValueError as e:
# logger.error('Error parsing invalid deployment name {:s}: {:}'.format(deployment, e))
print('need to enable logging')
else:
# logger.error('Cannot pull glider name from {:}'.format(deployment))
print('need to enable logging')
## exit the script????
savedir = os.path.join(savedir, str(trajectory_dt.year), deployment, 'kmz')
os.makedirs(savedir, exist_ok=True)
savefile = os.path.join(savedir, f'{deployment}{ext}.kml')
document_name = 'Glider Deployments'
glider_deployments = {deployment: 'ff43d0e9'} # track color = yellow
format_dict = dict()
deployment_dict = dict()
for gd, track_color in glider_deployments.items():
deployment_api = requests.get(f'{glider_api}deployments/?deployment={gd}').json()['data'][0]
# build the dictionary for the formatting section of the kml
glider_name = deployment_api['glider_name']
deployment_name = deployment_api['deployment_name']
glider_tail = os.path.join(glider_tails, f'{glider_name}.png')
format_dict[deployment_name] = dict(
name=glider_name,
glider_tail=glider_tail,
deployment_color=track_color
)
# get distance flown and calculate days deployed
distance_flown_km = deployment_api['distance_flown_km']
try:
end = dt.datetime.fromtimestamp(deployment_api['end_date_epoch'], dt.UTC)
except TypeError:
end = dt.datetime.now(dt.UTC)
start = dt.datetime.fromtimestamp(deployment_api['start_date_epoch'], dt.UTC)
seconds_deployed = ((end - start).days * 86400) + (end - start).seconds
days_deployed = format_float(seconds_deployed / 86400)
# grab the data from the surface sensors and store in a dictionary (so you only have to hit the API once
# per sensor per deployment)
sensor_data = dict()
for sensor in sensor_list:
sensor_api = requests.get(f'{glider_api}sensors/?deployment={deployment_name}&sensor={sensor}').json()['data']
if len(sensor_api) > 0:
sensor_df = pd.DataFrame(sensor_api)
sensor_df.sort_values(by='epoch_seconds', inplace=True, ignore_index=True)
sensor_df['ts'] = pd.to_datetime(sensor_df['ts'])
if sensor == 'm_gps_mag_var':
sensor_df['value'] = sensor_df['value'] * 180 / np.pi
sensor_df['units'] = 'degrees'
sensor_data[sensor] = sensor_df
# add m_gps_mag_var proxy if not being sent back
if 'm_water_vx' in sensor_data.keys() and 'm_water_vy' in sensor_data.keys() and 'm_gps_mag_var' not in sensor_data.keys():
calculator = MagneticFieldCalculator(model='igrf')
sensor_df = sensor_data['m_water_vx'][['ts', 'epoch_seconds', 'lat', 'lon']].copy()
sensor_df.insert(0, 'sensor', 'calculated_declination')
sensor_df.insert(1, 'units', 'degrees')
sensor_df.insert(2, 'value', np.nan)
sensor_df['date'] = sensor_df['ts'].dt.date
for d in np.unique(sensor_df['date']):
di = sensor_df['date'] == d
result = calculator.calculate(latitude=np.nanmedian(sensor_df['lat'][di]),
longitude=np.nanmedian(sensor_df['lon'][di]),
altitude=0,
date=d)
sensor_df.loc[di, 'value'] = -result['field-value']['declination']['value'] # units = degrees
sensor_data['m_gps_mag_var'] = sensor_df
# track information
# gather track timestamp and location from the API
track_dict = dict(
gps_epoch=np.array([], dtype='int'),
lon=np.array([], dtype='float'),
lat=np.array([], dtype='float'),
sid=np.array([], dtype='int')
)
track_features = requests.get(f'{glider_api}tracks/?deployment={deployment_name}').json()['features']
for tf in track_features:
if tf['geometry']['type'] == 'Point' and 'waypoint' not in tf['properties'].keys():
try:
track_dict['gps_epoch'] = np.append(track_dict['gps_epoch'], tf['properties']['gps_epoch'])
track_dict['lon'] = np.append(track_dict['lon'], tf['geometry']['coordinates'][0])
track_dict['lat'] = np.append(track_dict['lat'], tf['geometry']['coordinates'][1])
track_dict['sid'] = np.append(track_dict['sid'], tf['properties']['sid'])
except KeyError:
continue # these cases can be waypoints instead of points on the actual glider track
# # add the last surfacing to the dictionary - 2024-11-07 track API endpoint is fixed, last surfacing is now
# # in that API, don't need to add it anymore from the deployment API
ls_api = deployment_api['last_surfacing']
# track_dict['gps_epoch'] = np.append(track_dict['gps_epoch'], ls_api['connect_time_epoch'])
# track_dict['lon'] = np.append(track_dict['lon'], ls_api['gps_lon_degrees'])
# track_dict['lat'] = np.append(track_dict['lat'], ls_api['gps_lat_degrees'])
# track_dict['sid'] = np.append(track_dict['sid'], ls_api['surfacing_id'])
# convert to dataframe to sort by time
track_df = pd.DataFrame(track_dict)
track_df.sort_values(by='gps_epoch', inplace=True, ignore_index=True)
if kml_type in ['deployed', 'deployed_uv']:
track_df = track_df.copy()[['lon', 'lat']]
track_df['height'] = 4.999999999999999
track_values = track_df.values.tolist()
kml = simplekml.Kml()
track_data = kml.newlinestring(name="track")
for values in track_values:
track_data.coords.addcoordinates([(values[0], values[1], values[2])])
elif kml_type in ['deployed_ts', 'deployed_uv_ts']:
# build the dictionary that contains the track information to input into the kml template
track_data = dict()
for track_idx, row in track_df.iterrows():
if track_idx > 0:
prev_row = track_df.iloc[track_idx - 1]
start = dt.datetime.fromtimestamp(prev_row.gps_epoch, dt.UTC).strftime('%Y-%m-%dT%H:%M:%SZ')
end = dt.datetime.fromtimestamp(row.gps_epoch, dt.UTC).strftime('%Y-%m-%dT%H:%M:%SZ')
track_data[track_idx] = dict(
start=start,
end=end,
start_lon=prev_row.lon,
start_lat=prev_row.lat,
end_lon=row.lon,
end_lat=row.lat
)
# surface events
surface_events = requests.get(f'{glider_api}surfacings/?deployment={deployment_name}').json()['data']
surface_events_df = pd.DataFrame(surface_events)
surface_events_df.sort_values(by='connect_time_epoch', inplace=True, ignore_index=True)
surface_events_df = surface_events_df.replace({np.nan: None})
# calculate previous 24 hours
t24h = pd.to_datetime(ts_now) - pd.Timedelta(hours=24)
surface_events_dict = dict()
currents_dict = dict()
total_iridium_seconds = 0
# build the information for the surfacings and depth-averaged currents
for idx, row in surface_events_df.iterrows():
se = row.to_dict()
total_iridium_seconds = total_iridium_seconds + se['call_length_seconds']
surface_event_popup = build_popup_dict(se)
# define surfacing grouping (e.g. last 24 hours or day)
se_ts = pd.to_datetime(surface_event_popup['connect_ts'])
if se_ts >= t24h:
folder_name = 'Last 24 Hours'
style_name = 'RecentSurfacing'
else:
folder_name = se_ts.strftime('%Y-%m-%d')
style_name = 'Surfacing'
# define folder name for depth-average currents
currents_folder_name = se_ts.strftime('%Y-%m-%d')
connect_datetime = dt.datetime.fromtimestamp(se['connect_time_epoch'], dt.UTC)
# add the folder name to the surface events dictionary if it's not already there
try:
surface_events_dict[folder_name]
except KeyError:
surface_events_dict[folder_name] = dict()
# add the folder name to the currents dictionary if it's not already there
try:
currents_dict[currents_folder_name]
except KeyError:
currents_dict[currents_folder_name] = dict()
# build dictionary for depth-averaged currents
currents_dict[currents_folder_name][idx] = dict(
connect_HHMM=connect_datetime.strftime('%H:%M'),
connect_ts_Z=connect_datetime.strftime('%Y-%m-%dT%H:%M:%SZ'),
connect_ts=surface_event_popup['connect_ts'],
disconnect_ts=surface_event_popup['disconnect_ts'],
lon_degrees_start=se['gps_lon_degrees'],
lat_degrees_start=se['gps_lat_degrees']
)
if 'm_water_vx' in sensor_data.keys() and 'm_water_vy' in sensor_data.keys():
# calculate depth-averaged currents
# find m_water_vx, m_water_vy and m_gps_mag_var for this surfacing
for sensor in ['m_water_vx', 'm_water_vy', 'm_gps_mag_var']:
add_sensor_values(currents_dict[currents_folder_name][idx], sensor, sensor_data[sensor])
# rotate from magnetic plane to true plane and calculate current speed and angle
surfacing_m_water_vx = currents_dict[currents_folder_name][idx]['m_water_vx']
surfacing_m_water_vy = currents_dict[currents_folder_name][idx]['m_water_vy']
surfacing_m_gps_mag_var = currents_dict[currents_folder_name][idx]['m_gps_mag_var']
vx_test = isinstance(surfacing_m_water_vx, float)
vy_test = isinstance(surfacing_m_water_vy, float)
mag_test = isinstance(surfacing_m_gps_mag_var, float)
if np.logical_and(np.logical_and(vx_test, vy_test), mag_test):
# units: current_bearing = degrees, current_speed = m/s
current_bearing, current_speed = uv2spdir(surfacing_m_water_vx, surfacing_m_water_vy,
mag=-surfacing_m_gps_mag_var)
elif np.logical_and(vx_test, vy_test):
# calculate m_gps_mag_var if it's not there
calculator = MagneticFieldCalculator(model='igrf')
result = calculator.calculate(latitude=se['gps_lat_degrees'],
longitude=se['gps_lon_degrees'],
altitude=0,
date=currents_folder_name)
mag = -result['field-value']['declination']['value']
current_bearing, current_speed = uv2spdir(surfacing_m_water_vx, surfacing_m_water_vy, mag=-mag)
else:
current_bearing = None
current_speed = None
# calculate where the glider will be in 1 day floating at surface
try:
distance_1day_km = current_speed * 86400 / 1000
dest_1day = geopy.distance.distance(kilometers=distance_1day_km).destination((se['gps_lat_degrees'], se['gps_lon_degrees']), bearing=current_bearing)
currents_dict[currents_folder_name][idx]['lon_degrees_end'] = dest_1day.longitude
currents_dict[currents_folder_name][idx]['lat_degrees_end'] = dest_1day.latitude
except TypeError:
# if end lat/lon can't be calculated, remove this record from the currents dictionary
del currents_dict[currents_folder_name][idx]
else:
current_bearing = None
current_speed = None
surface_events_dict[folder_name][idx] = dict(
connect_ts=surface_event_popup['connect_ts'],
connect_ts_Z=connect_datetime.strftime('%Y-%m-%dT%H:%M:%SZ'),
gps_lat_degrees=se['gps_lat_degrees'],
gps_lon_degrees=se['gps_lon_degrees'],
style_name=style_name,
surface_event_popup=surface_event_popup
)
# add values for battery and vacuum to the surface event popup
for sensor in ['m_battery', 'm_vacuum']:
try:
add_sensor_values(surface_events_dict[folder_name][idx]['surface_event_popup'],
sensor, sensor_data[sensor], thresholds=sensor_thresholds)
except KeyError:
continue
if idx == 0: # deployment location
surface_events_dict[folder_name][idx]['surface_event_popup']['current_speed'] = format_float(current_speed) # m/s
surface_events_dict[folder_name][idx]['surface_event_popup']['current_speed_bearing'] = format_int(current_bearing)
deployment_popup_dict = surface_events_dict[folder_name][idx]['surface_event_popup']
deployment_ts_Z = dt.datetime.fromtimestamp(se['connect_time_epoch'], dt.UTC).strftime(
'%Y-%m-%dT%H:%M:%SZ')
deployment_gps_lat_degrees = se['gps_lat_degrees']
deployment_gps_lon_degrees = se['gps_lon_degrees']
else:
# calculate dive/speed from previous surfacing info
prev_row = surface_events_df.iloc[idx - 1].to_dict()
# calculate time elapsed and distance since last surfacing
time_elapsed_seconds = se['connect_time_epoch'] - prev_row['disconnect_time_epoch']
time_elapsed_minutes = format_float(time_elapsed_seconds / 60)
distance_travelled = format_float(geopy.distance.geodesic((prev_row['gps_lat_degrees'], prev_row['gps_lon_degrees']),
(se['gps_lat_degrees'], se['gps_lon_degrees'])).km)
# calculate total speed and bearing
total_speed = distance_travelled * 1000 / time_elapsed_seconds
total_bearing = calculate_compass_bearing((prev_row['gps_lat_degrees'], prev_row['gps_lon_degrees']),
(se['gps_lat_degrees'], se['gps_lon_degrees']))
# calculate glide speed and bearing
try:
cu, cv = spdir2uv(current_speed, current_bearing, deg=True)
tu, tv = spdir2uv(total_speed, total_bearing, deg=True)
glide_bearing, glide_speed = uv2spdir(tu - cu, tv - cv)
except TypeError:
glide_bearing = None
glide_speed = None
# add dive and current information to the surfacing event (time, distance, speed)
surface_events_dict[folder_name][idx]['surface_event_popup']['time_elapsed'] = time_elapsed_minutes # minutes
surface_events_dict[folder_name][idx]['surface_event_popup']['dist'] = distance_travelled # km
surface_events_dict[folder_name][idx]['surface_event_popup']['total_speed'] = format_float(total_speed) # m/s
surface_events_dict[folder_name][idx]['surface_event_popup']['total_speed_bearing'] = format_int(total_bearing)
surface_events_dict[folder_name][idx]['surface_event_popup']['current_speed'] = format_float(current_speed) # m/s
surface_events_dict[folder_name][idx]['surface_event_popup']['current_speed_bearing'] = format_int(current_bearing)
surface_events_dict[folder_name][idx]['surface_event_popup']['glide_speed'] = format_float(glide_speed) # m/s
surface_events_dict[folder_name][idx]['surface_event_popup']['glide_speed_bearing'] = format_int(glide_bearing)
if idx == len(surface_events_df) - 1: # last surfacing
last_surfacing_popup_dict = surface_events_dict[folder_name][idx]['surface_event_popup']
ls_gps_lat_degrees = se['gps_lat_degrees']
ls_gps_lon_degrees = se['gps_lon_degrees']
deployment_dict[deployment_name] = dict(
ts_now=ts_now,
glider_name=glider_name,
glider_tail=glider_tail,
ls_connect_ts=last_surfacing_popup_dict['connect_ts'],
deploy_ts_Z=deployment_ts_Z,
ls_gps_lat_degrees=ls_gps_lat_degrees,
ls_gps_lon_degrees=ls_gps_lon_degrees,
last_surfacing_popup=last_surfacing_popup_dict,
deploy_connect_ts=deployment_popup_dict['connect_ts'],
deploy_gps_lat_degrees=deployment_gps_lat_degrees,
deploy_gps_lon_degrees=deployment_gps_lon_degrees,
deployment_popup=deployment_popup_dict,
cwpt_since=last_surfacing_popup_dict['disconnect_ts'],
cwpt_lat=ls_api['waypoint_lat'],
cwpt_lon=ls_api['waypoint_lon'],
cwpt_lat_degrees=convert_coordinates(ls_api['waypoint_lat']),
cwpt_lon_degrees=convert_coordinates(ls_api['waypoint_lon']),
distance_flown_km=distance_flown_km,
days_deployed=days_deployed,
iridium_mins=format_int(np.round(total_iridium_seconds / 60)),
track_info=track_data,
surface_event_info=surface_events_dict,
currents_info=currents_dict
)
# render all of the information into the kml template
content = template.render(
document_name=document_name,
kml_type=kml_type,
format_info=format_dict,
deployment_info=deployment_dict
)
with open(savefile, mode="w", encoding="utf-8") as message:
message.write(content)
kmz_filename = convert_kml_to_kmz(savefile, kmz_file_path=None)
print(f'kmz file location: {kmz_filename}')
# remove the kml file if the kmz file was written
if os.path.isfile(kmz_filename):
os.remove(savefile)
if __name__ == '__main__':
arg_parser = argparse.ArgumentParser(description=main.__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
arg_parser.add_argument('deployment',
type=str,
help='Use "active" to generate one kml file for all active deployments, or provide '
'one glider deployment name formatted as glider-YYYYmmddTHHMM')
arg_parser.add_argument('-kml_type',
type=str,
help='Type to generate. "deployed": glider track with surfacings, "deployed_ts": same as'
'deployed with the option to filter by time, "deployed_uv": same as deployed with '
'vectors of depth-averaged currents, and "deployed_uv_ts": same as deployed_ts with'
'vectors of depth-averaged currents.',
choices=['deployed', 'deployed_ts', 'deployed_uv', 'deployed_uv_ts'],
default='deployed')
arg_parser.add_argument('-t', '--templatedir',
type=str,
help='Directory containing template .kmls')
arg_parser.add_argument('-c', '--configdir',
type=str,
help='Directory containing required configuration files: active_deployments.yml and sensor_thresholds.yml')
arg_parser.add_argument('-s', '--savedir',
type=str,
help='Save directory')
arg_parser.add_argument('-l', '--loglevel',
help='Verbosity level',
type=str,
choices=['debug', 'info', 'warning', 'error', 'critical'],
default='info')
parsed_args = arg_parser.parse_args()
sys.exit(main(parsed_args))