Conditions | 18 |
Total Lines | 341 |
Code Lines | 238 |
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:
Complex classes like data.datasets.gas_grid.define_gas_pipeline_list() 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 | # -*- coding: utf-8 -*- |
||
332 | def define_gas_pipeline_list( |
||
333 | gas_nodes_list, abroad_gas_nodes_list, scn_name="eGon2035" |
||
334 | ): |
||
335 | """Define gas pipelines in Germany from SciGRID_gas IGGIELGN data |
||
336 | |||
337 | Insert detailled description |
||
338 | |||
339 | Parameters |
||
340 | ---------- |
||
341 | gas_nodes_list : dataframe |
||
342 | description missing |
||
343 | abroad_gas_nodes_list: dataframe |
||
344 | description missing |
||
345 | scn_name : str |
||
346 | Name of the scenario |
||
347 | |||
348 | Returns |
||
349 | ------- |
||
350 | gas_pipelines_list : pandas.DataFrame |
||
351 | Dataframe containing the gas pipelines in Germany |
||
352 | |||
353 | """ |
||
354 | abroad_gas_nodes_list = abroad_gas_nodes_list.set_index("country") |
||
355 | |||
356 | main_gas_carrier = get_sector_parameters("gas", scenario=scn_name)[ |
||
357 | "main_gas_carrier" |
||
358 | ] |
||
359 | |||
360 | # Select next id value |
||
361 | new_id = db.next_etrago_id("link") |
||
362 | |||
363 | classifiaction_file = ( |
||
364 | Path(".") |
||
365 | / "data_bundle_egon_data" |
||
366 | / "pipeline_classification_gas" |
||
367 | / "pipeline_classification.csv" |
||
368 | ) |
||
369 | |||
370 | classification = pd.read_csv( |
||
371 | classifiaction_file, |
||
372 | delimiter=",", |
||
373 | usecols=["classification", "max_transport_capacity_Gwh/d"], |
||
374 | ) |
||
375 | |||
376 | target_file = ( |
||
377 | Path(".") |
||
378 | / "datasets" |
||
379 | / "gas_data" |
||
380 | / "data" |
||
381 | / "IGGIELGN_PipeSegments.csv" |
||
382 | ) |
||
383 | |||
384 | gas_pipelines_list = pd.read_csv( |
||
385 | target_file, |
||
386 | delimiter=";", |
||
387 | decimal=".", |
||
388 | usecols=["id", "node_id", "lat", "long", "country_code", "param"], |
||
389 | ) |
||
390 | |||
391 | # Select the links having at least one bus in Germany |
||
392 | gas_pipelines_list = gas_pipelines_list[ |
||
393 | gas_pipelines_list["country_code"].str.contains("DE") |
||
394 | ] |
||
395 | |||
396 | # Remove links disconnected of the rest of the grid |
||
397 | # Remove manually for disconnected link EntsoG_Map__ST_195 |
||
398 | gas_pipelines_list = gas_pipelines_list[ |
||
399 | ~gas_pipelines_list["id"].str.match("EntsoG_Map__ST_195") |
||
400 | ] |
||
401 | |||
402 | gas_pipelines_list["link_id"] = range( |
||
403 | new_id, new_id + len(gas_pipelines_list) |
||
404 | ) |
||
405 | gas_pipelines_list["link_id"] = gas_pipelines_list["link_id"].astype(int) |
||
406 | |||
407 | # Cut data to federal state if in testmode |
||
408 | NUTS1 = [] |
||
409 | for index, row in gas_pipelines_list.iterrows(): |
||
410 | param = ast.literal_eval(row["param"]) |
||
411 | NUTS1.append(param["nuts_id_1"]) |
||
412 | gas_pipelines_list["NUTS1"] = NUTS1 |
||
413 | |||
414 | map_states = { |
||
415 | "Baden-Württemberg": "DE1", |
||
416 | "Nordrhein-Westfalen": "DEA", |
||
417 | "Hessen": "DE7", |
||
418 | "Brandenburg": "DE4", |
||
419 | "Bremen": "DE5", |
||
420 | "Rheinland-Pfalz": "DEB", |
||
421 | "Sachsen-Anhalt": "DEE", |
||
422 | "Schleswig-Holstein": "DEF", |
||
423 | "Mecklenburg-Vorpommern": "DE8", |
||
424 | "Thüringen": "DEG", |
||
425 | "Niedersachsen": "DE9", |
||
426 | "Sachsen": "DED", |
||
427 | "Hamburg": "DE6", |
||
428 | "Saarland": "DEC", |
||
429 | "Berlin": "DE3", |
||
430 | "Bayern": "DE2", |
||
431 | "Everything": "Nan", |
||
432 | } |
||
433 | gas_pipelines_list["NUTS1_0"] = [x[0] for x in gas_pipelines_list["NUTS1"]] |
||
434 | gas_pipelines_list["NUTS1_1"] = [x[1] for x in gas_pipelines_list["NUTS1"]] |
||
435 | |||
436 | boundary = settings()["egon-data"]["--dataset-boundary"] |
||
437 | |||
438 | if boundary != "Everything": |
||
439 | |||
440 | gas_pipelines_list = gas_pipelines_list[ |
||
441 | gas_pipelines_list["NUTS1_0"].str.contains(map_states[boundary]) |
||
442 | | gas_pipelines_list["NUTS1_1"].str.contains(map_states[boundary]) |
||
443 | ] |
||
444 | |||
445 | # Add missing columns |
||
446 | gas_pipelines_list["scn_name"] = scn_name |
||
447 | gas_pipelines_list["carrier"] = main_gas_carrier |
||
448 | gas_pipelines_list["p_nom_extendable"] = False |
||
449 | |||
450 | diameter = [] |
||
451 | geom = [] |
||
452 | topo = [] |
||
453 | length_km = [] |
||
454 | |||
455 | for index, row in gas_pipelines_list.iterrows(): |
||
456 | |||
457 | param = ast.literal_eval(row["param"]) |
||
458 | diameter.append(param["diameter_mm"]) |
||
459 | length_km.append(param["length_km"]) |
||
460 | |||
461 | long_e = json.loads(row["long"]) |
||
462 | lat_e = json.loads(row["lat"]) |
||
463 | crd_e = list(zip(long_e, lat_e)) |
||
464 | topo.append(geometry.LineString(crd_e)) |
||
465 | |||
466 | long_path = param["path_long"] |
||
467 | lat_path = param["path_lat"] |
||
468 | crd = list(zip(long_path, lat_path)) |
||
469 | crd.insert(0, crd_e[0]) |
||
470 | crd.append(crd_e[1]) |
||
471 | lines = [] |
||
472 | for i in range(len(crd) - 1): |
||
473 | lines.append(geometry.LineString([crd[i], crd[i + 1]])) |
||
474 | geom.append(geometry.MultiLineString(lines)) |
||
475 | |||
476 | gas_pipelines_list["diameter"] = diameter |
||
477 | gas_pipelines_list["geom"] = geom |
||
478 | gas_pipelines_list["topo"] = topo |
||
479 | gas_pipelines_list["length_km"] = length_km |
||
480 | gas_pipelines_list = gas_pipelines_list.set_geometry("geom", crs=4326) |
||
481 | |||
482 | country_0 = [] |
||
483 | country_1 = [] |
||
484 | for index, row in gas_pipelines_list.iterrows(): |
||
485 | c = ast.literal_eval(row["country_code"]) |
||
486 | country_0.append(c[0]) |
||
487 | country_1.append(c[1]) |
||
488 | |||
489 | gas_pipelines_list["country_0"] = country_0 |
||
490 | gas_pipelines_list["country_1"] = country_1 |
||
491 | |||
492 | # Correct non valid neighbouring country nodes |
||
493 | gas_pipelines_list.loc[ |
||
494 | gas_pipelines_list["country_0"] == "XX", "country_0" |
||
495 | ] = "NO" |
||
496 | gas_pipelines_list.loc[ |
||
497 | gas_pipelines_list["country_1"] == "FI", "country_1" |
||
498 | ] = "RU" |
||
499 | gas_pipelines_list.loc[ |
||
500 | gas_pipelines_list["id"] == "ST_2612_Seg_0_Seg_0", "country_0" |
||
501 | ] = "AT" # bus "INET_N_1182" DE -> AT |
||
502 | gas_pipelines_list.loc[ |
||
503 | gas_pipelines_list["id"] == "INET_PL_385_EE_3_Seg_0_Seg_1", "country_1" |
||
504 | ] = "AT" # "INET_N_1182" DE -> AT |
||
505 | gas_pipelines_list.loc[ |
||
506 | gas_pipelines_list["id"] == "LKD_PS_0_Seg_0_Seg_3", "country_0" |
||
507 | ] = "NL" # bus "SEQ_10608_p" DE -> NL |
||
508 | |||
509 | # Remove uncorrect pipelines |
||
510 | gas_pipelines_list = gas_pipelines_list[ |
||
511 | (gas_pipelines_list["id"] != "PLNG_2637_Seg_0_Seg_0_Seg_0") |
||
512 | & (gas_pipelines_list["id"] != "NSG_6650_Seg_2_Seg_0") |
||
513 | & (gas_pipelines_list["id"] != "NSG_6734_Seg_2_Seg_0") |
||
514 | ] |
||
515 | |||
516 | # Remove link test if length = 0 |
||
517 | gas_pipelines_list = gas_pipelines_list[ |
||
518 | gas_pipelines_list["length_km"] != 0 |
||
519 | ] |
||
520 | |||
521 | # Adjust columns |
||
522 | bus0 = [] |
||
523 | bus1 = [] |
||
524 | geom_adjusted = [] |
||
525 | topo_adjusted = [] |
||
526 | length_adjusted = [] |
||
527 | pipe_class = [] |
||
528 | |||
529 | for index, row in gas_pipelines_list.iterrows(): |
||
530 | buses = row["node_id"].strip("][").split(", ") |
||
531 | |||
532 | if ( |
||
533 | (boundary != "Everything") |
||
534 | & (row["NUTS1_0"] != map_states[boundary]) |
||
535 | & (row["country_0"] == "DE") |
||
536 | ): |
||
537 | bus0.append(abroad_gas_nodes_list.loc["DE", "bus_id"]) |
||
538 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
539 | long_e = [ |
||
540 | abroad_gas_nodes_list.loc["DE", "x"], |
||
541 | json.loads(row["long"])[1], |
||
542 | ] |
||
543 | lat_e = [ |
||
544 | abroad_gas_nodes_list.loc["DE", "y"], |
||
545 | json.loads(row["lat"])[1], |
||
546 | ] |
||
547 | geom_pipe = geometry.MultiLineString( |
||
548 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
549 | ) |
||
550 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
551 | |||
552 | elif row["country_0"] != "DE": |
||
553 | country = str(row["country_0"]) |
||
554 | bus0.append(abroad_gas_nodes_list.loc[country, "bus_id"]) |
||
555 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
556 | long_e = [ |
||
557 | abroad_gas_nodes_list.loc[country, "x"], |
||
558 | json.loads(row["long"])[1], |
||
559 | ] |
||
560 | lat_e = [ |
||
561 | abroad_gas_nodes_list.loc[country, "y"], |
||
562 | json.loads(row["lat"])[1], |
||
563 | ] |
||
564 | geom_pipe = geometry.MultiLineString( |
||
565 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
566 | ) |
||
567 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
568 | |||
569 | elif ( |
||
570 | (boundary != "Everything") |
||
571 | & (row["NUTS1_1"] != map_states[boundary]) |
||
572 | & (row["country_1"] == "DE") |
||
573 | ): |
||
574 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
575 | bus1.append(abroad_gas_nodes_list.loc["DE", "bus_id"]) |
||
576 | long_e = [ |
||
577 | json.loads(row["long"])[0], |
||
578 | abroad_gas_nodes_list.loc["DE", "x"], |
||
579 | ] |
||
580 | lat_e = [ |
||
581 | json.loads(row["lat"])[0], |
||
582 | abroad_gas_nodes_list.loc["DE", "y"], |
||
583 | ] |
||
584 | geom_pipe = geometry.MultiLineString( |
||
585 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
586 | ) |
||
587 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
588 | |||
589 | elif row["country_1"] != "DE": |
||
590 | country = str(row["country_1"]) |
||
591 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
592 | bus1.append(abroad_gas_nodes_list.loc[country, "bus_id"]) |
||
593 | long_e = [ |
||
594 | json.loads(row["long"])[0], |
||
595 | abroad_gas_nodes_list.loc[country, "x"], |
||
596 | ] |
||
597 | lat_e = [ |
||
598 | json.loads(row["lat"])[0], |
||
599 | abroad_gas_nodes_list.loc[country, "y"], |
||
600 | ] |
||
601 | geom_pipe = geometry.MultiLineString( |
||
602 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
603 | ) |
||
604 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
605 | |||
606 | else: |
||
607 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
608 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
609 | geom_pipe = row["geom"] |
||
610 | topo_adjusted.append(row["topo"]) |
||
611 | |||
612 | geom_adjusted.append(geom_pipe) |
||
613 | length_adjusted.append(geom_pipe.length) |
||
614 | |||
615 | if row["diameter"] >= 1000: |
||
616 | pipe_class = "A" |
||
617 | elif 700 <= row["diameter"] <= 1000: |
||
618 | pipe_class = "B" |
||
619 | elif 500 <= row["diameter"] <= 700: |
||
620 | pipe_class = "C" |
||
621 | elif 350 <= row["diameter"] <= 500: |
||
622 | pipe_class = "D" |
||
623 | elif 200 <= row["diameter"] <= 350: |
||
624 | pipe_class = "E" |
||
625 | elif 100 <= row["diameter"] <= 200: |
||
626 | pipe_class = "F" |
||
627 | elif row["diameter"] <= 100: |
||
628 | pipe_class = "G" |
||
629 | |||
630 | gas_pipelines_list["bus0"] = bus0 |
||
631 | gas_pipelines_list["bus1"] = bus1 |
||
632 | gas_pipelines_list["geom"] = geom_adjusted |
||
633 | gas_pipelines_list["topo"] = topo_adjusted |
||
634 | gas_pipelines_list["length"] = length_adjusted |
||
635 | gas_pipelines_list["pipe_class"] = pipe_class |
||
636 | |||
637 | # Remove pipes having the same node for start and end |
||
638 | gas_pipelines_list = gas_pipelines_list[ |
||
639 | gas_pipelines_list["bus0"] != gas_pipelines_list["bus1"] |
||
640 | ] |
||
641 | |||
642 | gas_pipelines_list = gas_pipelines_list.merge( |
||
643 | classification, |
||
644 | how="left", |
||
645 | left_on="pipe_class", |
||
646 | right_on="classification", |
||
647 | ) |
||
648 | gas_pipelines_list["p_nom"] = gas_pipelines_list[ |
||
649 | "max_transport_capacity_Gwh/d" |
||
650 | ] * (1000 / 24) |
||
651 | |||
652 | # Remove useless columns |
||
653 | gas_pipelines_list = gas_pipelines_list.drop( |
||
654 | columns=[ |
||
655 | "id", |
||
656 | "node_id", |
||
657 | "param", |
||
658 | "NUTS1", |
||
659 | "NUTS1_0", |
||
660 | "NUTS1_1", |
||
661 | "country_code", |
||
662 | "diameter", |
||
663 | "pipe_class", |
||
664 | "classification", |
||
665 | "max_transport_capacity_Gwh/d", |
||
666 | "lat", |
||
667 | "long", |
||
668 | "length_km", |
||
669 | ] |
||
670 | ) |
||
671 | |||
672 | return gas_pipelines_list |
||
673 | |||
852 |