| Conditions | 2 |
| Total Lines | 76 |
| Code Lines | 38 |
| 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 | #!/usr/bin/env python3 |
||
| 111 | def plot_generation( |
||
| 112 | carrier,scenario, osm = False |
||
| 113 | ): |
||
| 114 | """ |
||
| 115 | Plots color maps according to the capacity of different generators |
||
| 116 | of the two existing scenarios (eGon2035 and eGon100RE) |
||
| 117 | |||
| 118 | |||
| 119 | Parameters |
||
| 120 | ---------- |
||
| 121 | carrier : generators |
||
| 122 | The list of generators: biomass, central_biomass_CHP, central_biomass_CHP_heat, |
||
| 123 | industrial_biomass_CHP, solar, solar_rooftop, wind_offshore, wind_onshore. |
||
| 124 | |||
| 125 | scenario: eGon2035, eGon100RE |
||
| 126 | |||
| 127 | |||
| 128 | """ |
||
| 129 | |||
| 130 | |||
| 131 | con = db.engine() |
||
| 132 | SQLBus = "SELECT bus_id, country FROM grid.egon_etrago_bus WHERE country='DE'" #imports buses of Germany |
||
| 133 | busDE = pd.read_sql(SQLBus,con) |
||
| 134 | busDE = busDE.rename({'bus_id': 'bus'},axis=1) |
||
| 135 | |||
| 136 | sql = "SELECT bus_id, geom FROM grid.egon_mv_grid_district"#Imports grid districs |
||
| 137 | distr = gpd.GeoDataFrame.from_postgis(sql, con) |
||
| 138 | distr = distr.rename({'bus_id': 'bus'},axis=1) |
||
| 139 | distr = distr.set_index("bus") |
||
| 140 | distr = pd.merge(busDE, distr, on='bus') #merges grid districts with buses |
||
| 141 | |||
| 142 | sqlCarrier = "SELECT carrier, p_nom, bus FROM grid.egon_etrago_generator" #Imports generator |
||
| 143 | sqlCarrier = "SELECT * FROM grid.egon_etrago_generator" |
||
| 144 | Carriers = pd.read_sql(sqlCarrier,con) |
||
| 145 | Carriers = Carriers.loc[Carriers['scn_name'] == scenario] |
||
| 146 | Carriers = Carriers.set_index("bus") |
||
| 147 | |||
| 148 | |||
| 149 | CarrierGen = Carriers.loc[Carriers['carrier'] == carrier] |
||
| 150 | |||
| 151 | Merge = pd.merge(CarrierGen, distr, on ='bus', how="outer") #merges districts with generators |
||
| 152 | |||
| 153 | |||
| 154 | Merge.loc[Merge ['carrier'] != carrier, "p_nom" ] = 0 |
||
| 155 | Merge.loc[Merge ['country'] != "DE", "p_nom" ] = 0 |
||
| 156 | |||
| 157 | gdf = gpd.GeoDataFrame(Merge , geometry='geom') |
||
| 158 | print(Merge) |
||
| 159 | print(gdf.crs) |
||
| 160 | pnom=gdf['p_nom'] |
||
| 161 | max_pnom=pnom.quantile(0.95) #0.95 quantile is used to filter values that are too high and make noise in the plots. |
||
| 162 | print(max_pnom) |
||
| 163 | gdf = gdf.to_crs(epsg=3857) |
||
| 164 | |||
| 165 | |||
| 166 | # Plot osm map in background |
||
| 167 | if osm != False: |
||
| 168 | #if network.srid == 4326: |
||
| 169 | #set_epsg_network(network) |
||
| 170 | fig, ax = plot_osm(osm['x'], osm['y'], osm['zoom']) |
||
| 171 | |||
| 172 | else: |
||
| 173 | fig, ax = plt.subplots(1, 1) |
||
| 174 | |||
| 175 | |||
| 176 | |||
| 177 | ax.set_axis_off(); |
||
| 178 | plt.title(f" {carrier} installed capacity in MW , {scenario}") |
||
| 179 | cmap = mpl.cm.coolwarm |
||
| 180 | norm = mpl.colors.Normalize(vmin=0, vmax=max_pnom) |
||
| 181 | gdf.plot(column='p_nom', ax=ax, legend=True, legend_kwds={'label': "p_nom(MW)", |
||
| 182 | |||
| 183 | 'orientation': "vertical"}, cmap=cmap, norm=norm, edgecolor='black', linewidth=0.1,zorder=2) |
||
| 184 | |||
| 185 | return 0 |
||
| 186 | plot_generation(carrier, scenario) |
||
| 187 | |||
| 198 |