Exemple #1
0
def add_additional_values(table_collection):
    """

    Parameters
    ----------
    table_collection

    Returns
    -------

    """
    transf = table_collection["power plants"]
    for values in ["variable_costs", "downtime_factor"]:
        if cfg.get("creator", "use_{0}".format(values)) is True:
            add_values = getattr(data.get_ewi_data(), values)
            if cfg.has_option("creator", "downtime_bioenergy"):
                add_values.loc["bioenergy",
                               "value"] = cfg.get("creator",
                                                  "downtime_bioenergy")
            transf = transf.merge(
                add_values,
                right_index=True,
                how="left",
                left_on="fuel",
            )
            transf.drop(["unit", "source"], axis=1, inplace=True)
            transf.rename({"value": values}, axis=1, inplace=True)
        else:
            transf[values] = 0
    table_collection["power plants"] = transf
    return table_collection
Exemple #2
0
def fetch_data_coordinates_by_id(coastdat_id):
    """
    Returns the coordinates of the weather data set.

    Parameters
    ----------
    coastdat_id : int or str
        ID of the coastdat weather data set

    Returns
    -------
    namedtuple : Fields are latitude and longitude

    Examples
    --------
    >>> location=fetch_data_coordinates_by_id(1132101)
    >>> round(location.latitude, 3)
    53.692
    >>> round(location.longitude, 3)
    11.351
    """
    coord = namedtuple("weather_location", "latitude, longitude")
    coastdat_polygons = geometries.load(
        cfg.get("paths", "geometry"),
        cfg.get("coastdat", "coastdatgrid_polygon"),
    )
    c = coastdat_polygons.loc[int(coastdat_id)].geometry.centroid
    return coord(latitude=c.y, longitude=c.x)
Exemple #3
0
def pv_yield_by_orientation():
    global START
    START = datetime.datetime.now()

    reduced = get_coastdat_onshore_polygons()

    sandia_modules = pvlib.pvsystem.retrieve_sam('SandiaMod')
    sapm_inverters = pvlib.pvsystem.retrieve_sam('cecinverter')
    module = sandia_modules['LG_LG290N1C_G3__2013_']
    inverter = sapm_inverters['ABB__MICRO_0_25_I_OUTD_US_208_208V__CEC_2014_']
    system = {'module': module, 'inverter': inverter}

    system['installed_capacity'] = (system['module']['Impo'] *
                                    system['module']['Vmpo'])

    orientation_sets = []

    n = 2

    for n in range(18):
        ts = n * 10
        te = (n + 1) * 10
        if te == 90:
            te = 91
        orientation_sets.append(
            sorted(
                set((x / 2, y / 2) for x in range(ts, te)
                    for y in range(0, 721))))

    year = 2014
    key = 1129089

    weather_file_name = os.path.join(
        cfg.get('paths', 'coastdat'),
        cfg.get('coastdat', 'file_pattern').format(year=year))
    if not os.path.isfile(weather_file_name):
        coastdat.get_coastdat_data(year, weather_file_name)
    weather = pd.read_hdf(weather_file_name, mode='r', key='/A' + str(key))

    path = os.path.join(cfg.get('paths', 'analysis'),
                        'pv_yield_by_orientation_c', '{0}')

    point = reduced.centroid[key]

    # pv_orientation(key, point, weather, system, orientation, path)
    coastdat_fields = []
    for orientation in orientation_sets:
        d = "tilt_{0}".format(str(orientation[0][0]).replace('.', ''))
        coastdat_fields.append({
            'key': key,
            'geom': point,
            'weather': weather,
            'system': system,
            'orientation': orientation,
            'path': path.format(d),
        })
    p = multiprocessing.Pool(6)
    p.map(_pv_orientation, coastdat_fields)
    p.close()
    p.join()
Exemple #4
0
def get_entsoe_renewable_data(file=None, version=None):
    """
    Load the default file for re time series or a specific file.

    Returns
    -------

    Examples
    --------
    >>> my_re=get_entsoe_renewable_data()
    >>> int(my_re['DE_solar_generation_actual'].sum())
    188160676
    """
    if version is None:
        version = cfg.get("entsoe", "timeseries_version")
    path_pattern = os.path.join(cfg.get("paths", "entsoe"), "{0}")
    if file is None:
        fn = path_pattern.format(
            cfg.get("entsoe", "renewables_file_csv").format(version=version))
    else:
        fn = file.format(version=version)

    if not os.path.isfile(fn):
        if file is None:
            renewables = split_timeseries_file(version=version).renewables
            renewables.to_csv(fn)

    re = pd.read_csv(
        fn,
        index_col=[0],
        parse_dates=True,
        date_parser=lambda x: datetime.datetime.strptime(
            x.split("+")[0], "%Y-%m-%d %H:%M:%S"),
    )
    return re
Exemple #5
0
def get_kba_table():
    """
    Get the "kfz" table for all vehicles and the "pkw" table for more
    statistics about passenger cars.

    Returns
    -------
    namedtuple

    Examples
    --------
    >>> table = get_kba_table()
    >>> kfz = table.kfz
    >>> print(type(kfz))
    <class 'pandas.core.frame.DataFrame'>
    """
    kba_table = namedtuple("kba_table", "kfz pkw")
    kba_filename = os.path.join(cfg.get("paths", "general"),
                                cfg.get("mobility", "table_kba"))

    # Download table if it does not exit
    if not os.path.isfile(kba_filename):
        tools.download_file(kba_filename, cfg.get("mobility", "url_kba"))

    return kba_table(
        kfz=format_kba_table(kba_filename, "Kfz_u_Kfz_Anh"),
        pkw=format_kba_table(kba_filename, "Pkw"),
    )
Exemple #6
0
def deflex_profile_from_entsoe(year,
                               share,
                               annual_demand=None,
                               overwrite=False):
    load_file = os.path.join(cfg.get('paths', 'entsoe'),
                             cfg.get('entsoe', 'load_file'))

    if not os.path.isfile(load_file) or overwrite:
        reegis.entsoe.split_timeseries_file(overwrite)

    # Fetch de load profile from entsoe
    de_load_profile = reegis.entsoe.get_entsoe_load(year).DE_load_

    load_profile = pd.DataFrame(index=de_load_profile.index)
    regions = pd.read_csv(os.path.join(cfg.get('paths', 'geo_deflex'),
                                       cfg.get('geometry',
                                               'deflex_polygon')).format(
                                                   map=cfg.get('init', 'map'),
                                                   type='polygon',
                                                   suffix='reegis'),
                          index_col=[0])

    for region in regions.index:
        region = 'DE{:02}'.format(region)
        if region not in share:
            share[region] = 0
        load_profile[region] = de_load_profile.multiply(float(share[region]))

    if annual_demand is not None:
        load_profile = load_profile.div(
            load_profile.sum().sum()).multiply(annual_demand)
    return load_profile
Exemple #7
0
def split_timeseries_file(filename=None, overwrite=False, version=None):
    """Split table into load and renewables."""
    entsoe_ts = namedtuple("entsoe", ["load", "renewables"])
    logging.info("Splitting time series.")
    if version is None:
        version = cfg.get("entsoe", "timeseries_version")
    path_pattern = os.path.join(cfg.get("paths", "entsoe"), "{0}")
    if filename is None:
        filename = path_pattern.format(
            cfg.get("entsoe", "de_file").format(version=version))

    if not os.path.isfile(filename) or overwrite:
        prepare_de_file(filename, overwrite, version)

    de_ts = pd.read_csv(
        filename.format(version=version),
        index_col="utc_timestamp",
        parse_dates=True,
        date_parser=lambda col: pd.to_datetime(col, utc=True),
    )
    de_ts.index = de_ts.index.tz_convert("Europe/Berlin")
    de_ts.index.rename("cet_timestamp", inplace=True)

    de_ts["DE_load_"] = de_ts["DE_load_actual_entsoe_transparency"]

    if "DE_load_actual_entsoe_power_statistics" in de_ts:
        berlin = pytz.timezone("Europe/Berlin")
        end_date = berlin.localize(datetime.datetime(2015, 1, 1, 0, 0, 0))
        de_ts.loc[de_ts.index < end_date, "DE_load_"] = de_ts.loc[
            de_ts.index < end_date, "DE_load_actual_entsoe_power_statistics"]

    load = pd.DataFrame(de_ts[pd.notnull(de_ts["DE_load_"])]["DE_load_"],
                        columns=["DE_load_"])

    re_columns = [
        "DE_solar_capacity",
        "DE_solar_generation_actual",
        "DE_solar_profile",
        "DE_wind_capacity",
        "DE_wind_generation_actual",
        "DE_wind_profile",
        "DE_wind_offshore_capacity",
        "DE_wind_offshore_generation_actual",
        "DE_wind_offshore_profile",
        "DE_wind_onshore_capacity",
        "DE_wind_onshore_generation_actual",
        "DE_wind_onshore_profile",
    ]
    re_subset = [
        "DE_solar_capacity",
        "DE_solar_generation_actual",
        "DE_solar_profile",
        "DE_wind_capacity",
        "DE_wind_generation_actual",
        "DE_wind_profile",
    ]

    renewables = de_ts.dropna(subset=re_subset, how="any")[re_columns]

    return entsoe_ts(load=load, renewables=renewables)
Exemple #8
0
def scenario_elec_demand(year, table, weather_year=None):
    if weather_year is None:
        demand_year = year
    else:
        demand_year = weather_year

    annual_demand = cfg.get('electricity_demand', 'annual_demand')
    demand_method = cfg.get('electricity_demand', 'demand_method')

    if annual_demand == 'bmwi':
        annual_demand = reegis.bmwi.get_annual_electricity_demand_bmwi(year)
        msg = ("Unit of BMWI electricity demand is 'TWh'. "
               "Will multiply it with {0} to get 'MWh'")
        converter = 1e+6
        annual_demand = annual_demand * 1e+6
        logging.warning(msg.format(converter))

    df = deflex.demand.get_deflex_profile(demand_year,
                                          demand_method,
                                          annual_demand=annual_demand)
    df = pd.concat([df], axis=1, keys=['electrical_load']).swaplevel(0, 1, 1)
    df = df.reset_index(drop=True)
    if not calendar.isleap(year) and len(df) > 8760:
        df = df.iloc[:8760]
    return pd.concat([table, df], axis=1).sort_index(1)
Exemple #9
0
def scenario_transmission(table_collection):
    vs = table_collection['volatile_source']

    # This should be done automatic e.g. if representative point outside the
    # landmass polygon.
    offshore_regions = (cfg.get_dict_list('offshore_regions_set')[cfg.get(
        'init', 'map')])

    elec_trans = deflex.transmission.get_electrical_transmission_deflex()

    # Set transmission capacity of offshore power lines to installed capacity
    # Multiply the installed capacity with 1.1 to get a buffer of 10%.
    for offreg in offshore_regions:
        elec_trans.loc[elec_trans.index.str.contains(offreg),
                       'capacity'] = (vs[offreg].sum().sum() * 1.1)

    elec_trans = (pd.concat([elec_trans], axis=1,
                            keys=['electrical']).sort_index(1))
    general_efficiency = cfg.get('transmission', 'general_efficiency')
    if general_efficiency is not None:
        elec_trans['electrical', 'efficiency'] = general_efficiency
    else:
        msg = ("The calculation of the efficiency by distance is not yet "
               "implemented")
        raise NotImplementedError(msg)
    if cfg.get('init', 'map') == 'de22':
        elec_trans.loc['DE22-DE01', ('electrical', 'efficiency')] = 0.9999
        elec_trans.loc['DE22-DE01', ('electrical', 'capacity')] = 9999999
    return elec_trans
Exemple #10
0
def read_original_timeseries_file(overwrite=False):
    """Read timeseries file if it exists. Otherwise download it from opsd.
    """

    orig_csv_file = os.path.join(cfg.get('paths', 'entsoe'),
                                 cfg.get('entsoe', 'original_file'))
    readme = os.path.join(cfg.get('paths', 'entsoe'),
                          cfg.get('entsoe', 'readme_file'))
    json = os.path.join(cfg.get('paths', 'entsoe'),
                        cfg.get('entsoe', 'json_file'))

    if not os.path.isfile(orig_csv_file) or overwrite:
        req = requests.get(cfg.get('entsoe', 'timeseries_data'))
        if not overwrite:
            logging.warning("File not found. Try to download it from server.")
        else:
            logging.warning("Will download file from server and overwrite"
                            "existing ones")
        logging.warning("Check URL if download does not work.")
        with open(orig_csv_file, 'wb') as fout:
            fout.write(req.content)
        logging.warning("Downloaded from {0} and copied to '{1}'.".format(
            cfg.get('entsoe', 'timeseries_data'), orig_csv_file))
        req = requests.get(cfg.get('entsoe', 'timeseries_readme'))
        with open(readme, 'wb') as fout:
            fout.write(req.content)
        req = requests.get(cfg.get('entsoe', 'timeseries_json'))
        with open(json, 'wb') as fout:
            fout.write(req.content)

    orig = pd.read_csv(orig_csv_file, index_col=[0], parse_dates=True)
    orig = orig.tz_localize('UTC').tz_convert('Europe/Berlin')
    return orig
Exemple #11
0
def get_ew_by_deflex_subregions(year):
    """Get a GeoDataFrame with the inhabitants of each region.

    Parameters
    ----------
    year : int

    Returns
    -------
    geopandas.geoDataFrame
    """
    deflex_sub = reegis.geometries.load(
        cfg.get('paths', 'geo_deflex'),
        cfg.get('geometry', 'overlap_federal_states_deflex_polygon').format(
            map=cfg.get('init', 'map')))
    deflex_sub['state'] = deflex_sub.index.to_series().str[2:]
    deflex_sub['region'] = deflex_sub.index.to_series().str[:2]
    deflex_sub['ew'] = reegis.inhabitants.get_ew_by_region(
        year, deflex_sub, name='deflex_subregions')

    deflex_sub = deflex_sub.replace({'state': cfg.get_dict('STATE_KEYS')})
    deflex_sub['region'] = deflex_sub.region.astype(str).apply(
        'DE{:0>2}'.format)
    no_inhabitants = deflex_sub[deflex_sub.ew == 0]
    deflex_sub = deflex_sub[deflex_sub.ew != 0]
    logging.info("States with no inhabitants have been removed: {0}".format(
        no_inhabitants.index))

    return deflex_sub
Exemple #12
0
def test_opsd2reegis():
    path = os.path.join(os.path.dirname(__file__), 'data')
    cfg.tmp_set('paths', 'opsd', path)
    cfg.tmp_set('paths', 'powerplants', path)
    fn_opsd = opsd.opsd_power_plants()
    fn_reegis = powerplants.pp_opsd2reegis()
    os.remove(fn_opsd)
    filename = str(fn_reegis.split(os.sep)[-1])

    geo_path = cfg.get('paths', 'geometry')
    geo_file = cfg.get('geometry', 'federalstates_polygon')
    gdf = geo.load(path=geo_path, filename=geo_file)
    powerplants.add_regions_to_powerplants(
        gdf, 'fed_states', filename=filename, path=path, dump=True)

    geo_path = cfg.get('paths', 'geometry')
    geo_file = cfg.get('coastdat', 'coastdatgrid_polygon')
    gdf = geo.load(path=geo_path, filename=geo_file)
    pp = powerplants.add_regions_to_powerplants(
        gdf, 'coastdat2', filename=filename, path=path, dump=False)

    os.remove(fn_reegis)
    eq_(int(pp.groupby('fed_states').sum().loc['BE', 'capacity']), 2427)

    year = 2000

    pp = powerplants.get_reegis_powerplants(year, pp=pp)
    eq_(int(pp.groupby('fed_states').sum().loc['BE', 'capacity_2000']), 2391)

    eq_(coastdat.windzone_region_fraction(
        pp, name='fed_states', year=year).round(2).loc['NI', 3], 0.24)
Exemple #13
0
def get_entsoe_load(year):
    """

    Parameters
    ----------
    year

    Returns
    -------

    Examples
    --------
    >>> entsoe=get_entsoe_load(2015)
    >>> int(entsoe.sum())
    477923089
    """
    filename = os.path.join(
        cfg.get("paths", "entsoe"), cfg.get("entsoe", "load_file")
    )
    if not os.path.isfile(filename):
        load = split_timeseries_file().load
        load.to_hdf(filename, "entsoe")

    # Read entsoe time series for the given year
    f = datetime.datetime(year, 1, 1, 0)
    t = datetime.datetime(year, 12, 31, 23)
    f = f.astimezone(pytz.timezone("Europe/Berlin"))
    t = t.astimezone(pytz.timezone("Europe/Berlin"))
    logging.info("Read entsoe load series from {0} to {1}".format(f, t))
    df = pd.DataFrame(pd.read_hdf(filename, "entsoe"))
    return df.loc[f:t]
Exemple #14
0
def get_ego_demand(filename=None, sectors=False, overwrite=False):
    """

    Parameters
    ----------
    filename : str
    sectors : bool
    overwrite : bool

    Returns
    -------
    pandas.DataFrame

    """
    if filename is None:
        path = cfg.get("paths", "demand")
        filename = os.path.join(path, cfg.get("open_ego", "ego_file"))

    if sectors is True:
        filename = filename.replace(".", "_sectors.")

    if os.path.isfile(filename) and not overwrite:
        return pd.DataFrame(pd.read_hdf(filename, "demand"))
    else:
        load = get_ego_data(osf=True, sectors=sectors)
        load.to_hdf(filename, "demand")
        return load
Exemple #15
0
def guess_coordinates_by_spatial_names_opsd(df, fs_column, cap_col,
                                            total_cap, stat):
    # *** Use municipal_code and federal_state to define coordinates ***
    if fs_column in df:
        if 'municipality_code' in df:
            if df.municipality_code.dtype == str:
                df.loc[df.municipality_code == 'AWZ', fs_column] = 'AWZ_NS'
        if 'postcode' in df:
            df.loc[df.postcode == '000XX', fs_column] = 'AWZ'
        states = df.loc[df.lon.isnull()].groupby(
            fs_column).sum()[cap_col]
        logging.debug("Fraction of undefined capacity by federal state " +
                      "(percentage):")
        for (state, capacity) in states.iteritems():
            logging.debug("{0}: {1:.4f}".format(
                state, capacity / total_cap * 100))
            stat.loc[state, 'undefined_capacity'] = capacity

        # A simple table with the centroid of each federal state.
        f2c = pd.read_csv(
            os.path.join(cfg.get('paths', 'geometry'),
                         cfg.get('geometry', 'federalstates_centroid')),
            index_col='name')

        # Use the centroid of each federal state if the federal state is given.
        # This is not very precise and should not be used for a high fraction
        # of plants.
        f2c = f2c.applymap(wkt_loads).centroid
        for l in df.loc[(df.lon.isnull() & df[fs_column].notnull())].index:
            if df.loc[l, fs_column] in f2c.index:
                df.loc[l, 'lon'] = f2c[df.loc[l, fs_column]].x
                df.loc[l, 'lat'] = f2c[df.loc[l, fs_column]].y
    return df
Exemple #16
0
def patch_offshore_wind(orig_df, columns):
    df = pd.DataFrame(columns=columns)

    offsh = pd.read_csv(os.path.join(
        cfg.get('paths', 'static_sources'),
        cfg.get('static_sources', 'patch_offshore_wind')),
                        header=[0, 1],
                        index_col=[0])

    offsh = offsh.loc[offsh['reegis', 'com_year'].notnull(), 'reegis']
    for column in offsh.columns:
        df[column] = offsh[column]
    df['decom_year'] = 2050
    df['decom_month'] = 12
    df['energy_source_level_1'] = 'Renewable energy'
    df['energy_source_level_2'] = 'Wind'
    df['energy_source_level_3'] = 'Offshore'
    goffsh = geo.create_geo_df(df)

    offsh_df = pd.DataFrame(goffsh)

    new_cap = offsh_df['capacity'].sum()
    old_cap = orig_df.loc[orig_df['technology'] == 'Offshore',
                          'capacity'].sum()

    # Remove Offshore technology from power plant table
    orig_df = orig_df.loc[orig_df['technology'] != 'Offshore']

    patched_df = pd.DataFrame(
        pd.concat([orig_df, offsh_df], ignore_index=True, sort=True))
    logging.warning(
        "Offshore wind is patched. {0} MW were replaced by {1} MW".format(
            old_cap, new_cap))
    return patched_df
Exemple #17
0
def get_heat_profiles_by_state(year=None, weather_year=None):
    if weather_year is None:
        heat_demand_state_file = os.path.join(
            cfg.get('paths', 'demand'),
            cfg.get('demand', 'heat_profile_state').format(year=year))
    else:
        heat_demand_state_file = os.path.join(
            cfg.get('paths', 'demand'),
            cfg.get('demand', 'heat_profile_state_var').format(
                year=year, weather_year=weather_year))

    # Load demand heat profiles by state
    if os.path.isfile(heat_demand_state_file):
        logging.info("Demand profiles by state exist. Reading file.")
        demand_state = pd.read_csv(heat_demand_state_file,
                                   index_col=[0],
                                   parse_dates=True,
                                   header=[0, 1, 2])
        demand_state = demand_state.tz_localize('UTC').tz_convert(
            'Europe/Berlin')
    else:
        demand_state = reegis.heat_demand.get_heat_profiles_by_state(
            year, to_csv=True, weather_year=weather_year)

    return demand_state
Exemple #18
0
def get_pv_wind_areas_by_nuts3(create_geojson=False):
    """
        Parameters
        ----------
        year : int
            Year of interest
        region_pick : list
            Selected regions in NUTS-3 format

        Returns: pd.DataFrame
            Dataframe containing yearly heat CTS heat consumption by NUTS-3 region
        -------
        """
    path = os.path.join(cfg.get("paths", "GLAES"), 'nuts3_geojson')

    if create_geojson:
        save_nuts3_to_geojson(path)

    fn = os.path.join(cfg.get("paths", "GLAES"), 'suitable_area_wind_pv.csv')

    if not os.path.isfile(fn):
        suitable_area = calc_wind_pv_areas(path)
        suitable_area.to_csv(fn)
    else:
        suitable_area = pd.read_csv(fn)
        suitable_area.set_index('nuts3', drop=True, inplace=True)

    return suitable_area
Exemple #19
0
def prepare_ego_demand(rmap=None, overwrite=False):
    if rmap is None:
        rmap = cfg.get('init', 'map')
    egofile_deflex = os.path.join(cfg.get('paths', 'demand'),
                                  cfg.get('demand',
                                          'ego_file_deflex')).format(map=rmap)

    if os.path.isfile(egofile_deflex) and not overwrite:
        ego_demand_deflex = pd.read_hdf(egofile_deflex, 'demand')
    else:
        ego_demand_df = reegis.openego.get_ego_demand(overwrite=overwrite)
        # Create GeoDataFrame from ego demand file.
        ego_demand = reegis.geometries.create_geo_df(ego_demand_df)

        # Load region polygons
        deflex_regions = deflex.geometries.deflex_regions()

        # Add column with region id
        ego_demand = reegis.geometries.spatial_join_with_buffer(ego_demand,
                                                                deflex_regions,
                                                                name=rmap +
                                                                '_region')

        # Overwrite Geometry object with its DataFrame, because it is not
        # needed anymore.
        ego_demand_deflex = pd.DataFrame(ego_demand)

        # Delete the geometry column, because spatial grouping will be done
        # only with the region column.
        del ego_demand_deflex['geometry']

        # Write out file (hdf-format).
        ego_demand_deflex.to_hdf(egofile_deflex, 'demand')

    return ego_demand_deflex.groupby('{0}_region'.format(rmap)).sum()
Exemple #20
0
def federal_state_average_weather(year, parameter):
    """
    Example for spatial_average_weather() with federal states polygons.

    Parameters
    ----------
    year
    parameter

    Returns
    -------

    """
    federal_states = geometries.load(
        cfg.get('paths', 'geometry'),
        cfg.get('geometry', 'federalstates_polygon'))
    filename = os.path.join(
        cfg.get('paths', 'coastdat'),
        'average_{0}_BB_TH_{1}.csv'.format(parameter, year))
    if not os.path.isfile(filename):
        spatial_average_weather(year,
                                federal_states,
                                parameter,
                                'federal_states',
                                outfile=filename)
    return pd.read_csv(filename, index_col=[0], parse_dates=True)
Exemple #21
0
def get_entsoe_load(year, version=None):
    """

    Parameters
    ----------
    year
    version

    Returns
    -------

    Examples
    --------
    >>> entsoe=get_entsoe_load(2015)
    >>> float(round(entsoe.sum()/1e6, 1))
    479.5
    """
    if version is None:
        version = cfg.get("entsoe", "timeseries_version")
    filename = os.path.join(cfg.get("paths", "entsoe"),
                            cfg.get("entsoe", "load_file"))
    if not os.path.isfile(filename):
        load = split_timeseries_file(version=version).load
        load.to_hdf(filename.format(version=version), "entsoe")

    # Read entsoe time series for the given year
    f = datetime.datetime(year, 1, 1, 0)
    t = datetime.datetime(year, 12, 31, 23)
    f = f.astimezone(pytz.timezone("Europe/Berlin"))
    t = t.astimezone(pytz.timezone("Europe/Berlin"))
    logging.info("Read entsoe load series from {0} to {1}".format(f, t))
    df = pd.DataFrame(pd.read_hdf(filename.format(version=version), "entsoe"))
    return df.loc[f:t]
Exemple #22
0
def aggregate_by_region(year, state):
    # Create the path for the output files.
    feedin_state_path = cfg.get('paths_pattern',
                                'state_feedin').format(year=year)
    os.makedirs(feedin_state_path, exist_ok=True)

    # Create pattern for the name of the resulting files.
    feedin_berlin_outfile_name = os.path.join(
        feedin_state_path,
        cfg.get('feedin', 'feedin_state_pattern').format(year=year,
                                                         type='{type}',
                                                         state=state))

    # Filter the capacity of the powerplants for the given year.
    pp = get_grouped_power_plants(year)

    # Loop over weather depending feed-in categories.
    # WIND and PV

    for cat in ['Wind', 'Solar']:
        outfile_name = feedin_berlin_outfile_name.format(type=cat.lower())
        if not os.path.isfile(outfile_name):
            aggregate_by_region_coastdat_feedin(pp, [state], year, cat,
                                                outfile_name)

    # HYDRO
    outfile_name = feedin_berlin_outfile_name.format(type='hydro')
    if not os.path.isfile(outfile_name):
        aggregate_by_region_hydro(pp, [state], year, outfile_name)

    # GEOTHERMAL
    outfile_name = feedin_berlin_outfile_name.format(type='geothermal')
    if not os.path.isfile(outfile_name):
        aggregate_by_region_geothermal([state], year, outfile_name)
Exemple #23
0
def get_ego_demand(overwrite=False):
    egofile = os.path.join(cfg.get('paths', 'demand'),
                           cfg.get('open_ego', 'ego_file'))
    if os.path.isfile(egofile) and not overwrite:
        return pd.read_hdf(egofile, 'demand')
    else:
        return prepare_ego_demand(egofile)
Exemple #24
0
def get_feedin_by_state(year, feedin_type, state):
    """

    Parameters
    ----------
    year
    feedin_type
    state : str
        Official abbreviation of state in Germany e.g. 'BE', 'SH', 'TH'...

    Returns
    -------

    """
    feedin_state_file_name = os.path.join(
        cfg.get('paths_pattern', 'state_feedin'),
        cfg.get('feedin', 'feedin_state_pattern')).format(year=year,
                                                          type=feedin_type,
                                                          state=state)

    # Add any federal state to get its normalised feed-in.
    if feedin_type in ['solar', 'wind']:
        if not os.path.isfile(feedin_state_file_name):
            aggregate_by_region(year, state)
        return pd.read_csv(feedin_state_file_name,
                           index_col=[0],
                           header=[0, 1, 2])
    elif feedin_type in ['hydro', 'geothermal']:
        if not os.path.isfile(feedin_state_file_name):
            aggregate_by_region(year, state)
        return pd.read_csv(feedin_state_file_name, index_col=[0], header=[0])
    else:
        return None
Exemple #25
0
def fetch_id_by_coordinates(latitude, longitude):
    """
    Get nearest weather data set to a given location.

    Parameters
    ----------
    latitude : float
    longitude : float

    Returns
    -------
    int : coastdat id

    Examples
    --------
    >>> fetch_id_by_coordinates(53.655119, 11.181475)
    1132101
    """
    coastdat_polygons = geometries.load(
        cfg.get("paths", "geometry"),
        cfg.get("coastdat", "coastdatgrid_polygon"),
    )
    location = Point(longitude, latitude)

    cid = coastdat_polygons[coastdat_polygons.contains(location)].index

    if len(cid) == 0:
        msg = "No id found for latitude {0} and longitude {1}."
        logging.warning(msg.format(latitude, longitude))
        return None
    elif len(cid) == 1:
        return cid[0]
Exemple #26
0
def get_time_series_for_one_location(latitude, longitude, year, set_name=None):
    coastdat_id = fetch_id_by_coordinates(latitude, longitude)

    # set_name = 'M_LG290G3__I_ABB_MICRO_025_US208'
    df = pd.DataFrame()
    if set_name is not None:
        hd_file = pd.HDFStore(os.path.join(
            cfg.get('paths', 'feedin'), 'coastdat', str(year), 'solar',
            cfg.get('feedin', 'file_pattern').format(year=year,
                                                     type='solar',
                                                     set_name=set_name)),
                              mode='r')
        df = hd_file['/A{0}'.format(coastdat_id)]
        hd_file.close()
    else:
        path = os.path.join(cfg.get('paths', 'feedin'), 'coastdat', str(year),
                            'solar')
        for file in os.listdir(path):
            hd_file = pd.HDFStore(os.path.join(path, file), mode='r')
            tmp = hd_file['/A{0}'.format(coastdat_id)]
            hd_file.close()
            df = pd.concat([df, tmp], axis=1)

    opt = int(round(feedin.get_optimal_pv_angle(latitude)))
    df.columns = df.columns.str.replace('opt', str(opt))
    return df
Exemple #27
0
def fetch_coastdat_weather(year, coastdat_id):
    """
    Fetch weather one coastdat weather data set.

    Parameters
    ----------
    year : int
        Year of the weather data set
    coastdat_id : numeric
        ID of the coastdat data set.

    Returns
    -------
    pd.DataFrame : Weather data set.

    Examples
    --------
    >>> coastdat_id=fetch_id_by_coordinates(53.655119, 11.181475)
    >>> fetch_coastdat_weather(2014, coastdat_id)['v_wind'].mean().round(2)
    4.39
    """
    weather_file_name = os.path.join(
        cfg.get("paths", "coastdat"),
        cfg.get("coastdat", "file_pattern").format(year=year),
    )
    if not os.path.isfile(weather_file_name):
        download_coastdat_data(filename=weather_file_name, year=year)
    key = "/A{0}".format(int(coastdat_id))
    return pd.DataFrame(pd.read_hdf(weather_file_name, key))
Exemple #28
0
def guess_coordinates_by_postcode_opsd(df):
    # *** Use postcode ***
    if 'postcode' in df:
        df_pstc = df.loc[(df.lon.isnull() & df.postcode.notnull())]
        if len(df_pstc) > 0:
            pstc = pd.read_csv(
                os.path.join(cfg.get('paths', 'geometry'),
                             cfg.get('geometry', 'postcode_polygon')),
                index_col='zip_code')
        for idx, val in df_pstc.iterrows():
            try:
                # If the postcode is not number the integer conversion will
                # raise a ValueError. Some postcode look like this '123XX'.
                # It would be possible to add the mayor regions to the postcode
                # map in order to search for the first two/three digits.
                postcode = int(val.postcode)
                if postcode in pstc.index:
                    df.loc[df.id == val.id, 'lon'] = wkt_loads(
                        pstc.loc[postcode].values[0]).centroid.x
                    df.loc[df.id == val.id, 'lat'] = wkt_loads(
                        pstc.loc[postcode].values[0]).centroid.y
                # Replace the last number with a zero and try again.
                elif round(postcode / 10) * 10 in pstc.index:
                    postcode = round(postcode / 10) * 10
                    df.loc[df.id == val.id, 'lon'] = wkt_loads(
                        pstc.loc[postcode].values[0]).centroid.x
                    df.loc[df.id == val.id, 'lat'] = wkt_loads(
                        pstc.loc[postcode].values[0]).centroid.y
                else:
                    logging.debug("Cannot find postcode {0}.".format(postcode))
            except ValueError:
                logging.debug("Cannot find postcode {0}.".format(val.postcode))
    return df
Exemple #29
0
def pp_reegis2deflex(regions, name, filename_in=None, filename_out=None):
    """
    Add federal states and deflex regions to powerplant table from reegis. As
    the process takes a while the result is stored for further usage.

    Returns
    -------
    str : The full path where the result file is stored.

    """
    if filename_out is None:
        filename_out = os.path.join(
            cfg.get("paths", "powerplants"),
            cfg.get("powerplants", "deflex_pp"),
        ).format(map=name)

    # Add deflex regions to powerplants
    pp = powerplants.add_regions_to_powerplants(regions,
                                                name,
                                                dump=False,
                                                filename=filename_in)

    # Add federal states to powerplants
    federal_states = reegis_geometries.get_federal_states_polygon()
    pp = powerplants.add_regions_to_powerplants(federal_states,
                                                "federal_states",
                                                pp=pp,
                                                dump=False)

    # store the results for further usage of deflex
    pp.to_hdf(filename_out, "pp")
    return filename_out
Exemple #30
0
def share_houses_flats(key=None):
    """

    Parameters
    ----------
    key str
        Valid keys are: 'total_area', 'avg_area', 'share_area', 'total_number',
         'share_number'.

    Returns
    -------
    dict or pd.DataFrame
    """
    size = pd.Series([1, 25, 50, 70, 90, 110, 130, 150, 170, 190, 210])
    infile = os.path.join(cfg.get('paths', 'data_de21'),
                          cfg.get('general_sources', 'zensus_flats'))
    whg = pd.read_csv(infile,
                      delimiter=';',
                      index_col=[0],
                      header=[0, 1],
                      skiprows=5)
    whg = whg.loc[whg['Insgesamt', 'Insgesamt'].notnull()]
    new_index = []
    states = cfg.get_dict('STATES')
    for i in whg.index:
        new_index.append(states[i[3:-13]])
    whg.index = new_index

    flat = {
        'total_area': pd.DataFrame(),
        'total_number': pd.DataFrame(),
    }
    for f in whg.columns.get_level_values(0).unique():
        df = pd.DataFrame(whg[f].values * size.values,
                          columns=whg[f].columns,
                          index=whg.index)
        flat['total_area'][f] = df.sum(1) - df['Insgesamt']
        flat['total_number'][f] = df['Insgesamt']
    flat['total_area']['1 + 2 Wohnungen'] = (flat['total_area']['1 Wohnung'] +
                                             flat['total_area']['2 Wohnungen'])
    flat['total_number']['1 + 2 Wohnungen'] = (
        flat['total_number']['1 Wohnung'] +
        flat['total_number']['2 Wohnungen'])

    flat['avg_area'] = flat['total_area'].div(flat['total_number'])
    flat['share_area'] = (flat['total_area'].transpose().div(
        flat['total_area']['Insgesamt'])).transpose().round(3)
    flat['share_number'] = (flat['total_number'].transpose().div(
        flat['total_number']['Insgesamt'])).transpose().round(3)

    if key is None:
        return flat
    elif key in flat:
        return flat[key].sort_index()
    else:
        logging.warning(
            "'{0}' is an invalid key for function 'share_houses_flats'".format(
                key))
    return None