dtcooper / python-fitparse

Python library to parse ANT/Garmin .FIT files

Home Page:http://pythonhosted.org/fitparse/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FIT containing multiple activities?

woodmicha opened this issue · comments

More of a question than an issue. Can you let me know how to determine which records align with which activity in instances where multiple activities have been included in one FIT file (e.g. triathlon with swim, transition, bike, transition, run)?

The records pull in fine but I'm trying to segregate the records into distinct sets for each activity.

Thanks in advance for any suggestions.

If you could send me a copy of a FIT file in question, I can try to help. david AT dtcooper.com

Thanks!

In our project which supports this type of multi-activities files we do something like this :

...
messages = []
self.sessions = []

for message in fit.get_messages(as_dict=True):
    name = message['name']

    # Store message
    messages.append(message)

    if name == 'session':
        # New session
        session = GarminSessionFIT(messages)
        self.sessions.append(session)

        # reset
        messages = []

And then in GarminSessionFIT something like :

...
def __init__(self, messages):
    # Init categorized messages
    self.messages = {}

    for message in messages:
        name = message['name']
        if name not in self.messages:
            self.messages[name] = []
        self.messages[name].append({f['name']: f for f in message['fields']})

    # Then you can do thing like
    # Find sport
        if 'sport' in self.messages:
            self.sport = self.messages['sport'][0]['sport']['value']

    # Find session attributes
    if 'session' in self.messages:
        session = self.messages['session'][0] # should always have only one

        # Find start date
        if 'start_time' in session:
            if isinstance(session['start_time']['value'], datetime):
                self.start_date = session['start_time']['value']

Thanks for the guidance. Assuming the messages are in sequential order the approach outlined by @jeanbaptistemora will work fine. I'll give it a whirl this weekend and holler if I have any more questions. Regards, Mike

They should be sequential @woodmicha. Going to close this, but please let us know if you continue to have problems. Thanks!

(As well, thanks for the help @jeanbaptistemora 😄)

You guys gave me great information. I have one more question I've been unable to establish the exact logic Garmin uses for smart recording mode. I'm curious if you know how it works or have any pointers?

We have a function to expand a stream that has been recorded with smart mode on. Right now we propagate values back in time instead of pulling the last value forward.

For example:

time[0] = 1; watts[0] = 100
time[1] = 2; watts[1] = 110
time[2] = 4; watts[2] = 115

yields [push back]:

watts[0] = 110
watts[1] = 110
watts[2] = 115
watts[3] = 115

instead of [pull forward]:
watts[0] = 110
watts[1] = 110
watts[2] = 110
watts[3] = 115

Do you know which approach is proper?

Thanks in advance for any pointers.

Best