Skip to content

Commit

Permalink
fix rdist.earth
Browse files Browse the repository at this point in the history
  • Loading branch information
kongdd committed Dec 15, 2023
1 parent 705f218 commit f1a6b64
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 66 deletions.
13 changes: 13 additions & 0 deletions R/find_near.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#' find_near
#' @param p a vector of length 2, `[lon, lat]`
#' @export
find_near <- function(p, st) {
loc <- st[, .(lon, lat)]
loc %<>%
check_matrix()
dist <- rdist.earth2(loc, p)
i <- which.min(dist)
s <- st[i, ] |>
cbind(dist = dist[i])
s
}
19 changes: 9 additions & 10 deletions R/interp_approx.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@ na_approx.dtime <- function(x, maxgap = 5, ...) {
#' @param .parallel boolean
#'
#' @export
interp_approx <- function(
xx, stationInfo, maxgap = 5, verbose = TRUE,
.parallel = FALSE, ...) {
interp_approx <- function(xx, stationInfo, maxgap = 5, verbose = TRUE,
.parallel = FALSE, ...)
{
I <- with(stationInfo, which(n_miss > 0 & gap_min <= maxgap)) # gap satisfied
N <- length(I)
step <- floor(N / 10)

FUN <- ifelse(.parallel, `%dopar%`, `%do%`)
xx[I] <- FUN(foreach(x = xx[I], i = icount()), {
if (verbose) {
runningId(i, step, N, "interp_approx")
}
`%dof%` <- ifelse(.parallel, `%dopar%`, `%do%`)

xx[I] <- foreach(x = xx[I], i = icount()) %dof% {
verbose && runningId(i, step, N, "interp_approx")
na_approx.dtime(x, maxgap)
})
return(xx)
}
xx
}
51 changes: 6 additions & 45 deletions R/rdist.earth.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,13 @@
#' }
#'
#' @export
rdist.earth <- function(x1, x2 = x1) {
R <- 6378.388
# coslat1 <- cos(x1[, 2]) sinlat1 <- sin(x1[, 2]) coslon1 <- cos(x1[, 1]) sinlon1 <- sin(x1[, 1])
coslat1 <- cos((x1[, 1] * pi) / 180)
sinlat1 <- sin((x1[, 1] * pi) / 180)
coslon1 <- cos((x1[, 2] * pi) / 180)
sinlon1 <- sin((x1[, 2] * pi) / 180)

coslat2 <- cos((x2[, 1] * pi) / 180)
sinlat2 <- sin((x2[, 1] * pi) / 180)
coslon2 <- cos((x2[, 2] * pi) / 180)
sinlon2 <- sin((x2[, 2] * pi) / 180)
pp <- cbind(coslat1 * coslon1, coslat1 * sinlon1, sinlat1) %*% t(cbind(
coslat2 * coslon2, coslat2 * sinlon2,
sinlat2
))
return(R * acos(ifelse(abs(pp) > 1, 1 * sign(pp), pp)))
}
rdist.earth <- function(x1, x2 = NULL) {
if (is.vector(x1)) x1 %<>% t()
if (is.vector(x2)) x2 %<>% t()
x2 = x2 %||% x1

rdist.earth2 <- function(x1, x2 = NULL) {
if (is.vector(x1)) {
x1 %<>%
t()
}
if (is.vector(x2)) {
x2 %<>%
t()
}
x1 %<>%
check_matrix()
x2 %<>%
check_matrix()
x1 %<>% check_matrix()
x2 %<>% check_matrix()

# miles = FALSE, R = NULL R = R %||% ifelse(miles, 3963.34, 6378.388)
R <- 6378.388
Expand Down Expand Up @@ -76,17 +51,3 @@ check_matrix <- function(x) {
}
x
}

#' find_near
#' @param p a vector of length 2, `[lon, lat]`
#' @export
find_near <- function(p, st) {
loc <- st[, .(lon, lat)]
loc %<>%
check_matrix()
dist <- rdist.earth2(loc, p)
i <- which.min(dist)
s <- st[i, ] |>
cbind(dist = dist[i])
s
}
2 changes: 1 addition & 1 deletion R/st-get_moveInfo.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ distToCentralPeriod <- function(lon, lat, n_period) {
i <- which.max(n_period) #

if (nrow(P) > 1) {
dist <- rdist.earth(P[i, , drop = FALSE], P[, , drop = FALSE])[1, ]
dist <- rdist.earth(P[i, , drop = FALSE], P[, , drop = FALSE])
round(dist, 2)
} else {
0
Expand Down
2 changes: 1 addition & 1 deletion R/st-revise_locaiton_multi.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ distToCentralPeriod <- function(lon, lat, n_period) {
i <- which.max(n_period) #

if (nrow(P) > 1) {
dist <- rdist.earth(P[i, , drop = FALSE], P[, , drop = FALSE])[1, ]
dist <- rdist.earth(P[i, , drop = FALSE], P[, , drop = FALSE])
round(dist, 2)
} else {
0
Expand Down
8 changes: 8 additions & 0 deletions R/tools.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,11 @@ mode <- function(x) {
names() %>%
as.numeric()
}

`%||%` <- function(x, y) {
if (is.null(x)) {
y
} else {
x
}
}
2 changes: 1 addition & 1 deletion man/find_near.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/rdist.earth.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion scripts/met840/Step03_缺失信息检测与插值.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if (file.exists(infile)){

fstation <- "说明文档/中国气候日志数据V3station839.xlsx"
stations <- read.xlsx(fstation)
dist <- rdist.earth(stations[, 3:2], miles = F)#lon, lat
dist <- rdist.earth(stations[, 3:2])#lon, lat
## ---------------------- prepare for parallel compute -------------------
site <- stations$StationNo
time_day <- as.Date("1951-01-01"):as.Date("2016-06-30") %>% as.Date()
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-interp_main.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
test_that("interp_main and dtime2mat works", {
data("prcp")
data("st840")
data("prcp")
data("st840")

r <- interp_main(prcp, st840, smax = 200, verbose = FALSE)
r <- interp_main(prcp, st840, smax = 200, verbose = FALSE)

df2 <- dtime2mat(r$x)
expect_equal(ncol(df2), ncol(prcp))
expect_false(all.equal(df2, prcp) == TRUE)
df2 <- dtime2mat(r$x)
expect_equal(ncol(df2), ncol(prcp))
expect_false(all.equal(df2, prcp) == TRUE)
})

0 comments on commit f1a6b64

Please sign in to comment.