Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

co2 emission PR is edited #748

Merged
merged 21 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
29af41c
co2 emission PR is edited
Emre-Yorat89 May 31, 2023
2fe96ae
accidentally deleted 2 line reinserted in co2emission PR
Emre-Yorat89 May 31, 2023
641dca4
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 31, 2023
01d8c00
Merge branch 'co2-emission_v2' of https://github.com/Emre-Yorat89/pyp…
Emre-Yorat89 May 31, 2023
df63f3b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] May 31, 2023
3cc84d6
co2-emission_v2 PR is updated
Emre-Yorat89 Jun 5, 2023
aef0e4b
Merge branch 'co2-emission_v2' of https://github.com/Emre-Yorat89/pyp…
Emre-Yorat89 Jun 5, 2023
4bedc1a
co2-emission_v2 PR is modified.
Emre-Yorat89 Jun 6, 2023
edbc23f
co2-emission_v2 PR is updated.
Emre-Yorat89 Jun 19, 2023
cffd79c
Merge branch 'pypsa-meets-earth:main' into co2-emission_v2
Emre-Yorat89 Aug 14, 2023
e6866c1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 14, 2023
3d174ee
co2 PR revised
Emre-Yorat89 Aug 14, 2023
8b496fd
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 14, 2023
2cec603
Merge branch 'pypsa-meets-earth:main' into co2-emission_v2
Emre-Yorat89 Aug 18, 2023
e408f9a
Merge branch 'pypsa-meets-earth:main' into co2-emission_v2
Emre-Yorat89 Aug 24, 2023
c763637
co2-emission PR is updated
Emre-Yorat89 Sep 1, 2023
a369cdf
Merge branch 'main' into co2-emission_v2
Emre-Yorat89 Sep 26, 2023
16b6ed7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 26, 2023
7b6b106
Merge branch 'main' into co2-emission_v2
davide-f Sep 26, 2023
f2a62e4
Add automatic emissions params to prepare_network
davide-f Sep 26, 2023
f5994d7
bugfix single country
davide-f Sep 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ electricity:
co2base: 1.487e+9 # European default, adjustment to Africa necessary
agg_p_nom_limits: data/agg_p_nom_minmax.csv
hvdc_as_lines: false # should HVDC lines be modeled as `Line` or as `Link` component?
automatic_emission: false
automatic_emission_base_year: 1990 # 1990 is taken as default. Any year from 1970 to 2018 can be selected.

operational_reserve: # like https://genxproject.github.io/GenX/dev/core/#Reserves
activate: false
Expand Down
2 changes: 2 additions & 0 deletions config.tutorial.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ electricity:
co2base: 1.487e+9
agg_p_nom_limits: data/agg_p_nom_minmax.csv
hvdc_as_lines: false # should HVDC lines be modeled as `Line` or as `Link` component?
automatic_emission: true
automatic_emission_base_year: 1990 # 1990 is taken as default. Any year from 1970 to 2018 can be selected.

operational_reserve: # like https://genxproject.github.io/GenX/dev/core/#Reserves
activate: false
Expand Down
2 changes: 2 additions & 0 deletions doc/configtables/electricity.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ base_voltage, kV, float, "Base voltage to which all lines are simplified/aggrega
voltages, kV, "Any subset of {220., 300., 380.}", "Voltage levels considered."
co2limit,:math:`t_{CO_2-eq}/a`, float, "Cap on system total annual carbon dioxide equivalent emissions."
co2base,:math:`t_{CO_2-eq}/a`, float, "Reference value of system total annual carbon dioxide equivalent emissions. Used only if relative emission reduction target is specified in ``{opts}`` wildcard."
automatic_emission, bool, "{True, False}", "True: Emissions are obtained from automatic emission extraction procedure. False: Emissions are obtained manually"
automatic_emission_base_year,, integer, "CO2 emissions of year 1990 from EDGAR category 1A1a (Public electricity and heat production)."
agg_p_nom_limits, file, path, "Reference to ``.csv`` file specifying per carrier generator nominal capacity constraints for individual countries if ``'CCL'`` is in ``{opts}`` wildcard. Default is ``data/agg_p_nom_minmax.csv``."
hvdc_as_lines, bool, "{True, False}", "True: HVDC cables are modelled as PyPSA Line components. False: HVDC cables are modeled as PyPSA Link components."
operational_reserve,,, "The total operational reserve requirements consist of three components: epsilon_load, epsilon_vres, contingency. See `GenX <https://genxproject.github.io/GenX/dev/core/#Reserves>`_ for more details."
Expand Down
2 changes: 2 additions & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ PyPSA-Earth 0.2.2

* Remove RES addition functionality from attach_conventional_generators `PR #769 <https://github.com/pypsa-meets-earth/pypsa-earth/pull/769>`__. Currently wind and solar powerplants stored in powerplants.csv are added to the network by attach_conventional_generators.

* Add functionalities to download and extract emission of countries. `PR #748 https://github.com/pypsa-meets-earth/pypsa-earth/pull/748`

PyPSA-Earth 0.2.1
=================

Expand Down
90 changes: 87 additions & 3 deletions scripts/prepare_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,13 @@
import logging
import os
import re
from zipfile import ZipFile
ekatef marked this conversation as resolved.
Show resolved Hide resolved

import country_converter as cc
import numpy as np
import pandas as pd
import pypsa
import requests
from _helpers import configure_logging
from add_electricity import load_costs, update_transmission_costs

Expand All @@ -71,6 +74,76 @@
logger = logging.getLogger(__name__)


def download_emission_data():
davide-f marked this conversation as resolved.
Show resolved Hide resolved
"""
Download emission file from EDGAR.

Returns
-------
global emission file for all countries in the world.
"""

try:
url = "https://jeodpp.jrc.ec.europa.eu/ftp/jrc-opendata/EDGAR/datasets/v60_GHG/CO2_excl_short-cycle_org_C/v60_GHG_CO2_excl_short-cycle_org_C_1970_2018.zip"
with requests.get(url) as rq:
with open("data/co2.zip", "wb") as file:
file.write(rq.content)
rootpath = os.getcwd()
file_path = os.path.join(rootpath, "data/co2.zip")
with ZipFile(file_path, "r") as zipObj:
zipObj.extract(
"v60_CO2_excl_short-cycle_org_C_1970_2018.xls", rootpath + "/data"
)
os.remove(file_path)
return "v60_CO2_excl_short-cycle_org_C_1970_2018.xls"
except:
logger.error(f"Failed download resource from '{url}'.")
return False


def emission_extractor(filename, emission_year, country_names):
davide-f marked this conversation as resolved.
Show resolved Hide resolved
"""
Extracts CO2 emission values for given country codes from the global
emission file.

Parameters
----------
filename : str
Global emission filename
emission_year : int
Year of CO2 emissions
country_names : numpy.ndarray
Two letter country codes of analysed countries.

Returns
-------
CO2 emission values of studied countries.
"""

# data reading process
datapath = os.path.join(os.getcwd(), "data", filename)
df = pd.read_excel(datapath, sheet_name="v6.0_EM_CO2_fossil_IPCC1996", skiprows=8)
df.columns = df.iloc[0]
df = df.set_index("Country_code_A3")
df = df.loc[
df["IPCC_for_std_report_desc"] == "Public electricity and heat production"
]
df = df.loc[:, "Y_1970":"Y_2018"].ffill(axis=1)
df = df.loc[:, "Y_1970":"Y_2018"].bfill(axis=1)
cc_iso3 = cc.convert(names=country_names, to="ISO3")
if len(country_names) == 1:
cc_iso3 = [cc_iso3]
emission_by_country = df.loc[
df.index.intersection(cc_iso3), "Y_" + str(emission_year)
]
missing_ccs = np.setdiff1d(cc_iso3, df.index.intersection(cc_iso3))
if missing_ccs.size:
logger.warning(
f"The emission value for the following countries has not been found: {missing_ccs}"
)
return emission_by_country


def add_co2limit(n, annual_emissions, Nyears=1.0):
n.add(
"GlobalConstraint",
Expand Down Expand Up @@ -281,14 +354,25 @@ def set_line_nom_max(n, s_nom_max_set=np.inf, p_nom_max_set=np.inf):
for o in opts:
if "Co2L" in o:
m = re.findall("[0-9]*\.?[0-9]+$", o)
if len(m) > 0:
if snakemake.params.electricity["automatic_emission"]:
country_names = n.buses.country.unique()
emission_year = snakemake.params.electricity[
"automatic_emission_base_year"
]
filename = download_emission_data()
co2limit = emission_extractor(
filename, emission_year, country_names
).sum()
if len(m) > 0:
co2limit = co2limit * float(m[0])
logger.info("Setting CO2 limit according to emission base year.")
elif len(m) > 0:
co2limit = float(m[0]) * snakemake.params.electricity["co2base"]
add_co2limit(n, co2limit, Nyears)
logger.info("Setting CO2 limit according to wildcard value.")
else:
co2limit = snakemake.params.electricity["co2limit"]
add_co2limit(n, co2limit, Nyears)
logger.info("Setting CO2 limit according to config value.")
add_co2limit(n, co2limit, Nyears)
break

for o in opts:
Expand Down
Loading