Source code for landlab.grid.raster_mappers

#! /usr/bin/env python
"""Grid element mappers that are specific to raster grids.

Mapping functions unique to raster grids
++++++++++++++++++++++++++++++++++++++++

.. autosummary::

    ~landlab.grid.raster_mappers.map_sum_of_inlinks_to_node
    ~landlab.grid.raster_mappers.map_mean_of_inlinks_to_node
    ~landlab.grid.raster_mappers.map_max_of_inlinks_to_node
    ~landlab.grid.raster_mappers.map_min_of_inlinks_to_node
    ~landlab.grid.raster_mappers.map_sum_of_outlinks_to_node
    ~landlab.grid.raster_mappers.map_mean_of_outlinks_to_node
    ~landlab.grid.raster_mappers.map_max_of_outlinks_to_node
    ~landlab.grid.raster_mappers.map_min_of_outlinks_to_node
    ~landlab.grid.raster_mappers.map_mean_of_links_to_node
    ~landlab.grid.raster_mappers.map_mean_of_horizontal_links_to_node
    ~landlab.grid.raster_mappers.map_mean_of_horizontal_active_links_to_node
    ~landlab.grid.raster_mappers.map_mean_of_vertical_links_to_node
    ~landlab.grid.raster_mappers.map_mean_of_vertical_active_links_to_node
"""

import numpy as np


def _node_out_link_ids(shape):
    """Links leaving each node.

    Parameters
    ----------
    shape : tuple of int
        Shape of grid of nodes.

    Returns
    -------
    tuple :
        Tuple of array of link IDs as (vertical_links, horizontal_links).

    Examples
    --------
    >>> from landlab.grid.raster_mappers import _node_out_link_ids
    >>> (vert, horiz) = _node_out_link_ids((3, 4))
    >>> vert
    array([[ 3,  4,  5,  6],
           [10, 11, 12, 13],
           [-1, -1, -1, -1]])
    >>> horiz
    array([[ 0,  1,  2, -1],
           [ 7,  8,  9, -1],
           [14, 15, 16, -1]])
    """
    from ..graph.structured_quad.structured_quad import StructuredQuadGraphTopology

    layout = StructuredQuadGraphTopology(shape)

    node_horizontal_link_ids = np.empty(shape, int)
    node_horizontal_link_ids[:, :-1] = layout.horizontal_links.reshape(
        (shape[0], shape[1] - 1)
    )
    node_horizontal_link_ids[:, -1] = -1

    node_vertical_link_ids = np.empty(shape, int)
    node_vertical_link_ids[:-1, :] = layout.vertical_links.reshape(
        (shape[0] - 1, shape[1])
    )
    node_vertical_link_ids[-1, :] = -1

    return node_vertical_link_ids, node_horizontal_link_ids


def _node_in_link_ids(shape):
    """Links entering each node.

    Parameters
    ----------
    shape : tuple of int
        Shape of grid of nodes.

    Returns
    -------
    tuple :
        Tuple of array of link IDs as (vertical_links, horizontal_links).

    Examples
    --------
    >>> from landlab.grid.raster_mappers import _node_in_link_ids
    >>> (vert, horiz) = _node_in_link_ids((3, 4))
    >>> vert
    array([[-1, -1, -1, -1],
           [ 3,  4,  5,  6],
           [10, 11, 12, 13]])
    >>> horiz
    array([[-1,  0,  1,  2],
           [-1,  7,  8,  9],
           [-1, 14, 15, 16]])
    """
    from ..graph.structured_quad.structured_quad import StructuredQuadGraphTopology

    layout = StructuredQuadGraphTopology(shape)

    node_horizontal_link_ids = np.empty(shape, int)
    node_horizontal_link_ids[:, 1:] = layout.horizontal_links.reshape(
        (shape[0], shape[1] - 1)
    )
    node_horizontal_link_ids[:, 0] = -1

    node_vertical_link_ids = np.empty(shape, int)
    node_vertical_link_ids[1:, :] = layout.vertical_links.reshape(
        (shape[0] - 1, shape[1])
    )
    node_vertical_link_ids[0, :] = -1

    return node_vertical_link_ids, node_horizontal_link_ids


def _number_of_links_per_node(shape):
    """Number of links touching each node.

    Parameters
    ----------
    shape : tuple of int
        Shape of grid of nodes.

    Returns
    -------
    ndarray :
        Array of number of links per node.

    Examples
    --------
    >>> from landlab.grid.raster_mappers import _number_of_links_per_node
    >>> _number_of_links_per_node((3, 4))
    array([[2, 3, 3, 2],
           [3, 4, 4, 3],
           [2, 3, 3, 2]])
    """
    from ..graph.structured_quad.structured_quad import StructuredQuadGraphTopology

    layout = StructuredQuadGraphTopology(shape)

    n_links_at_node = np.full(shape[0] * shape[1], 4, int)
    n_links_at_node[layout.perimeter_nodes] = 3
    n_links_at_node[layout.corner_nodes] = 2

    return n_links_at_node.reshape(shape)