Skip to content

Commit

Permalink
Merge pull request #359 from davide-f/fix_build_powerplants
Browse files Browse the repository at this point in the history
Fix powerplantmatching problem for DRC
  • Loading branch information
pz-max authored May 31, 2022
2 parents 1083a85 + 2f7f948 commit 46f2756
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
14 changes: 8 additions & 6 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,19 @@ Upcoming Release

* Add new countries and update iso code: `PR #330 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/330>`_

* Fix solar pv slope and add correction factor for wake losses `PR #335 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/350>`_
* Fix solar pv slope and add correction factor for wake losses: `PR #335 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/350>`_

* Add renewable potential notebook `PR #351 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/351>`_
* Add renewable potential notebook: `PR #351 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/351>`_

* Make cutout workflow simpler `PR #352 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/352>`_
* Make cutout workflow simpler: `PR #352 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/352>`_

* Add option to run workflow without pop and gdp raster `PR #353 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/353>`_
* Add option to run workflow without pop and gdp raster: `PR #353 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/353>`_

* Add latitude_optimal to get optimal solar orientation by default `Commit 1b2466b <https://github.com/pypsa-meets-africa/pypsa-africa/commit/de7d32be8807e4fc42486a60184f45680612fd46>`_
* Add latitude_optimal to get optimal solar orientation by default: `Commit 1b2466b <https://github.com/pypsa-meets-africa/pypsa-africa/commit/de7d32be8807e4fc42486a60184f45680612fd46>`_

* Harmonize CRSs by options `PR #356 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/356>`_
* Harmonize CRSs by options: `PR #356 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/356>`_

* Fix powerplantmatching problem for DRC and countries with multi-word name: `PR #359 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/359>`_

* Change default option for build_natura `PR #360 <https://github.com/pypsa-meets-africa/pypsa-africa/pull/360>`_

Expand Down
27 changes: 26 additions & 1 deletion scripts/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,20 @@ def three_2_two_digits_country(three_code_country):
return two_code_country


def two_digits_2_name_country(two_code_country):
def two_digits_2_name_country(two_code_country, nocomma=False, remove_start_words=[]):
"""
Convert 2-digit country code to full name country:
Parameters
----------
two_code_country: str
2-digit country name
nocomma: bool (optional, default False)
When true, country names with comma are extended to remove the comma.
Example CD -> Congo, The Democratic Republic of -> The Democratic Republic of Congo
remove_start_words: list (optional, default empty)
When a sentence starts with any of the provided words, the beginning is removed.
e.g. The Democratic Republic of Congo -> Democratic Republic of Congo (remove_start_words=["The"])
Returns
----------
Expand All @@ -527,6 +533,25 @@ def two_digits_2_name_country(two_code_country):
return f"{two_digits_2_name_country('SN')}-{two_digits_2_name_country('GM')}"

full_name = get_country("name", alpha_2=two_code_country)

if nocomma:
# separate list by delim
splits = full_name.split(", ")

# reverse the order
splits.reverse()

# return the merged string
full_name = " ".join(splits)

# when list is non empty
if remove_start_words:
# loop over every provided word
for word in remove_start_words:
# when the full_name starts with the desired word, then remove it
if full_name.startswith(word):
full_name = full_name.replace(word, "", 1)

return full_name


Expand Down
15 changes: 12 additions & 3 deletions scripts/build_powerplants.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@

logger = logging.getLogger(__name__)

# Auxiliary function to adapt to the ppl format
two_digits_2_nocomma_country_name = lambda x: two_digits_2_name_country(
x, nocomma=True, remove_start_words=["The ", "the "]
)


def convert_osm_to_pm(filepath_ppl_osm, filepath_ppl_pm):

Expand Down Expand Up @@ -141,7 +146,7 @@ def convert_osm_to_pm(filepath_ppl_osm, filepath_ppl_pm):
)
)
.assign(
Country=lambda df: df.Country.map(two_digits_2_name_country),
Country=lambda df: df.Country.map(two_digits_2_nocomma_country_name),
Name=lambda df: df.Name,
# Name=lambda df: "OSM_"
# + df.Country.astype(str)
Expand Down Expand Up @@ -235,7 +240,11 @@ def add_custom_powerplants(ppl):

n = pypsa.Network(snakemake.input.base_network)
countries_codes = n.buses.country.unique()
countries_names = list(map(two_digits_2_name_country, countries_codes))
countries_names = list(map(two_digits_2_nocomma_country_name, countries_codes))

# create code name mapping to be used as inverse function
country_mapping = dict(zip(countries_names, countries_codes))

config["target_countries"] = countries_names

if "EXTERNAL_DATABASE" in config:
Expand All @@ -255,7 +264,7 @@ def add_custom_powerplants(ppl):
df.Technology.replace("Steam Turbine", "OCGT").fillna("OCGT"),
)
),
Country=lambda df: df.Country.map(country_name_2_two_digits),
Country=lambda df: df.Country.map(lambda x: country_mapping[x]),
)
)

Expand Down

0 comments on commit 46f2756

Please sign in to comment.