Conditions | 4 |
Total Lines | 98 |
Code Lines | 60 |
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 | # -*- coding: utf-8 -*- |
||
29 | def load_NG_generators(scn_name="eGon2035"): |
||
30 | """Define the natural CH4 production units in Germany |
||
31 | |||
32 | Parameters |
||
33 | ---------- |
||
34 | scn_name : str |
||
35 | Name of the scenario. |
||
36 | Returns |
||
37 | ------- |
||
38 | CH4_generators_list : |
||
39 | Dataframe containing the natural gas production units in Germany |
||
40 | |||
41 | """ |
||
42 | # read carrier information from scnario parameter data |
||
43 | scn_params = get_sector_parameters("gas", scn_name) |
||
44 | |||
45 | target_file = ( |
||
46 | Path(".") |
||
47 | / "datasets" |
||
48 | / "gas_data" |
||
49 | / "data" |
||
50 | / "IGGIELGN_Productions.csv" |
||
51 | ) |
||
52 | |||
53 | NG_generators_list = pd.read_csv( |
||
54 | target_file, |
||
55 | delimiter=";", |
||
56 | decimal=".", |
||
57 | usecols=["lat", "long", "country_code", "param"], |
||
58 | ) |
||
59 | |||
60 | NG_generators_list = NG_generators_list[ |
||
61 | NG_generators_list["country_code"].str.match("DE") |
||
62 | ] |
||
63 | |||
64 | # Cut data to federal state if in testmode |
||
65 | NUTS1 = [] |
||
66 | for index, row in NG_generators_list.iterrows(): |
||
67 | param = ast.literal_eval(row["param"]) |
||
68 | NUTS1.append(param["nuts_id_1"]) |
||
69 | NG_generators_list = NG_generators_list.assign(NUTS1=NUTS1) |
||
70 | |||
71 | boundary = settings()["egon-data"]["--dataset-boundary"] |
||
72 | if boundary != "Everything": |
||
73 | map_states = { |
||
74 | "Baden-Württemberg": "DE1", |
||
75 | "Nordrhein-Westfalen": "DEA", |
||
76 | "Hessen": "DE7", |
||
77 | "Brandenburg": "DE4", |
||
78 | "Bremen": "DE5", |
||
79 | "Rheinland-Pfalz": "DEB", |
||
80 | "Sachsen-Anhalt": "DEE", |
||
81 | "Schleswig-Holstein": "DEF", |
||
82 | "Mecklenburg-Vorpommern": "DE8", |
||
83 | "Thüringen": "DEG", |
||
84 | "Niedersachsen": "DE9", |
||
85 | "Sachsen": "DED", |
||
86 | "Hamburg": "DE6", |
||
87 | "Saarland": "DEC", |
||
88 | "Berlin": "DE3", |
||
89 | "Bayern": "DE2", |
||
90 | } |
||
91 | |||
92 | NG_generators_list = NG_generators_list[ |
||
93 | NG_generators_list["NUTS1"].isin([map_states[boundary], np.nan]) |
||
94 | ] |
||
95 | |||
96 | NG_generators_list = NG_generators_list.rename( |
||
97 | columns={"lat": "y", "long": "x"} |
||
98 | ) |
||
99 | NG_generators_list = gpd.GeoDataFrame( |
||
100 | NG_generators_list, |
||
101 | geometry=gpd.points_from_xy( |
||
102 | NG_generators_list["x"], NG_generators_list["y"] |
||
103 | ), |
||
104 | ) |
||
105 | NG_generators_list = NG_generators_list.rename( |
||
106 | columns={"geometry": "geom"} |
||
107 | ).set_geometry("geom", crs=4326) |
||
108 | |||
109 | # Insert p_nom |
||
110 | p_nom = [] |
||
111 | for index, row in NG_generators_list.iterrows(): |
||
112 | param = ast.literal_eval(row["param"]) |
||
113 | p_nom.append(param["max_supply_M_m3_per_d"]) |
||
114 | |||
115 | conversion_factor = 437.5 # MCM/day to MWh/h |
||
116 | NG_generators_list["p_nom"] = [i * conversion_factor for i in p_nom] |
||
117 | |||
118 | # Add missing columns |
||
119 | NG_generators_list["marginal_cost"] = scn_params["marginal_cost"]["CH4"] |
||
120 | |||
121 | # Remove useless columns |
||
122 | NG_generators_list = NG_generators_list.drop( |
||
123 | columns=["x", "y", "param", "country_code", "NUTS1"] |
||
124 | ) |
||
125 | |||
126 | return NG_generators_list |
||
127 | |||
337 |