-
Notifications
You must be signed in to change notification settings - Fork 198
/
_helpers.py
692 lines (555 loc) · 20.6 KB
/
_helpers.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: : 2017-2020 The PyPSA-Eur Authors
#
# SPDX-License-Identifier: GPL-3.0-or-later
import os
from pathlib import Path
import country_converter as coco
import geopandas as gpd
import numpy as np
import pandas as pd
REGION_COLS = ["geometry", "name", "x", "y", "country"]
def sets_path_to_root(root_directory_name):
"""
Search and sets path to the given root directory (root/path/file).
Parameters
----------
root_directory_name : str
Name of the root directory.
n : int
Number of folders the function will check upwards/root directed.
"""
import os
repo_name = root_directory_name
n = 8 # check max 8 levels above. Random default.
n0 = n
while n >= 0:
n -= 1
# if repo_name is current folder name, stop and set path
if repo_name == os.path.basename(os.path.abspath(".")):
repo_path = os.getcwd() # os.getcwd() = current_path
os.chdir(repo_path) # change dir_path to repo_path
print("This is the repository path: ", repo_path)
print("Had to go %d folder(s) up." % (n0 - 1 - n))
break
# if repo_name NOT current folder name for 5 levels then stop
if n == 0:
print("Cant find the repo path.")
# if repo_name NOT current folder name, go one dir higher
else:
upper_path = os.path.dirname(os.path.abspath(".")) # name of upper folder
os.chdir(upper_path)
def configure_logging(snakemake, skip_handlers=False):
"""
Configure the basic behaviour for the logging module.
Note: Must only be called once from the __main__ section of a script.
The setup includes printing log messages to STDERR and to a log file defined
by either (in priority order): snakemake.log.python, snakemake.log[0] or "logs/{rulename}.log".
Additional keywords from logging.basicConfig are accepted via the snakemake configuration
file under snakemake.config.logging.
Parameters
----------
snakemake : snakemake object
Your snakemake object containing a snakemake.config and snakemake.log.
skip_handlers : True | False (default)
Do (not) skip the default handlers created for redirecting output to STDERR and file.
"""
import logging
kwargs = snakemake.config.get("logging", dict()).copy()
kwargs.setdefault("level", "INFO")
if skip_handlers is False:
fallback_path = Path(__file__).parent.joinpath(
"..", "logs", f"{snakemake.rule}.log"
)
logfile = snakemake.log.get(
"python", snakemake.log[0] if snakemake.log else fallback_path
)
kwargs.update(
{
"handlers": [
# Prefer the "python" log, otherwise take the first log for each
# Snakemake rule
logging.FileHandler(logfile),
logging.StreamHandler(),
]
}
)
logging.basicConfig(**kwargs, force=True)
def load_network(import_name=None, custom_components=None):
"""
Helper for importing a pypsa.Network with additional custom components.
Parameters
----------
import_name : str
As in pypsa.Network(import_name)
custom_components : dict
Dictionary listing custom components.
For using ``snakemake.config["override_components"]``
in ``config.yaml`` define:
.. code:: yaml
override_components:
ShadowPrice:
component: ["shadow_prices","Shadow price for a global constraint.",np.nan]
attributes:
name: ["string","n/a","n/a","Unique name","Input (required)"]
value: ["float","n/a",0.,"shadow value","Output"]
Returns
-------
pypsa.Network
"""
import pypsa
from pypsa.descriptors import Dict
override_components = None
override_component_attrs = None
if custom_components is not None:
override_components = pypsa.components.components.copy()
override_component_attrs = Dict(
{k: v.copy() for k, v in pypsa.components.component_attrs.items()}
)
for k, v in custom_components.items():
override_components.loc[k] = v["component"]
override_component_attrs[k] = pd.DataFrame(
columns=["type", "unit", "default", "description", "status"]
)
for attr, val in v["attributes"].items():
override_component_attrs[k].loc[attr] = val
return pypsa.Network(
import_name=import_name,
override_components=override_components,
override_component_attrs=override_component_attrs,
)
def pdbcast(v, h):
return pd.DataFrame(
v.values.reshape((-1, 1)) * h.values, index=v.index, columns=h.index
)
def load_network_for_plots(fn, tech_costs, config, combine_hydro_ps=True):
import pypsa
from add_electricity import load_costs, update_transmission_costs
n = pypsa.Network(fn)
n.loads["carrier"] = n.loads.bus.map(n.buses.carrier) + " load"
n.stores["carrier"] = n.stores.bus.map(n.buses.carrier)
n.links["carrier"] = (
n.links.bus0.map(n.buses.carrier) + "-" + n.links.bus1.map(n.buses.carrier)
)
n.lines["carrier"] = "AC line"
n.transformers["carrier"] = "AC transformer"
n.lines["s_nom"] = n.lines["s_nom_min"]
n.links["p_nom"] = n.links["p_nom_min"]
if combine_hydro_ps:
n.storage_units.loc[
n.storage_units.carrier.isin({"PHS", "hydro"}), "carrier"
] = "hydro+PHS"
# if the carrier was not set on the heat storage units
# bus_carrier = n.storage_units.bus.map(n.buses.carrier)
# n.storage_units.loc[bus_carrier == "heat","carrier"] = "water tanks"
Nyears = n.snapshot_weightings.objective.sum() / 8760.0
costs = load_costs(Nyears, tech_costs, config["costs"], config["electricity"])
update_transmission_costs(n, costs)
return n
def update_p_nom_max(n):
# if extendable carriers (solar/onwind/...) have capacity >= 0,
# e.g. existing assets from the OPSD project are included to the network,
# the installed capacity might exceed the expansion limit.
# Hence, we update the assumptions.
n.generators.p_nom_max = n.generators[["p_nom_min", "p_nom_max"]].max(1)
def aggregate_p_nom(n):
return pd.concat(
[
n.generators.groupby("carrier").p_nom_opt.sum(),
n.storage_units.groupby("carrier").p_nom_opt.sum(),
n.links.groupby("carrier").p_nom_opt.sum(),
n.loads_t.p.groupby(n.loads.carrier, axis=1).sum().mean(),
]
)
def aggregate_p(n):
return pd.concat(
[
n.generators_t.p.sum().groupby(n.generators.carrier).sum(),
n.storage_units_t.p.sum().groupby(n.storage_units.carrier).sum(),
n.stores_t.p.sum().groupby(n.stores.carrier).sum(),
-n.loads_t.p.sum().groupby(n.loads.carrier).sum(),
]
)
def aggregate_e_nom(n):
return pd.concat(
[
(n.storage_units["p_nom_opt"] * n.storage_units["max_hours"])
.groupby(n.storage_units["carrier"])
.sum(),
n.stores["e_nom_opt"].groupby(n.stores.carrier).sum(),
]
)
def aggregate_p_curtailed(n):
return pd.concat(
[
(
(
n.generators_t.p_max_pu.sum().multiply(n.generators.p_nom_opt)
- n.generators_t.p.sum()
)
.groupby(n.generators.carrier)
.sum()
),
(
(n.storage_units_t.inflow.sum() - n.storage_units_t.p.sum())
.groupby(n.storage_units.carrier)
.sum()
),
]
)
def aggregate_costs(n, flatten=False, opts=None, existing_only=False):
components = dict(
Link=("p_nom", "p0"),
Generator=("p_nom", "p"),
StorageUnit=("p_nom", "p"),
Store=("e_nom", "p"),
Line=("s_nom", None),
Transformer=("s_nom", None),
)
costs = {}
for c, (p_nom, p_attr) in zip(
n.iterate_components(components.keys(), skip_empty=False), components.values()
):
if c.df.empty:
continue
if not existing_only:
p_nom += "_opt"
costs[(c.list_name, "capital")] = (
(c.df[p_nom] * c.df.capital_cost).groupby(c.df.carrier).sum()
)
if p_attr is not None:
p = c.pnl[p_attr].sum()
if c.name == "StorageUnit":
p = p.loc[p > 0]
costs[(c.list_name, "marginal")] = (
(p * c.df.marginal_cost).groupby(c.df.carrier).sum()
)
costs = pd.concat(costs)
if flatten:
assert opts is not None
conv_techs = opts["conv_techs"]
costs = costs.reset_index(level=0, drop=True)
costs = costs["capital"].add(
costs["marginal"].rename({t: t + " marginal" for t in conv_techs}),
fill_value=0.0,
)
return costs
def progress_retrieve(url, file, data=None, disable_progress=False, roundto=1.0):
"""
Function to download data from a url with a progress bar progress in retrieving data
Parameters
----------
url : str
Url to download data from
file : str
File where to save the output
data : dict
Data for the request (default None), when not none Post method is used
disable_progress : bool
When true, no progress bar is shown
roundto : float
(default 0) Precision used to report the progress
e.g. 0.1 stands for 88.1, 10 stands for 90, 80
"""
import urllib
from tqdm import tqdm
pbar = tqdm(total=100, disable=disable_progress)
def dlProgress(count, blockSize, totalSize, roundto=roundto):
pbar.n = round(count * blockSize * 100 / totalSize / roundto) * roundto
pbar.refresh()
if data is not None:
data = urllib.parse.urlencode(data).encode()
urllib.request.urlretrieve(url, file, reporthook=dlProgress, data=data)
def get_aggregation_strategies(aggregation_strategies):
"""
default aggregation strategies that cannot be defined in .yaml format must be specified within
the function, otherwise (when defaults are passed in the function's definition) they get lost
when custom values are specified in the config.
"""
import numpy as np
from pypsa.networkclustering import _make_consense
bus_strategies = dict(country=_make_consense("Bus", "country"))
bus_strategies.update(aggregation_strategies.get("buses", {}))
generator_strategies = {"build_year": lambda x: 0, "lifetime": lambda x: np.inf}
generator_strategies.update(aggregation_strategies.get("generators", {}))
return bus_strategies, generator_strategies
def mock_snakemake(rulename, **wildcards):
"""
This function is expected to be executed from the "scripts"-directory of "
the snakemake project. It returns a snakemake.script.Snakemake object,
based on the Snakefile.
If a rule has wildcards, you have to specify them in **wildcards.
Parameters
----------
rulename: str
name of the rule for which the snakemake object should be generated
**wildcards:
keyword arguments fixing the wildcards. Only necessary if wildcards are
needed.
"""
import os
import snakemake as sm
from pypsa.descriptors import Dict
from snakemake.script import Snakemake
script_dir = Path(__file__).parent.resolve()
assert (
Path.cwd().resolve() == script_dir
), f"mock_snakemake has to be run from the repository scripts directory {script_dir}"
os.chdir(script_dir.parent)
for p in sm.SNAKEFILE_CHOICES:
if os.path.exists(p):
snakefile = p
break
workflow = sm.Workflow(snakefile, overwrite_configfiles=[], rerun_triggers=[])
workflow.include(snakefile)
workflow.global_resources = {}
try:
rule = workflow.get_rule(rulename)
except Exception as exception:
print(
exception,
f"The {rulename} might be a conditional rule in the Snakefile.\n"
f"Did you enable {rulename} in the config?",
)
raise
dag = sm.dag.DAG(workflow, rules=[rule])
wc = Dict(wildcards)
job = sm.jobs.Job(rule, dag, wc)
def make_accessable(*ios):
for io in ios:
for i in range(len(io)):
io[i] = os.path.abspath(io[i])
make_accessable(job.input, job.output, job.log)
snakemake = Snakemake(
job.input,
job.output,
job.params,
job.wildcards,
job.threads,
job.resources,
job.log,
job.dag.workflow.config,
job.rule.name,
None,
)
snakemake.benchmark = job.benchmark
# create log and output dir if not existent
for path in list(snakemake.log) + list(snakemake.output):
Path(path).parent.mkdir(parents=True, exist_ok=True)
os.chdir(script_dir)
return snakemake
def getContinent(code):
"""
Returns continent names that contains list of iso-code countries
Parameters
----------
code : str
List of two letter country ISO codes
Returns
-------
continent_list : str
List of continent names
Example
-------
from helpers import getContinent
code = ["DE", "GB", "NG", "ZA"]
getContinent(code)
>>> ["africa", "europe"]
"""
from config_osm_data import world_iso
continent_list = []
code_set = set(code)
for continent in world_iso:
single_continent_set = set(world_iso[continent])
if code_set.intersection(single_continent_set):
continent_list.append(continent)
return continent_list
def two_2_three_digits_country(two_code_country):
"""
Convert 2-digit to 3-digit country code:
Parameters
----------
two_code_country: str
2-digit country name
Returns
----------
three_code_country: str
3-digit country name
"""
if two_code_country == "SN-GM":
return f"{two_2_three_digits_country('SN')}-{two_2_three_digits_country('GM')}"
three_code_country = coco.convert(two_code_country, to="ISO3")
return three_code_country
def three_2_two_digits_country(three_code_country):
"""
Convert 3-digit to 2-digit country code:
Parameters
----------
three_code_country: str
3-digit country name
Returns
----------
two_code_country: str
2-digit country name
"""
if three_code_country == "SEN-GMB":
return f"{three_2_two_digits_country('SN')}-{three_2_two_digits_country('GM')}"
two_code_country = coco.convert(three_code_country, to="ISO2")
return 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
----------
full_name: str
full country name
"""
if two_code_country == "SN-GM":
return f"{two_digits_2_name_country('SN')}-{two_digits_2_name_country('GM')}"
full_name = coco.convert(two_code_country, to="name_short")
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
def country_name_2_two_digits(country_name):
"""
Convert full country name to 2-digit country code
Parameters
----------
country_name: str
country name
Returns
----------
two_code_country: str
2-digit country name
"""
if (
country_name
== f"{two_digits_2_name_country('SN')}-{two_digits_2_name_country('GM')}"
):
return "SN-GM"
full_name = coco.convert(country_name, to="ISO2")
return full_name
NA_VALUES = ["NULL"]
def read_csv_nafix(file, **kwargs):
"Function to open a csv as pandas file and standardize the na value"
if "keep_default_na" in kwargs:
del kwargs["keep_default_na"]
if "na_values" in kwargs:
del kwargs["na_values"]
if os.stat(file).st_size > 0:
return pd.read_csv(file, **kwargs, keep_default_na=False, na_values=NA_VALUES)
else:
return pd.DataFrame()
def to_csv_nafix(df, path, **kwargs):
if "na_rep" in kwargs:
del kwargs["na_rep"]
# if len(df) > 0:
if not df.empty or not df.columns.empty:
return df.to_csv(path, **kwargs, na_rep=NA_VALUES[0])
else:
with open(path, "w") as fp:
pass
def save_to_geojson(df, fn):
if os.path.exists(fn):
os.unlink(fn) # remove file if it exists
# save file if the (Geo)DataFrame is non-empty
if df.empty:
# create empty file to avoid issues with snakemake
with open(fn, "w") as fp:
pass
else:
# save file
df.to_file(fn, driver="GeoJSON")
def read_geojson(fn):
# if the file is non-zero, read the geodataframe and return it
if os.path.getsize(fn) > 0:
return gpd.read_file(fn)
else:
# else return an empty GeoDataFrame
return gpd.GeoDataFrame(geometry=[])
def create_country_list(input, iso_coding=True):
"""
Create a country list for defined regions in config_osm_data.py
Parameters
----------
input : str
Any two-letter country name, regional name, or continent given in config_osm_data.py
Country name duplications won't distort the result.
Examples are:
["NG","ZA"], downloading osm data for Nigeria and South Africa
["africa"], downloading data for Africa
["NAR"], downloading data for the North African Power Pool
["TEST"], downloading data for a customized test set.
["NG","ZA","NG"], won't distort result.
Returns
-------
full_codes_list : list
Example ["NG","ZA"]
"""
import logging
from config_osm_data import continent_regions, world_iso
_logger = logging.getLogger(__name__)
_logger.setLevel(logging.INFO)
def filter_codes(c_list, iso_coding=True):
"""
Filter list according to the specified coding.
When iso code are implemented (iso_coding=True), then remove the geofabrik-specific ones.
When geofabrik codes are selected(iso_coding=False), ignore iso-specific names.
"""
if (
iso_coding
): # if country lists are in iso coding, then check if they are 2-string
# 2-code countries
ret_list = [c for c in c_list if len(c) == 2]
# check if elements have been removed and return a working if so
if len(ret_list) < len(c_list):
_logger.warning(
"Specified country list contains the following non-iso codes: "
+ ", ".join(list(set(c_list) - set(ret_list)))
)
return ret_list
else:
return c_list # [c for c in c_list if c not in iso_to_geofk_dict]
full_codes_list = []
for value1 in input:
codes_list = []
# extract countries in world
if value1 == "Earth":
for continent in world_iso.keys():
codes_list.extend(list(world_iso[continent]))
# extract countries in continent
elif value1 in world_iso.keys():
codes_list = list(world_iso[value1])
# extract countries in regions
elif value1 in continent_regions.keys():
codes_list = continent_regions[value1]
# extract countries
else:
codes_list.extend([value1])
# create a list with all countries
full_codes_list.extend(codes_list)
# Removing duplicates and filter outputs by coding
full_codes_list = filter_codes(list(set(full_codes_list)), iso_coding=iso_coding)
return full_codes_list