-
Notifications
You must be signed in to change notification settings - Fork 8
/
build_demand.py
282 lines (228 loc) · 9.06 KB
/
build_demand.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# -*- coding: utf-8 -*-
"""
Estimates the population and the electric load of each microgrid.
Relevant Settings
-----------------
.. code:: yaml
microgrids_list:
microgridX:
lon_min:
lon_max:
lat_min:
lat_max:
load:
scaling_factor:
Inputs
------
- ``data/sample_profile.csv``: a load profile, which will be scaled through a scaling_factor to obtain the per person load
Outputs
-------
- ``resources/shapes/microgrid_shapes.geojson``: a geojson file of the shape of each microgrid,
- ``resources/masked_files/masked_file_{i+1}.tif``,
- ``resources/demand/microgrid_load_{i+1}.csv``: the electric load of the microgid,
Description
-----------
The rule :mod:`build_demand` contains functions that are used to create a shape file of the microgrid, to mask a raster with the shape file and to estimate
the population. Then the population is multiplied for the per person load and the microgrid load is then obtained. The process applies to all the microgrids specified in config.yaml.
"""
import json
import logging
import os
import shutil
import geopandas as gpd
import pandas as pd
import pypsa
import rasterio
import rasterio.mask
import requests
from _helpers_dist import (
configure_logging,
sets_path_to_root,
two_2_three_digits_country,
)
_logger = logging.getLogger(__name__)
_logger.setLevel(logging.INFO)
def get_WorldPop_data(
country_code,
year,
update=False,
out_logging=False,
size_min=300,
):
"""
Download tiff file for each country code using the standard method from worldpop datastore with 1kmx1km resolution.
Parameters
----------
country_code : str
Two letter country codes of the downloaded files.
Files downloaded from https://data.worldpop.org/ datasets WorldPop UN adjusted
year : int
Year of the data to download
update : bool
Update = true, forces re-download of files
size_min : int
Minimum size of each file to download
Returns
-------
WorldPop_inputfile : str
Path of the file
"""
three_digits_code = two_2_three_digits_country(country_code)
if out_logging:
_logger.info("Get WorldPop datasets")
if country_code == "XK":
WorldPop_filename = f"srb_ppp_{year}_UNadj_constrained.tif"
WorldPop_urls = [
f"https://data.worldpop.org/GIS/Population/Global_2000_2020_Constrained/2020/BSGM/SRB/{WorldPop_filename}",
f"https://data.worldpop.org/GIS/Population/Global_2000_2020_Constrained/2020/maxar_v1/SRB/{WorldPop_filename}",
]
else:
WorldPop_filename = (
f"{three_digits_code.lower()}_ppp_{year}_UNadj_constrained.tif"
)
# Urls used to possibly download the file
WorldPop_urls = [
f"https://data.worldpop.org/GIS/Population/Global_2000_2020_Constrained/2020/BSGM/{two_2_three_digits_country(country_code).upper()}/{WorldPop_filename}",
f"https://data.worldpop.org/GIS/Population/Global_2000_2020_Constrained/2020/maxar_v1/{two_2_three_digits_country(country_code).upper()}/{WorldPop_filename}",
]
WorldPop_inputfile = os.path.join(
os.getcwd(),
"pypsa-earth",
"data",
"WorldPop",
WorldPop_filename,
) # Input filepath tif
if not os.path.exists(WorldPop_inputfile) or update is True:
if out_logging:
_logger.warning(
f"{WorldPop_filename} does not exist, downloading to {WorldPop_inputfile}"
)
# create data/osm directory
os.makedirs(os.path.dirname(WorldPop_inputfile), exist_ok=True)
loaded = False
for WorldPop_url in WorldPop_urls:
with requests.get(WorldPop_url, stream=True) as r:
with open(WorldPop_inputfile, "wb") as f:
if float(r.headers["Content-length"]) > size_min:
shutil.copyfileobj(r.raw, f)
loaded = True
break
if not loaded:
_logger.error(f"Impossible to download {WorldPop_filename}")
return WorldPop_inputfile, WorldPop_filename
# Estimate the total population of tghe microgrid
def estimate_microgrid_population(
n, p, raster_path, shapes_path, sample_profile, output_file
):
# Read the sample profile of electricity demand and extract the column corresponding to the electric load
per_unit_load = pd.read_csv(sample_profile)["0"] / p
# Dataframe of the load
microgrid_load = pd.DataFrame()
# Load the GeoJSON file with the shapes to mask the raster
shapes = gpd.read_file(shapes_path)
# Mask the raster with each shape and save each masked raster as a new file
for i, shape in shapes.iterrows():
with rasterio.open(raster_path) as src:
# Mask the raster with the current shape
masked, out_transform = rasterio.mask.mask(src, [shape.geometry], crop=True)
out_meta = src.meta.copy()
out_meta.update(
{
"driver": "GTiff",
"height": masked.shape[1],
"width": masked.shape[2],
"transform": out_transform,
}
)
pop_microgrid = masked[masked >= 0].sum()
col_name = "microgrid_1_bus_572666767"
microgrid_load[col_name] = per_unit_load * pop_microgrid
return pop_microgrid, microgrid_load
def count_buildings_per_cluster(geojson_file):
with open(geojson_file) as f:
data = json.load(f)
cluster_counts = {}
for feature in data["features"]:
cluster = feature["properties"]["cluster"]
buildings = feature["properties"]["buildings"]
if cluster not in cluster_counts:
cluster_counts[cluster] = len(buildings)
else:
cluster_counts[cluster] += len(buildings)
total_buildings = sum(cluster_counts.values())
return total_buildings, cluster_counts
def calculate_load(
n, p, raster_path, shapes_path, sample_profile, geojson_file, output_file
):
# Estimate the microgrid population and load using the existing function
pop_microgrid, microgrid_load = estimate_microgrid_population(
n, p, raster_path, shapes_path, sample_profile, output_file
)
# Count the total number of buildings and clusters in the geojson file using the existing function
total_buildings, cluster_counts = count_buildings_per_cluster(geojson_file)
# Calculate the number population per building
population_per_building = pop_microgrid / total_buildings
# Calculate the population per cluster using the cluster counts dictionary
population_per_cluster = {
cluster: population_per_building * count
for cluster, count in cluster_counts.items()
}
# Calculate the per unit load
per_unit_load = pd.read_csv(sample_profile)["0"] / p
# Create a dictionary of DataFrames with the load per cluster
load_df_dict = {}
for cluster_id in population_per_cluster:
load_df_dict[cluster_id] = pd.DataFrame(
per_unit_load * population_per_cluster[cluster_id]
)
# Concatenate the DataFrames into a single DataFrame
load_df = pd.concat(load_df_dict, axis=1)
# Remove the second level index
load_df.columns = load_df.columns.droplevel(level=1)
# Change column names to 'bus_' + the original column number
load_df.columns = ["bus_" + str(col) for col in load_df.columns]
# Remove the bus_9 column
load_df = load_df.drop("bus_9", axis=1)
# Save the microgrid load to a CSV file with snapshots index
load_df.insert(0, "snapshots", n.snapshots)
load_df.set_index("snapshots", inplace=True)
load_df.to_csv(output_file, index=True)
# Return the DataFrame
return load_df
if __name__ == "__main__":
if "snakemake" not in globals():
from _helpers_dist import mock_snakemake
os.chdir(os.path.dirname(os.path.abspath(__file__)))
snakemake = mock_snakemake("build_demand")
sets_path_to_root("pypsa-distribution")
configure_logging(snakemake)
n = pypsa.Network(snakemake.input.create_network)
sample_profile = snakemake.input["sample_profile"]
assert (
len(snakemake.config["countries"]) == 1
), "Error: only a country shall be specified"
worldpop_path, worldpop_flname = get_WorldPop_data(
snakemake.config["countries"][
0
], # TODO: this needs fix to generalize the countries
snakemake.config["build_shape_options"]["year"],
False,
)
estimate_microgrid_population(
n,
snakemake.config["load"]["scaling_factor"],
worldpop_path,
snakemake.input["microgrid_shapes"],
sample_profile,
snakemake.output["electric_load"],
)
count_buildings_per_cluster(snakemake.input["clusters_with_buildings"])
calculate_load(
n,
snakemake.config["load"]["scaling_factor"],
worldpop_path,
snakemake.input["microgrid_shapes"],
sample_profile,
snakemake.input["clusters_with_buildings"],
snakemake.output["electric_load"],
)