Skip to content

Commit

Permalink
start setting up runoff functions
Browse files Browse the repository at this point in the history
  • Loading branch information
elbeejay committed Jul 17, 2023
1 parent 53c413e commit c05f0b4
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
from hyswap import percentiles
from hyswap import cumulative
from hyswap import plots
from hyswap import runoff
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
Expand Down
7 changes: 7 additions & 0 deletions docs/source/reference/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,10 @@ Plotting Functions
.. automodule:: hyswap.plots
:members:
:special-members:

Runoff Calculation Functions
-----------------------------

.. automodule:: hyswap.runoff
:members:
:special-members:
1 change: 1 addition & 0 deletions hyswap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from hyswap.percentiles import * # noqa
from hyswap.cumulative import * # noqa
from hyswap.plots import * # noqa
from hyswap.runoff import * # noqa

try:
__version__ = version('hyswap')
Expand Down
80 changes: 80 additions & 0 deletions hyswap/runoff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
"""Runoff functions for hyswap."""


def convert_cfs_to_mmyr(cfs, drainage_area):
"""Convert cfs to mm/yr.
Parameters
----------
cfs : float
Flow in cubic feet per second.
drainage_area : float
Drainage area in km2.
Returns
-------
float
Runoff in mm/yr.
Examples
--------
Convert 14 cfs to mm/yr for a 250 km2 drainage area.
.. doctest::
>>> mmyr = runoff.convert_cfs_to_mmyr(14, 250)
>>> np.round(mmyr)
50.0
"""
# convert cfs to cubic feet per year
cpy = cfs * 60 * 60 * 24 * 365.25
# convert cubic feet per year to cubic meters per year
cpy = cpy * (0.3048 ** 3)
# convert drainage area km2 to m2
drainage_area = drainage_area * 1000 * 1000
# convert cubic meters per year to mm per year
mmyr = cpy / drainage_area * 1000
return mmyr


def streamflow_to_runoff(df, data_col, drainage_area):
"""Convert streamflow to runoff for a given drainage area.
For a given gage/dataframe, convert streamflow to runoff using the
drainage area and the convert_cfs_to_mmyr function.
Parameters
----------
df : pandas.DataFrame
DataFrame containing streamflow data.
data_col : str
Column name containing streamflow data, assumed to be in cfs.
drainage_area : float
Drainage area in km2.
Returns
-------
pandas.DataFrame
DataFrame containing runoff data in a column named 'runoff'.
Examples
--------
Convert streamflow to runoff for a given drainage area.
.. doctest::
>>> df = pd.DataFrame({'streamflow': [14, 15, 16]})
>>> runoff_df = runoff.streamflow_to_runoff(df, 'streamflow', 250)
>>> print(runoff_df['runoff'].round(1))
0 50.0
1 53.6
2 57.2
Name: runoff, dtype: float64
"""
df['runoff'] = df[data_col].apply(
lambda x: convert_cfs_to_mmyr(x, drainage_area)
)
return df
17 changes: 17 additions & 0 deletions tests/test_runoff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Tests for the runoff.py module."""
import pytest
from hyswap import runoff
import pandas as pd


def test_convert_cfs_to_mmyr():
"""Test the convert_cfs_to_mmyr function."""
mmyr = runoff.convert_cfs_to_mmyr(14, 250)
assert pytest.approx(mmyr, 0.1) == 50.0


def test_streamflow_to_runoff():
"""Test the streamflow_to_runoff function."""
df = pd.DataFrame({"streamflow": [14, 15, 16]})
runoff_df = runoff.streamflow_to_runoff(df, "streamflow", 250)
assert pytest.approx(runoff_df["runoff"].round(1)) == [50.0, 53.6, 57.2]

0 comments on commit c05f0b4

Please sign in to comment.