Exemplo n.º 1
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.Geometry(name="Offshore wind patch", df=df)
    goffsh.create_geo_df()

    # Add column with region names of the model_region
    new_col = 'federal_states'
    if new_col in goffsh.gdf:
        del goffsh.gdf[new_col]
    federal_states = geo.Geometry(new_col)
    federal_states.load(cfg.get('paths', 'geometry'),
                        cfg.get('geometry', 'federalstates_polygon'))
    goffsh.gdf = geo.spatial_join_with_buffer(goffsh, federal_states,
                                              name=new_col)

    # Add column with coastdat id
    new_col = 'coastdat2'
    if new_col in goffsh.gdf:
        del goffsh.gdf[new_col]
    coastdat = geo.Geometry(new_col)
    coastdat.load(cfg.get('paths', 'geometry'),
                  cfg.get('coastdat', 'coastdatgrid_polygon'))
    goffsh.gdf = geo.spatial_join_with_buffer(goffsh, coastdat, name=new_col)
    offsh_df = goffsh.get_df()

    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))
    logging.warning(
        "Offshore wind is patched. {0} MW were replaced by {1} MW".format(
            old_cap, new_cap))
    return patched_df
Exemplo n.º 2
0
def spatial_preparation_power_plants(pp):
    """Add spatial names to DataFrame. Three columns will be added to the
    power plant table:

    federal_states: The federal state of Germany
    model_region: The name of the model region defined by the user.
    coastdat: The id of the nearest coastdat weather data set.

    Parameters
    ----------
    pp : reegis.Geometry
        An object containing Germany's power plants.

    Returns
    -------
    reegis.Geometry

    """

    if pp.gdf is None:
        logging.info("Create GeoDataFrame from lat/lon.")
        pp.create_geo_df()

    logging.info("Remove invalid geometries")
    pp.remove_invalid_geometries()

    # Add column with name of the federal state (Bayern, Berlin,...)
    federal_states = geo.Geometry('federal states')
    federal_states.load(cfg.get('paths', 'geometry'),
                        cfg.get('geometry', 'federalstates_polygon'))
    pp.gdf = geo.spatial_join_with_buffer(pp, federal_states,
                                          name='federal_states')

    # Add country code to federal state if country code is not 'DE'.
    if 'country_code' in pp.gdf.columns:
        country_codes = list(pp.gdf.country_code.unique())
        country_codes.remove('DE')
        for c_code in country_codes:
            pp.gdf.loc[pp.gdf.country_code == c_code, 'federal_states'] = (
                c_code)

    # Add column with coastdat id
    coastdat = geo.Geometry('coastdat2')
    coastdat.load(cfg.get('paths', 'geometry'),
                  cfg.get('coastdat', 'coastdatgrid_polygon'))
    pp.gdf = geo.spatial_join_with_buffer(pp, coastdat, name='coastdat2')

    return pp
Exemplo n.º 3
0
def get_inhabitants(polygon, name):
    """The inhabitants of 2016 are used."""
    table = 'ew'
    cfg_data = cfg.get_dict(table)

    ew_fn = os.path.join(cfg.get('paths', 'fis_broker'), cfg_data['table'],
                         'shp', cfg_data['table'] + '.shp')

    logging.debug("Reading {0}".format(ew_fn))

    if not os.path.isfile(ew_fn):
        ew_fn = download.download_maps(single=table)
    ew = geometries.load(fullname=ew_fn)
    ew['centroid_column'] = ew.representative_point()
    ew = ew.set_geometry('centroid_column')

    neu = geometries.spatial_join_with_buffer(
        ew,
        polygon,
        name=name,
        limit=0,
    )
    grp = neu.groupby(name).sum()
    grp['frac'] = grp['EW'].div(grp.sum()['EW']).multiply(100).round(1)
    return grp
Exemplo n.º 4
0
def get_nutslist_for_regions(regions):
    """
    Parameters
    ----------
    regions = Geodataframe
        Geodataframe containing the geometry where NUTS-regions should be mapped to

    Returns: DataFrame
        List of nuts3 regions for all zones in the overall geometry
    -------
    """
    # Fetch NUTS3-geometries from disaggregator database
    nuts3_disaggregator = data.database_shapes()

    # Transform CRS System to match reegis geometries
    nuts_centroid = nuts3_disaggregator.centroid.to_crs(4326)
    nuts_geo = nuts3_disaggregator.to_crs(crs=4326)

    # Match NUTS3-regions with federal states
    nuts_geo = geo.spatial_join_with_buffer(nuts_centroid , regions, 'fs', limit=0)

    # Create dictionary with lists of all NUTS3-regions for each state
    mapped_nuts = pd.DataFrame(index=regions.index, columns=["nuts"])

    for zone in regions.index:
        mapped_nuts.loc[zone, "nuts"] = list(nuts_geo.loc[nuts_geo['fs'] == zone].index)

    return mapped_nuts
Exemplo n.º 5
0
def windzone_region_fraction(pp, name, year=None, dump=False):
    """

    Parameters
    ----------
    pp : pd.DataFrame
    year : int
    name : str
    dump : bool

    Returns
    -------

    Examples
    --------
    >>> my_fn=os.path.join(cfg.get('paths', 'powerplants'),
    ...                      cfg.get('powerplants', 'reegis_pp'))
    >>> my_pp=pd.DataFrame(pd.read_hdf(my_fn, 'pp'))  # doctest: +SKIP
    >>> wz=windzone_region_fraction(my_pp, 'federal_states', 2014
    ...                               dump=False)  # doctest: +SKIP
    >>> round(float(wz.loc['NI', 1]), 2)  # doctest: +SKIP
    0.31
    """
    pp = pp.loc[pp.energy_source_level_2 == "Wind"]

    if year is None:
        capacity_col = "capacity"
    else:
        capacity_col = "capacity_{0}".format(year)

    path = cfg.get("paths", "geometry")
    filename = "windzones_germany.geojson"
    gdf = geometries.load(path=path, filename=filename)
    gdf.set_index("zone", inplace=True)

    geo_path = cfg.get("paths", "geometry")
    geo_file = cfg.get("coastdat", "coastdatgrid_polygon")
    coastdat_geo = geometries.load(path=geo_path, filename=geo_file)
    coastdat_geo["geometry"] = coastdat_geo.centroid

    points = geometries.spatial_join_with_buffer(coastdat_geo, gdf, "windzone")

    wz = pd.DataFrame(points["windzone"])
    pp = pd.merge(pp, wz, left_on="coastdat2", right_index=True)
    pp["windzone"].fillna(0, inplace=True)
    pp = pp.groupby([name, "windzone"]).sum()[capacity_col]
    wz_regions = pp.groupby(level=0).apply(lambda x: x / float(x.sum()))

    if dump is True:
        filename = "windzone_{0}.csv".format(name)
        fn = os.path.join(cfg.get("paths", "powerplants"), filename)
        wz_regions.to_csv(fn, header=False)
    return wz_regions
Exemplo n.º 6
0
def add_model_region_pp(pp, region_polygons, col_name):
    """
    """
    # Create a geoDataFrame from power plant DataFrame.
    pp = geo.create_geo_df(pp)

    # Add region names to power plant table
    pp = geo.spatial_join_with_buffer(pp, region_polygons, name=col_name)
    pp = pp.drop('geometry', axis=1)

    logging.info(
        "Region column {0} added to power plant table.".format(col_name))
    return pp
Exemplo n.º 7
0
def get_coastdat_onshore_polygons():
    cstd = geometries.load(cfg.get('paths', 'geometry'),
                           cfg.get('coastdat', 'coastdatgrid_polygon'),
                           index_col='gid')

    de02 = geometries.load(cfg.get('paths', 'geo_deflex'),
                           cfg.get('geometry',
                                   'deflex_polygon').format(type='polygons',
                                                            map='de02',
                                                            suffix='.geojson'),
                           index_col='region')

    cstd_pt = gpd.GeoDataFrame(cstd.centroid, columns=['geometry'])

    cstd_pt = geometries.spatial_join_with_buffer(cstd_pt,
                                                  de02,
                                                  'coastdat',
                                                  limit=0)
    reduced = cstd.loc[cstd_pt.coastdat == "DE01"]
    return reduced.sort_index()
Exemplo n.º 8
0
def divide_off_and_onshore(regions):
    """
    Sort regions into onshore and offshore regions. A namedtuple with two list
    of regions ids will be returned. Fetch the `onshore` and `offshore`
    attribute of the named tuple to get the list.

    Parameters
    ----------
    regions : GeoDataFrame
        A region set with the region id in the index.

    Returns
    -------
    named tuple

    Examples
    --------
    >>> reg=deflex_regions('de02')
    >>> divide_off_and_onshore(reg).onshore
    ['DE01']
    >>> reg=deflex_regions('de21')
    >>> divide_off_and_onshore(reg).offshore
    ['DE19', 'DE20', 'DE21']
    """
    region_type = namedtuple("RegionType", "offshore onshore")
    regions_centroid = regions.copy()
    regions_centroid.geometry = regions_centroid.centroid

    germany_onshore = geo.load(cfg.get("paths", "geometry"),
                               cfg.get("geometry", "germany_polygon"))

    gdf = geo.spatial_join_with_buffer(regions_centroid,
                                       germany_onshore,
                                       "onshore",
                                       limit=0)

    onshore = list(gdf.loc[gdf.onshore == 0].index)
    offshore = list(gdf.loc[gdf.onshore == "unknown"].index)

    return region_type(offshore=offshore, onshore=onshore)
Exemplo n.º 9
0
def get_admin_by_region(region):
    """
    Allocate admin keys to the given regions.

    Parameters
    ----------
    region : geopandas.GeoDataFrame

    Returns
    -------
    pd.DataFrame
    """
    fn = os.path.join(cfg.get("paths", "geometry"), "vg1000_geodata.geojson")
    vg = geometries.load(fullname=fn)
    vg.set_index("RS", inplace=True)

    reg2vg = geometries.spatial_join_with_buffer(vg.representative_point(),
                                                 region,
                                                 "fs",
                                                 limit=0)

    return pd.DataFrame(reg2vg.drop("geometry", axis=1))
Exemplo n.º 10
0
def add_model_region_pp(pp, region_polygons, col_name, subregion=False):
    """
    """
    # Create a geoDataFrame from power plant DataFrame.
    pp = geo.create_geo_df(pp)

    if subregion is True:
        limit = 0
    else:
        limit = 1

    # Add region names to power plant table
    pp = pd.DataFrame(
        geo.spatial_join_with_buffer(pp,
                                     region_polygons,
                                     name=col_name,
                                     limit=limit))
    pp['geometry'] = pp['geometry'].astype(str)

    logging.info(
        "Region column {0} added to power plant table.".format(col_name))
    return pp
Exemplo n.º 11
0
def ego_demand_by_region(regions, name, outfile=None, dump=False):
    ego_data = get_ego_demand()

    ego_demand = geometries.create_geo_df(ego_data)

    # Add column with regions
    ego_demand = geometries.spatial_join_with_buffer(ego_demand, regions, name)

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

    ego_demand['geometry'] = ego_demand['geometry'].astype(str)

    if outfile is not None:
        path = cfg.get('paths', 'demand')
        outfile = os.path.join(path, 'open_ego_demand_{0}.h5')

    # Write out file (hdf-format).
    if dump is True:
        ego_demand.to_hdf(outfile, 'demand')

    return ego_demand
Exemplo n.º 12
0
def prepare_ego_demand(egofile):
    ego_demand = geometries.create_geo_df(get_ego_data())

    # Add column with name of the federal state (Bayern, Berlin,...)
    federal_states = geometries.load(
        cfg.get('paths', 'geometry'),
        cfg.get('geometry', 'federalstates_polygon'))

    # Add column with federal_states
    ego_demand = geometries.spatial_join_with_buffer(ego_demand,
                                                     federal_states,
                                                     'federal_states')

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

    ego_demand['geometry'] = ego_demand['geometry'].astype(str)

    # Write out file (hdf-format).
    ego_demand.to_hdf(egofile, 'demand')

    return ego_demand
Exemplo n.º 13
0
def get_inhabitants_by_multi_regions(year, geo, name):
    """
    Get a MultiIndex table with the inhabitants from all given geometry sets.

    Parameters
    ----------
    year : int
    geo : tuple or list
    name : tuple or list

    Returns
    -------

    Examples
    --------
    >>> geo1=geometries.load(
    ...     cfg.get('paths', 'geometry'),
    ...     cfg.get('geometry', 'de21_polygons'), index_col='region')
    >>> geo2=geometries.get_federal_states_polygon()
    >>> inh=get_inhabitants_by_multi_regions(
    ...     2014, [geo1, geo2], ['de21', 'fs'])
    >>> inh.loc['DE01']['BB']
    1811137
    >>> inh.loc['DE01']['BE']
    3469849

    """
    ew = get_ew_geometry(year)
    n = 0
    for geo_one in geo:
        ew = geometries.spatial_join_with_buffer(ew,
                                                 geo_one,
                                                 name=name[n],
                                                 step=0.005)
        n += 1

    return ew.groupby(name).sum()["EWZ"]
Exemplo n.º 14
0
def get_inhabitants_by_region(year, geo, name):
    """
    Get inhabitants for the given region polygons.

    Parameters
    ----------
    year
    geo
    name

    Returns
    -------
    pd.DataFrame

    Examples
    --------
    >>> geo=geometries.get_federal_states_polygon()
    >>> get_inhabitants_by_region(2014, geo, name='federal_states').sum()
    81197537
    """
    ew = get_ew_geometry(year)
    ew = geometries.spatial_join_with_buffer(ew, geo, name=name, step=0.005)

    return ew.groupby(name).sum()["EWZ"]
Exemplo n.º 15
0
def get_ego_demand_by_region(
    regions,
    name,
    outfile=None,
    infile=None,
    dump=False,
    grouped=False,
    sectors=False,
    overwrite=False,
):
    """
    Add the region id from a given region set to the openego demand table. This
    can be used to calculate the demand or the share of each region.

    Parameters
    ----------
    regions : GeoDataFrame
        A region set.
    name : str
        The name of the region set will be used as the name of the column in
        the openego GeoDataFrame and to distinguish result files.
    outfile : str (optional)
        It is possible to pass a filename (with path) where the results should
        be stored. Only valid if `dump` is True.
    infile : str (optional)
        It is possible to use a specific infile (with path) where the openego
        map is stored.
    dump : bool
        If dump is True the result will be returned and stored into a file.
        Otherwise the result is just returned. (default: False)
    grouped : bool
        If grouped is False the openego table with a region column is returned.
        Otherwise the map is grouped by the region column and the consumption
        column is summed up. (default: False)
    sectors : bool
        Still missing.
    overwrite : bool

    Returns
    -------
    pandas.DataFrame or pandas.Series : A Series is returned if grouped is
        True.

    Notes
    -----
    The openego map may not be updated in the future so it might be necessary
    to scale the results to an overall demand.

    Examples
    --------
    >>> federal_states=geometries.get_federal_states_polygon()
    >>> bmwi_annual=bmwi_data.get_annual_electricity_demand_bmwi(
    ...    2015)  # doctest: +SKIP

    >>> egodemand=get_ego_demand_by_region(
    ...     federal_states, 'federal_states', grouped=True)  # doctest: +SKIP

    >>> egodemand.div(ego_demand.sum()).mul(bmwi_annual)  # doctest: +SKIP

    """
    if outfile is None:
        path = cfg.get("paths", "demand")
        outfile = os.path.join(path, "open_ego_demand_{0}.h5")
        if sectors:
            outfile = outfile.format(name + "_sectors")
        else:
            outfile = outfile.format(name)

    if not os.path.isfile(outfile) or overwrite:
        ego_data = get_ego_demand(filename=infile, sectors=sectors)
        ego_demand = geometries.create_geo_df(ego_data)

        # Add column with regions
        logging.debug("OpenEgo spatial join: Demand polygon centroids with "
                      "{0}".format(name))
        ego_demand = geometries.spatial_join_with_buffer(
            ego_demand, regions, name)

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

        ego_demand["geometry"] = ego_demand["geometry"].astype(str)

        # Write out file (hdf-format).
        if dump is True:
            ego_demand.to_hdf(outfile, "demand")
    else:
        ego_demand = pd.DataFrame(pd.read_hdf(outfile, "demand"))

    if grouped is True:
        return ego_demand.groupby(name)["consumption"].sum()
    else:
        return ego_demand
Exemplo n.º 16
0
def pumped_hydroelectric_storage(regions, name=None):
    """

    Parameters
    ----------
    regions : geopandas.geoDataFrame
    name : str or None

    Returns
    -------
    pd.DataFrame

    Examples
    --------
    >>> federal_states = geometries.load(
    ...     cfg.get('paths', 'geometry'),
    ...     cfg.get('geometry', 'federalstates_polygon'))
    >>> phes = pumped_hydroelectric_storage(federal_states, 'federal_states')
    >>> int(phes.turbine.sum())
    6593

    """
    phes_raw = pd.read_csv(os.path.join(cfg.get('paths', 'static_sources'),
                                        cfg.get('storages', 'hydro_storages')),
                           header=[0, 1]).sort_index(1)

    phes = phes_raw['dena'].copy()

    # add geometry from wikipedia
    phes_raw = phes_raw[phes_raw['Wikipedia', 'longitude'].notnull()]
    phes['geom'] = (phes_raw.apply(lat_lon2point, axis=1))

    # add energy from ZFES because dena values seem to be corrupted
    phes['energy'] = phes_raw['ZFES', 'energy']
    phes['name'] = phes_raw['ZFES', 'name']

    # TODO: 0.75 should come from config file
    phes['efficiency'] = phes['efficiency'].fillna(0.75)

    # remove storages that do not have an entry for energy capacity
    phes = phes[phes.energy.notnull()]

    # create a GeoDataFrame with geom column
    gphes = geometries.create_geo_df(phes)

    if name is None:
        name = '{0}_region'.format(cfg.get('init', 'map'))

    gphes = geometries.spatial_join_with_buffer(gphes, regions, name=name)

    # create turbine and pump efficiency from overall efficiency (square root)
    # multiply the efficiency with the capacity to group with "sum()"
    gphes['pump_eff'] = np.sqrt(gphes.efficiency) * gphes.pump
    gphes['turbine_eff'] = (np.sqrt(gphes.efficiency) * gphes.turbine)

    phes = gphes.groupby(name).sum()

    # divide by the capacity to get the efficiency and remove overall
    # efficiency
    phes['pump_eff'] = phes.pump_eff / phes.pump
    phes['turbine_eff'] = phes.turbine_eff / phes.turbine
    del phes['efficiency']

    return phes
Exemplo n.º 17
0
def spatial_average_weather(year, geo, parameter, outpath=None, outfile=None):
    """
    Calculate the average temperature for all regions (de21, states...).

    Parameters
    ----------
    year : int
        Select the year you want to calculate the average temperature for.
    geo : geometries.Geometry object
        Polygons to calculate the average parameter for.
    outpath : str
        Place to store the outputfile.
    outfile : str
        Set your own name for the outputfile.
    parameter : str
        Name of the item (temperature, wind speed,... of the weather data set.

    Returns
    -------
    str : Full file name of the created file.

    """
    logging.info("Getting average {0} for {1} in {2} from coastdat2.".format(
        parameter, geo.name, year))

    col_name = geo.name.replace(' ', '_')

    # Create a Geometry object for the coastdat centroids.
    coastdat_geo = geometries.Geometry(name='coastdat')
    coastdat_geo.load(cfg.get('paths', 'geometry'),
                      cfg.get('coastdat', 'coastdatgrid_polygon'))
    coastdat_geo.gdf['geometry'] = coastdat_geo.gdf.centroid

    # Join the tables to create a list of coastdat id's for each region.
    coastdat_geo.gdf = geometries.spatial_join_with_buffer(
        coastdat_geo, geo, name='federal_states', limit=0)

    # Fix regions with no matches (no matches if a region ist too small).
    fix = {}
    for reg in set(geo.gdf.index) - set(coastdat_geo.gdf[col_name].unique()):
        reg_point = geo.gdf.representative_point().loc[reg]
        coastdat_poly = geometries.Geometry(name='coastdat_poly')
        coastdat_poly.load(cfg.get('paths', 'geometry'),
                           cfg.get('coastdat', 'coastdatgrid_polygon'))
        fix[reg] = coastdat_poly.gdf.loc[coastdat_poly.gdf.intersects(
            reg_point)].index[0]

    # Open the weather file
    weatherfile = os.path.join(
        cfg.get('paths', 'coastdat'),
        cfg.get('coastdat', 'file_pattern').format(year=year))
    if not os.path.isfile(weatherfile):
        download_coastdat_data(year=year, filename=weatherfile)
    weather = pd.HDFStore(weatherfile, mode='r')

    # Calculate the average temperature for each region with more than one id.
    avg_value = pd.DataFrame()
    for region in geo.gdf.index:
        cd_ids = coastdat_geo.gdf[coastdat_geo.gdf[col_name] == region].index
        number_of_sets = len(cd_ids)
        tmp = pd.DataFrame()
        logging.debug((region, len(cd_ids)))
        for cid in cd_ids:
            try:
                cid = int(cid)
            except ValueError:
                pass
            if isinstance(cid, int):
                key = 'A' + str(cid)
            else:
                key = cid
            tmp[cid] = weather[key][parameter]
        if len(cd_ids) < 1:
            key = 'A' + str(fix[region])
            avg_value[region] = weather[key][parameter]
        else:
            avg_value[region] = tmp.sum(1).div(number_of_sets)
    weather.close()

    # Create the name an write to file
    regions = sorted(geo.gdf.index)
    if outfile is None:
        out_name = '{0}_{1}'.format(regions[0], regions[-1])
        outfile = os.path.join(
            outpath, 'average_{parameter}_{type}_{year}.csv'.format(
                year=year, type=out_name, parameter=parameter))

    avg_value.to_csv(outfile)
    logging.info("Average temperature saved to {0}".format(outfile))
    return outfile
Exemplo n.º 18
0
import os
from disaggregator import config, data
from reegis import geometries as geo, config as rconfig, demand_elec, demand_heat

nuts_geo_fn = os.path.join(rconfig.get('paths', 'geometry'),
                           'NUTS_RG_03M_2016_4326_LEVL_3_DE.geojson')

nuts_geo = geo.load(fullname=nuts_geo_fn)
nuts_geo.set_index('id', drop=True, inplace=True)
fed_states = geo.get_federal_states_polygon()

nuts_geo = geo.spatial_join_with_buffer(nuts_geo.centroid, fed_states, 'fs')
fed_states['nuts'] = '0'
for state in fed_states.index:
    fed_states.loc[state,
                   'nuts'] = list(nuts_geo.loc[nuts_geo['fs'] == state].index)

cfg = config.get_config()
dict_nuts3_name = config.region_id_to_nuts3(nuts3_to_name=True)
df_spatial = data.database_description('spatial')
df_temporal = data.database_description('temporal')
elc_consumption_hh_spat = data.elc_consumption_HH_spatial()
elc_consumption_hh_spattemp = data.elc_consumption_HH_spatiotemporal()

print(elc_consumption_hh_spattemp[fed_states.loc['BB', 'nuts']])
print(elc_consumption_hh_spattemp[fed_states.loc['HH', 'nuts']])

# Testing Disaggregator reegis functions
fed_states_nuts = fed_states.loc['BY', 'nuts']
demand_elec.get_household_powerload_by_NUTS3_profile(2014,
                                                     fed_states_nuts,
Exemplo n.º 19
0
def spatial_average_weather(year,
                            geo,
                            parameter,
                            name,
                            outpath=None,
                            outfile=None):
    """
    Calculate the mean value of a parameter over all data sets within each
    region for one year.

    Parameters
    ----------
    year : int
        Select the year you want to calculate the average temperature for.
    geo : geometries.Geometry object
        Polygons to calculate the average parameter for.
    outpath : str
        Place to store the outputfile.
    outfile : str
        Set your own name for the outputfile.
    parameter : str
        Name of the item (temperature, wind speed,... of the weather data set.
    name : str
        Name of the regions table to be used as a column name.

    Returns
    -------
    str : Full file name of the created file.

    Example
    -------
    >>> germany_geo=geometries.load(
    ...     cfg.get('paths', 'geometry'),
    ...     cfg.get('geometry', 'germany_polygon'))
    >>> fn=spatial_average_weather(2012, germany_geo, 'temp_air', 'deTemp',
    ...                              outpath=os.path.expanduser('~')
    ...                              )# doctest: +SKIP
    >>> temp=pd.read_csv(fn, index_col=[0], parse_dates=True, squeeze=True
    ...                    )# doctest: +SKIP
    >>> round(temp.mean() - 273.15, 2)# doctest: +SKIP
    8.28
    >>> os.remove(fn)# doctest: +SKIP
    """
    logging.info("Getting average {0} for {1} in {2} from coastdat2.".format(
        parameter, name, year))

    name = name.replace(" ", "_")

    # Create a Geometry object for the coastdat centroids.
    coastdat_geo = geometries.load(
        cfg.get("paths", "geometry"),
        cfg.get("coastdat", "coastdatgrid_polygon"),
    )
    coastdat_geo["geometry"] = coastdat_geo.centroid

    # Join the tables to create a list of coastdat id's for each region.
    coastdat_geo = geometries.spatial_join_with_buffer(coastdat_geo,
                                                       geo,
                                                       name=name,
                                                       limit=0)

    # Fix regions with no matches (no matches if a region ist too small).
    fix = {}
    for reg in set(geo.index) - set(coastdat_geo[name].unique()):
        reg_point = geo.representative_point().loc[reg]
        coastdat_poly = geometries.load(
            cfg.get("paths", "geometry"),
            cfg.get("coastdat", "coastdatgrid_polygon"),
        )
        fix[reg] = coastdat_poly.loc[coastdat_poly.intersects(
            reg_point)].index[0]

    # Open the weather file
    weather_file = os.path.join(
        cfg.get("paths", "coastdat"),
        cfg.get("coastdat", "file_pattern").format(year=year),
    )
    if not os.path.isfile(weather_file):
        download_coastdat_data(year=year, filename=weather_file)
    weather = pd.HDFStore(weather_file, mode="r")

    # Calculate the average temperature for each region with more than one id.
    avg_value = pd.DataFrame()
    for region in geo.index:
        cd_ids = coastdat_geo[coastdat_geo[name] == region].index
        number_of_sets = len(cd_ids)
        tmp = pd.DataFrame()
        logging.debug((region, len(cd_ids)))
        for cid in cd_ids:
            try:
                cid = int(cid)
            except ValueError:
                pass
            if isinstance(cid, int):
                key = "A" + str(cid)
            else:
                key = cid
            tmp[cid] = weather[key][parameter]
        if len(cd_ids) < 1:
            key = "A" + str(fix[region])
            avg_value[region] = weather[key][parameter]
        else:
            avg_value[region] = tmp.sum(1).div(number_of_sets)
    weather.close()

    # Create the name an write to file
    regions = sorted(geo.index)
    if outfile is None:
        out_name = "{0}_{1}".format(regions[0], regions[-1])
        outfile = os.path.join(
            outpath,
            "average_{parameter}_{type}_{year}.csv".format(
                year=year, type=out_name, parameter=parameter),
        )

    avg_value.to_csv(outfile)
    logging.info("Average temperature saved to {0}".format(outfile))
    return outfile
Exemplo n.º 20
0
def pumped_hydroelectric_storage_by_region(regions, year, name=None):
    """
    Fetch pumped hydroelectric storage by region. This function is based on
    static data. Please adapt the source file for years > 2018.

    Parameters
    ----------
    regions : geopandas.geoDataFrame
    name : str or None

    Returns
    -------
    pd.DataFrame

    Examples
    --------
    >>> federal_states=geometries.get_federal_states_polygon()
    >>> phes=pumped_hydroelectric_storage_by_region(
    ...     federal_states, 2002, 'federal_states')
    >>> int(phes.turbine.sum())
    5533
    >>> phes=pumped_hydroelectric_storage_by_region(
    ...     federal_states, 2018, 'federal_states')
    >>> int(phes.turbine.sum())
    6593
    >>> int(phes.energy.sum())
    37841
    >>> round(phes.loc['BW'].pump_eff, 2)
    0.86
    """
    phes_raw = pd.read_csv(
        os.path.join(
            cfg.get("paths", "static_sources"),
            cfg.get("storages", "hydro_storages"),
        ),
        header=[0, 1],
    ).sort_index(1)

    phes_raw = phes_raw.loc[phes_raw["Wikipedia", "commissioning"] < year]
    phes_raw = phes_raw.loc[phes_raw["Wikipedia", "ensured_operation"] >= year]

    phes = phes_raw["dena"].copy()

    # add geometry from wikipedia
    phes_raw = phes_raw[phes_raw["Wikipedia", "longitude"].notnull()]
    phes["geom"] = phes_raw.apply(lat_lon2point, axis=1)

    # add energy from ZFES because dena values seem to be corrupted
    phes["energy"] = phes_raw["ZFES", "energy"]
    phes["name"] = phes_raw["ZFES", "name"]

    phes["efficiency"] = phes["efficiency"].fillna(
        cfg.get("storages", "default_efficiency"))

    # remove storages that do not have an entry for energy capacity
    phes = phes[phes.energy.notnull()]

    # create a GeoDataFrame with geom column
    gphes = geometries.create_geo_df(phes)

    if name is None:
        name = "{0}_region".format(cfg.get("init", "map"))

    gphes = geometries.spatial_join_with_buffer(gphes,
                                                regions,
                                                name=name,
                                                limit=0)

    # create turbine and pump efficiency from overall efficiency (square root)
    # multiply the efficiency with the capacity to group with "sum()"
    gphes["pump_eff"] = np.sqrt(gphes.efficiency) * gphes.pump
    gphes["turbine_eff"] = np.sqrt(gphes.efficiency) * gphes.turbine

    phes = gphes.groupby(name).sum()

    # divide by the capacity to get the efficiency and remove overall
    # efficiency
    phes["pump_eff"] = phes.pump_eff / phes.pump
    phes["turbine_eff"] = phes.turbine_eff / phes.turbine
    del phes["efficiency"]

    return phes