pkern_sub_find: Check vectorized grid data for non-NA points that form a...

View source: R/pkern_index.R

pkern_sub_findR Documentation

Check vectorized grid data for non-NA points that form a complete sub-grid

Description

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

Usage

pkern_sub_find(g_obs, gdim = NULL)

Arguments

g_obs

logical vector, or any other object accepted by pkern_grid

gdim

integer vector, the grid dimensions (ny, nx)

g_out

logical, indicates to return a grid list object

Details

A sub-grid is only eligible if it contains all of the non-NA points in g_obs and none of the NAs; eg 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 the sub-grid and return NULL. If no points are NA, the function returns indices for the full grid.

In the special case that g_obs is a logical vector, it should indicate the the non-NA locations in a grid with dimensions gdim. Otherwise, grid dimensions are extracted from g_obs, overriding any argument to gdim.

Value

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

Examples


# define a grid and example data
gdim = c(50, 53)
g_bare = pkern_grid(gdim)
gval = pkern_sim(g_bare, modifyList(pkern_pars(g), list(eps=1e-12)))
g_obs = modifyList(g_bare, list(gval=gval))
pkern_plot(g_obs)

# define a super-grid containing the original data and make sure we can find it
g_obs_big = pkern_rescale(g_obs, down=3)
pkern_plot(g_obs_big)
str(pkern_sub_find(g_obs_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(\(idx, r, n) seq(idx, by=r, length.out=n), idx=ij_first, r=spacing, n=gdim_sg)
is_sg = pkern_sub_idx(gdim, ij_sg, idx=FALSE)

# assign values to the sub-grid points
g_obs_sub = g_bare
g_obs_sub$gval[is_sg] = gval[is_sg]
pkern_plot(g_obs)
pkern_plot(g_obs_sub, zlab='sub-grid')

# call the function and check for expected results
subgrid_result = pkern_sub_find(g_obs_sub)
all.equal(unname(subgrid_result$gdim), gdim_sg)
all.equal(unname(subgrid_result$ij), ij_sg)

# sub grids with side length 1 have no spacing defined along that dimension
spacing[gdim_sg==1] = NA
all.equal(unname(subgrid_result$res_scale), spacing)

# or call on the vector and supply gdim separately
identical(subgrid_result, pkern_sub_find(g_obs_sub$gval, g_obs_sub$gdim))
identical(subgrid_result, pkern_sub_find(!is.na(g_obs_sub$gval), g_obs_sub$gdim))


deankoch/pkern documentation built on Oct. 26, 2023, 8:54 p.m.