interpGrid: Grid interpolation

View source: R/interpGrid.R

interpGridR Documentation

Grid interpolation

Description

Interpolation of grids (gridded data or stations) into a user-defined grid using nearest-neighbour or bilinear weights.

Usage

interpGrid(
  grid,
  new.coordinates = list(x = NULL, y = NULL),
  method = "nearest",
  bilin.method = "fields",
  force.non.overlapping = FALSE,
  mask = NULL,
  parallel = FALSE,
  max.ncores = 16,
  ncores = NULL,
  ...
)

Arguments

grid

An input grid to be interpolated/regridded.

new.coordinates

Definition of the new grid (or points) coordinates, in the form of a list with the x and y components, in this order. If new coordinates correspond to an irregular grid (e.g. point locations), lengths for x and y must be the same: Each position in x and y correspond to a new location (a pair of coordinates).

method

Method for interpolation. Currently implemented methods are either "bilinear", for bilinear interpolation, and "nearest", for nearest-neighbor interpolation (default).

bilin.method

Algorithm chosen for bilinear interpolation. Two options available: "akima" uses interp and "fields" (default) the interp.surface.grid algorithm. In case any missing values exist in the input data matrix, the "fields" option, able to handle missing values, need to be used. Otherwise, the "akima" option performs much faster.

force.non.overlapping

Optional logical flag for method = "nearest" (otherwise ignored). If set to TRUE, the nerest neighbour interpolation will be applied over non-overlapping grid domains (See the Note below). Default to FALSE.

mask

Optional. It applies for the "nearest" method only. A static C4R grid (i.e., time dimension has length one or does not exist), in which the zero values indicate grid-cells that are not used in the nearest-neighbour search. This can be useful for selecting land/sea points for example in downscaling exercises or climate index calculation.

parallel

Logical. Should parallel execution be used?

max.ncores

Integer. Upper bound for user-defined number of cores.

ncores

Integer number of cores used in parallel computation. Self-selected number of cores is used when ncpus = NULL (the default), or when maxcores exceeds the default ncores value.

...

Further arguments for bilinear interpolation that are passed to function interp from package akima.

Details

The output has special attributes in the xyCoords element that indicate that the object has been interpolated. These attributes are interpolation, which indicates the method used and resX and resY, for the grid-cell resolutions in the X and Y axes respectively. It is also possible to pass the interpolator the grid of a previously existing grid dataset using the getGrid method. See examples.

Value

An interpolated object preserving the structure of the input

Parallel Processing

Parallel processing is enabled using the parallel package. Parallelization is undertaken by a FORK-type parallel socket cluster formed by ncores. If ncores is not specified (default), ncores will be one less than the autodetected number of cores. The maximum number of cores used for parallel processing can be set with the max.ncores argument, although this will be reset to the auto-detected number of cores minus 1 if this number is exceeded. Note that not all code, but just some critical loops within the function are parallelized.

In practice, parallelization does not always result in smaller execution times, due to the parallel overhead. However, parallel computing may potentially provide a significant speedup for the particular case of large multimember datasets or large grids.

Parallel computing is currently not available for Windows machines.

Note

To avoid unnecessary NA values, the function will not extrapolate using a new grid outside the current extent of the dataset, returning an error message. This behaviour can be overriden with the force.non.overlapping option when using the nearest-neighbor method.

Author(s)

J. Bedia, S. Herrera, M. de Felice, M. Iturbide

Examples


require(climate4R.datasets)
require(visualizeR)
# boreal winter (DJF) precipitation data for the Iberian Peninsula and the period 1983-2002
data(EOBS_Iberia_pr)
spatialPlot(climatology(EOBS_Iberia_pr))
# Bilinear interpolation to a regular grid of 0.5 degree 
# resolution centered in the Iberian Peninsula
t1 <- interpGrid(EOBS_Iberia_pr, new.coordinates = list(x = seq(-10,5,.5),
                                                        y = seq(36,44,.5)),
                 method = "bilinear",
                 bilin.method = "akima")
spatialPlot(climatology(t1), backdrop.theme = "countries")
# New attributes indicate that the data have been interpolated:
attributes(t1$xyCoords)

# Using the coordinate information of another grid via getGrid()
data(NCEP_Iberia_pr)
t2 <- interpGrid(EOBS_Iberia_pr, new.coordinates = getGrid(NCEP_Iberia_pr),
                 method = "nearest")
spatialPlot(climatology(t2), backdrop.theme = "countries")

#From station data to grid
data(VALUE_Iberia_pr)
spatialPlot(climatology(VALUE_Iberia_pr), backdrop.theme = "countries")
t3 <- interpGrid(VALUE_Iberia_pr, new.coordinates = getGrid(EOBS_Iberia_pr),
                 method = "bilinear")
spatialPlot(climatology(t3), backdrop.theme = "countries")

#From grid to station data
t4 <- interpGrid(EOBS_Iberia_pr, new.coordinates = getGrid(VALUE_Iberia_pr),
                 method = "nearest")
spatialPlot(climatology(t4), backdrop.theme = "countries")
t5 <- interpGrid(EOBS_Iberia_pr, 
                 new.coordinates = list(x = c(-6.7, -4.5, 2.5), 
                                        y = c(41.8, 40, 39)))
spatialPlot(climatology(t5), backdrop.theme = "countries")

#From grid to a single point or station
t6 <- interpGrid(grid = EOBS_Iberia_pr, 
                 new.coordinates = list(x = -6.7, y = 41.8))
str(t6$Data)


SantanderMetGroup/transformeR documentation built on Nov. 25, 2024, 1:25 p.m.