Skip to content

Commit

Permalink
Merge pull request #34 from OSeMOSYS/trade_fix
Browse files Browse the repository at this point in the history
Fix for trade, missing UK eu-iso2 code
  • Loading branch information
willu47 authored Jul 12, 2023
2 parents 0ed1304 + b5b81d4 commit d7c2344
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 127 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
# osemosys2iamc

[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.7473185.svg)](https://doi.org/10.5281/zenodo.7473185)

Convert OSeMOSYS results to IAMC format

## Acknowledgements

This work was financially supported by the European Union’s Horizon 2020 research and innovation programme under the grant agreement No 101022622 ([European Climate and Energy Modelling Forum ECEMF](https://doi.org/10.3030/101022622)).
This work was financially supported by:

- The European Union’s Horizon 2020 research and innovation programme under the grant agreement No 101022622 ([European Climate and Energy Modelling Forum ECEMF](https://doi.org/10.3030/101022622))
- The [IAM COMPACT](https://doi.org/10.3030/101056306) project has received funding from the European Union’s HORIZON EUROPE Research and Innovation Programme under grant agreement No 101056306

## Install from Github repository

Expand Down
46 changes: 32 additions & 14 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 All @@ -52,7 +57,7 @@ def iso_to_country(
format_regex = r"^iso[23]_([1-9]\d*|start|end)$"

# Verifies that given format is the expected format; Raises an error if expectation not met
if re.search(format_regex, iso_format) != None:
if re.search(format_regex, iso_format) is not None:

iso_type, abbr_loc = iso_format[3:].split("_")

Expand All @@ -75,14 +80,14 @@ def iso_to_country(
"""
Checks every technology/fuel name for a valid code. If found,
adds that country to the list for that tech/fuel name, otherwise
adds an empty string for that tech/fuel. A position exceeeding the
adds an empty string for that tech/fuel. A position exceeding the
length of the name, adds an empty string instead
"""
for i in index:
if re.search(region_regex, i.upper()) != None:
code = re.search(region_regex, i.upper()).groups()[0]
if code in country_dict:
countries_list.append(country_dict[code].name.upper())
countries_list.append(country_dict[code].name)
else:
countries_list.append("")
no_country_extracted.append(i)
Expand Down Expand Up @@ -431,7 +436,7 @@ def main(config: Dict, inputs_path: str, results_path: str) -> pyam.IamDataFrame
try:
for result in config["results"]:

if type(result["osemosys_param"]) == str:
if isinstance(result["osemosys_param"], str):
results = read_file(
results_path, result["osemosys_param"], config["region"]
)
Expand Down Expand Up @@ -470,15 +475,25 @@ def main(config: Dict, inputs_path: str, results_path: str) -> pyam.IamDataFrame
else:
data = extract_results(results, technologies)

else:
elif isinstance(result["osemosys_param"], list):
results = {}
unit = result["unit"]
for p in result["osemosys_param"]:
path_name = os.path.join(results_path, p + ".csv")
results[p] = read_file(path_name)
results[p] = read_file(results_path, p, config["region"])

if "trade_tech" in result.keys():
technologies = result["trade_tech"]
data = calculate_trade(results, technologies)

else:
name = result["iamc_variable"]
raise ValueError(f"No data found for {name}")

else:
name = result["iamc_variable"]
msg = f"Error in configuration file for entry {name}. The `osemosys_param` key must be a string or a list"
raise ValueError(msg)

if "transform" in result.keys():
if result["transform"] == "abs":
data["VALUE"] = data["VALUE"].abs()
Expand All @@ -501,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
4 changes: 4 additions & 0 deletions tests/fixtures/trade/ProductionByTechnologyAnnual.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
REGION,TECHNOLOGY,FUEL,YEAR,VALUE
REGION1,ATEL1234,ATEL,2010,26.324108350683794
REGION1,ATEL1234,ATEL,2011,26.324108350683794
REGION1,ATEL1234,ATEL,2012,26.324108350683794
4 changes: 4 additions & 0 deletions tests/fixtures/trade/UseByTechnology.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
REGION,TIMESLICE,TECHNOLOGY,FUEL,YEAR,VALUE
REGION1,ID,ATEL1234,ATEL,2010,1.5
REGION1,ID,ATEL1234,ATEL,2011,1.4
REGION1,ID,ATEL1234,ATEL,2012,1.3
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
83 changes: 63 additions & 20 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def test_main_input():

data = pd.DataFrame(
[
["AUSTRIA", "Price|Primary Energy|Biomass", 2015, 3.15],
["AUSTRIA", "Price|Primary Energy|Biomass", 2016, 4.2],
["BELGIUM", "Price|Primary Energy|Biomass", 2015, 1.785],
["BELGIUM", "Price|Primary Energy|Biomass", 2016, 1.890],
["Austria", "Price|Primary Energy|Biomass", 2015, 3.15],
["Austria", "Price|Primary Energy|Biomass", 2016, 4.2],
["Belgium", "Price|Primary Energy|Biomass", 2015, 1.785],
["Belgium", "Price|Primary Energy|Biomass", 2016, 1.890],
],
columns=["region", "variable", "year", "value"],
)
Expand Down Expand Up @@ -50,18 +50,18 @@ def test_main_result():

data = pd.DataFrame(
[
["AUSTRIA", "Capacity|Electricity", 2015, 0.446776],
["BELGIUM", "Capacity|Electricity", 2016, 0.184866],
["BULGARIA", "Capacity|Electricity", 2015, 4.141],
["CYPRUS", "Capacity|Electricity", 2015, 0.3904880555817921],
["CZECHIA", "Capacity|Electricity", 2015, 0.299709],
["DENMARK", "Capacity|Electricity", 2015, 0.0005],
["ESTONIA", "Capacity|Electricity", 2015, 0.006],
["FINLAND", "Capacity|Electricity", 2015, 0.0263],
["FRANCE", "Capacity|Electricity", 2015, 0.47835],
["GERMANY", "Capacity|Electricity", 2015, 9.62143],
["SPAIN", "Capacity|Electricity", 2015, 7.7308],
["SWITZERLAND", "Capacity|Electricity", 2026, 0.004563975391582646],
["Austria", "Capacity|Electricity", 2015, 0.446776],
["Belgium", "Capacity|Electricity", 2016, 0.184866],
["Bulgaria", "Capacity|Electricity", 2015, 4.141],
["Cyprus", "Capacity|Electricity", 2015, 0.3904880555817921],
["Czechia", "Capacity|Electricity", 2015, 0.299709],
["Denmark", "Capacity|Electricity", 2015, 0.0005],
["Estonia", "Capacity|Electricity", 2015, 0.006],
["Finland", "Capacity|Electricity", 2015, 0.0263],
["France", "Capacity|Electricity", 2015, 0.47835],
["Germany", "Capacity|Electricity", 2015, 9.62143],
["Spain", "Capacity|Electricity", 2015, 7.7308],
["Switzerland", "Capacity|Electricity", 2026, 0.004563975391582646],
],
columns=["region", "variable", "year", "value"],
)
Expand Down Expand Up @@ -92,10 +92,10 @@ def test_main_result_capture():

data = pd.DataFrame(
[
["AUSTRIA", "Carbon Capture|Biomass", 2026, 7.573069442598169],
["AUSTRIA", "Carbon Capture|Biomass", 2027, 7.766777427515737],
["BELGIUM", "Carbon Capture|Biomass", 2026, 2.24498280006968],
["BELGIUM", "Carbon Capture|Biomass", 2027, 6.746886436926597],
["Austria", "Carbon Capture|Biomass", 2026, 7.573069442598169],
["Austria", "Carbon Capture|Biomass", 2027, 7.766777427515737],
["Belgium", "Carbon Capture|Biomass", 2026, 2.24498280006968],
["Belgium", "Carbon Capture|Biomass", 2027, 6.746886436926597],
],
columns=["region", "variable", "year", "value"],
)
Expand All @@ -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)
Loading

0 comments on commit d7c2344

Please sign in to comment.