Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Jul 16, 2024
1 parent 0507d4c commit 8641c3b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 52 deletions.
7 changes: 3 additions & 4 deletions tests/test_hydrology.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_simple(self, snw_series, pr_series):

# 1 kg/ m2 /d of rain on day 11
b = np.zeros(365)
b[11] = 1.0 / 60 ** 2 / 24
b[11] = 1.0 / 60**2 / 24
pr = pr_series(b, start="1999-07-01")

out = xci.melt_and_precip_max(snw, pr)
Expand All @@ -87,7 +87,7 @@ def test_simple(self, q_series):
a[50:60] = 10
a[200:210] = 20
q = q_series(a)
out = xci.high_flow_frequency(q, 9, freq='YS')
out = xci.high_flow_frequency(q, 9, freq="YS")
np.testing.assert_array_equal(out, 20)


Expand All @@ -97,6 +97,5 @@ def test_simple(self, q_series):
a[50:60] = 1
a[200:210] = 1
q = q_series(a)
out = xci.low_flow_frequency(q, 0.2, freq='YS')
out = xci.low_flow_frequency(q, 0.2, freq="YS")
np.testing.assert_array_equal(out, 20)

84 changes: 43 additions & 41 deletions xclim/indices/_hydrology.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
from xclim.core.calendar import get_calendar
from xclim.core.missing import at_least_n_valid
from xclim.core.units import declare_units, rate2amount
from xclim.indices.generic import compare, threshold_count
from xclim.indices.generic import compare

from . import generic

__all__ = [
"base_flow_index",
"flow_index",
"high_flow_frequency",
"low_flow_frequency",
"melt_and_precip_max",
"rb_flashiness_index",
"snd_max",
"snd_max_doy",
"snow_melt_we_max",
"snw_max",
"snw_max_doy",
"flow_index",
"high_flow_frequency",
"low_flow_frequency"
]


Expand Down Expand Up @@ -288,46 +288,46 @@ def melt_and_precip_max(
@declare_units(q="[discharge]")
def flow_index(q: xr.DataArray, p: float = 0.95) -> xr.DataArray:
"""
Calculate the Qp (pth percentile of daily streamflow) normalized by the mean flow.
Calculate the Qp (pth percentile of daily streamflow) normalized by the mean flow.
Parameters
----------
q : xarray.DataArray
Daily streamflow data.
p : float
Percentile for
args = (<xarray.DataArray 'q' ()> Size: 8B
array(5.)
Coordinates:
quantile float64 8B 0.95
Attributes:
units: , 0.08202247191011236)
kwds = {}
@wraps(func)
def inner(*args, **kwds):
with self._recreate_cm():
> return func(*args, **kwds)
E AssertionError:
E Arrays are not almost equal to 7 decimals
E ACTUAL: <xarray.DataArray 'q' ()> Size: 8B
E array(5.)
E Coordinates:...
E DESIRED: 0.08202247191011236
/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/contextlib.py:81: AssertionError
Process finished with exit code 1
calculating the flow index, between 0 and 1. Default of 0.95 is for high flows.
q : xarray.DataArray
Daily streamflow data.
p : float
Percentile for
args = (<xarray.DataArray 'q' ()> Size: 8B
array(5.)
Coordinates:
quantile float64 8B 0.95
Attributes:
units: , 0.08202247191011236)
kwds = {}
@wraps(func)
def inner(*args, **kwds):
with self._recreate_cm():
> return func(*args, **kwds)
E AssertionError:
E Arrays are not almost equal to 7 decimals
E ACTUAL: <xarray.DataArray 'q' ()> Size: 8B
E array(5.)
E Coordinates:...
E DESIRED: 0.08202247191011236
/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/contextlib.py:81: AssertionError
Process finished with exit code 1
calculating the flow index, between 0 and 1. Default of 0.95 is for high flows.
Returns
-------
xarray.DataArray
Normalized Qp, which is the p th percentile of daily streamflow normalized by the median flow.
xarray.DataArray
Normalized Qp, which is the p th percentile of daily streamflow normalized by the median flow.
Reference:
1. Addor, Nans & Nearing, Grey & Prieto, Cristina & Newman, A. & Le Vine, Nataliya & Clark, Martyn. (2018). A Ranking of Hydrological Signatures Based on Their Predictability in Space. Water Resources Research. 10.1029/2018WR022606.
2. Clausen, B., & Biggs, B. J. F. (2000). Flow variables for ecological studies in temperate streams: Groupings based on covariance. Journal of Hydrology, 237(3–4), 184–197. https://doi.org/10.1016/S0022-1694(00)00306-1
Reference:
1. Addor, Nans & Nearing, Grey & Prieto, Cristina & Newman, A. & Le Vine, Nataliya & Clark, Martyn. (2018). A Ranking of Hydrological Signatures Based on Their Predictability in Space. Water Resources Research. 10.1029/2018WR022606.
2. Clausen, B., & Biggs, B. J. F. (2000). Flow variables for ecological studies in temperate streams: Groupings based on covariance. Journal of Hydrology, 237(3–4), 184–197. https://doi.org/10.1016/S0022-1694(00)00306-1
"""
qp = q.quantile(p, dim="time")
Expand Down Expand Up @@ -367,11 +367,12 @@ def high_flow_frequency(
1. Addor, Nans & Nearing, Grey & Prieto, Cristina & Newman, A. & Le Vine, Nataliya & Clark, Martyn. (2018). A Ranking of Hydrological Signatures Based on Their Predictability in Space. Water Resources Research. 10.1029/2018WR022606.
2. Clausen, B., & Biggs, B. J. F. (2000). Flow variables for ecological studies in temperate streams: Groupings based on covariance. Journal of Hydrology, 237(3–4), 184–197. https://doi.org/10.1016/S0022-1694(00)00306-1
"""

median_flow = q.median(dim="time")
with xr.set_options(keep_attrs=True):
threshold = threshold_factor * median_flow
high_flow_days = compare(q, op=">", right=threshold).resample(time=freq).sum(dim="time")
high_flow_days = (
compare(q, op=">", right=threshold).resample(time=freq).sum(dim="time")
)
out = high_flow_days.mean(dim="time")
out.attrs["units"] = "days"
return out
Expand Down Expand Up @@ -407,11 +408,12 @@ def low_flow_frequency(
Olden, J. D., & Poff, N. L. (2003). Redundancy and the choice of hydrologic indices for characterizing streamflow regimes. River Research and
Applications, 19(2), 101–121. https://doi.org/10.1002/rra.700
"""

mean_flow = q.mean(dim="time")
with xr.set_options(keep_attrs=True):
threshold = threshold_factor * mean_flow
low_flow_days = compare(q, op="<", right=threshold).resample(time=freq).sum(dim="time")
low_flow_days = (
compare(q, op="<", right=threshold).resample(time=freq).sum(dim="time")
)
out = low_flow_days.mean(dim="time")
out.attrs["units"] = "days"
return out
17 changes: 10 additions & 7 deletions xclim/indices/_streamflow_flow_indices.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from __future__ import annotations

import xarray as xr
import numpy as np

from xclim.core.units import declare_units
from xclim.indices.generic import compare, threshold_count
from xclim.indices.generic import compare

__all__ = [
"flow_index",
"high_flow_frequency",
"low_flow_frequency",
]


@declare_units(q="[discharge]")
def flow_index(q: xr.DataArray, p: float = 0.95) -> xr.DataArray:
"""
Expand Down Expand Up @@ -69,11 +71,12 @@ def high_flow_frequency(
1. Addor, Nans & Nearing, Grey & Prieto, Cristina & Newman, A. & Le Vine, Nataliya & Clark, Martyn. (2018). A Ranking of Hydrological Signatures Based on Their Predictability in Space. Water Resources Research. 10.1029/2018WR022606.
2. Clausen, B., & Biggs, B. J. F. (2000). Flow variables for ecological studies in temperate streams: Groupings based on covariance. Journal of Hydrology, 237(3–4), 184–197. https://doi.org/10.1016/S0022-1694(00)00306-1
"""

median_flow = q.median(dim="time")
with xr.set_options(keep_attrs=True):
threshold = threshold_factor * median_flow
high_flow_days = compare(q, op=">", right=threshold).resample(time=freq).sum(dim="time")
high_flow_days = (
compare(q, op=">", right=threshold).resample(time=freq).sum(dim="time")
)
out = high_flow_days.mean(dim="time")
out.attrs["units"] = "days/year"
return out
Expand Down Expand Up @@ -109,12 +112,12 @@ def low_flow_frequency(
Olden, J. D., & Poff, N. L. (2003). Redundancy and the choice of hydrologic indices for characterizing streamflow regimes. River Research and
Applications, 19(2), 101–121. https://doi.org/10.1002/rra.700
"""

mean_flow = q.mean(dim="time")
with xr.set_options(keep_attrs=True):
threshold = threshold_factor * mean_flow
low_flow_days = compare(q, op="<", right=threshold).resample(time=freq).sum(dim="time")
low_flow_days = (
compare(q, op="<", right=threshold).resample(time=freq).sum(dim="time")
)
out = low_flow_days.mean(dim="time")
out.attrs["units"] = "days"
return out

0 comments on commit 8641c3b

Please sign in to comment.