Last active
February 9, 2025 13:56
-
-
Save adrianjost/3d759461ce458b4db3b74bd40ce06e9e to your computer and use it in GitHub Desktop.
A utility to change filenames exported from Sigma Data Center (as .fit) to a format that can be copied to the Activities directory of an ROX 4.0 so it is synced back into the Sigma Ride App. The `D4790161BC34` part might be specific to your ROX 4.0, so it you maybe have to replace that one.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import uuid | |
import datetime | |
import shutil | |
import sys | |
from fitparse import FitFile | |
def extract_activity_start_time(fit_path): | |
"""Extracts the activity start time from a .fit file.""" | |
try: | |
fitfile = FitFile(fit_path) | |
for record in fitfile.get_messages("session"): # Extract session start time | |
for data in record: | |
if data.name == "start_time": | |
return data.value.strftime("%Y-%m-%d_%H-%M-%S") | |
except Exception as e: | |
print(f"Error reading {fit_path}: {e}") | |
return None | |
def extract_file_id_data(fit_path): | |
"""Extracts the file_id field 200 value and time_created timestamp from the .fit file.""" | |
try: | |
fitfile = FitFile(fit_path) | |
for record in fitfile.get_messages("file_id"): # Extract file_id data | |
file_id = None | |
time_created = None | |
for data in record: | |
if data.name == "unknown_200": | |
file_id = str(data.value).upper() | |
if "-" in file_id: | |
parts = file_id.rsplit("-", 1) | |
file_id = f"{parts[0]}-D4790161BC34" # Replace last part | |
if data.name == "time_created": | |
time_created = record.get_raw_value("time_created") | |
if file_id and time_created: | |
return file_id, f"{time_created}000" # Append 3 extra zeroes to time_created | |
except Exception as e: | |
print(f"Error reading file_id from {fit_path}: {e}") | |
return None, None | |
def process_directory(source_directory, target_directory): | |
"""Copies all .fit files from source to target directory with new names.""" | |
if not os.path.exists(target_directory): | |
os.makedirs(target_directory) | |
for filename in os.listdir(source_directory): | |
if filename.endswith(".fit"): | |
original_path = os.path.join(source_directory, filename) | |
activity_start_time = extract_activity_start_time(original_path) | |
file_id, timestamp_part = extract_file_id_data(original_path) | |
print(timestamp_part) | |
if not activity_start_time or not file_id or not timestamp_part: | |
print(f"Skipping {filename}: Unable to extract required metadata") | |
continue | |
new_filename = f"{activity_start_time}_{file_id}_{timestamp_part}_4.fit" | |
new_path = os.path.join(target_directory, new_filename) | |
shutil.copy2(original_path, new_path) | |
print(f"Copied: {filename} -> {new_filename}") | |
def main(): | |
if len(sys.argv) != 3: | |
print("Usage: python script.py <source_directory> <target_directory>") | |
sys.exit(1) | |
source_directory = sys.argv[1] | |
target_directory = sys.argv[2] | |
if not os.path.isdir(source_directory): | |
print("Invalid source directory!") | |
sys.exit(1) | |
process_directory(source_directory, target_directory) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment