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

enable add_hydro feature and parallel downloading of osm data #232

Merged
merged 26 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0248f48
enable add_hydro feature
davide-f Jan 5, 2022
aef573c
typo for saving empty files
davide-f Jan 19, 2022
4332339
Add default load shedding to avoid feasibility issues when no generat…
davide-f Jan 19, 2022
5631404
Add release note
davide-f Jan 19, 2022
eedf4a2
Restyled by black
restyled-commits Jan 19, 2022
b0ddf88
Restyled by isort
restyled-commits Jan 19, 2022
ca90a78
Restyled by reorder-python-imports
restyled-commits Jan 19, 2022
0a9bbe4
Restyled by yapf
restyled-commits Jan 19, 2022
068053e
Merge pull request #233 from pypsa-meets-africa/restyled/enable_hydro
davide-f Jan 19, 2022
d216278
Add test hydro capacities for ZA
davide-f Jan 20, 2022
824c5bd
Update draft hydro capacities file for all countries
davide-f Jan 20, 2022
ed7b006
Move validation_nigeria in validation folder
davide-f Jan 20, 2022
9db8c4c
Parallel download of osm data by optional configuration parameter
davide-f Jan 20, 2022
87c71fb
Update release note
davide-f Jan 20, 2022
33d4f83
Restyled by autopep8
restyled-commits Jan 20, 2022
1570795
Restyled by black
restyled-commits Jan 20, 2022
b9949e3
Restyled by isort
restyled-commits Jan 20, 2022
ab7e89e
Restyled by reorder-python-imports
restyled-commits Jan 20, 2022
c5cdc48
Restyled by whitespace
restyled-commits Jan 20, 2022
735d62c
Restyled by yapf
restyled-commits Jan 20, 2022
40091a0
Add load curtailment tech color
davide-f Jan 20, 2022
22504c5
Merge pull request #235 from pypsa-meets-africa/restyled/enable_hydro
davide-f Jan 20, 2022
1256bc9
Merge branch 'enable_hydro' of https://github.com/pypsa-meets-africa/…
davide-f Jan 20, 2022
31bf808
Typo load curtailment name
davide-f Jan 20, 2022
8c6e96e
Increase load shedding cost
davide-f Jan 20, 2022
a242f70
Revert typo
davide-f Jan 20, 2022
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
3 changes: 1 addition & 2 deletions Snakefile
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ rule add_electricity:
powerplants='resources/powerplants.csv',
load='resources/ssp2-2.6/2030/era5_2013/Africa.nc',
gadm_shapes='resources/gadm_shapes.geojson',
# hydro_capacities='data/bundle/hydro_capacities.csv',
# geth_hydro_capacities='data/geth2015_hydro_capacities.csv',
hydro_capacities='data/hydro_capacities.csv',
**{f"profile_{tech}": f"resources/profile_{tech}.nc"
for tech in config['renewable']}
output: "networks/elec.nc"
Expand Down
4 changes: 3 additions & 1 deletion config.default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ enable:
# requires cds API key https://cds.climate.copernicus.eu/api-how-to
# More information https://atlite.readthedocs.io/en/latest/introduction.html#datasets

# download_osm_data_nprocesses: 10 # (optional) number of threads used to download osm data

augmented_line_connection:
add_to_snakefile: True # If True, includes this rule to the workflow
Expand Down Expand Up @@ -228,7 +229,7 @@ costs:
solving:
options:
formulation: kirchhoff
load_shedding: false
load_shedding: true
noisy_costs: true
min_iterations: 4
max_iterations: 6
Expand Down Expand Up @@ -314,6 +315,7 @@ plotting:
"HVDC links": "#8a1caf"
"DC-DC": "#8a1caf"
"DC link": "#8a1caf"
"load": "#FF0000"
nice_names:
OCGT: "Open-Cycle Gas"
CCGT: "Combined-Cycle Gas"
Expand Down
3 changes: 2 additions & 1 deletion config.tutorial.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ costs:
solving:
options:
formulation: kirchhoff
load_shedding: false
load_shedding: true
noisy_costs: true
min_iterations: 4
max_iterations: 6
Expand Down Expand Up @@ -336,6 +336,7 @@ plotting:
"HVDC links": "#8a1caf"
"DC-DC": "#8a1caf"
"DC link": "#8a1caf"
"load": "#FF0000"
nice_names:
OCGT: "Open-Cycle Gas"
CCGT: "Combined-Cycle Gas"
Expand Down
52 changes: 52 additions & 0 deletions data/hydro_capacities.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
country_name,Country,p_nom_discharge[GW],p_nom_store[GW],E_store[TWh],InflowHourlyAvg[GWh]
Algeria,DZ,237.900,0.000,0.267,3.65331931
Angola,AO,2414.800,0.000,3.892,53.32168268
Benin,BJ,32.800,0.000,0.243,3.325925824
Botswana,BW,-,-,-,-
Burkina Faso,BF,32.000,0.000,1.096,15.00818418
Burundi,BI,56.000,0.000,0.000,0
Cameroon,CM,987.000,0.000,0.950,13.01468008
Central African Republic,CF,18.000,0.000,0.013,0.174394165
Chad,TD,-,-,-,-
Congo,CG,236.000,0.000,0.052,0.711041947
Cote d'Ivoire,CI,880.000,0.000,4.470,61.23522422
Democratic Republic of Congo,CD,2593.000,0.000,-,-
Djibouti,DJ,-,-,-,-
Egypt,EG,2842.000,0.000,49.823,682.5030197
Equatorial Guinea,GQ,128.000,0.000,0.005,0.064628888
Eritrea,ER,-,-,-,-
Eswatini,SZ,-,-,-,-
Ethiopia,ET,4073.500,0.000,-,-
Gabon,GA,330.590,0.000,0.084,1.155464484
Gambia,GM,-,-,-,-
Ghana,GH,1605.800,0.000,56.423,772.9224202
Guinea,GN,367.150,0.000,0.361,4.950213441
Guinea-Bissau,GW,-,-,-,-
Kenya,KE,836.480,0.000,-,-
Lesotho,LS,72.000,0.000,0.985,13.49333942
Liberia,LR,93.000,0.000,0.012,0.169565364
Libya,LY,-,-,-,-
Madagascar,MG,-,-,-,-
Malawi,MW,348.750,0.000,0.000,0
Mali,ML,356.478,0.000,2.317,31.74441348
Mauritania,MR,49.000,0.000,0.002,0.027472925
Morocco,MA,1922.972,466.000,2.999,41.08339401
Mozambique,MZ,2207.110,0.000,24.754,339.0949095
Namibia,NA,353.000,0.000,0.951,13.03140528
Niger,NE0
Nigeria,NG,2058.340,0.000,6.020,82.46639066
Rwanda,RW,140.350,0.000,-,-
Senegal,SN,150.000,0.000,0.002,0.027472925
Sierra Leone,SL,61.200,0.000,0.107,1.464723968
Somalia,SO,-,-,-,-
South Africa,ZA,3624.754,2922.400,2.311,31.66170131
South Sudan,SS,5.000,0.000,0.000,0
Sudan,SD,1903.800,0.000,4.650,63.69230241
Swaziland,SZ,88.490,0.000,0.119,1.633063617
Tanzania,TZ,572.000,0.000,0.507,6.946068164
Togo,TG,32.800,0.000,0.243,3.325925824
Tunisia,TN,62.325,0.000,0.106,1.45312886
Uganda,UG,741.200,0.000,6.553,89.76871852
Western Sahara,EH,-,-,-,-
Zambia,ZM,2325.550,0.000,34.136,467.6157751
Zimbabwe,ZW,1001.790,0.000,32.853,450.0456825
3 changes: 3 additions & 0 deletions doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Release Notes
Upcoming Release
================

* Attach hydro enabled with all hydro types
* Parallel download of osm data


PyPSA-Africa 0.0.1 (24th December 2021)
=====================================
Expand Down
3 changes: 2 additions & 1 deletion scripts/build_bus_regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def save_to_geojson(df, fn):
df.to_file(fn, driver="GeoJSON", schema=schema)
else:
# create empty file to avoid issues with snakemake
with os.open(fn, "w") as fp:
with open(fn, "w") as fp:
pass


Expand Down Expand Up @@ -147,6 +147,7 @@ def custom_voronoi_partition_pts(points,


def get_gadm_shape(onshore_locs, gadm_shapes):

def locate_bus(coords):
try:
return gadm_shapes[gadm_shapes.contains(
Expand Down
66 changes: 64 additions & 2 deletions scripts/download_osm_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import hashlib
import json
import logging
import multiprocessing as mp
import os
import pickle
import shutil
Expand Down Expand Up @@ -41,6 +42,7 @@
from shapely.geometry import LineString
from shapely.geometry import Point
from shapely.geometry import Polygon
from tqdm import tqdm

# esy.osm filter: https://gitlab.com/dlr-ve-esy/esy-osmfilter/-/tree/master/

Expand Down Expand Up @@ -426,18 +428,74 @@ def output_csv_geojson(output_files, country_code, df_all_feature,
driver="GeoJSON") # Generate GeoJson


# Auxiliary function to initialize the parallel data download
def _init_process_pop(update_, verify_):
global update, verify
update, verify = update_, verify_


# Auxiliary function to download the data
def _process_func_pop(c_code):
download_pbf(c_code, update, verify)


def parallel_download_pbf(country_list,
nprocesses,
update=False,
verify=False):
"""
Function to download pbf data in parallel

Parameters
----------
country_list : str
List of geofabrik country codes to download
nprocesses : int
Number of parallel processes
update : bool
If true, existing pbf files are updated. Default: False
verify : bool
If true, checks the md5 of the file. Default: False
"""

# argument for the parallel processing
kwargs = {
"initializer": _init_process_pop,
"initargs": (update, verify),
"maxtasksperchild": 20,
"processes": nprocesses,
}

# execute the parallel download with tqdm progressbar
with mp.get_context("spawn").Pool(**kwargs) as pool:
for _ in tqdm(
pool.imap(_process_func_pop, country_list),
ascii=False,
unit=" countries",
total=len(country_list),
desc="Download pbf ",
):
pass


def process_data(
feature_list,
country_list,
output_files,
iso_coding=True,
update=False,
verify=False,
nprocesses=1,
):
"""
Download the features in feature_list for each country of the country_list
"""

# parallel download of data if parallel download is enabled
if nprocesses > 1:
_logger.info(f"Parallel pbf download with {nprocesses} threads")
parallel_download_pbf(country_list, nprocesses, update, verify)

# loop the request for each feature

for feature in feature_list: # feature dataframe
Expand Down Expand Up @@ -511,6 +569,7 @@ def create_country_list(input, iso_coding=True):
full_codes_list : list
Example ["NG","ZA"]
"""

def filter_codes(c_list, iso_coding=True):
"""
Filter list according to the specified coding.
Expand Down Expand Up @@ -568,8 +627,10 @@ def filter_codes(c_list, iso_coding=True):
# ["substation", "generator", "line", "cable", "tower"]
feature_list = ["substation", "generator", "line", "cable"]

input = snakemake.config["countries"]
output_files = snakemake.output
input = snakemake.config["countries"] # country list or region
output_files = snakemake.output # output snakemake
nprocesses = snakemake.config.get("download_osm_data_nprocesses",
1) # number of threads

country_list = create_country_list(input)

Expand All @@ -581,4 +642,5 @@ def filter_codes(c_list, iso_coding=True):
iso_coding=True,
update=False,
verify=False,
nprocesses=nprocesses,
)
2 changes: 1 addition & 1 deletion scripts/solve_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def prepare_network(n, solve_opts):
bus=buses_i,
carrier="load",
sign=1e-3, # Adjust sign to measure p and p_nom in kW instead of MW
marginal_cost=1e2, # Eur/kWh
marginal_cost=8e3, # Eur/kWh
# intersect between macroeconomic and surveybased
# willingness to pay
# http://journal.frontiersin.org/article/10.3389/fenrg.2015.00055/full
Expand Down
5 changes: 4 additions & 1 deletion test/config.test1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ enable:
build_natura_raster: true # If True, then build_natura_raster can be run
# custom_busmap: false

download_osm_data_nprocesses: 4 # (optional) number of threads used to download osm data

electricity:
voltages: [220., 300., 380.]
co2limit: 7.75e+7 # 0.05 * 3.1e9*0.5
Expand Down Expand Up @@ -258,7 +260,7 @@ costs:
solving:
options:
formulation: kirchhoff
load_shedding: false
load_shedding: true
noisy_costs: true
min_iterations: 4
max_iterations: 6
Expand Down Expand Up @@ -336,6 +338,7 @@ plotting:
"HVDC links": "#8a1caf"
"DC-DC": "#8a1caf"
"DC link": "#8a1caf"
"load": "#FF0000"
nice_names:
OCGT: "Open-Cycle Gas"
CCGT: "Combined-Cycle Gas"
Expand Down