Initial fit file parsing.
This commit is contained in:
parent
bf25559ef2
commit
fb8883c185
|
|
@ -0,0 +1 @@
|
||||||
|
from corchestrate.measurement import Measurement
|
||||||
38
src/corchestrate/parse.py
Normal file
38
src/corchestrate/parse.py
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
from datetime import datetime
|
||||||
|
from corchestrate.measurement import Measurement
|
||||||
|
from garmin_fit_sdk import Stream, Decoder
|
||||||
|
|
||||||
|
TO_DEGREES = 180 / (2**31)
|
||||||
|
|
||||||
|
|
||||||
|
def from_fit_file_path(fit_file_path: str) -> list[Measurement]:
|
||||||
|
stream = Stream.from_file(fit_file_path)
|
||||||
|
decoder = Decoder(stream)
|
||||||
|
messages, _ = decoder.read()
|
||||||
|
records = messages["record_mesgs"]
|
||||||
|
|
||||||
|
def convert_to_measurement(record) -> Measurement:
|
||||||
|
measurement = Measurement(record.get("timestamp"))
|
||||||
|
measurement.heart_rate = record.get("heart_rate")
|
||||||
|
measurement.speed = record.get("enhanced_speed")
|
||||||
|
measurement.cadence = record.get("cadence")
|
||||||
|
measurement.power = record.get("power")
|
||||||
|
measurement.respiration_rate = record.get("respiration_rate")
|
||||||
|
measurement.temperature = record.get("temperature")
|
||||||
|
measurement.latitude = record.get(
|
||||||
|
"position_lat") * TO_DEGREES if "position_lat" in record else None
|
||||||
|
measurement.longitude = record.get(
|
||||||
|
"position_long"
|
||||||
|
) * TO_DEGREES if "position_long" in record else None
|
||||||
|
measurement.elevation = record.get("enhanced_altitude")
|
||||||
|
return measurement
|
||||||
|
|
||||||
|
dummy = datetime.fromordinal(1)
|
||||||
|
|
||||||
|
def has_timestamp(record) -> bool:
|
||||||
|
return type(record.get("timestamp")) == type(dummy)
|
||||||
|
|
||||||
|
return [
|
||||||
|
convert_to_measurement(record) for record in records
|
||||||
|
if has_timestamp(record)
|
||||||
|
]
|
||||||
Loading…
Reference in a new issue