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