| Conditions | 4 |
| Total Lines | 175 |
| Code Lines | 127 |
| 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 MaStR dataset and write to DB tables |
||
| 160 | def import_mastr() -> None: |
||
| 161 | """Import MaStR data into database""" |
||
| 162 | engine = db.engine() |
||
| 163 | cfg = egon.data.config.datasets()["power_plants"] |
||
| 164 | |||
| 165 | cols_mapping = { |
||
| 166 | "all": { |
||
| 167 | "EinheitMastrNummer": "gens_id", |
||
| 168 | "EinheitBetriebsstatus": "status", |
||
| 169 | "Inbetriebnahmedatum": "commissioning_date", |
||
| 170 | "Postleitzahl": "postcode", |
||
| 171 | "Ort": "city", |
||
| 172 | "Bundesland": "federal_state", |
||
| 173 | "Nettonennleistung": "capacity", |
||
| 174 | "Einspeisungsart": "feedin_type", |
||
| 175 | }, |
||
| 176 | "pv": { |
||
| 177 | "Lage": "site_type", |
||
| 178 | "Nutzungsbereich": "usage_sector", |
||
| 179 | "Hauptausrichtung": "orientation_primary", |
||
| 180 | "HauptausrichtungNeigungswinkel": "orientation_primary_angle", |
||
| 181 | "Nebenausrichtung": "orientation_secondary", |
||
| 182 | "NebenausrichtungNeigungswinkel": "orientation_secondary_angle", |
||
| 183 | "EinheitlicheAusrichtungUndNeigungswinkel": "orientation_uniform", |
||
| 184 | "AnzahlModule": "module_count", |
||
| 185 | "zugeordneteWirkleistungWechselrichter": "capacity_inverter", |
||
| 186 | }, |
||
| 187 | "wind": { |
||
| 188 | "Lage": "site_type", |
||
| 189 | "Hersteller": "manufacturer_name", |
||
| 190 | "Typenbezeichnung": "type_name", |
||
| 191 | "Nabenhoehe": "hub_height", |
||
| 192 | "Rotordurchmesser": "rotor_diameter", |
||
| 193 | }, |
||
| 194 | "biomass": { |
||
| 195 | "Technologie": "technology", |
||
| 196 | "Hauptbrennstoff": "fuel_name", |
||
| 197 | "Biomasseart": "fuel_type", |
||
| 198 | "ThermischeNutzleistung": "th_capacity", |
||
| 199 | }, |
||
| 200 | "hydro": { |
||
| 201 | "ArtDerWasserkraftanlage": "plant_type", |
||
| 202 | "ArtDesZuflusses": "water_origin", |
||
| 203 | }, |
||
| 204 | } |
||
| 205 | |||
| 206 | source_files = { |
||
| 207 | "pv": cfg["sources"]["mastr_pv"], |
||
| 208 | "wind": cfg["sources"]["mastr_wind"], |
||
| 209 | "biomass": cfg["sources"]["mastr_biomass"], |
||
| 210 | "hydro": cfg["sources"]["mastr_hydro"], |
||
| 211 | } |
||
| 212 | target_tables = { |
||
| 213 | "pv": EgonPowerPlantsPv, |
||
| 214 | "wind": EgonPowerPlantsWind, |
||
| 215 | "biomass": EgonPowerPlantsBiomass, |
||
| 216 | "hydro": EgonPowerPlantsHydro, |
||
| 217 | } |
||
| 218 | vlevel_mapping = { |
||
| 219 | "Höchstspannung": 1, |
||
| 220 | "UmspannungZurHochspannung": 2, |
||
| 221 | "Hochspannung": 3, |
||
| 222 | "UmspannungZurMittelspannung": 4, |
||
| 223 | "Mittelspannung": 5, |
||
| 224 | "UmspannungZurNiederspannung": 6, |
||
| 225 | "Niederspannung": 7, |
||
| 226 | } |
||
| 227 | |||
| 228 | # import locations |
||
| 229 | locations = pd.read_csv(cfg["sources"]["mastr_location"], index_col=None) |
||
| 230 | |||
| 231 | # import grid districts |
||
| 232 | mv_grid_districts = db.select_geodataframe( |
||
| 233 | f""" |
||
| 234 | SELECT * FROM {cfg['sources']['egon_mv_grid_district']} |
||
| 235 | """, |
||
| 236 | epsg=4326, |
||
| 237 | ) |
||
| 238 | |||
| 239 | # import units |
||
| 240 | technologies = ["pv", "wind", "biomass", "hydro"] |
||
| 241 | for tech in technologies: |
||
| 242 | # read units |
||
| 243 | print(f"Importing MaStR dataset: {tech}:") |
||
| 244 | print(" Reading CSV and filtering data...") |
||
| 245 | units = pd.read_csv( |
||
| 246 | source_files[tech], |
||
| 247 | usecols=( |
||
| 248 | ["LokationMastrNummer", "Laengengrad", "Breitengrad", "Land"] |
||
| 249 | + list(cols_mapping["all"].keys()) |
||
| 250 | + list(cols_mapping[tech].keys()) |
||
| 251 | ), |
||
| 252 | index_col=None, |
||
| 253 | dtype={"Postleitzahl": str}, |
||
| 254 | ).rename(columns=cols_mapping) |
||
| 255 | |||
| 256 | # drop units outside of Germany |
||
| 257 | len_old = len(units) |
||
| 258 | units = units.loc[units.Land == "Deutschland"] |
||
| 259 | print(f" {len_old-len(units)} units outside of Germany dropped...") |
||
| 260 | |||
| 261 | # filter for SH units if in testmode |
||
| 262 | if not TESTMODE_OFF: |
||
| 263 | print( |
||
| 264 | """ TESTMODE: |
||
| 265 | Dropping all units outside of Schleswig-Holstein... |
||
| 266 | """ |
||
| 267 | ) |
||
| 268 | units = units.loc[units.Bundesland == "SchleswigHolstein"] |
||
| 269 | |||
| 270 | # merge and rename voltage level |
||
| 271 | print(" Merging with locations and allocate voltage level...") |
||
| 272 | units = units.merge( |
||
| 273 | locations[["MaStRNummer", "Spannungsebene"]], |
||
| 274 | left_on="LokationMastrNummer", |
||
| 275 | right_on="MaStRNummer", |
||
| 276 | how="left", |
||
| 277 | ) |
||
| 278 | units["voltage_level"] = units.Spannungsebene.replace(vlevel_mapping) |
||
| 279 | |||
| 280 | # add geometry |
||
| 281 | print(" Adding geometries...") |
||
| 282 | units = gpd.GeoDataFrame( |
||
| 283 | units, |
||
| 284 | geometry=gpd.points_from_xy( |
||
| 285 | units["Laengengrad"], units["Breitengrad"], crs=4326 |
||
| 286 | ), |
||
| 287 | crs=4326, |
||
| 288 | ) |
||
| 289 | units_wo_geom = len( |
||
| 290 | units.loc[(units.Laengengrad.isna() | units.Laengengrad.isna())] |
||
| 291 | ) |
||
| 292 | print( |
||
| 293 | f" {units_wo_geom}/{len(units)} units do not have a geometry!" |
||
| 294 | ) |
||
| 295 | |||
| 296 | # drop unnecessary and rename columns |
||
| 297 | print(" Reformatting...") |
||
| 298 | units.drop( |
||
| 299 | columns=[ |
||
| 300 | "LokationMastrNummer", |
||
| 301 | "MaStRNummer", |
||
| 302 | "Laengengrad", |
||
| 303 | "Breitengrad", |
||
| 304 | "Spannungsebene", |
||
| 305 | "Land", |
||
| 306 | ], |
||
| 307 | inplace=True, |
||
| 308 | ) |
||
| 309 | mapping = cols_mapping["all"].copy() |
||
| 310 | mapping.update(cols_mapping[tech]) |
||
| 311 | mapping.update({"geometry": "geom"}) |
||
| 312 | units.rename(columns=mapping, inplace=True) |
||
| 313 | units["voltage_level"] = units.voltage_level.fillna(-1).astype(int) |
||
| 314 | if tech == "hydro": |
||
| 315 | units["plant_type"] = units.plant_type.fillna(-1).astype(int) |
||
| 316 | units.set_geometry("geom", inplace=True) |
||
| 317 | units["id"] = range(0, len(units)) |
||
| 318 | |||
| 319 | # assign bus ids |
||
| 320 | print(" Assigning bus ids...") |
||
| 321 | units = ( |
||
| 322 | units.loc[~units.geom.x.isna()] |
||
| 323 | .sjoin(mv_grid_districts[["bus_id", "geom"]], how="left") |
||
| 324 | .drop(columns=["index_right"]) |
||
| 325 | ) |
||
| 326 | units["bus_id"] = units.bus_id.fillna(-1).astype(int) |
||
| 327 | |||
| 328 | # write to DB |
||
| 329 | print(" Writing to DB...") |
||
| 330 | units.to_postgis( |
||
| 331 | name=target_tables[tech].__tablename__, |
||
| 332 | con=engine, |
||
| 333 | if_exists="append", |
||
| 334 | schema=target_tables[tech].__table_args__["schema"], |
||
| 335 | ) |
||
| 336 |