-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfit2subs.py
executable file
·1356 lines (1108 loc) · 61.3 KB
/
fit2subs.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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# Copyright (c) 2018 Wojciech Wieckowski
#
# This work is licensed under the terms of the MIT license.
# For a copy, see <https://opensource.org/licenses/MIT>.
# description :Subsurface log importer for Garmin FIT files
# author-email :[email protected]
__version__ = '0.13'
import hashlib
import datetime
import sys
import os
import glob
import argparse
import math
from fitparse import FitFile
from fitparse.utils import FitParseError
try:
from lxml import etree as ETree # Prefered as it keeps attrib order as is
except ImportError:
import xml.etree.ElementTree as ETree # Version from standard lib sorts attribs alphabetically
# Constants for default settings
NEW_SITE_DISTANCE_MIN = 500 # ('big circle') Min distance [m] to create new site
OLD_COORDS_DISTANCE_MAX = 50 # ('small circle') Max distance [m] to attach dive to existing coords and site UUID
def hashit(in_list):
"""Calculates 4 HEX digits hash string from input list"""
m = hashlib.sha1()
for item in in_list:
m.update(item.encode('utf-8'))
digest = m.hexdigest()[:8]
return digest
def errprint(*args, **kwargs):
"""Prints to stderr"""
print(*args, file=sys.stderr, **kwargs)
def npprint(*args, **kwargs):
"""Prints to stdout conditionally"""
if not settings.pipe_output: # Supress printing when pipe output selected
print(*args, file=sys.stdout, **kwargs)
def pretty_print(elem, level=0):
"""In-place prettyprint etree XML formatter"""
i = "\n" + level*" "
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + " "
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
pretty_print(elem, level+1)
if not elem.tail or not elem.tail.strip():
elem.tail = i
else:
if level and (not elem.tail or not elem.tail.strip()):
elem.tail = i
def haversine_distance(pos_from, pos_to):
"""Calculates circular meters distance between two GPS positions"""
lat_from, long_from = (float(pos_from.split(' ')[0]), float(pos_from.split(' ')[1])) # Pair in string to two floats
lat_to, long_to = (float(pos_to.split(' ')[0]), float(pos_to.split(' ')[1])) # Pair in string to two floats
e_radius = 6371000 # Earth radius in meters
delta_lat = math.radians(lat_to - lat_from)
delta_long = math.radians(long_to - long_from)
a = math.sin(delta_lat / 2) * math.sin(delta_lat / 2) + math.cos(math.radians(lat_from)) \
* math.cos(math.radians(lat_to)) * math.sin(delta_long / 2) * math.sin(delta_long / 2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
distance = int(e_radius * c)
return distance # Meters
def timestamps_diff(timestamp_from, timestamp_to):
"""Difference between two timestamp strings
returns delta in Subsurface 'MMM:SS min' format
"""
delta = int(timestamp_to) - int(timestamp_from)
return '%d:%02d min' % (delta / 60, delta % 60)
def date_time_loc(timestamp_utc, offset_secs):
"""Adds offset seconds to timestamp string to set local time
returns tuple of 'date' and 'time' strings
"""
utc_reference = 631065600 # timestamp for UTC 00:00 Dec 31 1989
# Compute the two's complement of int time_offset value
bits = 32
if (int(offset_secs) & (1 << (bits - 1))) != 0: # If sign bit is set
offset_secs = int(offset_secs) - (1 << bits) # Compute negative value
datetime_loc = datetime.datetime.utcfromtimestamp(utc_reference + int(timestamp_utc) + int(offset_secs))
date_loc = datetime.datetime.strftime(datetime_loc, '%Y-%m-%d') # Date format used in Subsurface
time_loc = datetime.datetime.strftime(datetime_loc, '%H:%M:%S') # Time format used in Subsurface
return date_loc, time_loc
def sc_to_degs(semicircles):
"""Semicirceles (Garmin GPS coordinates storage standard)
to decimal degrees conversion [float].
"""
if semicircles is None:
return None
# Seems that Subsurface uses 6 decimal digits after dot
degs = '%.6f' % round(int(semicircles) * (180 / 2 ** 31), 6)
return degs
def dd_to_dms(lat, long):
"""Decimal to DMS coordiantes convertion"""
# Longtitude
split_degx = math.modf(float(long))
degrees_x = int(split_degx[1])
minutes_x = abs(int(math.modf(split_degx[0] * 60)[1]))
seconds_x = abs(round(math.modf(split_degx[0] * 60)[0] * 60, 3))
# Latitude
split_degy = math.modf(float(lat))
degrees_y = int(split_degy[1])
minutes_y = abs(int(math.modf(split_degy[0] * 60)[1]))
seconds_y = abs(round(math.modf(split_degy[0] * 60)[0] * 60, 3))
if degrees_x < 0:
e_or_w = "W"
else:
e_or_w = "E"
if degrees_y < 0:
n_or_s = "S"
else:
n_or_s = "N"
long_dms = '%d\u00b0%02d\'%06.3f"%s' % (abs(degrees_x), minutes_x, seconds_x, e_or_w)
lat_dms = '%d\u00b0%02d\'%06.3f"%s' % (abs(degrees_y), minutes_y, seconds_y, n_or_s)
return '{} {}'.format(lat_dms, long_dms)
def fit_dump(fit_file, dump_file):
"""Dumps all data from all records to text file"""
# Fit file opening
try:
fitparser = FitFile(fit_file)
except Exception as err: # Catches fit file parsing errors
print("parse error: {}".format(err))
return False
df_handle = open(dump_file, 'w')
msg_unique_list = []
msg_unknown_list = []
msg_known_dict = {}
messages = fitparser.get_messages() # Fitparse messages iterator object
# Go through all the data entries
for record in messages:
if record.mesg_num not in msg_unique_list:
msg_unique_list.append(record.mesg_num)
if 'unknown' in record.name.lower():
if record.mesg_num not in msg_unknown_list:
msg_unknown_list.append(record.mesg_num)
else:
if record.mesg_num not in msg_known_dict:
msg_known_dict[record.mesg_num] = record.name
df_handle.write('name: {}\n'.format(record.name))
df_handle.write('mesg_num: {}\n'.format(record.mesg_num))
for record_data in record:
df_handle.write(' {}\n'.format(record_data))
df_handle.write('==========================\n')
msg_unique_list.sort()
msg_unknown_list.sort()
df_handle.write('\n======== SUMMARY =========\n')
df_handle.write('\nUnique records: {}\n'.format(msg_unique_list))
df_handle.write('\nUnknown records: {}\n'.format(msg_unknown_list))
df_handle.write('\nKnown records: {}\n'.format(msg_known_dict))
df_handle.close()
return True
def fit_summary(fit_file):
"""Returns FIT file summary in tuple of boolean and dictionary.
First field contains success status."""
content = {'file': os.path.basename(fit_file), 'time': '', 'sport': '', 'elapsed': '', 'depth': ''}
# Fit file opening
try:
fitparser = FitFile(fit_file)
except FitParseError: # Catches fit file parsing errors
content['sport'] = 'parse_error'
return False, content
messages = fitparser.get_messages() # Fitparse messages iterator object
time_offset = 0
for record in messages:
if record.name == 'device_settings':
val_dict = record.get_values()
time_offset = int(val_dict['time_offset'])
if record.name == 'session':
val_dict = record.get_values()
try:
sport = str(val_dict['sport'])
if sport in ('53', '54', '55', '56', '57'):
content['sport'] = str(val_dict['sub_sport']) # Use 'sub_sport' field for diving
else: # Or just sport for other activities
content['sport'] = str(val_dict['sport'])
except KeyError:
pass
try:
content['elapsed'] = str(val_dict['total_elapsed_time'])
except (KeyError, TypeError, ValueError):
pass
try:
# Decision to use dict values above caused stupid calculations below...
epoch = datetime.datetime(1989, 12, 31) # Garmin epoch
utc_delta = val_dict['start_time'] - epoch
time_stamp = int(utc_delta / datetime.timedelta(seconds=1))
loc_time = date_time_loc(time_stamp, time_offset)
content['time'] = '{} {}'.format(loc_time[0], loc_time[1])
except (KeyError, TypeError, ValueError):
pass
if record.name == 'dive_summary':
val_dict = record.get_values()
if val_dict['reference_mesg'] == 'session':
try:
content['depth'] = '%.2f' % float(val_dict['max_depth'])
except (KeyError, TypeError, ValueError):
pass
return True, content
class RecordDecoder(object):
"""Creates dictionary of record fields objects
and gives access to fields with possible conversion to Subsurface xml metric standard
"""
def __init__(self):
self._fields_dic = {}
@staticmethod
def _units_conv(item):
"""Values convertion to Subsurface metric xml standard"""
if item.value is None:
return None
# Seems that FIT files use metric units only - imperial converters not needed
if item.units == 's': # [s] to min
rd = round(float(item.value))
strval = '%d:%02d min' % (rd / 60, rd % 60)
elif item.units == 'm': # Meters rounded to 0.01
strval = '%.2f m' % round(float(item.value), 2)
elif item.units == 'C': # Celcius rounded to 0.1
strval = '%.1f C' % round(float(item.value), 1)
elif item.units == 'percent': # percent to %
strval = '%d%%' % int(item.value)
elif item.units == 'OTUs': # Nothing to do - used only in extra info
strval = '%d OTUs' % int(item.value)
elif item.units == 'kg/m^3': # No calculations needed - units equal
strval = '%.1f g/l' % float(item.value)
else: # Other source units to raw item value without units suffix
strval = str(item.value)
return strval
def load_record(self, record):
"""Fills dictionary with item objects"""
self._fields_dic = {}
for item in record:
self._fields_dic[item.name] = item
def field(self, field, noconv=False, raw=False):
"""Access to specific field
if noconv=True - strips [units] but leaves field unconverted
if raw=True - returns raw field, not converted by fitparse library parsers
"""
try:
if self._fields_dic[field].value is None:
return None
if noconv:
return str(self._fields_dic[field].value)
elif raw:
return str(self._fields_dic[field].raw_value)
else:
return self._units_conv(self._fields_dic[field])
except KeyError:
return None
class DiveLog(object):
"""Log processing class"""
def __init__(self):
# Private vars
if settings.in_subslog is not None: # Detects if source Subsurface log is used
self._log_file = settings.in_subslog
self._tree = ETree.parse(self._log_file)
else: # No source log - let's create empty XML structure
self._log_file = None
self._tree = ETree.ElementTree(ETree.fromstring('<divelog program="subsurface" version="3">'
'<settings/><divesites/><dives/></divelog>'))
self._root = self._tree.getroot()
self._current_dive_element = None
self._current_computer_element = None
self._sample_template = {'time': None, 'depth': None, 'temp': None, 'ndl': None, 'tts': None, 'in_deco': None,
'stoptime': None, 'stopdepth': None, 'cns': None, 'heartbeat': None}
self._dc_data_template = {'model': None, 'deviceid': None, 'serial': None, 'firmware': None}
self._dive_data_template = {'number': None, 'divesiteid': None, 'date': None, 'time': None, 'duration': None}
self.travel_gas_index = 0
self._sample_cache = self._sample_template.copy()
# Public vars
self.time_offset = None # Offset used to calculate dive start time
self.dive_detected = False # Dive activity found and started (flag)
self.processed_count = 0
self.activity_start_timestamp = None # Reference time for time offset calculations
self.lowest_temp = None # Used to store found lowest temp values during records processing
self.firstrecord = True # First sample flag - for calculating initial air pressure
self.dc_data = self._dc_data_template.copy() # Computer data
self.dive_data = self._dive_data_template.copy()
self.sample_data = self._sample_template.copy()
@property
def last_dive_no(self):
"""Finds highest dive # in Subsurface log"""
dive_elements = self._root.findall('./dives/dive')
try:
# List comprehension used to find highest dive number already present in log
count = max([int(number) for number in [dive.attrib['number'] for dive in dive_elements]])
return count
except (ValueError, KeyError):
return 0
def reset_all_vars(self):
"""Resets some vars to default values.
To be called before importing new file (just in case)
"""
self._current_dive_element = None
self._current_computer_element = None
self.time_offset = None
self.dive_detected = False
self.activity_start_timestamp = None
self.lowest_temp = None
self.firstrecord = True
self.dc_data = self._dc_data_template.copy()
self.dive_data = self._dive_data_template.copy()
self.sample_data = self._sample_template.copy()
self._sample_cache = self._sample_template.copy()
def reset_sample_vars(self):
"""Resets vars before storing single sample record.
To be called before importing new record (just in case)
"""
self.sample_data = self._sample_template.copy()
def save_log(self, file):
"""Saves modified Subsurface log"""
pretty_print(self._root) # Re-indents entire XML tree
self._tree.write(file)
def pipe_log(self):
"""Prints log to stdout"""
pretty_print(self._root)
print(ETree.tostring(self._root, encoding='unicode'))
# -----------------------------
# XML log manipulation methods
# -----------------------------
def update_dc(self):
"""Creates new computer entry or updates FW version"""
dc_element = self._root.find('./settings/divecomputerid[@deviceid="{}"]'.format(self.dc_data['deviceid']))
if dc_element is None: # Computer absent
settings_element = self._root.find('./settings')
new_dc = ETree.SubElement(settings_element, 'divecomputerid')
for attr in self.dc_data:
if self.dc_data[attr] is not None:
new_dc.set(attr, self.dc_data[attr])
def update_site(self, lat, long):
"""Creates or updates dive sites"""
# Constants used to decide if to create new site or attach to existing one
new_site_distance_min = settings.big_circle # Min distance [m] to create new site
old_coords_distance_max = settings.small_circle # Max distance [m] to attach dive to existing coords and site UUID
# When calculated distance value is between NEW_SITE_DISTANCE_MIN and OLD_COORDS_DISTANCE_MAX
# new site element with existing description will be created
sites_element = self._root.find('./divesites')
if lat is not None and long is not None:
current_coord = lat + ' ' + long # Both coordinates in Subsurface 'gps' attrib format
found_near_site_element = None
found_near_site_distance = new_site_distance_min + 1 # Set initial value higher than minimum
for site_element in sites_element.findall('site'): # Go thorough all stored sites
stored_coord = site_element.get('gps')
if stored_coord is not None:
distance = haversine_distance(current_coord, stored_coord) # Calculate circular distance [m]
if distance <= new_site_distance_min and distance <= found_near_site_distance: # Found site near enough
found_near_site_element = site_element
found_near_site_distance = distance
else: # No coordinates found - need to supplement some missing values
found_near_site_element = None
current_coord = None
found_near_site_distance = 0
if found_near_site_element is not None: # If near site found
if found_near_site_distance <= old_coords_distance_max: # Found site is close enough to use old coords
self.dive_data['divesiteid'] = found_near_site_element.get('uuid')
else: # Create new site element with old name
new_site = ETree.SubElement(sites_element, 'site')
new_site.set('gps', current_coord)
sitename = found_near_site_element.get('name')
uuid = hashit([self.activity_start_timestamp, sitename])
new_site.set('name', sitename) # Use old name
new_site.set('uuid', uuid)
self.dive_data['divesiteid'] = uuid
else: # Near site not found - let's create completely new
new_site = ETree.SubElement(sites_element, 'site')
if current_coord is not None:
new_site.set('gps', current_coord)
sitename = '= first time here {} {} ='.format(self.dive_data['date'],
self.dive_data['time'])
uuid = hashit([self.activity_start_timestamp, sitename])
new_site.set('name', sitename) # Create fake name
new_site.set('uuid', uuid)
self.dive_data['divesiteid'] = uuid
def start_dive(self):
"""Creates 'dive' and 'divecomputer' elements"""
diveid = hashit([self.activity_start_timestamp]) # Unique ID from timestamp
dives_element = self._root.find('./dives')
# Check if dive isn't already present in log
if dives_element.find('./dive/divecomputer[@diveid="{}"]'.format(diveid)) is not None:
npprint('Dive "{}" already imported, nothing to do!'.format(diveid))
return False # Dive already in log
self._current_dive_element = ETree.SubElement(dives_element, 'dive')
self._current_computer_element = ETree.SubElement(self._current_dive_element, 'divecomputer')
self._current_computer_element.set('model', self.dc_data['model'])
self._current_computer_element.set('deviceid', self.dc_data['deviceid'])
self._current_computer_element.set('diveid', diveid)
return True
def set_subsport(self, sub_sport):
"""Sets 'Freedive' attrib"""
if sub_sport in ('apnea_diving', 'apnea_hunting'): # Subsurface does not set this attrib for OC
self._current_computer_element.set('dctype', 'Freedive')
def set_travel_gas(self):
"""Sets travel gas index"""
self.add_gas_change(self.travel_gas_index, self.activity_start_timestamp)
def add_summary_temp(self, air):
"""Adds water/air temperature element
hack used - missing reliable air temperature data in diving activity
Garmin Connect uses internet weather service
"""
if air is not None:
new_temp = ETree.SubElement(self._current_computer_element, 'temperature')
new_temp.set('water', str(self.lowest_temp) + ' C')
new_temp.set('air', air)
def add_surface_pressure(self, pressure):
"""Adds surface pressure
hack used - missing reliable air pressure data in diving activity
"""
if pressure is not None:
new_press = ETree.SubElement(self._current_computer_element, 'surface')
new_press.set('pressure', str(pressure) + ' bar')
def add_summary_depths(self, mx, av):
"""Adds depths to dive summary"""
if mx is not None and av is not None:
new_depth = ETree.SubElement(self._current_computer_element, 'depth')
new_depth.set('max', mx)
new_depth.set('mean', av)
def add_notes(self, note):
"""Add note to dive element"""
new_note = self._current_dive_element.find('./notes')
if new_note is None:
new_note = ETree.SubElement(self._current_dive_element, 'notes')
else:
note = new_note.text + '\n' + note
new_note.text = note
def add_cylinder(self, cylinder_no, oxygen_perc, helium_perc):
"""Creates cylinders from MK1 mixes list"""
new_cylinder = ETree.SubElement(self._current_dive_element, 'cylinder')
if int(round(float(oxygen_perc.strip('%')))) >= 99:
description = '%s. Oxygen' % cylinder_no
elif int(round(float(helium_perc.strip('%')))) == 0 and int(round(float(oxygen_perc.strip('%')))) == 21:
description = '%s. Air' % cylinder_no
elif int(round(float(helium_perc.strip('%')))) == 0 and int(round(float(oxygen_perc.strip('%')))) > 21:
description = '%s. EAN%i' % (cylinder_no,
int(round(float(oxygen_perc.strip('%')))))
else:
description = '%s. %i/%i' % (cylinder_no,
int(round(float(oxygen_perc.strip('%')))),
int(round(float(helium_perc.strip('%')))))
new_cylinder.set('description', description)
new_cylinder.set('o2', oxygen_perc)
new_cylinder.set('he', helium_perc)
def add_extra_data(self, key, value):
"""Adds additional info as 'extradata' elements"""
if key is not None and value is not None:
new_extra = ETree.SubElement(self._current_computer_element, 'extradata')
new_extra.set('key', key)
new_extra.set('value', value)
def add_water_data(self, salinity):
"""Adds water density"""
if salinity is not None:
new_water = ETree.SubElement(self._current_computer_element, 'water')
new_water.set('salinity', salinity)
def add_surfacetime(self, time):
"""Creates 'surfacetime' element
...but it seems that Subsurface doesn't care about it
"""
if time is not None:
new_surfacetime = ETree.SubElement(self._current_computer_element, 'surfacetime')
new_surfacetime.text = time
def add_event(self, event_type, timestamp, severity):
"""General events handling"""
if event_type is not None and timestamp is not None:
new_event = ETree.SubElement(self._current_computer_element, 'event')
new_event.set('time', timestamps_diff(self.activity_start_timestamp, timestamp))
new_event.set('type', '26') # From libdivecomputer parser_sample_event_t enum SAMPLE_EVENT_STRING
new_event.set('flags', str(severity << 2)) # From garmin_parser.c
new_event.set('name', event_type)
def add_gas_change(self, cylinder_index, timestamp):
"""Adds gas change events"""
if cylinder_index is not None and timestamp is not None:
new_event = ETree.SubElement(self._current_computer_element, 'event')
new_event.set('time', timestamps_diff(self.activity_start_timestamp, timestamp))
new_event.set('type', '25') # From libdivecomputer parser_sample_event_t enum SAMPLE_EVENT_GASCHANGE2
new_event.set('flags', str(int(cylinder_index) + 1))
new_event.set('name', 'gaschange')
new_event.set('cylinder', cylinder_index)
# new_event.set('o2', '?%')
# new_event.set('he', '?%')
def save_dive(self):
"""Saves dive element data attributes"""
for attr in self.dive_data:
if self.dive_data[attr] is not None:
self._current_dive_element.set(attr, self.dive_data[attr])
def add_sample(self):
"""Adds sample record"""
new_sample = ETree.SubElement(self._current_computer_element, 'sample')
for key in self._sample_template: # Should retain items order from template in python AFAIK >= 3.6
curr_val = self.sample_data.get(key)
prev_val = self._sample_cache.get(key)
if key == 'in_deco': # Relate current flag value to its previous state and required deco time
next_stop_time = self.sample_data.get('stoptime')
if next_stop_time is not None and next_stop_time != '0:00 min': # Entering deco
if prev_val is None or prev_val == '0':
curr_val = '1'
elif prev_val == '1' and next_stop_time == '0:00 min': # Finishing deco
curr_val = '0'
# Skip attribs that should always exist (time, depth) and update cached value if current differs.
# Used to compress Subsurface log by adding only attribs changing previous (cached) state.
if curr_val is not None:
if key in ('time', 'depth') or prev_val != curr_val:
new_sample.set(key, curr_val)
self._sample_cache[key] = curr_val # Update values cache
class Settings(object):
"""Stores processing settings globally and provides access and checking functions"""
def __init__(self):
self.in_subslog = None # Path to original Subsurface log
self.out_subslog = None # Path to destination Subsurface log
self.fit_files = None # Fit files list
self.dump_dir = None # Output dir for dumps
self.big_circle = None # From module constants
self.small_circle = None # From module constants
self.apnea = None # Include apnea dives
self.min_time = None # Minimal dive time
self.list_only = None # List FIT summaries only
self.pipe_output = None # Output everything to stdout for piping
self.no_numbering = None # Numbering of dives optionally disabled
@staticmethod
def _has_access(filepath, mode):
"""Checks file reading, cwriting and creating access"""
if mode == 'r': # Check binary reading access
try:
f = open(filepath, 'rb')
_ = f.read(1) # read 1 byte
f.close()
return True
except IOError:
errprint("File: '{}' cannot be opened for reading".format(filepath))
return False
if mode == 'w': # Check writing access
try:
if os.path.exists(filepath): # Only open for appending and then close if file exists
f = open(filepath, 'a')
f.close()
else:
f = open(filepath, 'w') # Create new and delete immediately
f.close()
os.remove(filepath)
return True
except (IOError, OSError):
errprint("File: '{}' cannot be opened for writing".format(filepath))
return False
def settings_from_args(self, cmdparser):
"""Sets container vars with data from command line parser"""
args = cmdparser.parse_args()
self.fit_files = args.fitfiles
self.in_subslog = args.inlog
self.out_subslog = args.outlog
self.dump_dir = args.dump
self.apnea = args.apnea
self.min_time = args.timelimit
self.list_only = args.listdives
self.pipe_output = args.pipeoutput
self.no_numbering = args.nonumbering
if args.circles is not None:
self.big_circle = args.circles[0]
self.small_circle = args.circles[1]
def _check_params_pattern(self, ok_pattern):
"""Checks parameters combination pattern"""
no_error_found = True
for key, value in ok_pattern.items():
setting = vars(self)[key]
if setting is None:
if value == 'set':
no_error_found = False
elif setting is True:
if value == 'unset':
no_error_found = False
elif setting is False:
if value == 'set':
no_error_found = False
elif setting is not None:
if value == 'unset':
no_error_found = False
return no_error_found
def check_settings(self):
"""Checks processing options"""
# Check if wildcards have been expanded by shell (e.g. cmd is too dumb to do it)
wildcards = '*?[]{}'
temp_list = []
for fit_file in settings.fit_files:
# Check if wildcards are present in current item
if any(w in fit_file for w in wildcards): # Wildcards persisted - let's expand them internally
exp_list = glob.glob(fit_file)
if exp_list:
exp_list = sorted(exp_list) # Hopefully filename contains structured date - provide proper import order
temp_list.extend(exp_list)
else: # In spite of all keep item in list to be eventually reported by checkers from below
temp_list.append(fit_file)
else: # Add file without wildcards as is
temp_list.append(fit_file)
self.fit_files = temp_list
# Parameters consistency check
if self.dump_dir is not None: # Combination for dumping
ok_pattern = {'in_subslog': 'unset', 'out_subslog': 'unset', 'big_circle': 'unset',
'small_circle': 'unset', 'apnea': 'set', 'min_time': 'unset',
'list_only': 'unset', 'pipe_output': 'unset', 'no_numbering': 'unset'}
if not self._check_params_pattern(ok_pattern):
errprint('Wrong parameters combination for dumping!')
exit(2)
elif self.list_only: # Combination for listing
ok_pattern = {'in_subslog': 'unset', 'out_subslog': 'unset', 'big_circle': 'unset',
'small_circle': 'unset', 'apnea': 'set', 'min_time': 'unset',
'dump_dir': 'unset', 'no_numbering': 'unset'}
if not self._check_params_pattern(ok_pattern):
errprint('Wrong parameters combination for dives listing!')
exit(2)
elif self.pipe_output: # Combination for piping
ok_pattern = {'out_subslog': 'unset', 'dump_dir': 'unset'}
if not self._check_params_pattern(ok_pattern):
errprint('Wrong parameters combination for piping output!')
exit(2)
elif self.out_subslog is not None: # Combination for writing log file
ok_pattern = {'pipe_output': 'unset', 'dump_dir': 'unset'}
if not self._check_params_pattern(ok_pattern):
errprint('Wrong parameters combination for writing log output!')
exit(2)
else:
if self.out_subslog is None and not self.pipe_output: # Log output selection must be set
errprint('No output for log selected! File or pipe must be set!')
exit(2)
error_count = 0
# Fit files access
for filename in list(self.fit_files): # Had to create local copy to allow enumerating lists with removed items
if not self._has_access(filename, 'r'):
self.fit_files.remove(filename)
errprint('File removed from FITs list!\n')
if not self.fit_files: # Is FIT files list empty?
errprint("No FIT files to process!\n")
error_count += 1
if self.out_subslog is not None and not self._has_access(self.out_subslog, 'w'): # Is writing possible?
errprint('Output log cannot be created!\n')
error_count += 1
if self.in_subslog is not None and not self._has_access(self.in_subslog, 'r'): # Is reading possible?
errprint('Input log cannot be opened!\n')
error_count += 1
if self.dump_dir is not None: # Is writing dump files possible?
test_file = os.path.join(self.dump_dir, 'x9wp3ui8t.txt')
if not self._has_access(test_file, 'w'):
errprint('Dump file cannot be created!\n')
error_count += 1
if error_count > 0: # Error found in previous checks
errprint("Cannot continue, exiting...")
sys.exit(2)
def message_processor(dive_log, fit_file):
"""Interprets messages read from FIT files"""
# Fit file opening
try:
fitparser = FitFile(fit_file)
except Exception as err: # Catches fit file parsing errors
npprint(err)
return False
decoder = RecordDecoder() # Record fields access object
# Minimal dive time limit filtering
if settings.min_time is not None:
messages = fitparser.get_messages() # Fitparse messages iterator object
# As summary record is at the end of FIT files, extra record traversal is required (time consuming)
for record in messages:
if record.name == 'sport':
val_dict = record.get_values()
if val_dict['sub_sport'] in ('apnea_diving', 'apnea_hunting'): # Skip limit if apnea dives are included
break
if record.name == 'dive_summary':
val_dict = record.get_values()
if val_dict['reference_mesg'] == 'session':
bottom_time = val_dict['bottom_time']
if bottom_time < settings.min_time * 60:
npprint('discarded - bottom time: %.2f min '
'shorter than limit: %.2f min' % (bottom_time / 60, settings.min_time))
return False # Skip futrher processing
# Possible re-assignment because previous one uses (and remembers) dictionary fields access interface
messages = fitparser.get_messages() # Fitparse messages iterator object
# Records processing
for record in messages: # Iterate through all data messages
if record.name == 'device_info': # Dive computer info [1]
decoder.load_record(record)
if decoder.field('device_index') == 'creator':
fw = decoder.field('software_version')
product = decoder.field('garmin_product')
if product == '2859':
product = 'Garmin Descent MK1'
product = '{} ({})'.format(product, fw)
dive_log.dc_data['model'] = product
dive_log.dc_data['serial'] = decoder.field('serial_number')
# DC unique ID created by hashing 'model+serial+fw' fields
dive_log.dc_data['deviceid'] = hashit([product, dive_log.dc_data['serial'], fw])
if record.name == 'event': # Catches 'event' type records [3]
decoder.load_record(record)
if decoder.field('event_type') == 'start': # Activity start event
dive_log.activity_start_timestamp = decoder.field('timestamp', raw=True)
# Alerts =======
elif decoder.field('event') == '56' and decoder.field('data') == '0': # Deco required
dive_log.add_event('Deco required', decoder.field('timestamp', raw=True), 2)
elif decoder.field('event') == '56' and decoder.field('data') == '1': # Gass switch prompted
pass # Don't waste graph
elif decoder.field('event') == '56' and decoder.field('data') == '2': # Surface
pass # Don't waste graph
elif decoder.field('event') == '56' and decoder.field('data') == '3': # Approaching NDL
dive_log.add_event('Approaching NDL', decoder.field('timestamp', raw=True), 2)
elif decoder.field('event') == '56' and decoder.field('data') == '4': # ppO2 soft violation
dive_log.add_event('ppO2 warning', decoder.field('timestamp', raw=True), 3)
elif decoder.field('event') == '56' and decoder.field('data') == '5': # ppO2 high critical
dive_log.add_event('ppO2 critical high', decoder.field('timestamp', raw=True), 4)
elif decoder.field('event') == '56' and decoder.field('data') == '6': # ppO2 low critical
dive_log.add_event('ppO2 critical low', decoder.field('timestamp', raw=True), 4)
elif decoder.field('event') == '56' and decoder.field('data') == '7': # Time alert
dive_log.add_event('Time alert', decoder.field('timestamp', raw=True), 2)
elif decoder.field('event') == '56' and decoder.field('data') == '8': # Depth alert
dive_log.add_event('Depth alert', decoder.field('timestamp', raw=True), 2)
elif decoder.field('event') == '56' and decoder.field('data') == '9': # Deco ceiling broken
dive_log.add_event('Deco ceiling broken', decoder.field('timestamp', raw=True), 3)
elif decoder.field('event') == '56' and decoder.field('data') == '10': # Deco finished - free to exit
dive_log.add_event('Deco completed', decoder.field('timestamp', raw=True), 1)
elif decoder.field('event') == '56' and decoder.field('data') == '11': # Safety stop ceiling broken
dive_log.add_event('Safety stop ceiling broken', decoder.field('timestamp', raw=True), 3)
elif decoder.field('event') == '56' and decoder.field('data') == '12': # Safety stop completed
pass # Don't waste graph
elif decoder.field('event') == '56' and decoder.field('data') == '13': # CNS warning
dive_log.add_event('CNS warning', decoder.field('timestamp', raw=True), 3)
elif decoder.field('event') == '56' and decoder.field('data') == '14': # CNS critical
dive_log.add_event('CNS critical', decoder.field('timestamp', raw=True), 4)
elif decoder.field('event') == '56' and decoder.field('data') == '15': # OTU warning
dive_log.add_event('OTU warning', decoder.field('timestamp', raw=True), 3)
elif decoder.field('event') == '56' and decoder.field('data') == '16': # OTU critical
dive_log.add_event('OTU critical', decoder.field('timestamp', raw=True), 4)
elif decoder.field('event') == '56' and decoder.field('data') == '17': # Ascent speed too high
dive_log.add_event('Ascent speed alert', decoder.field('timestamp', raw=True), 3)
elif decoder.field('event') == '56' and decoder.field('data') == '18': # Alert manualy deleted
pass # Don't waste graph
elif decoder.field('event') == '56' and decoder.field('data') == '19': # Alert timed out
pass # Don't waste graph
elif decoder.field('event') == '56' and decoder.field('data') == '20': # Battery low
dive_log.add_event('Battery low', decoder.field('timestamp', raw=True), 3)
elif decoder.field('event') == '56' and decoder.field('data') == '21': # Battery critical
dive_log.add_event('Battery critical', decoder.field('timestamp', raw=True), 4)
elif decoder.field('event') == '56' and decoder.field('data') == '22': # Safety stop begin
dive_log.add_event('Safety stop begin', decoder.field('timestamp', raw=True), 1)
elif decoder.field('event') == '56' and decoder.field('data') == '23': # Approaching deco stop
dive_log.add_event('Approaching first deco stop', decoder.field('timestamp', raw=True), 1)
# Gas change ======
elif decoder.field('event') == '57': # Mix change - data contains gas number (cylinder index)
dive_log.add_gas_change(decoder.field('data'), decoder.field('timestamp', raw=True))
# Dive end events ======
elif decoder.field('event') == '48': # Dive finished (end GPS position fix maybe) ???
pass # Don't waste graph
elif decoder.field('event') == '38': # Dive finished (end GPS position fix maybe) ???
pass # Don't waste graph
else: # All other events (for reverse engineering only)
dive_log.add_event('event %s, data %s' % (decoder.field('event'), decoder.field('data')),
decoder.field('timestamp', raw=True)) # Fake event description
# -- Sample Fields -- #
# event: timer
# event_group: 0
# event_type: start
# timer_trigger: manual
# timestamp: 2018-04-11 08:56:59
if record.name == 'activity': # Activity record
decoder.load_record(record)
if decoder.field('event_type') == 'stop' and dive_log.dive_detected: # Stop event - last message in log
dive_log.save_dive()
# -- Sample Fields -- #
# event: activity
# event_group: None
# event_type: stop
# local_timestamp: 2018-04-11 12:22:14
# num_sessions: 1
# timestamp: 2018-04-11 10:22:14
# total_timer_time: 5115.432 [s]
if record.name == 'sport': # Contains sport type info - diving can be detected here
decoder.load_record(record)
if decoder.field('sport') in ('53', '54', '55', '56', '57'): # Sport types - probably diving uses only #53
sub_sport = decoder.field('sub_sport')
if settings.apnea and sub_sport in ('apnea_diving', 'apnea_hunting'):
npprint('discarded - apnea dive')
return False
if settings.no_numbering: # No dive numbering when no input log (enables further import)
dive_nr = None
else:
dive_nr = str(dive_log.last_dive_no + 1)
dive_log.dive_data['number'] = dive_nr
if not dive_log.start_dive(): # False if dive already imported
return False
dive_log.update_dc() # Dive computer info
dive_log.set_subsport(sub_sport) # Dive mode (OC, apnea, ...)
dive_log.dive_detected = True # Processing most of other messages will depend on this flag
else:
npprint('discarded - non-diving activity ({})'.format(decoder.field('sport')))
return False # Stop processing - no dive actvity found
# -- Sample Fields -- #
# name: Mieszanki
# sport: 53
# sub_sport: multi_gas_diving
if record.name == 'dive_settings' and dive_log.dive_detected: # Dive settings
decoder.load_record(record)
dive_log.add_water_data(decoder.field('water_density'))
dive_log.add_extra_data('GFLow', decoder.field('gf_low')) # Only global GF setting present in Subsurface
dive_log.add_extra_data('GFHigh', decoder.field('gf_high')) # Only global GF setting present in Subsurface
dive_log.travel_gas_index = decoder.field('message_index') # Travel gas setting (update from Garmin)
# -- Sample Fields -- #
# apnea_countdown_enabled: False
# apnea_countdown_time: 120
# backlight_brightness: 50
# backlight_mode: always_on
# backlight_timeout: 8
# bottom_depth: None
# bottom_time: None
# gf_high: 95[percent]
# gf_low: 45[percent]
# heart_rate_local_device_type: 10
# heart_rate_source_type: local
# message_index: 0