Skip to content

Commit f1649e4

Browse files
committed
Clean examples
1 parent 59835c3 commit f1649e4

File tree

3 files changed

+168
-19
lines changed

3 files changed

+168
-19
lines changed

example/simple_example.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
"""
2+
The ``turbine_cluster_modelchain_example`` module shows how to calculate the
3+
power output of wind farms and wind turbine clusters with the windpowerlib.
4+
A cluster can be useful if you want to calculate the feed-in of a region for
5+
which you want to use one single weather data point.
6+
7+
Functions that are used in the ``modelchain_example``, like the initialization
8+
of wind turbines, are imported and used without further explanations.
9+
10+
SPDX-FileCopyrightText: 2019 oemof developer group <[email protected]>
11+
SPDX-License-Identifier: MIT
12+
"""
13+
import pandas as pd
14+
import requests
15+
import os
16+
17+
from windpowerlib import WindFarm
18+
from windpowerlib import WindTurbine
19+
from windpowerlib import TurbineClusterModelChain
20+
from windpowerlib import WindTurbineCluster
21+
22+
# You can use the logging package to get logging messages from the windpowerlib
23+
# Change the logging level if you want more or less messages
24+
import logging
25+
26+
logging.getLogger().setLevel(logging.INFO)
27+
28+
29+
def get_weather_data(filename="weather.csv", **kwargs):
30+
r"""
31+
Imports weather data from a file.
32+
33+
The data include wind speed at two different heights in m/s, air
34+
temperature in two different heights in K, surface roughness length in m
35+
and air pressure in Pa. The height in m for which the data applies is
36+
specified in the second row.
37+
In case no weather data file exists, an example weather data file is
38+
automatically downloaded and stored in the same directory as this example.
39+
40+
Parameters
41+
----------
42+
filename : str
43+
Filename of the weather data file. Default: 'weather.csv'.
44+
45+
Other Parameters
46+
----------------
47+
datapath : str, optional
48+
Path where the weather data file is stored.
49+
Default is the same directory this example is stored in.
50+
51+
Returns
52+
-------
53+
:pandas:`pandas.DataFrame<frame>`
54+
DataFrame with time series for wind speed `wind_speed` in m/s,
55+
temperature `temperature` in K, roughness length `roughness_length`
56+
in m, and pressure `pressure` in Pa.
57+
The columns of the DataFrame are a MultiIndex where the first level
58+
contains the variable name as string (e.g. 'wind_speed') and the
59+
second level contains the height as integer at which it applies
60+
(e.g. 10, if it was measured at a height of 10 m). The index is a
61+
DateTimeIndex.
62+
63+
"""
64+
65+
if "datapath" not in kwargs:
66+
kwargs["datapath"] = os.path.dirname(__file__)
67+
68+
file = os.path.join(kwargs["datapath"], filename)
69+
70+
# download example weather data file in case it does not yet exist
71+
if not os.path.isfile(file):
72+
logging.debug("Download weather data for example.")
73+
req = requests.get("https://osf.io/59bqn/download")
74+
with open(file, "wb") as fout:
75+
fout.write(req.content)
76+
77+
# read csv file
78+
weather_df = pd.read_csv(
79+
file,
80+
index_col=0,
81+
header=[0, 1],
82+
date_parser=lambda idx: pd.to_datetime(idx, utc=True),
83+
)
84+
85+
# change time zone
86+
weather_df.index = weather_df.index.tz_convert("Europe/Berlin")
87+
88+
return weather_df
89+
90+
91+
def run_example():
92+
r"""
93+
Runs the example.
94+
95+
"""
96+
weather = get_weather_data("weather.csv")
97+
e126 = WindTurbine(
98+
**{
99+
"turbine_type": "E-126/4200", # turbine type as in register
100+
"hub_height": 135, # in m
101+
}
102+
)
103+
v90 = WindTurbine(
104+
**{
105+
"turbine_type": "V90/2000", # turbine name as in register
106+
"hub_height": 120, # in m
107+
}
108+
)
109+
110+
# specification of wind farm data (2) containing a wind farm efficiency
111+
# wind turbine fleet is provided using the to_group function
112+
example_farm = WindFarm(
113+
**{
114+
"name": "example_farm_2",
115+
"wind_turbine_fleet": [
116+
v90.to_group(number_turbines=6),
117+
e126.to_group(total_capacity=12.6e6),
118+
],
119+
"efficiency": 0.5
120+
}
121+
)
122+
123+
example_cluster = WindTurbineCluster(**{
124+
"name": "Offshore_cluster",
125+
"wind_farms": [example_farm, example_farm],
126+
})
127+
128+
# ModelChain with wind farm
129+
mc_farm = TurbineClusterModelChain(
130+
example_farm, wake_losses_model=None,
131+
).run_model(weather)
132+
flh_farm = mc_farm.power_output.sum() / example_farm.nominal_power
133+
134+
# ModelChain with wind cluster
135+
mc_cluster = TurbineClusterModelChain(
136+
example_cluster, wake_losses_model=None,
137+
).run_model(weather)
138+
flh_cluster = mc_cluster.power_output.sum() / example_cluster.nominal_power
139+
140+
print("Full Load Hours of cluster:", flh_cluster)
141+
print("Full Load Hours of farm:", flh_farm)
142+
143+
144+
if __name__ == "__main__":
145+
run_example()

example/turbine_cluster_modelchain_example.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# Change the logging level if you want more or less messages
2727
import logging
2828

29-
logging.getLogger().setLevel(logging.DEBUG)
29+
# logging.getLogger().setLevel(logging.DEBUG)
3030

3131

3232
def initialize_wind_farms(my_turbine, e126):
@@ -145,14 +145,15 @@ class that provides all necessary steps to calculate the power output of a
145145
# power output calculation for example_farm
146146
# initialize TurbineClusterModelChain with default parameters and use
147147
# run_model method to calculate power output
148-
mc_example_farm = TurbineClusterModelChain(example_farm).run_model(weather)
148+
mc_example_farm = TurbineClusterModelChain(example_farm, wake_losses_model="wind_farm_efficiency").run_model(weather)
149149
# write power output time series to WindFarm object
150150
example_farm.power_output = mc_example_farm.power_output
151151

152152
# power output calculation for turbine_cluster
153153
# own specifications for TurbineClusterModelChain setup
154154
modelchain_data = {
155-
"wake_losses_model": "wind_farm_efficiency", # 'dena_mean' (default), None,
155+
"wake_losses_model": "wind_farm_efficiency",
156+
# 'dena_mean' (default), None,
156157
# 'wind_farm_efficiency' or name
157158
# of another wind efficiency curve
158159
# see :py:func:`~.wake_losses.get_wind_efficiency_curve`
@@ -164,15 +165,14 @@ class that provides all necessary steps to calculate the power output of a
164165
"smoothing_order": "wind_farm_power_curves", #
165166
# 'wind_farm_power_curves' (default) or
166167
# 'turbine_power_curves'
167-
"wind_speed_model": "logarithmic", # 'logarithmic' (default),
168-
# 'hellman' or
169-
# 'interpolation_extrapolation'
170-
"density_model": "ideal_gas", # 'barometric' (default), 'ideal_gas' or
171-
# 'interpolation_extrapolation'
172-
"temperature_model": "linear_gradient", # 'linear_gradient' (def.) or
173-
# 'interpolation_extrapolation'
174-
"power_output_model": "power_curve", # 'power_curve' (default) or
175-
# 'power_coefficient_curve'
168+
"wind_speed_model": "logarithmic",
169+
# 'logarithmic' (default), 'hellman' or 'interpolation_extrapolation'
170+
"density_model": "ideal_gas", #
171+
# 'barometric' (default), 'ideal_gas' or 'interpolation_extrapolation'
172+
"temperature_model": "linear_gradient",
173+
# 'linear_gradient' (def.) or 'interpolation_extrapolation'
174+
"power_output_model": "power_curve",
175+
# 'power_curve' (default) or 'power_coefficient_curve'
176176
"density_correction": True, # False (default) or True
177177
"obstacle_height": 0, # default: 0
178178
"hellman_exp": None,

windpowerlib/wind_farm.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,9 +379,11 @@ def assign_power_curve(
379379
and kwargs["roughness_length"] is not None
380380
):
381381
# Calculate turbulence intensity and write to kwargs
382-
turbulence_intensity = tools.estimate_turbulence_intensity(
383-
row["wind_turbine"].hub_height,
384-
kwargs["roughness_length"],
382+
turbulence_intensity = (
383+
tools.estimate_turbulence_intensity(
384+
row["wind_turbine"].hub_height,
385+
kwargs["roughness_length"],
386+
)
385387
)
386388
kwargs["turbulence_intensity"] = turbulence_intensity
387389
else:
@@ -463,10 +465,12 @@ def assign_power_curve(
463465
)
464466
if wake_losses_model == "wind_farm_efficiency":
465467
if self.efficiency is not None:
466-
wind_farm_power_curve = power_curves.wake_losses_to_power_curve(
467-
wind_farm_power_curve["wind_speed"].values,
468-
wind_farm_power_curve["value"].values,
469-
wind_farm_efficiency=self.efficiency,
468+
wind_farm_power_curve = (
469+
power_curves.wake_losses_to_power_curve(
470+
wind_farm_power_curve["wind_speed"].values,
471+
wind_farm_power_curve["value"].values,
472+
wind_farm_efficiency=self.efficiency,
473+
)
470474
)
471475
else:
472476
msg = (

0 commit comments

Comments
 (0)