Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions R/ato_ani_from_glatos.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
ato_ani_from_glatos <- function(glatos_file) {
# Load the data file we've been given- probably detections data since published glatos data will only have detections and receivers.
glatos_data <- load_file(glatos_file)


#Glatos detection data doesn't store releases the same way we do, i.e, each release having its own distinct row, but release/capture info is included in each detection, so we can narrow the table down to only unique animal IDs
#and use that table.
unique_animals <- distinct(glatos_data, animal_id, utc_release_date_time, .keep_all=TRUE)

# We don't really have the option here to get this from an 'animal metadata' file so we're always going to be deriving.
ani <- make_ani(
animal = as.character(glatos_data$animal_id),
capture_location = as.character(glatos_data$capture_location),
capture_datetime = as.POSIXct(glatos_data$glatos_caught_date),
animal = as.character(unique_animals$animal_id),
capture_location = as.character(unique_animals$capture_location),
capture_datetime = as.POSIXct(NA_real_),
capture_lat = NA_real_,
capture_lon = NA_real_,
release_loaction = glatos_data$release_location,
release_datetime = as.POSIXct(glatos_data$utc_release_date_time),
release_lat = glatos_data$release_latitude,
release_lon = glatos_data$release_longitude,
release_location = unique_animals$release_location,
release_datetime = as.POSIXct(unique_animals$utc_release_date_time),
release_lat = as.numeric(unique_animals$release_latitude),
release_lon = as.numeric(unique_animals$release_longitude),
tz = "UTC"
)

Expand Down
2 changes: 1 addition & 1 deletion R/ato_ani_from_glatos_workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ato_ani_from_glatos_workbook <- function(glatos_data) {
ani <- make_ani(
animal = as.character(glatos_data$animal_id),
capture_location = as.character(glatos_data$capture_location),
capture_datetime = as.POSIXct(glatos_data$glatos_caught_date),
capture_datetime = as.POSIXct(NA_real_),
capture_lat = glatos_data$capture_latitude,
capture_lon = glatos_data$capture_longitude,
release_location = glatos_data$release_location,
Expand Down
13 changes: 8 additions & 5 deletions R/ato_dep_from_glatos.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ ato_dep_from_glatos <- function(glatos_file, glatos_detection_data = "", type =
# This we can pull directly from the metadata.
dep <- make_dep(
receiver_model = glatos_data$ins_model_no,
receiver_serial = glatos_data$ins_serial_no,
receiver_serial = as.character(glatos_data$ins_serial_no),
receiver_codeset = glatos_data$code_map,
deploy_location = glatos_data$station,
deploy_datetime = as.POSIXct(glatos_data$deploy_date_time),
Expand All @@ -27,7 +27,7 @@ ato_dep_from_glatos <- function(glatos_file, glatos_detection_data = "", type =
transmitter_manufacturer = NA_character_, # ???
transmitter_ping_rate = as.numeric(glatos_data$glatos_ins_frequency), # is this accurate? I think this mapping is right.
transmitter_model = NA_character_, # ???
transmitter_serial = NA_integer_
transmitter_serial = NA_character_
)
return(dep)
} else if (type == "extract") {
Expand Down Expand Up @@ -95,7 +95,7 @@ ato_dep_from_glatos <- function(glatos_file, glatos_detection_data = "", type =

dep <- make_dep(
receiver_model = NA_character_,
receiver_serial = as.integer(rcvr_grouped$receiver_sn),
receiver_serial = as.character(rcvr_grouped$receiver_sn),
receiver_codeset = NA_character_,
deploy_location = rcvr_grouped$station,
deploy_datetime = as.POSIXct(rcvr_grouped$minDetectionDate),
Expand All @@ -104,9 +104,12 @@ ato_dep_from_glatos <- function(glatos_file, glatos_detection_data = "", type =
recover_datetime = as.POSIXct(rcvr_grouped$maxDetectionDate),
recover_lat = NA_real_,
recover_lon = NA_real_,
transmitter = paste(rcvr_grouped$transmitter_codespace, "-", rcvr_grouped$transmitter_id, sep = ""),
#A mistake on my part, this info represents the TAG, not the potential transceiver.
#transmitter = paste(rcvr_grouped$transmitter_codespace, "-", rcvr_grouped$transmitter_id, sep = ""),
transmitter = NA_character_,
transmitter_model = NA_character_,
transmitter_serial = rcvr_grouped$transmitter_id,
#transmitter_serial = as.character(rcvr_grouped$transmitter_id),
transmitter_serial = NA_character_,
tz = "UTC"
)

Expand Down
3 changes: 2 additions & 1 deletion R/ato_obs_from_glatos_workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ ato_obs_from_glatos_workbook <- function(glatos_data) {

obs <- make_obs(
animal = glatos_data_filtered$animal_id,
transmitter = paste(glatos_data_filtered$tag_code_space, "-", glatos_data_filtered$tag_id_code),
#Commented out since the data we have is terminal for the animal but not the transmitter, and including the transmitter here is causing an error.
#transmitter = paste0(glatos_data_filtered$tag_code_space, "-", glatos_data_filtered$tag_id_code),
#Not sure how to engineer type out of this.
terminal = TRUE,
location = "Not supplied in data", #The field is mandatory but glatos doesn't require people to file the capture location, so we don't have the data.
Expand Down
8 changes: 4 additions & 4 deletions R/ato_tag_from_otn.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ ato_tag_from_otn <- function(otn_file, type = "meta") {
# Read in the file we've been given if we haven't been handed a dataframe.
if (!is.data.frame(otn_file)) {
# Grab the extension.
extension <- tools::file_ext(otn_detections)
extension <- tools::file_ext(otn_file)
# If it's a parquet, read it in as one...
if (extension == "parquet") {
otn_file <- read_parquet(otn_detections)
otn_file <- read_parquet(otn_file)
} else if (extension == "xlsx" || extension == "xls") {
otn_file <- read_excel(otn_detections)
otn_file <- read_excel(otn_file)
} else {
# Otherwise bring it in as a CSV.
otn_file <- read.csv(otn_detections, na = c("", "null", "NA"))
otn_file <- read.csv(otn_file, na = c("", "null", "NA"))
}
}

Expand Down
58 changes: 33 additions & 25 deletions R/glatos_to_ato.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@

glatos_to_ato <- function(glatos_detections, glatos_receivers = "", glatos_workbook = "") {
glatos_detections <- load_file(glatos_detections)
glatos_deployments <- NULL
glatos_animals <- NULL

#If we have the workbook, load that into something we can use with the glatos reading functions.
# First we'll read in the workbook using the glatos package. One day this functionality may live inside surimi, but not yet.
glatos_list <- read_glatos_workbook(glatos_workbook)

# Now we have a list containing 'metadata', 'animals', and 'receivers.' We really only need animals and receivers, so we'll split it up and get those.
glatos_animals <- glatos_list$animals

#I know this rename might be a little confusing but since someone might optionally hand in a receiver metadata sheet, I have to use different wording so that they don't get mixed up.
glatos_deployments <- glatos_list$receivers
if(glatos_workbook != "") {
glatos_list <- read_glatos_workbook(glatos_workbook)

# Now we have a list containing 'metadata', 'animals', and 'receivers.' We really only need animals and receivers, so we'll split it up and get those.
glatos_animals <- glatos_list$animals

#I know this rename might be a little confusing but since someone might optionally hand in a receiver metadata sheet, I have to use different wording so that they don't get mixed up.
glatos_deployments <- glatos_list$receivers

#temporarily dealing with some NaN values while I test.
glatos_deployments <- drop_na(glatos_deployments, any_of(c("recover_date_time", "deploy_date_time")))
}

#temporarily dealing with some NaN values while I test.
glatos_deployments <- drop_na(glatos_deployments, any_of(c("recover_date_time", "deploy_date_time")))

# Now we have a dataframe we can start loading into an ATO object. Let's make an instance of the object.
GLATOS_ATO <- init_ato()

# Make the "detections" object,
det <- make_det(
datetime = as.POSIXct(glatos_detections$detection_timestamp_utc),
Expand All @@ -46,8 +47,6 @@ glatos_to_ato <- function(glatos_detections, glatos_receivers = "", glatos_workb
tz = "UTC"
)

GLATOS_ATO <- set_det(GLATOS_ATO, det)

# I used to have a 'derive' argument as in some of the original OTN-to-IMOS functions but then I realised it was safer to just
# automatically try to derive receiver metadata from the extract if no file is supplied.
dep <- ""
Expand All @@ -63,27 +62,36 @@ glatos_to_ato <- function(glatos_detections, glatos_receivers = "", glatos_workb
dep <- ato_dep_from_glatos(glatos_detections, type = "extract")
}

GLATOS_ATO <- set_dep(GLATOS_ATO, dep)

# Tag information is not part of what's distributed in glatos publications so we can't derive it, but if someone has their workbook handy, we can use that to get tag data.
if(!is.null(glatos_animals)) {
tag <- ato_tag_from_glatos_workbook(glatos_animals)
GLATOS_ATO <- set_tag(GLATOS_ATO, tag)

#We also need to create observations, which we can do from the glatos_animals information since it records whether or not the tag has been captured and recovered.
obs <- ato_obs_from_glatos_workbook(glatos_animals)
GLATOS_ATO <- set_obs(GLATOS_ATO, obs)
}

#Finally we can get animal info from either the workbook, if provided, or partially from the detections themselves.
if(!is.null(glatos_animals)) {

#We can also get animal information from the glatos workbook, so if we have that, then let's do it.
ani <- ato_ani_from_glatos_workbook(glatos_animals)

#If we can do all of those, then we can make and return an appropriately-filled ATO with all of our data.
GLATOS_ATO <- init_ato(
det = det,
dep = dep,
ani = ani,
tag = tag,
obs = obs
)
}

#If we don't have the workbook then we will just have to make what animal data we can from the extract and call it a day. Tag and Obs
#Won't get created and passed in.
else {
ani <- ato_ani_from_glatos(glatos_detections)
GLATOS_ATO <- init_ato(
det = det,
dep = dep,
ani = ani
)
}

GLATOS_ATO <- set_ani(GLATOS_ATO, ani)

return(GLATOS_ATO)
}
Loading