Conditions | 1 |
Total Lines | 262 |
Code Lines | 134 |
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 | """ |
||
130 | def insert(): |
||
131 | """Insert combined heat and power plants into eTraGo tables. |
||
132 | |||
133 | Gas CHP plants are modeled as links to the gas grid, |
||
134 | biomass CHP plants (only in eGon2035) are modeled as generators |
||
135 | |||
136 | Returns |
||
137 | ------- |
||
138 | None. |
||
139 | |||
140 | """ |
||
141 | |||
142 | sources = config.datasets()["chp_etrago"]["sources"] |
||
143 | |||
144 | targets = config.datasets()["chp_etrago"]["targets"] |
||
145 | |||
146 | db.execute_sql( |
||
147 | f""" |
||
148 | DELETE FROM {targets['link']['schema']}.{targets['link']['table']} |
||
149 | WHERE carrier LIKE '%%CHP%%' |
||
150 | AND scn_name = 'eGon2035' |
||
151 | AND bus0 IN |
||
152 | (SELECT bus_id |
||
153 | FROM {sources['etrago_buses']['schema']}.{sources['etrago_buses']['table']} |
||
154 | WHERE scn_name = 'eGon2035' |
||
155 | AND country = 'DE') |
||
156 | AND bus1 IN |
||
157 | (SELECT bus_id |
||
158 | FROM {sources['etrago_buses']['schema']}.{sources['etrago_buses']['table']} |
||
159 | WHERE scn_name = 'eGon2035' |
||
160 | AND country = 'DE') |
||
161 | """ |
||
162 | ) |
||
163 | db.execute_sql( |
||
164 | f""" |
||
165 | DELETE FROM {targets['generator']['schema']}.{targets['generator']['table']} |
||
166 | WHERE carrier LIKE '%%CHP%%' |
||
167 | AND scn_name = 'eGon2035' |
||
168 | """ |
||
169 | ) |
||
170 | # Select all CHP plants used in district heating |
||
171 | chp_dh = db.select_dataframe( |
||
172 | f""" |
||
173 | SELECT electrical_bus_id, ch4_bus_id, a.carrier, |
||
174 | SUM(el_capacity) AS el_capacity, SUM(th_capacity) AS th_capacity, |
||
175 | c.bus_id as heat_bus_id |
||
176 | FROM {sources['chp_table']['schema']}. |
||
177 | {sources['chp_table']['table']} a |
||
178 | JOIN {sources['district_heating_areas']['schema']}. |
||
179 | {sources['district_heating_areas']['table']} b |
||
180 | ON a.district_heating_area_id = b.area_id |
||
181 | JOIN grid.egon_etrago_bus c |
||
182 | ON ST_Transform(ST_Centroid(b.geom_polygon), 4326) = c.geom |
||
183 | |||
184 | WHERE a.scenario='eGon2035' |
||
185 | AND b.scenario = 'eGon2035' |
||
186 | AND c.scn_name = 'eGon2035' |
||
187 | AND c.carrier = 'central_heat' |
||
188 | AND NOT district_heating_area_id IS NULL |
||
189 | GROUP BY ( |
||
190 | electrical_bus_id, ch4_bus_id, a.carrier, c.bus_id) |
||
191 | """ |
||
192 | ) |
||
193 | # Divide into biomass and gas CHP which are modelled differently |
||
194 | chp_link_dh = chp_dh[chp_dh.carrier != "biomass"].index |
||
195 | chp_generator_dh = chp_dh[chp_dh.carrier == "biomass"].index |
||
196 | |||
197 | # Create geodataframes for gas CHP plants |
||
198 | chp_el = link_geom_from_buses( |
||
199 | gpd.GeoDataFrame( |
||
200 | index=chp_link_dh, |
||
201 | data={ |
||
202 | "scn_name": "eGon2035", |
||
203 | "bus0": chp_dh.loc[chp_link_dh, "ch4_bus_id"].astype(int), |
||
204 | "bus1": chp_dh.loc[chp_link_dh, "electrical_bus_id"].astype( |
||
205 | int |
||
206 | ), |
||
207 | "p_nom": chp_dh.loc[chp_link_dh, "el_capacity"], |
||
208 | "carrier": "central_gas_CHP", |
||
209 | }, |
||
210 | ), |
||
211 | "eGon2035", |
||
212 | ) |
||
213 | # Set index |
||
214 | chp_el["link_id"] = range( |
||
215 | db.next_etrago_id("link"), len(chp_el) + db.next_etrago_id("link") |
||
216 | ) |
||
217 | |||
218 | # Add marginal cost which is only VOM in case of gas chp |
||
219 | chp_el["marginal_cost"] = get_sector_parameters("gas", "eGon2035")[ |
||
220 | "marginal_cost" |
||
221 | ]["chp_gas"] |
||
222 | |||
223 | # Insert into database |
||
224 | chp_el.to_postgis( |
||
225 | targets["link"]["table"], |
||
226 | schema=targets["link"]["schema"], |
||
227 | con=db.engine(), |
||
228 | if_exists="append", |
||
229 | ) |
||
230 | |||
231 | # |
||
232 | chp_heat = link_geom_from_buses( |
||
233 | gpd.GeoDataFrame( |
||
234 | index=chp_link_dh, |
||
235 | data={ |
||
236 | "scn_name": "eGon2035", |
||
237 | "bus0": chp_dh.loc[chp_link_dh, "ch4_bus_id"].astype(int), |
||
238 | "bus1": chp_dh.loc[chp_link_dh, "heat_bus_id"].astype(int), |
||
239 | "p_nom": chp_dh.loc[chp_link_dh, "th_capacity"], |
||
240 | "carrier": "central_gas_CHP_heat", |
||
241 | }, |
||
242 | ), |
||
243 | "eGon2035", |
||
244 | ) |
||
245 | |||
246 | chp_heat["link_id"] = range( |
||
247 | db.next_etrago_id("link"), len(chp_heat) + db.next_etrago_id("link") |
||
248 | ) |
||
249 | |||
250 | chp_heat.to_postgis( |
||
251 | targets["link"]["table"], |
||
252 | schema=targets["link"]["schema"], |
||
253 | con=db.engine(), |
||
254 | if_exists="append", |
||
255 | ) |
||
256 | |||
257 | # Insert biomass CHP as generators |
||
258 | # Create geodataframes for CHP plants |
||
259 | chp_el_gen = pd.DataFrame( |
||
260 | index=chp_generator_dh, |
||
261 | data={ |
||
262 | "scn_name": "eGon2035", |
||
263 | "bus": chp_dh.loc[chp_generator_dh, "electrical_bus_id"].astype( |
||
264 | int |
||
265 | ), |
||
266 | "p_nom": chp_dh.loc[chp_generator_dh, "el_capacity"], |
||
267 | "carrier": "central_biomass_CHP", |
||
268 | }, |
||
269 | ) |
||
270 | |||
271 | chp_el_gen["generator_id"] = range( |
||
272 | db.next_etrago_id("generator"), |
||
273 | len(chp_el_gen) + db.next_etrago_id("generator"), |
||
274 | ) |
||
275 | |||
276 | # Add marginal cost |
||
277 | chp_el_gen["marginal_cost"] = get_sector_parameters( |
||
278 | "electricity", "eGon2035" |
||
279 | )["marginal_cost"]["biomass"] |
||
280 | |||
281 | chp_el_gen.to_sql( |
||
282 | targets["generator"]["table"], |
||
283 | schema=targets["generator"]["schema"], |
||
284 | con=db.engine(), |
||
285 | if_exists="append", |
||
286 | index=False, |
||
287 | ) |
||
288 | |||
289 | chp_heat_gen = pd.DataFrame( |
||
290 | index=chp_generator_dh, |
||
291 | data={ |
||
292 | "scn_name": "eGon2035", |
||
293 | "bus": chp_dh.loc[chp_generator_dh, "heat_bus_id"].astype(int), |
||
294 | "p_nom": chp_dh.loc[chp_generator_dh, "th_capacity"], |
||
295 | "carrier": "central_biomass_CHP_heat", |
||
296 | }, |
||
297 | ) |
||
298 | |||
299 | chp_heat_gen["generator_id"] = range( |
||
300 | db.next_etrago_id("generator"), |
||
301 | len(chp_heat_gen) + db.next_etrago_id("generator"), |
||
302 | ) |
||
303 | |||
304 | chp_heat_gen.to_sql( |
||
305 | targets["generator"]["table"], |
||
306 | schema=targets["generator"]["schema"], |
||
307 | con=db.engine(), |
||
308 | if_exists="append", |
||
309 | index=False, |
||
310 | ) |
||
311 | |||
312 | chp_industry = db.select_dataframe( |
||
313 | f""" |
||
314 | SELECT electrical_bus_id, ch4_bus_id, carrier, |
||
315 | SUM(el_capacity) AS el_capacity, SUM(th_capacity) AS th_capacity |
||
316 | FROM {sources['chp_table']['schema']}.{sources['chp_table']['table']} |
||
317 | WHERE scenario='eGon2035' |
||
318 | AND district_heating_area_id IS NULL |
||
319 | GROUP BY (electrical_bus_id, ch4_bus_id, carrier) |
||
320 | """ |
||
321 | ) |
||
322 | chp_link_ind = chp_industry[chp_industry.carrier != "biomass"].index |
||
323 | |||
324 | chp_generator_ind = chp_industry[chp_industry.carrier == "biomass"].index |
||
325 | |||
326 | chp_el_ind = link_geom_from_buses( |
||
327 | gpd.GeoDataFrame( |
||
328 | index=chp_link_ind, |
||
329 | data={ |
||
330 | "scn_name": "eGon2035", |
||
331 | "bus0": chp_industry.loc[chp_link_ind, "ch4_bus_id"].astype( |
||
332 | int |
||
333 | ), |
||
334 | "bus1": chp_industry.loc[ |
||
335 | chp_link_ind, "electrical_bus_id" |
||
336 | ].astype(int), |
||
337 | "p_nom": chp_industry.loc[chp_link_ind, "el_capacity"], |
||
338 | "carrier": "industrial_gas_CHP", |
||
339 | }, |
||
340 | ), |
||
341 | "eGon2035", |
||
342 | ) |
||
343 | |||
344 | chp_el_ind["link_id"] = range( |
||
345 | db.next_etrago_id("link"), len(chp_el_ind) + db.next_etrago_id("link") |
||
346 | ) |
||
347 | |||
348 | # Add marginal cost which is only VOM in case of gas chp |
||
349 | chp_el_ind["marginal_cost"] = get_sector_parameters("gas", "eGon2035")[ |
||
350 | "marginal_cost" |
||
351 | ]["chp_gas"] |
||
352 | |||
353 | chp_el_ind.to_postgis( |
||
354 | targets["link"]["table"], |
||
355 | schema=targets["link"]["schema"], |
||
356 | con=db.engine(), |
||
357 | if_exists="append", |
||
358 | ) |
||
359 | |||
360 | # Insert biomass CHP as generators |
||
361 | chp_el_ind_gen = pd.DataFrame( |
||
362 | index=chp_generator_ind, |
||
363 | data={ |
||
364 | "scn_name": "eGon2035", |
||
365 | "bus": chp_industry.loc[ |
||
366 | chp_generator_ind, "electrical_bus_id" |
||
367 | ].astype(int), |
||
368 | "p_nom": chp_industry.loc[chp_generator_ind, "el_capacity"], |
||
369 | "carrier": "industrial_biomass_CHP", |
||
370 | }, |
||
371 | ) |
||
372 | |||
373 | chp_el_ind_gen["generator_id"] = range( |
||
374 | db.next_etrago_id("generator"), |
||
375 | len(chp_el_ind_gen) + db.next_etrago_id("generator"), |
||
376 | ) |
||
377 | |||
378 | # Add marginal cost |
||
379 | chp_el_ind_gen["marginal_cost"] = get_sector_parameters( |
||
380 | "electricity", "eGon2035" |
||
381 | )["marginal_cost"]["biomass"] |
||
382 | |||
383 | chp_el_ind_gen.to_sql( |
||
384 | targets["generator"]["table"], |
||
385 | schema=targets["generator"]["schema"], |
||
386 | con=db.engine(), |
||
387 | if_exists="append", |
||
388 | index=False, |
||
389 | ) |
||
390 | |||
391 | insert_egon100re() |
||
392 |