|
4 | 4 |
|
5 | 5 | """ |
6 | 6 |
|
7 | | -import numpy as np |
8 | | - |
9 | 7 |
|
10 | 8 | __copyright__ = "Copyright oemof developer group" |
11 | 9 | __license__ = "GPLv3" |
12 | 10 |
|
13 | 11 |
|
14 | | -def smallest_difference(value_1, value_2, comp_value, corresp_1, corresp_2): |
| 12 | +def smallest_difference(data_frame, comp_value, column_name): |
15 | 13 | 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. |
17 | 15 |
|
18 | 16 | Additionally returns a corresponding value. This function is for example |
19 | 17 | used in :py:func:`~.modelchain.v_wind_hub` of the |
20 | 18 | :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. |
24 | 22 |
|
25 | 23 | Parameters |
26 | 24 | ---------- |
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. |
32 | 30 | comp_value : float |
33 | 31 | 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. |
38 | 35 |
|
39 | 36 | Returns |
40 | 37 | ------- |
41 | 38 | 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. |
44 | 41 |
|
45 | 42 | """ |
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] |
59 | 48 | return (closest_value, corresp_value) |
60 | 49 |
|
61 | 50 |
|
@@ -95,29 +84,19 @@ def linear_extra_interpolation(data_frame, requested_height, column_name): |
95 | 84 | (height_{requested} - height_1) + value_1 |
96 | 85 |
|
97 | 86 | 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, |
100 | 89 | :math:`value_2`: corresponding value to `height_2`, |
101 | 90 | :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 |
103 | 92 | place |
104 | 93 |
|
105 | 94 | """ |
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) |
123 | 102 | return interpolant |
0 commit comments