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