<commit_before>#!/usr/local/bin/python3

import argparse
import gpxpy

parser = argparse.ArgumentParser(description='Clean GPX tracks and split into multiple files.')
parser.add_argument('input', nargs='+', help='a .gpx file to clean and split')
args = parser.parse_args()

for infile in args.input:
	pass<commit_msg>split file into one file per track segment<commit_after>#!/usr/local/bin/python3

import argparse
import gpxpy
import gpxpy.gpx
from pathlib import Path

parser = argparse.ArgumentParser(description='Clean GPX tracks and split into multiple files.')
parser.add_argument('input', nargs='+', help='a .gpx file to clean and split')
args = parser.parse_args()


def makePath(base_name, i):
    return Path('{} {:03d}.gpx'.format(base_name, i))

def createUniqueFile(base_name, time):
    i = 0
    file_name = base_name
    if time:
        file_name += ' ' + time.strftime('%Y-%m-%d %H.%M.%S')
    while makePath(file_name, i).exists():
        i += 1
    return makePath(file_name, i)

def writeAndCreateNewFile(segment, base_name):
    if segment is not None:
        # Create new GPX file
        gpx = gpxpy.gpx.GPX()

        # Create first track in our GPX:
        track = gpxpy.gpx.GPXTrack()
        gpx.tracks.append(track)

        # Add segment to our GPX track:
        track.segments.append(segment)
        
        time = segment.get_time_bounds().start_time
        outfile = createUniqueFile(base_name, time)
        with outfile.open('w') as output:
            output.write(gpx.to_xml())
    
    # Return a new segment
    return gpxpy.gpx.GPXTrackSegment()


new_segment = None

for infile in args.input:
    infile = Path(infile)
    with infile.open() as gpx_file:
        gpx = gpxpy.parse(gpx_file)

        for track in gpx.tracks:
            for segment in track.segments:
                # Create new segment, and write out the current one
                new_segment = writeAndCreateNewFile(segment, infile.stem)




# 'gpsbabel', '-t', '-i', 'gpx', '-f', str(outfile), '-x', 'track,sdistance=0.3k', '-o', 'gpx', '-F', outfile

                # for point in segment.points:

                #     print 'Point at ({0},{1}) -> {2}'.format(point.latitude, point.longitude, point.elevation)
{'fraction_non_alphanumeric': 0.12494402149574563, 'fraction_numerical': 0.0049261083743842365, 'mean_word_length': 2.912434325744308, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1485662', 'n_tokens_mistral': 751, 'n_tokens_neox': 722, 'n_words': 197}