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

Fix snakemake dependencies in carbon budget runs #228

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ config.yaml
doc/_build

*.xls
data/h2_salt_caverns_GWh_per_sqkm.geojson
data/pypsa-eur-sec-data-bundle.tar.gz
2 changes: 2 additions & 0 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,8 @@ rule build_transport_demand:

rule prepare_sector_network:
input:
co2="data/eea/UNFCCC_v23.csv",
eurostat=input_eurostat,
overrides="data/override_component_attrs",
network=pypsaeur('networks/elec_s{simpl}_{clusters}_ec_lv{lv}_{opts}.nc'),
energy_totals_name='resources/energy_totals.csv',
Expand Down
17 changes: 10 additions & 7 deletions scripts/build_energy_totals.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def reverse(dictionary):
}


def build_eurostat(countries, year):
def build_eurostat(snakemake, countries, year):
"""Return multi-index for all countries' energy data in TWh/a."""

report_year = snakemake.config["energy"]["eurostat_report_year"]
Expand Down Expand Up @@ -563,7 +563,7 @@ def build_energy_totals(countries, eurostat, swiss, idees):
return df


def build_eea_co2(year=1990):
def build_eea_co2(snakemake, year=1990):

# https://www.eea.europa.eu/data-and-maps/data/national-emissions-reported-to-the-unfccc-and-to-the-eu-greenhouse-gas-monitoring-mechanism-16
# downloaded 201228 (modified by EEA last on 201221)
Expand Down Expand Up @@ -611,9 +611,9 @@ def build_eea_co2(year=1990):
return emissions / 1e3


def build_eurostat_co2(countries, year=1990):
def build_eurostat_co2(snakemake, countries, year=1990):

eurostat = build_eurostat(countries, year)
eurostat = build_eurostat(snakemake, countries, year)

specific_emissions = pd.Series(index=eurostat.columns, dtype=float)

Expand All @@ -634,6 +634,9 @@ def build_co2_totals(countries, eea_co2, eurostat_co2):

co2 = eea_co2.reindex(countries)

# Convert dtype of variable countries to pandas.index object to ensure following 'intersection' operation works even with numpy.ndarray as input
countries = pd.Index(countries)

for ct in countries.intersection(["BA", "RS", "AL", "ME", "MK"]):

mappings = {
Expand Down Expand Up @@ -702,16 +705,16 @@ def build_transport_data(countries, population, idees):
idees_countries = countries.intersection(eu28)

data_year = config["energy_totals_year"]
eurostat = build_eurostat(countries, data_year)
eurostat = build_eurostat(snakemake, countries, data_year)
swiss = build_swiss(data_year)
idees = build_idees(idees_countries, data_year)

energy = build_energy_totals(countries, eurostat, swiss, idees)
energy.to_csv(snakemake.output.energy_name)

base_year_emissions = config["base_emissions_year"]
eea_co2 = build_eea_co2(base_year_emissions)
eurostat_co2 = build_eurostat_co2(countries, base_year_emissions)
eea_co2 = build_eea_co2(snakemake, base_year_emissions)
eurostat_co2 = build_eurostat_co2(snakemake, countries, base_year_emissions)

co2 = build_co2_totals(countries, eea_co2, eurostat_co2)
co2.to_csv(snakemake.output.co2_name)
Expand Down
12 changes: 6 additions & 6 deletions scripts/prepare_sector_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,16 +163,16 @@ def co2_emissions_year(countries, opts, year):
Calculate CO2 emissions in one specific year (e.g. 1990 or 2018).
"""

eea_co2 = build_eea_co2(year)
eea_co2 = build_eea_co2(snakemake, year)

# TODO: read Eurostat data from year > 2014
# this only affects the estimation of CO2 emissions for BA, RS, AL, ME, MK
if year > 2014:
eurostat_co2 = build_eurostat_co2(year=2014)
eurostat_co2 = build_eurostat_co2(snakemake, countries, year=2014)
else:
eurostat_co2 = build_eurostat_co2(year)
eurostat_co2 = build_eurostat_co2(snakemake, countries, year)

co2_totals = build_co2_totals(eea_co2, eurostat_co2)
co2_totals = build_co2_totals(countries, eea_co2, eurostat_co2)

sectors = emission_sectors_from_opts(opts)

Expand Down Expand Up @@ -234,7 +234,7 @@ def exponential_decay(t):

# TODO log in Snakefile
if not os.path.exists(fn):
os.makedirs(fn)
os.makedirs(os.path.dirname(fn))
co2_cap.to_csv(fn, float_format='%.3f')


Expand Down Expand Up @@ -2336,7 +2336,7 @@ def limit_individual_line_extension(n, maxext):
opts="",
clusters="37",
lv=1.5,
sector_opts='Co2L0-168H-T-H-B-I-solar3-dist1',
sector_opts='cb40ex0-2000H-T-H-B-I-A',
planning_horizons="2020",
)

Expand Down