Skip to content

Commit 8dd1070

Browse files
committed
Merge branch 'dev' into release_preparation
2 parents ead05a6 + 8f79ecd commit 8dd1070

File tree

2 files changed

+67
-94
lines changed

2 files changed

+67
-94
lines changed

tests/test_wake_losses.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from pandas.util.testing import assert_series_equal
55

66
from windpowerlib.wake_losses import (reduce_wind_speed,
7-
display_wind_efficiency_curves)
7+
get_wind_efficiency_curve)
8+
89

910
class TestWakeLosses:
1011

@@ -31,6 +32,20 @@ def test_reduce_wind_speed(self):
3132
parameters['wind_efficiency_curve_name'] = 'dena_misspelled'
3233
reduce_wind_speed(**parameters)
3334

34-
def test_display_wind_efficiency_curves(self):
35-
# This test just runs the function
36-
display_wind_efficiency_curves()
35+
def test_get_wind_efficiency_curve_one(self):
36+
"""Test get_wind_efficiency_curve() for one curve."""
37+
wec = get_wind_efficiency_curve().sum()
38+
wec_exp = pd.Series({'efficiency': 162.45047,
39+
'wind_speed': 1915.23620})
40+
assert_series_equal(wec.sort_index(), wec_exp.sort_index())
41+
42+
def test_get_wind_efficiency_curve_all(self):
43+
"""Test get_wind_efficiency_curve() for all curves."""
44+
wec_all_sum = int(get_wind_efficiency_curve('all').sum().round().sum())
45+
assert wec_all_sum == 12145
46+
47+
def test_get_wind_efficiency_curve_list(self):
48+
"""Test get_wind_efficiency_curve() for all curves."""
49+
wec_all_sum = int(get_wind_efficiency_curve(
50+
['dena_mean', 'knorr_mean']).sum().round().sum())
51+
assert wec_all_sum == 3568

windpowerlib/wake_losses.py

Lines changed: 48 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@
1010
import numpy as np
1111
import pandas as pd
1212
import os
13-
import logging
14-
15-
try:
16-
from matplotlib import pyplot as plt
17-
except ImportError:
18-
plt = None
1913

2014

2115
def reduce_wind_speed(wind_speed, wind_efficiency_curve_name='dena_mean'):
@@ -67,9 +61,10 @@ def get_wind_efficiency_curve(curve_name='dena_mean'):
6761
6862
Parameters
6963
----------
70-
curve_name : String
64+
curve_name : str or list
7165
Specifies the curve. Use display_wind_efficiency_curves() to see all
72-
provided wind efficiency curves. Default: 'dena_mean'.
66+
provided wind efficiency curves. Default: 'dena_mean'. Use 'all' to
67+
get all curves in a MultiIndex DataFrame.
7368
7469
Returns
7570
-------
@@ -98,91 +93,54 @@ def get_wind_efficiency_curve(curve_name='dena_mean'):
9893
Windleistungssimulationen". Universität Kassel, Diss., 2016,
9994
p. 124
10095
96+
Examples
97+
--------
98+
# Example to plot all curves
99+
fig, ax = plt.subplots()
100+
df = get_wind_efficiency_curve(curve_name='all')
101+
for t in df.columns.get_level_values(0).unique():
102+
p = df[t].set_index('wind_speed')['efficiency']
103+
p.name = t
104+
ax = p.plot(ax=ax, legend=True)
105+
plt.show()
106+
101107
"""
102108
possible_curve_names = ['dena_mean', 'knorr_mean', 'dena_extreme1',
103109
'dena_extreme2', 'knorr_extreme1',
104110
'knorr_extreme2', 'knorr_extreme3']
105-
if curve_name.split('_')[0] not in ['dena', 'knorr']:
106-
raise ValueError("`curve_name` must be one of the following: " +
107-
"{} but is {}".format(possible_curve_names,
108-
curve_name))
109-
path = os.path.join(os.path.dirname(__file__), 'data',
110-
'wind_efficiency_curves_{}.csv'.format(
111-
curve_name.split('_')[0]))
112-
# Read wind efficiency curves from file
113-
wind_efficiency_curves = pd.read_csv(path)
114-
# Raise error if wind efficiency curve specified in 'curve_name' does not
115-
# exist
116-
if curve_name not in list(wind_efficiency_curves):
117-
raise ValueError("Efficiency curve name does not exist. Must be one" +
118-
"of the following: {}".format(possible_curve_names) +
119-
"but is {}".format(curve_name))
120-
# Get wind efficiency curve and rename column containing efficiency
121-
efficiency_curve = wind_efficiency_curves[['wind_speed', curve_name]]
122-
efficiency_curve.columns = ['wind_speed', 'efficiency']
123-
return efficiency_curve
124-
125-
126-
def display_wind_efficiency_curves(print_out=False, plot=False):
127-
r"""
128-
Displays all names of wind efficiency curves available in the windpowerlib.
129-
130-
Parameters
131-
----------
132-
print_out : Boolean
133-
If True the values of the wind efficiency curves are printed. Default:
134-
False.
135-
plot : Boolean
136-
If True all available wind efficiency curves are plotted - if
137-
matplotlib is installed. Default: False.
138111

139-
Notes
140-
-----
141-
The wind efficiency curves were generated in the "Dena Netzstudie" and in
142-
the dissertation of Kaspar Knorr. For more information see [1]_ and [2]_.
143-
144-
References
145-
----------
146-
.. [1] Kohler et.al.: "dena-Netzstudie II. Integration erneuerbarer
147-
Energien in die deutsche Stromversorgung im Zeitraum 2015 – 2020
148-
mit Ausblick 2025.", Deutsche Energie-Agentur GmbH (dena),
149-
Tech. rept., 2010, p. 101
150-
.. [2] Knorr, K.: "Modellierung von raum-zeitlichen Eigenschaften der
151-
Windenergieeinspeisung für wetterdatenbasierte
152-
Windleistungssimulationen". Universität Kassel, Diss., 2016,
153-
p. 124
154-
155-
"""
156-
origins = ['dena', 'knorr']
157-
paths = [os.path.join(os.path.dirname(__file__), 'data',
158-
'wind_efficiency_curves_{}.csv'.format(origin)) for
159-
origin in origins]
160-
# Read wind efficiency curves from files
161-
# Create separate data frames for origin of curve
162-
dena_df = pd.read_csv(paths[0])
163-
knorr_df = pd.read_csv(paths[1])
164-
# Print names of all available curves
165-
curves_list = [col for col in dena_df if 'wind_speed' not in col]
166-
curves_list.extend([col for col in knorr_df if 'wind_speed' not in col])
167-
print("Names of the provided wind efficiency curves are the " +
168-
"following: {}".format(curves_list))
169-
print("To plot the different wind efficiency curves use this function " +
170-
"with the parameter plot=True.")
171-
if plt and plot is True:
172-
# Create data frames for plot
173-
dena_df.set_index('wind_speed', inplace=True)
174-
knorr_df.set_index('wind_speed', inplace=True)
175-
fig, ax = plt.subplots()
176-
dena_df.plot(ax=ax, legend=True, linestyle='--')
177-
knorr_df.plot(ax=ax, legend=True)
178-
plt.ylabel('Wind efficiency')
179-
plt.xlabel('Wind speed m/s')
180-
plt.show()
112+
if curve_name == 'all':
113+
curve_names = possible_curve_names
114+
elif isinstance(curve_name, str):
115+
curve_names = [curve_name]
181116
else:
182-
logging.debug("Matplotlib is not installed.")
183-
if print_out is True:
184-
print(dena_df)
185-
print(knorr_df)
186-
187-
if __name__ == "__main__":
188-
display_wind_efficiency_curves()
117+
curve_names = curve_name
118+
119+
efficiency_curve = pd.DataFrame(columns=pd.MultiIndex(levels=[[], []],
120+
labels=[[], []]))
121+
122+
for curve_name in curve_names:
123+
if curve_name.split('_')[0] not in ['dena', 'knorr']:
124+
raise ValueError("`curve_name` must be one of the following: " +
125+
"{} but is {}".format(possible_curve_names,
126+
curve_name))
127+
path = os.path.join(os.path.dirname(__file__), 'data',
128+
'wind_efficiency_curves_{}.csv'.format(
129+
curve_name.split('_')[0]))
130+
# Read wind efficiency curves from file
131+
wind_efficiency_curves = pd.read_csv(path)
132+
# Raise error if wind efficiency curve specified in 'curve_name' does
133+
# not exist
134+
if curve_name not in list(wind_efficiency_curves):
135+
msg = ("Efficiency curve <{0}> does not exist. Must be one of the"
136+
"following: {1}.")
137+
raise ValueError(msg.format(curve_name, *possible_curve_names))
138+
139+
# Get wind efficiency curve and rename column containing efficiency
140+
wec = wind_efficiency_curves[['wind_speed', curve_name]]
141+
efficiency_curve[curve_name, 'wind_speed'] = wec['wind_speed']
142+
efficiency_curve[curve_name, 'efficiency'] = wec[curve_name]
143+
if len(curve_names) == 1:
144+
return efficiency_curve[curve_names[0]]
145+
else:
146+
return efficiency_curve

0 commit comments

Comments
 (0)