Beispiel #1
0
def fig_electricity_demand_by_state():
    """
    Comparison of two methods to get the electricity demand for the federal
    states of Germany. The reegis energy balance module and the reegis openego
    module. The openego module can be used for any region polygon.
    """
    plt.rcParams.update({"font.size": 14})
    ax = plt.figure(figsize=(9, 4)).add_subplot(1, 1, 1)
    eb = energy_balance.get_states_energy_balance(2014).swaplevel()
    total = eb.loc["Endenergieverbrauch", "electricity"].sum()
    elec_fs = eb.loc["Endenergieverbrauch", "electricity"]
    elec_fs.name = "energy balance"
    share = pd.DataFrame(elec_fs.div(total).mul(100))

    federal_states = geometries.get_federal_states_polygon()
    ego_demand = openego.get_ego_demand_by_region(federal_states,
                                                  "federal_states",
                                                  grouped=True)

    share["openego"] = ego_demand.div(ego_demand.sum()).mul(100)

    print(share)
    share.plot(kind="bar", ax=ax)

    share["diff"] = (share["energy balance"] -
                     share["openego"]) / share["energy balance"]
    print(share)

    ax.set_ylabel("share [%]")
    t = ("Share of overall electricity demand from the reegis\n energy"
         " balance module and from the reegis openego module.")
    plt.title(t)
    plt.subplots_adjust(right=0.96, left=0.09, bottom=0.13, top=0.85)
    return "electricity_demand_by_state"
Beispiel #2
0
def table_demand_federal_states(outpath):
    ego_year = 2014

    path = cfg.get('paths', 'data_my_reegis')
    filename = 'lak_electricity_demand_federal_states.csv'
    lak = pd.read_csv(os.path.join(path, filename), index_col=[0, 1], sep=';')
    lak = lak.swaplevel()
    federal_states = geometries.get_federal_states_polygon()
    ego = oego.get_ego_demand_by_region(
        federal_states, 'federal_states', grouped=True)

    ego_sum = ego.sum()

    lak = lak.rename(index=cfg.get_dict('STATES'))
    lak['Strom'] = pd.to_numeric(lak['Strom'], errors='coerce')

    new_table = pd.DataFrame(
        columns=pd.MultiIndex(levels=[[], []], codes=[[], []]),
        index=ego.index)

    new_table[('openEgo', ego_year)] = ego

    for y in [2010, 2011, 2012, 2013, 2014]:
        key = ('lak', y)
        new_table[key] = lak.loc[y]['Strom'] / 3.6
        f = new_table[key].sum() / ego_sum
        new_table[('frac', y)] = (((new_table[('openEgo', ego_year)] * f) -
                                  new_table[key]) / new_table[key])

    fk = new_table[('lak', ego_year)].sum() / ego_sum
    new_table[('openEgo', ego_year)] = new_table[('openEgo', ego_year)] * fk
    new_table.sort_index(axis=1, inplace=True)
    new_table.to_excel(
        os.path.join(outpath, 'federal_states_demand_ego_lak.xls'))
    print(new_table)
Beispiel #3
0
 def test_demand_by_region(self):
     region_demand = (
         openego.get_ego_demand_by_region(self.geo, "test", infile=self.fn)
         .groupby("test")
         .sum()
     )
     eq_(int(region_demand.loc["BB", "consumption"]), 279)
     eq_(int(region_demand.loc["BY", "consumption"]), 7290)
Beispiel #4
0
def get_open_ego_slp_profile_by_region(
    region,
    year,
    name,
    annual_demand=None,
    filename=None,
    dynamic_H0=True,
):
    """
    Create standardised load profiles (slp) for each region.

    Parameters
    ----------
    region : geopandas.geoDataFrame
        Regions set.
    year : int
        Year.
    name : str
        Name of the region set.
    annual_demand : float
        Annual demand for all regions.
    filename : str (optional)
        Filename of the output file.
    dynamic_H0 : bool (optional)
        Use the dynamic function of the H0. If you doubt, "True" might be the
        tight choice (default: True)

    Returns
    -------

    """
    ego_demand = openego.get_ego_demand_by_region(region,
                                                  name,
                                                  sectors=True,
                                                  dump=True)

    # Add holidays
    cal = Germany()
    holidays = dict(cal.holidays(year))

    # Drop geometry column and group by region
    ego_demand.drop("geometry", inplace=True, axis=1)
    ego_demand_grouped = ego_demand.groupby(name).sum()

    if filename is None:
        path = cfg.get("paths", "demand")
        filename = os.path.join(path,
                                "open_ego_slp_profile_{0}.csv").format(name)

    if not os.path.isfile(filename):
        regions = ego_demand_grouped.index
    else:
        regions = []

    # Create standardised load profiles (slp)
    fs_profile = pd.DataFrame()
    for region in regions:
        logging.info("Create SLP for {0}".format(region))
        annual_demand_type = ego_demand_grouped.loc[region]

        annual_electrical_demand_per_sector = {
            "g0":
            annual_demand_type.sector_consumption_retail,
            "h0":
            annual_demand_type.sector_consumption_residential,
            "l0":
            annual_demand_type.sector_consumption_agricultural,
            "i0":
            annual_demand_type.sector_consumption_industrial +
            annual_demand_type.sector_consumption_large_consumers,
        }
        e_slp = bdew.ElecSlp(year, holidays=holidays)

        elec_demand = e_slp.get_profile(annual_electrical_demand_per_sector,
                                        dyn_function_h0=dynamic_H0)

        # Add the slp for the industrial group
        ilp = particular_profiles.IndustrialLoadProfile(e_slp.date_time_index,
                                                        holidays=holidays)

        elec_demand["i0"] = ilp.simple_profile(
            annual_electrical_demand_per_sector["i0"])
        elec_demand = elec_demand.resample("H").mean()
        elec_demand.columns = pd.MultiIndex.from_product([[region],
                                                          elec_demand.columns])
        fs_profile = pd.concat([fs_profile, elec_demand], axis=1)

    if not os.path.isfile(filename):
        fs_profile.set_index(fs_profile.index -
                             pd.DateOffset(hours=1)).to_csv(filename)

    df = pd.read_csv(
        filename,
        index_col=[0],
        header=[0, 1],
        parse_dates=True,
        date_parser=lambda col: pd.to_datetime(col, utc=True),
    ).tz_convert("Europe/Berlin")

    if annual_demand is None:
        return df
    else:
        return df.mul(annual_demand / df.sum().sum())
Beispiel #5
0
def get_entsoe_profile_by_region(region,
                                 year,
                                 name,
                                 annual_demand,
                                 version=None):
    """

    Parameters
    ----------
    region
    year
    name
    annual_demand : str or numeric
        A numeric annual value or a method to fetch the annual value.
        Valid methods are: bmwi, entsoe, openego
    version : str or None
        Version of the opsd file. If set to "None" the latest version is used.

    Returns
    -------
    pandas.DataFrame : A table with a time series for each region. The unit
        is MW for the internal methods or the same unit order as the input
        of the annual_demand parameter.
    Examples
    --------
    >>> my_fs=geometries.get_federal_states_polygon()
    >>> d1=get_entsoe_profile_by_region(my_fs, 2014, 'federal_states', 'entsoe'
    ...     )  # doctest: +SKIP
    >>> int(d1.sum().sum())  # doctest: +SKIP
    519757349
    >>> d2=get_entsoe_profile_by_region(my_fs, 2014, 'federal_states', 'bmwi'
    ...     )  # doctest: +SKIP
    >>> int(d2.sum().sum())  # doctest: +SKIP
    523988000
    >>> d3=get_entsoe_profile_by_region(my_fs, 2014, 'federal_states', 200000
    ...     )  # doctest: +SKIP
    >>> round(d3.sum().sum())  # doctest: +SKIP
    200000.0

    """
    logging.debug("Get entsoe profile {0} for {1}".format(name, year))

    profile = entsoe.get_entsoe_load(year, version=version)["DE_load_"]
    idx = profile.index
    profile.reset_index(drop=True, inplace=True)
    norm_profile = profile.div(profile.sum() / 1000000)
    ego_demand = openego.get_ego_demand_by_region(region, name, grouped=True)

    if annual_demand == "bmwi":
        annual_demand = (bmwi_data.get_annual_electricity_demand_bmwi(year) *
                         10**6)
    elif annual_demand == "entsoe":
        annual_demand = profile.sum()
    elif annual_demand == "openego":
        annual_demand = ego_demand.sum() * 10**3
    elif isinstance(annual_demand, (int, float)):
        pass
    else:
        msg = ("{0} of type {1} is not a valid input for 'annual_demand'.\n"
               "Use 'bmwi', 'entsoe' or a float/int value.")
        raise ValueError(msg.format(annual_demand, type(annual_demand)))

    demand_fs = (ego_demand.div(ego_demand.sum() /
                                1000).mul(annual_demand).div(1000))

    df = (pd.DataFrame([demand_fs.values] * len(norm_profile),
                       columns=demand_fs.index).mul(norm_profile,
                                                    axis=0).div(1000000))
    return df.set_index(idx.tz_convert("Europe/Berlin"))