Skip to content
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

Create _streamflow_flow_indices.py #1832

Merged
merged 25 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c88593b
Create _streamflow_flow_indices.py
faimahsho Jul 8, 2024
8362e33
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 8, 2024
39351e2
Update _streamflow_flow_indices.py
faimahsho Jul 11, 2024
4795cbe
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 11, 2024
21381a9
Update xclim/indices/_streamflow_flow_indices.py
faimahsho Jul 11, 2024
d8b8876
Update xclim/indices/_streamflow_flow_indices.py
faimahsho Jul 11, 2024
48ded61
Update references.bib
faimahsho Jul 16, 2024
bb69dd4
Update _streamflow_flow_indices.py
faimahsho Jul 16, 2024
a1dcd76
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 16, 2024
4e086d6
return time series for high flow and low flow frequency, instead of m…
huard Jul 17, 2024
222b33b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 18, 2024
2d409ff
Merge branch 'main' into Watershed-indices
Zeitsperre Jul 25, 2024
e3dfa79
Update xclim/indices/_hydrology.py
faimahsho Jul 26, 2024
62d4ebe
Update xclim/indices/_hydrology.py
Zeitsperre Jul 26, 2024
163faa7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 26, 2024
fc4fc32
Merge branch 'main' into Watershed-indices
Zeitsperre Aug 22, 2024
a6cbc8f
Merge branch 'main' into Watershed-indices
huard Sep 17, 2024
adc9144
resolved merge conflicts. added indicator tests
huard Jul 17, 2024
e73b101
Merge branch 'main' into Watershed-indices
huard Sep 17, 2024
f8b4fce
Update xclim/data/fr.json
aulemahal Oct 2, 2024
e3f42bc
Merge branch 'main' into Watershed-indices
aulemahal Oct 2, 2024
1717e23
merge
huard Oct 7, 2024
3b02e3e
suggestions from review
huard Oct 7, 2024
8fabf4f
removed indexing to all three functions. I think the potential for co…
huard Oct 8, 2024
6118234
Merge branch 'main' into Watershed-indices
huard Oct 8, 2024
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
Binary file added .DS_Store
faimahsho marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ Contributors
* Dante Castro <[email protected]> `@profesorpaiche <https://github.com/profesorpaiche>`_
* Sascha Hofmann <[email protected]> `@saschahofmann <https://github.com/saschahofmann>`_
* Javier Diez-Sierra <[email protected]> `@JavierDiezSierra <https://github.com/JavierDiezSierra>`_
* Faisal Mahmood <[email protected]> <[email protected]> `@faimahsho <https://github.com/faimahsho>`_
32 changes: 32 additions & 0 deletions docs/references.bib
Original file line number Diff line number Diff line change
Expand Up @@ -2152,3 +2152,35 @@ @article{droogers2002
url = {https://www.scopus.com/inward/record.uri?eid=2-s2.0-0036464359&doi=10.1023%2fA%3a1015508322413&partnerID=40&md5=7322aaa4c6874878f5b1dab3c73c1718},
type = {Article}
}

@article{article,
author = {Addor, Nans and Nearing, Grey and Prieto, Cristina and Newman, A. and Le Vine, Nataliya and Clark, Martyn},
year = {2018},
month = {11},
pages = {},
title = {A Ranking of Hydrological Signatures Based on Their Predictability in Space},
journal = {Water Resources Research},
doi = {10.1029/2018WR022606}
}

@article{article,
author = {Clausen, B and Biggs, Barry},
year = {2000},
month = {11},
pages = {184-197},
title = {Flow variables for ecological studies in temperate streams: Groupings based on covariance},
volume = {237},
journal = {Journal of Hydrology},
doi = {10.1016/S0022-1694(00)00306-1}
}

@article{article,
author = {Olden, Julian and Poff, N.},
year = {2003},
month = {03},
pages = {101 - 121},
title = {Redundancy and the Choice of Hydrologic Indices for Characterizing Stream Flow Regimes},
volume = {19},
journal = {River Research and Applications},
doi = {10.1002/rra.700}
}
faimahsho marked this conversation as resolved.
Show resolved Hide resolved
Binary file added xclim/.DS_Store
Binary file not shown.
124 changes: 124 additions & 0 deletions xclim/indices/_streamflow_flow_indices.py
faimahsho marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
from __future__ import annotations

from xclim.core.units import declare_units


@declare_units(q="[discharge]")
def flow_index(q: xr.DataArray, p: float = 0.95) -> xr.DataArray:
"""
faimahsho marked this conversation as resolved.
Show resolved Hide resolved
Calculate the Qp (pth percentile of daily streamflow) normalized by the mean 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
faimahsho marked this conversation as resolved.
Show resolved Hide resolved
faimahsho marked this conversation as resolved.
Show resolved Hide resolved

Parameters
----------
q : xarray.DataArray
Daily streamflow data.
p : float
Percentile for calculating the flow index, between 0 and 1. Default of 0.95 is for high flows.

Returns
-------
xarray.DataArray
out = Normalized Qp, which is the p th percentile of daily streamflow normalized by the median flow.
faimahsho marked this conversation as resolved.
Show resolved Hide resolved

"""
qp = q.quantile(p, dim="time")
q_median = q.median(dim="time")
out = qp / q_median
out.attrs["units"] = " "
return out.rename("flow_index")


@declare_units(q="[discharge]")
def high_flow_frequency(
q: xr.DataArray,
threshold_factor: int = 9
freq: str = "A-SEP",
statistic: str = "mean",
) -> xr.DataArray:
"""
Calculate the mean number of days in a given period with flows greater than a specified threshold. By default, the period is the water year starting on 1st October and ending on 30th September, as commonly defined in North America.

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

Parameters
----------
q : xarray.DataArray
Daily streamflow data.
threshold_factor : float, optional
Factor by which the median flow is multiplied to set the high flow threshold, default is 9.0.
freq : str, optional
Resampling frequency, default is 'A-SEP' for water year ending in September.
statistic : str, optional
Type of statistic to return ('mean', 'sum', 'max', median etc.), default is 'mean'.

Returns
-------
xarray.DataArray
Calculated statistic of high flow days per water year, by default it is set as mean
"""
median_flow = q.median(dim="time")
threshold = threshold_factor * median_flow

# Resample data to the given frequency and count days above threshold
high_flow_days = (q > threshold).resample(time=freq).sum(dim="time")

# Dynamically apply the chosen statistic using getattr
out = getattr(high_flow_days, statistic)(dim="time")
faimahsho marked this conversation as resolved.
Show resolved Hide resolved

# Assign units to the result based on the statistic
out.attrs["units"] = "days/year" if statistic == "mean" else "days"
faimahsho marked this conversation as resolved.
Show resolved Hide resolved

# Rename the result for clarity
return out.rename(f"high flow frequency({statistic})")
faimahsho marked this conversation as resolved.
Show resolved Hide resolved


@declare_units(q="[discharge]")
def low_flow_frequency(
q: xr.DataArray,
threshold_factor: float = 0.2,
freq: str = "A-SEP",
statistic: str = "mean",
) -> xr.DataArray:
"""
Calculate the specified statistic of the number of days in a given period with flows lower than a specified threshold.
By default, the period is the water year starting on 1st October and ending on 30th September, as commonly defined in North America.

Reference:
faimahsho marked this conversation as resolved.
Show resolved Hide resolved
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

Parameters
----------
q : xarray.DataArray
Daily streamflow data.
threshold_factor : float, optional
Factor by which the mean flow is multiplied to set the low flow threshold, default is 0.2.
freq : str, optional
Resampling frequency, default is 'A-SEP' for water year ending in September.
statistic : str, optional
Type of statistic to return ('mean', 'sum', 'max', median etc.), default is 'mean'.

Returns
-------
xarray.DataArray
Calculated statistic of low flow days per water year, by default it is set as mean
"""
mean_flow = q.mean(dim="time")
threshold = threshold_factor * mean_flow

# Resample data to the given frequency and count days below threshold
low_flow_days = (q < threshold).resample(time=freq).sum(dim="time")

# Dynamically apply the chosen statistic using getattr
out = getattr(low_flow_days, statistic)(dim="time")

# Assign units to the result based on the statistic
out.attrs["units"] = "days/year" if statistic == "mean" else "days"

# Rename the result for clarity
return out.rename(f"low flow frequency({statistic})")
faimahsho marked this conversation as resolved.
Show resolved Hide resolved