sk_sub: Return a sub-grid of a sk grid object

View source: R/sk_index.R

sk_subR Documentation

Return a sub-grid of a sk grid object

Description

Creates a "sk" object containing only the grid-lines specified in idx_keep. Alternatively, grid lines to remove can be specified in idx_rem.

Usage

sk_sub(g, ij_keep = NULL, ij_rem = NULL, idx = FALSE, mirror = FALSE)

Arguments

g

sk grid or any grid-like object accepted by sk

ij_keep

list of grid line numbers ("i" and "j") forming regular sub-grid

ij_rem

list of grid line numbers ("i" and "j") whose exclusion forms regular sub-grid

idx

logical, if TRUE the function returns a list containing ij_keep and ij_rem

mirror

logical, whether to mirror the selection in ij_rem (see details)

Details

One of idx_keep or idx_rem (but not both) can be specified, and the grid line numbers (not intercepts) should be supplied in ascending order in list entries named "i" and "j".

If idx_rem is specified, mirror=TRUE will cause the selection in idx_rem to be reflected about the central grid line (useful for specifying outer grid lines). mirror is ignored if idx_keep is specified instead.

Default idx=FALSE causes the function to return the sub-grid as a sk grid object. If idx=TRUE, the function instead returns a list containing idx_keep and idx_rem as specified above.

If neither idx_keep nor idx_rem is supplied, the function removes outer grid lines iteratively (selecting the one with highest proportion of NAs), attempting to find a complete sub-grid (having no NAs) somewhere in the interior. This heuristic is designed for rasters with few NAs, all located around the perimeter.

Value

an sk grid object, the requested sub-grid of g

See Also

Other sk constructors: sk_rescale(), sk_snap(), sk()

Examples


# make an example grid
g = sk(c(50, 100))
g[] = apply(expand.grid(g[['gyx']]), 1, \(z) cos( 2*sum(z^2) ) )
plot(g)

# subset by specifying grid lines to keep
ij_keep = list(i=seq(1, 50, by=2), j=seq(1, 50, by=2))
g_keep = sk_sub(g, ij_keep)
plot(g_keep)

# get the indices kept and removed
idx = sk_sub(g, ij_keep, idx=TRUE)

# equivalent call specifying grid lines to omit
g_rem = sk_sub(g, ij_rem=idx[['rem']])
identical(g_rem, g_keep)

# remove some data around the edges of the grid
idx = sk_sub(g, ij_rem=list(i=seq(10), j=seq(10)), mirror=TRUE, idx=TRUE)
idx_y_pts = sk_sub_idx(dim(g), idx[['rem']]['i'], idx=TRUE)
idx_x_pts = sk_sub_idx(dim(g), idx[['rem']]['j'], idx=TRUE)
idx_pts = c(idx_y_pts, idx_x_pts)
idx_na = sort(sample(idx_pts, 0.6*length(idx_pts)))
g[idx_na] = NA
plot(g)

# identify the interior sub-grid that is complete
g_sub = sk_sub(g)
print(g_sub)
plot(g_sub)

# verify it is as large as expected
( dim(g) - dim(g_sub) ) == sapply(idx[['rem']], length)


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