https://doi.org/10.5281/zenodo.14318846
03 - filter for max nsd above 40.R
#
#
#currently set to only add tod and filter for date/min spm
#
#
rm(list=ls())
#packages
library(dplyr)
library(ggplot2)
library(sp)
library(maptools)
#file list
file_list <- list.files("data/find ladder/SSF data with tod filtered for migration and min spm over 2/",full.names=T)
#keep only tracks where max_nsd >40
#get list of Good track ids and fish
#need to do filtering in track_straightness as well to get Same Tracks
for(p in 1:length(file_list)){
data <- read.csv(file_list[[p]])
id <- data$fish_id[1]
data$t1_ <- as.POSIXct(data$t1_)
data$t2_ <- as.POSIXct(data$t2_)
if(length(data$fish_id)>1){
#calculate nsd
X <- split(data, list(data$fish_id,data$track),drop=T)
for (d in 1:length(X)) { #ok getting errors as assuming levels for all parts of track
track <- X[[d]]
#save starting coord
start <- track[1,c("x1_","y1_")] #start coords
track$nsd <- with(track, sqrt((x2_ - start[[1]])^2 + (y2_ - start[[2]])^2))
if(d==1){
data_filt <- track
} else{
data_filt <- rbind(data_filt,track)
}
}
#now summarise nsd per track
sum_nsd <- data_filt %>% group_by(track) %>% summarise(max_nsd=max(nsd))
#now filter to remove tracks nsd<40
tracks_max40 <- sum_nsd %>% filter(max_nsd>=40)
#now filter data
dat_filt <- data %>% filter(track %in% unique(tracks_max40$track))
write.csv(dat_filt_final,
paste0("data/find ladder/FINAL final SSF dataframes - filtered - decision tree/",id,".csv"),
row.names = F)
} else{
print(paste0("fish id ",id," no data post date filtering"))
}
}