forked from rich-iannone/splitr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_grid.R
40 lines (34 loc) · 1.12 KB
/
create_grid.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
#' Create a grid of sites
#'
#' Flexibly create a grid of sites for ensemble trajectory model runs.
#'
#' @param lat,lon Latitude and longitude values in decimal degrees for the
#' center point of the grid.
#' @param range The latitude and longitude range about the `grid_ref`.
#' @param division The division distances across the latitude and longitude
#' ranges.
#' @export
create_grid <- function(lat = 49.263,
lon = -123.250,
range = c(5, 5),
division = c(0.5, 0.5)) {
n_s_points <- range[1] / division[1]
w_e_points <- range[2] / division[2]
lat_vec <-
sort(
(lat + range[1]/2) - seq(0, range[1], division[1]),
decreasing = TRUE
)
lon_vec <- sort((lon + range[2]/2) - seq(0, range[2], division[2]))
for (i in seq_along(lat_vec)) {
for (j in seq_along(lon_vec)) {
if (i == 1 & j == 1) {
coords <- vector("list", 2)
names(coords) <- c("lat", "lon")
}
coords$lat[length(coords$lat) + 1] <- lat_vec[i]
coords$lon[length(coords$lon) + 1] <- lon_vec[j]
}
}
coords
}