Skip to content

Commit 619c671

Browse files
committed
Merge branch 'features/tools' into features/tests
2 parents c2a572f + e16ec3c commit 619c671

File tree

2 files changed

+34
-54
lines changed

2 files changed

+34
-54
lines changed

windpowerlib/modelchain.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ def rho_hub(self, weather, data_height):
118118
----------
119119
weather : DataFrame or Dictionary
120120
Containing columns or keys with timeseries for temperature
121-
`temp_air` in K and pressure `pressure` in Pa.
121+
`temp_air` in K and pressure `pressure` in Pa, as well as
122+
optionally the temperature `temp_air_2` in K at a different height.
122123
If a Dictionary is used the data inside the dictionary has to be of
123124
the types pandas.Series or numpy.array.
124125
data_height : Dictionary
@@ -305,8 +306,8 @@ def run_model(self, weather, data_height):
305306
Containing columns or keys with the timeseries for wind speed
306307
`v_wind` in m/s, roughness length `z0` in m, temperature
307308
`temp_air` in K and pressure `pressure` in Pa, as well as
308-
optionally wind speed `v_wind_2` in m/s in K at different height
309-
for interpolation.
309+
optionally wind speed `v_wind_2` in m/s and temperature
310+
- `temp_air_2` in K at different height.
310311
If a Dictionary is used the data inside the dictionary has to be of
311312
the types pandas.Series or numpy.array.
312313
data_height : Dictionary

windpowerlib/tools.py

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,58 +4,47 @@
44
55
"""
66

7-
import numpy as np
8-
97

108
__copyright__ = "Copyright oemof developer group"
119
__license__ = "GPLv3"
1210

1311

14-
def smallest_difference(value_1, value_2, comp_value, corresp_1, corresp_2):
12+
def smallest_difference(data_frame, comp_value, column_name):
1513
r"""
16-
Selects the value with the smaller difference to a comparative value.
14+
Selects a value with the smallest difference to a comparative value.
1715
1816
Additionally returns a corresponding value. This function is for example
1917
used in :py:func:`~.modelchain.v_wind_hub` of the
2018
:class:`~.modelchain.ModelChain` to choose the wind speed data that is
21-
close to the hub height of the examined wind turbine. In this case
22-
`value_1` and `value_2` are the heights of the corresponding wind speed
23-
data sets `corresp_1` and `corresp_2`.
19+
close 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.
2422
2523
Parameters
2624
----------
27-
value_1 : float
28-
First value of which the difference to `comp_value` will be
29-
compared with the difference to `comp_value` of `value_2`.
30-
value_2 : float
31-
Second value for comparison.
25+
data_frame : 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, pd.Series or
29+
np.arrays.
3230
comp_value : float
3331
Comparative value.
34-
corresp_1 : pd.Series or np.array or float
35-
Corresponding value to `value_1`.
36-
corresp_2 : pd.Series or np.array or float
37-
Corresponding value to `value_2`.
32+
column_name : string
33+
Name of the column in the `data_frame` that contains the
34+
correponding values.
3835
3936
Returns
4037
-------
4138
Tuple(float, pd.Series or np.array or float)
42-
Value closer to comparing value as float and its corresponding value as
43-
float.
39+
Closest value to comparative value as float and its corresponding value
40+
as float.
4441
4542
"""
46-
if (value_2 is not None and corresp_2 is not None):
47-
if abs(value_1 - comp_value) <= abs(value_2 - comp_value):
48-
closest_value = value_1
49-
else:
50-
closest_value = value_2
51-
else:
52-
closest_value = value_1
53-
54-
# Select correponding value
55-
if closest_value == value_1:
56-
corresp_value = corresp_1
57-
else:
58-
corresp_value = corresp_2
43+
diff = []
44+
for index in sorted(data_frame.index):
45+
diff.append(abs(comp_value - index))
46+
closest_value = sorted(data_frame.index)[diff.index(min(diff))]
47+
corresp_value = data_frame[column_name][closest_value]
5948
return (closest_value, corresp_value)
6049

6150

@@ -95,29 +84,19 @@ def linear_extra_interpolation(data_frame, requested_height, column_name):
9584
(height_{requested} - height_1) + value_1
9685
9786
with:
98-
:math:`height_2`: largest/smallest value in data frame,
99-
:math:`height_1`: second largest/smallest value in data frame,
87+
:math:`height_2`: largest/smallest index of data frame,
88+
:math:`height_1`: second largest/smallest index of data frame,
10089
:math:`value_2`: corresponding value to `height_2`,
10190
:math:`value_1`: correponding value to `height_1`,
102-
:math:`height_{requested}` : Height for which the interpolation takes
91+
:math:`height_{requested}` : height for which the interpolation takes
10392
place
10493
10594
"""
106-
if requested_height > max(data_frame.index):
107-
height_2 = max(data_frame.index)
108-
value_2 = data_frame[column_name][height_2]
109-
height_1 = sorted(data_frame.index)[-2] # Second largest number
110-
value_1 = data_frame[column_name][height_1]
111-
interpolant = ((value_2 - value_1) / (height_2 - height_1) *
112-
(requested_height - height_1) + value_1)
113-
elif requested_height < min(data_frame.index):
114-
height_2 = min(data_frame.index)
115-
value_2 = data_frame[column_name][height_2]
116-
height_1 = sorted(data_frame.index)[1] # Second smallest number
117-
value_1 = data_frame[column_name][height_1]
118-
interpolant = ((value_2 - value_1) / (height_2 - height_1) *
119-
(requested_height - height_1) + value_1)
120-
else:
121-
interpolant = np.interp(requested_height, data_frame.index,
122-
data_frame[column_name])
95+
height_2, value_2 = smallest_difference(data_frame, requested_height,
96+
column_name)
97+
data_frame_2 = data_frame.drop(height_2)
98+
height_1, value_1 = smallest_difference(data_frame_2, requested_height,
99+
column_name)
100+
interpolant = ((value_2 - value_1) / (height_2 - height_1) *
101+
(requested_height - height_1) + value_1)
123102
return interpolant

0 commit comments

Comments
 (0)