-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow user provided functions to write to disc (#35)
* allow user provided functions to write to disc * additional saving example * proofread docs * correct return statement * correct kwarg name in tutorial
- Loading branch information
1 parent
39c8181
commit fc1f331
Showing
8 changed files
with
155 additions
and
36 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
## Writing Routed Flows to Disc | ||
|
||
You can override the default function used by `river-route` when writing routed flows to disc. The MuskingumCunge class | ||
formats the discharge data into a Pandas DataFrame and then calls the `write_outflows` method. By default, this function | ||
writes the dataframe to a netCDF file. | ||
|
||
A single netCDF is not ideal for all use cases so you can override it to store your data how you prefer. Some examples | ||
of reasons you would want to do this include appending the outputs to an existing file, writing the DataFrame to a | ||
database, or to add metadata or attributes to the file. | ||
|
||
You can override the `write_outflows` method directly in your code or use the `set_write_outflows` method. Your custom | ||
function should accept exactly 3 keyword arguments: | ||
|
||
1. `df`: a Pandas DataFrame with a datetime index, river id numbers as column labels, and float discharge values. | ||
2. `outflow_file`: a string with the path to the output file. | ||
3. `runoff_file`: a string with the path to the runoff file used to produce this output. | ||
|
||
As an example, you might want to write the output DataFrame to a Parquet file instead. | ||
|
||
```python title="Write Routed Flows to Parquet" | ||
import pandas as pd | ||
|
||
import river_route as rr | ||
|
||
|
||
def custom_write_outflows(df: pd.DataFrame, outflow_file: str, runoff_file: str) -> None: | ||
df.to_parquet(outflow_file) | ||
return | ||
|
||
|
||
( | ||
rr | ||
.MuskingumCunge('config.yaml') | ||
.set_write_outflows(custom_write_outflows) | ||
.route() | ||
) | ||
``` | ||
|
||
```python title="Write Routed Flows to SQLite" | ||
import pandas as pd | ||
import sqlite3 | ||
|
||
import river_route as rr | ||
|
||
|
||
def write_outflows_to_sqlite(df: pd.DataFrame, outflow_file: str, runoff_file: str) -> None: | ||
conn = sqlite3.connect(outflow_file) | ||
df.to_sql('routed_flows', conn, if_exists='replace') | ||
conn.close() | ||
return | ||
|
||
|
||
( | ||
rr | ||
.MuskingumCunge('config.yaml') | ||
.set_write_outflows(write_outflows_to_sqlite) | ||
.route() | ||
) | ||
``` | ||
|
||
```python title="Append Routed Flows to Existing netCDF" | ||
import os | ||
|
||
import pandas as pd | ||
import xarray as xr | ||
|
||
import river_route as rr | ||
|
||
|
||
def append_to_existing_file(df: pd.DataFrame, outflow_file: str, runoff_file: str) -> None: | ||
ensemble_number = os.path.basename(runoff_file).split('_')[1] | ||
with xr.open_dataset(outflow_file) as ds: | ||
ds.sel(ensemble=ensemble_number).Q = df.values | ||
ds.to_netcdf(outflow_file) | ||
return | ||
|
||
|
||
( | ||
rr | ||
.MuskingumCunge('config.yaml') | ||
.set_write_outflows(append_to_existing_file) | ||
.route() | ||
) | ||
``` | ||
|
||
```python title="Save a Subset of the Routed Flows" | ||
import pandas as pd | ||
|
||
import river_route as rr | ||
|
||
|
||
def save_partial_results(df: pd.DataFrame, outflow_file: str, runoff_file: str) -> None: | ||
river_ids_to_save = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
df = df[river_ids_to_save] | ||
df.to_parquet(outflow_file) | ||
return | ||
|
||
|
||
( | ||
rr | ||
.MuskingumCunge('config.yaml') | ||
.set_write_outflows(save_partial_results) | ||
.route() | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
__version__ = '0.13.0' | ||
__version__ = '0.14.0' | ||
__author__ = 'Riley Hales PhD' | ||
__url__ = 'https://github.com/rileyhales/river-route' |