-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00.QPADCorrections.R
More file actions
285 lines (243 loc) · 9.75 KB
/
Copy path00.QPADCorrections.R
File metadata and controls
285 lines (243 loc) · 9.75 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
284
285
#1. Make X ----
.make_x <- function(data, tz="local", check_xy=TRUE) {
# Download message
message("Downloading geospatial assets. This may take a moment.")
# Function to download and read a raster file using httr2
download_and_read_raster <- function(url, filename) {
req <- httr2::request(url) %>%
httr2::req_perform() # Perform the request
# Save the response content to a file
writeBin(req$body, filename)
return(terra::rast(filename)) # Read the raster file
}
# Download and read TIFF files
.rlcc <- download_and_read_raster("https://raw.githubusercontent.com/ABbiodiversity/wildRtrax-assets/main/lcc.tif", "lcc.tif")
.rtree <- download_and_read_raster("https://raw.githubusercontent.com/ABbiodiversity/wildRtrax-assets/main/tree.tif", "tree.tif")
.rd1 <- download_and_read_raster("https://raw.githubusercontent.com/ABbiodiversity/wildRtrax-assets/main/seedgrow.tif", "seedgrow.tif")
.rtz <- download_and_read_raster("https://raw.githubusercontent.com/ABbiodiversity/wildRtrax-assets/main/utcoffset.tif", "utcoffset.tif")
crs <- terra::crs(.rtree)
#get vars
date <- date(data$date_time)
time <- substr(as.character(data$date_time), 12, 19)
lon <- as.numeric(data$longitude)
lat <- as.numeric(data$latitude)
dur <- as.numeric(data$task_duration)
dis <- Inf
#parse date+time into POSIXlt
if(tz=="local"){
dtm <- strptime(paste0(date, " ", time, ":00"),
format="%Y-%m-%d %H:%M:%S", tz="America/Edmonton")
}
if(tz=="utc"){
dtm <- strptime(paste0(date, " ", time, ":00"),
format="%Y-%m-%d %H:%M:%S", tz="GMT")
}
day <- as.integer(dtm$yday)
hour <- as.numeric(round(dtm$hour + dtm$min/60, 2))
#checks
checkfun <- function(x, name="", range=c(-Inf, Inf)) {
if (any(x[!is.na(x)] < range[1] | x[!is.na(x)] > range[2])) {
stop(sprintf("Parameter %s is out of range [%.0f, %.0f]", name, range[1], range[2]))
}
invisible(NULL)
}
#Coordinates
if (check_xy) {
checkfun(lon, "lon", c(-164, -52))
checkfun(lat, "lat", c(39, 69))
}
if (any(is.infinite(lon)))
stop("Parameter lon must be finite")
if (any(is.infinite(lat)))
stop("Parameter lat must be finite")
#handling missing values
ok_xy <- !is.na(lon) & !is.na(lat)
#Other fields
checkfun(day, "day", c(0, 365))
checkfun(hour, "hour", c(0, 24))
checkfun(dur, "dur", c(0, Inf))
#intersect here
xydf <- data.frame(x=lon, y=lat)
xydf$x[is.na(xydf$x)] <- mean(xydf$x, na.rm=TRUE)
xydf$y[is.na(xydf$y)] <- mean(xydf$y, na.rm=TRUE)
xy <- vect(xydf, geom=c("x", "y"), crs="+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
xy <- project(xy, crs)
#LCC4 and LCC2
vlcc <- terra::extract(.rlcc, xy)$lcc
lcclevs <- c("0"="", "1"="Conif", "2"="Conif", "3"="", "4"="",
"5"="DecidMixed", "6"="DecidMixed", "7"="", "8"="Open", "9"="",
"10"="Open", "11"="Open", "12"="Open", "13"="Open", "14"="Wet",
"15"="Open", "16"="Open", "17"="Open", "18"="", "19"="")
lcc4 <- factor(lcclevs[vlcc+1], c("DecidMixed", "Conif", "Open", "Wet"))
lcc2 <- lcc4
levels(lcc2) <- c("Forest", "Forest", "OpenWet", "OpenWet")
#TREE
vtree <- terra::extract(.rtree, xy)$tree
TREE <- vtree / 100
TREE[TREE < 0 | TREE > 1] <- 0
#raster::extract seedgrow value (this is rounded)
d1 <- terra::extract(.rd1, xy)$seedgrow
#UTC offset + 7 makes Alberta 0 (MDT offset) for local times
if(tz=="local"){
ltz <- terra::extract(.rtz, xy)$utcoffset + 7
}
if(tz=="utc"){
ltz <- 0
}
message("Removing geospatial assets from local")
# Remove once downloaded and read
file.remove(list.files(pattern = "*.tif$"))
#sunrise time adjusted by offset
ok_dt <- !is.na(dtm)
dtm[is.na(dtm)] <- mean(dtm, na.rm=TRUE)
if(tz=="local"){
sr <- suntools::sunriset(cbind("X"=xydf$x, "Y"=xydf$y),
as.POSIXct(dtm, tz="America/Edmonton"),
direction="sunrise", POSIXct.out=FALSE) * 24
}
if(tz=="utc"){
sr <- suntools::sunriset(cbind("X"=xydf$x, "Y"=xydf$y),
as.POSIXct(dtm, tz="GMT"),
direction="sunrise", POSIXct.out=FALSE) * 24
}
TSSR <- round(unname((hour - sr - ltz) / 24), 4)
#days since local spring
DSLS <- (day - d1) / 365
#transform the rest
JDAY <- round(day / 365, 4) # 0-365
TREE <- round(vtree / 100, 4)
MAXDIS <- round(dis / 100, 4)
MAXDUR <- round(dur, 4)
out <- data.frame(
TSSR=TSSR,
JDAY=JDAY,
DSLS=DSLS,
LCC2=lcc2,
LCC4=lcc4,
TREE=TREE,
MAXDUR=MAXDUR,
MAXDIS=MAXDIS)
out$TSSR[!ok_xy | !ok_dt] <- NA
out$DSLS[!ok_xy] <- NA
out$LCC2[!ok_xy] <- NA
out$LCC4[!ok_xy] <- NA
out$TREE[!ok_xy] <- NA
return(out)
}
#2. Make offsets ----
.make_off <- function(spp, x){
if (length(spp) > 1L)
stop("spp argument must be length 1. Use a loop or map for multiple species.")
spp <- as.character(spp)
#checks
if (!(spp %in% getBAMspecieslist()))
stop(sprintf("Species %s has no QPAD estimate available", spp))
#constant for NA cases
cf0 <- exp(unlist(coefBAMspecies(spp, 0, 0)))
#best model
mi <- bestmodelBAMspecies(spp, type="BIC")
cfi <- coefBAMspecies(spp, mi$sra, mi$edr)
TSSR <- x$TSSR
DSLS <- x$DSLS
JDAY <- x$JDAY
lcc2 <- x$LCC2
lcc4 <- x$LCC4
TREE <- x$TREE
MAXDUR <- x$MAXDUR
MAXDIS <- x$MAXDIS
n <- nrow(x)
#Design matrices for singing rates (`Xp`) and for EDR (`Xq`)
Xp <- cbind(
"(Intercept)"=1,
"TSSR"=TSSR,
"JDAY"=JDAY,
"TSSR2"=TSSR^2,
"JDAY2"=JDAY^2,
"DSLS"=DSLS,
"DSLS2"=DSLS^2)
Xq <- cbind("(Intercept)"=1,
"TREE"=TREE,
"LCC2OpenWet"=ifelse(lcc4 %in% c("Open", "Wet"), 1, 0),
"LCC4Conif"=ifelse(lcc4=="Conif", 1, 0),
"LCC4Open"=ifelse(lcc4=="Open", 1, 0),
"LCC4Wet"=ifelse(lcc4=="Wet", 1, 0))
p <- rep(NA, n)
A <- q <- p
#design matrices matching the coefs
Xp2 <- Xp[,names(cfi$sra),drop=FALSE]
OKp <- rowSums(is.na(Xp2)) == 0
Xq2 <- Xq[,names(cfi$edr),drop=FALSE]
OKq <- rowSums(is.na(Xq2)) == 0
#calculate p, q, and A based on constant phi and tau for the respective NAs
p[!OKp] <- sra_fun(MAXDUR[!OKp], cf0[1])
unlim <- ifelse(MAXDIS[!OKq] == Inf, TRUE, FALSE)
A[!OKq] <- ifelse(unlim, pi * cf0[2]^2, pi * MAXDIS[!OKq]^2)
q[!OKq] <- ifelse(unlim, 1, edr_fun(MAXDIS[!OKq], cf0[2]))
#calculate time/lcc varying phi and tau for non-NA cases
phi1 <- exp(drop(Xp2[OKp,,drop=FALSE] %*% cfi$sra))
tau1 <- exp(drop(Xq2[OKq,,drop=FALSE] %*% cfi$edr))
p[OKp] <- sra_fun(MAXDUR[OKp], phi1)
unlim <- ifelse(MAXDIS[OKq] == Inf, TRUE, FALSE)
A[OKq] <- ifelse(unlim, pi * tau1^2, pi * MAXDIS[OKq]^2)
q[OKq] <- ifelse(unlim, 1, edr_fun(MAXDIS[OKq], tau1))
#log(0) is not a good thing, apply constant instead
ii <- which(p == 0)
p[ii] <- sra_fun(MAXDUR[ii], cf0[1])
#package output
data.frame(
p=p,
q=q,
A=A,
correction=p*A*q,
offset=log(p) + log(A) + log(q))
}
#3. Wrapper ----
qpad_correction <- function (data, species = c("all"), version = 3, together = FALSE)
{
if ("survey_url" %in% colnames(data)) {
data <- ungroup(mutate(rowwise(rename(data, task_id = survey_id,
recording_date_time = survey_date, observer_id = observer)),
durationMethod = ifelse(substr(survey_duration_method,
nchar(survey_duration_method), nchar(survey_duration_method)) ==
"+", substr(survey_duration_method, 1, nchar(survey_duration_method) -
2), survey_duration_method), chardur = gregexpr("-",
durationMethod, fixed = TRUE), chardurmax = max(unlist(chardur)),
task_duration = as.numeric(substr(durationMethod,
chardurmax + 1, nchar(durationMethod) - 3)) *
60, chardis = gregexpr("-", survey_distance_method,
fixed = TRUE), chardismax = max(unlist(chardis)),
distance1 = substr(survey_distance_method, chardismax +
1, nchar(survey_distance_method) - 1), task_distance = ifelse(distance1 %in%
c("AR", "IN"), Inf, as.numeric(distance1))))
}
cat("Extracting covariates for offset calculation. This may take a moment.")
x <- .make_x(data)
cat("\nLoading QPAD estimates... ")
load_BAM_QPAD(version)
if ("all" %in% species)
spp <- sort(intersect(getBAMspecieslist(), colnames(data)))
else spp <- species
cat("\nCalculating offsets...")
off <- matrix(0, nrow(x), length(spp))
colnames(off) <- spp
for (i in 1:length(spp)) {
cat("\n", spp[i])
o <- .make_off(spp[i], x)
off[, i] <- o$correction
}
if (together == FALSE) {
return(data.frame(off))
}
if (together == TRUE) {
out <- cbind(data, rename_with(data.frame(off), .fn = ~paste0(.x,
".off")))
if ("survey_url" %in% colnames(data)) {
out <- dplyr::select(rename(out, survey_id = task_id,
survey_date = recording_date_time, observer = observer_id),
-durationMethod, -chardur, -chardurmax, -task_duration,
-chardis, -chardismax, -distance1, -task_distance)
}
return(out)
}
cat("\nDone!")
}