| 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 -*- |
||
| 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 | |||
| 421 | # Remove links disconnected of the rest of the grid |
||
| 422 | # Remove manually for disconnected link EntsoG_Map__ST_195 |
||
| 423 | gas_pipelines_list = gas_pipelines_list[ |
||
| 424 | ~gas_pipelines_list["id"].str.match("EntsoG_Map__ST_195")
|
||
| 425 | ] |
||
| 426 | |||
| 427 | gas_pipelines_list["link_id"] = range( |
||
| 428 | new_id, new_id + len(gas_pipelines_list) |
||
| 429 | ) |
||
| 430 | gas_pipelines_list["link_id"] = gas_pipelines_list["link_id"].astype(int) |
||
| 431 | |||
| 432 | # Cut data to federal state if in testmode |
||
| 433 | NUTS1 = [] |
||
| 434 | for index, row in gas_pipelines_list.iterrows(): |
||
| 435 | param = ast.literal_eval(row["param"]) |
||
| 436 | NUTS1.append(param["nuts_id_1"]) |
||
| 437 | gas_pipelines_list["NUTS1"] = NUTS1 |
||
| 438 | |||
| 439 | map_states = {
|
||
| 440 | "Baden-Württemberg": "DE1", |
||
| 441 | "Nordrhein-Westfalen": "DEA", |
||
| 442 | "Hessen": "DE7", |
||
| 443 | "Brandenburg": "DE4", |
||
| 444 | "Bremen": "DE5", |
||
| 445 | "Rheinland-Pfalz": "DEB", |
||
| 446 | "Sachsen-Anhalt": "DEE", |
||
| 447 | "Schleswig-Holstein": "DEF", |
||
| 448 | "Mecklenburg-Vorpommern": "DE8", |
||
| 449 | "Thüringen": "DEG", |
||
| 450 | "Niedersachsen": "DE9", |
||
| 451 | "Sachsen": "DED", |
||
| 452 | "Hamburg": "DE6", |
||
| 453 | "Saarland": "DEC", |
||
| 454 | "Berlin": "DE3", |
||
| 455 | "Bayern": "DE2", |
||
| 456 | "Everything": "Nan", |
||
| 457 | } |
||
| 458 | gas_pipelines_list["NUTS1_0"] = [x[0] for x in gas_pipelines_list["NUTS1"]] |
||
| 459 | gas_pipelines_list["NUTS1_1"] = [x[1] for x in gas_pipelines_list["NUTS1"]] |
||
| 460 | |||
| 461 | boundary = settings()["egon-data"]["--dataset-boundary"] |
||
| 462 | |||
| 463 | if boundary != "Everything": |
||
| 464 | |||
| 465 | gas_pipelines_list = gas_pipelines_list[ |
||
| 466 | gas_pipelines_list["NUTS1_0"].str.contains(map_states[boundary]) |
||
| 467 | | gas_pipelines_list["NUTS1_1"].str.contains(map_states[boundary]) |
||
| 468 | ] |
||
| 469 | |||
| 470 | # Add missing columns |
||
| 471 | gas_pipelines_list["scn_name"] = scn_name |
||
| 472 | gas_pipelines_list["carrier"] = main_gas_carrier |
||
| 473 | gas_pipelines_list["p_nom_extendable"] = False |
||
| 474 | |||
| 475 | diameter = [] |
||
| 476 | geom = [] |
||
| 477 | topo = [] |
||
| 478 | length_km = [] |
||
| 479 | |||
| 480 | for index, row in gas_pipelines_list.iterrows(): |
||
| 481 | |||
| 482 | param = ast.literal_eval(row["param"]) |
||
| 483 | diameter.append(param["diameter_mm"]) |
||
| 484 | length_km.append(param["length_km"]) |
||
| 485 | |||
| 486 | long_e = json.loads(row["long"]) |
||
| 487 | lat_e = json.loads(row["lat"]) |
||
| 488 | crd_e = list(zip(long_e, lat_e)) |
||
| 489 | topo.append(geometry.LineString(crd_e)) |
||
| 490 | |||
| 491 | long_path = param["path_long"] |
||
| 492 | lat_path = param["path_lat"] |
||
| 493 | crd = list(zip(long_path, lat_path)) |
||
| 494 | crd.insert(0, crd_e[0]) |
||
| 495 | crd.append(crd_e[1]) |
||
| 496 | lines = [] |
||
| 497 | for i in range(len(crd) - 1): |
||
| 498 | lines.append(geometry.LineString([crd[i], crd[i + 1]])) |
||
| 499 | geom.append(geometry.MultiLineString(lines)) |
||
| 500 | |||
| 501 | gas_pipelines_list["diameter"] = diameter |
||
| 502 | gas_pipelines_list["geom"] = geom |
||
| 503 | gas_pipelines_list["topo"] = topo |
||
| 504 | gas_pipelines_list["length_km"] = length_km |
||
| 505 | gas_pipelines_list = gas_pipelines_list.set_geometry("geom", crs=4326)
|
||
| 506 | |||
| 507 | country_0 = [] |
||
| 508 | country_1 = [] |
||
| 509 | for index, row in gas_pipelines_list.iterrows(): |
||
| 510 | c = ast.literal_eval(row["country_code"]) |
||
| 511 | country_0.append(c[0]) |
||
| 512 | country_1.append(c[1]) |
||
| 513 | |||
| 514 | gas_pipelines_list["country_0"] = country_0 |
||
| 515 | gas_pipelines_list["country_1"] = country_1 |
||
| 516 | |||
| 517 | # Correct non valid neighbouring country nodes |
||
| 518 | gas_pipelines_list.loc[ |
||
| 519 | gas_pipelines_list["country_0"] == "XX", "country_0" |
||
| 520 | ] = "NO" |
||
| 521 | gas_pipelines_list.loc[ |
||
| 522 | gas_pipelines_list["country_1"] == "FI", "country_1" |
||
| 523 | ] = "RU" |
||
| 524 | gas_pipelines_list.loc[ |
||
| 525 | gas_pipelines_list["id"] == "ST_2612_Seg_0_Seg_0", "country_0" |
||
| 526 | ] = "AT" # bus "INET_N_1182" DE -> AT |
||
| 527 | gas_pipelines_list.loc[ |
||
| 528 | gas_pipelines_list["id"] == "INET_PL_385_EE_3_Seg_0_Seg_1", "country_1" |
||
| 529 | ] = "AT" # "INET_N_1182" DE -> AT |
||
| 530 | gas_pipelines_list.loc[ |
||
| 531 | gas_pipelines_list["id"] == "LKD_PS_0_Seg_0_Seg_3", "country_0" |
||
| 532 | ] = "NL" # bus "SEQ_10608_p" DE -> NL |
||
| 533 | |||
| 534 | # Remove uncorrect pipelines |
||
| 535 | gas_pipelines_list = gas_pipelines_list[ |
||
| 536 | (gas_pipelines_list["id"] != "PLNG_2637_Seg_0_Seg_0_Seg_0") |
||
| 537 | & (gas_pipelines_list["id"] != "NSG_6650_Seg_2_Seg_0") |
||
| 538 | & (gas_pipelines_list["id"] != "NSG_6734_Seg_2_Seg_0") |
||
| 539 | ] |
||
| 540 | |||
| 541 | # Remove link test if length = 0 |
||
| 542 | gas_pipelines_list = gas_pipelines_list[ |
||
| 543 | gas_pipelines_list["length_km"] != 0 |
||
| 544 | ] |
||
| 545 | |||
| 546 | # Adjust columns |
||
| 547 | bus0 = [] |
||
| 548 | bus1 = [] |
||
| 549 | geom_adjusted = [] |
||
| 550 | topo_adjusted = [] |
||
| 551 | length_adjusted = [] |
||
| 552 | pipe_class = [] |
||
| 553 | |||
| 554 | for index, row in gas_pipelines_list.iterrows(): |
||
| 555 | buses = row["node_id"].strip("][").split(", ")
|
||
| 556 | |||
| 557 | if ( |
||
| 558 | (boundary != "Everything") |
||
| 559 | & (row["NUTS1_0"] != map_states[boundary]) |
||
| 560 | & (row["country_0"] == "DE") |
||
| 561 | ): |
||
| 562 | bus0.append(abroad_gas_nodes_list.loc["DE", "bus_id"]) |
||
| 563 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
| 564 | long_e = [ |
||
| 565 | abroad_gas_nodes_list.loc["DE", "x"], |
||
| 566 | json.loads(row["long"])[1], |
||
| 567 | ] |
||
| 568 | lat_e = [ |
||
| 569 | abroad_gas_nodes_list.loc["DE", "y"], |
||
| 570 | json.loads(row["lat"])[1], |
||
| 571 | ] |
||
| 572 | geom_pipe = geometry.MultiLineString( |
||
| 573 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
| 574 | ) |
||
| 575 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
| 576 | |||
| 577 | elif row["country_0"] != "DE": |
||
| 578 | country = str(row["country_0"]) |
||
| 579 | bus0.append(abroad_gas_nodes_list.loc[country, "bus_id"]) |
||
| 580 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
| 581 | long_e = [ |
||
| 582 | abroad_gas_nodes_list.loc[country, "x"], |
||
| 583 | json.loads(row["long"])[1], |
||
| 584 | ] |
||
| 585 | lat_e = [ |
||
| 586 | abroad_gas_nodes_list.loc[country, "y"], |
||
| 587 | json.loads(row["lat"])[1], |
||
| 588 | ] |
||
| 589 | geom_pipe = geometry.MultiLineString( |
||
| 590 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
| 591 | ) |
||
| 592 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
| 593 | |||
| 594 | elif ( |
||
| 595 | (boundary != "Everything") |
||
| 596 | & (row["NUTS1_1"] != map_states[boundary]) |
||
| 597 | & (row["country_1"] == "DE") |
||
| 598 | ): |
||
| 599 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
| 600 | bus1.append(abroad_gas_nodes_list.loc["DE", "bus_id"]) |
||
| 601 | long_e = [ |
||
| 602 | json.loads(row["long"])[0], |
||
| 603 | abroad_gas_nodes_list.loc["DE", "x"], |
||
| 604 | ] |
||
| 605 | lat_e = [ |
||
| 606 | json.loads(row["lat"])[0], |
||
| 607 | abroad_gas_nodes_list.loc["DE", "y"], |
||
| 608 | ] |
||
| 609 | geom_pipe = geometry.MultiLineString( |
||
| 610 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
| 611 | ) |
||
| 612 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
| 613 | |||
| 614 | elif row["country_1"] != "DE": |
||
| 615 | country = str(row["country_1"]) |
||
| 616 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
| 617 | bus1.append(abroad_gas_nodes_list.loc[country, "bus_id"]) |
||
| 618 | long_e = [ |
||
| 619 | json.loads(row["long"])[0], |
||
| 620 | abroad_gas_nodes_list.loc[country, "x"], |
||
| 621 | ] |
||
| 622 | lat_e = [ |
||
| 623 | json.loads(row["lat"])[0], |
||
| 624 | abroad_gas_nodes_list.loc[country, "y"], |
||
| 625 | ] |
||
| 626 | geom_pipe = geometry.MultiLineString( |
||
| 627 | [geometry.LineString(list(zip(long_e, lat_e)))] |
||
| 628 | ) |
||
| 629 | topo_adjusted.append(geometry.LineString(list(zip(long_e, lat_e)))) |
||
| 630 | |||
| 631 | else: |
||
| 632 | bus0.append(gas_nodes_list.loc[buses[0][1:-1], "bus_id"]) |
||
| 633 | bus1.append(gas_nodes_list.loc[buses[1][1:-1], "bus_id"]) |
||
| 634 | geom_pipe = row["geom"] |
||
| 635 | topo_adjusted.append(row["topo"]) |
||
| 636 | |||
| 637 | geom_adjusted.append(geom_pipe) |
||
| 638 | length_adjusted.append(geom_pipe.length) |
||
| 639 | |||
| 640 | if row["diameter"] >= 1000: |
||
| 641 | pipe_class = "A" |
||
| 642 | elif 700 <= row["diameter"] <= 1000: |
||
| 643 | pipe_class = "B" |
||
| 644 | elif 500 <= row["diameter"] <= 700: |
||
| 645 | pipe_class = "C" |
||
| 646 | elif 350 <= row["diameter"] <= 500: |
||
| 647 | pipe_class = "D" |
||
| 648 | elif 200 <= row["diameter"] <= 350: |
||
| 649 | pipe_class = "E" |
||
| 650 | elif 100 <= row["diameter"] <= 200: |
||
| 651 | pipe_class = "F" |
||
| 652 | elif row["diameter"] <= 100: |
||
| 653 | pipe_class = "G" |
||
| 654 | |||
| 655 | gas_pipelines_list["bus0"] = bus0 |
||
| 656 | gas_pipelines_list["bus1"] = bus1 |
||
| 657 | gas_pipelines_list["geom"] = geom_adjusted |
||
| 658 | gas_pipelines_list["topo"] = topo_adjusted |
||
| 659 | gas_pipelines_list["length"] = length_adjusted |
||
| 660 | gas_pipelines_list["pipe_class"] = pipe_class |
||
| 661 | |||
| 662 | # Remove pipes having the same node for start and end |
||
| 663 | gas_pipelines_list = gas_pipelines_list[ |
||
| 664 | gas_pipelines_list["bus0"] != gas_pipelines_list["bus1"] |
||
| 665 | ] |
||
| 666 | |||
| 667 | gas_pipelines_list = gas_pipelines_list.merge( |
||
| 668 | classification, |
||
| 669 | how="left", |
||
| 670 | left_on="pipe_class", |
||
| 671 | right_on="classification", |
||
| 672 | ) |
||
| 673 | gas_pipelines_list["p_nom"] = gas_pipelines_list[ |
||
| 674 | "max_transport_capacity_Gwh/d" |
||
| 675 | ] * (1000 / 24) |
||
| 676 | |||
| 677 | # Remove useless columns |
||
| 678 | gas_pipelines_list = gas_pipelines_list.drop( |
||
| 679 | columns=[ |
||
| 680 | "id", |
||
| 681 | "node_id", |
||
| 682 | "param", |
||
| 683 | "NUTS1", |
||
| 684 | "NUTS1_0", |
||
| 685 | "NUTS1_1", |
||
| 686 | "country_code", |
||
| 687 | "diameter", |
||
| 688 | "pipe_class", |
||
| 689 | "classification", |
||
| 690 | "max_transport_capacity_Gwh/d", |
||
| 691 | "lat", |
||
| 692 | "long", |
||
| 693 | "length_km", |
||
| 694 | ] |
||
| 695 | ) |
||
| 696 | |||
| 697 | return gas_pipelines_list |
||
| 698 | |||
| 877 |