Exemplo n.º 1
0
    n.storage_units['marginal_cost'] += n.storage_units.carrier.map(ep)


if __name__ == "__main__":
    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils.snakemake import MockSnakemake, Dict

        snakemake = MockSnakemake(output=['networks/elec.nc'])
        snakemake.input = snakemake.expand(
            Dict(base_network='networks/base.nc',
                 tech_costs='data/costs.csv',
                 regions="resources/regions_onshore.geojson",
                 powerplants="resources/powerplants.csv",
                 hydro_capacities='data/bundle/hydro_capacities.csv',
                 opsd_load=
                 'data/bundle/time_series_60min_singleindex_filtered.csv',
                 nuts3_shapes='resources/nuts3_shapes.geojson',
                 **{
                     'profile_' + t: "resources/profile_" + t + ".nc"
                     for t in snakemake.config['renewable']
                 }))

    logging.basicConfig(level=snakemake.config['logging_level'])

    n = pypsa.Network(snakemake.input.base_network)
    Nyears = n.snapshot_weightings.sum() / 8760.

    costs = load_costs(Nyears)
    ppl = load_powerplants(n)

    attach_load(n)
Exemplo n.º 2
0
if __name__ == __main__:
    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils.snakemake import MockSnakemake, Dict
        snakemake = MockSnakemake(
            path='..',
            wildcards={
                'clusters': '45,90,181,full',
                'country': 'all'
            },
            params=dict(techs=['onwind', 'offwind-ac', 'offwind-dc', 'solar']),
            input=Dict(
                **{
                    'full': 'networks/elec_s.nc',
                    '45': 'networks/elec_s_45.nc',
                    '90': 'networks/elec_s_90.nc',
                    '181': 'networks/elec_s_181.nc',
                }),
            output=['results/plots/cum_p_nom_max_{clusters}_{country}.pdf'])

    logging.basicConfig(level=snakemake.config['logging_level'])

    plot_kwds = dict(drawstyle="steps-post")

    clusters = snakemake.wildcards.clusters.split(',')
    techs = snakemake.params.techs
    country = snakemake.wildcards.country
    if country == 'all':
        country = None
    else:
Exemplo n.º 3
0
    _set_links_underwater_fraction(n)

    _replace_b2b_converter_at_country_border_by_link(n)

    _adjust_capacities_of_under_construction_branches(n)

    return n


if __name__ == "__main__":
    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils.snakemake import MockSnakemake, Dict
        snakemake = MockSnakemake(
            path='..',
            wildcards={},
            input=Dict(eg_buses='data/entsoegridkit/buses.csv',
                       eg_lines='data/entsoegridkit/lines.csv',
                       eg_links='data/entsoegridkit/links.csv',
                       eg_converters='data/entsoegridkit/converters.csv',
                       eg_transformers='data/entsoegridkit/transformers.csv',
                       parameter_corrections='data/parameter_corrections.yaml',
                       links_p_nom='data/links_p_nom.csv'),
            output=['networks/base_LC.nc'])

    logging.basicConfig(level=snakemake.config['logging_level'])

    n = base_network()
    n.export_to_netcdf(snakemake.output[0])
Exemplo n.º 4
0
if 'snakemake' not in globals():
    from vresutils.snakemake import MockSnakemake, Dict
    from snakemake.rules import expand
    import yaml
    snakemake = Dict()
    snakemake = MockSnakemake(
        path='..',
        wildcards=dict(network='elec', simpl='', clusters='90', lv='1.25', opts='Co2L-3H', attr='p_nom', ext="pdf"),
        input=dict(network="results/networks/{network}_s{simpl}_{clusters}_lv{lv}_{opts}.nc",
                   tech_costs="data/costs.csv"),
        output=dict(only_map="results/plots/{network}_s{simpl}_{clusters}_lv{lv}_{opts}_{attr}.{ext}",
                    ext="results/plots/{network}_s{simpl}_{clusters}_lv{lv}_{opts}_{attr}_ext.{ext}")
    )

import pypsa

from _helpers import load_network, aggregate_p, aggregate_costs
from vresutils import plot as vplot

import os
import pypsa
import pandas as pd
import geopandas as gpd
import numpy as np
from itertools import product, chain
from six.moves import map, zip
from six import itervalues, iterkeys
from collections import OrderedDict as odict

import matplotlib.pyplot as plt
import matplotlib as mpl
Exemplo n.º 5
0
    n = _adjust_capacities_of_under_construction_branches(n)

    return n


if __name__ == "__main__":
    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils.snakemake import MockSnakemake, Dict
        snakemake = MockSnakemake(
            path='..',
            wildcards={},
            input=Dict(eg_buses='data/entsoegridkit/buses.csv',
                       eg_lines='data/entsoegridkit/lines.csv',
                       eg_links='data/entsoegridkit/links.csv',
                       eg_converters='data/entsoegridkit/converters.csv',
                       eg_transformers='data/entsoegridkit/transformers.csv',
                       parameter_corrections='data/parameter_corrections.yaml',
                       links_p_nom='data/links_p_nom.csv',
                       links_tyndp='data/links_tyndp.csv',
                       country_shapes='resources/country_shapes.geojson',
                       offshore_shapes='resources/offshore_shapes.geojson',
                       europe_shape='resources/europe_shape.geojson'),
            output=['networks/base.nc'])

    logging.basicConfig(level=snakemake.config['logging_level'])

    n = base_network()
    n.export_to_netcdf(snakemake.output[0])
Exemplo n.º 6
0
        s = s.reset_index()
    s.to_file(fn, driver='GeoJSON')


if __name__ == "__main__":
    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils.snakemake import MockSnakemake, Dict
        snakemake = MockSnakemake(
            path='..',
            wildcards={},
            input=Dict(
                naturalearth=
                'data/bundle/naturalearth/ne_10m_admin_0_countries.shp',
                eez='data/bundle/eez/World_EEZ_v8_2014.shp',
                nuts3='data/bundle/NUTS_2013_60M_SH/data/NUTS_RG_60M_2013.shp',
                nuts3pop='data/bundle/nama_10r_3popgdp.tsv.gz',
                nuts3gdp='data/bundle/nama_10r_3gdp.tsv.gz',
                ch_cantons='data/bundle/ch_cantons.csv',
                ch_popgdp='data/bundle/je-e-21.03.02.xls'),
            output=Dict(country_shapes='resources/country_shapes.geojson',
                        offshore_shapes='resource/offshore_shapes.geojson',
                        europe_shape='resources/europe_shape.geojson',
                        nuts3_shapes='resources/nuts3_shapes.geojson'))

    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)
Exemplo n.º 7
0
# coding: utf-8

import logging
import pandas as pd
from scipy.spatial import cKDTree as KDTree

import pypsa
import powerplantmatching as ppm

if 'snakemake' not in globals():
    from vresutils.snakemake import MockSnakemake, Dict

    snakemake = MockSnakemake(input=Dict(base_network='networks/base.nc'),
                              output=['resources/powerplants.csv'])

logging.basicConfig(level=snakemake.config['logging_level'])

n = pypsa.Network(snakemake.input.base_network)

ppl = (ppm.collection.matched_data()[lambda df: ~df.Fueltype.isin(
    ('Solar', 'Wind'))].pipe(ppm.cleaning.clean_technology).assign(
        Fueltype=lambda df: (df.Fueltype.where(
            df.Fueltype != 'Natural Gas',
            df.Technology.replace('Steam Turbine', 'OCGT').fillna('OCGT')))).
       pipe(ppm.utils.fill_geoposition, parse=True,
            only_saved_locs=True).pipe(ppm.heuristics.fill_missing_duration))

# ppl.loc[(ppl.Fueltype == 'Other') & ppl.Technology.str.contains('CCGT'), 'Fueltype'] = 'CCGT'
# ppl.loc[(ppl.Fueltype == 'Other') & ppl.Technology.str.contains('Steam Turbine'), 'Fueltype'] = 'CCGT'

ppl = ppl.loc[ppl.lon.notnull() & ppl.lat.notnull()]
Exemplo n.º 8
0
        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__':
    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils.snakemake import MockSnakemake, Dict
        snakemake = MockSnakemake(
            wildcards=Dict(technology='solar'),
            input=Dict(
                base_network="networks/base.nc",
                corine="data/bundle/corine/g250_clc06_V18_5.tif",
                natura="resources/natura.tiff",
                gebco="data/bundle/GEBCO_2014_2D.nc",
                country_shapes='resources/country_shapes.geojson',
                offshore_shapes='resources/offshore_shapes.geojson',
                pietzker="data/pietzker2014.xlsx"
            ),
            output=Dict(
                area="resources/country_flh_area_{technology}.csv",
                aggregated="resources/country_flh_aggregated_{technology}.csv",
                uncorrected="resources/country_flh_uncorrected_{technology}.csv",
                plot="resources/country_flh_{technology}.pdf",
                exclusion="resources/country_exclusion_{technology}"
Exemplo n.º 9
0
def remove_stubs(n):
    logger.info("Removing stubs")

    busmap = busmap_by_stubs(n, ['country'])
    _aggregate_and_move_components(n, busmap)

    return n, busmap


if __name__ == "__main__":
    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils.snakemake import MockSnakemake, Dict
        snakemake = MockSnakemake(
            path='..',
            wildcards=Dict(simpl=''),
            input=Dict(network='networks/elec.nc',
                       regions_onshore='resources/regions_onshore.geojson',
                       regions_offshore='resources/regions_offshore.geojson'),
            output=Dict(
                network='networks/elec_s{simpl}.nc',
                regions_onshore='resources/regions_onshore_s{simpl}.geojson',
                regions_offshore='resources/regions_offshore_s{simpl}.geojson')
        )

    logger = logging.getLogger()
    logger.setLevel(snakemake.config['logging_level'])

    n = pypsa.Network(snakemake.input.network)

    n, trafo_map = simplify_network_to_380(n)
Exemplo n.º 10
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__":
    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils.snakemake import MockSnakemake, Dict
        snakemake = MockSnakemake(
            wildcards=Dict(network='elec', simpl='', clusters='45'),
            input=Dict(
                network='networks/{network}_s{simpl}.nc',
                regions_onshore=
                'resources/regions_onshore_{network}_s{simpl}.geojson',
                regions_offshore=
                'resources/regions_offshore_{network}_s{simpl}.geojson'),
            output=Dict(
                network='networks/{network}_s{simpl}_{clusters}.nc',
                regions_onshore=
                'resources/regions_onshore_{network}_s{simpl}_{clusters}.geojson',
                regions_offshore=
                'resources/regions_offshore_{network}_s{simpl}_{clusters}.geojson'
            ))

    logging.basicConfig(level=snakemake.config['logging_level'])
Exemplo n.º 11
0
def add_custom_powerplants(ppl):
    custom_ppl_query = snakemake.config['electricity']['custom_powerplants']
    if not custom_ppl_query:
        return ppl
    add_ppls = pd.read_csv(snakemake.input.custom_powerplants, index_col=0)
    if isinstance(custom_ppl_query, str):
        add_ppls.query(add_ppls, inplace=True)
    return ppl.append(add_ppls, sort=False)


if __name__ == "__main__":
    if 'snakemake' not in globals():
        from vresutils.snakemake import MockSnakemake, Dict

        snakemake = MockSnakemake(input=Dict(
            base_network='networks/base.nc',
            custom_powerplants='data/custom_powerplants.csv'),
                                  output=['resources/powerplants.csv'])

    logging.basicConfig(level=snakemake.config['logging_level'])

    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'
        }
Exemplo n.º 12
0
    clustering = clustering_for_n_clusters(
        n,
        n_clusters,
        potential_mode=potential_mode,
        solver_name=snakemake.config['solving']['solver']['name'])

    return clustering.network, clustering.busmap


if __name__ == "__main__":
    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils.snakemake import MockSnakemake, Dict
        snakemake = MockSnakemake(
            path='..',
            wildcards=Dict(simpl='1024', network='elec'),
            input=Dict(network='networks/{network}.nc',
                       tech_costs="data/costs.csv",
                       regions_onshore="resources/regions_onshore.geojson",
                       regions_offshore="resources/regions_offshore.geojson"),
            output=Dict(
                network='networks/{network}_s{simpl}.nc',
                regions_onshore=
                "resources/regions_onshore_{network}_s{simpl}.geojson",
                regions_offshore=
                "resources/regions_offshore_{network}_s{simpl}.geojson",
                clustermaps='resources/clustermaps_{network}_s{simpl}.h5'))

    logging.basicConfig(level=snakemake.config['logging_level'])

    n = pypsa.Network(snakemake.input.network)
Exemplo n.º 13
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__":
    # Detect running outside of snakemake and mock snakemake for testing
    if 'snakemake' not in globals():
        from vresutils.snakemake import MockSnakemake, Dict
        snakemake = MockSnakemake(
            wildcards=Dict(network='elec', simpl='', clusters='45'),
            input=Dict(
                network='networks/{network}_s{simpl}.nc',
                regions_onshore='resources/regions_onshore_{network}_s{simpl}.geojson',
                regions_offshore='resources/regions_offshore_{network}_s{simpl}.geojson',
                clustermaps='resources/clustermaps_{network}_s{simpl}.h5',
                tech_costs='data/costs.csv',

            ),
            output=Dict(
                network='networks/{network}_s{simpl}_{clusters}.nc',
                regions_onshore='resources/regions_onshore_{network}_s{simpl}_{clusters}.geojson',
                regions_offshore='resources/regions_offshore_{network}_s{simpl}_{clusters}.geojson',
                clustermaps='resources/clustermaps_{network}_s{simpl}_{clusters}.h5'
            )
        )