| Total Complexity | 68 |
| Total Lines | 1627 |
| Duplicated Lines | 4.86 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like data.datasets.power_plants often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | """The central module containing all code dealing with the distribution and |
||
| 2 | allocation of data on conventional and renewable power plants. |
||
| 3 | """ |
||
| 4 | |||
| 5 | from pathlib import Path |
||
| 6 | import logging |
||
| 7 | |||
| 8 | from geoalchemy2 import Geometry |
||
| 9 | from shapely.geometry import Point |
||
| 10 | from sqlalchemy import BigInteger, Column, Float, Integer, Sequence, String |
||
| 11 | from sqlalchemy.dialects.postgresql import JSONB |
||
| 12 | from sqlalchemy.ext.declarative import declarative_base |
||
| 13 | from sqlalchemy.orm import sessionmaker |
||
| 14 | import geopandas as gpd |
||
| 15 | import numpy as np |
||
| 16 | import pandas as pd |
||
| 17 | |||
| 18 | from egon.data import db, logger |
||
| 19 | from egon.data.datasets import Dataset, wrapped_partial |
||
| 20 | from egon.data.datasets.mastr import ( |
||
| 21 | WORKING_DIR_MASTR_NEW, |
||
| 22 | WORKING_DIR_MASTR_OLD, |
||
| 23 | ) |
||
| 24 | from egon.data.datasets.power_plants.conventional import ( |
||
| 25 | match_nep_no_chp, |
||
| 26 | select_nep_power_plants, |
||
| 27 | select_no_chp_combustion_mastr, |
||
| 28 | ) |
||
| 29 | from egon.data.datasets.power_plants.mastr import ( |
||
| 30 | EgonPowerPlantsBiomass, |
||
| 31 | EgonPowerPlantsHydro, |
||
| 32 | EgonPowerPlantsPv, |
||
| 33 | EgonPowerPlantsWind, |
||
| 34 | import_mastr, |
||
| 35 | ) |
||
| 36 | from egon.data.datasets.power_plants.pv_rooftop import pv_rooftop_per_mv_grid |
||
| 37 | from egon.data.datasets.power_plants.pv_rooftop_buildings import ( |
||
| 38 | pv_rooftop_to_buildings, |
||
| 39 | ) |
||
| 40 | import egon.data.config |
||
| 41 | import egon.data.datasets.power_plants.assign_weather_data as assign_weather_data # noqa: E501 |
||
| 42 | import egon.data.datasets.power_plants.metadata as pp_metadata |
||
| 43 | import egon.data.datasets.power_plants.pv_ground_mounted as pv_ground_mounted |
||
| 44 | import egon.data.datasets.power_plants.wind_farms as wind_onshore |
||
| 45 | import egon.data.datasets.power_plants.wind_offshore as wind_offshore |
||
| 46 | |||
| 47 | Base = declarative_base() |
||
| 48 | |||
| 49 | |||
| 50 | View Code Duplication | class EgonPowerPlants(Base): |
|
|
|
|||
| 51 | __tablename__ = "egon_power_plants" |
||
| 52 | __table_args__ = {"schema": "supply"} |
||
| 53 | id = Column(BigInteger, Sequence("pp_seq"), primary_key=True) |
||
| 54 | sources = Column(JSONB) |
||
| 55 | source_id = Column(JSONB) |
||
| 56 | carrier = Column(String) |
||
| 57 | el_capacity = Column(Float) |
||
| 58 | bus_id = Column(Integer) |
||
| 59 | voltage_level = Column(Integer) |
||
| 60 | weather_cell_id = Column(Integer) |
||
| 61 | scenario = Column(String) |
||
| 62 | geom = Column(Geometry("POINT", 4326), index=True) |
||
| 63 | |||
| 64 | |||
| 65 | def create_tables(): |
||
| 66 | """Create tables for power plant data |
||
| 67 | Returns |
||
| 68 | ------- |
||
| 69 | None. |
||
| 70 | """ |
||
| 71 | |||
| 72 | # Tables for future scenarios |
||
| 73 | cfg = egon.data.config.datasets()["power_plants"] |
||
| 74 | db.execute_sql(f"CREATE SCHEMA IF NOT EXISTS {cfg['target']['schema']};") |
||
| 75 | engine = db.engine() |
||
| 76 | db.execute_sql( |
||
| 77 | f"""DROP TABLE IF EXISTS |
||
| 78 | {cfg['target']['schema']}.{cfg['target']['table']}""" |
||
| 79 | ) |
||
| 80 | |||
| 81 | db.execute_sql("""DROP SEQUENCE IF EXISTS pp_seq""") |
||
| 82 | EgonPowerPlants.__table__.create(bind=engine, checkfirst=True) |
||
| 83 | |||
| 84 | # Tables for status quo |
||
| 85 | tables = [ |
||
| 86 | EgonPowerPlantsWind, |
||
| 87 | EgonPowerPlantsPv, |
||
| 88 | EgonPowerPlantsBiomass, |
||
| 89 | EgonPowerPlantsHydro, |
||
| 90 | ] |
||
| 91 | for t in tables: |
||
| 92 | db.execute_sql( |
||
| 93 | f""" |
||
| 94 | DROP TABLE IF EXISTS {t.__table_args__['schema']}. |
||
| 95 | {t.__tablename__} CASCADE; |
||
| 96 | """ |
||
| 97 | ) |
||
| 98 | t.__table__.create(bind=engine, checkfirst=True) |
||
| 99 | |||
| 100 | |||
| 101 | def scale_prox2now(df, target, level="federal_state"): |
||
| 102 | """Scale installed capacities linear to status quo power plants |
||
| 103 | |||
| 104 | Parameters |
||
| 105 | ---------- |
||
| 106 | df : pandas.DataFrame |
||
| 107 | Status Quo power plants |
||
| 108 | target : pandas.Series |
||
| 109 | Target values for future scenario |
||
| 110 | level : str, optional |
||
| 111 | Scale per 'federal_state' or 'country'. The default is 'federal_state'. |
||
| 112 | |||
| 113 | Returns |
||
| 114 | ------- |
||
| 115 | df : pandas.DataFrame |
||
| 116 | Future power plants |
||
| 117 | |||
| 118 | """ |
||
| 119 | if level == "federal_state": |
||
| 120 | df.loc[:, "Nettonennleistung"] = ( |
||
| 121 | ( |
||
| 122 | df.groupby(df.Bundesland) |
||
| 123 | .Nettonennleistung.apply(lambda grp: grp / grp.sum()) |
||
| 124 | .mul(target[df.Bundesland.values].values) |
||
| 125 | ) |
||
| 126 | .reset_index(level=[0]) |
||
| 127 | .Nettonennleistung |
||
| 128 | ) |
||
| 129 | else: |
||
| 130 | df.loc[:, "Nettonennleistung"] = df.Nettonennleistung * ( |
||
| 131 | target / df.Nettonennleistung.sum() |
||
| 132 | ) |
||
| 133 | |||
| 134 | df = df[df.Nettonennleistung > 0] |
||
| 135 | |||
| 136 | return df |
||
| 137 | |||
| 138 | |||
| 139 | def select_target(carrier, scenario): |
||
| 140 | """Select installed capacity per scenario and carrier |
||
| 141 | |||
| 142 | Parameters |
||
| 143 | ---------- |
||
| 144 | carrier : str |
||
| 145 | Name of energy carrier |
||
| 146 | scenario : str |
||
| 147 | Name of scenario |
||
| 148 | |||
| 149 | Returns |
||
| 150 | ------- |
||
| 151 | pandas.Series |
||
| 152 | Target values for carrier and scenario |
||
| 153 | |||
| 154 | """ |
||
| 155 | cfg = egon.data.config.datasets()["power_plants"] |
||
| 156 | |||
| 157 | return ( |
||
| 158 | pd.read_sql( |
||
| 159 | f"""SELECT DISTINCT ON (b.gen) |
||
| 160 | REPLACE(REPLACE(b.gen, '-', ''), 'ü', 'ue') as state, |
||
| 161 | a.capacity |
||
| 162 | FROM {cfg['sources']['capacities']} a, |
||
| 163 | {cfg['sources']['geom_federal_states']} b |
||
| 164 | WHERE a.nuts = b.nuts |
||
| 165 | AND scenario_name = '{scenario}' |
||
| 166 | AND carrier = '{carrier}' |
||
| 167 | AND b.gen NOT IN ('Baden-Württemberg (Bodensee)', |
||
| 168 | 'Bayern (Bodensee)')""", |
||
| 169 | con=db.engine(), |
||
| 170 | ) |
||
| 171 | .set_index("state") |
||
| 172 | .capacity |
||
| 173 | ) |
||
| 174 | |||
| 175 | |||
| 176 | def filter_mastr_geometry(mastr, federal_state=None): |
||
| 177 | """Filter data from MaStR by geometry |
||
| 178 | |||
| 179 | Parameters |
||
| 180 | ---------- |
||
| 181 | mastr : pandas.DataFrame |
||
| 182 | All power plants listed in MaStR |
||
| 183 | federal_state : str or None |
||
| 184 | Name of federal state whoes power plants are returned. |
||
| 185 | If None, data for Germany is returned |
||
| 186 | |||
| 187 | Returns |
||
| 188 | ------- |
||
| 189 | mastr_loc : pandas.DataFrame |
||
| 190 | Power plants listed in MaStR with geometry inside German boundaries |
||
| 191 | |||
| 192 | """ |
||
| 193 | cfg = egon.data.config.datasets()["power_plants"] |
||
| 194 | |||
| 195 | if type(mastr) == pd.core.frame.DataFrame: |
||
| 196 | # Drop entries without geometry for insert |
||
| 197 | mastr_loc = mastr[ |
||
| 198 | mastr.Laengengrad.notnull() & mastr.Breitengrad.notnull() |
||
| 199 | ] |
||
| 200 | |||
| 201 | # Create geodataframe |
||
| 202 | mastr_loc = gpd.GeoDataFrame( |
||
| 203 | mastr_loc, |
||
| 204 | geometry=gpd.points_from_xy( |
||
| 205 | mastr_loc.Laengengrad, mastr_loc.Breitengrad, crs=4326 |
||
| 206 | ), |
||
| 207 | ) |
||
| 208 | else: |
||
| 209 | mastr_loc = mastr.copy() |
||
| 210 | |||
| 211 | # Drop entries outside of germany or federal state |
||
| 212 | if not federal_state: |
||
| 213 | sql = f"SELECT geometry as geom FROM {cfg['sources']['geom_germany']}" |
||
| 214 | else: |
||
| 215 | sql = f""" |
||
| 216 | SELECT geometry as geom |
||
| 217 | FROM boundaries.vg250_lan_union |
||
| 218 | WHERE REPLACE(REPLACE(gen, '-', ''), 'ü', 'ue') = '{federal_state}'""" |
||
| 219 | |||
| 220 | mastr_loc = ( |
||
| 221 | gpd.sjoin( |
||
| 222 | gpd.read_postgis(sql, con=db.engine()).to_crs(4326), |
||
| 223 | mastr_loc, |
||
| 224 | how="right", |
||
| 225 | ) |
||
| 226 | .query("index_left==0") |
||
| 227 | .drop("index_left", axis=1) |
||
| 228 | ) |
||
| 229 | |||
| 230 | return mastr_loc |
||
| 231 | |||
| 232 | |||
| 233 | def insert_biomass_plants(scenario): |
||
| 234 | """Insert biomass power plants of future scenario |
||
| 235 | |||
| 236 | Parameters |
||
| 237 | ---------- |
||
| 238 | scenario : str |
||
| 239 | Name of scenario. |
||
| 240 | |||
| 241 | Returns |
||
| 242 | ------- |
||
| 243 | None. |
||
| 244 | |||
| 245 | """ |
||
| 246 | cfg = egon.data.config.datasets()["power_plants"] |
||
| 247 | |||
| 248 | # import target values |
||
| 249 | target = select_target("biomass", scenario) |
||
| 250 | |||
| 251 | # import data for MaStR |
||
| 252 | mastr = pd.read_csv( |
||
| 253 | WORKING_DIR_MASTR_OLD / cfg["sources"]["mastr_biomass"] |
||
| 254 | ).query("EinheitBetriebsstatus=='InBetrieb'") |
||
| 255 | |||
| 256 | # Drop entries without federal state or 'AusschließlichWirtschaftszone' |
||
| 257 | mastr = mastr[ |
||
| 258 | mastr.Bundesland.isin( |
||
| 259 | pd.read_sql( |
||
| 260 | f"""SELECT DISTINCT ON (gen) |
||
| 261 | REPLACE(REPLACE(gen, '-', ''), 'ü', 'ue') as states |
||
| 262 | FROM {cfg['sources']['geom_federal_states']}""", |
||
| 263 | con=db.engine(), |
||
| 264 | ).states.values |
||
| 265 | ) |
||
| 266 | ] |
||
| 267 | |||
| 268 | # Scaling will be done per federal state in case of eGon2035 scenario. |
||
| 269 | if scenario == "eGon2035": |
||
| 270 | level = "federal_state" |
||
| 271 | else: |
||
| 272 | level = "country" |
||
| 273 | |||
| 274 | # Choose only entries with valid geometries inside DE/test mode |
||
| 275 | mastr_loc = filter_mastr_geometry(mastr).set_geometry("geometry") |
||
| 276 | |||
| 277 | # Scale capacities to meet target values |
||
| 278 | mastr_loc = scale_prox2now(mastr_loc, target, level=level) |
||
| 279 | |||
| 280 | # Assign bus_id |
||
| 281 | if len(mastr_loc) > 0: |
||
| 282 | mastr_loc["voltage_level"] = assign_voltage_level( |
||
| 283 | mastr_loc, cfg, WORKING_DIR_MASTR_OLD |
||
| 284 | ) |
||
| 285 | mastr_loc = assign_bus_id(mastr_loc, cfg) |
||
| 286 | |||
| 287 | # Insert entries with location |
||
| 288 | session = sessionmaker(bind=db.engine())() |
||
| 289 | |||
| 290 | for i, row in mastr_loc.iterrows(): |
||
| 291 | if not row.ThermischeNutzleistung > 0: |
||
| 292 | entry = EgonPowerPlants( |
||
| 293 | sources={"el_capacity": "MaStR scaled with NEP 2021"}, |
||
| 294 | source_id={"MastrNummer": row.EinheitMastrNummer}, |
||
| 295 | carrier="biomass", |
||
| 296 | el_capacity=row.Nettonennleistung, |
||
| 297 | scenario=scenario, |
||
| 298 | bus_id=row.bus_id, |
||
| 299 | voltage_level=row.voltage_level, |
||
| 300 | geom=f"SRID=4326;POINT({row.Laengengrad} {row.Breitengrad})", |
||
| 301 | ) |
||
| 302 | session.add(entry) |
||
| 303 | |||
| 304 | session.commit() |
||
| 305 | |||
| 306 | |||
| 307 | def insert_hydro_plants(scenario): |
||
| 308 | """Insert hydro power plants of future scenario. |
||
| 309 | |||
| 310 | Hydro power plants are diveded into run_of_river and reservoir plants |
||
| 311 | according to Marktstammdatenregister. |
||
| 312 | Additional hydro technologies (e.g. turbines inside drinking water |
||
| 313 | systems) are not considered. |
||
| 314 | |||
| 315 | Parameters |
||
| 316 | ---------- |
||
| 317 | scenario : str |
||
| 318 | Name of scenario. |
||
| 319 | |||
| 320 | Returns |
||
| 321 | ------- |
||
| 322 | None. |
||
| 323 | |||
| 324 | """ |
||
| 325 | cfg = egon.data.config.datasets()["power_plants"] |
||
| 326 | |||
| 327 | # Map MaStR carriers to eGon carriers |
||
| 328 | map_carrier = { |
||
| 329 | "run_of_river": ["Laufwasseranlage"], |
||
| 330 | "reservoir": ["Speicherwasseranlage"], |
||
| 331 | } |
||
| 332 | |||
| 333 | for carrier in map_carrier.keys(): |
||
| 334 | # import target values |
||
| 335 | if scenario == "eGon100RE": |
||
| 336 | try: |
||
| 337 | target = pd.read_sql( |
||
| 338 | f"""SELECT capacity FROM supply.egon_scenario_capacities |
||
| 339 | WHERE scenario_name = '{scenario}' |
||
| 340 | AND carrier = '{carrier}' |
||
| 341 | """, |
||
| 342 | con=db.engine(), |
||
| 343 | ).capacity[0] |
||
| 344 | except: |
||
| 345 | logger.info( |
||
| 346 | f"No assigned capacity for {carrier} in {scenario}" |
||
| 347 | ) |
||
| 348 | continue |
||
| 349 | |||
| 350 | elif scenario == "eGon2035": |
||
| 351 | target = select_target(carrier, scenario) |
||
| 352 | |||
| 353 | # import data for MaStR |
||
| 354 | mastr = pd.read_csv( |
||
| 355 | WORKING_DIR_MASTR_NEW / cfg["sources"]["mastr_hydro"] |
||
| 356 | ).query("EinheitBetriebsstatus=='InBetrieb'") |
||
| 357 | |||
| 358 | # Choose only plants with specific carriers |
||
| 359 | mastr = mastr[mastr.ArtDerWasserkraftanlage.isin(map_carrier[carrier])] |
||
| 360 | |||
| 361 | # Drop entries without federal state or 'AusschließlichWirtschaftszone' |
||
| 362 | mastr = mastr[ |
||
| 363 | mastr.Bundesland.isin( |
||
| 364 | pd.read_sql( |
||
| 365 | f"""SELECT DISTINCT ON (gen) |
||
| 366 | REPLACE(REPLACE(gen, '-', ''), 'ü', 'ue') as states |
||
| 367 | FROM {cfg['sources']['geom_federal_states']}""", |
||
| 368 | con=db.engine(), |
||
| 369 | ).states.values |
||
| 370 | ) |
||
| 371 | ] |
||
| 372 | |||
| 373 | # Scaling will be done per federal state in case of eGon2035 scenario. |
||
| 374 | if scenario == "eGon2035": |
||
| 375 | level = "federal_state" |
||
| 376 | else: |
||
| 377 | level = "country" |
||
| 378 | |||
| 379 | # Scale capacities to meet target values |
||
| 380 | mastr = scale_prox2now(mastr, target, level=level) |
||
| 381 | |||
| 382 | # Choose only entries with valid geometries inside DE/test mode |
||
| 383 | mastr_loc = filter_mastr_geometry(mastr).set_geometry("geometry") |
||
| 384 | # TODO: Deal with power plants without geometry |
||
| 385 | |||
| 386 | # Assign bus_id and voltage level |
||
| 387 | if len(mastr_loc) > 0: |
||
| 388 | mastr_loc["voltage_level"] = assign_voltage_level( |
||
| 389 | mastr_loc, cfg, WORKING_DIR_MASTR_NEW |
||
| 390 | ) |
||
| 391 | mastr_loc = assign_bus_id(mastr_loc, cfg) |
||
| 392 | |||
| 393 | # Insert entries with location |
||
| 394 | session = sessionmaker(bind=db.engine())() |
||
| 395 | for i, row in mastr_loc.iterrows(): |
||
| 396 | entry = EgonPowerPlants( |
||
| 397 | sources={"el_capacity": "MaStR scaled with NEP 2021"}, |
||
| 398 | source_id={"MastrNummer": row.EinheitMastrNummer}, |
||
| 399 | carrier=carrier, |
||
| 400 | el_capacity=row.Nettonennleistung, |
||
| 401 | scenario=scenario, |
||
| 402 | bus_id=row.bus_id, |
||
| 403 | voltage_level=row.voltage_level, |
||
| 404 | geom=f"SRID=4326;POINT({row.Laengengrad} {row.Breitengrad})", |
||
| 405 | ) |
||
| 406 | session.add(entry) |
||
| 407 | |||
| 408 | session.commit() |
||
| 409 | |||
| 410 | |||
| 411 | def assign_voltage_level(mastr_loc, cfg, mastr_working_dir): |
||
| 412 | """Assigns voltage level to power plants. |
||
| 413 | |||
| 414 | If location data inluding voltage level is available from |
||
| 415 | Marktstammdatenregister, this is used. Otherwise the voltage level is |
||
| 416 | assigned according to the electrical capacity. |
||
| 417 | |||
| 418 | Parameters |
||
| 419 | ---------- |
||
| 420 | mastr_loc : pandas.DataFrame |
||
| 421 | Power plants listed in MaStR with geometry inside German boundaries |
||
| 422 | |||
| 423 | Returns |
||
| 424 | ------- |
||
| 425 | pandas.DataFrame |
||
| 426 | Power plants including voltage_level |
||
| 427 | |||
| 428 | """ |
||
| 429 | mastr_loc["Spannungsebene"] = np.nan |
||
| 430 | mastr_loc["voltage_level"] = np.nan |
||
| 431 | |||
| 432 | if "LokationMastrNummer" in mastr_loc.columns: |
||
| 433 | # Adjust column names to format of MaStR location dataset |
||
| 434 | if mastr_working_dir == WORKING_DIR_MASTR_OLD: |
||
| 435 | cols = ["LokationMastrNummer", "Spannungsebene"] |
||
| 436 | elif mastr_working_dir == WORKING_DIR_MASTR_NEW: |
||
| 437 | cols = ["MaStRNummer", "Spannungsebene"] |
||
| 438 | else: |
||
| 439 | raise ValueError("Invalid MaStR working directory!") |
||
| 440 | |||
| 441 | location = ( |
||
| 442 | pd.read_csv( |
||
| 443 | mastr_working_dir / cfg["sources"]["mastr_location"], |
||
| 444 | usecols=cols, |
||
| 445 | ) |
||
| 446 | .rename(columns={"MaStRNummer": "LokationMastrNummer"}) |
||
| 447 | .set_index("LokationMastrNummer") |
||
| 448 | ) |
||
| 449 | |||
| 450 | location = location[~location.index.duplicated(keep="first")] |
||
| 451 | |||
| 452 | mastr_loc.loc[ |
||
| 453 | mastr_loc[ |
||
| 454 | mastr_loc.LokationMastrNummer.isin(location.index) |
||
| 455 | ].index, |
||
| 456 | "Spannungsebene", |
||
| 457 | ] = location.Spannungsebene[ |
||
| 458 | mastr_loc[ |
||
| 459 | mastr_loc.LokationMastrNummer.isin(location.index) |
||
| 460 | ].LokationMastrNummer |
||
| 461 | ].values |
||
| 462 | |||
| 463 | # Transfer voltage_level as integer from Spanungsebene |
||
| 464 | map_voltage_levels = pd.Series( |
||
| 465 | data={ |
||
| 466 | "Höchstspannung": 1, |
||
| 467 | "Hoechstspannung": 1, |
||
| 468 | "UmspannungZurHochspannung": 2, |
||
| 469 | "Hochspannung": 3, |
||
| 470 | "UmspannungZurMittelspannung": 4, |
||
| 471 | "Mittelspannung": 5, |
||
| 472 | "UmspannungZurNiederspannung": 6, |
||
| 473 | "Niederspannung": 7, |
||
| 474 | } |
||
| 475 | ) |
||
| 476 | |||
| 477 | mastr_loc.loc[ |
||
| 478 | mastr_loc[mastr_loc["Spannungsebene"].notnull()].index, |
||
| 479 | "voltage_level", |
||
| 480 | ] = map_voltage_levels[ |
||
| 481 | mastr_loc.loc[ |
||
| 482 | mastr_loc[mastr_loc["Spannungsebene"].notnull()].index, |
||
| 483 | "Spannungsebene", |
||
| 484 | ].values |
||
| 485 | ].values |
||
| 486 | |||
| 487 | else: |
||
| 488 | print( |
||
| 489 | "No information about MaStR location available. " |
||
| 490 | "All voltage levels are assigned using threshold values." |
||
| 491 | ) |
||
| 492 | |||
| 493 | # If no voltage level is available from mastr, choose level according |
||
| 494 | # to threshold values |
||
| 495 | |||
| 496 | mastr_loc.voltage_level = assign_voltage_level_by_capacity(mastr_loc) |
||
| 497 | |||
| 498 | return mastr_loc.voltage_level |
||
| 499 | |||
| 500 | |||
| 501 | def assign_voltage_level_by_capacity(mastr_loc): |
||
| 502 | |||
| 503 | for i, row in mastr_loc[mastr_loc.voltage_level.isnull()].iterrows(): |
||
| 504 | |||
| 505 | if row.Nettonennleistung > 120: |
||
| 506 | level = 1 |
||
| 507 | elif row.Nettonennleistung > 20: |
||
| 508 | level = 3 |
||
| 509 | elif row.Nettonennleistung > 5.5: |
||
| 510 | level = 4 |
||
| 511 | elif row.Nettonennleistung > 0.2: |
||
| 512 | level = 5 |
||
| 513 | elif row.Nettonennleistung > 0.1: |
||
| 514 | level = 6 |
||
| 515 | else: |
||
| 516 | level = 7 |
||
| 517 | |||
| 518 | mastr_loc.loc[i, "voltage_level"] = level |
||
| 519 | |||
| 520 | mastr_loc.voltage_level = mastr_loc.voltage_level.astype(int) |
||
| 521 | |||
| 522 | return mastr_loc.voltage_level |
||
| 523 | |||
| 524 | |||
| 525 | View Code Duplication | def assign_bus_id(power_plants, cfg, drop_missing=False): |
|
| 526 | """Assigns bus_ids to power plants according to location and voltage level |
||
| 527 | |||
| 528 | Parameters |
||
| 529 | ---------- |
||
| 530 | power_plants : pandas.DataFrame |
||
| 531 | Power plants including voltage level |
||
| 532 | |||
| 533 | Returns |
||
| 534 | ------- |
||
| 535 | power_plants : pandas.DataFrame |
||
| 536 | Power plants including voltage level and bus_id |
||
| 537 | |||
| 538 | """ |
||
| 539 | |||
| 540 | mv_grid_districts = db.select_geodataframe( |
||
| 541 | f""" |
||
| 542 | SELECT * FROM {cfg['sources']['egon_mv_grid_district']} |
||
| 543 | """, |
||
| 544 | epsg=4326, |
||
| 545 | ) |
||
| 546 | |||
| 547 | ehv_grid_districts = db.select_geodataframe( |
||
| 548 | f""" |
||
| 549 | SELECT * FROM {cfg['sources']['ehv_voronoi']} |
||
| 550 | """, |
||
| 551 | epsg=4326, |
||
| 552 | ) |
||
| 553 | |||
| 554 | # Assign power plants in hv and below to hvmv bus |
||
| 555 | power_plants_hv = power_plants[power_plants.voltage_level >= 3].index |
||
| 556 | if len(power_plants_hv) > 0: |
||
| 557 | power_plants.loc[power_plants_hv, "bus_id"] = gpd.sjoin( |
||
| 558 | power_plants[power_plants.index.isin(power_plants_hv)], |
||
| 559 | mv_grid_districts, |
||
| 560 | ).bus_id |
||
| 561 | |||
| 562 | # Assign power plants in ehv to ehv bus |
||
| 563 | power_plants_ehv = power_plants[power_plants.voltage_level < 3].index |
||
| 564 | |||
| 565 | if len(power_plants_ehv) > 0: |
||
| 566 | ehv_join = gpd.sjoin( |
||
| 567 | power_plants[power_plants.index.isin(power_plants_ehv)], |
||
| 568 | ehv_grid_districts, |
||
| 569 | ) |
||
| 570 | |||
| 571 | if "bus_id_right" in ehv_join.columns: |
||
| 572 | power_plants.loc[power_plants_ehv, "bus_id"] = gpd.sjoin( |
||
| 573 | power_plants[power_plants.index.isin(power_plants_ehv)], |
||
| 574 | ehv_grid_districts, |
||
| 575 | ).bus_id_right |
||
| 576 | |||
| 577 | else: |
||
| 578 | power_plants.loc[power_plants_ehv, "bus_id"] = gpd.sjoin( |
||
| 579 | power_plants[power_plants.index.isin(power_plants_ehv)], |
||
| 580 | ehv_grid_districts, |
||
| 581 | ).bus_id |
||
| 582 | |||
| 583 | if drop_missing: |
||
| 584 | power_plants = power_plants[~power_plants.bus_id.isnull()] |
||
| 585 | |||
| 586 | # Assert that all power plants have a bus_id |
||
| 587 | assert power_plants.bus_id.notnull().all(), f"""Some power plants are |
||
| 588 | not attached to a bus: {power_plants[power_plants.bus_id.isnull()]}""" |
||
| 589 | |||
| 590 | return power_plants |
||
| 591 | |||
| 592 | |||
| 593 | def insert_hydro_biomass(): |
||
| 594 | """Insert hydro and biomass power plants in database |
||
| 595 | |||
| 596 | Returns |
||
| 597 | ------- |
||
| 598 | None. |
||
| 599 | |||
| 600 | """ |
||
| 601 | cfg = egon.data.config.datasets()["power_plants"] |
||
| 602 | db.execute_sql( |
||
| 603 | f""" |
||
| 604 | DELETE FROM {cfg['target']['schema']}.{cfg['target']['table']} |
||
| 605 | WHERE carrier IN ('biomass', 'reservoir', 'run_of_river') |
||
| 606 | AND scenario IN ('eGon2035', 'eGon100RE') |
||
| 607 | """ |
||
| 608 | ) |
||
| 609 | |||
| 610 | s = egon.data.config.settings()["egon-data"]["--scenarios"] |
||
| 611 | scenarios = [] |
||
| 612 | if "eGon2035" in s: |
||
| 613 | scenarios.append("eGon2035") |
||
| 614 | insert_biomass_plants("eGon2035") |
||
| 615 | if "eGon100RE" in s: |
||
| 616 | scenarios.append("eGon100RE") |
||
| 617 | |||
| 618 | for scenario in scenarios: |
||
| 619 | insert_hydro_plants(scenario) |
||
| 620 | |||
| 621 | |||
| 622 | def allocate_conventional_non_chp_power_plants(): |
||
| 623 | """Allocate conventional power plants without CHPs based on the NEP target |
||
| 624 | values and data from power plant registry (MaStR) by assigning them in a |
||
| 625 | cascaded manner. |
||
| 626 | |||
| 627 | Returns |
||
| 628 | ------- |
||
| 629 | None. |
||
| 630 | |||
| 631 | """ |
||
| 632 | # This function is only designed to work for the eGon2035 scenario |
||
| 633 | if ( |
||
| 634 | "eGon2035" |
||
| 635 | not in egon.data.config.settings()["egon-data"]["--scenarios"] |
||
| 636 | ): |
||
| 637 | return |
||
| 638 | |||
| 639 | carrier = ["oil", "gas"] |
||
| 640 | |||
| 641 | cfg = egon.data.config.datasets()["power_plants"] |
||
| 642 | |||
| 643 | # Delete existing plants in the target table |
||
| 644 | db.execute_sql( |
||
| 645 | f""" |
||
| 646 | DELETE FROM {cfg ['target']['schema']}.{cfg ['target']['table']} |
||
| 647 | WHERE carrier IN ('gas', 'oil') |
||
| 648 | AND scenario='eGon2035'; |
||
| 649 | """ |
||
| 650 | ) |
||
| 651 | |||
| 652 | for carrier in carrier: |
||
| 653 | |||
| 654 | nep = select_nep_power_plants(carrier) |
||
| 655 | |||
| 656 | if nep.empty: |
||
| 657 | print(f"DataFrame from NEP for carrier {carrier} is empty!") |
||
| 658 | |||
| 659 | else: |
||
| 660 | |||
| 661 | mastr = select_no_chp_combustion_mastr(carrier) |
||
| 662 | |||
| 663 | # Assign voltage level to MaStR |
||
| 664 | mastr["voltage_level"] = assign_voltage_level( |
||
| 665 | mastr.rename({"el_capacity": "Nettonennleistung"}, axis=1), |
||
| 666 | cfg, |
||
| 667 | WORKING_DIR_MASTR_OLD, |
||
| 668 | ) |
||
| 669 | |||
| 670 | # Initalize DataFrame for matching power plants |
||
| 671 | matched = gpd.GeoDataFrame( |
||
| 672 | columns=[ |
||
| 673 | "carrier", |
||
| 674 | "el_capacity", |
||
| 675 | "scenario", |
||
| 676 | "geometry", |
||
| 677 | "MaStRNummer", |
||
| 678 | "source", |
||
| 679 | "voltage_level", |
||
| 680 | ] |
||
| 681 | ) |
||
| 682 | |||
| 683 | # Match combustion plants of a certain carrier from NEP list |
||
| 684 | # using PLZ and capacity |
||
| 685 | matched, mastr, nep = match_nep_no_chp( |
||
| 686 | nep, |
||
| 687 | mastr, |
||
| 688 | matched, |
||
| 689 | buffer_capacity=0.1, |
||
| 690 | consider_carrier=False, |
||
| 691 | ) |
||
| 692 | |||
| 693 | # Match plants from NEP list using city and capacity |
||
| 694 | matched, mastr, nep = match_nep_no_chp( |
||
| 695 | nep, |
||
| 696 | mastr, |
||
| 697 | matched, |
||
| 698 | buffer_capacity=0.1, |
||
| 699 | consider_carrier=False, |
||
| 700 | consider_location="city", |
||
| 701 | ) |
||
| 702 | |||
| 703 | # Match plants from NEP list using plz, |
||
| 704 | # neglecting the capacity |
||
| 705 | matched, mastr, nep = match_nep_no_chp( |
||
| 706 | nep, |
||
| 707 | mastr, |
||
| 708 | matched, |
||
| 709 | consider_location="plz", |
||
| 710 | consider_carrier=False, |
||
| 711 | consider_capacity=False, |
||
| 712 | ) |
||
| 713 | |||
| 714 | # Match plants from NEP list using city, |
||
| 715 | # neglecting the capacity |
||
| 716 | matched, mastr, nep = match_nep_no_chp( |
||
| 717 | nep, |
||
| 718 | mastr, |
||
| 719 | matched, |
||
| 720 | consider_location="city", |
||
| 721 | consider_carrier=False, |
||
| 722 | consider_capacity=False, |
||
| 723 | ) |
||
| 724 | |||
| 725 | # Match remaining plants from NEP using the federal state |
||
| 726 | matched, mastr, nep = match_nep_no_chp( |
||
| 727 | nep, |
||
| 728 | mastr, |
||
| 729 | matched, |
||
| 730 | buffer_capacity=0.1, |
||
| 731 | consider_location="federal_state", |
||
| 732 | consider_carrier=False, |
||
| 733 | ) |
||
| 734 | |||
| 735 | # Match remaining plants from NEP using the federal state |
||
| 736 | matched, mastr, nep = match_nep_no_chp( |
||
| 737 | nep, |
||
| 738 | mastr, |
||
| 739 | matched, |
||
| 740 | buffer_capacity=0.7, |
||
| 741 | consider_location="federal_state", |
||
| 742 | consider_carrier=False, |
||
| 743 | ) |
||
| 744 | |||
| 745 | print(f"{matched.el_capacity.sum()} MW of {carrier} matched") |
||
| 746 | print(f"{nep.c2035_capacity.sum()} MW of {carrier} not matched") |
||
| 747 | |||
| 748 | matched.crs = "EPSG:4326" |
||
| 749 | |||
| 750 | # Assign bus_id |
||
| 751 | # Load grid district polygons |
||
| 752 | mv_grid_districts = db.select_geodataframe( |
||
| 753 | f""" |
||
| 754 | SELECT * FROM {cfg['sources']['egon_mv_grid_district']} |
||
| 755 | """, |
||
| 756 | epsg=4326, |
||
| 757 | ) |
||
| 758 | |||
| 759 | ehv_grid_districts = db.select_geodataframe( |
||
| 760 | f""" |
||
| 761 | SELECT * FROM {cfg['sources']['ehv_voronoi']} |
||
| 762 | """, |
||
| 763 | epsg=4326, |
||
| 764 | ) |
||
| 765 | |||
| 766 | # Perform spatial joins for plants in ehv and hv level seperately |
||
| 767 | power_plants_hv = gpd.sjoin( |
||
| 768 | matched[matched.voltage_level >= 3], |
||
| 769 | mv_grid_districts[["bus_id", "geom"]], |
||
| 770 | how="left", |
||
| 771 | ).drop(columns=["index_right"]) |
||
| 772 | power_plants_ehv = gpd.sjoin( |
||
| 773 | matched[matched.voltage_level < 3], |
||
| 774 | ehv_grid_districts[["bus_id", "geom"]], |
||
| 775 | how="left", |
||
| 776 | ).drop(columns=["index_right"]) |
||
| 777 | |||
| 778 | # Combine both dataframes |
||
| 779 | power_plants = pd.concat([power_plants_hv, power_plants_ehv]) |
||
| 780 | |||
| 781 | # Insert into target table |
||
| 782 | session = sessionmaker(bind=db.engine())() |
||
| 783 | for i, row in power_plants.iterrows(): |
||
| 784 | entry = EgonPowerPlants( |
||
| 785 | sources={"el_capacity": row.source}, |
||
| 786 | source_id={"MastrNummer": row.MaStRNummer}, |
||
| 787 | carrier=row.carrier, |
||
| 788 | el_capacity=row.el_capacity, |
||
| 789 | voltage_level=row.voltage_level, |
||
| 790 | bus_id=row.bus_id, |
||
| 791 | scenario=row.scenario, |
||
| 792 | geom=f"SRID=4326;POINT({row.geometry.x} {row.geometry.y})", |
||
| 793 | ) |
||
| 794 | session.add(entry) |
||
| 795 | session.commit() |
||
| 796 | |||
| 797 | |||
| 798 | def allocate_other_power_plants(): |
||
| 799 | # This function is only designed to work for the eGon2035 scenario |
||
| 800 | if ( |
||
| 801 | "eGon2035" |
||
| 802 | not in egon.data.config.settings()["egon-data"]["--scenarios"] |
||
| 803 | ): |
||
| 804 | return |
||
| 805 | |||
| 806 | # Get configuration |
||
| 807 | cfg = egon.data.config.datasets()["power_plants"] |
||
| 808 | boundary = egon.data.config.settings()["egon-data"]["--dataset-boundary"] |
||
| 809 | |||
| 810 | db.execute_sql( |
||
| 811 | f""" |
||
| 812 | DELETE FROM {cfg['target']['schema']}.{cfg['target']['table']} |
||
| 813 | WHERE carrier ='others' |
||
| 814 | """ |
||
| 815 | ) |
||
| 816 | |||
| 817 | # Define scenario, carrier 'others' is only present in 'eGon2035' |
||
| 818 | scenario = "eGon2035" |
||
| 819 | |||
| 820 | # Select target values for carrier 'others' |
||
| 821 | target = db.select_dataframe( |
||
| 822 | f""" |
||
| 823 | SELECT sum(capacity) as capacity, carrier, scenario_name, nuts |
||
| 824 | FROM {cfg['sources']['capacities']} |
||
| 825 | WHERE scenario_name = '{scenario}' |
||
| 826 | AND carrier = 'others' |
||
| 827 | GROUP BY carrier, nuts, scenario_name; |
||
| 828 | """ |
||
| 829 | ) |
||
| 830 | |||
| 831 | # Assign name of federal state |
||
| 832 | |||
| 833 | map_states = { |
||
| 834 | "DE1": "BadenWuerttemberg", |
||
| 835 | "DEA": "NordrheinWestfalen", |
||
| 836 | "DE7": "Hessen", |
||
| 837 | "DE4": "Brandenburg", |
||
| 838 | "DE5": "Bremen", |
||
| 839 | "DEB": "RheinlandPfalz", |
||
| 840 | "DEE": "SachsenAnhalt", |
||
| 841 | "DEF": "SchleswigHolstein", |
||
| 842 | "DE8": "MecklenburgVorpommern", |
||
| 843 | "DEG": "Thueringen", |
||
| 844 | "DE9": "Niedersachsen", |
||
| 845 | "DED": "Sachsen", |
||
| 846 | "DE6": "Hamburg", |
||
| 847 | "DEC": "Saarland", |
||
| 848 | "DE3": "Berlin", |
||
| 849 | "DE2": "Bayern", |
||
| 850 | } |
||
| 851 | |||
| 852 | target = ( |
||
| 853 | target.replace({"nuts": map_states}) |
||
| 854 | .rename(columns={"nuts": "Bundesland"}) |
||
| 855 | .set_index("Bundesland") |
||
| 856 | ) |
||
| 857 | target = target.capacity |
||
| 858 | |||
| 859 | # Select 'non chp' power plants from mastr table |
||
| 860 | mastr_combustion = select_no_chp_combustion_mastr("others") |
||
| 861 | |||
| 862 | # Rename columns |
||
| 863 | mastr_combustion = mastr_combustion.rename( |
||
| 864 | columns={ |
||
| 865 | "carrier": "Energietraeger", |
||
| 866 | "plz": "Postleitzahl", |
||
| 867 | "city": "Ort", |
||
| 868 | "federal_state": "Bundesland", |
||
| 869 | "el_capacity": "Nettonennleistung", |
||
| 870 | } |
||
| 871 | ) |
||
| 872 | |||
| 873 | # Select power plants representing carrier 'others' from MaStR files |
||
| 874 | mastr_sludge = pd.read_csv( |
||
| 875 | WORKING_DIR_MASTR_OLD / cfg["sources"]["mastr_gsgk"] |
||
| 876 | ).query( |
||
| 877 | """EinheitBetriebsstatus=='InBetrieb'and Energietraeger=='Klärschlamm'""" # noqa: E501 |
||
| 878 | ) |
||
| 879 | mastr_geothermal = pd.read_csv( |
||
| 880 | WORKING_DIR_MASTR_OLD / cfg["sources"]["mastr_gsgk"] |
||
| 881 | ).query( |
||
| 882 | "EinheitBetriebsstatus=='InBetrieb' and Energietraeger=='Geothermie' " |
||
| 883 | "and Technologie == 'ORCOrganicRankineCycleAnlage'" |
||
| 884 | ) |
||
| 885 | |||
| 886 | mastr_sg = pd.concat([mastr_sludge, mastr_geothermal]) |
||
| 887 | |||
| 888 | # Insert geometry column |
||
| 889 | mastr_sg = mastr_sg[~(mastr_sg["Laengengrad"].isnull())] |
||
| 890 | mastr_sg = gpd.GeoDataFrame( |
||
| 891 | mastr_sg, |
||
| 892 | geometry=gpd.points_from_xy( |
||
| 893 | mastr_sg["Laengengrad"], mastr_sg["Breitengrad"], crs=4326 |
||
| 894 | ), |
||
| 895 | ) |
||
| 896 | |||
| 897 | # Exclude columns which are not essential |
||
| 898 | mastr_sg = mastr_sg.filter( |
||
| 899 | [ |
||
| 900 | "EinheitMastrNummer", |
||
| 901 | "Nettonennleistung", |
||
| 902 | "geometry", |
||
| 903 | "Energietraeger", |
||
| 904 | "Postleitzahl", |
||
| 905 | "Ort", |
||
| 906 | "Bundesland", |
||
| 907 | ], |
||
| 908 | axis=1, |
||
| 909 | ) |
||
| 910 | # Rename carrier |
||
| 911 | mastr_sg.Energietraeger = "others" |
||
| 912 | |||
| 913 | # Change data type |
||
| 914 | mastr_sg["Postleitzahl"] = mastr_sg["Postleitzahl"].astype(int) |
||
| 915 | |||
| 916 | # Capacity in MW |
||
| 917 | mastr_sg.loc[:, "Nettonennleistung"] *= 1e-3 |
||
| 918 | |||
| 919 | # Merge different sources to one df |
||
| 920 | mastr_others = pd.concat([mastr_sg, mastr_combustion]).reset_index() |
||
| 921 | |||
| 922 | # Delete entries outside Schleswig-Holstein for test mode |
||
| 923 | if boundary == "Schleswig-Holstein": |
||
| 924 | mastr_others = mastr_others[ |
||
| 925 | mastr_others["Bundesland"] == "SchleswigHolstein" |
||
| 926 | ] |
||
| 927 | |||
| 928 | # Scale capacities prox to now to meet target values |
||
| 929 | mastr_prox = scale_prox2now(mastr_others, target, level="federal_state") |
||
| 930 | |||
| 931 | # Assign voltage_level based on scaled capacity |
||
| 932 | mastr_prox["voltage_level"] = np.nan |
||
| 933 | mastr_prox["voltage_level"] = assign_voltage_level_by_capacity(mastr_prox) |
||
| 934 | |||
| 935 | # Rename columns |
||
| 936 | mastr_prox = mastr_prox.rename( |
||
| 937 | columns={ |
||
| 938 | "Energietraeger": "carrier", |
||
| 939 | "Postleitzahl": "plz", |
||
| 940 | "Ort": "city", |
||
| 941 | "Bundesland": "federal_state", |
||
| 942 | "Nettonennleistung": "el_capacity", |
||
| 943 | } |
||
| 944 | ) |
||
| 945 | |||
| 946 | # Assign bus_id |
||
| 947 | mastr_prox = assign_bus_id(mastr_prox, cfg) |
||
| 948 | mastr_prox = mastr_prox.set_crs(4326, allow_override=True) |
||
| 949 | |||
| 950 | # Insert into target table |
||
| 951 | session = sessionmaker(bind=db.engine())() |
||
| 952 | for i, row in mastr_prox.iterrows(): |
||
| 953 | entry = EgonPowerPlants( |
||
| 954 | sources=row.el_capacity, |
||
| 955 | source_id={"MastrNummer": row.EinheitMastrNummer}, |
||
| 956 | carrier=row.carrier, |
||
| 957 | el_capacity=row.el_capacity, |
||
| 958 | voltage_level=row.voltage_level, |
||
| 959 | bus_id=row.bus_id, |
||
| 960 | scenario=scenario, |
||
| 961 | geom=f"SRID=4326; {row.geometry}", |
||
| 962 | ) |
||
| 963 | session.add(entry) |
||
| 964 | session.commit() |
||
| 965 | |||
| 966 | |||
| 967 | def discard_not_available_generators(gen, max_date): |
||
| 968 | gen["decommissioning_date"] = pd.to_datetime(gen["decommissioning_date"]) |
||
| 969 | gen["commissioning_date"] = pd.to_datetime(gen["commissioning_date"]) |
||
| 970 | # drop plants that are commissioned after the max date |
||
| 971 | gen = gen[gen["commissioning_date"] < max_date] |
||
| 972 | |||
| 973 | # drop decommissioned plants while keeping the ones decommissioned |
||
| 974 | # after the max date |
||
| 975 | gen.loc[(gen["decommissioning_date"] > max_date), "status"] = "InBetrieb" |
||
| 976 | |||
| 977 | gen = gen.loc[ |
||
| 978 | gen["status"].isin(["InBetrieb", "VoruebergehendStillgelegt"]) |
||
| 979 | ] |
||
| 980 | |||
| 981 | # drop unnecessary columns |
||
| 982 | gen = gen.drop(columns=["commissioning_date", "decommissioning_date"]) |
||
| 983 | |||
| 984 | return gen |
||
| 985 | |||
| 986 | |||
| 987 | def fill_missing_bus_and_geom( |
||
| 988 | gens, carrier, geom_municipalities, mv_grid_districts |
||
| 989 | ): |
||
| 990 | # drop generators without data to get geometry. |
||
| 991 | drop_id = gens[ |
||
| 992 | (gens.geom.is_empty) & ~(gens.location.isin(geom_municipalities.index)) |
||
| 993 | ].index |
||
| 994 | new_geom = gens["capacity"][ |
||
| 995 | (gens.geom.is_empty) & (gens.location.isin(geom_municipalities.index)) |
||
| 996 | ] |
||
| 997 | logger.info( |
||
| 998 | f"""{len(drop_id)} {carrier} generator(s) ({int(gens.loc[drop_id, 'capacity'] |
||
| 999 | .sum())}MW) were drop""" |
||
| 1000 | ) |
||
| 1001 | |||
| 1002 | logger.info( |
||
| 1003 | f"""{len(new_geom)} {carrier} generator(s) ({int(new_geom |
||
| 1004 | .sum())}MW) received a geom based on location |
||
| 1005 | """ |
||
| 1006 | ) |
||
| 1007 | gens.drop(index=drop_id, inplace=True) |
||
| 1008 | |||
| 1009 | # assign missing geometries based on location and buses based on geom |
||
| 1010 | |||
| 1011 | gens["geom"] = gens.apply( |
||
| 1012 | lambda x: ( |
||
| 1013 | geom_municipalities.at[x["location"], "geom"] |
||
| 1014 | if x["geom"].is_empty |
||
| 1015 | else x["geom"] |
||
| 1016 | ), |
||
| 1017 | axis=1, |
||
| 1018 | ) |
||
| 1019 | gens["bus_id"] = gens.sjoin( |
||
| 1020 | mv_grid_districts[["bus_id", "geom"]], how="left" |
||
| 1021 | ).bus_id_right.values |
||
| 1022 | |||
| 1023 | gens = gens.dropna(subset=["bus_id"]) |
||
| 1024 | # convert geom to WKB |
||
| 1025 | gens["geom"] = gens["geom"].to_wkt() |
||
| 1026 | |||
| 1027 | return gens |
||
| 1028 | |||
| 1029 | |||
| 1030 | def power_plants_status_quo(scn_name="status2019"): |
||
| 1031 | def convert_master_info(df): |
||
| 1032 | # Add further information |
||
| 1033 | df["sources"] = [{"el_capacity": "MaStR"}] * df.shape[0] |
||
| 1034 | df["source_id"] = df["gens_id"].apply(lambda x: {"MastrNummer": x}) |
||
| 1035 | return df |
||
| 1036 | |||
| 1037 | def log_insert_capacity(df, tech): |
||
| 1038 | logger.info( |
||
| 1039 | f""" |
||
| 1040 | {len(df)} {tech} generators with a total installed capacity of |
||
| 1041 | {int(df["el_capacity"].sum())} MW were inserted into the db |
||
| 1042 | """ |
||
| 1043 | ) |
||
| 1044 | |||
| 1045 | con = db.engine() |
||
| 1046 | cfg = egon.data.config.datasets()["power_plants"] |
||
| 1047 | |||
| 1048 | db.execute_sql( |
||
| 1049 | f""" |
||
| 1050 | DELETE FROM {cfg['target']['schema']}.{cfg['target']['table']} |
||
| 1051 | WHERE carrier IN ('wind_onshore', 'solar', 'biomass', |
||
| 1052 | 'run_of_river', 'reservoir', 'solar_rooftop', |
||
| 1053 | 'wind_offshore', 'nuclear', 'coal', 'lignite', 'oil', |
||
| 1054 | 'gas') |
||
| 1055 | AND scenario = '{scn_name}' |
||
| 1056 | """ |
||
| 1057 | ) |
||
| 1058 | |||
| 1059 | # import municipalities to assign missing geom and bus_id |
||
| 1060 | geom_municipalities = gpd.GeoDataFrame.from_postgis( |
||
| 1061 | """ |
||
| 1062 | SELECT gen, ST_UNION(geometry) as geom |
||
| 1063 | FROM boundaries.vg250_gem |
||
| 1064 | GROUP BY gen |
||
| 1065 | """, |
||
| 1066 | con, |
||
| 1067 | geom_col="geom", |
||
| 1068 | ).set_index("gen") |
||
| 1069 | geom_municipalities["geom"] = geom_municipalities["geom"].centroid |
||
| 1070 | |||
| 1071 | mv_grid_districts = gpd.GeoDataFrame.from_postgis( |
||
| 1072 | f""" |
||
| 1073 | SELECT * FROM {cfg['sources']['egon_mv_grid_district']} |
||
| 1074 | """, |
||
| 1075 | con, |
||
| 1076 | ) |
||
| 1077 | mv_grid_districts.geom = mv_grid_districts.geom.to_crs(4326) |
||
| 1078 | |||
| 1079 | # Conventional non CHP |
||
| 1080 | # ################### |
||
| 1081 | conv = get_conventional_power_plants_non_chp(scn_name) |
||
| 1082 | |||
| 1083 | conv = fill_missing_bus_and_geom( |
||
| 1084 | conv, "conventional", geom_municipalities, mv_grid_districts |
||
| 1085 | ) |
||
| 1086 | |||
| 1087 | conv = conv.rename(columns={"capacity": "el_capacity"}) |
||
| 1088 | |||
| 1089 | # Write into DB |
||
| 1090 | with db.session_scope() as session: |
||
| 1091 | session.bulk_insert_mappings( |
||
| 1092 | EgonPowerPlants, |
||
| 1093 | conv.to_dict(orient="records"), |
||
| 1094 | ) |
||
| 1095 | |||
| 1096 | log_insert_capacity(conv, tech="conventional non chp") |
||
| 1097 | |||
| 1098 | # Hydro Power Plants |
||
| 1099 | # ################### |
||
| 1100 | hydro = gpd.GeoDataFrame.from_postgis( |
||
| 1101 | f"""SELECT *, city AS location FROM {cfg['sources']['hydro']} |
||
| 1102 | WHERE plant_type IN ('Laufwasseranlage', 'Speicherwasseranlage')""", |
||
| 1103 | con, |
||
| 1104 | geom_col="geom", |
||
| 1105 | ) |
||
| 1106 | |||
| 1107 | hydro = fill_missing_bus_and_geom( |
||
| 1108 | hydro, "hydro", geom_municipalities, mv_grid_districts |
||
| 1109 | ) |
||
| 1110 | |||
| 1111 | hydro = convert_master_info(hydro) |
||
| 1112 | hydro["carrier"] = hydro["plant_type"].replace( |
||
| 1113 | to_replace={ |
||
| 1114 | "Laufwasseranlage": "run_of_river", |
||
| 1115 | "Speicherwasseranlage": "reservoir", |
||
| 1116 | } |
||
| 1117 | ) |
||
| 1118 | hydro["scenario"] = scn_name |
||
| 1119 | hydro = hydro.rename(columns={"capacity": "el_capacity"}) |
||
| 1120 | hydro = hydro.drop(columns="id") |
||
| 1121 | |||
| 1122 | # Write into DB |
||
| 1123 | with db.session_scope() as session: |
||
| 1124 | session.bulk_insert_mappings( |
||
| 1125 | EgonPowerPlants, |
||
| 1126 | hydro.to_dict(orient="records"), |
||
| 1127 | ) |
||
| 1128 | |||
| 1129 | log_insert_capacity(hydro, tech="hydro") |
||
| 1130 | |||
| 1131 | # Biomass |
||
| 1132 | # ################### |
||
| 1133 | biomass = gpd.GeoDataFrame.from_postgis( |
||
| 1134 | f"""SELECT *, city AS location FROM {cfg['sources']['biomass']}""", |
||
| 1135 | con, |
||
| 1136 | geom_col="geom", |
||
| 1137 | ) |
||
| 1138 | |||
| 1139 | # drop chp generators |
||
| 1140 | biomass["th_capacity"] = biomass["th_capacity"].fillna(0) |
||
| 1141 | biomass = biomass[biomass.th_capacity == 0] |
||
| 1142 | |||
| 1143 | biomass = fill_missing_bus_and_geom( |
||
| 1144 | biomass, "biomass", geom_municipalities, mv_grid_districts |
||
| 1145 | ) |
||
| 1146 | |||
| 1147 | biomass = convert_master_info(biomass) |
||
| 1148 | biomass["scenario"] = scn_name |
||
| 1149 | biomass["carrier"] = "biomass" |
||
| 1150 | biomass = biomass.rename(columns={"capacity": "el_capacity"}) |
||
| 1151 | biomass = biomass.drop(columns="id") |
||
| 1152 | |||
| 1153 | # Write into DB |
||
| 1154 | with db.session_scope() as session: |
||
| 1155 | session.bulk_insert_mappings( |
||
| 1156 | EgonPowerPlants, |
||
| 1157 | biomass.to_dict(orient="records"), |
||
| 1158 | ) |
||
| 1159 | |||
| 1160 | log_insert_capacity(biomass, tech="biomass") |
||
| 1161 | |||
| 1162 | # Solar |
||
| 1163 | # ################### |
||
| 1164 | solar = gpd.GeoDataFrame.from_postgis( |
||
| 1165 | f"""SELECT *, city AS location FROM {cfg['sources']['pv']} |
||
| 1166 | WHERE site_type IN ('Freifläche', |
||
| 1167 | 'Bauliche Anlagen (Hausdach, Gebäude und Fassade)') """, |
||
| 1168 | con, |
||
| 1169 | geom_col="geom", |
||
| 1170 | ) |
||
| 1171 | map_solar = { |
||
| 1172 | "Freifläche": "solar", |
||
| 1173 | "Bauliche Anlagen (Hausdach, Gebäude und Fassade)": "solar_rooftop", |
||
| 1174 | } |
||
| 1175 | solar["carrier"] = solar["site_type"].replace(to_replace=map_solar) |
||
| 1176 | |||
| 1177 | solar = fill_missing_bus_and_geom( |
||
| 1178 | solar, "solar", geom_municipalities, mv_grid_districts |
||
| 1179 | ) |
||
| 1180 | |||
| 1181 | solar = convert_master_info(solar) |
||
| 1182 | solar["scenario"] = scn_name |
||
| 1183 | solar = solar.rename(columns={"capacity": "el_capacity"}) |
||
| 1184 | solar = solar.drop(columns="id") |
||
| 1185 | |||
| 1186 | # Write into DB |
||
| 1187 | with db.session_scope() as session: |
||
| 1188 | session.bulk_insert_mappings( |
||
| 1189 | EgonPowerPlants, |
||
| 1190 | solar.to_dict(orient="records"), |
||
| 1191 | ) |
||
| 1192 | |||
| 1193 | log_insert_capacity(solar, tech="solar") |
||
| 1194 | |||
| 1195 | # Wind |
||
| 1196 | # ################### |
||
| 1197 | wind_onshore = gpd.GeoDataFrame.from_postgis( |
||
| 1198 | f"""SELECT *, city AS location FROM {cfg['sources']['wind']}""", |
||
| 1199 | con, |
||
| 1200 | geom_col="geom", |
||
| 1201 | ) |
||
| 1202 | |||
| 1203 | wind_onshore = fill_missing_bus_and_geom( |
||
| 1204 | wind_onshore, "wind_onshore", geom_municipalities, mv_grid_districts |
||
| 1205 | ) |
||
| 1206 | |||
| 1207 | wind_onshore = convert_master_info(wind_onshore) |
||
| 1208 | wind_onshore["scenario"] = scn_name |
||
| 1209 | wind_onshore = wind_onshore.rename(columns={"capacity": "el_capacity"}) |
||
| 1210 | wind_onshore["carrier"] = "wind_onshore" |
||
| 1211 | wind_onshore = wind_onshore.drop(columns="id") |
||
| 1212 | |||
| 1213 | # Write into DB |
||
| 1214 | with db.session_scope() as session: |
||
| 1215 | session.bulk_insert_mappings( |
||
| 1216 | EgonPowerPlants, |
||
| 1217 | wind_onshore.to_dict(orient="records"), |
||
| 1218 | ) |
||
| 1219 | |||
| 1220 | log_insert_capacity(wind_onshore, tech="wind_onshore") |
||
| 1221 | |||
| 1222 | |||
| 1223 | def get_conventional_power_plants_non_chp(scn_name): |
||
| 1224 | |||
| 1225 | cfg = egon.data.config.datasets()["power_plants"] |
||
| 1226 | # Write conventional power plants in supply.egon_power_plants |
||
| 1227 | common_columns = [ |
||
| 1228 | "EinheitMastrNummer", |
||
| 1229 | "Energietraeger", |
||
| 1230 | "Nettonennleistung", |
||
| 1231 | "Laengengrad", |
||
| 1232 | "Breitengrad", |
||
| 1233 | "Gemeinde", |
||
| 1234 | "Inbetriebnahmedatum", |
||
| 1235 | "EinheitBetriebsstatus", |
||
| 1236 | "DatumEndgueltigeStilllegung", |
||
| 1237 | ] |
||
| 1238 | # import nuclear power plants |
||
| 1239 | nuclear = pd.read_csv( |
||
| 1240 | WORKING_DIR_MASTR_OLD / cfg["sources"]["mastr_nuclear"], |
||
| 1241 | usecols=common_columns, |
||
| 1242 | ) |
||
| 1243 | # import combustion power plants |
||
| 1244 | comb = pd.read_csv( |
||
| 1245 | WORKING_DIR_MASTR_OLD / cfg["sources"]["mastr_combustion"], |
||
| 1246 | usecols=common_columns + ["ThermischeNutzleistung"], |
||
| 1247 | ) |
||
| 1248 | |||
| 1249 | conv = pd.concat([comb, nuclear]) |
||
| 1250 | |||
| 1251 | conv = conv[ |
||
| 1252 | conv.Energietraeger.isin( |
||
| 1253 | [ |
||
| 1254 | "Braunkohle", |
||
| 1255 | "Mineralölprodukte", |
||
| 1256 | "Steinkohle", |
||
| 1257 | "Kernenergie", |
||
| 1258 | "Erdgas", |
||
| 1259 | ] |
||
| 1260 | ) |
||
| 1261 | ] |
||
| 1262 | |||
| 1263 | # drop plants that are decommissioned |
||
| 1264 | conv["DatumEndgueltigeStilllegung"] = pd.to_datetime( |
||
| 1265 | conv["DatumEndgueltigeStilllegung"] |
||
| 1266 | ) |
||
| 1267 | |||
| 1268 | # keep plants that were decommissioned after the max date |
||
| 1269 | conv.loc[ |
||
| 1270 | ( |
||
| 1271 | conv.DatumEndgueltigeStilllegung |
||
| 1272 | > egon.data.config.datasets()["mastr_new"][f"{scn_name}_date_max"] |
||
| 1273 | ), |
||
| 1274 | "EinheitBetriebsstatus", |
||
| 1275 | ] = "InBetrieb" |
||
| 1276 | |||
| 1277 | conv = conv.loc[conv.EinheitBetriebsstatus == "InBetrieb"] |
||
| 1278 | |||
| 1279 | conv = conv.drop( |
||
| 1280 | columns=["EinheitBetriebsstatus", "DatumEndgueltigeStilllegung"] |
||
| 1281 | ) |
||
| 1282 | |||
| 1283 | # convert from KW to MW |
||
| 1284 | conv["Nettonennleistung"] = conv["Nettonennleistung"] / 1000 |
||
| 1285 | |||
| 1286 | # drop generators installed after 2019 |
||
| 1287 | conv["Inbetriebnahmedatum"] = pd.to_datetime(conv["Inbetriebnahmedatum"]) |
||
| 1288 | conv = conv[ |
||
| 1289 | conv["Inbetriebnahmedatum"] |
||
| 1290 | < egon.data.config.datasets()["mastr_new"][f"{scn_name}_date_max"] |
||
| 1291 | ] |
||
| 1292 | |||
| 1293 | conv_cap_chp = ( |
||
| 1294 | conv.groupby("Energietraeger")["Nettonennleistung"].sum() / 1e3 |
||
| 1295 | ) |
||
| 1296 | # drop chp generators |
||
| 1297 | conv["ThermischeNutzleistung"] = conv["ThermischeNutzleistung"].fillna(0) |
||
| 1298 | conv = conv[conv.ThermischeNutzleistung == 0] |
||
| 1299 | conv_cap_no_chp = ( |
||
| 1300 | conv.groupby("Energietraeger")["Nettonennleistung"].sum() / 1e3 |
||
| 1301 | ) |
||
| 1302 | |||
| 1303 | logger.info("Dropped CHP generators in GW") |
||
| 1304 | logger.info(conv_cap_chp - conv_cap_no_chp) |
||
| 1305 | |||
| 1306 | # rename carriers |
||
| 1307 | # rename carriers |
||
| 1308 | conv["Energietraeger"] = conv["Energietraeger"].replace( |
||
| 1309 | to_replace={ |
||
| 1310 | "Braunkohle": "lignite", |
||
| 1311 | "Steinkohle": "coal", |
||
| 1312 | "Erdgas": "gas", |
||
| 1313 | "Mineralölprodukte": "oil", |
||
| 1314 | "Kernenergie": "nuclear", |
||
| 1315 | } |
||
| 1316 | ) |
||
| 1317 | |||
| 1318 | # rename columns |
||
| 1319 | conv.rename( |
||
| 1320 | columns={ |
||
| 1321 | "EinheitMastrNummer": "gens_id", |
||
| 1322 | "Energietraeger": "carrier", |
||
| 1323 | "Nettonennleistung": "capacity", |
||
| 1324 | "Gemeinde": "location", |
||
| 1325 | }, |
||
| 1326 | inplace=True, |
||
| 1327 | ) |
||
| 1328 | conv["bus_id"] = np.nan |
||
| 1329 | conv["geom"] = gpd.points_from_xy( |
||
| 1330 | conv.Laengengrad, conv.Breitengrad, crs=4326 |
||
| 1331 | ) |
||
| 1332 | conv.loc[(conv.Laengengrad.isna() | conv.Breitengrad.isna()), "geom"] = ( |
||
| 1333 | Point() |
||
| 1334 | ) |
||
| 1335 | conv = gpd.GeoDataFrame(conv, geometry="geom") |
||
| 1336 | |||
| 1337 | # assign voltage level by capacity |
||
| 1338 | conv["voltage_level"] = np.nan |
||
| 1339 | conv["voltage_level"] = assign_voltage_level_by_capacity( |
||
| 1340 | conv.rename(columns={"capacity": "Nettonennleistung"}) |
||
| 1341 | ) |
||
| 1342 | # Add further information |
||
| 1343 | conv["sources"] = [{"el_capacity": "MaStR"}] * conv.shape[0] |
||
| 1344 | conv["source_id"] = conv["gens_id"].apply(lambda x: {"MastrNummer": x}) |
||
| 1345 | conv["scenario"] = scn_name |
||
| 1346 | |||
| 1347 | return conv |
||
| 1348 | |||
| 1349 | |||
| 1350 | def import_gas_gen_egon100(): |
||
| 1351 | scn_name = "eGon100RE" |
||
| 1352 | if scn_name not in egon.data.config.settings()["egon-data"]["--scenarios"]: |
||
| 1353 | return |
||
| 1354 | con = db.engine() |
||
| 1355 | session = sessionmaker(bind=db.engine())() |
||
| 1356 | cfg = egon.data.config.datasets()["power_plants"] |
||
| 1357 | scenario_date_max = "2045-12-31 23:59:00" |
||
| 1358 | |||
| 1359 | db.execute_sql( |
||
| 1360 | f""" |
||
| 1361 | DELETE FROM {cfg['target']['schema']}.{cfg['target']['table']} |
||
| 1362 | WHERE carrier = 'gas' |
||
| 1363 | AND bus_id IN (SELECT bus_id from grid.egon_etrago_bus |
||
| 1364 | WHERE scn_name = '{scn_name}' |
||
| 1365 | AND country = 'DE') |
||
| 1366 | AND scenario = '{scn_name}' |
||
| 1367 | """ |
||
| 1368 | ) |
||
| 1369 | |||
| 1370 | # import municipalities to assign missing geom and bus_id |
||
| 1371 | geom_municipalities = gpd.GeoDataFrame.from_postgis( |
||
| 1372 | """ |
||
| 1373 | SELECT gen, ST_UNION(geometry) as geom |
||
| 1374 | FROM boundaries.vg250_gem |
||
| 1375 | GROUP BY gen |
||
| 1376 | """, |
||
| 1377 | con, |
||
| 1378 | geom_col="geom", |
||
| 1379 | ).set_index("gen") |
||
| 1380 | geom_municipalities["geom"] = geom_municipalities["geom"].centroid |
||
| 1381 | |||
| 1382 | mv_grid_districts = gpd.GeoDataFrame.from_postgis( |
||
| 1383 | f""" |
||
| 1384 | SELECT * FROM {cfg['sources']['egon_mv_grid_district']} |
||
| 1385 | """, |
||
| 1386 | con, |
||
| 1387 | ) |
||
| 1388 | mv_grid_districts.geom = mv_grid_districts.geom.to_crs(4326) |
||
| 1389 | |||
| 1390 | target = db.select_dataframe( |
||
| 1391 | f""" |
||
| 1392 | SELECT capacity FROM supply.egon_scenario_capacities |
||
| 1393 | WHERE scenario_name = '{scn_name}' |
||
| 1394 | AND carrier = 'gas' |
||
| 1395 | """, |
||
| 1396 | ).iat[0, 0] |
||
| 1397 | |||
| 1398 | conv = pd.read_csv( |
||
| 1399 | WORKING_DIR_MASTR_OLD / cfg["sources"]["mastr_combustion"], |
||
| 1400 | usecols=[ |
||
| 1401 | "EinheitMastrNummer", |
||
| 1402 | "Energietraeger", |
||
| 1403 | "Nettonennleistung", |
||
| 1404 | "Laengengrad", |
||
| 1405 | "Breitengrad", |
||
| 1406 | "Gemeinde", |
||
| 1407 | "Inbetriebnahmedatum", |
||
| 1408 | "EinheitBetriebsstatus", |
||
| 1409 | "DatumEndgueltigeStilllegung", |
||
| 1410 | "ThermischeNutzleistung", |
||
| 1411 | ], |
||
| 1412 | ) |
||
| 1413 | |||
| 1414 | conv = conv[conv.Energietraeger == "Erdgas"] |
||
| 1415 | |||
| 1416 | conv.rename( |
||
| 1417 | columns={ |
||
| 1418 | "Inbetriebnahmedatum": "commissioning_date", |
||
| 1419 | "EinheitBetriebsstatus": "status", |
||
| 1420 | "DatumEndgueltigeStilllegung": "decommissioning_date", |
||
| 1421 | "EinheitMastrNummer": "gens_id", |
||
| 1422 | "Energietraeger": "carrier", |
||
| 1423 | "Nettonennleistung": "capacity", |
||
| 1424 | "Gemeinde": "location", |
||
| 1425 | }, |
||
| 1426 | inplace=True, |
||
| 1427 | ) |
||
| 1428 | |||
| 1429 | conv = discard_not_available_generators(conv, scenario_date_max) |
||
| 1430 | |||
| 1431 | # convert from KW to MW |
||
| 1432 | conv["capacity"] = conv["capacity"] / 1000 |
||
| 1433 | |||
| 1434 | # drop chp generators |
||
| 1435 | conv["ThermischeNutzleistung"] = conv["ThermischeNutzleistung"].fillna(0) |
||
| 1436 | conv = conv[conv.ThermischeNutzleistung == 0] |
||
| 1437 | |||
| 1438 | # rename carriers |
||
| 1439 | map_carrier_conv = {"Erdgas": "gas"} |
||
| 1440 | conv["carrier"] = conv["carrier"].map(map_carrier_conv) |
||
| 1441 | |||
| 1442 | conv["bus_id"] = np.nan |
||
| 1443 | |||
| 1444 | conv["geom"] = gpd.points_from_xy( |
||
| 1445 | conv.Laengengrad, conv.Breitengrad, crs=4326 |
||
| 1446 | ) |
||
| 1447 | conv.loc[(conv.Laengengrad.isna() | conv.Breitengrad.isna()), "geom"] = ( |
||
| 1448 | Point() |
||
| 1449 | ) |
||
| 1450 | conv = gpd.GeoDataFrame(conv, geometry="geom") |
||
| 1451 | |||
| 1452 | conv = fill_missing_bus_and_geom( |
||
| 1453 | conv, "conventional", geom_municipalities, mv_grid_districts |
||
| 1454 | ) |
||
| 1455 | conv["voltage_level"] = np.nan |
||
| 1456 | |||
| 1457 | conv["voltage_level"] = assign_voltage_level_by_capacity( |
||
| 1458 | conv.rename(columns={"capacity": "Nettonennleistung"}) |
||
| 1459 | ) |
||
| 1460 | |||
| 1461 | conv["capacity"] = conv["capacity"] * (target / conv["capacity"].sum()) |
||
| 1462 | |||
| 1463 | max_id = db.select_dataframe( |
||
| 1464 | """ |
||
| 1465 | SELECT max(id) FROM supply.egon_power_plants |
||
| 1466 | """, |
||
| 1467 | ).iat[0, 0] |
||
| 1468 | |||
| 1469 | conv["id"] = range(max_id + 1, max_id + 1 + len(conv)) |
||
| 1470 | |||
| 1471 | for i, row in conv.iterrows(): |
||
| 1472 | entry = EgonPowerPlants( |
||
| 1473 | id=row.id, |
||
| 1474 | sources={"el_capacity": "MaStR"}, |
||
| 1475 | source_id={"MastrNummer": row.gens_id}, |
||
| 1476 | carrier=row.carrier, |
||
| 1477 | el_capacity=row.capacity, |
||
| 1478 | scenario=scn_name, |
||
| 1479 | bus_id=row.bus_id, |
||
| 1480 | voltage_level=row.voltage_level, |
||
| 1481 | geom=row.geom, |
||
| 1482 | ) |
||
| 1483 | session.add(entry) |
||
| 1484 | session.commit() |
||
| 1485 | |||
| 1486 | logging.info( |
||
| 1487 | f""" |
||
| 1488 | {len(conv)} gas generators with a total installed capacity of |
||
| 1489 | {conv.capacity.sum()}MW were inserted into the db |
||
| 1490 | """ |
||
| 1491 | ) |
||
| 1492 | |||
| 1493 | return |
||
| 1494 | |||
| 1495 | |||
| 1496 | tasks = ( |
||
| 1497 | create_tables, |
||
| 1498 | import_mastr, |
||
| 1499 | ) |
||
| 1500 | |||
| 1501 | for scn_name in egon.data.config.settings()["egon-data"]["--scenarios"]: |
||
| 1502 | if "status" in scn_name: |
||
| 1503 | tasks += ( |
||
| 1504 | wrapped_partial( |
||
| 1505 | power_plants_status_quo, |
||
| 1506 | scn_name=scn_name, |
||
| 1507 | postfix=f"_{scn_name[-4:]}", |
||
| 1508 | ), |
||
| 1509 | ) |
||
| 1510 | |||
| 1511 | if ( |
||
| 1512 | "eGon2035" in egon.data.config.settings()["egon-data"]["--scenarios"] |
||
| 1513 | or "eGon100RE" in egon.data.config.settings()["egon-data"]["--scenarios"] |
||
| 1514 | ): |
||
| 1515 | tasks = tasks + ( |
||
| 1516 | insert_hydro_biomass, |
||
| 1517 | allocate_conventional_non_chp_power_plants, |
||
| 1518 | allocate_other_power_plants, |
||
| 1519 | { |
||
| 1520 | wind_onshore.insert, |
||
| 1521 | pv_ground_mounted.insert, |
||
| 1522 | pv_rooftop_per_mv_grid, |
||
| 1523 | }, |
||
| 1524 | ) |
||
| 1525 | |||
| 1526 | if "eGon100RE" in egon.data.config.settings()["egon-data"]["--scenarios"]: |
||
| 1527 | tasks = tasks + (import_gas_gen_egon100,) |
||
| 1528 | |||
| 1529 | tasks = tasks + ( |
||
| 1530 | pv_rooftop_to_buildings, |
||
| 1531 | wind_offshore.insert, |
||
| 1532 | ) |
||
| 1533 | |||
| 1534 | for scn_name in egon.data.config.settings()["egon-data"]["--scenarios"]: |
||
| 1535 | tasks += ( |
||
| 1536 | wrapped_partial( |
||
| 1537 | assign_weather_data.weatherId_and_busId, |
||
| 1538 | scn_name=scn_name, |
||
| 1539 | postfix=f"_{scn_name}", |
||
| 1540 | ), |
||
| 1541 | ) |
||
| 1542 | |||
| 1543 | tasks += (pp_metadata.metadata,) |
||
| 1544 | |||
| 1545 | |||
| 1546 | class PowerPlants(Dataset): |
||
| 1547 | """ |
||
| 1548 | This dataset deals with the distribution and allocation of power plants |
||
| 1549 | |||
| 1550 | For the distribution and allocation of power plants to their corresponding |
||
| 1551 | grid connection point different technology-specific methods are applied. |
||
| 1552 | In a first step separate tables are created for wind, pv, hydro and biomass |
||
| 1553 | based power plants by running :py:func:`create_tables`. |
||
| 1554 | Different methods rely on the locations of existing power plants retrieved |
||
| 1555 | from the official power plant registry 'Marktstammdatenregister' applying |
||
| 1556 | function :py:func:`ìmport_mastr`. |
||
| 1557 | |||
| 1558 | *Hydro and Biomass* |
||
| 1559 | Hydro and biomass power plants are distributed based on the status quo |
||
| 1560 | locations of existing power plants assuming that no further expansion of |
||
| 1561 | these technologies is to be expected in Germany. Hydro power plants include |
||
| 1562 | reservoir and run-of-river plants. |
||
| 1563 | Power plants without a correct geolocation are not taken into account. |
||
| 1564 | To compensate this, the installed capacities of the suitable plants are |
||
| 1565 | scaled up to meet the target value using function :py:func:`scale_prox2now` |
||
| 1566 | |||
| 1567 | *Conventional power plants without CHP* |
||
| 1568 | The distribution of conventional plants, excluding CHPs, takes place in |
||
| 1569 | function :py:func:`allocate_conventional_non_chp_power_plants`. Therefore |
||
| 1570 | information about future power plants from the grid development plan |
||
| 1571 | function as the target value and are matched with actual existing power |
||
| 1572 | plants with correct geolocations from MaStR registry. |
||
| 1573 | |||
| 1574 | *Wind onshore* |
||
| 1575 | |||
| 1576 | |||
| 1577 | *Wind offshore* |
||
| 1578 | |||
| 1579 | *PV ground-mounted* |
||
| 1580 | |||
| 1581 | *PV rooftop* |
||
| 1582 | |||
| 1583 | *others* |
||
| 1584 | |||
| 1585 | *Dependencies* |
||
| 1586 | * :py:class:`Chp <egon.data.datasets.chp.Chp>` |
||
| 1587 | * :py:class:`CtsElectricityDemand |
||
| 1588 | <egon.data.datasets.electricity_demand.CtsElectricityDemand>` |
||
| 1589 | * :py:class:`HouseholdElectricityDemand |
||
| 1590 | <egon.data.datasets.electricity_demand.HouseholdElectricityDemand>` |
||
| 1591 | * :py:class:`mastr_data <egon.data.datasets.mastr.mastr_data>` |
||
| 1592 | * :py:func:`define_mv_grid_districts |
||
| 1593 | <egon.data.datasets.mv_grid_districts.define_mv_grid_districts>` |
||
| 1594 | * :py:class:`RePotentialAreas |
||
| 1595 | <egon.data.datasets.re_potential_areas.RePotentialAreas>` |
||
| 1596 | * :py:class:`ZensusVg250 |
||
| 1597 | <egon.data.datasets.RenewableFeedin>` |
||
| 1598 | * :py:class:`ScenarioCapacities |
||
| 1599 | <egon.data.datasets.scenario_capacities.ScenarioCapacities>` |
||
| 1600 | * :py:class:`ScenarioParameters |
||
| 1601 | <egon.data.datasets.scenario_parameters.ScenarioParameters>` |
||
| 1602 | * :py:func:`Setup <egon.data.datasets.database.setup>` |
||
| 1603 | * :py:class:`substation_extraction |
||
| 1604 | <egon.data.datasets.substation.substation_extraction>` |
||
| 1605 | * :py:class:`Vg250MvGridDistricts |
||
| 1606 | <egon.data.datasets.Vg250MvGridDistricts>` |
||
| 1607 | * :py:class:`ZensusMvGridDistricts |
||
| 1608 | <egon.data.datasets.zensus_mv_grid_districts.ZensusMvGridDistricts>` |
||
| 1609 | |||
| 1610 | *Resulting tables* |
||
| 1611 | * :py:class:`supply.egon_power_plants |
||
| 1612 | <egon.data.datasets.power_plants.EgonPowerPlants>` is filled |
||
| 1613 | |||
| 1614 | """ |
||
| 1615 | |||
| 1616 | #: |
||
| 1617 | name: str = "PowerPlants" |
||
| 1618 | #: |
||
| 1619 | version: str = "0.0.29" |
||
| 1620 | |||
| 1621 | def __init__(self, dependencies): |
||
| 1622 | super().__init__( |
||
| 1623 | name=self.name, |
||
| 1624 | version=self.version, |
||
| 1625 | dependencies=dependencies, |
||
| 1626 | tasks=tasks, |
||
| 1627 | ) |
||
| 1628 |