#
# Life Artina - track2kba analysis
# mate.zec@biom.hr
# Jan 2022
#

# install.packages("track2KBA")

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.1.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(readxl)
library(sf)
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1
library(track2KBA)
## Registered S3 methods overwritten by 'adehabitatMA':
##   method                       from
##   print.SpatialPixelsDataFrame sp  
##   print.SpatialPixels          sp
# definitions -------------------------------------------------------------

# store needed CRS definitions as variables:

wgs84 <- st_crs("EPSG:4326")
htrs96 <- st_crs("EPSG:3765")

# where are all the files? ------------------------------------------------

# find Yelkouan .pos files by searching the data input folder recursively:

yelkouan_pos_2019 <- list.files(path = "data-input/Shearwater tagging/Yelkouan 2019/",
                                pattern = ".pos",
                                recursive = TRUE,
                                include.dirs = TRUE,
                                full.names = TRUE)
yelkouan_pos_2020 <- list.files(path = "data-input/Shearwater tagging/Yelkouan 2020/",
                                pattern = ".pos",
                                recursive = TRUE,
                                include.dirs = TRUE,
                                full.names = TRUE)
yelkouan_pos_2021 <- list.files(path = "data-input/Shearwater tagging/Yelkouan 2021/",
                                pattern = ".pos",
                                recursive = TRUE,
                                include.dirs = TRUE,
                                full.names = TRUE)

# find Scopoli .pos files

scopoli_pos_2019 <- list.files(path = "data-input/Shearwater tagging/Scopoli 2019/",
                                pattern = ".pos",
                                recursive = TRUE,
                                include.dirs = TRUE,
                                full.names = TRUE)
scopoli_pos_2020 <- list.files(path = "data-input/Shearwater tagging/Scopoli 2020/",
                                pattern = ".pos",
                                recursive = TRUE,
                                include.dirs = TRUE,
                                full.names = TRUE)
scopoli_pos_2021 <- list.files(path = "data-input/Shearwater tagging/Scopoli 2021/",
                                pattern = ".pos",
                                recursive = TRUE,
                                include.dirs = TRUE,
                                full.names = TRUE)


colony_data <- read_excel(path = "data-input/MetaData_tagging_Artina.xlsx")
colony_data_spat <- st_as_sf(colony_data, coords = c("lon_colony", "lat_colony"), crs = wgs84) %>% 
  st_transform(crs = htrs96)
colony_coords_htrs <- st_coordinates(colony_data_spat)
colony_data$colony_X <- colony_coords_htrs[,1]
colony_data$colony_Y <- colony_coords_htrs[,2]

# reading the .pos files --------------------------------------------------

# function that fetches the data from individual files:

getposdata <- function(filelist, ...) {
  posnames <- c("day",  "month",    "year", "hour", "minute",   "second", 
                 "satellites",  "latitude", "longitude",    "altitude", 
                 "time_offset", "accuracy", "voltage")
  poscols <- "iiiiii_innnnnn__"
  list_of_pos_tables <- lapply(filelist, FUN = read_csv, 
                               col_types = poscols, 
                               col_names = posnames,
                               skip = 5)
  for (i in 1:length(filelist)) {
    # get id of tag from filename structure:
    tag_id <- gsub(".*?/(Tag.*?) - (.*?)/.*", "\\1_\\2", filelist[i])
    # get id of colony from filename structure:
    list_of_pos_tables[[i]]$colony_code <- gsub(".*?/(Tag.*?) - (.*?)-.*", "\\2", filelist[i])
    print(tag_id)
    list_of_pos_tables[[i]]$bird_id <- paste0(list_of_pos_tables[[i]]$year, "_",
                                              tag_id)
    }
  postable <- bind_rows(list_of_pos_tables) %>% 
    mutate(dttm = ymd_hms(paste(year, month, day, hour, minute, second, sep = "-")))
  return(postable)  
}

# yelkouan loading and preprocessing --------------------------------------

yelk2019 <- getposdata(yelkouan_pos_2019)
## [1] "Tag17600_Z-9"
## [1] "Tag17604_Z-7"
## [1] "Tag17617_Z-4 (2nd Parent)"
## [1] "Tag17644_Z-13"
## [1] "Tag17652_Z-2"
## [1] "Tag17704_Z-11"
## [1] "Tag17735_Z-3 (RAW DATA MISSING)"
## [1] "Tag40066_Z-14"
## [1] "Tag40069_Z-6"
## [1] "Tag40073_Z-1"
## [1] "Tag40078_Z-3 (2nd Parent)"
## [1] "Tag40086_Z-11 (2nd Parent)"
## [1] "Tag40094_Z-16"
## [1] "Tag40118_Z-2 (2nd Parent)"
## [1] "Tag40133_Z-15"
## [1] "Tag40138_Z-17"
## [1] "Tag40170_Z-12"
## [1] "Tag40177_Z-17 (2nd Parent)"
## [1] "Tag40182_Z-4"
yelk2020 <- getposdata(yelkouan_pos_2020)
## [1] "Tag17600_Z-170"
## [1] "Tag17604_Z-95"
## [1] "Tag17644_Z-106"
## [1] "Tag17677_Z-170 (2nd Parent)"
## [1] "Tag17700_VM-13"
## [1] "Tag17717_VM-8"
## [1] "Tag17724_Z-106 (2nd Parent)"
## [1] "Tag40014_VM-3 (2nd Parent)"
## [1] "Tag40024_Z-178 (2nd Parent)"
## [1] "Tag40039_Z-15 (2nd Parent)"
## [1] "Tag40073_Z-131 (2nd Parent)"
## [1] "Tag40078_Z-178"
## [1] "Tag40086_VM-3"
## [1] "Tag40094_Z-131"
## [1] "Tag40118_Z-175"
## [1] "Tag40133_Z-1 (2nd Parent)"
## [1] "Tag40138_VM-18"
## [1] "Tag40536_VM-8 (2nd Parent)"
## [1] "Tag40615_VM-23"
## [1] "Tag40817_VM-12"
## [1] "Tag40859_Z-179"
## [1] "Tag41108_Z-95 (2nd Parent)"
## [1] "Tag40193_Z-13 (2nd Parent)"
yelk2021 <- getposdata(yelkouan_pos_2021)
## [1] "Tag41220_VR-1"
yelk <- bind_rows(yelk2019, yelk2020, yelk2021) %>% 
  # filter(year != 21) %>% # misunderstanding, still a breeding bird
  filter(latitude != 0) %>% 
  filter(time_offset < 1000)

dupes <- yelk %>% dplyr::select(bird_id, dttm) %>% 
  duplicated()

yelk <- yelk %>% filter(!dupes)

yelk_xy <- st_as_sf(yelk, coords = c("longitude", "latitude"), crs = wgs84)
st_write(yelk_xy, "data-output/yelk_points.gpkg", append = FALSE)
## Deleting layer `yelk_points' using driver `GPKG'
## Writing layer `yelk_points' to data source 
##   `data-output/yelk_points.gpkg' using driver `GPKG'
## Writing 16692 features with 14 fields and geometry type Point.
# formatting for track2KBA ------------------------------------------------

# Zaklopatica
yelk_zaklop <- filter(yelk, colony_code == "Z") %>% 
  rename(DateTime = dttm,
         Latitude = latitude,
         Longitude = longitude,
         ID = bird_id)


colony_yelk_zaklop <- filter(colony_data, colony_code == "Z") %>% 
  dplyr::select(Latitude = lat_colony,
                Longitude = lon_colony)

trips_yelk_zaklop <- suppressWarnings(tripSplit(
  dataGroup = yelk_zaklop,
  colony = colony_yelk_zaklop,
  innerBuff = 3,
  returnBuff = 10,
  duration = 1,
  rmNonTrip = TRUE
))
## track 19_Tag17600_Z-901 starts out on trip
## track 19_Tag17600_Z-97 does not return to the colony
## track 19_Tag17604_Z-78 does not return to the colony
## track 19_Tag17617_Z-4 (2nd Parent)6 does not return to the colony
## track 19_Tag17644_Z-1301 starts out on trip
## track 19_Tag17652_Z-201 starts out on trip
## track 19_Tag17704_Z-119 does not return to the colony
## track 19_Tag17735_Z-3 (RAW DATA MISSING)01 starts out on trip
## track 19_Tag40066_Z-1413 does not return to the colony
## track 19_Tag40069_Z-601 starts out on trip
## track 19_Tag40073_Z-117 does not return to the colony
## track 19_Tag40078_Z-3 (2nd Parent)2 does not return to the colony
## track 19_Tag40086_Z-11 (2nd Parent)01 starts out on trip
## track 19_Tag40094_Z-162 does not return to the colony
## track 19_Tag40133_Z-1513 does not return to the colony
## track 19_Tag40138_Z-176 does not return to the colony
## track 19_Tag40170_Z-1213 does not return to the colony
## track 19_Tag40177_Z-17 (2nd Parent)01 starts out on trip
## track 19_Tag40182_Z-401 starts out on trip
## track 20_Tag17600_Z-17013 does not return to the colony
## track 20_Tag17644_Z-10601 starts out on trip
## track 20_Tag17644_Z-1067 does not return to the colony
## track 20_Tag40024_Z-178 (2nd Parent)15 does not return to the colony
## track 20_Tag40073_Z-131 (2nd Parent)01 starts out on trip
## track 20_Tag40073_Z-131 (2nd Parent)18 does not return to the colony
## track 20_Tag40078_Z-1785 does not return to the colony
## track 20_Tag40094_Z-13101 starts out on trip
## track 20_Tag40118_Z-17501 starts out on trip
## track 20_Tag40118_Z-17524 does not return to the colony
## track 20_Tag40133_Z-1 (2nd Parent)01 starts out on trip
## track 20_Tag40193_Z-13 (2nd Parent)01 starts out on trip
## track 20_Tag40859_Z-17901 starts out on trip
## track 20_Tag40859_Z-1793 does not return to the colony
## track 20_Tag41108_Z-95 (2nd Parent)16 does not return to the colony
trips_yelk_zaklop <- subset(trips_yelk_zaklop, trips_yelk_zaklop$Returns == "Yes")

sumTrips <- tripSummary(trips = trips_yelk_zaklop, colony = colony_yelk_zaklop)
mapTrips(trips = trips_yelk_zaklop, colony = colony_yelk_zaklop)
## Too many individuals to plot. Only 25 IDs will be shown.
## Trips colored by completeness. Indicate colorBy=='trip' to color by
##     trips.

# Veli Maslovnjak
yelk_velmas <- filter(yelk, colony_code == "VM") %>% 
  rename(DateTime = dttm,
         Latitude = latitude,
         Longitude = longitude,
         ID = bird_id)

colony_yelk_velmas <- filter(colony_data, colony_code == "VM") %>% 
  dplyr::select(Latitude = lat_colony,
                Longitude = lon_colony)

trips_yelk_velmas <- suppressWarnings(tripSplit(
  dataGroup = yelk_velmas,
  colony = colony_yelk_velmas,
  innerBuff = 3,
  returnBuff = 10,
  duration = 1,
  rmNonTrip = TRUE
))
## track 20_Tag17700_VM-1301 starts out on trip
## track 20_Tag17700_VM-1316 does not return to the colony
## track 20_Tag17717_VM-801 starts out on trip
## track 20_Tag17717_VM-84 does not return to the colony
## track 20_Tag40014_VM-3 (2nd Parent)01 starts out on trip
## track 20_Tag40014_VM-3 (2nd Parent)19 does not return to the colony
## track 20_Tag40086_VM-301 starts out on trip
## track 20_Tag40138_VM-1814 does not return to the colony
## track 20_Tag40536_VM-8 (2nd Parent)01 starts out on trip
## track 20_Tag40615_VM-2301 starts out on trip
## track 20_Tag40615_VM-2312 does not return to the colony
## track 20_Tag40817_VM-1201 starts out on trip
trips_yelk_velmas <- subset(trips_yelk_velmas, trips_yelk_velmas$Returns == "Yes")

sumTrips <- tripSummary(trips = trips_yelk_velmas, colony = colony_yelk_velmas)
mapTrips(trips = trips_yelk_velmas, colony = colony_yelk_velmas)
## Trips colored by completeness. Indicate colorBy=='trip' to color by
##     trips.

# processing of all yelkouan tracks ---------------------------------------

trips_yelk <- rbind(trips_yelk_zaklop, trips_yelk_velmas)

tracks <- projectTracks(dataGroup = trips_yelk, 
                        projType = 'azim', 
                        custom=TRUE )
## NOTE: projection is data specific
hVals <- findScale(
  tracks   = tracks,
  scaleARS = TRUE,
  sumTrips = sumTrips)
## No 'res' was specified. Movement scale in the data was compared
##         to a 500-cell grid with cell size of 1.096 km squared.

hVals
##   med_max_dist step_length  mag  href scaleARS
## 1        52.16        4.53 3.95 14.75        9
tracks <- tracks[tracks$ColDist > 3, ] # remove trip start and end points near colony

KDE <- suppressWarnings(estSpaceUse(
  tracks = tracks, 
  scale = hVals$mag, 
  levelUD = 50, 
  polyOut = TRUE
))
## No grid resolution ('res') was specified, or the specified resolution was
##     >99 km and therefore ignored. Space use was calculated on a 500-cell grid,
##     with cells of 1.205 square km
mapKDE(KDE$UDPolygons)

repr <- repAssess(
  tracks    = tracks, 
  KDE       = KDE$KDE.Surface,
  levelUD   = 50,
  iteration = 1, 
  bootTable = FALSE)
## 
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
## 
##     select
## nls (non linear regression) successful, asymptote estimated for
##     bootstrap sample.

# scopoli loading and preprocessing ---------------------------------------

scop2019 <- getposdata(scopoli_pos_2019)
## [1] "Tag17727_SL-5 (NEST FAILED)"
## [1] "Tag40111_SL-3 (NEST FAILED)"
scop2020 <- getposdata(scopoli_pos_2020)
## [1] "Tag41186_MM-23 (2nd Parent)"
## [1] "Tag41220_MM-22"
## [1] "Tag41221_MM-24"
## [1] "Tag41226_MM-41"
## [1] "Tag41235_MM-42"
## [1] "Tag41254_MM-36 (2nd Parent)"
## [1] "Tag41256_MM-9"
## [1] "Tag41260_MM-5 (2nd Parent)"
## [1] "Tag41271_MM-18"
## [1] "Tag41274_MM-12 (2nd Parent)"
## [1] "Tag41278_MM-16"
## [1] "Tag41280_MM-36"
## [1] "Tag41281_MM-21"
## [1] "Tag41315_MM-39 (2nd Parent)"
## [1] "Tag41318_MM-42 (2nd Parent)"
## [1] "Tag41335_MM-23"
## [1] "Tag41337_MM-33"
## [1] "Tag41344_MM-5"
## [1] "Tag41349_MM-21 (2nd Parent)"
## [1] "Tag41378_MM-22 (2nd Parent)"
## [1] "Tag40122_MM-12"
## [1] "Tag40182_MM-39"
## [1] "Tag40245_MM-17 (NEST FAILED)"
scop2021 <- getposdata(scopoli_pos_2021)
## [1] "Tag41002_SL-1"
## [1] "Tag41049_SL-3 (2nd Parent)"
## [1] "Tag41212_SL-6"
## [1] "Tag41221_GL-1 (NEST FAILED)"
## [1] "Tag41226_GL-4 (NEST FAILED)"
## [1] "Tag41233_SL-50 (NEST FAILED)"
## [1] "Tag41235_GL-13"
## [1] "Tag41246_SL-3"
## [1] "Tag41256_GL-4 (2nd Parent) (NEST FAILED)"
## [1] "Tag41258_SL-50 (2nd Parent) (NEST FAILED)"
## [1] "Tag41262_SL-5 (2nd Parent)"
## [1] "Tag41271_SL-7 (2nd Parent)"
## [1] "Tag41281_GL-7"
## [1] "Tag41318_SL-11 (NEST FAILED)"
## [1] "Tag41334_SL-5"
## [1] "Tag41335_GL-8"
## [1] "Tag41337_GL-7 (2nd Parent)"
## [1] "Tag41344_SL-8"
## [1] "Tag41378_SL-6 (2nd Parent)"
scop <- bind_rows(scop2019, scop2020, scop2021) %>% 
  filter(latitude != 0) %>% 
  filter(time_offset < 1000)

dupes <- scop %>% dplyr::select(bird_id, dttm) %>% 
  duplicated()

scop <- scop %>% filter(!dupes)

scop_xy <- st_as_sf(scop, coords = c("longitude", "latitude"), crs = wgs84)
st_write(scop_xy, "data-output/scop_points.gpkg", append = FALSE)
## Deleting layer `scop_points' using driver `GPKG'
## Writing layer `scop_points' to data source 
##   `data-output/scop_points.gpkg' using driver `GPKG'
## Writing 38377 features with 14 fields and geometry type Point.
# Mali Maslovnjak
scop_malmas <- filter(scop, colony_code == "MM") %>% 
  rename(DateTime = dttm,
         Latitude = latitude,
         Longitude = longitude,
         ID = bird_id)

colony_scop_malmas <- filter(colony_data, colony_code == "MM") %>% 
  dplyr::select(Latitude = lat_colony,
                Longitude = lon_colony)

trips_scop_malmas <- suppressWarnings(tripSplit(
  dataGroup = scop_malmas,
  colony = colony_scop_malmas,
  innerBuff = 3,
  returnBuff = 10,
  duration = 1,
  rmNonTrip = TRUE
))
## track 20_Tag40122_MM-121 does not return to the colony
## track 20_Tag40182_MM-3901 starts out on trip
## track 20_Tag40182_MM-394 does not return to the colony
## track 20_Tag40245_MM-17 (NEST FAILED)01 starts out on trip
## track 20_Tag41186_MM-23 (2nd Parent)01 starts out on trip
## track 20_Tag41221_MM-2401 starts out on trip
## track 20_Tag41221_MM-2428 does not return to the colony
## track 20_Tag41226_MM-4101 starts out on trip
## track 20_Tag41235_MM-4201 starts out on trip
## track 20_Tag41235_MM-4227 does not return to the colony
## track 20_Tag41254_MM-36 (2nd Parent)01 starts out on trip
## track 20_Tag41256_MM-901 starts out on trip
## track 20_Tag41256_MM-919 does not return to the colony
## track 20_Tag41260_MM-5 (2nd Parent)01 starts out on trip
## track 20_Tag41260_MM-5 (2nd Parent)21 does not return to the colony
## track 20_Tag41271_MM-1801 starts out on trip
## track 20_Tag41271_MM-1819 does not return to the colony
## track 20_Tag41274_MM-12 (2nd Parent)01 starts out on trip
## track 20_Tag41280_MM-3601 starts out on trip
## track 20_Tag41280_MM-3622 does not return to the colony
## track 20_Tag41315_MM-39 (2nd Parent)01 starts out on trip
## track 20_Tag41318_MM-42 (2nd Parent)01 starts out on trip
## track 20_Tag41335_MM-2301 starts out on trip
## track 20_Tag41337_MM-3301 starts out on trip
## track 20_Tag41337_MM-3319 does not return to the colony
## track 20_Tag41344_MM-501 starts out on trip
## track 20_Tag41344_MM-533 does not return to the colony
## track 20_Tag41349_MM-21 (2nd Parent)01 starts out on trip
## track 20_Tag41378_MM-22 (2nd Parent)01 starts out on trip
trips_scop_malmas <- subset(trips_scop_malmas, trips_scop_malmas$Returns == "Yes")

sumTrips <- tripSummary(trips = trips_scop_malmas, colony = colony_scop_malmas)
mapTrips(trips = trips_scop_malmas, colony = colony_scop_malmas)
## Trips colored by completeness. Indicate colorBy=='trip' to color by
##     trips.

# Gornji Lukovac
scop_gorluk <- filter(scop, colony_code == "GL") %>% 
  rename(DateTime = dttm,
         Latitude = latitude,
         Longitude = longitude,
         ID = bird_id)

colony_scop_gorluk <- filter(colony_data, colony_code == "GL") %>% 
  dplyr::select(Latitude = lat_colony,
                Longitude = lon_colony)

trips_scop_gorluk <- suppressWarnings(tripSplit(
  dataGroup = scop_gorluk,
  colony = colony_scop_gorluk,
  innerBuff = 3,
  returnBuff = 10,
  duration = 1,
  rmNonTrip = TRUE
))
## track 21_Tag41221_GL-1 (NEST FAILED)01 starts out on trip
## track 21_Tag41235_GL-1301 starts out on trip
## track 21_Tag41256_GL-4 (2nd Parent) (NEST FAILED)01 starts out on trip
## track 21_Tag41256_GL-4 (2nd Parent) (NEST FAILED)6 does not return to the colony
## track 21_Tag41337_GL-7 (2nd Parent)31 does not return to the colony
trips_scop_gorluk <- subset(trips_scop_gorluk, trips_scop_gorluk$Returns == "Yes")

sumTrips <- tripSummary(trips = trips_scop_gorluk, colony = colony_scop_gorluk)
mapTrips(trips = trips_scop_gorluk, colony = colony_scop_gorluk)
## Trips colored by completeness. Indicate colorBy=='trip' to color by
##     trips.

# Srednji Lukovac
scop_sreluk <- filter(scop, colony_code == "SL") %>% 
  rename(DateTime = dttm,
         Latitude = latitude,
         Longitude = longitude,
         ID = bird_id)

colony_scop_sreluk <- filter(colony_data, colony_code == "SL") %>% 
  dplyr::select(Latitude = lat_colony,
                Longitude = lon_colony)

trips_scop_sreluk <- suppressWarnings(tripSplit(
  dataGroup = scop_sreluk,
  colony = colony_scop_sreluk,
  innerBuff = 3,
  returnBuff = 10,
  duration = 1,
  rmNonTrip = TRUE
))
## track 19_Tag40111_SL-3 (NEST FAILED)01 starts out on trip
## track 21_Tag41212_SL-601 starts out on trip
## track 21_Tag41233_SL-50 (NEST FAILED)01 starts out on trip
## track 21_Tag41262_SL-5 (2nd Parent)01 starts out on trip
## track 21_Tag41271_SL-7 (2nd Parent)01 starts out on trip
## track 21_Tag41334_SL-501 starts out on trip
## track 21_Tag41344_SL-801 starts out on trip
## track 21_Tag41344_SL-838 does not return to the colony
trips_scop_sreluk <- subset(trips_scop_sreluk, trips_scop_sreluk$Returns == "Yes")

sumTrips <- tripSummary(trips = trips_scop_sreluk, colony = colony_scop_sreluk)
mapTrips(trips = trips_scop_sreluk, colony = colony_scop_sreluk)
## Trips colored by completeness. Indicate colorBy=='trip' to color by
##     trips.

# processing of all scopoli tracks ----------------------------------------

trips_scop <- rbind(trips_scop_gorluk, trips_scop_sreluk, trips_scop_malmas)

tracks <- projectTracks(dataGroup = trips_scop, 
                        projType = 'azim', 
                        custom=TRUE )
## NOTE: projection is data specific
hVals <- findScale(
  tracks   = tracks,
  scaleARS = TRUE,
  sumTrips = sumTrips)
## No 'res' was specified. Movement scale in the data was compared
##         to a 500-cell grid with cell size of 1.036 km squared.

hVals
##   med_max_dist step_length  mag href scaleARS
## 1        26.66         1.8 3.28 9.38       15
tracks <- tracks[tracks$ColDist > 3, ] # remove trip start and end points near colony

KDE <-suppressWarnings(estSpaceUse(
  tracks = tracks, 
  scale = hVals$mag, 
  levelUD = 50, 
  polyOut = TRUE
))
## No grid resolution ('res') was specified, or the specified resolution was
##     >99 km and therefore ignored. Space use was calculated on a 500-cell grid,
##     with cells of 1.14 square km
mapKDE(KDE$UDPolygons)

repr <- repAssess(
  tracks    = tracks, 
  KDE       = KDE$KDE.Surface,
  levelUD   = 50,
  iteration = 1, 
  bootTable = FALSE)
## nls (non linear regression) successful, asymptote estimated for
##     bootstrap sample.