sk_sub_find: Find complete regular sub-grids in a sk grid object

View source: R/sk_index.R

sk_sub_findR Documentation

Find complete regular sub-grids in a sk grid object

Description

If a sk grid g has missing values (NAs) but the set of non-NA points form a complete (regular) sub-grid, this function finds its grid lines, resolution, and dimensions. If no eligible sub-grids are found, the function returns NULL.

Usage

sk_sub_find(g, gdim = NULL)

Arguments

g

logical vector, sk grid, or any grid object accepted by sk

gdim

integer vector, the grid dimensions (in order 'y', 'x')

Details

A sub-grid is only eligible if it contains ALL of the non-NA points in g and none of the NAs. For example if a single point missing from the sub-grid, or a single non-NA point lies outside the sub-grid, the function will fail to detect any sub-grids and return NULL. If no points are NA, the function returns indices for the full grid.

The returned list contains the following named elements:

  • ij the grid line numbers of the sub-grid with respect to g

  • res_scale the resolution scaling factor (relative increase in grid line spacing of g)

  • gdim the number of y and x grid lines in the sub-grid

As in sk, each of these is given in the 'y', 'x' order.

Users can also pass the logical vector returned by !is.na(g) instead of g, in which case argument gdim must also be specified. This can be much faster with large grids.

Value

NULL or list of information about the location and spacing of the sub-grid within g (see details)

See Also

Other indexing functions: sk_mat2vec(), sk_rescale(), sk_sub_idx(), sk_vec2mat()

Examples


# define a grid and example data
gdim = c(50, 53)
pars = utils::modifyList(sk_pars(gdim), list(eps=1e-12))
g = sk_sim(gdim, pars)
plot(g)

# define a super-grid containing the original data and make sure we can find it
g_big = sk_rescale(g, down=3)
plot(g_big)
print(sk_sub_find(g_big))

# define a smaller sub-grid at random
spacing = sapply(floor(gdim/10), function(x) 1 + sample.int(x, 1))
gdim_sg = sapply(floor( (gdim - 1) / spacing), function(x) sample.int(x, 1))
ij_first = sapply(gdim - ( spacing * gdim_sg ), function(x) sample.int(x, 1))

# find index of sub-grid lines and vectorized index of points
ij_sg = Map(function(idx, r, n) seq(idx, by=r, length.out=n), idx=ij_first, r=spacing, n=gdim_sg)
names(ij_sg) = c('i', 'j')
is_sg = sk_sub_idx(gdim, ij_sg, idx=FALSE)

# assign values to the sub-grid points
g_sub = sk(gdim)
g_sub[is_sg] = g[is_sg]
plot(g_sub, zlab='sub-grid')

# call the function and check for expected results
sub_result = sk_sub_find(g_sub)
all.equal(unname(sub_result[['gdim']]), gdim_sg)
all.equal(unname(sub_result[['ij']]), unname(ij_sg))

# sub grids with side length 1 have no spacing defined along that dimension
spacing[gdim_sg==1] = NA

# check consistency in spacing
all.equal(unname(sub_result[['res_scale']]), spacing)

# can also call on the vector and supply gdim separately
identical(sub_result, sk_sub_find(!is.na(g_sub), dim(g_sub)))


snapKrig documentation built on May 31, 2023, 6:34 p.m.