I succesfuly run the modify_activity example from fit_tools which decode a fit file (Activity.fit) then save the file with a new name, and checke with a fit reader with no errors. I could do that too with mywhoosh fit files with no errors. However, when I provide my own garmit file from a forerunner 965, I get a decode error, generated by the fit_tool file field.py. It appears that the error is related to the wrong format in the decode option. Any hints?
This is the error message:
string_container = bytes_buffer.decode('utf-8')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd2 in position 33: invalid continuation byte
This is the program
from fit_tool.fit_file import FitFile
from fit_tool.fit_file_builder import FitFileBuilder
from fit_tool.profile.messages.record_message import RecordMessage, RecordHeartRateField, RecordPowerField
import copy
def main():
"""The following program reads all the bytes from a FIT formatted file and then
decodes these bytes to create a FIT file object. We then build a modified FIT file
based on a variety of criteria (see comments below). Finally we output
the modified data to a new FIT file.
"""
path = '../fit_tool/tests/data/sdk/2024-03-16-14-21.fit' # original example with Activity.fit
fit_file = FitFile.from_file(path)
builder = FitFileBuilder(auto_define=False)
mod_records = copy.deepcopy(fit_file.records)
for record in fit_file.records:
message = record.message
include_record = True
if message.global_id == RecordMessage.ID:
# Remove the heart rate field from all record definition and data messages
message.remove_field(RecordHeartRateField.ID)
if isinstance(message, RecordMessage):
# remove records where the power is too high
power_field = message.get_field(RecordPowerField.ID)
if power_field and power_field.is_valid():
power = power_field.get_value()
if power > 800:
include_record = False
if include_record:
builder.add(message)
modified_file = builder.build()
modified_file.to_file('../fit_tool/tests/out/modified_activity2.fit')
fit_file2 = FitFile.from_file('../fit_tool/tests/out/modified_activity.fit')
if __name__ == "__main__":
main()