Skip to content

Feat/add plotter #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,30 +105,27 @@ Creates a `RiverNetwork`. Current options are
- source: An earthkit-data compatable source. See [list](https://earthkit-data.readthedocs.io/en/latest/guide/sources.html)

### Computing Metrics Over River Networks
_Currently supported metrics are "sum", "mean", "max", "min" and "product". If weights is provided, it is used to weight the field in the calculation._

```
ekh.calculate_catchment_metric(river_network, field, stations, metric, weights=None)
```
Calculates the metric over each catchment defined by stations. Current options are
- metric: "sum", "max", "min", "mean"
Calculates the metric over each catchment defined by stations.

```
ekh.calculate_subcatchment_metric(river_network, field, stations, metric, weights=None)
```
Calculates the metric over each subcatchment defined by stations. Current options are
- metric: "sum", "max", "min", "mean"
Calculates the metric over each subcatchment defined by stations.

```
ekh.calculate_upstream_metric(river_network, field, metric, weights=None)
```
Calculates a metric over all upstream nodes for a river network. If weights is provided, it is used to weight the field in the calculation. Options are
- metric: "sum", "mean", "max", "min"
Calculates a metric over all upstream nodes for a river network.

```
ekh.calculate_metric_for_labels(field, labels, metric, weights=None)
```
_(for advanced users)_ Calculates a metric over field for each label in the labels field. If weights is provided, it is used to weight the field in the calculation. Options are
- metric: "sum", "mean", "max", "min"
_(for advanced users)_ Calculates a metric over field for each label in the labels field.

```
ekh.flow_downstream(river_network, field)
Expand Down
1 change: 1 addition & 0 deletions src/earthkit/hydro/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .core import flow
from .label import calculate_metric_for_labels
from .movement import move_downstream, move_upstream
from .plot import plot
from .readers import (
create_river_network,
from_cama_downxy,
Expand Down
7 changes: 6 additions & 1 deletion src/earthkit/hydro/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,9 @@ class Min:
base_val = np.inf


metrics_dict = {"sum": Sum, "mean": Mean, "max": Max, "min": Min}
class Product:
func = np.multiply
base_val = 1


metrics_dict = {"sum": Sum, "mean": Mean, "max": Max, "min": Min, "product": Product}
33 changes: 33 additions & 0 deletions src/earthkit/hydro/plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import numpy as np


def plot(river_network, field, ax=None):

try:
import matplotlib.pyplot as plt
except ModuleNotFoundError:
raise ModuleNotFoundError(
"matplotlib.pyplot is required for plotting."
"\nTo install it, run `pip install matplotlib`"
)

x = np.arange(river_network.mask.shape[1])
y = np.arange(river_network.mask.shape[0])[::-1]

X, Y = np.meshgrid(x, y)
x = X[river_network.mask]
y = Y[river_network.mask]

not_sinks = river_network.downstream_nodes != river_network.n_nodes
u = np.zeros_like(x)
u[not_sinks] = x[river_network.downstream_nodes[not_sinks]] - x[not_sinks]
v = np.zeros_like(x)
v[not_sinks] = y[river_network.downstream_nodes[not_sinks]] - y[not_sinks]

c = field[river_network.mask]

if ax is None:
_, ax = plt.subplots()

ax.quiver(x, y, u, v, c, scale=1, scale_units="xy", angles="xy")
return ax
Loading