| Conditions | 3 |
| Total Lines | 82 |
| Code Lines | 28 |
| 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 processing |
||
| 189 | def distribute_cts_demands(): |
||
| 190 | """Distribute electrical demands for cts to zensus cells. |
||
| 191 | |||
| 192 | The demands on nuts3-level from demandregio are linear distributed |
||
| 193 | to the heat demand of cts in each zensus cell. |
||
| 194 | |||
| 195 | Returns |
||
| 196 | ------- |
||
| 197 | None. |
||
| 198 | |||
| 199 | """ |
||
| 200 | |||
| 201 | sources = egon.data.config.datasets()["electrical_demands_cts"]["sources"] |
||
| 202 | |||
| 203 | target = egon.data.config.datasets()["electrical_demands_cts"]["targets"][ |
||
| 204 | "cts_demands_zensus" |
||
| 205 | ] |
||
| 206 | |||
| 207 | db.execute_sql( |
||
| 208 | f"""DELETE FROM {target['schema']}.{target['table']} |
||
| 209 | WHERE sector = 'service'""" |
||
| 210 | ) |
||
| 211 | |||
| 212 | # Select match between zensus cells and nuts3 regions of vg250 |
||
| 213 | map_nuts3 = db.select_dataframe( |
||
| 214 | f"""SELECT zensus_population_id, vg250_nuts3 as nuts3 FROM |
||
| 215 | {sources['map_zensus_vg250']['schema']}. |
||
| 216 | {sources['map_zensus_vg250']['table']}""", |
||
| 217 | index_col="zensus_population_id", |
||
| 218 | ) |
||
| 219 | |||
| 220 | # Insert data per scenario |
||
| 221 | for scn in egon.data.config.settings()["egon-data"]["--scenarios"]: |
||
| 222 | # Select heat_demand per zensus cell |
||
| 223 | peta = db.select_dataframe( |
||
| 224 | f"""SELECT zensus_population_id, demand as heat_demand, |
||
| 225 | sector, scenario FROM |
||
| 226 | {sources['heat_demand_cts']['schema']}. |
||
| 227 | {sources['heat_demand_cts']['table']} |
||
| 228 | WHERE scenario = '{scn}' |
||
| 229 | AND sector = 'service'""", |
||
| 230 | index_col="zensus_population_id", |
||
| 231 | ) |
||
| 232 | |||
| 233 | # Add nuts3 key to zensus cells |
||
| 234 | peta["nuts3"] = map_nuts3.nuts3 |
||
| 235 | |||
| 236 | # Calculate share of nuts3 heat demand per zensus cell |
||
| 237 | for nuts3, df in peta.groupby("nuts3"): |
||
| 238 | peta.loc[df.index, "share"] = ( |
||
| 239 | df["heat_demand"] / df["heat_demand"].sum() |
||
| 240 | ) |
||
| 241 | |||
| 242 | # Select forecasted electrical demands from demandregio table |
||
| 243 | demand_nuts3 = db.select_dataframe( |
||
| 244 | f"""SELECT nuts3, SUM(demand) as demand FROM |
||
| 245 | {sources['demandregio']['schema']}. |
||
| 246 | {sources['demandregio']['table']} |
||
| 247 | WHERE scenario = '{scn}' |
||
| 248 | AND wz IN ( |
||
| 249 | SELECT wz FROM |
||
| 250 | {sources['demandregio_wz']['schema']}. |
||
| 251 | {sources['demandregio_wz']['table']} |
||
| 252 | WHERE sector = 'CTS') |
||
| 253 | GROUP BY nuts3""", |
||
| 254 | index_col="nuts3", |
||
| 255 | ) |
||
| 256 | |||
| 257 | # Scale demands on nuts3 level linear to heat demand share |
||
| 258 | peta["demand"] = peta["share"].mul( |
||
| 259 | demand_nuts3.demand[peta["nuts3"]].values |
||
| 260 | ) |
||
| 261 | |||
| 262 | # Rename index |
||
| 263 | peta.index = peta.index.rename("zensus_population_id") |
||
| 264 | |||
| 265 | # Insert data to target table |
||
| 266 | peta[["scenario", "demand", "sector"]].to_sql( |
||
| 267 | target["table"], |
||
| 268 | schema=target["schema"], |
||
| 269 | con=db.engine(), |
||
| 270 | if_exists="append", |
||
| 271 | ) |
||
| 272 |