<commit_before>"""
The ``wind_farm`` module contains the class WindFarm that implements
a wind farm in the windpowerlib and functions needed for the modelling of a
wind farm.

"""

__copyright__ = "Copyright oemof developer group"
__license__ = "GPLv3"

from windpowerlib import tools, power_curves, WindTurbine
import numpy as np
import pandas as pd
import logging
import warnings


class WindFarm(object):
    r"""
    Defines a standard set of wind farm attributes.

    Parameters
    ----------
    wind_turbine_fleet : :pandas:`pandas.DataFrame<frame>` or list(dict)
        Wind turbines of wind farm. DataFrame/Dictionaries must have
        'wind_turbine' containing a :class:`~.wind_turbine.WindTurbine` object
        and either 'number_of_turbines' (number of wind turbines of the same
        turbine type in the wind farm, can be a float) or 'total_capacity'
        (installed capacity of wind turbines of the same turbine type in the
        wind farm) as columns/keys. See example below.
    efficiency : float or :pandas:`pandas.DataFrame<frame>` or None (optional)
        Efficiency of the wind farm. Provide as either constant (float) or
        power efficiency curve (pd.DataFrame) containing 'wind_speed' and
        'efficiency' columns with wind speeds in m/s and the corresponding
        dimensionless wind farm efficiency. Default: None.
    name : str (optional)
        Can be used as an identifier of the wind farm. Default: ''.

    Attributes
    ----------
    wind_turbine_fleet : list(dict)
        Wind turbines of wind farm. Dictionaries must have 'wind_turbine'
        (contains a :class:`~.wind_turbine.WindTurbine` object) and
        'number_of_turbines' (number of wind turbines of the same turbine type
        in the wind farm) as keys.
    efficiency : float or :pandas:`pandas.DataFrame<frame>` or None
        Efficiency of the wind farm. Either constant (float) power efficiency
        curve (pd.DataFrame) containing 'wind_speed' and 'efficiency'
        columns with wind speeds in m/s and the corresponding
        dimensionless wind farm efficiency. Default: None.
    name : str
        If set this is used as an identifier of the wind farm.
    hub_height : float
        The calculated mean hub height of the wind farm. See
        :py:func:`mean_hub_height` for more information.
    nominal_power : float
        The nominal power is the sum of the nominal power of all turbines in
        the wind farm in W.
    power_curve : :pandas:`pandas.DataFrame<frame>` or None
        The calculated power curve of the wind farm. See
        :py:func:`assign_power_curve` for more information.

    Examples
    --------
    >>> from windpowerlib import wind_farm
    >>> from windpowerlib import WindTurbine
    >>> import pandas as pd
    >>> enerconE126 = {
    ...    'hub_height': 135,
    ...    'rotor_diameter': 127,
    ...    'turbine_type': 'E-126/4200'}
    >>> e126 = WindTurbine(**enerconE126)
    >>> vestasV90 = {
    ...     'hub_height': 90,
    ...     'turbine_type': 'V90/2000',
    ...     'nominal_power': 2e6}
    >>> v90 = WindTurbine(**vestasV90)
    >>> # turbine fleet as DataFrame with number of turbines provided
    >>> wind_turbine_fleet = pd.DataFrame(
    ...     {'wind_turbine': [e126, v90],
    ...      'number_of_turbines': [6, 3]})
    >>> example_farm = wind_farm.WindFarm(wind_turbine_fleet)
    >>> print(example_farm.nominal_power)
    31200000.0
    >>> # turbine fleet as list with total capacity of each turbine type
    >>> # provided
    >>> example_farm_data = {
    ...    'name': 'example_farm',
    ...    'wind_turbine_fleet': [{'wind_turbine': e126,
    ...                            'total_capacity': 6 * 4.2e6},
    ...                           {'wind_turbine': v90,
    ...                            'total_capacity': 3 * 2e6}]}
    >>> example_farm = wind_farm.WindFarm(**example_farm_data)
    >>> print(example_farm.nominal_power)
    31200000.0

    """

    def __init__(self, wind_turbine_fleet, efficiency=None, name='', **kwargs):

        self.wind_turbine_fleet = wind_turbine_fleet
        self.efficiency = efficiency
        self.name = name

        self.hub_height = None
        self._nominal_power = None
        self.power_curve = None

        self.check_and_complete_wind_turbine_fleet()

    def check_and_complete_wind_turbine_fleet(self):
        """
        Function to check wind turbine fleet user input.

        Besides checking if all necessary parameters to fully define the wind
        turbine fleet are provided, this function also fills in the
        number of turbines or total capacity of each turbine type and checks
        if they are consistent.

        """
        # convert list to dataframe if necessary
        if isinstance(self.wind_turbine_fleet, list):
            try:
                self.wind_turbine_fleet = pd.DataFrame(self.wind_turbine_fleet)
            except ValueError:
                raise ValueError("Wind turbine fleet not provided properly.")

        # check wind turbines
        try:
            for turbine in self.wind_turbine_fleet['wind_turbine']:
                if not isinstance(turbine, WindTurbine):
                    raise ValueError(
                        'Wind turbine must be provided as WindTurbine object '
                        'but was provided as {}.'.format(type(turbine)))
        except KeyError:
            raise ValueError('Missing wind_turbine key/column in '
                             'wind_turbine_fleet parameter.')

        # add columns for number of turbines and total capacity if they don't
        # yet exist
        if 'number_of_turbines' not in self.wind_turbine_fleet.columns:
            self.wind_turbine_fleet['number_of_turbines'] = np.nan<commit_msg>Fix catching error<commit_after>"""
The ``wind_farm`` module contains the class WindFarm that implements
a wind farm in the windpowerlib and functions needed for the modelling of a
wind farm.

"""

__copyright__ = "Copyright oemof developer group"
__license__ = "GPLv3"

from windpowerlib import tools, power_curves, WindTurbine
import numpy as np
import pandas as pd
import logging
import warnings


class WindFarm(object):
    r"""
    Defines a standard set of wind farm attributes.

    Parameters
    ----------
    wind_turbine_fleet : :pandas:`pandas.DataFrame<frame>` or list(dict)
        Wind turbines of wind farm. DataFrame/Dictionaries must have
        'wind_turbine' containing a :class:`~.wind_turbine.WindTurbine` object
        and either 'number_of_turbines' (number of wind turbines of the same
        turbine type in the wind farm, can be a float) or 'total_capacity'
        (installed capacity of wind turbines of the same turbine type in the
        wind farm) as columns/keys. See example below.
    efficiency : float or :pandas:`pandas.DataFrame<frame>` or None (optional)
        Efficiency of the wind farm. Provide as either constant (float) or
        power efficiency curve (pd.DataFrame) containing 'wind_speed' and
        'efficiency' columns with wind speeds in m/s and the corresponding
        dimensionless wind farm efficiency. Default: None.
    name : str (optional)
        Can be used as an identifier of the wind farm. Default: ''.

    Attributes
    ----------
    wind_turbine_fleet : list(dict)
        Wind turbines of wind farm. Dictionaries must have 'wind_turbine'
        (contains a :class:`~.wind_turbine.WindTurbine` object) and
        'number_of_turbines' (number of wind turbines of the same turbine type
        in the wind farm) as keys.
    efficiency : float or :pandas:`pandas.DataFrame<frame>` or None
        Efficiency of the wind farm. Either constant (float) power efficiency
        curve (pd.DataFrame) containing 'wind_speed' and 'efficiency'
        columns with wind speeds in m/s and the corresponding
        dimensionless wind farm efficiency. Default: None.
    name : str
        If set this is used as an identifier of the wind farm.
    hub_height : float
        The calculated mean hub height of the wind farm. See
        :py:func:`mean_hub_height` for more information.
    nominal_power : float
        The nominal power is the sum of the nominal power of all turbines in
        the wind farm in W.
    power_curve : :pandas:`pandas.DataFrame<frame>` or None
        The calculated power curve of the wind farm. See
        :py:func:`assign_power_curve` for more information.

    Examples
    --------
    >>> from windpowerlib import wind_farm
    >>> from windpowerlib import WindTurbine
    >>> import pandas as pd
    >>> enerconE126 = {
    ...    'hub_height': 135,
    ...    'rotor_diameter': 127,
    ...    'turbine_type': 'E-126/4200'}
    >>> e126 = WindTurbine(**enerconE126)
    >>> vestasV90 = {
    ...     'hub_height': 90,
    ...     'turbine_type': 'V90/2000',
    ...     'nominal_power': 2e6}
    >>> v90 = WindTurbine(**vestasV90)
    >>> # turbine fleet as DataFrame with number of turbines provided
    >>> wind_turbine_fleet = pd.DataFrame(
    ...     {'wind_turbine': [e126, v90],
    ...      'number_of_turbines': [6, 3]})
    >>> example_farm = wind_farm.WindFarm(wind_turbine_fleet)
    >>> print(example_farm.nominal_power)
    31200000.0
    >>> # turbine fleet as list with total capacity of each turbine type
    >>> # provided
    >>> example_farm_data = {
    ...    'name': 'example_farm',
    ...    'wind_turbine_fleet': [{'wind_turbine': e126,
    ...                            'total_capacity': 6 * 4.2e6},
    ...                           {'wind_turbine': v90,
    ...                            'total_capacity': 3 * 2e6}]}
    >>> example_farm = wind_farm.WindFarm(**example_farm_data)
    >>> print(example_farm.nominal_power)
    31200000.0

    """

    def __init__(self, wind_turbine_fleet, efficiency=None, name='', **kwargs):

        self.wind_turbine_fleet = wind_turbine_fleet
        self.efficiency = efficiency
        self.name = name

        self.hub_height = None
        self._nominal_power = None
        self.power_curve = None

        self.check_and_complete_wind_turbine_fleet()

    def check_and_complete_wind_turbine_fleet(self):
        """
        Function to check wind turbine fleet user input.

        Besides checking if all necessary parameters to fully define the wind
        turbine fleet are provided, this function also fills in the
        number of turbines or total capacity of each turbine type and checks
        if they are consistent.

        """
        # convert list to dataframe if necessary
        if isinstance(self.wind_turbine_fleet, list):
            try:
                self.wind_turbine_fleet = pd.DataFrame(self.wind_turbine_fleet)
            except:
                raise ValueError("Wind turbine fleet not provided properly.")

        # check wind turbines
        try:
            for turbine in self.wind_turbine_fleet['wind_turbine']:
                if not isinstance(turbine, WindTurbine):
                    raise ValueError(
                        'Wind turbine must be provided as WindTurbine object '
                        'but was provided as {}.'.format(type(turbine)))
        except KeyError:
            raise ValueError('Missing wind_turbine key/column in '
                             'wind_turbine_fleet parameter.')

        # add columns for number of turbines and total capacity if they don't
        # yet exist
        if 'number_of_turbines' not in self.wind_turbine_fleet.columns:
            self.wind_turbine_fleet['number_of_turbines'] = np.nan
{'fraction_non_alphanumeric': 0.08836244163928757, 'fraction_numerical': 0.013141967836762926, 'mean_word_length': 2.4405116002379534, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1651169', 'n_tokens_mistral': 3548, 'n_tokens_neox': 3276, 'n_words': 1080}