Skip to content

Commit

Permalink
feat: Working CSV export
Browse files Browse the repository at this point in the history
  • Loading branch information
HlisTilen committed Apr 8, 2024
1 parent 937113a commit 1fb934a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 59 deletions.
60 changes: 7 additions & 53 deletions examples/Main.jl → examples/reader_demo.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
include("../src/TCXreader.jl")

using .TCXreader: loadTCXFile
using CSV, DataFrames

function main()
author, activities = loadTCXFile("../example_data/15.tcx")

# Displaying author information
println("Author Information:")
println("Name: ", author.name)
Expand All @@ -14,7 +13,7 @@ function main()
println("Language ID: ", author.langID)
println("Part Number: ", author.partNumber)
println("===================================")

for activity in activities
# Displaying activity information
println("Activity Information:")
Expand All @@ -41,62 +40,17 @@ function main()
println("\tAverage Speed: ", lap.avgSpeed)
println("\tTrack Points:")
for tp in lap.trackPoints
println("\t\tTime: ", tp.time, ", Latitude: ", tp.latitude, ", Longitude: ", tp.longitude,
", Altitude: ", tp.altitude_meters, ", Distance: ", tp.distance_meters,
", Heart Rate: ", tp.heart_rate_bpm, ", Speed: ", tp.speed)
println("\t\tTime: ", tp.time, ", Latitude: ", tp.latitude, ", Longitude: ", tp.longitude,
", Altitude: ", tp.altitude_meters, ", Distance: ", tp.distance_meters,
", Heart Rate: ", tp.heart_rate_bpm, ", Speed: ", tp.speed)
end
end
println("===================================")
end

# Initialize a DataFrame to store our data
df = DataFrame(
AuthorName = String[],
AuthorBuildVersion = String[],
AuthorLangID = String[],
AuthorPartNumber = String[],
ActivitySport = String[],
ActivityID = String[],
DeviceName = String[],
DeviceVersion = String[],
LapNumber = Int[],
StartTime = String[],
TotalTimeSeconds = Float64[],
DistanceMeters = Float64[],
Calories = Int[],
AverageHR = Int[],
MaximumHR = Int[],
Intensity = String[]
)

# Loop through activities and their laps to populate the DataFrame
for activity in activities
for (i, lap) in enumerate(activity.laps)
push!(df, (
author.name,
string(author.build.versionMajor, ".", author.build.versionMinor),
author.langID,
author.partNumber,
activity.sport,
string(activity.id),
activity.device.name,
activity.device.version,
i,
string(lap.startTime),
lap.totalTimeSeconds,
lap.distanceMeters,
lap.calories,
isnothing(lap.averageHeartRateBpm) ? missing : lap.averageHeartRateBpm,
isnothing(lap.maximumHeartRateBpm) ? missing : lap.maximumHeartRateBpm,
lap.intensity
))
end
end

# Write the DataFrame to a CSV file
CSV.write("tcx_data_export.csv", df)
# Load a TCX file and export the data to CSV
author, activities = loadTCXFile("../example_data/15.tcx", "tcx_data_export.csv")

println("Exported TCX data to tcx_data_export.csv")
end

main()
78 changes: 72 additions & 6 deletions src/TCXreader.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module TCXreader

using Dates

using EzXML
using EzXML, CSV, DataFrames, Dates

export TCXTrackPoint, BuildVersion, TCXAuthor, TCXLap, TCXActivity, DeviceInfo, loadTCXFile, parseTCXAuthor, parseTCXLap, parseTCXTrackPoint, parseDeviceInfo

Expand Down Expand Up @@ -78,7 +76,7 @@ end

function parseTCXAuthor(doc::EzXML.Document)
authorNode = findfirst(".//g:Author", doc.root, NS_MAP)

if authorNode !== nothing
name = nodecontent(findfirst(".//g:Name", authorNode, NS_MAP))
buildNode = findfirst(".//g:Build", authorNode, NS_MAP)
Expand Down Expand Up @@ -107,12 +105,76 @@ function parseDeviceInfo(doc::EzXML.Document)
end
end

function loadTCXFile(filepath::String)
function exportCSV(author::TCXAuthor, activities::Vector{TCXActivity}, csv_filepath::String)
df = DataFrame(
AuthorName = String[],
AuthorBuildVersion = String[],
AuthorLangID = String[],
AuthorPartNumber = String[],
ActivitySport = String[],
ActivityID = String[],
DeviceName = String[],
DeviceVersion = String[],
LapNumber = Int[],
StartTime = String[],
TotalTimeSeconds = Float64[],
DistanceMeters = Float64[],
Calories = Int[],
AverageHR = Union{Int, Missing}[],
MaximumHR = Union{Int, Missing}[],
Intensity = String[],
TrackPointTime = String[],
Latitude = Union{Float64, Missing}[],
Longitude = Union{Float64, Missing}[],
AltitudeMeters = Union{Float64, Missing}[],
DistanceMetersTP = Union{Float64, Missing}[],
HeartRateBpm = Union{Int, Missing}[],
Speed = Union{Float64, Missing}[]
)

for activity in activities
for (lap_num, lap) in enumerate(activity.laps)
for tp in lap.trackPoints
# Ensure all Nothing values are replaced with missing
row = (
author.name,
string(author.build.versionMajor, ".", author.build.versionMinor),
author.langID,
author.partNumber,
activity.sport,
string(activity.id),
activity.device.name,
activity.device.version,
lap_num,
string(lap.startTime),
lap.totalTimeSeconds,
lap.distanceMeters,
lap.calories,
lap.averageHeartRateBpm === nothing ? missing : lap.averageHeartRateBpm,
lap.maximumHeartRateBpm === nothing ? missing : lap.maximumHeartRateBpm,
lap.intensity,
string(tp.time),
tp.latitude === nothing ? missing : tp.latitude,
tp.longitude === nothing ? missing : tp.longitude,
tp.altitude_meters === nothing ? missing : tp.altitude_meters,
tp.distance_meters === nothing ? missing : tp.distance_meters,
tp.heart_rate_bpm === nothing ? missing : tp.heart_rate_bpm,
tp.speed === nothing ? missing : tp.speed
)
push!(df, row)
end
end
end

CSV.write(csv_filepath, df; writeheader=true)
end

function loadTCXFile(filepath::String, csv_filepath::Union{String,Nothing}=nothing)
doc = readxml(filepath)
parsed_author = parseTCXAuthor(doc)

activitiesNode = findfirst(".//g:Activities", doc.root, NS_MAP)
activities = []
activities = Vector{TCXActivity}()

for activityNode in findall(".//g:Activity", activitiesNode, NS_MAP)
sport = activityNode["Sport"]
Expand All @@ -125,6 +187,10 @@ function loadTCXFile(filepath::String)
push!(activities, TCXActivity(sport, id, parsed_laps, device_info))
end

if csv_filepath !== nothing
exportCSV(parsed_author, activities, csv_filepath)
end

return parsed_author, activities
end

Expand Down

0 comments on commit 1fb934a

Please sign in to comment.