Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add write_dwc() function #257

Merged
merged 39 commits into from
Dec 9, 2022
Merged
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
5696439
Update RoxygenNote
peterdesmet Nov 21, 2022
1896ae6
Start write_dwc()
peterdesmet Nov 21, 2022
76ba59c
Rename zzz.R to utils.R
peterdesmet Nov 21, 2022
578a79a
Rewrite check_value() so it can return (lowercase) value
peterdesmet Nov 21, 2022
266d18f
Start SQL for human obs
peterdesmet Nov 22, 2022
882b930
Update write_dwc() to download human obs
peterdesmet Nov 22, 2022
38e597d
Update download message
peterdesmet Nov 22, 2022
673cd50
Order table and control sex
peterdesmet Nov 22, 2022
83f8df1
Rename sql (combine human + machine into one)
peterdesmet Nov 22, 2022
dd9f242
Shorten license codes
peterdesmet Nov 22, 2022
f8f62a3
Get (non-aggregated) detections
peterdesmet Nov 22, 2022
16a7bf5
Select animals in WITH (drop project_id)
peterdesmet Nov 22, 2022
8f32105
Sample down detections (fast, without count)
peterdesmet Nov 23, 2022
7906dec
Use static `dataGeneralizations`
peterdesmet Nov 23, 2022
a65ebe1
Add family helper functions
peterdesmet Nov 28, 2022
f150e05
Document write_dwc()
peterdesmet Nov 28, 2022
2f7551b
Use `EPSG:4326`
peterdesmet Nov 28, 2022
e9fdbce
Only add lifeStage to release
peterdesmet Nov 28, 2022
bd53e62
Only set lifeStage at release
peterdesmet Nov 28, 2022
a5adc9c
Include file paths in messages
peterdesmet Nov 29, 2022
49aa462
Use CC0 as default license (recommended)
peterdesmet Nov 29, 2022
d3d15f7
Set IMIS title as dataset name
peterdesmet Nov 29, 2022
a643dff
Include count of detections (slower)
peterdesmet Nov 29, 2022
ad72142
Include write_dwc in function list
peterdesmet Nov 29, 2022
30070ca
Highlight downsampling
peterdesmet Nov 29, 2022
e0cda33
Remove ELSE NULL
peterdesmet Nov 29, 2022
5a7d89b
Use acoustic telemetry as samplingProtocol
peterdesmet Nov 29, 2022
80b89d6
Set coordinateUncertainty for detections at 1000
peterdesmet Nov 30, 2022
c56d23f
Control lifestage values
peterdesmet Dec 2, 2022
5841ab7
Set coordinateUncertainty
peterdesmet Dec 2, 2022
de21266
Disable code blocks in etn_fields
peterdesmet Dec 2, 2022
18404e6
remove collapse_transformer: unused
PietrH Dec 7, 2022
7df3608
Use expect_identical()
peterdesmet Dec 9, 2022
387c618
Set CC-BY as default license
peterdesmet Dec 9, 2022
5134c23
Allow to return object rather than files
peterdesmet Dec 9, 2022
cf186e1
Add write_dwc tests
peterdesmet Dec 9, 2022
f1125f0
Preserve case of DwC terms in returned data
peterdesmet Dec 9, 2022
3dde8f1
Run devtools::document()
peterdesmet Dec 9, 2022
7908c69
Bump version and build site
peterdesmet Dec 9, 2022
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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Imports:
DBI,
dplyr,
glue,
jsonlite,
lubridate,
methods,
odbc,
Expand All @@ -42,4 +43,4 @@ LazyData: true
Encoding: UTF-8
VignetteBuilder: knitr
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
RoxygenNote: 7.2.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export(list_station_names)
export(list_tag_ids)
export(list_tag_serial_numbers)
export(list_values)
export(write_dwc)
importFrom(dplyr,"%>%")
importFrom(dplyr,.data)
importFrom(dplyr,distinct)
Expand Down
40 changes: 20 additions & 20 deletions R/download_acoustic_dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,15 @@
#'
#' # Download data for the 2012_leopoldkanaal animal project (all scientific names)
#' download_acoustic_dataset(animal_project_code = "2012_leopoldkanaal")
#' #> Downloading data to directory "2012_leopoldkanaal":
#' #> (existing files with the same name will be overwritten)
#' #> Downloading data to directory `2012_leopoldkanaal`:
#' #> * (1/6): downloading animals.csv
#' #> * (2/6): downloading tags.csv
#' #> * (3/6): downloading detections.csv
#' #> * (4/6): downloading deployments.csv
#' #> * (5/6): downloading receivers.csv
#' #> * (6/6): adding datapackage.json as file metadata

#' #> Summary statistics for dataset "2012_leopoldkanaal":
#' #> Summary statistics for dataset `2012_leopoldkanaal`:
#' #> * number of animals: 104
#' #> * number of tags: 103
#' #> * number of detections: 2215243
Expand All @@ -85,28 +84,29 @@ download_acoustic_dataset <- function(connection = con,
check_connection(connection)

# Check animal_project_code
if (missing(animal_project_code) || is.null(animal_project_code)) {
stop("Please provide an animal_project_code")
} else {
animal_project_code <- tolower(animal_project_code)
valid_animal_project_codes <- tolower(list_animal_project_codes(connection))
check_value(animal_project_code, valid_animal_project_codes, "animal_project_code")
}
assertthat::assert_that(
length(animal_project_code) == 1,
msg = "`animal_project_code` must be a single value."
)
animal_project_code <- check_value(
animal_project_code,
list_animal_project_codes(connection),
"animal_project_code",
lowercase = TRUE
)

# Check scientific_name
if (!is.null(scientific_name)) {
valid_scientific_names <- list_scientific_names(connection)
check_value(scientific_name, valid_scientific_names, "scientific_name")
scientific_name <- check_value(
scientific_name,
list_scientific_names(connection),
"scientific_name"
)
}

# Check directory
dir.create(directory, recursive = TRUE, showWarnings = FALSE)

# Start downloading
message(glue::glue(
"Downloading data to directory \"{directory}\":
(existing files with the same name will be overwritten)"
))
dir.create(directory, recursive = TRUE, showWarnings = FALSE)
message(glue::glue("Downloading data to directory `{directory}`:"))

# ANIMALS
message("* (1/6): downloading animals.csv")
Expand Down Expand Up @@ -199,7 +199,7 @@ download_acoustic_dataset <- function(connection = con,
sort()

message("")
message(glue::glue("\nSummary statistics for dataset \"{animal_project_code}\":"))
message(glue::glue("\nSummary statistics for dataset `{animal_project_code}`:"))
message("* number of animals: ", nrow(animals))
message("* number of tags: ", nrow(tags))
message("* number of detections: ", nrow(detections))
Expand Down
30 changes: 21 additions & 9 deletions R/get_acoustic_deployments.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ get_acoustic_deployments <- function(connection = con,
if (is.null(deployment_id)) {
deployment_id_query <- "True"
} else {
valid_deployment_ids <- list_deployment_ids(connection)
check_value(deployment_id, valid_deployment_ids, "receiver_id")
deployment_id <- check_value(
deployment_id,
list_deployment_ids(connection),
"receiver_id"
)
deployment_id_query <- glue::glue_sql(
"dep.id_pk IN ({deployment_id*})",
.con = connection
Expand All @@ -65,8 +68,11 @@ get_acoustic_deployments <- function(connection = con,
if (is.null(receiver_id)) {
receiver_id_query <- "True"
} else {
valid_receiver_ids <- list_receiver_ids(connection)
check_value(receiver_id, valid_receiver_ids, "receiver_id")
receiver_id <- check_value(
receiver_id,
list_receiver_ids(connection),
"receiver_id"
)
receiver_id_query <- glue::glue_sql(
"receiver.receiver IN ({receiver_id*})",
.con = connection
Expand All @@ -77,9 +83,12 @@ get_acoustic_deployments <- function(connection = con,
if (is.null(acoustic_project_code)) {
acoustic_project_code_query <- "True"
} else {
acoustic_project_code <- tolower(acoustic_project_code)
valid_acoustic_project_codes <- tolower(list_acoustic_project_codes(connection))
check_value(acoustic_project_code, valid_acoustic_project_codes, "acoustic_project_code")
acoustic_project_code <- check_value(
acoustic_project_code,
list_acoustic_project_codes(connection),
"acoustic_project_code",
lowercase = TRUE
)
acoustic_project_code_query <- glue::glue_sql(
"LOWER(network_project.projectcode) IN ({acoustic_project_code*})",
.con = connection
Expand All @@ -90,8 +99,11 @@ get_acoustic_deployments <- function(connection = con,
if (is.null(station_name)) {
station_name_query <- "True"
} else {
valid_station_names <- list_station_names(connection)
check_value(station_name, valid_station_names, "station_name")
station_name <- check_value(
station_name,
list_station_names(connection),
"station_name"
)
station_name_query <- glue::glue_sql(
"dep.station_name IN ({station_name*})",
.con = connection
Expand Down
46 changes: 32 additions & 14 deletions R/get_acoustic_detections.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,11 @@ get_acoustic_detections <- function(connection = con,
if (is.null(acoustic_tag_id)) {
acoustic_tag_id_query <- "True"
} else {
valid_acoustic_tag_ids <- list_acoustic_tag_ids(connection)
check_value(acoustic_tag_id, valid_acoustic_tag_ids, "acoustic_tag_id")
acoustic_tag_id <- check_value(
acoustic_tag_id,
list_acoustic_tag_ids(connection),
"acoustic_tag_id"
)
acoustic_tag_id_query <- glue::glue_sql(
"det.transmitter IN ({acoustic_tag_id*})",
.con = connection
Expand All @@ -120,9 +123,12 @@ get_acoustic_detections <- function(connection = con,
if (is.null(animal_project_code)) {
animal_project_code_query <- "True"
} else {
animal_project_code <- tolower(animal_project_code)
valid_animal_project_codes <- tolower(list_animal_project_codes(connection))
check_value(animal_project_code, valid_animal_project_codes, "animal_project_code")
animal_project_code <- check_value(
animal_project_code,
list_animal_project_codes(connection),
"animal_project_code",
lowercase = TRUE
)
animal_project_code_query <- glue::glue_sql(
"LOWER(animal_project_code) IN ({animal_project_code*})",
.con = connection
Expand All @@ -133,8 +139,11 @@ get_acoustic_detections <- function(connection = con,
if (is.null(scientific_name)) {
scientific_name_query <- "True"
} else {
valid_scientific_name_ids <- list_scientific_names(connection)
check_value(scientific_name, valid_scientific_name_ids, "scientific_name")
scientific_name <- check_value(
scientific_name,
list_scientific_names(connection),
"scientific_name"
)
scientific_name_query <- glue::glue_sql(
"animal_scientific_name IN ({scientific_name*})",
.con = connection
Expand All @@ -145,9 +154,12 @@ get_acoustic_detections <- function(connection = con,
if (is.null(acoustic_project_code)) {
acoustic_project_code_query <- "True"
} else {
acoustic_project_code <- tolower(acoustic_project_code)
valid_acoustic_project_codes <- tolower(list_acoustic_project_codes(connection))
check_value(acoustic_project_code, valid_acoustic_project_codes, "acoustic_project_code")
acoustic_project_code <- check_value(
acoustic_project_code,
list_acoustic_project_codes(connection),
"acoustic_project_code",
lowercase = TRUE
)
acoustic_project_code_query <- glue::glue_sql(
"LOWER(network_project_code) IN ({acoustic_project_code*})",
.con = connection
Expand All @@ -158,8 +170,11 @@ get_acoustic_detections <- function(connection = con,
if (is.null(receiver_id)) {
receiver_id_query <- "True"
} else {
valid_receiver_ids <- list_receiver_ids(connection)
check_value(receiver_id, valid_receiver_ids, "receiver_id")
receiver_id <- check_value(
receiver_id,
list_receiver_ids(connection),
"receiver_id"
)
receiver_id_query <- glue::glue_sql(
"det.receiver IN ({receiver_id*})",
.con = connection
Expand All @@ -170,8 +185,11 @@ get_acoustic_detections <- function(connection = con,
if (is.null(station_name)) {
station_name_query <- "True"
} else {
valid_station_names <- list_station_names(connection)
check_value(station_name, valid_station_names, "station_name")
station_name <- check_value(
station_name,
list_station_names(connection),
"station_name"
)
station_name_query <- glue::glue_sql(
"deployment_station_name IN ({station_name*})",
.con = connection
Expand Down
9 changes: 6 additions & 3 deletions R/get_acoustic_projects.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ get_acoustic_projects <- function(connection = con,
if (is.null(acoustic_project_code)) {
acoustic_project_code_query <- "True"
} else {
acoustic_project_code <- tolower(acoustic_project_code)
valid_acoustic_project_codes <- tolower(list_acoustic_project_codes(connection))
check_value(acoustic_project_code, valid_acoustic_project_codes, "acoustic_project_code")
acoustic_project_code <- check_value(
acoustic_project_code,
list_acoustic_project_codes(connection),
"acoustic_project_code",
lowercase = TRUE
)
acoustic_project_code_query <- glue::glue_sql(
"LOWER(project.project_code) IN ({acoustic_project_code*})",
.con = connection
Expand Down
9 changes: 6 additions & 3 deletions R/get_animal_projects.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ get_animal_projects <- function(connection = con,
if (is.null(animal_project_code)) {
animal_project_code_query <- "True"
} else {
animal_project_code <- tolower(animal_project_code)
valid_animal_project_codes <- tolower(list_animal_project_codes(connection))
check_value(animal_project_code, valid_animal_project_codes, "animal_project_code")
animal_project_code <- check_value(
animal_project_code,
list_animal_project_codes(connection),
"animal_project_code",
lowercase = TRUE
)
animal_project_code_query <- glue::glue_sql(
"LOWER(project.project_code) IN ({animal_project_code*})",
.con = connection
Expand Down
31 changes: 21 additions & 10 deletions R/get_animals.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ get_animals <- function(connection = con,
if (is.null(animal_id)) {
animal_id_query <- "True"
} else {
valid_animal_ids <- list_animal_ids(connection)
check_value(animal_id, valid_animal_ids, "animal_id")
animal_id <- check_value(
animal_id,
list_animal_ids(connection),
"animal_id"
)
animal_id_query <- glue::glue_sql(
"animal.id_pk IN ({animal_id*})",
.con = connection
Expand All @@ -66,9 +69,12 @@ get_animals <- function(connection = con,
if (is.null(animal_project_code)) {
animal_project_code_query <- "True"
} else {
animal_project_code <- tolower(animal_project_code)
valid_animal_project_codes <- tolower(list_animal_project_codes(connection))
check_value(animal_project_code, valid_animal_project_codes, "animal_project_code")
animal_project_code <- check_value(
animal_project_code,
list_animal_project_codes(connection),
"animal_project_code",
lowercase = TRUE
)
animal_project_code_query <- glue::glue_sql(
"LOWER(animal_project.projectcode) IN ({animal_project_code*})",
.con = connection
Expand All @@ -79,9 +85,11 @@ get_animals <- function(connection = con,
if (is.null(tag_serial_number)) {
tag_serial_number_query <- "True"
} else {
valid_tag_serial_numbers <- list_tag_serial_numbers(connection)
tag_serial_number <- as.character(tag_serial_number) # Cast to character
check_value(tag_serial_number, valid_tag_serial_numbers, "tag_serial_number")
tag_serial_number <- check_value(
as.character(tag_serial_number), # Cast to character
list_tag_serial_numbers(connection),
"tag_serial_number"
)
tag_serial_number_query <- glue::glue_sql(
"tag.tag_serial_number IN ({tag_serial_number*})",
.con = connection
Expand All @@ -92,8 +100,11 @@ get_animals <- function(connection = con,
if (is.null(scientific_name)) {
scientific_name_query <- "True"
} else {
valid_scientific_names <- list_scientific_names(connection)
check_value(scientific_name, valid_scientific_names, "scientific_name")
scientific_name <- check_value(
scientific_name,
list_scientific_names(connection),
"scientific_name"
)
scientific_name_query <- glue::glue_sql(
"animal.scientific_name IN ({scientific_name*})",
.con = connection
Expand Down
9 changes: 6 additions & 3 deletions R/get_cpod_projects.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ get_cpod_projects <- function(connection = con,
if (is.null(cpod_project_code)) {
cpod_project_code_query <- "True"
} else {
cpod_project_code <- tolower(cpod_project_code)
valid_cpod_project_codes <- tolower(list_cpod_project_codes(connection))
check_value(cpod_project_code, valid_cpod_project_codes, "cpod_project_code")
cpod_project_code <- check_value(
cpod_project_code,
list_cpod_project_codes(connection),
"cpod_project_code",
lowercase = TRUE
)
cpod_project_code_query <- glue::glue_sql(
"LOWER(project.project_code) IN ({cpod_project_code*})",
.con = connection
Expand Down
Loading