Conditions | 4 |
Total Lines | 178 |
Code Lines | 96 |
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 | """The central module containing all code dealing with power plant data. |
||
91 | def allocate_pumped_hydro_eGon2035(export=True): |
||
92 | """Allocates pumped_hydro plants for eGon2035 scenario and either exports |
||
93 | results to data base or returns as a dataframe |
||
94 | |||
95 | Parameters |
||
96 | ---------- |
||
97 | export : bool |
||
98 | Choose if allocated pumped hydro plants should be exported to the data |
||
99 | base. The default is True. |
||
100 | If export=False a data frame will be returned |
||
101 | |||
102 | Returns |
||
103 | ------- |
||
104 | power_plants : pandas.DataFrame |
||
105 | List of pumped hydro plants in 'eGon2035' scenario |
||
106 | """ |
||
107 | |||
108 | carrier = "pumped_hydro" |
||
109 | |||
110 | cfg = config.datasets()["power_plants"] |
||
111 | |||
112 | nep = select_nep_pumped_hydro() |
||
113 | mastr = select_mastr_pumped_hydro() |
||
114 | |||
115 | # Assign voltage level to MaStR |
||
116 | mastr["voltage_level"] = assign_voltage_level( |
||
117 | mastr.rename({"el_capacity": "Nettonennleistung"}, axis=1), cfg |
||
118 | ) |
||
119 | |||
120 | # Initalize DataFrame for matching power plants |
||
121 | matched = gpd.GeoDataFrame( |
||
122 | columns=[ |
||
123 | "carrier", |
||
124 | "el_capacity", |
||
125 | "scenario", |
||
126 | "geometry", |
||
127 | "MaStRNummer", |
||
128 | "source", |
||
129 | "voltage_level", |
||
130 | ] |
||
131 | ) |
||
132 | |||
133 | # Match pumped_hydro units from NEP list |
||
134 | # using PLZ and capacity |
||
135 | matched, mastr, nep = match_storage_units( |
||
136 | nep, mastr, matched, buffer_capacity=0.1, consider_carrier=False |
||
137 | ) |
||
138 | |||
139 | # Match plants from NEP list using plz, |
||
140 | # neglecting the capacity |
||
141 | matched, mastr, nep = match_storage_units( |
||
142 | nep, |
||
143 | mastr, |
||
144 | matched, |
||
145 | consider_location="plz", |
||
146 | consider_carrier=False, |
||
147 | consider_capacity=False, |
||
148 | ) |
||
149 | |||
150 | # Match plants from NEP list using city, |
||
151 | # neglecting the capacity |
||
152 | matched, mastr, nep = match_storage_units( |
||
153 | nep, |
||
154 | mastr, |
||
155 | matched, |
||
156 | consider_location="city", |
||
157 | consider_carrier=False, |
||
158 | consider_capacity=False, |
||
159 | ) |
||
160 | |||
161 | # Match remaining plants from NEP using the federal state |
||
162 | matched, mastr, nep = match_storage_units( |
||
163 | nep, |
||
164 | mastr, |
||
165 | matched, |
||
166 | buffer_capacity=0.1, |
||
167 | consider_location="federal_state", |
||
168 | consider_carrier=False, |
||
169 | ) |
||
170 | |||
171 | # Match remaining plants from NEP using the federal state |
||
172 | matched, mastr, nep = match_storage_units( |
||
173 | nep, |
||
174 | mastr, |
||
175 | matched, |
||
176 | buffer_capacity=0.7, |
||
177 | consider_location="federal_state", |
||
178 | consider_carrier=False, |
||
179 | ) |
||
180 | |||
181 | print(f"{matched.el_capacity.sum()} MW of {carrier} matched") |
||
182 | print(f"{nep.c2035_capacity.sum()} MW of {carrier} not matched") |
||
183 | |||
184 | if nep.c2035_capacity.sum() > 0: |
||
185 | |||
186 | # Get location using geolocator and city information |
||
187 | located, unmatched = get_location(nep) |
||
188 | |||
189 | # Bring both dataframes together |
||
190 | matched = matched.append( |
||
191 | located[ |
||
192 | [ |
||
193 | "carrier", |
||
194 | "el_capacity", |
||
195 | "scenario", |
||
196 | "geometry", |
||
197 | "source", |
||
198 | "MaStRNummer", |
||
199 | ] |
||
200 | ], |
||
201 | ignore_index=True, |
||
202 | ) |
||
203 | |||
204 | # Set CRS |
||
205 | matched.crs = "EPSG:4326" |
||
206 | |||
207 | # Assign voltage level |
||
208 | matched = apply_voltage_level_thresholds(matched) |
||
209 | |||
210 | # Assign bus_id |
||
211 | # Load grid district polygons |
||
212 | mv_grid_districts = db.select_geodataframe( |
||
213 | f""" |
||
214 | SELECT * FROM {cfg['sources']['egon_mv_grid_district']} |
||
215 | """, |
||
216 | epsg=4326, |
||
217 | ) |
||
218 | |||
219 | ehv_grid_districts = db.select_geodataframe( |
||
220 | f""" |
||
221 | SELECT * FROM {cfg['sources']['ehv_voronoi']} |
||
222 | """, |
||
223 | epsg=4326, |
||
224 | ) |
||
225 | |||
226 | # Perform spatial joins for plants in ehv and hv level seperately |
||
227 | power_plants_hv = gpd.sjoin( |
||
228 | matched[matched.voltage_level >= 3], |
||
229 | mv_grid_districts[["bus_id", "geom"]], |
||
230 | how="left", |
||
231 | ).drop(columns=["index_right"]) |
||
232 | power_plants_ehv = gpd.sjoin( |
||
233 | matched[matched.voltage_level < 3], |
||
234 | ehv_grid_districts[["bus_id", "geom"]], |
||
235 | how="left", |
||
236 | ).drop(columns=["index_right"]) |
||
237 | |||
238 | # Combine both dataframes |
||
239 | power_plants = pd.concat([power_plants_hv, power_plants_ehv]) |
||
240 | |||
241 | # Delete existing units in the target table |
||
242 | db.execute_sql( |
||
243 | f""" DELETE FROM {cfg ['target']['schema']}.{cfg ['target']['table']} |
||
244 | WHERE carrier IN ('pumped_hydro') |
||
245 | AND scenario='eGon2035';""" |
||
246 | ) |
||
247 | |||
248 | # If export = True export pumped_hydro plants to data base |
||
249 | |||
250 | if export: |
||
251 | # Insert into target table |
||
252 | session = sessionmaker(bind=db.engine())() |
||
253 | for i, row in power_plants.iterrows(): |
||
254 | entry = EgonStorages( |
||
255 | sources={"el_capacity": row.source}, |
||
256 | source_id={"MastrNummer": row.MaStRNummer}, |
||
257 | carrier=row.carrier, |
||
258 | el_capacity=row.el_capacity, |
||
259 | voltage_level=row.voltage_level, |
||
260 | bus_id=row.bus_id, |
||
261 | scenario=row.scenario, |
||
262 | geom=f"SRID=4326;POINT({row.geometry.x} {row.geometry.y})", |
||
263 | ) |
||
264 | session.add(entry) |
||
265 | session.commit() |
||
266 | |||
267 | else: |
||
268 | return power_plants |
||
269 | |||
434 |