Conditions | 9 |
Total Lines | 372 |
Code Lines | 247 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | """Import MaStR dataset and write to DB tables |
||
157 | def import_mastr() -> None: |
||
158 | """Import MaStR data into database""" |
||
159 | engine = db.engine() |
||
160 | |||
161 | # import geocoded data |
||
162 | cfg = config.datasets()["mastr_new"] |
||
163 | path_parts = cfg["geocoding_path"] |
||
164 | path = Path(*["."] + path_parts).resolve() |
||
165 | path = list(path.iterdir())[0] |
||
166 | |||
167 | deposit_id_geocoding = int(path.parts[-1].split(".")[0].split("_")[-1]) |
||
168 | deposit_id_mastr = cfg["deposit_id"] |
||
169 | |||
170 | if deposit_id_geocoding != deposit_id_mastr: |
||
171 | raise AssertionError( |
||
172 | f"The zenodo (sandbox) deposit ID {deposit_id_mastr} for the MaStR" |
||
173 | f" dataset is not matching with the geocoding version " |
||
174 | f"{deposit_id_geocoding}. Make sure to hermonize the data. When " |
||
175 | f"the MaStR dataset is updated also update the geocoding and " |
||
176 | f"update the egon data bundle. The geocoding can be done using: " |
||
177 | f"https://github.com/RLI-sandbox/mastr-geocoding" |
||
178 | ) |
||
179 | |||
180 | geocoding_gdf = gpd.read_file(path) |
||
181 | |||
182 | # remove failed requests |
||
183 | geocoding_gdf = geocoding_gdf.loc[geocoding_gdf.geometry.is_valid] |
||
184 | |||
185 | EgonMastrGeocoded.__table__.drop(bind=engine, checkfirst=True) |
||
186 | EgonMastrGeocoded.__table__.create(bind=engine, checkfirst=True) |
||
187 | |||
188 | geocoding_gdf.to_postgis( |
||
189 | name=EgonMastrGeocoded.__tablename__, |
||
190 | con=engine, |
||
191 | if_exists="append", |
||
192 | schema=EgonMastrGeocoded.__table_args__["schema"], |
||
193 | index=True, |
||
194 | ) |
||
195 | |||
196 | cfg = config.datasets()["power_plants"] |
||
197 | |||
198 | cols_mapping = { |
||
199 | "all": { |
||
200 | "EinheitMastrNummer": "gens_id", |
||
201 | "EinheitBetriebsstatus": "status", |
||
202 | "Inbetriebnahmedatum": "commissioning_date", |
||
203 | "Postleitzahl": "postcode", |
||
204 | "Ort": "city", |
||
205 | "Gemeinde": "municipality", |
||
206 | "Bundesland": "federal_state", |
||
207 | "Nettonennleistung": "capacity", |
||
208 | "Einspeisungsart": "feedin_type", |
||
209 | }, |
||
210 | "pv": { |
||
211 | "Lage": "site_type", |
||
212 | "Standort": "site", |
||
213 | "Nutzungsbereich": "usage_sector", |
||
214 | "Hauptausrichtung": "orientation_primary", |
||
215 | "HauptausrichtungNeigungswinkel": "orientation_primary_angle", |
||
216 | "Nebenausrichtung": "orientation_secondary", |
||
217 | "NebenausrichtungNeigungswinkel": "orientation_secondary_angle", |
||
218 | "EinheitlicheAusrichtungUndNeigungswinkel": "orientation_uniform", |
||
219 | "AnzahlModule": "module_count", |
||
220 | "zugeordneteWirkleistungWechselrichter": "capacity_inverter", |
||
221 | }, |
||
222 | "wind": { |
||
223 | "Lage": "site_type", |
||
224 | "Hersteller": "manufacturer_name", |
||
225 | "Typenbezeichnung": "type_name", |
||
226 | "Nabenhoehe": "hub_height", |
||
227 | "Rotordurchmesser": "rotor_diameter", |
||
228 | }, |
||
229 | "biomass": { |
||
230 | "Technologie": "technology", |
||
231 | "Hauptbrennstoff": "main_fuel", |
||
232 | "Biomasseart": "fuel_type", |
||
233 | "ThermischeNutzleistung": "th_capacity", |
||
234 | }, |
||
235 | "hydro": { |
||
236 | "ArtDerWasserkraftanlage": "plant_type", |
||
237 | "ArtDesZuflusses": "water_origin", |
||
238 | }, |
||
239 | "combustion": { |
||
240 | "Energietraeger": "carrier", |
||
241 | "Hauptbrennstoff": "main_fuel", |
||
242 | "WeitererHauptbrennstoff": "other_main_fuel", |
||
243 | "Technologie": "technology", |
||
244 | "ThermischeNutzleistung": "th_capacity", |
||
245 | }, |
||
246 | "gsgk": { |
||
247 | "Energietraeger": "carrier", |
||
248 | "Technologie": "technology", |
||
249 | }, |
||
250 | "nuclear": { |
||
251 | "Energietraeger": "carrier", |
||
252 | "Technologie": "technology", |
||
253 | }, |
||
254 | "storage": { |
||
255 | "Energietraeger": "carrier", |
||
256 | "Technologie": "technology", |
||
257 | "Batterietechnologie": "battery_type", |
||
258 | "Pumpspeichertechnologie": "pump_storage_type", |
||
259 | }, |
||
260 | } |
||
261 | |||
262 | source_files = { |
||
263 | "pv": WORKING_DIR_MASTR_NEW / cfg["sources"]["mastr_pv"], |
||
264 | "wind": WORKING_DIR_MASTR_NEW / cfg["sources"]["mastr_wind"], |
||
265 | "biomass": WORKING_DIR_MASTR_NEW / cfg["sources"]["mastr_biomass"], |
||
266 | "hydro": WORKING_DIR_MASTR_NEW / cfg["sources"]["mastr_hydro"], |
||
267 | "combustion": WORKING_DIR_MASTR_NEW |
||
268 | / cfg["sources"]["mastr_combustion"], |
||
269 | "gsgk": WORKING_DIR_MASTR_NEW / cfg["sources"]["mastr_gsgk"], |
||
270 | "nuclear": WORKING_DIR_MASTR_NEW / cfg["sources"]["mastr_nuclear"], |
||
271 | "storage": WORKING_DIR_MASTR_NEW / cfg["sources"]["mastr_storage"], |
||
272 | } |
||
273 | |||
274 | target_tables = { |
||
275 | "pv": EgonPowerPlantsPv, |
||
276 | "wind": EgonPowerPlantsWind, |
||
277 | "biomass": EgonPowerPlantsBiomass, |
||
278 | "hydro": EgonPowerPlantsHydro, |
||
279 | "combustion": EgonPowerPlantsCombustion, |
||
280 | "gsgk": EgonPowerPlantsGsgk, |
||
281 | "nuclear": EgonPowerPlantsNuclear, |
||
282 | "storage": EgonPowerPlantsStorage, |
||
283 | } |
||
284 | |||
285 | vlevel_mapping = { |
||
286 | "Höchstspannung": 1, |
||
287 | "UmspannungZurHochspannung": 2, |
||
288 | "Hochspannung": 3, |
||
289 | "UmspannungZurMittelspannung": 4, |
||
290 | "Mittelspannung": 5, |
||
291 | "UmspannungZurNiederspannung": 6, |
||
292 | "Niederspannung": 7, |
||
293 | } |
||
294 | |||
295 | # import locations |
||
296 | locations = pd.read_csv( |
||
297 | WORKING_DIR_MASTR_NEW / cfg["sources"]["mastr_location"], |
||
298 | index_col=None, |
||
299 | ) |
||
300 | |||
301 | # import grid districts |
||
302 | mv_grid_districts = db.select_geodataframe( |
||
303 | f""" |
||
304 | SELECT * FROM {cfg['sources']['egon_mv_grid_district']} |
||
305 | """, |
||
306 | epsg=4326, |
||
307 | ) |
||
308 | |||
309 | # import units |
||
310 | technologies = [ |
||
311 | "pv", |
||
312 | "wind", |
||
313 | "biomass", |
||
314 | "hydro", |
||
315 | "combustion", |
||
316 | "gsgk", |
||
317 | "nuclear", |
||
318 | "storage", |
||
319 | ] |
||
320 | |||
321 | for tech in technologies: |
||
322 | # read units |
||
323 | logger.info(f"===== Importing MaStR dataset: {tech} =====") |
||
324 | logger.debug("Reading CSV and filtering data...") |
||
325 | units = pd.read_csv( |
||
326 | source_files[tech], |
||
327 | usecols=( |
||
328 | ["LokationMastrNummer", "Laengengrad", "Breitengrad", "Land"] |
||
329 | + list(cols_mapping["all"].keys()) |
||
330 | + list(cols_mapping[tech].keys()) |
||
331 | ), |
||
332 | index_col=None, |
||
333 | dtype={"Postleitzahl": str}, |
||
334 | low_memory=False, |
||
335 | ).rename(columns=cols_mapping) |
||
336 | |||
337 | # drop units outside of Germany |
||
338 | len_old = len(units) |
||
339 | units = units.loc[units.Land == "Deutschland"] |
||
340 | logger.debug( |
||
341 | f"{len_old - len(units)} units outside of Germany dropped..." |
||
342 | ) |
||
343 | |||
344 | # get boundary |
||
345 | boundary = ( |
||
346 | federal_state_data(geocoding_gdf.crs).dissolve().at[0, "geom"] |
||
347 | ) |
||
348 | |||
349 | # drop units installed after reference date from cfg |
||
350 | # (eGon2021 scenario) |
||
351 | len_old = len(units) |
||
352 | ts = pd.Timestamp(config.datasets()["mastr_new"]["egon2021_date_max"]) |
||
353 | units = units.loc[pd.to_datetime(units.Inbetriebnahmedatum) <= ts] |
||
354 | logger.debug( |
||
355 | f"{len_old - len(units)} units installed after {ts} dropped..." |
||
356 | ) |
||
357 | |||
358 | # drop not operating units |
||
359 | len_old = len(units) |
||
360 | units = units.loc[ |
||
361 | units.EinheitBetriebsstatus.isin( |
||
362 | ["InBetrieb", "VoruebergehendStillgelegt"] |
||
363 | ) |
||
364 | ] |
||
365 | logger.debug(f"{len_old - len(units)} not operating units dropped...") |
||
366 | |||
367 | # filter for SH units if in testmode |
||
368 | if not TESTMODE_OFF: |
||
369 | logger.info( |
||
370 | "TESTMODE: Dropping all units outside of Schleswig-Holstein..." |
||
371 | ) |
||
372 | units = units.loc[units.Bundesland == "SchleswigHolstein"] |
||
373 | |||
374 | # merge and rename voltage level |
||
375 | logger.debug("Merging with locations and allocate voltage level...") |
||
376 | units = units.merge( |
||
377 | locations[["MaStRNummer", "Spannungsebene"]], |
||
378 | left_on="LokationMastrNummer", |
||
379 | right_on="MaStRNummer", |
||
380 | how="left", |
||
381 | ) |
||
382 | # convert voltage levels to numbers |
||
383 | units["voltage_level"] = units.Spannungsebene.replace(vlevel_mapping) |
||
384 | # set voltage level for nan values |
||
385 | units = infer_voltage_level(units) |
||
386 | |||
387 | # add geometry |
||
388 | logger.debug("Adding geometries...") |
||
389 | units = gpd.GeoDataFrame( |
||
390 | units, |
||
391 | geometry=gpd.points_from_xy( |
||
392 | units["Laengengrad"], units["Breitengrad"], crs=4326 |
||
393 | ), |
||
394 | crs=4326, |
||
395 | ) |
||
396 | |||
397 | units["geometry_geocoded"] = ( |
||
398 | units.Laengengrad.isna() | units.Laengengrad.isna() |
||
399 | ) |
||
400 | |||
401 | units.loc[~units.geometry_geocoded, "geometry_geocoded"] = ~units.loc[ |
||
402 | ~units.geometry_geocoded, "geometry" |
||
403 | ].is_valid |
||
404 | |||
405 | units_wo_geom = units["geometry_geocoded"].sum() |
||
406 | |||
407 | logger.debug( |
||
408 | f"{units_wo_geom}/{len(units)} units do not have a geometry!" |
||
409 | " Adding geocoding results." |
||
410 | ) |
||
411 | |||
412 | # determine zip and municipality string |
||
413 | mask = ( |
||
414 | units.Postleitzahl.apply(isfloat) |
||
415 | & ~units.Postleitzahl.isna() |
||
416 | & ~units.Gemeinde.isna() |
||
417 | ) |
||
418 | units["zip_and_municipality"] = np.nan |
||
419 | ok_units = units.loc[mask] |
||
420 | |||
421 | units.loc[mask, "zip_and_municipality"] = ( |
||
422 | ok_units.Postleitzahl.astype(int).astype(str).str.zfill(5) |
||
423 | + " " |
||
424 | + ok_units.Gemeinde.astype(str).str.rstrip().str.lstrip() |
||
425 | + ", Deutschland" |
||
426 | ) |
||
427 | |||
428 | # get zip and municipality from Standort |
||
429 | parse_df = units.loc[~mask] |
||
430 | |||
431 | if not parse_df.empty and "Standort" in parse_df.columns: |
||
432 | init_len = len(parse_df) |
||
433 | |||
434 | logger.info( |
||
435 | f"Parsing ZIP code and municipality from Standort for " |
||
436 | f"{init_len} values for {tech}." |
||
437 | ) |
||
438 | |||
439 | parse_df[["zip_and_municipality", "drop_this"]] = ( |
||
440 | parse_df.Standort.astype(str) |
||
441 | .apply(zip_and_municipality_from_standort) |
||
442 | .tolist() |
||
443 | ) |
||
444 | |||
445 | parse_df = parse_df.loc[parse_df.drop_this] |
||
446 | |||
447 | if not parse_df.empty: |
||
448 | units.loc[ |
||
449 | parse_df.index, "zip_and_municipality" |
||
450 | ] = parse_df.zip_and_municipality |
||
451 | |||
452 | # add geocoding to missing |
||
453 | units = units.merge( |
||
454 | right=geocoding_gdf[["zip_and_municipality", "geometry"]].rename( |
||
455 | columns={"geometry": "temp"} |
||
456 | ), |
||
457 | how="left", |
||
458 | on="zip_and_municipality", |
||
459 | ) |
||
460 | |||
461 | units.loc[units.geometry_geocoded, "geometry"] = units.loc[ |
||
462 | units.geometry_geocoded, "temp" |
||
463 | ] |
||
464 | |||
465 | init_len = len(units) |
||
466 | |||
467 | logger.info( |
||
468 | "Dropping units outside boundary by geometry or without geometry" |
||
469 | "..." |
||
470 | ) |
||
471 | |||
472 | units.dropna(subset=["geometry"], inplace=True) |
||
473 | |||
474 | units = units.loc[units.geometry.within(boundary)] |
||
475 | |||
476 | logger.debug( |
||
477 | f"{init_len - len(units)}/{init_len} " |
||
478 | f"({((init_len - len(units)) / init_len) * 100: g} %) dropped." |
||
479 | ) |
||
480 | |||
481 | # drop unnecessary and rename columns |
||
482 | logger.debug("Reformatting...") |
||
483 | units.drop( |
||
484 | columns=[ |
||
485 | "LokationMastrNummer", |
||
486 | "MaStRNummer", |
||
487 | "Laengengrad", |
||
488 | "Breitengrad", |
||
489 | "Spannungsebene", |
||
490 | "Land", |
||
491 | "temp", |
||
492 | ], |
||
493 | inplace=True, |
||
494 | ) |
||
495 | mapping = cols_mapping["all"].copy() |
||
496 | mapping.update(cols_mapping[tech]) |
||
497 | mapping.update({"geometry": "geom"}) |
||
498 | units.rename(columns=mapping, inplace=True) |
||
499 | units["voltage_level"] = units.voltage_level.fillna(-1).astype(int) |
||
500 | |||
501 | units.set_geometry("geom", inplace=True) |
||
502 | units["id"] = range(0, len(units)) |
||
503 | |||
504 | # change capacity unit: kW to MW |
||
505 | units["capacity"] = units["capacity"] / 1e3 |
||
506 | if "capacity_inverter" in units.columns: |
||
507 | units["capacity_inverter"] = units["capacity_inverter"] / 1e3 |
||
508 | if "th_capacity" in units.columns: |
||
509 | units["th_capacity"] = units["th_capacity"] / 1e3 |
||
510 | |||
511 | # assign bus ids |
||
512 | logger.debug("Assigning bus ids...") |
||
513 | units = units.assign( |
||
514 | bus_id=units.loc[~units.geom.x.isna()] |
||
515 | .sjoin(mv_grid_districts[["bus_id", "geom"]], how="left") |
||
516 | .drop(columns=["index_right"]) |
||
517 | .bus_id |
||
518 | ) |
||
519 | units["bus_id"] = units.bus_id.fillna(-1).astype(int) |
||
520 | |||
521 | # write to DB |
||
522 | logger.info(f"Writing {len(units)} units to DB...") |
||
523 | |||
524 | units.to_postgis( |
||
525 | name=target_tables[tech].__tablename__, |
||
526 | con=engine, |
||
527 | if_exists="append", |
||
528 | schema=target_tables[tech].__table_args__["schema"], |
||
529 | ) |
||
530 |