Skip to content

Commit 458bce5

Browse files
committed
faster get_distances
1 parent bc4dde2 commit 458bce5

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

R/extract_serie.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
#' @param variable variable name
88
#' @param field dimension of the variable, '4d' (default), '2dz', '2dz', '2d' or 'xyzt', 'xyz', 'xyz', and 'xy' see notes
99
#' \tabular{lll}{
10-
#' \strong{Field}\tab \strong{Dimensions}\tab \strong{Notes} \cr
10+
#' \strong{Field}\tab \strong{Dimensions}\tab \strong{Example} \cr
1111
#' 3dt \tab xyzt \tab WRF dimensions for 3D array with multiple times\cr
1212
#' 2dt \tab xyt \tab WRF dimensions for 2D array with multiple times \cr
1313
#' 2dz \tab xyz \tab WRF dimensions for 3D array with single time\cr
1414
#' 2d \tab xy \tab WRF dimensions for 2D array with single time\cr
1515
#' }
16-
#' @param level model level to be extracted
16+
#' @param level model level (index) to be extracted
1717
#' @param prefix to output file, default is serie
1818
#' @param new TRUE, FALSE of 'check' see notes
1919
#' @param return.nearest return the data.frame of nearest points instead of extract the serie
@@ -26,7 +26,7 @@
2626
#'
2727
#' @return No return value
2828
#'
29-
#' @note The field argument '4d' or '2dz' is used to read a 4d/3d variable droping the 3rd dimension (z), this is based on WRF outputs format and the array order from ncdf4.
29+
#' @note The field argument '4d' or '2dz' is used to read a 4d/3d variable droping the 3rd dimension (z), this should be based how ncdf4 R-package reads the model output.
3030
#'
3131
#' @note new = TRUE create a new file, new = FALSE append the data in a old file, and new = 'check' check if the file exist and append if the file exist and create if the file doesnt exist
3232
#'

R/get_distances.R

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@
1212
#'
1313
#' @export
1414
#'
15-
get_distances <- function(lat1, long1, lat2, long2,R = 6371) {
16-
deg2rad <- function(deg) {
17-
return(deg * (pi/180))
18-
}
19-
d_lat <- deg2rad(lat2 - lat1)
20-
d_long <- deg2rad(long2 - long1)
21-
a <- sin(d_lat/2) * sin(d_lat/2) +
22-
cos(deg2rad(lat1)) * cos(deg2rad(lat2)) *
23-
sin(d_long/2) * sin(d_long/2)
24-
c1 <- 2 * atan2(sqrt(a), sqrt(1-a))
25-
d <- R * c1; # Distance in km
15+
get_distances <- function(lat1, long1, lat2, long2, R = 6371) {
16+
lat1 <- lat1 * pi/180
17+
lat2 <- lat2 * pi/180
18+
d_lat <- lat2 - lat1
19+
d_long <- (long2 - long1) * pi/180
20+
a <- sin(d_lat/2)^2 + cos(lat1) * cos(lat2) * sin(d_long/2)^2
21+
d <- R * 2 * atan2(sqrt(a), sqrt(1 - a))
2622
return(d)
2723
}

0 commit comments

Comments
 (0)