Skip to content

Commit

Permalink
Add test for trade and override for UK and EL iso2 codes
Browse files Browse the repository at this point in the history
  • Loading branch information
willu47 committed Jul 12, 2023
1 parent 9c80d43 commit 34d6b5b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/osemosys2iamc/resultify.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
import matplotlib.dates as mdates
import re

# Creates an alias for United Kingdom and Greece using alternate 2-letter code
# (see issue https://github.com/OSeMOSYS/osemosys2iamc/issues/33)
countries_by_alpha2["UK"] = countries_by_alpha2["GB"]
countries_by_alpha2["EL"] = countries_by_alpha2["GR"]


def iso_to_country(
iso_format: str, index: List[str], osemosys_param: str
Expand Down Expand Up @@ -472,6 +477,7 @@ def main(config: Dict, inputs_path: str, results_path: str) -> pyam.IamDataFrame

elif isinstance(result["osemosys_param"], list):
results = {}
unit = result["unit"]
for p in result["osemosys_param"]:
results[p] = read_file(results_path, p, config["region"])

Expand Down Expand Up @@ -510,15 +516,18 @@ def main(config: Dict, inputs_path: str, results_path: str) -> pyam.IamDataFrame
except KeyError:
pass

all_data = pyam.concat(blob)
if len(blob) > 0:
all_data = pyam.concat(blob)

all_data = all_data.convert_unit("PJ/yr", to="EJ/yr")
all_data = all_data.convert_unit("ktCO2/yr", to="Mt CO2/yr", factor=0.001)
all_data = all_data.convert_unit("MEUR_2015/PJ", to="EUR_2020/GJ", factor=1.05)
all_data = all_data.convert_unit("kt CO2/yr", to="Mt CO2/yr")
all_data = all_data.convert_unit("PJ/yr", to="EJ/yr")
all_data = all_data.convert_unit("ktCO2/yr", to="Mt CO2/yr", factor=0.001)
all_data = all_data.convert_unit("MEUR_2015/PJ", to="EUR_2020/GJ", factor=1.05)
all_data = all_data.convert_unit("kt CO2/yr", to="Mt CO2/yr")

all_data = pyam.IamDataFrame(all_data)
return all_data
all_data = pyam.IamDataFrame(all_data)
return all_data
else:
raise ValueError("No data found")


def aggregate(func):
Expand Down
11 changes: 11 additions & 0 deletions tests/fixtures/trade/config_trade.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
model: OSeMBE v1.0.0
scenario: DIAG-C400-lin-ResidualFossil
region: 'iso2_start' #iso2_x, iso3_x, from_csv, or a name of a country/region [substitute x with start, end, or a positive number]
results:
- iamc_variable: Trade|Secondary Energy|Electricity|Volume
osemosys_param:
- UseByTechnology
- ProductionByTechnologyAnnual
trade_tech:
- (?=^.{2}(EL))^((?!00).)*$
unit: PJ/yr
43 changes: 43 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,46 @@ def test_main_result_capture():
)

assert_iamframe_equal(actual, expected)


def test_main_trade():
"""Test operation of trade filter
Config
------
- iamc_variable: Trade|Secondary Energy|Electricity|Volume
osemosys_param:
- UseByTechnology
- ProductionByTechnologyAnnual
trade_tech:
- (?=^.{2}(EL))^((?!00).)*$
unit: PJ/yr
"""

config_path = os.path.join("tests", "fixtures", "trade", "config_trade.yaml")
inputs_path = os.path.join("tests", "fixtures", "trade")
results_path = os.path.join("tests", "fixtures", "trade")

with open(config_path, "r") as config_file:
config = load(config_file, Loader=SafeLoader)

actual = main(config, inputs_path, results_path)

data = pd.DataFrame(
[
["Austria", "Trade|Secondary Energy|Electricity|Volume", 2010, -0.024824],
["Austria", "Trade|Secondary Energy|Electricity|Volume", 2011, -0.024924],
["Austria", "Trade|Secondary Energy|Electricity|Volume", 2012, -0.025024],
],
columns=["region", "variable", "year", "value"],
)

expected = IamDataFrame(
data,
model="OSeMBE v1.0.0",
scenario="DIAG-C400-lin-ResidualFossil",
unit="EJ/yr",
)

assert_iamframe_equal(actual, expected)
6 changes: 6 additions & 0 deletions tests/test_resultify.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,9 @@ def test_iso_to_country_iso3middle(self):
expected = ["Ghana", "Saint Lucia", "", ""]

assert actual == expected

def test_iso_to_country_uk(self):
techs = ["UKNGA"]
actual = iso_to_country("iso2_start", techs, "TotalCapacityAnnual")
expected = ["United Kingdom of Great Britain and Northern Ireland"]
assert actual == expected

0 comments on commit 34d6b5b

Please sign in to comment.