|
1 | 1 | import numpy as np
|
2 |
| -from .utils import mask_and_unmask_data, check_missing |
3 |
| -from ._accumulation import ( |
4 |
| - _ufunc_to_downstream, |
5 |
| - _ufunc_to_downstream_missing_values_2D, |
6 |
| - _ufunc_to_downstream_missing_values_ND, |
7 |
| -) |
| 2 | +from .utils import mask_and_unmask_data, check_missing, is_missing |
8 | 3 | from .core import flow
|
9 | 4 |
|
10 | 5 |
|
@@ -50,3 +45,88 @@ def operation(river_network, field, grouping, mv):
|
50 | 45 | return op(river_network, field, grouping, mv, ufunc=ufunc)
|
51 | 46 |
|
52 | 47 | return flow(river_network, field, False, operation, mv)
|
| 48 | + |
| 49 | + |
| 50 | +def _ufunc_to_downstream(river_network, field, grouping, mv, ufunc): |
| 51 | + """ |
| 52 | + Updates field in-place by applying a ufunc at the downstream nodes of the grouping, ignoring missing values. |
| 53 | +
|
| 54 | + Parameters |
| 55 | + ---------- |
| 56 | + river_network : earthkit.hydro.RiverNetwork |
| 57 | + An earthkit-hydro river network object. |
| 58 | + field : numpy.ndarray |
| 59 | + The input field. |
| 60 | + grouping : numpy.ndarray |
| 61 | + An array of indices. |
| 62 | + mv : scalar |
| 63 | + A missing value indicator (not used in the function but kept for consistency). |
| 64 | + ufunc : numpy.ufunc |
| 65 | + A universal function from the numpy library to be applied to the field data. |
| 66 | + Available ufuncs: https://numpy.org/doc/2.2/reference/ufuncs.html. Note: must allow two operands. |
| 67 | +
|
| 68 | + Returns |
| 69 | + ------- |
| 70 | + None |
| 71 | + """ |
| 72 | + ufunc.at(field, river_network.downstream_nodes[grouping], field[grouping]) |
| 73 | + |
| 74 | + |
| 75 | +def _ufunc_to_downstream_missing_values_2D(river_network, field, grouping, mv, ufunc): |
| 76 | + """ |
| 77 | + Applies a universal function (ufunc) to downstream nodes in a river network, dealing with missing values for 2D fields. |
| 78 | +
|
| 79 | + Parameters |
| 80 | + ---------- |
| 81 | + river_network : earthkit.hydro.RiverNetwork |
| 82 | + An earthkit-hydro river network object. |
| 83 | + field : numpy.ndarray |
| 84 | + The input field. |
| 85 | + grouping : numpy.ndarray |
| 86 | + An array of indices. |
| 87 | + mv : scalar |
| 88 | + A missing value indicator. |
| 89 | + ufunc : numpy.ufunc |
| 90 | + A universal function from the numpy library to be applied to the field data. |
| 91 | + Available ufuncs: https://numpy.org/doc/2.2/reference/ufuncs.html. Note: must allow two operands. |
| 92 | +
|
| 93 | + Returns |
| 94 | + ------- |
| 95 | + None |
| 96 | + """ |
| 97 | + nodes_to_update = river_network.downstream_nodes[grouping] |
| 98 | + values_to_add = field[grouping] |
| 99 | + missing_indices = np.logical_or(is_missing(values_to_add, mv), is_missing(field[nodes_to_update], mv)) |
| 100 | + ufunc.at(field, nodes_to_update, values_to_add) |
| 101 | + field[nodes_to_update[missing_indices]] = mv |
| 102 | + |
| 103 | + |
| 104 | +def _ufunc_to_downstream_missing_values_ND(river_network, field, grouping, mv, ufunc): |
| 105 | + """ |
| 106 | + Applies a universal function (ufunc) to downstream nodes in a river network, dealing with missing values for ND fields. |
| 107 | +
|
| 108 | + Parameters |
| 109 | + ---------- |
| 110 | + river_network : earthkit.hydro.RiverNetwork |
| 111 | + An earthkit-hydro river network object. |
| 112 | + field : numpy.ndarray |
| 113 | + The input field. |
| 114 | + grouping : numpy.ndarray |
| 115 | + An array of indices. |
| 116 | + mv : scalar |
| 117 | + A missing value indicator. |
| 118 | + ufunc : numpy.ufunc |
| 119 | + A universal function from the numpy library to be applied to the field data. |
| 120 | + Available ufuncs: https://numpy.org/doc/2.2/reference/ufuncs.html. Note: must allow two operands. |
| 121 | +
|
| 122 | + Returns |
| 123 | + ------- |
| 124 | + None |
| 125 | + """ |
| 126 | + nodes_to_update = river_network.downstream_nodes[grouping] |
| 127 | + values_to_add = field[grouping] |
| 128 | + missing_indices = np.logical_or(is_missing(values_to_add, mv), is_missing(field[nodes_to_update], mv)) |
| 129 | + ufunc.at(field, nodes_to_update, values_to_add) |
| 130 | + missing_indices = np.array(np.where(missing_indices)) |
| 131 | + missing_indices[0] = nodes_to_update[missing_indices[0]] |
| 132 | + field[tuple(missing_indices)] = mv |
0 commit comments