|
11 | 11 | __license__ = "GPLv3" |
12 | 12 |
|
13 | 13 |
|
14 | | -def smallest_difference(value_1, value_2, comp_value, corresp_1, corresp_2): |
| 14 | +def smallest_difference(data_frame, comp_value, column_name): |
15 | 15 | r""" |
16 | | - Selects the value with the smaller difference to a comparative value. |
| 16 | + Selects a value with the smallest difference to a comparative value. |
17 | 17 |
|
18 | 18 | Additionally returns a corresponding value. This function is for example |
19 | 19 | used in :py:func:`~.modelchain.v_wind_hub` of the |
20 | 20 | :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`. |
| 21 | + close to the hub height of the examined wind turbine. In this case the |
| 22 | + column of the data frame contains wind speed time series and the indices |
| 23 | + are the corresponding heights for which these time series apply. |
24 | 24 |
|
25 | 25 | Parameters |
26 | 26 | ---------- |
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. |
| 27 | + data_frame : DataFrame |
| 28 | + Indices are the values of which the smallest difference to `comp_value` |
| 29 | + will be found, the corresponding values are in the column |
| 30 | + specified by `column_name` and they can be floats, pd.Series or |
| 31 | + np.arrays. |
32 | 32 | comp_value : float |
33 | 33 | 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`. |
| 34 | + column_name : string |
| 35 | + Name of the column in the `data_frame` that contains the |
| 36 | + correponding values. |
38 | 37 |
|
39 | 38 | Returns |
40 | 39 | ------- |
41 | 40 | Tuple(float, pd.Series or np.array or float) |
42 | | - Value closer to comparing value as float and its corresponding value as |
43 | | - float. |
| 41 | + Closest value to comparative value as float and its corresponding value |
| 42 | + as float. |
44 | 43 |
|
45 | 44 | """ |
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 |
| 45 | + diff = [] |
| 46 | + for index in sorted(data_frame.index): |
| 47 | + diff.append(abs(comp_value - index)) |
| 48 | + closest_value = sorted(data_frame.index)[diff.index(min(diff))] |
| 49 | + corresp_value = data_frame[column_name][closest_value] |
59 | 50 | return (closest_value, corresp_value) |
60 | 51 |
|
61 | 52 |
|
|
0 commit comments