| Conditions | 3 |
| Total Lines | 98 |
| Code Lines | 49 |
| 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 | # -*- coding: utf-8 -*- |
||
| 151 | def load_biogas_generators(scn_name): |
||
| 152 | """Define the biogas production units in Germany |
||
| 153 | |||
| 154 | Parameters |
||
| 155 | ---------- |
||
| 156 | scn_name : str |
||
| 157 | Name of the scenario. |
||
| 158 | |||
| 159 | Returns |
||
| 160 | ------- |
||
| 161 | CH4_generators_list : |
||
| 162 | Dataframe containing the biogas production units in Germany |
||
| 163 | |||
| 164 | """ |
||
| 165 | # read carrier information from scnario parameter data |
||
| 166 | scn_params = get_sector_parameters("gas", scn_name)
|
||
| 167 | |||
| 168 | basename = "Biogaspartner_Einspeiseatlas_Deutschland_2021.xlsx" |
||
| 169 | target_file = Path(".") / "datasets" / "gas_data" / basename
|
||
| 170 | |||
| 171 | # Read-in data from csv-file |
||
| 172 | biogas_generators_list = pd.read_excel( |
||
| 173 | target_file, |
||
| 174 | usecols=["Koordinaten", "Einspeisung Biomethan [(N*m^3)/h)]"], |
||
| 175 | ) |
||
| 176 | |||
| 177 | x = [] |
||
| 178 | y = [] |
||
| 179 | for index, row in biogas_generators_list.iterrows(): |
||
| 180 | coordinates = row["Koordinaten"].split(",")
|
||
| 181 | y.append(coordinates[0]) |
||
| 182 | x.append(coordinates[1]) |
||
| 183 | biogas_generators_list["x"] = x |
||
| 184 | biogas_generators_list["y"] = y |
||
| 185 | |||
| 186 | biogas_generators_list = gpd.GeoDataFrame( |
||
| 187 | biogas_generators_list, |
||
| 188 | geometry=gpd.points_from_xy( |
||
| 189 | biogas_generators_list["x"], biogas_generators_list["y"] |
||
| 190 | ), |
||
| 191 | ) |
||
| 192 | biogas_generators_list = biogas_generators_list.rename( |
||
| 193 | columns={"geometry": "geom"}
|
||
| 194 | ).set_geometry("geom", crs=4326)
|
||
| 195 | |||
| 196 | # Connect to local database |
||
| 197 | engine = db.engine() |
||
| 198 | |||
| 199 | # Cut data to federal state if in testmode |
||
| 200 | boundary = settings()["egon-data"]["--dataset-boundary"] |
||
| 201 | if boundary != "Everything": |
||
| 202 | db.execute_sql( |
||
| 203 | """ |
||
| 204 | DROP TABLE IF EXISTS grid.egon_biogas_generator CASCADE; |
||
| 205 | """ |
||
| 206 | ) |
||
| 207 | biogas_generators_list.to_postgis( |
||
| 208 | "egon_biogas_generator", |
||
| 209 | engine, |
||
| 210 | schema="grid", |
||
| 211 | index=False, |
||
| 212 | if_exists="replace", |
||
| 213 | ) |
||
| 214 | |||
| 215 | sql = """SELECT * |
||
| 216 | FROM grid.egon_biogas_generator, boundaries.vg250_sta_union as vg |
||
| 217 | WHERE ST_Transform(vg.geometry,4326) && egon_biogas_generator.geom |
||
| 218 | AND ST_Contains(ST_Transform(vg.geometry,4326), egon_biogas_generator.geom)""" |
||
| 219 | |||
| 220 | biogas_generators_list = gpd.GeoDataFrame.from_postgis( |
||
| 221 | sql, con=engine, geom_col="geom", crs=4326 |
||
| 222 | ) |
||
| 223 | biogas_generators_list = biogas_generators_list.drop( |
||
| 224 | columns=["id", "bez", "area_ha", "geometry"] |
||
| 225 | ) |
||
| 226 | db.execute_sql( |
||
| 227 | """ |
||
| 228 | DROP TABLE IF EXISTS grid.egon_biogas_generator CASCADE; |
||
| 229 | """ |
||
| 230 | ) |
||
| 231 | |||
| 232 | # Insert p_nom |
||
| 233 | conversion_factor = 0.01083 # m^3/h to MWh/h |
||
| 234 | biogas_generators_list["p_nom"] = [ |
||
| 235 | i * conversion_factor |
||
| 236 | for i in biogas_generators_list["Einspeisung Biomethan [(N*m^3)/h)]"] |
||
| 237 | ] |
||
| 238 | |||
| 239 | # Add missing columns |
||
| 240 | biogas_generators_list["marginal_cost"] = scn_params["marginal_cost"][ |
||
| 241 | "biogas" |
||
| 242 | ] |
||
| 243 | |||
| 244 | # Remove useless columns |
||
| 245 | biogas_generators_list = biogas_generators_list.drop( |
||
| 246 | columns=["x", "y", "Koordinaten", "Einspeisung Biomethan [(N*m^3)/h)]"] |
||
| 247 | ) |
||
| 248 | return biogas_generators_list |
||
| 249 | |||
| 337 |