2019-11-05 22:22:46 +01:00
|
|
|
source("packrat/init.R")
|
|
|
|
|
2019-11-05 21:17:20 +01:00
|
|
|
library(dplyr)
|
|
|
|
|
2019-11-13 20:54:24 +01:00
|
|
|
write_empty_file <- function(file_path){
|
|
|
|
write.csv(data.frame(local_date= character(),
|
|
|
|
hometime= numeric(),
|
|
|
|
disttravelled= numeric(),
|
|
|
|
rog= numeric(),
|
|
|
|
maxdiam= numeric(),
|
|
|
|
maxhomedist= numeric(),
|
|
|
|
siglocsvisited= numeric(),
|
|
|
|
avgflightlen= numeric(),
|
|
|
|
stdflightlen= numeric(),
|
|
|
|
avgflightdur= numeric(),
|
|
|
|
stdflightdur= numeric(),
|
|
|
|
probpause= numeric(),
|
|
|
|
siglocentropy= numeric(),
|
|
|
|
minsmissing= numeric(),
|
|
|
|
circdnrtn= numeric(),
|
|
|
|
wkenddayrtn= numeric()
|
|
|
|
), file_path, row.names = F)
|
|
|
|
}
|
|
|
|
|
2019-11-05 21:17:20 +01:00
|
|
|
# Load Ian Barnett's code. Taken from https://scholar.harvard.edu/ibarnett/software/gpsmobility
|
|
|
|
file.sources = list.files(c("src/features/location_barnett"), pattern="*.R$", full.names=TRUE, ignore.case=TRUE)
|
|
|
|
sapply(file.sources,source,.GlobalEnv)
|
|
|
|
|
2019-12-10 01:15:10 +01:00
|
|
|
locations_to_use <- snakemake@params[["locations_to_use"]]
|
2019-11-05 21:17:20 +01:00
|
|
|
accuracy_limit <- snakemake@params[["accuracy_limit"]]
|
|
|
|
timezone <- snakemake@params[["timezone"]]
|
|
|
|
|
2019-12-10 01:15:10 +01:00
|
|
|
# By deafult we use all raw locations: fused without resampling and not fused (gps, network)
|
|
|
|
location <- read.csv(snakemake@input[["raw"]], stringsAsFactors = F) %>%
|
2019-11-05 21:17:20 +01:00
|
|
|
select(timestamp, latitude = double_latitude, longitude = double_longitude, altitude = double_altitude, accuracy)
|
|
|
|
|
2019-12-10 01:15:10 +01:00
|
|
|
if(locations_to_use == "ALL_EXCEPT_FUSED"){
|
|
|
|
location <- location %>% filter(provider != "fused")
|
|
|
|
} else if (locations_to_use == "RESAMPLE_FUSED"){
|
|
|
|
location <- read.csv(snakemake@input[["fused"]], stringsAsFactors = F) %>%
|
|
|
|
select(timestamp, latitude = double_latitude, longitude = double_longitude, altitude = double_altitude, accuracy)
|
|
|
|
} else if (locations_to_use != "ALL"){
|
|
|
|
print("Unkown filter, provide one of the following three: ALL, ALL_EXCEPT_FUSED, or RESAMPLE_FUSED")
|
|
|
|
quit(save = "no", status = 1, runLast = FALSE)
|
|
|
|
}
|
|
|
|
|
2019-11-13 20:54:24 +01:00
|
|
|
if (nrow(location) > 1){
|
2019-11-05 21:17:20 +01:00
|
|
|
features <- MobilityFeatures(location, ACCURACY_LIM = accuracy_limit, tz = timezone)
|
2019-11-13 20:54:24 +01:00
|
|
|
if(is.null(features)){
|
|
|
|
write_empty_file(snakemake@output[[1]])
|
|
|
|
} else{
|
|
|
|
# Copy index (dates) as a column
|
|
|
|
outmatrix <- cbind(rownames(features$featavg), features$featavg)
|
|
|
|
outmatrix <- as.data.frame(outmatrix)
|
|
|
|
outmatrix[-1] <- lapply(lapply(outmatrix[-1], as.character), as.numeric)
|
|
|
|
colnames(outmatrix)=c("local_date",tolower(colnames(features$featavg)))
|
|
|
|
write.csv(outmatrix,snakemake@output[[1]], row.names = F)
|
|
|
|
}
|
|
|
|
|
2019-11-05 21:17:20 +01:00
|
|
|
} else {
|
2019-11-13 20:54:24 +01:00
|
|
|
write_empty_file(snakemake@output[[1]])
|
2019-11-05 21:17:20 +01:00
|
|
|
}
|