Interpolate: Bilinear interpolation

View source: R/Interpolate.R

InterpolateR Documentation

Bilinear interpolation

Description

Interpolates values using bilinear interpolation.

Usage

Interpolate(formula, x.out, y.out, data = NULL, grid = TRUE, path = FALSE)

Arguments

formula

a formula indicating dependent and independent variables (see Details)

x.out, y.out

x and y values where to interpolate (see Details)

data

optional data.frame with the data

grid

logical indicating if x.out and y.out define a regular grid.

path

a logical or character indicating if the x.out and y.out define a path. If character, it will be the name of the column returning the order of said path.

Details

formula must be of the form VAR1 | VAR2 ~ X + Y where VAR1, VAR2, etc... are the names of the variables to interpolate and X and Y the names of the x and y values, respectively. It is also possible to pass only values of x, in which case, regular linear interpolation is performed and y.out, if exists, is ignored with a warning.

If grid = TRUE, x.out and y.out must define the values of a regular grid. If grid = FALSE, they define the locations where to interpolate. Both grid and path cannot be set to TRUE and the value of path takes precedence.

x.out can be a list, in which case, the first two elements will be interpreted as the x and y values where to interpolate and it can also have a path element that will be used in place of the path argument. This helps when creating a path with as.path (see Examples)

Value

A data.frame with interpolated values and locations

Examples

library(data.table)
data(geopotential)
geopotential <- geopotential[date == date[1]]
# new grid
x.out <- seq(0, 360, by = 10)
y.out <- seq(-90, 0, by = 10)

# Interpolate values to a new grid
interpolated <- geopotential[, Interpolate(gh ~ lon + lat, x.out, y.out)]

# Add values to an existing grid
geopotential[, gh.new := Interpolate(gh ~ lon + lat, lon, lat,
                                     data = interpolated, grid = FALSE)$gh]

# Interpolate multiple values
geopotential[, c("u", "v") := GeostrophicWind(gh, lon, lat)]
interpolated <- geopotential[, Interpolate(u | v ~ lon + lat, x.out, y.out)]

# Interpolate values following a path
lats <- c(-34, -54, -30)   # start and end latitudes
lons <- c(302, 290, 180)   # start and end longituded
path <- geopotential[, Interpolate(gh ~ lon + lat, as.path(lons, lats))]


metR documentation built on Nov. 2, 2023, 6:01 p.m.