Skip to content

Commit c09f586

Browse files
committed
Add function for selection of value closer to comparative value
1 parent 9c4d102 commit c09f586

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

windpowerlib/tools.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,66 @@
66

77
__copyright__ = "Copyright oemof developer group"
88
__license__ = "GPLv3"
9+
10+
import collections
11+
12+
13+
def select_closer_value(value_1, value_2, comp_value, corresp_1, corresp_2):
14+
r"""
15+
Selects the value with the smaller difference to a comparative value.
16+
17+
Additionally returns a corresponding value. This function is for example
18+
used in :py:func:`~.modelchain.v_wind_hub` of the
19+
:class:`~.modelchain.ModelChain` to choose the wind speed data that is
20+
close to the hub height of the examined wind turbine. In this case
21+
`value_1` and `value_2` are the heights of the corresponding wind speed
22+
data sets `corresp_1` and `corresp_2`.
23+
24+
Parameters
25+
----------
26+
value_1 : float
27+
First value of which the difference to `comp_value` will be
28+
compared with the difference to `comp_value` of `value_2`.
29+
value_2 : float
30+
Second value for comparison.
31+
comp_value : float
32+
Comparative value.
33+
corresp_1 : float
34+
Corresponding value to `value_1`.
35+
corresp_2 : float
36+
Corresponding value to `value_2`.
37+
38+
Returns
39+
-------
40+
Tuple(float, float, string)
41+
Value closer to comparing value as float, corresponding value as
42+
float and a string for logging.debug.
43+
"""
44+
if (value_2 is not None and corresp_2 is not None):
45+
if value_1 == comp_value:
46+
closest_value = value_1
47+
logging_string = '(at hub height).'
48+
elif value_2 == comp_value:
49+
closest_value = value_2
50+
logging_string = '(2) (at hub height).'
51+
elif abs(value_1 - comp_value) <= abs(value_2 - comp_value):
52+
closest_value = value_1
53+
logging_string = None
54+
else:
55+
closest_value = value_2
56+
logging_string = None
57+
else:
58+
closest_value = value_1
59+
if value_1 == comp_value:
60+
logging_string = '(at hub height).'
61+
62+
# Select correponding value
63+
if closest_value == value_1:
64+
corresp_value = corresp_1
65+
else:
66+
corresp_value = corresp_2
67+
# Store values in a named tuple
68+
return_tuple = collections.namedtuple('selected_values',
69+
['closest_value',
70+
'corresp_value', 'logging_string'])
71+
return return_tuple(closest_value, corresp_value, logging_string)

0 commit comments

Comments
 (0)