# Source code for landlab.utils.source_tracking_algorithm

#! /usr/bin/env python
"""
Source Tracking Algorithm
+++++++++++++++++++++++++
.. autosummary::

~landlab.utils.source_tracking_algorithm.convert_arc_flow_directions_to_landlab_node_ids
~landlab.utils.source_tracking_algorithm.track_source
~landlab.utils.source_tracking_algorithm.find_unique_upstream_hsd_ids_and_fractions

Authors: Sai Nudurupati & Erkan Istanbulluoglu

Ref 1: 'The Landlab LandslideProbability Component User Manual' @
https://github.com/RondaStrauch/pub_strauch_etal_esurf/blob/master/LandslideComponentUsersManual.pdf

+----------+-------------------------------------------------------------------+
| Notation | Definition                                                        |
+==========+===================================================================+
| MD       | Modeling Domain - Raster grid that is being analyzed/worked upon. |
+----------+-------------------------------------------------------------------+
+ HSD      | Hydrologic Source Domain - Grid that is at least as coarse as MD. |
|          | For more info, refer Ref 1                                        |
+----------+-------------------------------------------------------------------+

"""
import copy
from collections import Counter

import numpy as np

[docs]def convert_arc_flow_directions_to_landlab_node_ids(grid, flow_dir_arc): """Convert Arc flow_directions to RasterModelGrid node ids. This function receives flow directions (D8) from ESRI ArcGIS and converts them to Landlab's RasterModelGrid node id. ESRI ArcGIS D8 flow directions are either of the eight valid output directions relating to the eight adjacent cells into which flow could travel. The valid output directions are powers of 2 starting from 2^0 (1) in the Eastern neighbor going clockwise to 2^7 (128) at Northeastern neighbor. For more information refer 'https://pro.arcgis.com/en/pro-app/tool-reference/spatial-analyst/ how-flow-direction-works.htm' Parameters ---------- grid: RasterModelGrid A grid. flow_dir_arc: ndarray of int, shape (n_nodes, ) flow directions derived from ESRII ArcGIS. Returns ------- receiver_nodes: ndarray of int, shape (n_nodes, ) downstream node at each node. Note that this array gives the receiver nodes only for the core nodes. For non-core nodes, a zero is used. """ r_arc_raw = np.log2(flow_dir_arc) r_arc_raw = r_arc_raw.astype("int") neigh_ = grid.adjacent_nodes_at_node diag_ = grid.diagonals_at_node neigh_ = np.fliplr(neigh_) diag_ = np.fliplr(diag_) a_n = np.hsplit(neigh_, 4) a_d = np.hsplit(diag_, 4) neighbors = np.hstack( (a_n[-1], a_d[0], a_n[0], a_d[1], a_n[1], a_d[2], a_n[2], a_d[3]) ) # Now neighbors has node ids of neighboring nodes in cw order starting at # right, hence the order of neighbors = [r, br, b, bl, l, tl, t, tr] receiver_nodes = np.zeros(grid.number_of_nodes, dtype=int) receiver_nodes[grid.core_nodes] = np.choose( r_arc_raw[grid.core_nodes], np.transpose(neighbors[grid.core_nodes]) ) return receiver_nodes
# %% # Source Routing Algorithm # Note 1: This algorithm works on core nodes only because core nodes # have neighbors that are real values and not -1s. # Note 2: Nodes in the following comments in this section refer to core nodes.