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