landlab

Miscellaneous raster-grid functions

calculate_flux_divergence_at_nodes(grid, active_link_flux, out=None)[source]

Net flux into or out of nodes.

Same as calculate_flux_divergence_at_core_cells, but works with and returns a list of net unit fluxes that corresponds to all nodes, rather than just core nodes.

Parameters:

grid : RasterModelGrid

Input grid.

active_link_flux : array_like

Flux values at links.

out : ndarray, optional

Alternative output array in which to place the result. Must be of the same shape and buffer length as the expected output.

See also

calculate_flux_divergence_at_active_cells

Notes

Note that we DO compute net unit fluxes at boundary nodes (even though these don’t have active cells associated with them, and often don’t have cells of any kind, because they are on the perimeter). It’s up to the user to decide what to do with these boundary values.

Examples

Calculate the gradient of values at a grid’s nodes.

>>> from landlab import RasterModelGrid
>>> rmg = RasterModelGrid((4, 5), spacing=1.0)
>>> u = np.array([0., 1., 2., 3., 0.,
...               1., 2., 3., 2., 3.,
...               0., 1., 2., 1., 2.,
...               0., 0., 2., 2., 0.])
>>> grad = rmg.calc_grad_at_link(u)[rmg.active_links]
>>> grad 
array([ 1.,  1., -1.,
        1.,  1., -1.,  1.,
       -1., -1., -1.,
        1.,  1., -1.,  1.,
       -1.,  0.,  1.])

Calculate the divergence of the gradients at each node.

>>> flux = - grad    # downhill flux proportional to gradient
>>> rmg.calculate_flux_divergence_at_nodes(flux)
...     
array([ 0., -1., -1.,  1.,  0.,
       -1.,  2.,  4., -2.,  1.,
       -1.,  0.,  1., -4.,  1.,
        0., -1.,  0.,  1.,  0.])

If calculate_gradients_at_nodes is called inside a loop, you can improve speed by creating an array outside the loop. For example, do this once, before the loop:

>>> df = rmg.zeros(centering='node') # outside loop
>>> rmg.number_of_nodes
20

Then do this inside the loop so that the function will not have to create the df array but instead puts values into the df array.

>>> df = rmg.calculate_flux_divergence_at_nodes(flux, out=df)
>>> grid = RasterModelGrid((4, 5), spacing=(1, 2))
>>> u = np.array([0., 1., 2., 3., 0.,
...               1., 2., 3., 2., 3.,
...               0., 1., 2., 1., 2.,
...               0., 0., 2., 2., 0.])
>>> grad = grid.calc_grad_at_link(2 * u)[grid.active_links]
>>> grad 
array([ 2.,  2., -2.,
        1.,  1., -1.,  1.,
       -2., -2., -2.,
        1.,  1., -1., 1.,
       -2.,  0.,  2.])
>>> grid.calculate_flux_divergence_at_nodes(- grad)
...     
array([ 0., -1., -1.,  1.,  0.,
       -1.,  2.,  4., -2.,  1.,
       -1.,  0.,  1., -4.,  1.,
        0., -1.,  0.,  1.,  0.])
calculate_slope_aspect_bfp(xs, ys, zs)[source]

Calculate slope and aspect.

Code author: Katy Barnhart <katherine.barnhart@colorado.edu>

Fits a plane to the given N points with given xs, ys, and zs values using single value decomposition.

Returns a tuple of (slope, aspect) based on the normal vector to the best fit plane.

Note

This function does not check if the points fall on a line, rather than a plane.

corner_node_at_cell(grid, corner_ids[, cell_ids])[source]

Return an array of the node ids for diagonal neighbors of cell_id cells. corner_ids is an index into the corners of a cell as measured clockwise starting from the southeast.

If cell_ids is not given, return neighbors for all cells in the grid.

Parameters:

grid : RasterModelGrid

Input grid.

corner_ids : array_like

IDs of the corner nodes.

cell_ids : array_like, optional

IDs of cell about which to get corners

Examples

>>> from landlab import RasterModelGrid
>>> from landlab.grid.raster_funcs import corner_node_at_cell
>>> grid = RasterModelGrid(4, 5, 1.0)
>>> corner_node_at_cell(grid, 0, 0)
array([2])

Get the lower-right and the the upper-left corners for all the cells.

>>> corner_node_at_cell(grid, 0)
array([2, 3, 4, 7, 8, 9])
>>> corner_node_at_cell(grid, 2)
array([10, 11, 12, 15, 16, 17])

As an alternative to the above, use fancy-indexing to get both sets of corners with one call.

>>> corner_node_at_cell(grid, np.array([0, 2]), [1, 4])
array([[ 3, 11],
       [ 8, 16]])
find_nearest_node(rmg, coords, mode='raise')[source]

Find the node nearest a point.

Find the index to the node nearest the given x, y coordinates. Coordinates are provided as numpy arrays in the coords tuple. coords is tuple of coordinates, one for each dimension.

The mode keyword to indicate what to do if a point is outside of the grid. Valid choices are the same as that used with the numpy function ravel_multi_index.

A point is considered to be outside of the grid if it is outside the perimeter of the grid by one half of the grid spacing.

Parameters:

rmg : RasterModelGrid

The source grid.

coords : tuple

Coordinates of point as (x, y)

mode : {‘raise’, ‘wrap’, ‘clip’}, optional

Action to take if a point is outside of the grid.

Returns:

array_like :

Indices of the nodes nearest the given coordinates.

Examples

Create a grid of 4 by 5 nodes with unit spacing.

>>> import landlab
>>> from landlab.grid.raster_funcs import find_nearest_node
>>> rmg = landlab.RasterModelGrid(4, 5)

The points can be either a tuple of scalars or of arrays.

>>> find_nearest_node(rmg, (0.2, 0.6))
5
>>> find_nearest_node(rmg, (np.array([1.6, 3.6]), np.array([2.3, .7])))
array([12,  9])

The mode keyword indicates what to do if a point is outside of the grid.

>>> find_nearest_node(rmg, (-0.6, 0.6), mode='raise')
Traceback (most recent call last):
    ...
ValueError: invalid entry in coordinates array
>>> find_nearest_node(rmg, (-0.6, 0.6), mode='clip')
5
>>> find_nearest_node(rmg, (-0.6, 0.6), mode='wrap')
9
is_coord_on_grid(rmg, coords, axes=(0, 1))[source]

Check if coordinates are contained on a grid.

Parameters:

rmg : RasterModelGrid

Source grid.

coords : tuple

Coordinates of point as (x, y)

axes : tuple, optional

Check bounds only on a particular axis

Examples

Create a grid that ranges from x=0 to x=4, and y=0 to y=3.

>>> from landlab import RasterModelGrid
>>> from landlab.grid.raster_funcs import is_coord_on_grid
>>> grid = RasterModelGrid(4, 5)
>>> is_coord_on_grid(grid, (3.999, 2.999))
True

Check two points with one call. Numpy broadcasting rules apply for the point coordinates.

>>> is_coord_on_grid(grid, ([3.9, 4.1], 2.9))
array([ True, False], dtype=bool)
>>> is_coord_on_grid(grid, ([3.9, 4.1], 2.9), axes=(0, ))
array([ True,  True], dtype=bool)

Return an array of the active link ids for neighbors of cell_id cells. link_ids is an index into the links of a cell as measured clockwise starting from the south.

If cell_ids is not given, return neighbors for all cells in the grid.

Parameters:

grid : RasterModelGrid

Source grid.

link_inds : array_like

IDs of links

cell_ids : array_like, optional

IDs of cells for which to get links

neighbor_node_at_cell(grid, neighbor_ids[, cell_ids])[source]

Return an array of the node ids for neighbors of cell_id cells. neighbor_ids is an index into the neighbors of a cell as measured clockwise starting from the south.

If cell_ids is not given, return neighbors for all cells in the grid.

Parameters:

grid : RasterModelGrid

Input grid.

neighbor_ids : array_like

IDs of the neighbor nodes.

cell_ids : array_like, optional

IDs of cell about which to get neighbors.

Returns:

ndarray

Node IDs for given neighbors of cells.

Examples

>>> from landlab import RasterModelGrid
>>> from landlab.grid.raster_funcs import neighbor_node_at_cell
>>> grid = RasterModelGrid(4, 5, 1.0)
>>> neighbor_node_at_cell(grid, 0, 0)
array([1])

Get the lower and the the upper neighbors for all the cells.

>>> neighbor_node_at_cell(grid, 0)
array([1, 2, 3, 6, 7, 8])
>>> neighbor_node_at_cell(grid, 2)
array([11, 12, 13, 16, 17, 18])

As an alternative to the above, use fancy-indexing to get both sets of neighbors with one call.

>>> neighbor_node_at_cell(grid, np.array([0, 2]), [1, 4])
array([[ 2, 12],
       [ 7, 17]])