Skip to content

Commit a05d8d0

Browse files
Birgit SchachlerBirgit Schachler
authored andcommitted
New definition linear_interpolation_extrapolation
1 parent a65ad83 commit a05d8d0

File tree

1 file changed

+43
-76
lines changed

1 file changed

+43
-76
lines changed

windpowerlib/tools.py

Lines changed: 43 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
The ``tools`` module contains a collection of functions used in the
2+
The ``tools`` module contains a collection of helper functions used in the
33
windpowerlib.
44
55
"""
@@ -9,97 +9,64 @@
99
__license__ = "GPLv3"
1010

1111

12-
def smallest_difference(data_frame, comp_value, column_name):
13-
r"""
14-
Selects a value with the smallest difference to a comparative value.
15-
16-
Additionally returns a corresponding value. This function is for example
17-
used in :py:func:`~.modelchain.v_wind_hub` of the
18-
:class:`~.modelchain.ModelChain` to choose the wind speed data that is the
19-
closest to the hub height of the examined wind turbine. In this case the
20-
column of the data frame contains wind speed time series and the indices
21-
are the corresponding heights for which these time series apply.
22-
23-
Parameters
24-
----------
25-
data_frame : pandas.DataFrame
26-
Indices are the values of which the smallest difference to `comp_value`
27-
will be found, the corresponding values are in the column
28-
specified by `column_name` and they can be floats, pandas.Series or
29-
numpy.arrays.
30-
comp_value : float
31-
Comparative value.
32-
column_name : string
33-
Name of the column in the `data_frame` that contains the
34-
corresponding values.
35-
36-
Returns
37-
-------
38-
Tuple(float, float or pandas.Series or numpy.array)
39-
Closest value to comparative value as float and its corresponding value
40-
as float, pandas.Series or numpy.array.
41-
42-
"""
43-
# Calculate difference to comp_value for all indices of data_frame
44-
diff = []
45-
for index in sorted(data_frame.index):
46-
diff.append(abs(comp_value - index))
47-
# Find smallest difference
48-
closest_value = sorted(data_frame.index)[diff.index(min(diff))]
49-
corresp_value = data_frame[column_name][closest_value]
50-
return closest_value, corresp_value
51-
52-
53-
def linear_extra_interpolation(data_frame, requested_height):
12+
def linear_interpolation_extrapolation(df, target_height):
5413
r"""
5514
Inter- or extrapolates between the values of a data frame.
5615
57-
This function can for example be used for the interpolation of a wind
58-
speed, density or temperature to calculated these values at hub height of a
59-
wind turbine.
16+
This function can be used for the inter-/extrapolation of a parameter
17+
(e.g wind speed) available at two or more different heights, to approximate
18+
the value at hub height. The function is carried out when the parameter
19+
`wind_speed_model`, `density_model` or `temperature_model` of an
20+
instance of the :class:`~.modelchain.ModelChain` class is
21+
'interpolation_extrapolation'.
6022
6123
Parameters
6224
----------
63-
data_frame : pandas.DataFrame
64-
Indices are the values between which will be interpolated or from which
65-
will be extrapolated, the corresponding values are in the column
66-
specified by `column_name` and can be floats, pandas.Series or
67-
numpy.arrays.
68-
requested_height : float
69-
Height for which the interpolation takes place (e.g. hub height of wind
70-
turbine).
71-
column_name : string
72-
Name of the column in the DataFrame `data_frame` that contains the
73-
corresponding values.
25+
df : pandas.DataFrame
26+
DataFrame with time series for parameter that is to be interpolated or
27+
extrapolated. The columns of the DataFrame are the different heights
28+
for which the parameter is available. If more than two heights are
29+
given, the two closest heights are used. See example below on how the
30+
DataFrame should look like and how the function can be used.
31+
target_height : float
32+
Height for which the parameter is approximated (e.g. hub height).
7433
7534
Returns
7635
-------
77-
interpolant : float or pandas.Series or numpy.array
78-
Result of the interpolation (e.g. density at hub height).
36+
pandas.Series
37+
Result of the inter-/extrapolation (e.g. wind speed at hub height).
7938
8039
Notes
8140
-----
8241
8342
For the inter- and extrapolation the following equation is used:
8443
85-
.. math:: interpolant = (value_2 - value_1) / (height_2 - height_1) *
86-
(height_{requested} - height_1) + value_1
87-
88-
with:
89-
:math:`height_2`: index of data frame closest to
90-
:math:`height_{requested}`, :math:`height_1`: index of data frame
91-
second closest to :math:`height_{requested}`,
92-
:math:`value_2`: corresponding value to `height_2`,
93-
:math:`value_1`: corresponding value to `height_1`,
94-
:math:`height_{requested}` : height for which the interpolation takes
95-
place
44+
.. math:: f(x) = (f(x_2) - f(x_1)) / (x_2 - x_1) * (x - x_1) + f(x_1)
45+
46+
Examples
47+
---------
48+
>>> import numpy as np
49+
>>> import pandas as pd
50+
>>> wind_speed_10m = np.array([[3], [4]])
51+
>>> wind_speed_80m = np.array([[6], [6]])
52+
>>> weather_df = pd.DataFrame(np.hstack((wind_speed_10m,
53+
... wind_speed_80m)),
54+
... index=pd.date_range('1/1/2012',
55+
... periods=2,
56+
... freq='H'),
57+
... columns=[np.array(['wind_speed',
58+
... 'wind_speed']),
59+
... np.array([10, 80])])
60+
>>> linear_interpolation_extrapolation(
61+
... weather_df['wind_speed'], 100)[0]
62+
6.857143
9663
9764
"""
9865
# find closest heights
99-
heights_sorted = data_frame.columns[
100-
sorted(range(len(data_frame.columns)),
101-
key=lambda i: abs(data_frame.columns[i] - requested_height))]
102-
return ((data_frame[heights_sorted[1]] - data_frame[heights_sorted[0]]) /
66+
heights_sorted = df.columns[
67+
sorted(range(len(df.columns)),
68+
key=lambda i: abs(df.columns[i] - target_height))]
69+
return ((df[heights_sorted[1]] - df[heights_sorted[0]]) /
10370
(heights_sorted[1] - heights_sorted[0]) *
104-
(requested_height - heights_sorted[0]) +
105-
data_frame[heights_sorted[0]])
71+
(target_height - heights_sorted[0]) +
72+
df[heights_sorted[0]])

0 commit comments

Comments
 (0)