-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_acoustic_detections.R
More file actions
283 lines (267 loc) · 9.08 KB
/
Copy pathget_acoustic_detections.R
File metadata and controls
283 lines (267 loc) · 9.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#' Get acoustic detections data
#'
#' Get data for acoustic detections, with options to filter results. Use
#' `limit` to limit the number of returned records.
#'
#' @param credentials A list with the username and password to connect to the ETN database.
#' @param start_date Character. Start date (inclusive) in ISO 8601 format (
#' `yyyy-mm-dd`, `yyyy-mm` or `yyyy`).
#' @param end_date Character. End date (exclusive) in ISO 8601 format (
#' `yyyy-mm-dd`, `yyyy-mm` or `yyyy`).
#' @param acoustic_tag_id Character (vector). One or more acoustic tag ids.
#' @param animal_project_code Character (vector). One or more animal project
#' codes. Case-insensitive.
#' @param scientific_name Character (vector). One or more scientific names.
#' @param acoustic_project_code Character (vector). One or more acoustic
#' project codes. Case-insensitive.
#' @param receiver_id Character (vector). One or more receiver identifiers.
#' @param station_name Character (vector). One or more deployment station
#' names.
#' @param limit Logical. Limit the number of returned records to 100 (useful
#' for testing purposes). Defaults to `FALSE`.
#'
#' @return A tibble with acoustic detections data, sorted by `acoustic_tag_id`
#' and `date_time`. See also
#' [field definitions](https://inbo.github.io/etn/articles/etn_fields.html).
#'
#' @export
#'
#' @examples
#' \dontrun{
#' # Set default connection variable
#' con <- connect_to_etn()
#'
#' # Get limited sample of acoustic detections
#' get_acoustic_detections(con, limit = TRUE)
#'
#' # Get all acoustic detections from a specific animal project
#' get_acoustic_detections(con, animal_project_code = "2014_demer")
#'
#' # Get 2015 acoustic detections from that animal project
#' get_acoustic_detections(
#' con,
#' animal_project_code = "2014_demer",
#' start_date = "2015",
#' end_date = "2016",
#' )
#'
#' # Get April 2015 acoustic detections from that animal project
#' get_acoustic_detections(
#' con,
#' animal_project_code = "2014_demer",
#' start_date = "2015-04",
#' end_date = "2015-05",
#' )
#'
#' # Get April 24, 2015 acoustic detections from that animal project
#' get_acoustic_detections(
#' con,
#' animal_project_code = "2014_demer",
#' start_date = "2015-04-24",
#' end_date = "2015-04-25",
#' )
#'
#' # Get acoustic detections for a specific tag at two specific stations
#' get_acoustic_detections(
#' con,
#' acoustic_tag_id = "A69-1601-16130",
#' station_name = c("de-9", "de-10")
#' )
#'
#' # Get acoustic detections for a specific species, receiver and acoustic project
#' get_acoustic_detections(
#' con,
#' scientific_name = "Rutilus rutilus",
#' receiver_id = "VR2W-124070",
#' acoustic_project_code = "demer"
#' )
#' }
get_acoustic_detections <- function(credentials = list(
username = Sys.getenv("ETN_USER"),
password = Sys.getenv("ETN_PWD")
),
start_date = NULL,
end_date = NULL,
acoustic_tag_id = NULL,
animal_project_code = NULL,
scientific_name = NULL,
acoustic_project_code = NULL,
receiver_id = NULL,
station_name = NULL,
limit = FALSE) {
# Check if credentials object has right shape
check_credentials(credentials)
# Create connection object
connection <- connect_to_etn(credentials$username, credentials$password)
# Check if we can make a connection
check_connection(connection)
# Check start_date
if (is.null(start_date)) {
start_date_query <- "True"
} else {
start_date <- check_date_time(start_date, "start_date")
start_date_query <- glue::glue_sql("det.datetime >= {start_date}", .con = connection)
}
# Check end_date
if (is.null(end_date)) {
end_date_query <- "True"
} else {
end_date <- check_date_time(end_date, "end_date")
end_date_query <- glue::glue_sql("det.datetime < {end_date}", .con = connection)
}
# Check acoustic_tag_id
if (is.null(acoustic_tag_id)) {
acoustic_tag_id_query <- "True"
} else {
acoustic_tag_id <- check_value(
acoustic_tag_id,
list_acoustic_tag_ids(credentials),
"acoustic_tag_id"
)
acoustic_tag_id_query <- glue::glue_sql(
"det.transmitter IN ({acoustic_tag_id*})",
.con = connection
)
include_ref_tags <- TRUE
}
# Check animal_project_code
if (is.null(animal_project_code)) {
animal_project_code_query <- "True"
} else {
animal_project_code <- check_value(
animal_project_code,
list_animal_project_codes(credentials),
"animal_project_code",
lowercase = TRUE
)
animal_project_code_query <- glue::glue_sql(
"LOWER(animal_project_code) IN ({animal_project_code*})",
.con = connection
)
}
# Check scientific_name
if (is.null(scientific_name)) {
scientific_name_query <- "True"
} else {
scientific_name <- check_value(
scientific_name,
list_scientific_names(credentials),
"scientific_name"
)
scientific_name_query <- glue::glue_sql(
"animal_scientific_name IN ({scientific_name*})",
.con = connection
)
}
# Check acoustic_project_code
if (is.null(acoustic_project_code)) {
acoustic_project_code_query <- "True"
} else {
acoustic_project_code <- check_value(
acoustic_project_code,
list_acoustic_project_codes(credentials),
"acoustic_project_code",
lowercase = TRUE
)
acoustic_project_code_query <- glue::glue_sql(
"LOWER(network_project_code) IN ({acoustic_project_code*})",
.con = connection
)
}
# Check receiver_id
if (is.null(receiver_id)) {
receiver_id_query <- "True"
} else {
receiver_id <- check_value(
receiver_id,
list_receiver_ids(credentials),
"receiver_id"
)
receiver_id_query <- glue::glue_sql(
"det.receiver IN ({receiver_id*})",
.con = connection
)
}
# Check station_name
if (is.null(station_name)) {
station_name_query <- "True"
} else {
station_name <- check_value(
station_name,
list_station_names(credentials),
"station_name"
)
station_name_query <- glue::glue_sql(
"deployment_station_name IN ({station_name*})",
.con = connection
)
}
# Check limit
assertthat::assert_that(is.logical(limit), msg = "limit must be a logical: TRUE/FALSE.")
if (limit) {
limit_query <- glue::glue_sql("LIMIT 100", .con = connection)
} else {
limit_query <- glue::glue_sql("LIMIT ALL}", .con = connection)
}
acoustic_tag_id_sql <- glue::glue_sql(
readr::read_file(system.file("sql", "acoustic_tag_id.sql", package = "etnservice")),
.con = connection
)
# Build query
query <- glue::glue_sql("
SELECT
det.id_pk AS detection_id,
det.datetime AS date_time,
tag_serial_number AS tag_serial_number, -- exclusive to detections_limited
det.transmitter AS acoustic_tag_id,
animal_project_code AS animal_project_code, -- exclusive to detections_limited
animal_id_pk AS animal_id, -- exclusive to detections_limited
animal_scientific_name AS scientific_name, -- exclusive to detections_limited
network_project_code AS acoustic_project_code, -- exclusive to detections_limited
det.receiver AS receiver_id,
deployment_station_name AS station_name, -- exclusive to detections_limited
deployment_latitude AS deploy_latitude, -- exclusive to detections_limited
deployment_longitude AS deploy_longitude, -- exclusive to detections_limited
det.depth_in_meters AS depth_in_meters,
det.sensor_value AS sensor_value,
det.sensor_unit AS sensor_unit,
det.sensor2_value AS sensor2_value,
det.sensor2_unit AS sensor2_unit,
det.signal_to_noise_ratio AS signal_to_noise_ratio,
det.file AS source_file,
det.qc_flag AS qc_flag,
det.deployment_fk AS deployment_id
-- det.transmitter_name
-- det.transmitter_serial: via tag_device instead
-- det.station_name: deployment.station_name instead
-- det.latitude: deployment.deploy_lat instead
-- det.longitude: deployment.deploy_long instead
-- det.detection_file_id
-- det.receiver_serial
-- det.gain
-- external_id
FROM acoustic.detections_limited AS det
WHERE
{start_date_query}
AND {end_date_query}
AND {acoustic_tag_id_query}
AND {animal_project_code_query}
AND {scientific_name_query}
AND {acoustic_project_code_query}
AND {receiver_id_query}
AND {station_name_query}
{limit_query}
", .con = connection)
detections <- DBI::dbGetQuery(connection, query)
# Sort data (faster than in SQL)
detections <-
detections |>
dplyr::arrange(
factor(.data$acoustic_tag_id, levels = list_acoustic_tag_ids(credentials)),
.data$date_time
)
# Close connection
DBI::dbDisconnect(connection)
# Return detections
dplyr::as_tibble(detections)
}