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