### Navigation

- index
- modules
- next
- previous
- Home »
- Contents »
- InaSAFE’s API documentation »
- Package::safe »
- Package::safe.common »

# Module: safe.common.interpolation2d

Module for 2D interpolation over a rectangular mesh

This module:

- provides piecewise constant (nearest neighbour) and bilinear interpolation
- is fast (based on numpy vector operations)
- depends only on numpy
- guarantees that interpolated values never exceed the four nearest neighbours
- handles missing values in domain sensibly using NaN
- is unit tested with a range of common and corner cases

See end of this file for documentation of the mathematical derivation used.

`safe.common.interpolation2d.``check_inputs`(x, y, Z, points, mode, bounds_error)

  Check inputs for interpolate2d function

`safe.common.interpolation2d.``interpolate2d`(x, y, Z, points, mode='linear', bounds_error=False)

  Fundamental 2D interpolation routine

  Args:

    - x: 1D array of x-coordinates of the mesh on which to interpolate

    - y: 1D array of y-coordinates of the mesh on which to interpolate

    - Z: 2D array of values for each x, y pair

    - points: Nx2 array of coordinates where interpolated values are sought

    - mode: Determines the interpolation order. Options are

      > - ‘constant’ - piecewise constant nearest neighbour interpolation

      > - ‘linear’ - bilinear interpolation using the four

      >     nearest neighbours (default)

    - bounds_error: Boolean flag. If True (default) a BoundsError exception

        will be raised when interpolated values are requested outside the domain of the input data. If False, nan is returned for those values

  Returns:
    - 1D array with same length as points with interpolated values

  Raises: Exception, BoundsError (see note about bounds_error)

  Notes:

    Input coordinates x and y are assumed to be monotonically increasing, but need not be equidistantly spaced. No such assumption regarding ordering of points is made.

    Z is assumed to have dimension M x N, where M = len(x) and N = len(y). In other words it is assumed that the x values follow the first (vertical) axis downwards and y values the second (horizontal) axis from left to right.

    If this routine is to be used for interpolation of raster grids where data is typically organised with longitudes (x) going from left to right and latitudes (y) from left to right then user interpolate_raster in this module

`safe.common.interpolation2d.``interpolate_raster`(x, y, Z, points, mode='linear', bounds_error=False)

  2D interpolation of raster data

  It is assumed that data is organised in matrix Z as latitudes from bottom up along the first dimension and longitudes from west to east along the second dimension.

  Further it is assumed that x is the vector of longitudes and y the vector of latitudes.

  See interpolate2d for details of the interpolation routine

This module forms part of the InaSAFE tool.

### Navigation

- index
- modules
- next
- previous
- Home »
- Contents »
- InaSAFE’s API documentation »
- Package::safe »
- Package::safe.common »