Skip to content

Commit

Permalink
Merge pull request #272 from pypsa-meets-earth/271-keyerror-total-rai…
Browse files Browse the repository at this point in the history
…l-for-namibia

fix: write nan values when sector data is not given
  • Loading branch information
energyLS authored Feb 8, 2024
2 parents 7338253 + c19fca2 commit e26df4c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
49 changes: 45 additions & 4 deletions scripts/build_base_energy_totals.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import glob
import logging
import os
import sys
from io import BytesIO
Expand All @@ -15,6 +16,8 @@
import requests
from helpers import sets_path_to_root, three_2_two_digits_country

_logger = logging.getLogger(__name__)

pd.options.mode.chained_assignment = None


Expand All @@ -39,7 +42,47 @@ def calc_sector(sector):
]

if df_sector.empty:
pass
if sector == "consumption by households":
energy_totals_base.at[country, "electricity residential"] = np.NaN
energy_totals_base.at[country, "residential oil"] = np.NaN
energy_totals_base.at[country, "residential biomass"] = np.NaN
energy_totals_base.at[country, "residential gas"] = np.NaN
energy_totals_base.at[country, "total residential space"] = np.NaN
energy_totals_base.at[country, "total residential water"] = np.NaN

elif sector == "services":
energy_totals_base.at[country, "services electricity"] = np.NaN
energy_totals_base.at[country, "services oil"] = np.NaN
energy_totals_base.at[country, "services biomass"] = np.NaN
energy_totals_base.at[country, "services gas"] = np.NaN
energy_totals_base.at[country, "total services space"] = np.NaN
energy_totals_base.at[country, "total services water"] = np.NaN

elif sector == "road":
energy_totals_base.at[country, "total road"] = np.NaN

elif sector == "agriculture":
energy_totals_base.at[country, "agriculture electricity"] = np.NaN
energy_totals_base.at[country, "agriculture oil"] = np.NaN
energy_totals_base.at[country, "agriculture biomass"] = np.NaN
# energy_totals_base.at[country, "electricity rail"] = np.NaN

elif sector == "rail":
energy_totals_base.at[country, "total rail"] = np.NaN
energy_totals_base.at[country, "electricity rail"] = np.NaN

elif sector == "aviation":
energy_totals_base.at[country, "total international aviation"] = np.NaN
energy_totals_base.at[country, "total domestic aviation"] = np.NaN

elif sector == "navigation":
energy_totals_base.at[
country, "total international navigation"
] = np.NaN
energy_totals_base.at[country, "total domestic navigation"] = np.NaN

_logger.warning("No data for " + country + " in the sector " + sector + ".")

else:
index_mass = df_sector.loc[
df_sector["Unit"] == "Metric tons, thousand"
Expand Down Expand Up @@ -361,6 +404,4 @@ def calc_sector(sector):
calc_sector(sector)

# Export the base energy totals file
energy_totals_base.dropna(axis=1, how="all").to_csv(
snakemake.output.energy_totals_base
)
energy_totals_base.to_csv(snakemake.output.energy_totals_base)
2 changes: 0 additions & 2 deletions scripts/prepare_energy_totals.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,4 @@ def calculate_end_values(df):
energy_totals["electricity services space"] = 0
energy_totals["electricity services water"] = 0

energy_totals = energy_totals.dropna(axis=1, how="all")

energy_totals.fillna(0).to_csv(snakemake.output.energy_totals)

0 comments on commit e26df4c

Please sign in to comment.