Skip to content

Commit

Permalink
refactor and adjust tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianKneissl committed Jun 5, 2024
1 parent 2228aff commit b6ccc87
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 24 deletions.
2 changes: 0 additions & 2 deletions co2calculator/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import iso3166
from co2calculator.data_handlers import Airports

KWH_TO_TJ = 277777.77777778


class HeatingFuel(enum.Enum):
"""Enum for heating fuel types"""
Expand Down
3 changes: 2 additions & 1 deletion co2calculator/data_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ def get(self, fuel_type, unit):
:return:
:rtype:
"""
print(f'fuel_type == "{fuel_type.value}" & unit == "{unit}"')
selected_factors = self.conversion_factors.query(
f'fuel_type=="{fuel_type}" & unit=="{unit}"'
f'fuel_type == "{fuel_type.value}" & unit == "{unit}"'
)
if selected_factors.empty:
raise ConversionFactorNotFound(
Expand Down
19 changes: 11 additions & 8 deletions co2calculator/energy/calculate_energy.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Function colleciton to calculate energy type co2 emissions"""

from typing import Union
from co2calculator.constants import KWH_TO_TJ, Unit
from co2calculator.constants import Unit
from co2calculator.data_handlers import ConversionFactors, EmissionFactors
from co2calculator.parameters import (
ElectricityEmissionParameters,
Expand Down Expand Up @@ -31,14 +31,17 @@ def calc_co2_heating(
if options is None:
options = {}
params = HeatingParameters(**options)
emission_params = HeatingEmissionParameters(**params.heating_emission_parameters)
emission_params = HeatingEmissionParameters(
**params.heating_emission_parameters.dict()
)

# Get the co2 factor
co2e = emission_factors.get(params.dict())
co2e = emission_factors.get(emission_params.dict())

if params.unit is not Unit.KWH:
print(emission_params.fuel_type, params.unit)
# Get the conversion factor
conversion_factor = ConversionFactors.get(
conversion_factor = conversion_factors.get(
fuel_type=emission_params.fuel_type, unit=params.unit
)

Expand All @@ -48,7 +51,7 @@ def calc_co2_heating(

# 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
return consumption_kwh * params.area_share * co2e


def calc_co2_electricity(
Expand All @@ -71,12 +74,12 @@ def calc_co2_electricity(
options = {}
params = ElectricityParameters(**options)
emission_params = ElectricityEmissionParameters(
**params.electricity_emission_parameters
**params.electricity_emission_parameters.dict()
)

# Get the co2 factor
co2e = emission_factors.get(params.dict())
co2e = emission_factors.get(emission_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
return consumption * params.energy_share / co2e
8 changes: 5 additions & 3 deletions co2calculator/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ class ElectricityEmissionParameters(BaseModel):

category: EmissionCategory = EmissionCategory.ELECTRICITY
fuel_type: Union[ElectricityFuel, str] = ElectricityFuel.PRODUCTION_FUEL_MIX
country_code: CountryCode2 # TODO: Shall we set a default? Or add a watning if not provided?
country_code: Union[
CountryCode2, str
] = "DE" # TODO: Shall we set a default? Or add a warning if not provided?

@validator("fuel_type", allow_reuse=True)
def check_fueltype(cls, v):
Expand All @@ -170,7 +172,7 @@ def check_fueltype(cls, v):

class ElectricityParameters(BaseModel):
electricity_emission_parameters: ElectricityEmissionParameters
energy_share: float
energy_share: float = 1.0

@validator("energy_share", allow_reuse=True)
def check_energy_share(cls, v):
Expand All @@ -182,7 +184,7 @@ class HeatingEmissionParameters(BaseModel):

category: EmissionCategory = EmissionCategory.HEATING
fuel_type: Union[HeatingFuel, str] = HeatingFuel.GAS
country_code: str = "global"
country_code: Union[CountryCode2, str] = "global"

@validator("fuel_type", allow_reuse=True)
def check_fueltype(cls, v):
Expand Down
2 changes: 1 addition & 1 deletion data/conversion_factors_heating.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
1,liquid_gas,kg,14.1
2,coal,kg,6.0
3,pellet,kg,5.4
4,woodchips,kg,5.2
4,wood chips,kg,5.2
5,gas,m^3,10.8
2 changes: 1 addition & 1 deletion data/emission_factors_heating.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
2,global,heating,"UK, Department for Business, Energy & Industrial Strategy",LPG,kWh,liquid_gas,kg/kWh,0.21
3,global,heating,"UK, Department for Business, Energy & Industrial Strategy",fuel oil,kWh,oil,kg/kWh,0.27
4,global,heating,"UK, Department for Business, Energy & Industrial Strategy",wood pellets,kWh,pellet,kg/kWh,0.01074
5,global,heating,"UK, Department for Business, Energy & Industrial Strategy",wood chips,kWh,woodchips,kg/kWh,0.01074
5,global,heating,"UK, Department for Business, Energy & Industrial Strategy",wood chips,kWh,wood chips,kg/kWh,0.01074
2 changes: 1 addition & 1 deletion data/test_data_users/heating.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group_id;building;timestamp;consumption;fuel_type;unit;group_share
1;;2018-01-01;250;woodchips;kg;1.0
1;;2018-01-01;250;wood chips;kg;1.0
2;;2018-01-01;29653;gas;kWh;0.5
3;;2018-01-01;47569;oil;kWh;1.0
4;;2018-01-01;35124;district_heating;kWh;0.2
Expand Down
1 change: 1 addition & 0 deletions tests/functional/test_data_code_compliance.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def test_defaults(default_parameters):
"""Test if default parameters are available in the csv files"""

# Get the emission factor for the default parameter combination
print(default_parameters().dict())
co2e = emission_factors.get(default_parameters().dict())
assert isinstance(
co2e, float
Expand Down
12 changes: 5 additions & 7 deletions tests/unit/test_calculate_energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,17 @@


def test_heating_woodchips():
"""Test co2e calculation for heating: woodchips"""
"""Test co2e calculation for heating: wood chips"""
# Given parameters
consumption = 250
co2e_kg_expected = 43.63

co2e_kg_expected = 13.962
func_options = {
# Given parameters
"heating_emission_parameters": {
"fuel_type": "wood chips" # 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)

Expand All @@ -27,13 +25,13 @@ def test_heating_woodchips():
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
co2e_kg_expected = 22265

func_options = {
"electricity_emission_parameters": {
"fuel_type": "german_energy_mix" # emission factor: 109518 kg/TJ
"fuel_type": "production fuel mix",
"country_code": "DE",
}
}

Expand Down

0 comments on commit b6ccc87

Please sign in to comment.