| Conditions | 34 |
| Total Lines | 869 |
| Code Lines | 579 |
| 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.pypsaeur.neighbor_reduction() 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 importing data from |
||
| 419 | def neighbor_reduction(): |
||
| 420 | network = read_network() |
||
| 421 | |||
| 422 | # network.links.drop("pipe_retrofit", axis="columns", inplace=True) |
||
| 423 | |||
| 424 | wanted_countries = [ |
||
| 425 | "DE", |
||
| 426 | "AT", |
||
| 427 | "CH", |
||
| 428 | "CZ", |
||
| 429 | "PL", |
||
| 430 | "SE", |
||
| 431 | "NO", |
||
| 432 | "DK", |
||
| 433 | "GB", |
||
| 434 | "NL", |
||
| 435 | "BE", |
||
| 436 | "FR", |
||
| 437 | "LU", |
||
| 438 | ] |
||
| 439 | foreign_buses = network.buses[ |
||
| 440 | ~network.buses.index.str.contains("|".join(wanted_countries)) |
||
| 441 | ] |
||
| 442 | network.buses = network.buses.drop( |
||
| 443 | network.buses.loc[foreign_buses.index].index |
||
| 444 | ) |
||
| 445 | |||
| 446 | # drop foreign lines and links from the 2nd row |
||
| 447 | |||
| 448 | network.lines = network.lines.drop( |
||
| 449 | network.lines[ |
||
| 450 | (network.lines["bus0"].isin(network.buses.index) == False) |
||
| 451 | & (network.lines["bus1"].isin(network.buses.index) == False) |
||
| 452 | ].index |
||
| 453 | ) |
||
| 454 | |||
| 455 | # select all lines which have at bus1 the bus which is kept |
||
| 456 | lines_cb_1 = network.lines[ |
||
| 457 | (network.lines["bus0"].isin(network.buses.index) == False) |
||
| 458 | ] |
||
| 459 | |||
| 460 | # create a load at bus1 with the line's hourly loading |
||
| 461 | for i, k in zip(lines_cb_1.bus1.values, lines_cb_1.index): |
||
| 462 | network.add( |
||
| 463 | "Load", |
||
| 464 | "slack_fix " + i + " " + k, |
||
| 465 | bus=i, |
||
| 466 | p_set=network.lines_t.p1[k], |
||
| 467 | ) |
||
| 468 | network.loads.carrier.loc["slack_fix " + i + " " + k] = ( |
||
| 469 | lines_cb_1.carrier[k] |
||
| 470 | ) |
||
| 471 | |||
| 472 | # select all lines which have at bus0 the bus which is kept |
||
| 473 | lines_cb_0 = network.lines[ |
||
| 474 | (network.lines["bus1"].isin(network.buses.index) == False) |
||
| 475 | ] |
||
| 476 | |||
| 477 | # create a load at bus0 with the line's hourly loading |
||
| 478 | for i, k in zip(lines_cb_0.bus0.values, lines_cb_0.index): |
||
| 479 | network.add( |
||
| 480 | "Load", |
||
| 481 | "slack_fix " + i + " " + k, |
||
| 482 | bus=i, |
||
| 483 | p_set=network.lines_t.p0[k], |
||
| 484 | ) |
||
| 485 | network.loads.carrier.loc["slack_fix " + i + " " + k] = ( |
||
| 486 | lines_cb_0.carrier[k] |
||
| 487 | ) |
||
| 488 | |||
| 489 | # do the same for links |
||
| 490 | |||
| 491 | network.links = network.links.drop( |
||
| 492 | network.links[ |
||
| 493 | (network.links["bus0"].isin(network.buses.index) == False) |
||
| 494 | & (network.links["bus1"].isin(network.buses.index) == False) |
||
| 495 | ].index |
||
| 496 | ) |
||
| 497 | |||
| 498 | # select all links which have at bus1 the bus which is kept |
||
| 499 | links_cb_1 = network.links[ |
||
| 500 | (network.links["bus0"].isin(network.buses.index) == False) |
||
| 501 | ] |
||
| 502 | |||
| 503 | # create a load at bus1 with the link's hourly loading |
||
| 504 | for i, k in zip(links_cb_1.bus1.values, links_cb_1.index): |
||
| 505 | network.add( |
||
| 506 | "Load", |
||
| 507 | "slack_fix_links " + i + " " + k, |
||
| 508 | bus=i, |
||
| 509 | p_set=network.links_t.p1[k], |
||
| 510 | ) |
||
| 511 | network.loads.carrier.loc["slack_fix_links " + i + " " + k] = ( |
||
| 512 | links_cb_1.carrier[k] |
||
| 513 | ) |
||
| 514 | |||
| 515 | # select all links which have at bus0 the bus which is kept |
||
| 516 | links_cb_0 = network.links[ |
||
| 517 | (network.links["bus1"].isin(network.buses.index) == False) |
||
| 518 | ] |
||
| 519 | |||
| 520 | # create a load at bus0 with the link's hourly loading |
||
| 521 | for i, k in zip(links_cb_0.bus0.values, links_cb_0.index): |
||
| 522 | network.add( |
||
| 523 | "Load", |
||
| 524 | "slack_fix_links " + i + " " + k, |
||
| 525 | bus=i, |
||
| 526 | p_set=network.links_t.p0[k], |
||
| 527 | ) |
||
| 528 | network.loads.carrier.loc["slack_fix_links " + i + " " + k] = ( |
||
| 529 | links_cb_0.carrier[k] |
||
| 530 | ) |
||
| 531 | |||
| 532 | # drop remaining foreign components |
||
| 533 | |||
| 534 | network.lines = network.lines.drop( |
||
| 535 | network.lines[ |
||
| 536 | (network.lines["bus0"].isin(network.buses.index) == False) |
||
| 537 | | (network.lines["bus1"].isin(network.buses.index) == False) |
||
| 538 | ].index |
||
| 539 | ) |
||
| 540 | |||
| 541 | network.links = network.links.drop( |
||
| 542 | network.links[ |
||
| 543 | (network.links["bus0"].isin(network.buses.index) == False) |
||
| 544 | | (network.links["bus1"].isin(network.buses.index) == False) |
||
| 545 | ].index |
||
| 546 | ) |
||
| 547 | |||
| 548 | network.transformers = network.transformers.drop( |
||
| 549 | network.transformers[ |
||
| 550 | (network.transformers["bus0"].isin(network.buses.index) == False) |
||
| 551 | | (network.transformers["bus1"].isin(network.buses.index) == False) |
||
| 552 | ].index |
||
| 553 | ) |
||
| 554 | network.generators = network.generators.drop( |
||
| 555 | network.generators[ |
||
| 556 | (network.generators["bus"].isin(network.buses.index) == False) |
||
| 557 | ].index |
||
| 558 | ) |
||
| 559 | |||
| 560 | network.loads = network.loads.drop( |
||
| 561 | network.loads[ |
||
| 562 | (network.loads["bus"].isin(network.buses.index) == False) |
||
| 563 | ].index |
||
| 564 | ) |
||
| 565 | |||
| 566 | network.storage_units = network.storage_units.drop( |
||
| 567 | network.storage_units[ |
||
| 568 | (network.storage_units["bus"].isin(network.buses.index) == False) |
||
| 569 | ].index |
||
| 570 | ) |
||
| 571 | |||
| 572 | components = [ |
||
| 573 | "loads", |
||
| 574 | "generators", |
||
| 575 | "lines", |
||
| 576 | "buses", |
||
| 577 | "transformers", |
||
| 578 | "links", |
||
| 579 | ] |
||
| 580 | for g in components: # loads_t |
||
| 581 | h = g + "_t" |
||
| 582 | nw = getattr(network, h) # network.loads_t |
||
| 583 | for i in nw.keys(): # network.loads_t.p |
||
| 584 | cols = [ |
||
| 585 | j |
||
| 586 | for j in getattr(nw, i).columns |
||
| 587 | if j not in getattr(network, g).index |
||
| 588 | ] |
||
| 589 | for k in cols: |
||
| 590 | del getattr(nw, i)[k] |
||
| 591 | |||
| 592 | # writing components of neighboring countries to etrago tables |
||
| 593 | |||
| 594 | # Set country tag for all buses |
||
| 595 | network.buses.country = network.buses.index.str[:2] |
||
| 596 | neighbors = network.buses[network.buses.country != "DE"] |
||
| 597 | |||
| 598 | neighbors["new_index"] = ( |
||
| 599 | db.next_etrago_id("bus") + neighbors.reset_index().index |
||
| 600 | ) |
||
| 601 | |||
| 602 | # Use index of AC buses created by electrical_neigbors |
||
| 603 | foreign_ac_buses = db.select_dataframe( |
||
| 604 | """ |
||
| 605 | SELECT * FROM grid.egon_etrago_bus |
||
| 606 | WHERE carrier = 'AC' AND v_nom = 380 |
||
| 607 | AND country!= 'DE' AND scn_name ='eGon100RE' |
||
| 608 | AND bus_id NOT IN (SELECT bus_i FROM osmtgmod_results.bus_data) |
||
| 609 | """ |
||
| 610 | ) |
||
| 611 | buses_with_defined_id = neighbors[ |
||
| 612 | (neighbors.carrier == "AC") |
||
| 613 | & (neighbors.country.isin(foreign_ac_buses.country.values)) |
||
| 614 | ].index |
||
| 615 | neighbors.loc[buses_with_defined_id, "new_index"] = ( |
||
| 616 | foreign_ac_buses.set_index("x") |
||
| 617 | .loc[neighbors.loc[buses_with_defined_id, "x"]] |
||
| 618 | .bus_id.values |
||
| 619 | ) |
||
| 620 | |||
| 621 | # lines, the foreign crossborder lines |
||
| 622 | # (without crossborder lines to Germany!) |
||
| 623 | |||
| 624 | neighbor_lines = network.lines[ |
||
| 625 | network.lines.bus0.isin(neighbors.index) |
||
| 626 | & network.lines.bus1.isin(neighbors.index) |
||
| 627 | ] |
||
| 628 | if not network.lines_t["s_max_pu"].empty: |
||
| 629 | neighbor_lines_t = network.lines_t["s_max_pu"][neighbor_lines.index] |
||
| 630 | |||
| 631 | neighbor_lines.reset_index(inplace=True) |
||
| 632 | neighbor_lines.bus0 = ( |
||
| 633 | neighbors.loc[neighbor_lines.bus0, "new_index"].reset_index().new_index |
||
| 634 | ) |
||
| 635 | neighbor_lines.bus1 = ( |
||
| 636 | neighbors.loc[neighbor_lines.bus1, "new_index"].reset_index().new_index |
||
| 637 | ) |
||
| 638 | neighbor_lines.index += db.next_etrago_id("line") |
||
| 639 | |||
| 640 | if not network.lines_t["s_max_pu"].empty: |
||
| 641 | for i in neighbor_lines_t.columns: |
||
| 642 | new_index = neighbor_lines[neighbor_lines["name"] == i].index |
||
| 643 | neighbor_lines_t.rename(columns={i: new_index[0]}, inplace=True) |
||
| 644 | |||
| 645 | # links |
||
| 646 | neighbor_links = network.links[ |
||
| 647 | network.links.bus0.isin(neighbors.index) |
||
| 648 | & network.links.bus1.isin(neighbors.index) |
||
| 649 | ] |
||
| 650 | |||
| 651 | neighbor_links.reset_index(inplace=True) |
||
| 652 | neighbor_links.bus0 = ( |
||
| 653 | neighbors.loc[neighbor_links.bus0, "new_index"].reset_index().new_index |
||
| 654 | ) |
||
| 655 | neighbor_links.bus1 = ( |
||
| 656 | neighbors.loc[neighbor_links.bus1, "new_index"].reset_index().new_index |
||
| 657 | ) |
||
| 658 | neighbor_links.index += db.next_etrago_id("link") |
||
| 659 | |||
| 660 | # generators |
||
| 661 | neighbor_gens = network.generators[ |
||
| 662 | network.generators.bus.isin(neighbors.index) |
||
| 663 | ] |
||
| 664 | neighbor_gens_t = network.generators_t["p_max_pu"][ |
||
| 665 | neighbor_gens[ |
||
| 666 | neighbor_gens.index.isin(network.generators_t["p_max_pu"].columns) |
||
| 667 | ].index |
||
| 668 | ] |
||
| 669 | |||
| 670 | neighbor_gens.reset_index(inplace=True) |
||
| 671 | neighbor_gens.bus = ( |
||
| 672 | neighbors.loc[neighbor_gens.bus, "new_index"].reset_index().new_index |
||
| 673 | ) |
||
| 674 | neighbor_gens.index += db.next_etrago_id("generator") |
||
| 675 | |||
| 676 | for i in neighbor_gens_t.columns: |
||
| 677 | new_index = neighbor_gens[neighbor_gens["Generator"] == i].index |
||
| 678 | neighbor_gens_t.rename(columns={i: new_index[0]}, inplace=True) |
||
| 679 | |||
| 680 | # loads |
||
| 681 | |||
| 682 | neighbor_loads = network.loads[network.loads.bus.isin(neighbors.index)] |
||
| 683 | neighbor_loads_t_index = neighbor_loads.index[ |
||
| 684 | neighbor_loads.index.isin(network.loads_t.p_set.columns) |
||
| 685 | ] |
||
| 686 | neighbor_loads_t = network.loads_t["p_set"][neighbor_loads_t_index] |
||
| 687 | |||
| 688 | neighbor_loads.reset_index(inplace=True) |
||
| 689 | neighbor_loads.bus = ( |
||
| 690 | neighbors.loc[neighbor_loads.bus, "new_index"].reset_index().new_index |
||
| 691 | ) |
||
| 692 | neighbor_loads.index += db.next_etrago_id("load") |
||
| 693 | |||
| 694 | for i in neighbor_loads_t.columns: |
||
| 695 | new_index = neighbor_loads[neighbor_loads["Load"] == i].index |
||
| 696 | neighbor_loads_t.rename(columns={i: new_index[0]}, inplace=True) |
||
| 697 | |||
| 698 | # stores |
||
| 699 | neighbor_stores = network.stores[network.stores.bus.isin(neighbors.index)] |
||
| 700 | neighbor_stores_t_index = neighbor_stores.index[ |
||
| 701 | neighbor_stores.index.isin(network.stores_t.e_min_pu.columns) |
||
| 702 | ] |
||
| 703 | neighbor_stores_t = network.stores_t["e_min_pu"][neighbor_stores_t_index] |
||
| 704 | |||
| 705 | neighbor_stores.reset_index(inplace=True) |
||
| 706 | neighbor_stores.bus = ( |
||
| 707 | neighbors.loc[neighbor_stores.bus, "new_index"].reset_index().new_index |
||
| 708 | ) |
||
| 709 | neighbor_stores.index += db.next_etrago_id("store") |
||
| 710 | |||
| 711 | for i in neighbor_stores_t.columns: |
||
| 712 | new_index = neighbor_stores[neighbor_stores["Store"] == i].index |
||
| 713 | neighbor_stores_t.rename(columns={i: new_index[0]}, inplace=True) |
||
| 714 | |||
| 715 | # storage_units |
||
| 716 | neighbor_storage = network.storage_units[ |
||
| 717 | network.storage_units.bus.isin(neighbors.index) |
||
| 718 | ] |
||
| 719 | neighbor_storage_t_index = neighbor_storage.index[ |
||
| 720 | neighbor_storage.index.isin(network.storage_units_t.inflow.columns) |
||
| 721 | ] |
||
| 722 | neighbor_storage_t = network.storage_units_t["inflow"][ |
||
| 723 | neighbor_storage_t_index |
||
| 724 | ] |
||
| 725 | |||
| 726 | neighbor_storage.reset_index(inplace=True) |
||
| 727 | neighbor_storage.bus = ( |
||
| 728 | neighbors.loc[neighbor_storage.bus, "new_index"] |
||
| 729 | .reset_index() |
||
| 730 | .new_index |
||
| 731 | ) |
||
| 732 | neighbor_storage.index += db.next_etrago_id("storage") |
||
| 733 | |||
| 734 | for i in neighbor_storage_t.columns: |
||
| 735 | new_index = neighbor_storage[ |
||
| 736 | neighbor_storage["StorageUnit"] == i |
||
| 737 | ].index |
||
| 738 | neighbor_storage_t.rename(columns={i: new_index[0]}, inplace=True) |
||
| 739 | |||
| 740 | # Connect to local database |
||
| 741 | engine = db.engine() |
||
| 742 | |||
| 743 | neighbors["scn_name"] = "eGon100RE" |
||
| 744 | neighbors.index = neighbors["new_index"] |
||
| 745 | |||
| 746 | # Correct geometry for non AC buses |
||
| 747 | carriers = set(neighbors.carrier.to_list()) |
||
| 748 | carriers = [e for e in carriers if e not in ("AC", "biogas")] |
||
| 749 | non_AC_neighbors = pd.DataFrame() |
||
| 750 | for c in carriers: |
||
| 751 | c_neighbors = neighbors[neighbors.carrier == c].set_index( |
||
| 752 | "location", drop=False |
||
| 753 | ) |
||
| 754 | for i in ["x", "y"]: |
||
| 755 | c_neighbors = c_neighbors.drop(i, axis=1) |
||
| 756 | coordinates = neighbors[neighbors.carrier == "AC"][ |
||
| 757 | ["location", "x", "y"] |
||
| 758 | ].set_index("location") |
||
| 759 | c_neighbors = pd.concat([coordinates, c_neighbors], axis=1).set_index( |
||
| 760 | "new_index", drop=False |
||
| 761 | ) |
||
| 762 | non_AC_neighbors = pd.concat([non_AC_neighbors, c_neighbors]) |
||
| 763 | neighbors = pd.concat( |
||
| 764 | [neighbors[neighbors.carrier == "AC"], non_AC_neighbors] |
||
| 765 | ) |
||
| 766 | |||
| 767 | for i in [ |
||
| 768 | "new_index", |
||
| 769 | "control", |
||
| 770 | "generator", |
||
| 771 | "location", |
||
| 772 | "sub_network", |
||
| 773 | "unit", |
||
| 774 | ]: |
||
| 775 | neighbors = neighbors.drop(i, axis=1) |
||
| 776 | |||
| 777 | # Add geometry column |
||
| 778 | neighbors = ( |
||
| 779 | gpd.GeoDataFrame( |
||
| 780 | neighbors, geometry=gpd.points_from_xy(neighbors.x, neighbors.y) |
||
| 781 | ) |
||
| 782 | .rename_geometry("geom") |
||
| 783 | .set_crs(4326) |
||
| 784 | ) |
||
| 785 | |||
| 786 | # Unify carrier names |
||
| 787 | neighbors.carrier = neighbors.carrier.str.replace(" ", "_") |
||
| 788 | neighbors.carrier.replace( |
||
| 789 | { |
||
| 790 | "gas": "CH4", |
||
| 791 | "gas_for_industry": "CH4_for_industry", |
||
| 792 | }, |
||
| 793 | inplace=True, |
||
| 794 | ) |
||
| 795 | |||
| 796 | neighbors[~neighbors.carrier.isin(["AC"])].to_postgis( |
||
| 797 | "egon_etrago_bus", |
||
| 798 | engine, |
||
| 799 | schema="grid", |
||
| 800 | if_exists="append", |
||
| 801 | index=True, |
||
| 802 | index_label="bus_id", |
||
| 803 | ) |
||
| 804 | |||
| 805 | # prepare and write neighboring crossborder lines to etrago tables |
||
| 806 | def lines_to_etrago(neighbor_lines=neighbor_lines, scn="eGon100RE"): |
||
| 807 | neighbor_lines["scn_name"] = scn |
||
| 808 | neighbor_lines["cables"] = 3 * neighbor_lines["num_parallel"].astype( |
||
| 809 | int |
||
| 810 | ) |
||
| 811 | neighbor_lines["s_nom"] = neighbor_lines["s_nom_min"] |
||
| 812 | |||
| 813 | for i in [ |
||
| 814 | "Line", |
||
| 815 | "x_pu_eff", |
||
| 816 | "r_pu_eff", |
||
| 817 | "sub_network", |
||
| 818 | "x_pu", |
||
| 819 | "r_pu", |
||
| 820 | "g_pu", |
||
| 821 | "b_pu", |
||
| 822 | "s_nom_opt", |
||
| 823 | "i_nom", |
||
| 824 | ]: |
||
| 825 | neighbor_lines = neighbor_lines.drop(i, axis=1) |
||
| 826 | |||
| 827 | # Define geometry and add to lines dataframe as 'topo' |
||
| 828 | gdf = gpd.GeoDataFrame(index=neighbor_lines.index) |
||
| 829 | gdf["geom_bus0"] = neighbors.geom[neighbor_lines.bus0].values |
||
| 830 | gdf["geom_bus1"] = neighbors.geom[neighbor_lines.bus1].values |
||
| 831 | gdf["geometry"] = gdf.apply( |
||
| 832 | lambda x: LineString([x["geom_bus0"], x["geom_bus1"]]), axis=1 |
||
| 833 | ) |
||
| 834 | |||
| 835 | neighbor_lines = ( |
||
| 836 | gpd.GeoDataFrame(neighbor_lines, geometry=gdf["geometry"]) |
||
| 837 | .rename_geometry("topo") |
||
| 838 | .set_crs(4326) |
||
| 839 | ) |
||
| 840 | |||
| 841 | neighbor_lines["lifetime"] = get_sector_parameters("electricity", scn)[ |
||
| 842 | "lifetime" |
||
| 843 | ]["ac_ehv_overhead_line"] |
||
| 844 | |||
| 845 | neighbor_lines.to_postgis( |
||
| 846 | "egon_etrago_line", |
||
| 847 | engine, |
||
| 848 | schema="grid", |
||
| 849 | if_exists="append", |
||
| 850 | index=True, |
||
| 851 | index_label="line_id", |
||
| 852 | ) |
||
| 853 | |||
| 854 | lines_to_etrago(neighbor_lines=neighbor_lines, scn="eGon100RE") |
||
| 855 | |||
| 856 | def links_to_etrago(neighbor_links, scn="eGon100RE", extendable=True): |
||
| 857 | """Prepare and write neighboring crossborder links to eTraGo table |
||
| 858 | |||
| 859 | This function prepare the neighboring crossborder links |
||
| 860 | generated the PyPSA-eur-sec (p-e-s) run by: |
||
| 861 | * Delete the useless columns |
||
| 862 | * If extendable is false only (non default case): |
||
| 863 | * Replace p_nom = 0 with the p_nom_op values (arrising |
||
| 864 | from the p-e-s optimisation) |
||
| 865 | * Setting p_nom_extendable to false |
||
| 866 | * Add geomtry to the links: 'geom' and 'topo' columns |
||
| 867 | * Change the name of the carriers to have the consistent in |
||
| 868 | eGon-data |
||
| 869 | |||
| 870 | The function insert then the link to the eTraGo table and has |
||
| 871 | no return. |
||
| 872 | |||
| 873 | Parameters |
||
| 874 | ---------- |
||
| 875 | neighbor_links : pandas.DataFrame |
||
| 876 | Dataframe containing the neighboring crossborder links |
||
| 877 | scn_name : str |
||
| 878 | Name of the scenario |
||
| 879 | extendable : bool |
||
| 880 | Boolean expressing if the links should be extendable or not |
||
| 881 | |||
| 882 | Returns |
||
| 883 | ------- |
||
| 884 | None |
||
| 885 | |||
| 886 | """ |
||
| 887 | neighbor_links["scn_name"] = scn |
||
| 888 | |||
| 889 | dropped_carriers = [ |
||
| 890 | "Link", |
||
| 891 | "geometry", |
||
| 892 | "tags", |
||
| 893 | "under_construction", |
||
| 894 | "underground", |
||
| 895 | "underwater_fraction", |
||
| 896 | "bus2", |
||
| 897 | "bus3", |
||
| 898 | "bus4", |
||
| 899 | "efficiency2", |
||
| 900 | "efficiency3", |
||
| 901 | "efficiency4", |
||
| 902 | "lifetime", |
||
| 903 | "pipe_retrofit", |
||
| 904 | "committable", |
||
| 905 | "start_up_cost", |
||
| 906 | "shut_down_cost", |
||
| 907 | "min_up_time", |
||
| 908 | "min_down_time", |
||
| 909 | "up_time_before", |
||
| 910 | "down_time_before", |
||
| 911 | "ramp_limit_up", |
||
| 912 | "ramp_limit_down", |
||
| 913 | "ramp_limit_start_up", |
||
| 914 | "ramp_limit_shut_down", |
||
| 915 | "length_original", |
||
| 916 | "reversed", |
||
| 917 | ] |
||
| 918 | |||
| 919 | if extendable: |
||
| 920 | dropped_carriers.append("p_nom_opt") |
||
| 921 | neighbor_links = neighbor_links.drop( |
||
| 922 | columns=dropped_carriers, |
||
| 923 | errors="ignore", |
||
| 924 | ) |
||
| 925 | |||
| 926 | else: |
||
| 927 | dropped_carriers.append("p_nom") |
||
| 928 | dropped_carriers.append("p_nom_extendable") |
||
| 929 | neighbor_links = neighbor_links.drop( |
||
| 930 | columns=dropped_carriers, |
||
| 931 | errors="ignore", |
||
| 932 | ) |
||
| 933 | neighbor_links = neighbor_links.rename( |
||
| 934 | columns={"p_nom_opt": "p_nom"} |
||
| 935 | ) |
||
| 936 | neighbor_links["p_nom_extendable"] = False |
||
| 937 | |||
| 938 | if neighbor_links.empty: |
||
| 939 | print("No links selected") |
||
| 940 | return |
||
| 941 | |||
| 942 | # Define geometry and add to lines dataframe as 'topo' |
||
| 943 | gdf = gpd.GeoDataFrame( |
||
| 944 | index=neighbor_links.index, |
||
| 945 | data={ |
||
| 946 | "geom_bus0": neighbors.loc[neighbor_links.bus0, "geom"].values, |
||
| 947 | "geom_bus1": neighbors.loc[neighbor_links.bus1, "geom"].values, |
||
| 948 | }, |
||
| 949 | ) |
||
| 950 | |||
| 951 | gdf["geometry"] = gdf.apply( |
||
| 952 | lambda x: LineString([x["geom_bus0"], x["geom_bus1"]]), axis=1 |
||
| 953 | ) |
||
| 954 | |||
| 955 | neighbor_links = ( |
||
| 956 | gpd.GeoDataFrame(neighbor_links, geometry=gdf["geometry"]) |
||
| 957 | .rename_geometry("topo") |
||
| 958 | .set_crs(4326) |
||
| 959 | ) |
||
| 960 | |||
| 961 | # Unify carrier names |
||
| 962 | neighbor_links.carrier = neighbor_links.carrier.str.replace(" ", "_") |
||
| 963 | |||
| 964 | neighbor_links.carrier.replace( |
||
| 965 | { |
||
| 966 | "H2_Electrolysis": "power_to_H2", |
||
| 967 | "H2_Fuel_Cell": "H2_to_power", |
||
| 968 | "H2_pipeline_retrofitted": "H2_retrofit", |
||
| 969 | "SMR": "CH4_to_H2", |
||
| 970 | "Sabatier": "H2_to_CH4", |
||
| 971 | "gas_for_industry": "CH4_for_industry", |
||
| 972 | "gas_pipeline": "CH4", |
||
| 973 | }, |
||
| 974 | inplace=True, |
||
| 975 | ) |
||
| 976 | |||
| 977 | for c in [ |
||
| 978 | "H2_to_CH4", |
||
| 979 | "H2_to_power", |
||
| 980 | "power_to_H2", |
||
| 981 | "CH4_to_H2", |
||
| 982 | ]: |
||
| 983 | neighbor_links.loc[ |
||
| 984 | (neighbor_links.carrier == c), |
||
| 985 | "lifetime", |
||
| 986 | ] = get_sector_parameters("gas", "eGon100RE")["lifetime"][c] |
||
| 987 | |||
| 988 | neighbor_links.to_postgis( |
||
| 989 | "egon_etrago_link", |
||
| 990 | engine, |
||
| 991 | schema="grid", |
||
| 992 | if_exists="append", |
||
| 993 | index=True, |
||
| 994 | index_label="link_id", |
||
| 995 | ) |
||
| 996 | |||
| 997 | non_extendable_links_carriers = [ |
||
| 998 | "H2 pipeline retrofitted", |
||
| 999 | "H2 pipeline", |
||
| 1000 | "gas pipeline", |
||
| 1001 | "biogas to gas", |
||
| 1002 | ] |
||
| 1003 | |||
| 1004 | # delete unwanted carriers for eTraGo |
||
| 1005 | excluded_carriers = [ |
||
| 1006 | "gas for industry CC", |
||
| 1007 | "SMR CC", |
||
| 1008 | "biogas to gas", |
||
| 1009 | "DAC", |
||
| 1010 | "electricity distribution grid", |
||
| 1011 | ] |
||
| 1012 | neighbor_links = neighbor_links[ |
||
| 1013 | ~neighbor_links.carrier.isin(excluded_carriers) |
||
| 1014 | ] |
||
| 1015 | |||
| 1016 | links_to_etrago( |
||
| 1017 | neighbor_links[ |
||
| 1018 | ~neighbor_links.carrier.isin(non_extendable_links_carriers) |
||
| 1019 | ], |
||
| 1020 | "eGon100RE", |
||
| 1021 | ) |
||
| 1022 | links_to_etrago( |
||
| 1023 | neighbor_links[ |
||
| 1024 | neighbor_links.carrier.isin(non_extendable_links_carriers) |
||
| 1025 | ], |
||
| 1026 | "eGon100RE", |
||
| 1027 | extendable=False, |
||
| 1028 | ) |
||
| 1029 | |||
| 1030 | # prepare neighboring generators for etrago tables |
||
| 1031 | neighbor_gens["scn_name"] = "eGon100RE" |
||
| 1032 | neighbor_gens["p_nom"] = neighbor_gens["p_nom_opt"] |
||
| 1033 | neighbor_gens["p_nom_extendable"] = False |
||
| 1034 | |||
| 1035 | # Unify carrier names |
||
| 1036 | neighbor_gens.carrier = neighbor_gens.carrier.str.replace(" ", "_") |
||
| 1037 | |||
| 1038 | neighbor_gens.carrier.replace( |
||
| 1039 | { |
||
| 1040 | "onwind": "wind_onshore", |
||
| 1041 | "ror": "run_of_river", |
||
| 1042 | "offwind-ac": "wind_offshore", |
||
| 1043 | "offwind-dc": "wind_offshore", |
||
| 1044 | "urban_central_solar_thermal": "urban_central_solar_thermal_collector", |
||
| 1045 | "residential_rural_solar_thermal": "residential_rural_solar_thermal_collector", |
||
| 1046 | "services_rural_solar_thermal": "services_rural_solar_thermal_collector", |
||
| 1047 | }, |
||
| 1048 | inplace=True, |
||
| 1049 | ) |
||
| 1050 | |||
| 1051 | for i in [ |
||
| 1052 | "Generator", |
||
| 1053 | "weight", |
||
| 1054 | "lifetime", |
||
| 1055 | "p_set", |
||
| 1056 | "q_set", |
||
| 1057 | "p_nom_opt", |
||
| 1058 | ]: |
||
| 1059 | neighbor_gens = neighbor_gens.drop(i, axis=1) |
||
| 1060 | |||
| 1061 | neighbor_gens.to_sql( |
||
| 1062 | "egon_etrago_generator", |
||
| 1063 | engine, |
||
| 1064 | schema="grid", |
||
| 1065 | if_exists="append", |
||
| 1066 | index=True, |
||
| 1067 | index_label="generator_id", |
||
| 1068 | ) |
||
| 1069 | |||
| 1070 | # prepare neighboring loads for etrago tables |
||
| 1071 | neighbor_loads["scn_name"] = "eGon100RE" |
||
| 1072 | |||
| 1073 | # Unify carrier names |
||
| 1074 | neighbor_loads.carrier = neighbor_loads.carrier.str.replace(" ", "_") |
||
| 1075 | |||
| 1076 | neighbor_loads.carrier.replace( |
||
| 1077 | { |
||
| 1078 | "electricity": "AC", |
||
| 1079 | "DC": "AC", |
||
| 1080 | "industry_electricity": "AC", |
||
| 1081 | "H2_pipeline_retrofitted": "H2_system_boundary", |
||
| 1082 | "gas_pipeline": "CH4_system_boundary", |
||
| 1083 | "gas_for_industry": "CH4_for_industry", |
||
| 1084 | }, |
||
| 1085 | inplace=True, |
||
| 1086 | ) |
||
| 1087 | |||
| 1088 | neighbor_loads = neighbor_loads.drop( |
||
| 1089 | columns=["Load"], |
||
| 1090 | errors="ignore", |
||
| 1091 | ) |
||
| 1092 | |||
| 1093 | neighbor_loads.to_sql( |
||
| 1094 | "egon_etrago_load", |
||
| 1095 | engine, |
||
| 1096 | schema="grid", |
||
| 1097 | if_exists="append", |
||
| 1098 | index=True, |
||
| 1099 | index_label="load_id", |
||
| 1100 | ) |
||
| 1101 | |||
| 1102 | # prepare neighboring stores for etrago tables |
||
| 1103 | neighbor_stores["scn_name"] = "eGon100RE" |
||
| 1104 | |||
| 1105 | # Unify carrier names |
||
| 1106 | neighbor_stores.carrier = neighbor_stores.carrier.str.replace(" ", "_") |
||
| 1107 | |||
| 1108 | neighbor_stores.carrier.replace( |
||
| 1109 | { |
||
| 1110 | "Li_ion": "battery", |
||
| 1111 | "gas": "CH4", |
||
| 1112 | }, |
||
| 1113 | inplace=True, |
||
| 1114 | ) |
||
| 1115 | neighbor_stores.loc[ |
||
| 1116 | ( |
||
| 1117 | (neighbor_stores.e_nom_max <= 1e9) |
||
| 1118 | & (neighbor_stores.carrier == "H2") |
||
| 1119 | ), |
||
| 1120 | "carrier", |
||
| 1121 | ] = "H2_underground" |
||
| 1122 | neighbor_stores.loc[ |
||
| 1123 | ( |
||
| 1124 | (neighbor_stores.e_nom_max > 1e9) |
||
| 1125 | & (neighbor_stores.carrier == "H2") |
||
| 1126 | ), |
||
| 1127 | "carrier", |
||
| 1128 | ] = "H2_overground" |
||
| 1129 | |||
| 1130 | for i in [ |
||
| 1131 | "Store", |
||
| 1132 | "p_set", |
||
| 1133 | "q_set", |
||
| 1134 | "e_nom_opt", |
||
| 1135 | "lifetime", |
||
| 1136 | "e_initial_per_period", |
||
| 1137 | "e_cyclic_per_period", |
||
| 1138 | "location", |
||
| 1139 | ]: |
||
| 1140 | neighbor_stores = neighbor_stores.drop(i, axis=1, errors="ignore") |
||
| 1141 | |||
| 1142 | for c in ["H2_underground", "H2_overground"]: |
||
| 1143 | neighbor_stores.loc[ |
||
| 1144 | (neighbor_stores.carrier == c), |
||
| 1145 | "lifetime", |
||
| 1146 | ] = get_sector_parameters("gas", "eGon100RE")["lifetime"][c] |
||
| 1147 | |||
| 1148 | neighbor_stores.to_sql( |
||
| 1149 | "egon_etrago_store", |
||
| 1150 | engine, |
||
| 1151 | schema="grid", |
||
| 1152 | if_exists="append", |
||
| 1153 | index=True, |
||
| 1154 | index_label="store_id", |
||
| 1155 | ) |
||
| 1156 | |||
| 1157 | # prepare neighboring storage_units for etrago tables |
||
| 1158 | neighbor_storage["scn_name"] = "eGon100RE" |
||
| 1159 | |||
| 1160 | # Unify carrier names |
||
| 1161 | neighbor_storage.carrier = neighbor_storage.carrier.str.replace(" ", "_") |
||
| 1162 | |||
| 1163 | neighbor_storage.carrier.replace( |
||
| 1164 | {"PHS": "pumped_hydro", "hydro": "reservoir"}, inplace=True |
||
| 1165 | ) |
||
| 1166 | |||
| 1167 | for i in [ |
||
| 1168 | "StorageUnit", |
||
| 1169 | "p_nom_opt", |
||
| 1170 | "state_of_charge_initial_per_period", |
||
| 1171 | "cyclic_state_of_charge_per_period", |
||
| 1172 | ]: |
||
| 1173 | neighbor_storage = neighbor_storage.drop(i, axis=1, errors="ignore") |
||
| 1174 | |||
| 1175 | neighbor_storage.to_sql( |
||
| 1176 | "egon_etrago_storage", |
||
| 1177 | engine, |
||
| 1178 | schema="grid", |
||
| 1179 | if_exists="append", |
||
| 1180 | index=True, |
||
| 1181 | index_label="storage_id", |
||
| 1182 | ) |
||
| 1183 | |||
| 1184 | # writing neighboring loads_t p_sets to etrago tables |
||
| 1185 | |||
| 1186 | neighbor_loads_t_etrago = pd.DataFrame( |
||
| 1187 | columns=["scn_name", "temp_id", "p_set"], |
||
| 1188 | index=neighbor_loads_t.columns, |
||
| 1189 | ) |
||
| 1190 | neighbor_loads_t_etrago["scn_name"] = "eGon100RE" |
||
| 1191 | neighbor_loads_t_etrago["temp_id"] = 1 |
||
| 1192 | for i in neighbor_loads_t.columns: |
||
| 1193 | neighbor_loads_t_etrago["p_set"][i] = neighbor_loads_t[ |
||
| 1194 | i |
||
| 1195 | ].values.tolist() |
||
| 1196 | |||
| 1197 | neighbor_loads_t_etrago.to_sql( |
||
| 1198 | "egon_etrago_load_timeseries", |
||
| 1199 | engine, |
||
| 1200 | schema="grid", |
||
| 1201 | if_exists="append", |
||
| 1202 | index=True, |
||
| 1203 | index_label="load_id", |
||
| 1204 | ) |
||
| 1205 | |||
| 1206 | # writing neighboring generator_t p_max_pu to etrago tables |
||
| 1207 | neighbor_gens_t_etrago = pd.DataFrame( |
||
| 1208 | columns=["scn_name", "temp_id", "p_max_pu"], |
||
| 1209 | index=neighbor_gens_t.columns, |
||
| 1210 | ) |
||
| 1211 | neighbor_gens_t_etrago["scn_name"] = "eGon100RE" |
||
| 1212 | neighbor_gens_t_etrago["temp_id"] = 1 |
||
| 1213 | for i in neighbor_gens_t.columns: |
||
| 1214 | neighbor_gens_t_etrago["p_max_pu"][i] = neighbor_gens_t[ |
||
| 1215 | i |
||
| 1216 | ].values.tolist() |
||
| 1217 | |||
| 1218 | neighbor_gens_t_etrago.to_sql( |
||
| 1219 | "egon_etrago_generator_timeseries", |
||
| 1220 | engine, |
||
| 1221 | schema="grid", |
||
| 1222 | if_exists="append", |
||
| 1223 | index=True, |
||
| 1224 | index_label="generator_id", |
||
| 1225 | ) |
||
| 1226 | |||
| 1227 | # writing neighboring stores_t e_min_pu to etrago tables |
||
| 1228 | neighbor_stores_t_etrago = pd.DataFrame( |
||
| 1229 | columns=["scn_name", "temp_id", "e_min_pu"], |
||
| 1230 | index=neighbor_stores_t.columns, |
||
| 1231 | ) |
||
| 1232 | neighbor_stores_t_etrago["scn_name"] = "eGon100RE" |
||
| 1233 | neighbor_stores_t_etrago["temp_id"] = 1 |
||
| 1234 | for i in neighbor_stores_t.columns: |
||
| 1235 | neighbor_stores_t_etrago["e_min_pu"][i] = neighbor_stores_t[ |
||
| 1236 | i |
||
| 1237 | ].values.tolist() |
||
| 1238 | |||
| 1239 | neighbor_stores_t_etrago.to_sql( |
||
| 1240 | "egon_etrago_store_timeseries", |
||
| 1241 | engine, |
||
| 1242 | schema="grid", |
||
| 1243 | if_exists="append", |
||
| 1244 | index=True, |
||
| 1245 | index_label="store_id", |
||
| 1246 | ) |
||
| 1247 | |||
| 1248 | # writing neighboring storage_units inflow to etrago tables |
||
| 1249 | neighbor_storage_t_etrago = pd.DataFrame( |
||
| 1250 | columns=["scn_name", "temp_id", "inflow"], |
||
| 1251 | index=neighbor_storage_t.columns, |
||
| 1252 | ) |
||
| 1253 | neighbor_storage_t_etrago["scn_name"] = "eGon100RE" |
||
| 1254 | neighbor_storage_t_etrago["temp_id"] = 1 |
||
| 1255 | for i in neighbor_storage_t.columns: |
||
| 1256 | neighbor_storage_t_etrago["inflow"][i] = neighbor_storage_t[ |
||
| 1257 | i |
||
| 1258 | ].values.tolist() |
||
| 1259 | |||
| 1260 | neighbor_storage_t_etrago.to_sql( |
||
| 1261 | "egon_etrago_storage_timeseries", |
||
| 1262 | engine, |
||
| 1263 | schema="grid", |
||
| 1264 | if_exists="append", |
||
| 1265 | index=True, |
||
| 1266 | index_label="storage_id", |
||
| 1267 | ) |
||
| 1268 | |||
| 1269 | # writing neighboring lines_t s_max_pu to etrago tables |
||
| 1270 | if not network.lines_t["s_max_pu"].empty: |
||
| 1271 | neighbor_lines_t_etrago = pd.DataFrame( |
||
| 1272 | columns=["scn_name", "s_max_pu"], index=neighbor_lines_t.columns |
||
| 1273 | ) |
||
| 1274 | neighbor_lines_t_etrago["scn_name"] = "eGon100RE" |
||
| 1275 | |||
| 1276 | for i in neighbor_lines_t.columns: |
||
| 1277 | neighbor_lines_t_etrago["s_max_pu"][i] = neighbor_lines_t[ |
||
| 1278 | i |
||
| 1279 | ].values.tolist() |
||
| 1280 | |||
| 1281 | neighbor_lines_t_etrago.to_sql( |
||
| 1282 | "egon_etrago_line_timeseries", |
||
| 1283 | engine, |
||
| 1284 | schema="grid", |
||
| 1285 | if_exists="append", |
||
| 1286 | index=True, |
||
| 1287 | index_label="line_id", |
||
| 1288 | ) |
||
| 1654 |