Skip to content

Commit f8eaacc

Browse files
committed
Add function for linear inter- and/or extrapolation
1 parent f27be71 commit f8eaacc

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

windpowerlib/tools.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
55
"""
66

7+
import numpy as np
8+
9+
710
__copyright__ = "Copyright oemof developer group"
811
__license__ = "GPLv3"
912

@@ -54,3 +57,64 @@ def smallest_difference(value_1, value_2, comp_value, corresp_1, corresp_2):
5457
else:
5558
corresp_value = corresp_2
5659
return (closest_value, corresp_value)
60+
61+
62+
def linear_extra_interpolation(data_frame, requested_height, column_name):
63+
r"""
64+
Inter- or extrapolates between the values of a data frame.
65+
66+
This function can for example be used for the interpolation of a wind
67+
speed, density or temperature.
68+
69+
Parameters
70+
----------
71+
data_frame : DataFrame
72+
Indices are the values between which will be interpolated or from which
73+
will be extrapolated, the corresponding values are in the column
74+
specified by `column_name` and they can be floats, pd.Series or
75+
np.arrays.
76+
requested_height : float
77+
Height for which the interpolation takes place (e.g. hub height of wind
78+
turbine).
79+
column_name : string
80+
Name of the column in the DataFrame `data_frame` that contains the
81+
correponding values.
82+
83+
Returns
84+
-------
85+
interpolant : pandas.Series, numpy.array or float
86+
Result of the interpolation (e.g. density at hub height).
87+
88+
Notes
89+
-----
90+
91+
For the interpolation np.interp() is used and the following equation is
92+
used for extrapolation:
93+
94+
.. math:: interpolant = (value_2 - value_1) / (height_2 - height_1) *
95+
(requested_height - height_1) + value_1
96+
97+
with:
98+
height_2: largest/smallest value in data frame, height_1: second
99+
largest/smallest value in data frame, value_2: corresponding value to
100+
height_2, value_1: correponding value to height_1
101+
102+
"""
103+
if requested_height > max(data_frame.index):
104+
height_2 = max(data_frame.index)
105+
value_2 = data_frame[column_name][height_2]
106+
height_1 = sorted(data_frame.index)[-2] # Second largest number
107+
value_1 = data_frame[column_name][height_1]
108+
interpolant = ((value_2 - value_1) / (height_2 - height_1) *
109+
(requested_height - height_1) + value_1)
110+
elif requested_height < min(data_frame.index):
111+
height_2 = min(data_frame.index)
112+
value_2 = data_frame[column_name][height_2]
113+
height_1 = sorted(data_frame.index)[1] # Second smallest number
114+
value_1 = data_frame[column_name][height_1]
115+
interpolant = ((value_2 - value_1) / (height_2 - height_1) *
116+
(requested_height - height_1) + value_1)
117+
else:
118+
interpolant = np.interp(requested_height, data_frame.index,
119+
data_frame[column_name])
120+
return interpolant

0 commit comments

Comments
 (0)