grimbough / FITfileR

R package for reading data from FIT files using only native R code, rather than relying on external libraries.

Home Page:https://msmith.de/FITfileR

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Where is the "steps" time series located?

skgrange opened this issue · comments

Hello Mike Smith,
Great work on this package. It helps me a lot when dealing with my running logs. I am struggling to find were the "steps" time series or sums are located in the fit file return. Has this been implemented yet? If you would like an example file, please let me know. Enjoy,
Stuart.

I'm pretty certain steps get called 'cycles' in FIT records. The examples below use a file record from swimming, so it was counting strokes, but I think the principle is sound.

You can get the total count from the total_cycles field from the session message e.g.

library(FITfileR)
library(dplyr)

fenix6_file <- system.file("extdata", "Activities", "garmin-fenix6-swim.fit", 
                           package = "FITfileR")

swim_data <- readFitFile(fenix6_file)
getMessagesByType(swim_data, "session") %>%
    dplyr::select("total_cycles")
#> # A tibble: 1 x 1
#>   total_cycles
#>          <dbl>
#> 1          304

In this file a cumulative count is found in the cycles field of the records messages. That data field can only hold a single-byte integer, so it rolls over at 255. You'll have deal with that in code if you want a true running total. This example takes that field ad checks we get the same value back for the total count:

## there are three types of record in my example file, but only 'record_2' matters
swim_records <- swim_data %>% 
    records() %>%
    magrittr::extract2("record_2")

## crude function for counting the total number of cycles
cumsum_cycles <- function(x) { 
    y <- dplyr::lag(x, default = tail(x, n = 1))
    sum(256 * (x == 0 & y == 255)) + tail(x, n = 1)
}

cumsum_cycles( swim_records$cycles )
#> [1] 304

Many thanks for the information. I will have a browse for the cycles variables and see what I can find. Thanks again!