Conditions | 2 |
Total Lines | 80 |
Code Lines | 37 |
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 |
||
108 | def plot_installedcapacity( |
||
109 | carrier,scenario, osm = False |
||
110 | ): |
||
111 | """ |
||
112 | Plots color maps according to the capacity of different generators |
||
113 | of the two existing scenarios (eGon2035 and eGon100RE) |
||
114 | |||
115 | |||
116 | Parameters |
||
117 | ---------- |
||
118 | carrier : generators |
||
119 | The list of generators: biomass, central_biomass_CHP, central_biomass_CHP_heat, |
||
120 | industrial_biomass_CHP, solar, solar_rooftop, wind_offshore, wind_onshore. |
||
121 | |||
122 | scenario: eGon2035, eGon100RE |
||
123 | |||
124 | |||
125 | """ |
||
126 | |||
127 | #This function must be called while in the folder |
||
128 | #that contains the file egon-data.configuration.yaml. |
||
129 | con = db.engine() |
||
130 | #imports buses of Germany |
||
131 | SQLBus = "SELECT bus_id, country FROM grid.egon_etrago_bus WHERE country='DE'" |
||
132 | busDE = pd.read_sql(SQLBus,con) |
||
133 | busDE = busDE.rename({'bus_id': 'bus'},axis=1) |
||
134 | #Imports grid districs |
||
135 | sql = "SELECT bus_id, geom FROM grid.egon_mv_grid_district" |
||
136 | distr = gpd.GeoDataFrame.from_postgis(sql, con) |
||
137 | distr = distr.rename({'bus_id': 'bus'},axis=1) |
||
138 | distr = distr.set_index("bus") |
||
139 | #merges grid districts with buses |
||
140 | distr = pd.merge(busDE, distr, on='bus') |
||
141 | #Imports generator |
||
142 | sqlCarrier = "SELECT carrier, p_nom, bus FROM grid.egon_etrago_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 | #merges districts with generators |
||
151 | Merge = pd.merge(CarrierGen, distr, on ='bus', how="outer") |
||
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 | pnom=gdf['p_nom'] # |
||
159 | #0.95 quantile is used to filter values that are too high and make noise in the plots. |
||
160 | max_pnom=pnom.quantile(0.95) |
||
161 | gdf = gdf.to_crs(epsg=3857) |
||
162 | |||
163 | |||
164 | # Plot osm map in background |
||
165 | if osm != False: |
||
166 | #if network.srid == 4326: |
||
167 | #set_epsg_network(network) |
||
168 | fig, ax = plot_osm(osm['x'], osm['y'], osm['zoom']) |
||
169 | |||
170 | else: |
||
171 | fig, ax = plt.subplots(1, 1) |
||
172 | |||
173 | |||
174 | |||
175 | ax.set_axis_off(); |
||
176 | plt.title(f" {carrier} installed capacity in MW , {scenario}") |
||
177 | cmap = mpl.cm.coolwarm |
||
178 | |||
179 | |||
180 | norm = mpl.colors.Normalize(vmin=0, vmax=max_pnom) |
||
181 | gdf.plot(column='p_nom', ax=ax, legend=False, legend_kwds={'label': "p_nom(MW)", |
||
182 | |||
183 | 'orientation': "vertical"}, cmap=cmap, norm=norm, edgecolor='black', linewidth=0.1,zorder=2) |
||
184 | scatter = ax.collections[0] |
||
185 | cbar=plt.colorbar(scatter, ax=ax, extend='max') |
||
186 | cbar.set_label('p_nom(MW)', rotation=90) |
||
187 | return 0 |
||
188 | |||
193 |