View source: R/sl.findnn.curvilin.R
sl.findnn.curvilin | R Documentation |
Find the nearest neighbour of a gridded field relative to a specific point on a sphere.
sl.findnn.curvilin(lon = NULL, lat = NULL, x = NULL, y = NULL, z = NULL, lon.grid = NULL, lat.grid = NULL, x.grid = NULL, y.grid = NULL, z.grid = NULL, bruteforce = FALSE, Rsphere = 1)
lon |
a scalar or numeric vector of length |
lat |
a scalar or numeric vector of length |
x |
a scalar or numeric vector of length |
y |
a scalar or numeric vector of length |
z |
a scalar or numeric vector of length |
lon.grid |
an |
lat.grid |
an |
x.grid |
an |
y.grid |
an |
z.grid |
an |
bruteforce |
a logical value specifying whether to find the nearest neighbour(s) by computing the distance(s) from all grid points. If |
Rsphere |
a scalar specifying the radius of the sphere to be used when computing the distance; default is |
If FALSE
(default) and if max(M,N)>5
, the nearest neighbour(s) is (are) found in an iterative manner by subsetting (coarsening) the grid such that distances are first computed only for 5x5
quasi-evenly distributed points (using the indices [round(seq(0,4)*((M-1)/4))+1),round(seq(0,4)*((N-1)/4))+1)]
). In the next step, another 5x5
subset refined by a factor 2
, centered on the nearest neighbour of the previous subset, is used. This procedure is continued until the subgrid resolution reaches the original grid resolution (in both dimensions), which will have 'zoomed' into the location of the overall nearest neighbour. This iterative procedure can be much faster for large grids because distances don't need to be computed from all grid points (scaling much better).
a list with three elements:
nn.index |
an |
nn.dist |
a scalar or numeric vector of length |
Helge Goessling
sl.grid2path
# generate an example curvilinear grid; first just lon-lat, then rotate (to around the North Pole) to make it more general
lon.seq = seq(-60,60,5)
lat.seq = seq(-30,30,5)
Nx = length(lon.seq)
Ny = length(lat.seq)
lon.0 = matrix(rep(lon.seq,Ny),nrow=Nx)
lat.0 = matrix(rep(lat.seq,each=Nx),nrow=Nx)
abg = sl.lonlatrot2abg(c(0,0,30))
rot.lonlat = sl.rot(lon = lon.0, lat = lat.0, alpha = abg[1], beta = abg[2], gamma = abg[3])
lon.grid = matrix(rot.lonlat$lon,nrow=Nx)
lat.grid = matrix(rot.lonlat$lat,nrow=Nx)
# define a few points, 'spiralling' to the North Pole
points.lon = seq(-135,180,45)
points.lat = seq(50,85,5)
# compute the nearest neighbours
nn = sl.findnn.curvilin(lon = points.lon, lat = points.lat, lon.grid = lon.grid, lat.grid = lat.grid)
# visualize the grid, points, and nearest neighbours in a map
pir = sl.plot.init(projection = "polar", do.init.device = FALSE)
sl.plot.naturalearth(pir, what = "land", resolution = "coarse", fill.col = "grey")
sl.plot.lonlatgrid(pir, col = "black", pole.hole = TRUE)
sl.plot.points(pir, lon = lon.grid, lat = lat.grid, pch = 16, cex = 0.3, col = "black")
sl.plot.points(pir, lon = points.lon, lat = points.lat, col = "red")
for (i in 1:length(points.lon)) {
sl.plot.points(pir, lon = lon.grid[nn$nn.index$row[i],nn$nn.index$col[i]], lat = lat.grid[nn$nn.index$row[i],nn$nn.index$col[i]], col = "blue")
}
sl.plot.end(pir, do.close.device = FALSE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.