def test_linear_interpolation_extrapolation(self): parameters = {'target_height': 80} df = pd.DataFrame(data={ 10: [2.0, 2.0, 3.0], 80: [4.0, 5.0, 6.0], 200: [5.0, 8.0, 10.0] }, index=[0, 1, 2]) # target_height is equal to height given in a column of the DataFrame exp_output = pd.Series(data=[4.0, 5.0, 6.0]) assert_series_equal( linear_interpolation_extrapolation(df, **parameters), exp_output) # target_height is between heights given in the columns of the # DataFrame exp_output = pd.Series(data=[4.5, 6.5, 8.0]) parameters['target_height'] = 140 assert_series_equal( linear_interpolation_extrapolation(df, **parameters), exp_output) exp_output = pd.Series(data=[4.285714, 5.428571, 6.428571]) parameters['target_height'] = 90 assert_series_equal( linear_interpolation_extrapolation(df, **parameters), exp_output) # target_height is greater than the heights given in the columns of the # DataFrame exp_output = pd.Series(data=[5.333333, 9.0, 11.333333]) parameters['target_height'] = 240 assert_series_equal( linear_interpolation_extrapolation(df, **parameters), exp_output) # target_height is smaller than the heights given in the columns of the # DataFrame exp_output = pd.Series(data=[1.857143, 1.785714, 2.785714]) parameters['target_height'] = 5 assert_series_equal( linear_interpolation_extrapolation(df, **parameters), exp_output)
def test_linear_target_height_is_between_given_heights(self): """ Test linear interpolation and extrapolation if target_height is between heights given in the columns of the DataFrame """ exp_output = pd.Series(data=[4.5, 6.5, 8.0]) self.parameters['target_height'] = 140 assert_series_equal( linear_interpolation_extrapolation(self.df, **self.parameters), exp_output) exp_output = pd.Series(data=[4.285714, 5.428571, 6.428571]) self.parameters['target_height'] = 90 assert_series_equal( linear_interpolation_extrapolation(self.df, **self.parameters), exp_output)
def test_linear_target_height_is_equal_to_given_height(self): """ Test linear interpolation and extrapolation if target_height is equal to height given in a column of the DataFrame. """ exp_output = pd.Series(data=[4.0, 5.0, 6.0]) assert_series_equal( linear_interpolation_extrapolation(self.df, **self.parameters), exp_output)
def temperature_hub(self, weather_df): r""" Calculates the temperature of air at hub height. The temperature is calculated using the method specified by the parameter `temperature_model`. Parameters ---------- weather_df : :pandas:`pandas.DataFrame<frame>` DataFrame with time series for temperature `temperature` in K. The columns of the DataFrame are a MultiIndex where the first level contains the variable name (e.g. temperature) and the second level contains the height at which it applies (e.g. 10, if it was measured at a height of 10 m). See documentation of :func:`ModelChain.run_model` for an example on how to create the weather_df DataFrame. Returns ------- :pandas:`pandas.Series<series>` or numpy.array Temperature of air in K at hub height. Notes ----- If `weather_df` contains temperatures at different heights the given temperature(s) closest to the hub height are used. """ if self.power_plant.hub_height in weather_df["temperature"]: temperature_hub = weather_df["temperature"][ self.power_plant.hub_height] elif self.temperature_model == "linear_gradient": logging.debug("Calculating temperature using temperature " "gradient.") closest_height = weather_df["temperature"].columns[min( range(len(weather_df["temperature"].columns)), key=lambda i: abs(weather_df["temperature"].columns[i] - self. power_plant.hub_height), )] temperature_hub = temperature.linear_gradient( weather_df["temperature"][closest_height], closest_height, self.power_plant.hub_height, ) elif self.temperature_model == "interpolation_extrapolation": logging.debug("Calculating temperature using linear inter- or " "extrapolation.") temperature_hub = tools.linear_interpolation_extrapolation( weather_df["temperature"], self.power_plant.hub_height) else: raise ValueError( "'{0}' is an invalid value. ".format(self.temperature_model) + "`temperature_model` must be " "'linear_gradient' or 'interpolation_extrapolation'.") return temperature_hub
def test_linear_target_height_is_smaller_than_the_given_heights(self): """ Test linear interpolation and extrapolation if target_height is smaller than the heights given in the columns of the DataFrame """ exp_output = pd.Series(data=[1.857143, 1.785714, 2.785714]) self.parameters['target_height'] = 5 assert_series_equal( linear_interpolation_extrapolation(self.df, **self.parameters), exp_output)
def test_linear_target_height_is_greater_than_the_given_heights(self): """ Test linear interpolation and extrapolation if target_height is greater than the heights given in the columns of the DataFrame """ exp_output = pd.Series(data=[5.333333, 9.0, 11.333333]) self.parameters['target_height'] = 240 assert_series_equal( linear_interpolation_extrapolation(self.df, **self.parameters), exp_output)
def wind_speed_hub(self, weather_df): r""" Calculates the wind speed at hub height. The method specified by the parameter `wind_speed_model` is used. Parameters ---------- weather_df : :pandas:`pandas.DataFrame<frame>` DataFrame with time series for wind speed `wind_speed` in m/s and roughness length `roughness_length` in m. The columns of the DataFrame are a MultiIndex where the first level contains the variable name (e.g. wind_speed) and the second level contains the height at which it applies (e.g. 10, if it was measured at a height of 10 m). See documentation of :func:`ModelChain.run_model` for an example on how to create the weather_df DataFrame. Returns ------- :pandas:`pandas.Series<series>` or numpy.array Wind speed in m/s at hub height. Notes ----- If `weather_df` contains wind speeds at different heights the given wind speed(s) closest to the hub height are used. """ if self.power_plant.hub_height in weather_df["wind_speed"]: wind_speed_hub = weather_df["wind_speed"][ self.power_plant.hub_height] elif self.wind_speed_model == "logarithmic": logging.debug("Calculating wind speed using logarithmic wind " "profile.") closest_height = weather_df["wind_speed"].columns[min( range(len(weather_df["wind_speed"].columns)), key=lambda i: abs(weather_df["wind_speed"].columns[i] - self. power_plant.hub_height), )] wind_speed_hub = wind_speed.logarithmic_profile( weather_df["wind_speed"][closest_height], closest_height, self.power_plant.hub_height, weather_df["roughness_length"].iloc[:, 0], self.obstacle_height, ) elif self.wind_speed_model == "hellman": logging.debug("Calculating wind speed using hellman equation.") closest_height = weather_df["wind_speed"].columns[min( range(len(weather_df["wind_speed"].columns)), key=lambda i: abs(weather_df["wind_speed"].columns[i] - self. power_plant.hub_height), )] wind_speed_hub = wind_speed.hellman( weather_df["wind_speed"][closest_height], closest_height, self.power_plant.hub_height, weather_df["roughness_length"].iloc[:, 0], self.hellman_exp, ) elif self.wind_speed_model == "interpolation_extrapolation": logging.debug("Calculating wind speed using linear inter- or " "extrapolation.") wind_speed_hub = tools.linear_interpolation_extrapolation( weather_df["wind_speed"], self.power_plant.hub_height) elif self.wind_speed_model == "log_interpolation_extrapolation": logging.debug("Calculating wind speed using logarithmic inter- or " "extrapolation.") wind_speed_hub = tools.logarithmic_interpolation_extrapolation( weather_df["wind_speed"], self.power_plant.hub_height) else: raise ValueError( "'{0}' is an invalid value. ".format(self.wind_speed_model) + "`wind_speed_model` must be " "'logarithmic', 'hellman', 'interpolation_extrapolation' " + "or 'log_interpolation_extrapolation'.") return wind_speed_hub
def density_hub(self, weather_df): r""" Calculates the density of air at hub height. The density is calculated using the method specified by the parameter `density_model`. Previous to the calculation of the density the temperature at hub height is calculated using the method specified by the parameter `temperature_model`. Parameters ---------- weather_df : :pandas:`pandas.DataFrame<frame>` DataFrame with time series for temperature `temperature` in K, pressure `pressure` in Pa and/or density `density` in kg/m³, depending on the `density_model` used. The columns of the DataFrame are a MultiIndex where the first level contains the variable name (e.g. temperature) and the second level contains the height at which it applies (e.g. 10, if it was measured at a height of 10 m). See documentation of :func:`ModelChain.run_model` for an example on how to create the weather_df DataFrame. Returns ------- :pandas:`pandas.Series<series>` or numpy.array Density of air in kg/m³ at hub height. Notes ----- If `weather_df` contains data at different heights the data closest to the hub height are used. If `interpolation_extrapolation` is used to calculate the density at hub height, the `weather_df` must contain at least two time series for density. """ if self.density_model != "interpolation_extrapolation": temperature_hub = self.temperature_hub(weather_df) # Calculation of density in kg/m³ at hub height if self.density_model == "barometric": logging.debug("Calculating density using barometric height " "equation.") closest_height = weather_df["pressure"].columns[min( range(len(weather_df["pressure"].columns)), key=lambda i: abs(weather_df["pressure"].columns[i] - self. power_plant.hub_height), )] density_hub = density.barometric( weather_df["pressure"][closest_height], closest_height, self.power_plant.hub_height, temperature_hub, ) elif self.density_model == "ideal_gas": logging.debug("Calculating density using ideal gas equation.") closest_height = weather_df["pressure"].columns[min( range(len(weather_df["pressure"].columns)), key=lambda i: abs(weather_df["pressure"].columns[i] - self. power_plant.hub_height), )] density_hub = density.ideal_gas( weather_df["pressure"][closest_height], closest_height, self.power_plant.hub_height, temperature_hub, ) elif self.density_model == "interpolation_extrapolation": logging.debug("Calculating density using linear inter- or " "extrapolation.") density_hub = tools.linear_interpolation_extrapolation( weather_df["density"], self.power_plant.hub_height) else: raise ValueError( "'{0}' is an invalid value. ".format(self.density_model) + "`density_model` " + "must be 'barometric', 'ideal_gas' or " + "'interpolation_extrapolation'.") return density_hub