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