-
Notifications
You must be signed in to change notification settings - Fork 3
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
distance to ice with terra #138
Comments
new draft, worked up with Laura dist_ice1 <- function(xyt, icereader = read_amsr2_3k_ice, levels = 15) {
#data(lon = , lat = , dat)
xyt$g <- cut(xyt[[3]], "1 day")
xyt[["original_order124566"]] <- 1:nrow(xyt)
l <- split(xyt, xyt$g)[unique(xyt$g)]
out <- vector("list", length(l))
for (i in seq_along(l)) {
ice <- terra::rast(icereader(l[[i]][[3]][1]))
cl <- disagg(as.contour(ice, levels = levels))
clp <- as.points(cl)
v <- vect(cbind(l[[i]][[1]], l[[i]][[2]]), crs = "OGC:CRS84")
out[[i]] <- as.data.frame(nearest(v, project(clp, "OGC:CRS84")))
out[[i]]$date <- l[[i]][[3]][1]
out[[i]]$iceconc <- extract(ice, project(v, crs(ice)))[,2L]
}
out <- do.call(rbind, out)
out <- out[order(out[["original_order124566"]]), ]
out[["original_order124566"]] <- NULL
out[["from_id"]] <- NULL
out
}
|
new version with progress and ordering dist_ice1 <- function(xyt, icereader = read_amsr2_3k_ice, levels = 15) {
#data(lon = , lat = , dat)
xyt$g <- cut(xyt[[3]], "1 day")
xyt[["original_order"]] <- 1:nrow(xyt)
l <- split(xyt, xyt$g)[unique(xyt$g)]
out <- vector("list", length(l))
pb <- progress::progress_bar$new(total =length(out))
for (i in seq_along(l)) {
ice <- terra::rast(icereader(l[[i]][[3]][1]))
cl <- disagg(as.contour(ice, levels = levels))
clp <- as.points(cl)
v <- vect(cbind(l[[i]][[1]], l[[i]][[2]]), crs = "OGC:CRS84")
out[[i]] <- as.data.frame(nearest(v, project(clp, "OGC:CRS84")))
out[[i]]$date <- l[[i]][[3]][1]
out[[i]]$iceconc <- extract(ice, project(v, crs(ice)))[,2L]
out[[i]]$order <- l[[i]][["original_order"]]
pb$tick()
}
out <- do.call(rbind, out)
out <- out[order(out[["order"]]), ]
out[["order"]] <- NULL
out[["from_id"]] <- NULL
out[["to_id"]] <- NULL
out
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is about 10000x faster
The text was updated successfully, but these errors were encountered: