pkern_rescale: Up or down-scale a grid

View source: R/pkern_index.R

pkern_rescaleR Documentation

Up or down-scale a grid

Description

Changes the resolution of a grid by a factor of up or down.

Usage

pkern_rescale(g, up = NULL, down = NULL)

Arguments

g

any object accepted or returned by pkern_grid

up

integer > 0, or vector of two, the up-scaling factor(s)

down

integer > 0, or vector of two, the down-scaling factor(s)

Details

Users should specify a grid g to re-scale and an integer scaling factor; either up or down. This effects the scaling of resolution (g$gres) by up or 1/down.

up (or down) should be a vector of two positive integers, supplying the re-scaling factors in the y and x dimensions in that order, or a single value to be used for both.

When up is supplied, a lower resolution grid is returned comprising every upth grid line of g along each dimension. All other grid lines, and any data values lying on them, are ignored. up should be no greater than g$gdim - 1. Note that if up does not evenly divide this number, the bounding box will shrink slightly.

When down is supplied, the function returns a higher resolution grid (g_fine) with the same bounding box as g. Along each dimension, every downth grid line of g_fine coincides with a grid line of g. Any values found in g$gval are copied to g_fine, and un-mapped grid lines in g_fine are initialized to NA. Recover g from g_fine with pkern_rescale(g_fine, up=down).

Value

a grid list of the form returned by pkern_grid

Examples


# example data
gdim = c(50, 53)
g = pkern_grid(gdim)
pars = modifyList(pkern_pars(g), list(eps=1e-6))
gval = pkern_sim(g, pars)
g_obs = modifyList(g, list(gval=gval))
pkern_plot(g_obs)

# upscale
pkern_plot(pkern_rescale(g=g_obs, up=1)) # does nothing
pkern_plot(pkern_rescale(g=g_obs, up=2))

# downscale
pkern_plot(pkern_rescale(g=g_obs, down=1)) # does nothing
pkern_plot(pkern_rescale(g=g_obs, down=2))

# length-2 vectors to rescale differently in x and y directions
pkern_plot(pkern_rescale(g=g_obs, up=c(2,3)))
pkern_plot(pkern_rescale(g=g_obs, down=c(2,3)))

# invert a down-scaling
g_obs_compare = pkern_rescale(pkern_rescale(g=g_obs, down=c(5,3)), up=c(5,3))
identical(g_obs, g_obs_compare)

# multi-layer example with missing data
n_pt = prod(gdim)
n_layer = 3

# generate some data and omit 50% of it
gval_multi = pkern_sim(pkern_grid(list(gdim=gdim, gval=matrix(NA, n_pt, n_layer))), pars)
idx_miss = sample.int(n_pt, round(0.5*n_pt))
gval_multi[idx_miss,] = NA

# plot third layer, then down-scaled and up-scaled versions
g_sim_multi = modifyList(g, list(gval=gval_multi))
pkern_plot(g_sim_multi, layer=3)
pkern_plot(pkern_rescale(g=g_sim_multi, down=2), layer=3)
pkern_plot(pkern_rescale(g=g_sim_multi, up=2), layer=3)


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