# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: 2022-2023 Tanguy Fardet
# SPDX-License-Identifier: GPL-3.0-or-later
# orgmatt/impacts/fertilizers.py
from typing import Optional
from pint import Quantity
from .._utils import return_values
from ..data import fertilizer_impact as fi
from ..typing import NumericArrayLike, NumericOrArray
from ..units import check_dim, ureg
__all__ = [
"ammonium_nitrate_impacts",
"superphosphate_impacts",
]
[docs]
@check_dim(arglist=('amount', 0, '[mass]*[nitrogen]'), result='[mass]*[X]')
def ammonium_nitrate_impacts(
amount: Quantity,
impact_type: str = "GWP",
ci: int = 0,
q: Optional[NumericArrayLike] = None,
) -> Quantity:
'''
Impact associated to ammonium nitrate manufacturing and usage.
Parameters
----------
amount : float or list-like object [mass]*[nitrogen]
Mass of euivalent nitrogen applied.
impact_type : str (optional, default: "GWP")
Impact associated, among "GWP" (global warming potential) for climate
impact, "EP" for eutrophication, and "AP" for acidification potential.
ci : int (optional, default: 0)
Confidence interval (CI). If non-zero, the function will also return
the low and high expected values corresponding to that CI, otherwise it
will only return the average value.
Returns
-------
impact : float or array of size 3 or (len(pop), 3) [mass]*[X]
Expected impact if `ci` is 0, otherwise one (low, mean, high) value for
each entry in `pop`, giving the expected impact:
* if `impact_type` is "GWP", then [X] is [carbon dioxide_equivalent] so
the result will be in kgCO2e,
* if `impact_type` is "EP", then [X] is [phosphate_equivalent] so
the result will be in kgPO4e,
* if `impact_type` is "AP", then [X] is [sulfur_dioxide_equivalent] so
the result will be in kgSO2e.
'''
df = fi[(fi.type == impact_type) & (fi.compound == "AN")]
unit = ureg(df.iloc[0].unit)
return return_values(df, "impact", amount, ci, q) * unit
[docs]
@check_dim(arglist=('amount', 0, '[mass]*[phosphorus]'), result='[mass]*[X]')
def superphosphate_impacts(
amount: Quantity,
sp_type: str = "SSP",
impact_type: str = "GWP",
ci: int = 0,
q: Optional[NumericArrayLike] = None,
) -> Quantity:
'''
Impact associated to Super Phosphate manufacturing and usage, either
single (SSP) or triple (TSP).
Parameters
----------
amount : float or list-like object [mass]*[phosphorus]
Mass of euivalent phosphorus applied.
sp_type : str, optional (default: "SSP")
Type of superphosphate, either single ("SSP") or triple ("TSP").
impact_type : str (optional, default: "GWP")
Impact associated, among "GWP" (global warming potential) for climate
impact, "EP" for eutrophication, and "AP" for acidification potential.
ci : int (optional, default: 0)
Confidence interval (CI). If non-zero, the function will also return
the low and high expected values corresponding to that CI, otherwise it
will only return the average value.
Returns
-------
impact : float or array of size 3 or (len(pop), 3) [mass]*[X]
Expected impact if `ci` is 0, otherwise (low, mean, high) values for
the expected impact:
* if `impact_type` is "GWP", then [X] is [carbon dioxide_equivalent] so
the result will be in kgCO2e,
* if `impact_type` is "EP", then [X] is [phosphate_equivalent] so
the result will be in kgPO4e,
* if `impact_type` is "AP", then [X] is [sulfur_dioxide_equivalent] so
the result will be in kgSO2e.
'''
df = fi[(fi.type == impact_type) & (fi.compound == sp_type)]
unit = ureg(df.iloc[0].unit)
return return_values(df, "impact", amount, ci, q) * unit