Skip to content

Commit

Permalink
fixed read function for "Mote" data
Browse files Browse the repository at this point in the history
  • Loading branch information
dsjohnson committed Mar 1, 2024
1 parent 46c8c1f commit a1bf3ee
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 80 deletions.
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: crawlUtils
Type: Package
Title: Enhance And Integrate the {crawl} Package For Spatial Analysis Of Telemetry Output
Version: 0.1.47
Date: 2023-10-07
Version: 0.1.50
Date: 2024-03-01
Authors@R: c(
person("Devin S.", "Johnson", email = "devin.johnson@noaa.gov",role = c("aut", "cre")),
person("Josh", "London", email = "josh.london@noaa.gov", role = c("aut"))
Expand All @@ -15,12 +15,12 @@ Description: Utility functions to augment the the {crawl} package and integrate
License: CC0
Encoding: UTF-8
LazyData: true
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Roxygen: list(markdown = TRUE)
Imports: fuzzyjoin, dplyr, lubridate, magrittr, purrr,
sf, crawl, foreach, rmapshaper, nngeo, units, janitor, readr, ggplot2, mgcv,
smoothr, sfheaders, GPSeqClus, rlang, mclust
smoothr, sfheaders, rlang, mclust
Suggests: pathroutr, ctmm
Remotes:
jmlondon/pathroutr, dsjohnson/GPSeqClus@fixes
jmlondon/pathroutr
LinkingTo: Rcpp
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ import(mgcv, except = mvn)
import(sf)
import(smoothr)
import(units)
importFrom(GPSeqClus,GPSeq_clus)
importFrom(dplyr,group_by)
importFrom(dplyr,summarize)
importFrom(fuzzyjoin,fuzzy_left_join)
Expand Down
4 changes: 2 additions & 2 deletions R/crawlUtils-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#' \tabular{ll}{
#' Package: \tab crawlUtils\cr
#' Type: \tab Package\cr
#' Version: \tab 0.1.47\cr
#' Date: \tab October 7, 2023\cr
#' Version: \tab 0.1.50\cr
#' Date: \tab March 1, 2024\cr
#' License: \tab CC0 \cr
#' LazyLoad: \tab yes\cr
#' }
Expand Down
133 changes: 66 additions & 67 deletions R/cu_GPSeq_clus.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#' building, model application and field site investigations. Methods in Ecology
#' and Evolution, 12(5), 787-793.
#' @import sf dplyr
#' @importFrom GPSeqClus GPSeq_clus
#' @importFrom sfheaders sf_to_df
#' @importFrom rlang .data
#' @author Devin S. Johnson
Expand All @@ -31,70 +30,70 @@ cu_GPSeq_clus <- function(dat, search_radius_m, window_days, clus_min_locs = 2,
centroid_calc = "mean", show_plots = c(FALSE, "mean"), scale_plot_clus = TRUE,
store_plots = FALSE,
season_breaks_jul = NA, daylight_hrs = NA){

data <- AID <- TelemDate <- datetime <- y <- point_id <- sfg_id <- . <- .data <- x <- NULL

## Function to convert crw objects to GPSeqClus objects
crw_to_GPSeq <- function(dat){
if("x" %in% colnames(dat)) dat <- dplyr::rename(dat, x..1=.data[["x"]])
if("y" %in% colnames(dat)) dat <- dplyr::rename(dat, y..1=.data[["y"]])
dat <- st_transform(dat, 4326) |> sfheaders::sf_to_df(fill=TRUE) |> dplyr::select(-sfg_id, -point_id) |>
dplyr::rename(Long=x, Lat=y, TelemDate=datetime)
if("x..1" %in% colnames(dat)) dat <- dplyr::rename(dat, x=.data[["x..1"]])
if("y..1" %in% colnames(dat)) dat <- dplyr::rename(dat, y=.data[["y..1"]])
dat$AID <- 1
return(dat)
}

## function to execute clustering
ex_GPSeq_clus <- function(dat,search_radius_m=search_radius_m, window_days=window_days, clus_min_locs=clus_min_locs,
centroid_calc=centroid_calc, show_plots=show_plots, scale_plot_clus=scale_plot_clus,
store_plots=store_plots, season_breaks_jul=season_breaks_jul, daylight_hrs=daylight_hrs){
x_type <- attr(dat, "crw_type")
x_crs <- st_crs(dat)
dat <- crw_to_GPSeq(dat)
clus <- try(suppressWarnings(
GPSeqClus::GPSeq_clus(dat=dat,search_radius_m=search_radius_m, window_days=window_days, clus_min_locs=clus_min_locs,
centroid_calc=centroid_calc, show_plots=show_plots, scale_plot_clus=scale_plot_clus,
store_plots=store_plots,
season_breaks_jul=season_breaks_jul, daylight_hrs=daylight_hrs)
), silent=TRUE)
if(!inherits(clus,"try-error")){
clus <- clus[[1]] |> rename(datetime=TelemDate) |> st_as_sf(coords=c("Long","Lat"), crs=4326) |>
st_transform(x_crs)
} else {
clus <- dat
clus <- clus |> rename(datetime=TelemDate) |> st_as_sf(coords=c("Long","Lat"), crs=4326) |>
st_transform(x_crs)
warning("Clustering failed, returning original data. Try different 'search_radius_m' values")
}
clus <- select(clus, -AID)
attr(clus, "crw_type") <- x_type
return(clus)
}

## Main portion of function
x_type <- attr(dat, "crw_type")
if(inherits(dat, "list")){
if(!all(sapply(dat, attr, "crw_type")=="crwIS_sf")) stop("The 'dat' argument is not the correct form!")
x_type <- "crwIS_sf_list"
}
if(! x_type %in% c("crwIS_sf_list","crwIS_sf","crwPredict_sf")) stop("The 'dat' argument is not the correct form!")
if(x_type %in% c("crwIS_sf","crwPredict_sf")){
dat <- ex_GPSeq_clus(dat, search_radius_m=search_radius_m, window_days=window_days,
clus_min_locs=clus_min_locs,centroid_calc=centroid_calc,
show_plots=show_plots, scale_plot_clus=scale_plot_clus,
store_plots=store_plots, season_breaks_jul=season_breaks_jul,
daylight_hrs=daylight_hrs)
} else{
for(i in 1:length(dat)){
dat[[i]] <- ex_GPSeq_clus(dat[[i]], search_radius_m=search_radius_m, window_days=window_days,
clus_min_locs=clus_min_locs,centroid_calc=centroid_calc,
show_plots=show_plots, scale_plot_clus=scale_plot_clus,
store_plots=store_plots, season_breaks_jul=season_breaks_jul,
daylight_hrs=daylight_hrs)
}
attr(dat, "crw_type") <- "crwIS_sf_list"
}
return(dat)
return("This function has been removed")
# data <- AID <- TelemDate <- datetime <- y <- point_id <- sfg_id <- . <- .data <- x <- NULL
#
# ## Function to convert crw objects to GPSeqClus objects
# crw_to_GPSeq <- function(dat){
# if("x" %in% colnames(dat)) dat <- dplyr::rename(dat, x..1=.data[["x"]])
# if("y" %in% colnames(dat)) dat <- dplyr::rename(dat, y..1=.data[["y"]])
# dat <- st_transform(dat, 4326) |> sfheaders::sf_to_df(fill=TRUE) |> dplyr::select(-sfg_id, -point_id) |>
# dplyr::rename(Long=x, Lat=y, TelemDate=datetime)
# if("x..1" %in% colnames(dat)) dat <- dplyr::rename(dat, x=.data[["x..1"]])
# if("y..1" %in% colnames(dat)) dat <- dplyr::rename(dat, y=.data[["y..1"]])
# dat$AID <- 1
# return(dat)
# }
#
# ## function to execute clustering
# ex_GPSeq_clus <- function(dat,search_radius_m=search_radius_m, window_days=window_days, clus_min_locs=clus_min_locs,
# centroid_calc=centroid_calc, show_plots=show_plots, scale_plot_clus=scale_plot_clus,
# store_plots=store_plots, season_breaks_jul=season_breaks_jul, daylight_hrs=daylight_hrs){
# x_type <- attr(dat, "crw_type")
# x_crs <- st_crs(dat)
# dat <- crw_to_GPSeq(dat)
# clus <- try(suppressWarnings(
# GPSeqClus::GPSeq_clus(dat=dat,search_radius_m=search_radius_m, window_days=window_days, clus_min_locs=clus_min_locs,
# centroid_calc=centroid_calc, show_plots=show_plots, scale_plot_clus=scale_plot_clus,
# store_plots=store_plots,
# season_breaks_jul=season_breaks_jul, daylight_hrs=daylight_hrs)
# ), silent=TRUE)
# if(!inherits(clus,"try-error")){
# clus <- clus[[1]] |> rename(datetime=TelemDate) |> st_as_sf(coords=c("Long","Lat"), crs=4326) |>
# st_transform(x_crs)
# } else {
# clus <- dat
# clus <- clus |> rename(datetime=TelemDate) |> st_as_sf(coords=c("Long","Lat"), crs=4326) |>
# st_transform(x_crs)
# warning("Clustering failed, returning original data. Try different 'search_radius_m' values")
# }
# clus <- select(clus, -AID)
# attr(clus, "crw_type") <- x_type
# return(clus)
# }
#
# ## Main portion of function
# x_type <- attr(dat, "crw_type")
# if(inherits(dat, "list")){
# if(!all(sapply(dat, attr, "crw_type")=="crwIS_sf")) stop("The 'dat' argument is not the correct form!")
# x_type <- "crwIS_sf_list"
# }
# if(! x_type %in% c("crwIS_sf_list","crwIS_sf","crwPredict_sf")) stop("The 'dat' argument is not the correct form!")
# if(x_type %in% c("crwIS_sf","crwPredict_sf")){
# dat <- ex_GPSeq_clus(dat, search_radius_m=search_radius_m, window_days=window_days,
# clus_min_locs=clus_min_locs,centroid_calc=centroid_calc,
# show_plots=show_plots, scale_plot_clus=scale_plot_clus,
# store_plots=store_plots, season_breaks_jul=season_breaks_jul,
# daylight_hrs=daylight_hrs)
# } else{
# for(i in 1:length(dat)){
# dat[[i]] <- ex_GPSeq_clus(dat[[i]], search_radius_m=search_radius_m, window_days=window_days,
# clus_min_locs=clus_min_locs,centroid_calc=centroid_calc,
# show_plots=show_plots, scale_plot_clus=scale_plot_clus,
# store_plots=store_plots, season_breaks_jul=season_breaks_jul,
# daylight_hrs=daylight_hrs)
# }
# attr(dat, "crw_type") <- "crwIS_sf_list"
# }
# return(dat)
}
17 changes: 14 additions & 3 deletions R/cu_read_wc_dirs.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,27 @@ cu_read_wc_dirs <- function(x, remove_duplicates=TRUE){
# Determine which file to load for each animal:
dirs <- list.dirs(x)[-1]
nms1 <- paste0(list.dirs(x, full.names=FALSE)[-1],
"-1-Locations.csv"
"-2-Locations.csv"
)
loc_file1 <- paste(dirs, nms1, sep="/")
nms2 <- paste0(list.dirs(x, full.names=FALSE)[-1],
"-Locations.csv"
"-1-Locations.csv"
)
loc_file2 <- paste(dirs, nms2, sep="/")
nms3 <- paste0(list.dirs(x, full.names=FALSE)[-1],
"-Locations.csv"
)
loc_file3 <- paste(dirs, nms3, sep="/")


# Container for file names
loc_file <- ifelse(file.exists(loc_file1), loc_file1, loc_file2)
if(file.exists(loc_file1)){
loc_file <- loc_file1
} else if(file.exists(loc_file2)){
loc_file <- loc_file2
} else{
loc_file <- loc_file3
}

# Read in data and combine into single table
# There are 2 animals with no location data
Expand Down
4 changes: 2 additions & 2 deletions man/crawlUtils-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a1bf3ee

Please sign in to comment.