-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ebf4dbb
commit 02d882e
Showing
6 changed files
with
155 additions
and
101 deletions.
There are no files selected for viewing
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
Empty file.
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,81 @@ | ||
"""Function colleciton to calculate energy type co2 emissions""" | ||
|
||
from ctypes import Union | ||
from co2calculator.constants import KWH_TO_TJ, Unit | ||
from co2calculator.data_handlers import ConversionFactors, EmissionFactors | ||
from co2calculator.parameters import ( | ||
ElectricityEmissionParameters, | ||
ElectricityParameters, | ||
HeatingEmissionParameters, | ||
HeatingParameters, | ||
) | ||
from co2calculator._types import Kilogram | ||
|
||
emission_factors = EmissionFactors() | ||
conversion_factors = ConversionFactors() | ||
|
||
|
||
def calc_co2_heating( | ||
consumption: float, | ||
options: Union[HeatingParameters, dict] = None, | ||
) -> Kilogram: | ||
"""Function to compute heating emissions | ||
:param consumption: energy consumption | ||
:param options: parameters for heating emissions calculation | ||
:type consumption: float | ||
:type options: HeatingParameters | dict | ||
:return: total emissions of heating energy consumption | ||
:rtype: Kilogram | ||
""" | ||
# Validate parameters | ||
if options is None: | ||
options = {} | ||
params = HeatingParameters(**options) | ||
emission_params = HeatingEmissionParameters(**params.heating_emission_parameters) | ||
|
||
# Get the co2 factor | ||
co2e = emission_factors.get(params.dict()) | ||
|
||
if params.unit is not Unit.KWH: | ||
# Get the conversion factor | ||
conversion_factor = ConversionFactors.get( | ||
fuel_type=emission_params.fuel_type, unit=params.unit | ||
) | ||
|
||
consumption_kwh = consumption * conversion_factor | ||
else: | ||
consumption_kwh = consumption | ||
|
||
# co2 equivalents for heating and electricity refer to a consumption of 1 TJ | ||
# so consumption needs to be converted to TJ | ||
return consumption_kwh * params.area_share / KWH_TO_TJ * co2e | ||
|
||
|
||
def calc_co2_electricity( | ||
consumption: float, options: Union[ElectricityParameters, dict] = None | ||
) -> Kilogram: | ||
"""Function to compute electricity emissions | ||
:param consumption: energy consumption | ||
:param fuel_type: energy (mix) used for electricity [german_energy_mix, solar] | ||
:param energy_share: the research group's approximate share of the total electricity energy consumption | ||
:type consumption: float | ||
:type fuel_type: str | ||
:type energy_share: float | ||
:return: total emissions of electricity energy consumption | ||
:rtype: Kilogram | ||
""" | ||
|
||
# Validate parameters | ||
if options is None: | ||
options = {} | ||
params = ElectricityParameters(**options) | ||
emission_params = ElectricityEmissionParameters(**params) | ||
|
||
# Get the co2 factor | ||
co2e = emission_factors.get(params.dict()) | ||
|
||
# co2 equivalents for heating and electricity refer to a consumption of 1 TJ | ||
# so consumption needs to be converted to TJ | ||
return consumption * emission_params.energy_share / KWH_TO_TJ * co2e |
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,46 @@ | ||
"""tests for energy calculation""" | ||
import co2calculator.energy.calculate_energy as energy | ||
import pytest | ||
|
||
|
||
def test_heating_woodchips(): | ||
"""Test co2e calculation for heating: woodchips""" | ||
# Given parameters | ||
consumption = 250 | ||
co2e_kg_expected = 43.63 | ||
|
||
func_options = { | ||
# Given parameters | ||
"heating_emission_parameters": { | ||
"fuel_type": "woodchips" # emission factor: 9322 kg/TJ | ||
}, | ||
"unit": "kg", # conversion factor to kWh = 5.4 | ||
} | ||
|
||
# Calculate co2e | ||
co2e = energy.calc_co2_heating(consumption=consumption, options=func_options) | ||
|
||
# Check if expected result matches calculated result | ||
assert co2e == pytest.approx(co2e_kg_expected, rel=0.01) | ||
|
||
|
||
def test_electricity(): | ||
"""Test co2e calculation for electricity""" | ||
# Given parameters | ||
fuel_type = "german_energy_mix" | ||
consumption_kwh = 10000 | ||
co2e_kg_expected = 3942.65 # emission factor: 109518 kg/TJ | ||
|
||
func_options = { | ||
"electricity_emission_parameters": { | ||
"fuel_type": "german_energy_mix" # emission factor: 109518 kg/TJ | ||
} | ||
} | ||
|
||
# Calculate co2e | ||
co2e = energy.calc_co2_electricity( | ||
consumption=consumption_kwh, options=func_options | ||
) | ||
|
||
# Check if expected result matches calculated result | ||
assert co2e == pytest.approx(co2e_kg_expected, rel=0.01) |