if country is not None: generators = generators.loc[generators.country == country] generators["cum_p_nom_max"] = generators["p_nom_max"].cumsum() / 1e6 return generators if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('plot_p_nom_max', network='elec', simpl='', techs='solar,onwind,offwind-dc', ext='png', clusts='5,full', country='all') configure_logging(snakemake) plot_kwds = dict(drawstyle="steps-post") clusters = snakemake.wildcards.clusts.split(',') techs = snakemake.wildcards.techs.split(',') country = snakemake.wildcards.country if country == 'all': country = None else: plot_kwds['marker'] = 'x'
@author: fabian """ import netallocation as ntl import pypsa import xarray as xr import geopandas as gpd import cartopy.crs as ccrs from config import to_explanation, source_carrier import matplotlib.pyplot as plt import os if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('plot_price_maps', nname='test-de10bf', method='ptpf', power='net') if not os.path.isdir(snakemake.output.folder): os.mkdir(snakemake.output.folder) n = pypsa.Network(snakemake.input.network) regions = gpd.read_file(snakemake.input.regions).set_index('name') payments = xr.open_dataset(snakemake.input.payments).sum(source_carrier) w = ntl.cost.snapshot_weightings(n) payments['one_port_operational_cost'] /= w payments['co2_cost'] /= w demand = ntl.power_demand(n, per_carrier=True).sel(carrier='Load', drop=True)\ .rename(bus="sink")
if out_logging: _logger.info( "Stage 3/5: Unify protected shape area. Step 3: Set geometry of unified shape" ) unified_shape = gpd.GeoDataFrame(geometry=[unified_shape_file], crs=area_crs) return unified_shape if __name__ == "__main__": if "snakemake" not in globals(): from _helpers import mock_snakemake os.chdir(os.path.dirname(os.path.abspath(__file__))) snakemake = mock_snakemake("build_natura_raster") configure_logging(snakemake) # get crs area_crs = snakemake.config["crs"]["area_crs"] out_logging = True inputs = snakemake.input cutouts = inputs.cutouts shapefiles = get_fileshapes(inputs) xs, Xs, ys, Ys = zip( *(determine_cutout_xXyY(cutout, out_logging=out_logging) for cutout in cutouts)) bounds = transform_bounds(CUTOUT_CRS, area_crs, min(xs), min(ys), max(Xs), max(Ys)) transform, out_shape = get_transform_and_shape(bounds,
text = ax.text(1.1,(bottom-0.5*data)[-1]-3,opts['nice_names_n'].get(ind,ind)) texts.append(text) ax.set_ylabel("Average system cost [Eur/MWh]") ax.set_ylim([0, 80]) # opts['costs_max']]) ax.set_xlim([0, 1]) #ax.set_xticks([0.5]) ax.set_xticklabels([]) #["w/o\nEp", "w/\nEp"]) ax.grid(True, axis="y", color='k', linestyle='dotted') if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('plot_network', network='elec', simpl='', clusters='5', ll='copt', opts='Co2L-24H', attr='p_nom', ext="pdf") configure_logging(snakemake) set_plot_style() opts = snakemake.config['plotting'] map_figsize = opts['map']['figsize'] map_boundaries = opts['map']['boundaries'] n = load_network_for_plots(snakemake.input.network, snakemake.input.tech_costs, snakemake.config) scenario_opts = snakemake.wildcards.opts.split('-') fig, ax = plt.subplots(figsize=map_figsize, subplot_kw={"projection": ccrs.PlateCarree()}) plot_map(n, ax, snakemake.wildcards.attr, opts)
n.import_components_from_dataframe(lines, "Line") n.import_components_from_dataframe(transformers, "Transformer") n.import_components_from_dataframe(links, "Link") n.import_components_from_dataframe(converters, "Link") n = _remove_unconnected_components(n) _set_lines_s_nom_from_linetypes(n) _apply_parameter_corrections(n) _set_countries_and_substations(n) _set_links_underwater_fraction(n) _replace_b2b_converter_at_country_border_by_link(n) n = _adjust_capacities_of_under_construction_branches(n) return n if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('base_network') configure_logging(snakemake) n = base_network() n.export_to_netcdf(snakemake.output[0])
clustering = clustering_for_n_clusters( n, n_clusters, custom_busmap=False, potential_mode=potential_mode, solver_name=snakemake.config['solving']['solver']['name'], focus_weights=focus_weights) return clustering.network, clustering.busmap if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('simplify_network', simpl='', network='elec') configure_logging(snakemake) n = pypsa.Network(snakemake.input.network) n, trafo_map = simplify_network_to_380(n) n, simplify_links_map = simplify_links(n) n, stub_map = remove_stubs(n) busmaps = [trafo_map, simplify_links_map, stub_map] if snakemake.wildcards.simpl: n, cluster_map = cluster(n, int(snakemake.wildcards.simpl))
return s.str[0].astype(float) * s.str[1].astype(float) def extract_coordinates(s): regex = (r"(\d{1,2})°(\d{1,2})′(\d{1,2})″(N|S) " r"(\d{1,2})°(\d{1,2})′(\d{1,2})″(E|W)") e = s.str.extract(regex, expand=True) lat = (e[0].astype(float) + (e[1].astype(float) + e[2].astype(float)/60.)/60.)*e[3].map({'N': +1., 'S': -1.}) lon = (e[4].astype(float) + (e[5].astype(float) + e[6].astype(float)/60.)/60.)*e[7].map({'E': +1., 'W': -1.}) return lon, lat if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake #rule must be enabled in config snakemake = mock_snakemake('prepare_links_p_nom', simpl='', network='elec') configure_logging(snakemake) links_p_nom = pd.read_html('https://en.wikipedia.org/wiki/List_of_HVDC_projects', header=0, match="SwePol")[0] mw = "Power (MW)" m_b = links_p_nom[mw].str.contains('x').fillna(False) links_p_nom.loc[m_b, mw] = links_p_nom.loc[m_b, mw].str.split('x').pipe(multiply) links_p_nom[mw] = links_p_nom[mw].str.extract("[-/]?([\d.]+)", expand=False).astype(float) links_p_nom['x1'], links_p_nom['y1'] = extract_coordinates(links_p_nom['Converterstation 1']) links_p_nom['x2'], links_p_nom['y2'] = extract_coordinates(links_p_nom['Converterstation 2']) links_p_nom.dropna(subset=['x1', 'y1', 'x2', 'y2']).to_csv(snakemake.output[0], index=False)
@author: fabian """ import netallocation as ntl import pypsa import xarray as xr import geopandas as gpd import cartopy.crs as ccrs from config import to_explanation import matplotlib.pyplot as plt import os from helpers import combine_oneports, scfmt, load if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('plot_price_maps', nname='de50') plt.rc('figure', dpi=300) n = pypsa.Network(snakemake.input.network) regions = gpd.read_file(snakemake.input.regions).set_index('name') cost = (combine_oneports( xr.open_dataset(snakemake.input.costs).sum('snapshot')).set_index({ 'branch': ['component', 'branch_i'] }).transpose('sink', 'source', 'source_carrier', 'branch')) if 'price' in snakemake.output[0]: if any(n.snapshot_weightings.objective != 1): w = ntl.cost.snapshot_weightings(n) cost['one_port_operational_cost'] /= w cost['co2_cost'] /= w
m.set_snapshots(snapshot_weightings.index) m.snapshot_weightings = snapshot_weightings for c in n.iterate_components(): pnl = getattr(m, c.list_name+"_t") for k, df in iteritems(c.pnl): if not df.empty: pnl[k] = df.resample(offset).mean() return m if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('prepare_network', network='elec', simpl='', clusters='40', ll='v0.3', opts='Co2L-24H') configure_logging(snakemake) opts = snakemake.wildcards.opts.split('-') n = pypsa.Network(snakemake.input[0]) Nyears = n.snapshot_weightings.sum()/8760. set_line_s_max_pu(n) for o in opts: m = re.match(r'^\d+h$', o, re.IGNORECASE) if m is not None: n = average_every_nhours(n, m.group(0)) break else:
import xarray as xr import matplotlib.pyplot as plt import cartopy.crs as ccrs import matplotlib as mpl import pypsa from config import color, to_symbol, sink_dims, to_explanation import netallocation as ntl from matplotlib.pyplot import Line2D from pypsa.plot import projected_area_factor from netallocation.plot_helpers import ( make_handler_map_to_scale_circles_as_in, make_legend_circles_for) if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('plot_allocated_payment', nname='de50', sink='highest-lmp') n = pypsa.Network(snakemake.input.network) costs = xr.open_dataset(snakemake.input.costs) sink = snakemake.wildcards.sink regions = gpd.read_file(snakemake.input.regions).set_index('name') if sink == 'lowest-lmp': sink = n.buses_t.marginal_price.mean().idxmin() elif sink == 'highest-lmp': sink = n.buses_t.marginal_price.mean().idxmax() payment = (xr.open_dataset(snakemake.input.costs).sel( sink=sink, drop=True).fillna(0).sum('snapshot').rename( source='bus',
dx, dy = cutout.dx, cutout.dy return [x - dx / 2., X + dx / 2., y - dy / 2., Y + dy / 2.] def get_transform_and_shape(bounds, res): left, bottom = [(b // res) * res for b in bounds[:2]] right, top = [(b // res + 1) * res for b in bounds[2:]] shape = int((top - bottom) // res), int((right - left) / res) transform = rio.Affine(res, 0, left, 0, -res, top) return transform, shape if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('build_natura_raster') configure_logging(snakemake) cutouts = snakemake.input.cutouts xs, Xs, ys, Ys = zip(*(determine_cutout_xXyY(cutout) for cutout in cutouts)) bounds = transform_bounds(4326, 3035, min(xs), min(ys), max(Xs), max(Ys)) transform, out_shape = get_transform_and_shape(bounds, res=100) # adjusted boundaries shapes = gpd.read_file(snakemake.input.natura).to_crs(3035) raster = ~geometry_mask(shapes.geometry, out_shape[::-1], transform) raster = raster.astype(rio.uint8) with rio.open(snakemake.output[0], 'w',
# -*- coding: utf-8 -*- """ Created on Tue Sep 8 15:43:11 2020 @author: fabian """ import netallocation as ntl import pypsa import matplotlib.pyplot as plt import cartopy.crs as ccrs import geopandas as gpd import config if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('plot_operation_high_expenditure', nname='test-de10') n = pypsa.Network(snakemake.input.network) regions = gpd.read_file(snakemake.input.regions).set_index('name') sn = (n.loads_t.p * n.buses_t.marginal_price).sum(1).idxmax() # %% fig, ax = plt.subplots(subplot_kw={"projection": ccrs.EqualEarth()}, figsize=(5, 4)) prod = ntl.power_production(n, sn, per_carrier=True).to_pandas().stack() n.plot(flow=sn, line_widths=0.01, link_widths=.01, bus_sizes=prod/1e5, ax=ax, boundaries=regions.total_bounds[[0,2,1,3]], geomap='10m', link_colors='rosybrown')
if save_map is not None: ec.draw() plt.savefig(save_map, transparent=True) plt.close() availability = downsample_to_coarse_grid( bounds, dx, dy, ec.region, np.where(ec.region.mask, ec._availability, 0)) return csr_matrix(gk.raster.extractMatrix(availability).flatten() / 100.) if __name__ == '__main__': if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('build_renewable_profiles', technology='solar') configure_logging(snakemake) pgb.streams.wrap_stderr() config = snakemake.config['renewable'][snakemake.wildcards.technology] time = pd.date_range(freq='m', **snakemake.config['snapshots']) params = dict(years=slice(*time.year[[0, -1]]), months=slice(*time.month[[0, -1]])) cutout = atlite.Cutout(config['cutout'], cutout_dir=os.path.dirname(snakemake.input.cutout), **params) minx, maxx, miny, maxy = cutout.extent
logger = logging.getLogger(__name__) from _helpers import configure_logging from vresutils.graph import voronoi_partition_pts import os import pandas as pd import geopandas as gpd import pypsa if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('build_bus_regions') configure_logging(snakemake) countries = snakemake.config['countries'] n = pypsa.Network(snakemake.input.base_network) country_shapes = gpd.read_file(snakemake.input.country_shapes).set_index('name')['geometry'] offshore_shapes = gpd.read_file(snakemake.input.offshore_shapes).set_index('name')['geometry'] onshore_regions = [] offshore_regions = [] for country in countries: c_b = n.buses.country == country
add_ppls = pd.read_csv(snakemake.input.custom_powerplants, index_col=0, dtype={'bus': 'str'}) if isinstance(custom_ppl_query, str): add_ppls.query(custom_ppl_query, inplace=True) return ppl.append(add_ppls, sort=False, ignore_index=True, verify_integrity=True) if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('build_powerplants') configure_logging(snakemake) n = pypsa.Network(snakemake.input.base_network) countries = n.buses.country.unique() ppl = (pm.powerplants(from_url=True).powerplant.fill_missing_decommyears( ).powerplant.convert_country_to_alpha2().query( 'Fueltype not in ["Solar", "Wind"] and Country in @countries' ).replace({ 'Technology': { 'Steam Turbine': 'OCGT' } }).assign(Fueltype=lambda df: (df.Fueltype.where( df.Fueltype != 'Natural Gas', df.Technology.replace('Steam Turbine', 'OCGT').fillna('OCGT')))))
def set_line_nom_max(n): s_nom_max_set = snakemake.config["lines"].get("s_nom_max,", np.inf) p_nom_max_set = snakemake.config["links"].get("p_nom_max", np.inf) n.lines.s_nom_max.clip(upper=s_nom_max_set, inplace=True) n.links.p_nom_max.clip(upper=p_nom_max_set, inplace=True) if __name__ == "__main__": if "snakemake" not in globals(): from _helpers import mock_snakemake os.chdir(os.path.dirname(os.path.abspath(__file__))) snakemake = mock_snakemake( "prepare_network", network="elec", simpl="", clusters="10", ll="v0.3", opts="Co2L-24H", ) configure_logging(snakemake) opts = snakemake.wildcards.opts.split("-") n = pypsa.Network(snakemake.input[0]) Nyears = n.snapshot_weightings.objective.sum() / 8760.0 set_line_s_max_pu(n) for o in opts: m = re.match(r"^\d+h$", o, re.IGNORECASE) if m is not None:
# Functions def haversine(p): coord0 = n.buses.loc[p.bus0, ["x", "y"]].values coord1 = n.buses.loc[p.bus1, ["x", "y"]].values return 1.5 * haversine_pts(coord0, coord1) if __name__ == "__main__": if "snakemake" not in globals(): from _helpers import mock_snakemake os.chdir(os.path.dirname(os.path.abspath(__file__))) snakemake = mock_snakemake( "augmented_line_connections", network="elec", simpl="", clusters="10" ) configure_logging(snakemake) n = pypsa.Network(snakemake.input.network) Nyears = n.snapshot_weightings.sum().values[0] / 8760.0 costs = load_costs( Nyears, snakemake.input.tech_costs, snakemake.config["costs"], snakemake.config["electricity"], ) options = snakemake.config["augmented_line_connection"] min_expansion_option = options.get("min_expansion") k_edge_option = options.get("connectivity_upgrade", 3) line_type_option = options.get("new_line_type", "HVDC")
Description ----------- """ import logging logger = logging.getLogger(__name__) from _helpers import configure_logging import os import atlite if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('build_cutout', cutout='europe-2013-era5') configure_logging(snakemake) cutout_params = snakemake.config['atlite']['cutouts'][ snakemake.wildcards.cutout] for p in ('xs', 'ys', 'years', 'months'): if p in cutout_params: cutout_params[p] = slice(*cutout_params[p]) cutout = atlite.Cutout(snakemake.wildcards.cutout, cutout_dir=os.path.dirname(snakemake.output[0]), **cutout_params) cutout.prepare(nprocesses=snakemake.config['atlite'].get('nprocesses', 4))
def to_csv(dfs): dir = snakemake.output[0] os.makedirs(dir, exist_ok=True) for key, df in iteritems(dfs): df.to_csv(os.path.join(dir, f"{key}.csv")) if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('make_summary', network='elec', simpl='', clusters='5', ll='copt', opts='Co2L-24H', country='all') network_dir = os.path.join('..', 'results', 'networks') else: network_dir = os.path.join('results', 'networks') configure_logging(snakemake) def expand_from_wildcard(key): w = getattr(snakemake.wildcards, key) return snakemake.config["scenario"][key] if w == "all" else [w] if snakemake.wildcards.ll.endswith("all"): ll = snakemake.config["scenario"]["ll"] if len(snakemake.wildcards.ll) == 4:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Aug 26 13:21:30 2020 @author: fabian """ import pypsa import xarray as xr import pandas as pd import os from helpers import combine_oneports, load if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('write_tables', nname='test-de10', sink='DE0 1') n = pypsa.Network(snakemake.input.network) costs = xr.open_dataset(snakemake.input.costs) outdir = snakemake.output[0] os.makedirs(outdir, exist_ok=True) # %% # capital and operational prices cols = ['marginal_cost', 'capital_cost'] n.lines['marginal_cost'] = 0 adjust_cp = (lambda df: df.assign(capital_cost=df.capital_cost / df.length) if 'length' in df else df) prices = pd.concat( {
ax.legend(handles, labels, ncol=4, loc="upper left") fig.tight_layout() if fn is not None: fig.savefig(fn, transparent=True) if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('plot_summary', summary='energy', network='elec', simpl='', clusters=5, ll='copt', opts='Co2L-24H', attr='', ext='png', country='all') configure_logging(snakemake) summary = snakemake.wildcards.summary try: func = globals()[f"plot_{summary}"] except KeyError: raise RuntimeError( f"plotting function for {summary} has not been defined") func(os.path.join(snakemake.input[0], f"{summary}.csv"), snakemake.output[0])
busmap = reduce(lambda x, y: x.map(y), busmaps[1:], busmaps[0]) for which in ('regions_onshore', 'regions_offshore'): regions = gpd.read_file(getattr(input, which)).set_index('name') geom_c = regions.geometry.groupby(busmap).apply( shapely.ops.cascaded_union) regions_c = gpd.GeoDataFrame(dict(geometry=geom_c)) regions_c.index.name = 'name' save_to_geojson(regions_c, getattr(output, which)) if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('cluster_network', network='elec', simpl='', clusters='5') configure_logging(snakemake) n = pypsa.Network(snakemake.input.network) focus_weights = snakemake.config.get('focus_weights', None) renewable_carriers = pd.Index([ tech for tech in n.generators.carrier.unique() if tech.split('-', 2)[0] in snakemake.config['renewable'] ]) if snakemake.wildcards.clusters.endswith('m'): n_clusters = int(snakemake.wildcards.clusters[:-1]) aggregate_carriers = pd.Index(
if cf_solving.get('skip_iterations', False): network_lopf(n, solver_name=solver_name, solver_options=solver_options, extra_functionality=extra_functionality, **kwargs) else: ilopf(n, solver_name=solver_name, solver_options=solver_options, track_iterations=track_iterations, min_iterations=min_iterations, max_iterations=max_iterations, extra_functionality=extra_functionality, **kwargs) return n if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('solve_network', network='elec', simpl='', clusters='5', ll='copt', opts='Co2L-BAU-CCL-24H') configure_logging(snakemake) tmpdir = snakemake.config['solving'].get('tmpdir') if tmpdir is not None: Path(tmpdir).mkdir(parents=True, exist_ok=True) opts = snakemake.wildcards.opts.split('-') solve_opts = snakemake.config['solving']['options'] fn = getattr(snakemake.log, 'memory', None) with memory_logger(filename=fn, interval=30.) as mem: n = pypsa.Network(snakemake.input[0]) n = prepare_network(n, solve_opts) n = solve_network(n, config=snakemake.config, solver_dir=tmpdir, solver_log=snakemake.log.solver, opts=opts) n.export_to_netcdf(snakemake.output[0])
p_min_pu=-1, p_nom_extendable=True, length=h2_links.length.values, capital_cost=costs.at["H2 pipeline", "capital_cost"] * h2_links.length, efficiency=costs.at["H2 pipeline", "efficiency"], carrier="H2 pipeline", ) if __name__ == "__main__": if "snakemake" not in globals(): from _helpers import mock_snakemake os.chdir(os.path.dirname(os.path.abspath(__file__))) snakemake = mock_snakemake( "add_extra_components", network="elec", simpl="", clusters=10 ) configure_logging(snakemake) n = pypsa.Network(snakemake.input.network) Nyears = n.snapshot_weightings.objective.sum() / 8760.0 costs = load_costs( Nyears, tech_costs=snakemake.input.tech_costs, config=snakemake.config["costs"], elec_config=snakemake.config["electricity"], ) attach_storageunits(n, costs) attach_stores(n, costs) attach_hydrogen_pipelines(n, costs)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Jul 20 17:50:06 2020 @author: fabian """ import pypsa import netallocation as ntl if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('solve_tiny_network') n = ntl.test.get_network_ac_dc() n.generators['p_nom_min'] = 500 n.generators['p_nom_max'] = 3000 # n.carriers = n.carriers.drop('battery') n.add('StorageUnit', 'battery', bus='Frankfurt', capital_cost=50, p_nom_extendable=True, marginal_cost=10, carrier='battery') n.lopf(pyomo=False, keep_shadowprices=True, solver_name='gurobi') n.export_to_netcdf(snakemake.output[0])
:mod:`build_renewable_profiles` """ import logging logger = logging.getLogger(__name__) from _helpers import configure_logging import os import atlite import geopandas as gpd from vresutils import hydro as vhydro if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('build_hydro_profile') configure_logging(snakemake) config = snakemake.config['renewable']['hydro'] cutout = atlite.Cutout(config['cutout'], cutout_dir=os.path.dirname(snakemake.input.cutout)) countries = snakemake.config['countries'] country_shapes = gpd.read_file(snakemake.input.country_shapes).set_index( 'name')['geometry'].reindex(countries) country_shapes.index.name = 'countries' eia_stats = vhydro.get_eia_annual_hydro_generation( snakemake.input.eia_hydro_generation).reindex(columns=countries) inflow = cutout.runoff(shapes=country_shapes, smooth=True,
midpoints = (breaks[1:] + breaks[:-1])/2. p = p_area.T # Per-country FLH comparison agg_p = pd.Series((p / p.sum()).multiply(midpoints, axis=0).sum(), name="Pietzker") agg_p['Overall'] = float((p.sum(axis=1) / p.sum().sum()).multiply(midpoints, axis=0).sum()) agg = pd.DataFrame({'PyPSA-Eur': agg_a, 'Pietzcker': agg_p, 'Ratio': agg_p / agg_a}) agg.to_csv(fn) if __name__ == '__main__': if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('build_country_flh', technology='solar') configure_logging(snakemake) pgb.streams.wrap_stderr() config = snakemake.config['renewable'][snakemake.wildcards.technology] time = pd.date_range(freq='m', **snakemake.config['snapshots']) params = dict(years=slice(*time.year[[0, -1]]), months=slice(*time.month[[0, -1]])) cutout = atlite.Cutout(config['cutout'], cutout_dir=os.path.dirname(snakemake.input.cutout), **params) minx, maxx, miny, maxy = cutout.extent
return df def save_to_geojson(df, fn): if os.path.exists(fn): os.unlink(fn) if not isinstance(df, gpd.GeoDataFrame): df = gpd.GeoDataFrame(dict(geometry=df)) df = df.reset_index() schema = {**gpd.io.file.infer_schema(df), 'geometry': 'Unknown'} df.to_file(fn, driver='GeoJSON', schema=schema) if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('build_shapes') configure_logging(snakemake) country_shapes = countries() save_to_geojson(country_shapes, snakemake.output.country_shapes) offshore_shapes = eez(country_shapes) save_to_geojson(offshore_shapes, snakemake.output.offshore_shapes) europe_shape = country_cover(country_shapes, offshore_shapes) save_to_geojson(gpd.GeoSeries(europe_shape), snakemake.output.europe_shape) nuts3_shapes = nuts3(country_shapes) save_to_geojson(nuts3_shapes, snakemake.output.nuts3_shapes)
carrier_i = n.carriers.index nice_names = (pd.Series(config['plotting']['nice_names']) .reindex(carrier_i).fillna(carrier_i.to_series().str.title())) n.carriers['nice_name'] = nice_names colors = pd.Series(config['plotting']['tech_colors']).reindex(carrier_i) if colors.isna().any(): missing_i = list(colors.index[colors.isna()]) logger.warning(f'tech_colors for carriers {missing_i} not defined ' 'in config.') n.carriers['color'] = colors if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('add_electricity') configure_logging(snakemake) n = pypsa.Network(snakemake.input.base_network) Nyears = n.snapshot_weightings.sum() / 8760. costs = load_costs(Nyears) ppl = load_powerplants() attach_load(n) update_transmission_costs(n, costs) attach_conventional_generators(n, costs, ppl) attach_wind_and_solar(n, costs) attach_hydro(n, costs, ppl)
""" import pypsa import pandas as pd import xarray as xr import matplotlib.pyplot as plt from config import color, to_total_symbol import numpy as np from pypsa.descriptors import nominal_attrs plt.rc('font', family='serif') plt.rc("text", usetex=False) if __name__ == "__main__": if 'snakemake' not in globals(): from _helpers import mock_snakemake snakemake = mock_snakemake('plot_total_costs', nname='acdc') n = pypsa.Network(snakemake.input.network) ca = xr.open_dataset(snakemake.input.costs) stacked_cost = ca.sum().to_array().to_series() TC = n.objective_constant + n.objective if 'lv_limit' in n.global_constraints.index: for c in n.branch_components: if n.df(c).empty: continue mu_upper = (n.global_constraints.at['lv_limit', 'mu'] * n.df(c).length) nom = nominal_attrs[c] n.df(c)['mu_upper_' + nom] += mu_upper stacked_cost['subsidy_cost'] = 0