Skip to content

Commit d7c2344

Browse files
authored
Merge pull request #34 from OSeMOSYS/trade_fix
Fix for trade, missing UK eu-iso2 code
2 parents 0ed1304 + b5b81d4 commit d7c2344

File tree

7 files changed

+218
-127
lines changed

7 files changed

+218
-127
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
# osemosys2iamc
22

3+
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.7473185.svg)](https://doi.org/10.5281/zenodo.7473185)
4+
35
Convert OSeMOSYS results to IAMC format
46

57
## Acknowledgements
68

7-
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)).
9+
This work was financially supported by:
10+
11+
- 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))
12+
- 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
813

914
## Install from Github repository
1015

src/osemosys2iamc/resultify.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
import matplotlib.dates as mdates
2727
import re
2828

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

3035
def iso_to_country(
3136
iso_format: str, index: List[str], osemosys_param: str
@@ -52,7 +57,7 @@ def iso_to_country(
5257
format_regex = r"^iso[23]_([1-9]\d*|start|end)$"
5358

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

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

@@ -75,14 +80,14 @@ def iso_to_country(
7580
"""
7681
Checks every technology/fuel name for a valid code. If found,
7782
adds that country to the list for that tech/fuel name, otherwise
78-
adds an empty string for that tech/fuel. A position exceeeding the
83+
adds an empty string for that tech/fuel. A position exceeding the
7984
length of the name, adds an empty string instead
8085
"""
8186
for i in index:
8287
if re.search(region_regex, i.upper()) != None:
8388
code = re.search(region_regex, i.upper()).groups()[0]
8489
if code in country_dict:
85-
countries_list.append(country_dict[code].name.upper())
90+
countries_list.append(country_dict[code].name)
8691
else:
8792
countries_list.append("")
8893
no_country_extracted.append(i)
@@ -431,7 +436,7 @@ def main(config: Dict, inputs_path: str, results_path: str) -> pyam.IamDataFrame
431436
try:
432437
for result in config["results"]:
433438

434-
if type(result["osemosys_param"]) == str:
439+
if isinstance(result["osemosys_param"], str):
435440
results = read_file(
436441
results_path, result["osemosys_param"], config["region"]
437442
)
@@ -470,15 +475,25 @@ def main(config: Dict, inputs_path: str, results_path: str) -> pyam.IamDataFrame
470475
else:
471476
data = extract_results(results, technologies)
472477

473-
else:
478+
elif isinstance(result["osemosys_param"], list):
474479
results = {}
480+
unit = result["unit"]
475481
for p in result["osemosys_param"]:
476-
path_name = os.path.join(results_path, p + ".csv")
477-
results[p] = read_file(path_name)
482+
results[p] = read_file(results_path, p, config["region"])
483+
478484
if "trade_tech" in result.keys():
479485
technologies = result["trade_tech"]
480486
data = calculate_trade(results, technologies)
481487

488+
else:
489+
name = result["iamc_variable"]
490+
raise ValueError(f"No data found for {name}")
491+
492+
else:
493+
name = result["iamc_variable"]
494+
msg = f"Error in configuration file for entry {name}. The `osemosys_param` key must be a string or a list"
495+
raise ValueError(msg)
496+
482497
if "transform" in result.keys():
483498
if result["transform"] == "abs":
484499
data["VALUE"] = data["VALUE"].abs()
@@ -501,15 +516,18 @@ def main(config: Dict, inputs_path: str, results_path: str) -> pyam.IamDataFrame
501516
except KeyError:
502517
pass
503518

504-
all_data = pyam.concat(blob)
519+
if len(blob) > 0:
520+
all_data = pyam.concat(blob)
505521

506-
all_data = all_data.convert_unit("PJ/yr", to="EJ/yr")
507-
all_data = all_data.convert_unit("ktCO2/yr", to="Mt CO2/yr", factor=0.001)
508-
all_data = all_data.convert_unit("MEUR_2015/PJ", to="EUR_2020/GJ", factor=1.05)
509-
all_data = all_data.convert_unit("kt CO2/yr", to="Mt CO2/yr")
522+
all_data = all_data.convert_unit("PJ/yr", to="EJ/yr")
523+
all_data = all_data.convert_unit("ktCO2/yr", to="Mt CO2/yr", factor=0.001)
524+
all_data = all_data.convert_unit("MEUR_2015/PJ", to="EUR_2020/GJ", factor=1.05)
525+
all_data = all_data.convert_unit("kt CO2/yr", to="Mt CO2/yr")
510526

511-
all_data = pyam.IamDataFrame(all_data)
512-
return all_data
527+
all_data = pyam.IamDataFrame(all_data)
528+
return all_data
529+
else:
530+
raise ValueError("No data found")
513531

514532

515533
def aggregate(func):
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
REGION,TECHNOLOGY,FUEL,YEAR,VALUE
2+
REGION1,ATEL1234,ATEL,2010,26.324108350683794
3+
REGION1,ATEL1234,ATEL,2011,26.324108350683794
4+
REGION1,ATEL1234,ATEL,2012,26.324108350683794
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
REGION,TIMESLICE,TECHNOLOGY,FUEL,YEAR,VALUE
2+
REGION1,ID,ATEL1234,ATEL,2010,1.5
3+
REGION1,ID,ATEL1234,ATEL,2011,1.4
4+
REGION1,ID,ATEL1234,ATEL,2012,1.3
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
model: OSeMBE v1.0.0
2+
scenario: DIAG-C400-lin-ResidualFossil
3+
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]
4+
results:
5+
- iamc_variable: Trade|Secondary Energy|Electricity|Volume
6+
osemosys_param:
7+
- UseByTechnology
8+
- ProductionByTechnologyAnnual
9+
trade_tech:
10+
- (?=^.{2}(EL))^((?!00).)*$
11+
unit: PJ/yr

tests/test_main.py

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ def test_main_input():
1919

2020
data = pd.DataFrame(
2121
[
22-
["AUSTRIA", "Price|Primary Energy|Biomass", 2015, 3.15],
23-
["AUSTRIA", "Price|Primary Energy|Biomass", 2016, 4.2],
24-
["BELGIUM", "Price|Primary Energy|Biomass", 2015, 1.785],
25-
["BELGIUM", "Price|Primary Energy|Biomass", 2016, 1.890],
22+
["Austria", "Price|Primary Energy|Biomass", 2015, 3.15],
23+
["Austria", "Price|Primary Energy|Biomass", 2016, 4.2],
24+
["Belgium", "Price|Primary Energy|Biomass", 2015, 1.785],
25+
["Belgium", "Price|Primary Energy|Biomass", 2016, 1.890],
2626
],
2727
columns=["region", "variable", "year", "value"],
2828
)
@@ -50,18 +50,18 @@ def test_main_result():
5050

5151
data = pd.DataFrame(
5252
[
53-
["AUSTRIA", "Capacity|Electricity", 2015, 0.446776],
54-
["BELGIUM", "Capacity|Electricity", 2016, 0.184866],
55-
["BULGARIA", "Capacity|Electricity", 2015, 4.141],
56-
["CYPRUS", "Capacity|Electricity", 2015, 0.3904880555817921],
57-
["CZECHIA", "Capacity|Electricity", 2015, 0.299709],
58-
["DENMARK", "Capacity|Electricity", 2015, 0.0005],
59-
["ESTONIA", "Capacity|Electricity", 2015, 0.006],
60-
["FINLAND", "Capacity|Electricity", 2015, 0.0263],
61-
["FRANCE", "Capacity|Electricity", 2015, 0.47835],
62-
["GERMANY", "Capacity|Electricity", 2015, 9.62143],
63-
["SPAIN", "Capacity|Electricity", 2015, 7.7308],
64-
["SWITZERLAND", "Capacity|Electricity", 2026, 0.004563975391582646],
53+
["Austria", "Capacity|Electricity", 2015, 0.446776],
54+
["Belgium", "Capacity|Electricity", 2016, 0.184866],
55+
["Bulgaria", "Capacity|Electricity", 2015, 4.141],
56+
["Cyprus", "Capacity|Electricity", 2015, 0.3904880555817921],
57+
["Czechia", "Capacity|Electricity", 2015, 0.299709],
58+
["Denmark", "Capacity|Electricity", 2015, 0.0005],
59+
["Estonia", "Capacity|Electricity", 2015, 0.006],
60+
["Finland", "Capacity|Electricity", 2015, 0.0263],
61+
["France", "Capacity|Electricity", 2015, 0.47835],
62+
["Germany", "Capacity|Electricity", 2015, 9.62143],
63+
["Spain", "Capacity|Electricity", 2015, 7.7308],
64+
["Switzerland", "Capacity|Electricity", 2026, 0.004563975391582646],
6565
],
6666
columns=["region", "variable", "year", "value"],
6767
)
@@ -92,10 +92,10 @@ def test_main_result_capture():
9292

9393
data = pd.DataFrame(
9494
[
95-
["AUSTRIA", "Carbon Capture|Biomass", 2026, 7.573069442598169],
96-
["AUSTRIA", "Carbon Capture|Biomass", 2027, 7.766777427515737],
97-
["BELGIUM", "Carbon Capture|Biomass", 2026, 2.24498280006968],
98-
["BELGIUM", "Carbon Capture|Biomass", 2027, 6.746886436926597],
95+
["Austria", "Carbon Capture|Biomass", 2026, 7.573069442598169],
96+
["Austria", "Carbon Capture|Biomass", 2027, 7.766777427515737],
97+
["Belgium", "Carbon Capture|Biomass", 2026, 2.24498280006968],
98+
["Belgium", "Carbon Capture|Biomass", 2027, 6.746886436926597],
9999
],
100100
columns=["region", "variable", "year", "value"],
101101
)
@@ -108,3 +108,46 @@ def test_main_result_capture():
108108
)
109109

110110
assert_iamframe_equal(actual, expected)
111+
112+
113+
def test_main_trade():
114+
"""Test operation of trade filter
115+
116+
Config
117+
------
118+
- iamc_variable: Trade|Secondary Energy|Electricity|Volume
119+
osemosys_param:
120+
- UseByTechnology
121+
- ProductionByTechnologyAnnual
122+
trade_tech:
123+
- (?=^.{2}(EL))^((?!00).)*$
124+
unit: PJ/yr
125+
126+
"""
127+
128+
config_path = os.path.join("tests", "fixtures", "trade", "config_trade.yaml")
129+
inputs_path = os.path.join("tests", "fixtures", "trade")
130+
results_path = os.path.join("tests", "fixtures", "trade")
131+
132+
with open(config_path, "r") as config_file:
133+
config = load(config_file, Loader=SafeLoader)
134+
135+
actual = main(config, inputs_path, results_path)
136+
137+
data = pd.DataFrame(
138+
[
139+
["Austria", "Trade|Secondary Energy|Electricity|Volume", 2010, -0.024824],
140+
["Austria", "Trade|Secondary Energy|Electricity|Volume", 2011, -0.024924],
141+
["Austria", "Trade|Secondary Energy|Electricity|Volume", 2012, -0.025024],
142+
],
143+
columns=["region", "variable", "year", "value"],
144+
)
145+
146+
expected = IamDataFrame(
147+
data,
148+
model="OSeMBE v1.0.0",
149+
scenario="DIAG-C400-lin-ResidualFossil",
150+
unit="EJ/yr",
151+
)
152+
153+
assert_iamframe_equal(actual, expected)

0 commit comments

Comments
 (0)