| Conditions | 3 |
| Total Lines | 172 |
| Code Lines | 86 |
| 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 | import os |
||
| 179 | def CTS_demand_scale(aggregation_level): |
||
| 180 | """ |
||
| 181 | |||
| 182 | Description: caling the demand curves to the annual demand of the respective aggregation level |
||
| 183 | |||
| 184 | |||
| 185 | Parameters |
||
| 186 | ---------- |
||
| 187 | aggregation_level : str |
||
| 188 | aggregation_level : str |
||
| 189 | if further processing is to be done in zensus cell level 'other' |
||
| 190 | else 'dsitrict' |
||
| 191 | |||
| 192 | Returns |
||
| 193 | ------- |
||
| 194 | CTS_per_district : pandas.DataFrame |
||
| 195 | if aggregation ='district' |
||
| 196 | Profiles scaled up to annual demand |
||
| 197 | else |
||
| 198 | 0 |
||
| 199 | CTS_per_grid : pandas.DataFrame |
||
| 200 | if aggregation ='district' |
||
| 201 | Profiles scaled up to annual demandd |
||
| 202 | else |
||
| 203 | 0 |
||
| 204 | CTS_per_zensus : pandas.DataFrame |
||
| 205 | if aggregation ='district' |
||
| 206 | 0 |
||
| 207 | else |
||
| 208 | Profiles scaled up to annual demand |
||
| 209 | |||
| 210 | """ |
||
| 211 | scenarios = ["eGon2035", "eGon100RE"] |
||
| 212 | |||
| 213 | CTS_district = pd.DataFrame() |
||
| 214 | CTS_grid = pd.DataFrame() |
||
| 215 | CTS_zensus = pd.DataFrame() |
||
| 216 | |||
| 217 | for scenario in scenarios: |
||
| 218 | ( |
||
| 219 | CTS_per_district, |
||
| 220 | CTS_per_grid, |
||
| 221 | CTS_per_zensus, |
||
| 222 | ) = cts_demand_per_aggregation_level(aggregation_level, scenario) |
||
| 223 | CTS_per_district = CTS_per_district.transpose() |
||
| 224 | CTS_per_grid = CTS_per_grid.transpose() |
||
| 225 | CTS_per_zensus = CTS_per_zensus.transpose() |
||
| 226 | |||
| 227 | demand = db.select_dataframe( |
||
| 228 | f""" |
||
| 229 | SELECT demand, zensus_population_id |
||
| 230 | FROM demand.egon_peta_heat |
||
| 231 | WHERE sector = 'service' |
||
| 232 | AND scenario = '{scenario}' |
||
| 233 | ORDER BY zensus_population_id |
||
| 234 | """ |
||
| 235 | ) |
||
| 236 | |||
| 237 | if aggregation_level == "district": |
||
| 238 | |||
| 239 | district_heating = db.select_dataframe( |
||
| 240 | f""" |
||
| 241 | SELECT area_id, zensus_population_id |
||
| 242 | FROM demand.egon_map_zensus_district_heating_areas |
||
| 243 | WHERE scenario = '{scenario}' |
||
| 244 | """ |
||
| 245 | ) |
||
| 246 | |||
| 247 | CTS_demands_district = pd.merge( |
||
| 248 | demand, |
||
| 249 | district_heating, |
||
| 250 | on="zensus_population_id", |
||
| 251 | how="inner", |
||
| 252 | ) |
||
| 253 | CTS_demands_district.drop( |
||
| 254 | "zensus_population_id", axis=1, inplace=True |
||
| 255 | ) |
||
| 256 | CTS_demands_district = CTS_demands_district.groupby( |
||
| 257 | "area_id" |
||
| 258 | ).sum() |
||
| 259 | |||
| 260 | CTS_per_district = pd.merge( |
||
| 261 | CTS_per_district, |
||
| 262 | CTS_demands_district[["demand"]], |
||
| 263 | how="inner", |
||
| 264 | right_on=CTS_per_district.index, |
||
| 265 | left_on=CTS_demands_district.index, |
||
| 266 | ) |
||
| 267 | |||
| 268 | CTS_per_district = CTS_per_district.rename( |
||
| 269 | columns={"key_0": "area_id"} |
||
| 270 | ) |
||
| 271 | CTS_per_district.set_index("area_id", inplace=True) |
||
| 272 | |||
| 273 | CTS_per_district = CTS_per_district[ |
||
| 274 | CTS_per_district.columns[:-1] |
||
| 275 | ].multiply(CTS_per_district.demand, axis=0) |
||
| 276 | |||
| 277 | CTS_per_district.insert(0, "scenario", scenario) |
||
| 278 | |||
| 279 | CTS_district = CTS_district.append(CTS_per_district) |
||
| 280 | CTS_district = CTS_district.sort_index() |
||
| 281 | |||
| 282 | mv_grid_ind = db.select_dataframe( |
||
| 283 | f""" |
||
| 284 | SELECT bus_id, zensus_population_id |
||
| 285 | FROM boundaries.egon_map_zensus_grid_districts |
||
| 286 | WHERE zensus_population_id NOT IN |
||
| 287 | (SELECT zensus_population_id |
||
| 288 | FROM demand.egon_map_zensus_district_heating_areas |
||
| 289 | WHERE scenario = '{scenario}') |
||
| 290 | """ |
||
| 291 | ) |
||
| 292 | |||
| 293 | CTS_demands_grid = pd.merge( |
||
| 294 | demand, |
||
| 295 | mv_grid_ind[["bus_id", "zensus_population_id"]], |
||
| 296 | on="zensus_population_id", |
||
| 297 | how="inner", |
||
| 298 | ) |
||
| 299 | |||
| 300 | CTS_demands_grid.drop("zensus_population_id", axis=1, inplace=True) |
||
| 301 | CTS_demands_grid = CTS_demands_grid.groupby("bus_id").sum() |
||
| 302 | |||
| 303 | CTS_per_grid = pd.merge( |
||
| 304 | CTS_per_grid, |
||
| 305 | CTS_demands_grid[["demand"]], |
||
| 306 | how="inner", |
||
| 307 | right_on=CTS_per_grid.index, |
||
| 308 | left_on=CTS_demands_grid.index, |
||
| 309 | ) |
||
| 310 | |||
| 311 | CTS_per_grid = CTS_per_grid.rename(columns={"key_0": "bus_id"}) |
||
| 312 | CTS_per_grid.set_index("bus_id", inplace=True) |
||
| 313 | |||
| 314 | CTS_per_grid = CTS_per_grid[CTS_per_grid.columns[:-1]].multiply( |
||
| 315 | CTS_per_grid.demand, axis=0 |
||
| 316 | ) |
||
| 317 | |||
| 318 | CTS_per_grid.insert(0, "scenario", scenario) |
||
| 319 | |||
| 320 | CTS_grid = CTS_grid.append(CTS_per_grid) |
||
| 321 | CTS_grid = CTS_grid.sort_index() |
||
| 322 | |||
| 323 | CTS_per_zensus = 0 |
||
| 324 | |||
| 325 | else: |
||
| 326 | CTS_per_district = 0 |
||
| 327 | CTS_per_grid = 0 |
||
| 328 | |||
| 329 | CTS_per_zensus = pd.merge( |
||
| 330 | CTS_per_zensus, |
||
| 331 | demand, |
||
| 332 | how="inner", |
||
| 333 | right_on=CTS_per_zensus.index, |
||
| 334 | left_on=demand.zensus_population_id, |
||
| 335 | ) |
||
| 336 | CTS_per_zensus = CTS_per_zensus.drop("key_0", axis=1) |
||
| 337 | CTS_per_zensus.set_index("zensus_population_id", inplace=True) |
||
| 338 | |||
| 339 | CTS_per_zensus = CTS_per_zensus[ |
||
| 340 | CTS_per_zensus.columns[:-1] |
||
| 341 | ].multiply(CTS_per_zensus.demand, axis=0) |
||
| 342 | CTS_per_zensus.insert(0, "scenario", scenario) |
||
| 343 | |||
| 344 | CTS_per_zensus.reset_index(inplace=True) |
||
| 345 | |||
| 346 | CTS_zensus = CTS_zensus.append(CTS_per_grid) |
||
| 347 | CTS_zensus = CTS_zensus.set_index("bus_id") |
||
| 348 | CTS_zensus = CTS_zensus.sort_index() |
||
| 349 | |||
| 350 | return CTS_district, CTS_grid, CTS_zensus |
||
| 351 |