-
Notifications
You must be signed in to change notification settings - Fork 9
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
Nutrient concentrations #72
Changes from 5 commits
662d5ad
da4085a
66dc33f
8710464
6abab8e
41514f5
e54ea41
6a03bf5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the greywater and blackwater type. In the future this may be extended to light grey and dark grey, blue, green and yellow water :-) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,10 @@ | |
import numpy as np | ||
from dataclasses import dataclass | ||
from typing import Union | ||
import toml | ||
import xarray as xr | ||
import os | ||
from pysimdeum.data import DATA_DIR | ||
|
||
|
||
def chooser(data: Union[pd.Series, pd.DataFrame], myproperty: str=''): | ||
|
@@ -195,12 +199,10 @@ def handle_discharge_spillover(discharge, discharge_pattern, time, discharge_tim | |
""" | ||
if discharge_time >= (total_days * 24 * 60 * 60): | ||
spillover_time = discharge_time - end_of_day | ||
discharge[spillover_time, j, ind_enduse, pattern_num, 0] = discharge_pattern[time] | ||
discharge[spillover_time, j, ind_enduse, pattern_num, 1] = discharge_pattern[time] | ||
else: | ||
# Continue to the next day | ||
discharge[discharge_time, j, ind_enduse, pattern_num, 0] = discharge_pattern[time] | ||
|
||
#discharge[spillover_time, j, ind_enduse, pattern_num, 0] = discharge_pattern[time] | ||
discharge[discharge_time, j, ind_enduse, pattern_num, 1] = discharge_pattern[time] | ||
|
||
return discharge | ||
|
||
|
@@ -354,6 +356,65 @@ def complex_discharge_pattern(config, enduse_pattern, resolution='1s'): | |
return discharge_pattern | ||
|
||
|
||
def process_discharge_nutrients(discharge): | ||
"""Process discharge data and add nutrient concentrations based on values from a .toml file. | ||
|
||
This function reads nutrient multipliers from a TOML file, converts the discharge data from an | ||
xarray.DataArray to a pandas DataFrame, calculates the nutrient concentrations based on the | ||
multipliers, and adds the nutrient data back to an xarray.Dataset. | ||
|
||
Args: | ||
discharge (xr.DataArray): The discharge data. | ||
|
||
Returns: | ||
xr.Dataset: The updated xarray.Dataset containing the discharge data and the nutrient concentrations. | ||
""" | ||
|
||
toml_file_path = os.path.join(DATA_DIR, 'NL', 'ww_nutrients.toml') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The hard coded There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point, we'll address that in your PR when pulling from master #77 |
||
|
||
# Read the .toml file | ||
nutrient_data = toml.load(toml_file_path) | ||
|
||
# Convert a xarray.DataArray to a pd.DataFrame | ||
df = discharge.to_dataframe(name='flow').reset_index() | ||
|
||
# list of nutrient types | ||
nutrients = ['n', 'p', 'cod', 'bod5', 'ss', 'amm'] | ||
|
||
# Add new columns for each nutrient initialised to zero | ||
for nutrient in nutrients: | ||
df[nutrient] = 0.0 | ||
|
||
# Set the values for each nutrient based on the multipliers from the TOML file | ||
for nutrient in nutrients: | ||
for enduse in nutrient_data.keys(): | ||
multiplier = nutrient_data[enduse][nutrient] | ||
df.loc[df['enduse'] == enduse, nutrient] = df['flow'] * multiplier | ||
|
||
# Create an xarray.Dataset and add the discharge DataArray to it | ||
ds = xr.Dataset({'discharge': discharge}) | ||
|
||
# Add the pandas DataFrame to the xarray.Dataset as a new variable | ||
ds['df'] = (('index', 'columns'), df.values) | ||
ds['df_index'] = ('index', df.index) | ||
ds['df_columns'] = ('columns', df.columns) | ||
|
||
return ds | ||
|
||
|
||
def dataset_to_df(ds): | ||
"""Convert an xarray.Dataset to a pd.DataFrame. | ||
|
||
Args: | ||
ds (xr.Dataset): The input xarray.Dataset containing the 'df', 'df_index', and 'df_columns' variables. | ||
|
||
Returns: | ||
pd.DataFrame: The converted pandas DataFrame. | ||
""" | ||
df_from_ds = pd.DataFrame(data=ds['df'].values, index=ds['df_index'].values, columns=ds['df_columns'].values) | ||
|
||
return df_from_ds | ||
|
||
@dataclass | ||
class Base: | ||
"""Base class of pysimdeum for generating objects. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for the Wc, I can imagine you would have different values for a full flush (faeces) and half flush (urine). For the dishwasher and washing machine you may also want to discern between first discharge (dirt and grease), second discharge (soap residu added) and 3rd + 4th discharge (mainly water). This may be too much detail for now, but would be nice to at least have a simple solution in a later version. We may want to discuss this next week There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This makes sense. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Nutrient statistics per enduse | ||
|
||
[Shower] | ||
n = 0.1 | ||
p = 0.2 | ||
cod = 0.3 | ||
bod5 = 0.4 | ||
ss = 0.5 | ||
amm = 0.6 | ||
|
||
[Bathtub] | ||
n = 0.1 | ||
p = 0.2 | ||
cod = 0.3 | ||
bod5 = 0.4 | ||
ss = 0.5 | ||
amm = 0.6 | ||
|
||
[Wc] | ||
n = 0.1 | ||
p = 0.2 | ||
cod = 0.3 | ||
bod5 = 0.4 | ||
ss = 0.5 | ||
amm = 0.6 | ||
|
||
[KitchenTap] | ||
n = 0.1 | ||
p = 0.2 | ||
cod = 0.3 | ||
bod5 = 0.4 | ||
ss = 0.5 | ||
amm = 0.6 | ||
|
||
[BathroomTap] | ||
n = 0.1 | ||
p = 0.2 | ||
cod = 0.3 | ||
bod5 = 0.4 | ||
ss = 0.5 | ||
amm = 0.6 | ||
|
||
[WashingMachine] | ||
n = 0.1 | ||
p = 0.2 | ||
cod = 0.3 | ||
bod5 = 0.4 | ||
ss = 0.5 | ||
amm = 0.6 | ||
|
||
[Dishwasher] | ||
n = 0.1 | ||
p = 0.2 | ||
cod = 0.3 | ||
bod5 = 0.4 | ||
ss = 0.5 | ||
amm = 0.6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a useful addition!