-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_bird_data_from_WildTrax.R
96 lines (90 loc) · 3.74 KB
/
fetch_bird_data_from_WildTrax.R
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
# ---
# title: "Gather Bird Data from WildTrax"
# author: "Brendan Casey"
# created: "2024-01-13"
# description: >
# "This script is designed to fetch bird data from the
# WildTrax platform. It logs into WildTrax using
# credentials stored in a user config file. The script
# proceeds to fetch all data by sensor type: Point count
# and ARU. The output of this module is two .rData files
# containing bird detection data from WildTrax separated
# by sensor type. The files are named wildtrax_raw_pc_<date>.rData
# and wildtrax_raw_aru_<date>.rData, where <date> is the
# current system date. These files can be loaded into an R
# environment for cleaning and further analysis."
# ---
# 1. Setup ----
## 1.1 Load packages ----
# If not installed, uncomment the lines below to install
# install.packages("remotes")
# remotes::install_github("ABbiodiversity/wildRtrax")
library(tidyverse) # data manipulation and visualization
library(wildRtrax) # interacting with the WildTrax platform
## 1.2 Login to WildTrax ----
# The config file contains the login credentials.
config <- "1_code/r_scripts/.wtlogin.R"
source(config)
wt_auth()
# 2. Fetch data from WildTrax by sensor type: PC and ARU ----
## 2.1 Point count data ----
# Get the summary of downloads for the point counts. Filter for PC
# sensor data,download the report for each project, and store in a
# dataframe.
my_report_pc <- wt_get_download_summary(sensor_id = "PC") %>%
tibble::as_tibble() %>%
filter(sensor=="PC") %>%
dplyr::mutate(data = purrr::map(.x = project_id,
.f = ~wt_download_report(
project_id = .x,
sensor_id = "PC",
weather_cols = F,
reports = "main"))) %>%
dplyr::select(c(data)) %>%
mutate(class=sapply(data, function(df)
class(df$individual_count))) %>%
filter(class!="logical") %>%
filter(class!="NULL") %>%
mutate(data = lapply(data, function(df)
mutate(df, individual_count = as.character(individual_count)))) %>%
mutate(data = lapply(data, function(df)
mutate(df, survey_id = as.character(survey_id)))) %>%
mutate(data = lapply(data, function(df)
mutate(df, observer = as.character(observer)))) %>%
dplyr::select(-class) %>%
unnest(col = data) %>%
mutate(survey_type="PC")
## 2.2 ARU data ----
# Get the summary of downloads for the ARUs. Filter for ARU sensor
# data, download the report for each project, and store in a
# dataframe.
my_report_aru <- wt_get_download_summary(sensor_id = "PC") %>%
tibble::as_tibble() %>%
filter(sensor=="ARU") %>%
dplyr::mutate(data = purrr::map(.x = project_id,
.f = ~wt_download_report(
project_id = .x,
sensor_id = "ARU",
weather_cols = F,
reports = "main"))) %>%
dplyr::select(c(project, data)) %>%
mutate(class=sapply(data, function(df)
class(df$individual_count))) %>%
filter(class!="logical") %>%
filter(class!="NULL") %>%
mutate(data = lapply(data, function(df)
mutate(df, individual_count = as.character(individual_count)))) %>%
mutate(data = lapply(data, function(df)
mutate(df, observer = as.character(observer)))) %>%
dplyr::select(-class) %>%
unnest(col=data) %>%
mutate(survey_type="ARU")
# 3. Save ----
wildtrax_raw_pc <- my_report_pc
save(wildtrax_raw_pc,
file=paste0("0_data/manual/response/wildtrax_raw_pc_",
Sys.Date(), ".rData"))
wildtrax_raw_aru <- my_report_aru
save(wildtrax_raw_aru,
file=paste0("0_data/manual/response/wildtrax_raw_aru_",
Sys.Date(), ".rData"))