def test_scalar_arg():
    grad = rfuncs.calculate_steepest_descent_across_cell_faces(
        _GRID, _VALUES_AT_NODES, 0)
    assert_equal(grad, -5.)

    grad = _GRID.calculate_steepest_descent_across_cell_faces(_VALUES_AT_NODES, 0)
    assert_equal(grad, -5.)
def test_scalar_arg():
    grad = rfuncs.calculate_steepest_descent_across_cell_faces(
        _GRID, _VALUES_AT_NODES, 0)
    assert_equal(grad, -5.)

    grad = _GRID.calculate_steepest_descent_across_cell_faces(_VALUES_AT_NODES, 0)
    assert_equal(grad, -5.)
def test_iterable():
    grad = rfuncs.calculate_steepest_descent_across_cell_faces(
        _GRID, _VALUES_AT_NODES, [0, 4])
    assert_array_equal(grad, [-5., -5.])

    grad = _GRID.calculate_steepest_descent_across_cell_faces(
        _VALUES_AT_NODES, [0, 4])
    assert_array_equal(grad, [-5., -5.])
def test_iterable():
    grad = rfuncs.calculate_steepest_descent_across_cell_faces(
        _GRID, _VALUES_AT_NODES, [0, 4])
    assert_array_equal(grad, [-5., -5.])

    grad = _GRID.calculate_steepest_descent_across_cell_faces(
        _VALUES_AT_NODES, [0, 4])
    assert_array_equal(grad, [-5., -5.])
Esempio n. 5
0
def grid_flow_directions(grid, elevations):
    """Flow directions on raster grid.

    Calculate flow directions for node elevations on a raster grid. 
    Each node is assigned a single direction, toward one of its neighboring
    nodes (or itself, if none of its neighbors are lower). There is only
    flow from one node to another if there is a negative gradient. If a
    node's steepest gradient is >= 0., then its slope is set to zero and
    its receiver node is listed as itself.

    Parameters
    ----------
    grid : RasterModelGrid
        a raster grid.
    elevations: ndarray
        Node elevations.

    Returns
    -------
    receiver : (ncells, ) ndarray
        For each cell, the node in the direction of steepest descent, or
        itself if no downstream nodes.
    steepest_slope : (ncells, ) ndarray
        The slope value in the steepest direction of flow.

    Notes
    -----
    This function considers only nodes that have four neighbors. Thus, only
    calculate flow directions and slopes for nodes that have associated
    cells.

    Examples
    --------
    This example calculates flow routing on a (4,5) raster grid with the
    following node elevations::

        5 - 5 - 5 - 5 - 5
        |   |   |   |   |
        5 - 3 - 4 - 3 - 5
        |   |   |   |   |
        5 - 1 - 2 - 2 - 5
        |   |   |   |   |
        5 - 0 - 5 - 5 - 5
        
    >>> import numpy as np
    >>> from landlab import RasterModelGrid
    >>> from landlab.components.flow_routing.flow_direction_DN import grid_flow_directions
    >>> mg = RasterModelGrid(4,5)
    >>> z = np.array([5., 0., 5., 5., 5.,
    ...               5., 1., 2., 2., 5.,
    ...               5., 3., 4., 3., 5.,
    ...               5., 5., 5., 5., 5.])
    >>> recv_nodes, slope = grid_flow_directions(mg, z)

    Each node with a cell has a receiving node (although that node may be
    itself).

    >>> recv_nodes
    array([1, 6, 8, 6, 7, 8])

    All positive gradients are clipped to zero.

    >>> slope
    array([-1., -1.,  0., -2., -2., -1.])

    If a cell has no surrounding neighbors lower than itself, it is a sink.
    Use :attr:`~landlab.grid.base.ModelGrid.node_index_at_cells` to get the
    nodes associated with the cells.

    >>> sink_cells = np.where(slope >= 0)[0]
    >>> sink_cells
    array([2])
    >>> mg.node_index_at_cells[sink_cells] # Sink nodes
    array([8])

    The source/destination node pairs for the flow.

    >>> zip(mg.node_index_at_cells, recv_nodes)
    [(6, 1), (7, 6), (8, 8), (11, 6), (12, 7), (13, 8)]
    """
    slope, receiver = calculate_steepest_descent_across_cell_faces(
        grid, elevations, return_node=True)

    (sink_cell, ) = numpy.where(slope >= 0.)
    receiver[sink_cell] = grid.node_index_at_cells[sink_cell]
    slope[sink_cell] = 0.

    return receiver, slope