Skip to content

Commit 0fc4757

Browse files
Birgit SchachlerBirgit Schachler
authored andcommitted
Add jupyter notebook with basic example
1 parent 05d1082 commit 0fc4757

File tree

1 file changed

+307
-0
lines changed

1 file changed

+307
-0
lines changed

example/basic_example.ipynb

Lines changed: 307 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,307 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"This example shows you the basic usage of the windpowerlib. There are mainly three steps.\n",
8+
"First you need to define your wind turbine, then get your weather data and in the last step call the windpowerlib ModelChain to generate your feedin timeseries.\n",
9+
"Be careful with the units!"
10+
]
11+
},
12+
{
13+
"cell_type": "markdown",
14+
"metadata": {},
15+
"source": [
16+
"### Import necessary packages and modules"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": null,
22+
"metadata": {
23+
"collapsed": true
24+
},
25+
"outputs": [],
26+
"source": [
27+
"__copyright__ = \"Copyright oemof developer group\"\n",
28+
"__license__ = \"GPLv3\"\n",
29+
"\n",
30+
"import os\n",
31+
"import pandas as pd\n",
32+
"\n",
33+
"from windpowerlib import modelchain\n",
34+
"from windpowerlib import wind_turbine as wt"
35+
]
36+
},
37+
{
38+
"cell_type": "markdown",
39+
"metadata": {},
40+
"source": [
41+
"You can use the logging package to get logging messages from the windpowerlib. Change the logging level if you want more or less messages."
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": null,
47+
"metadata": {
48+
"collapsed": true
49+
},
50+
"outputs": [],
51+
"source": [
52+
"import logging\n",
53+
"logging.getLogger().setLevel(logging.DEBUG)"
54+
]
55+
},
56+
{
57+
"cell_type": "markdown",
58+
"metadata": {},
59+
"source": [
60+
"### Import weather data\n",
61+
"\n",
62+
"The function below imports the example weather data from the weather.csv file provided along with the windpowerlib. The data include wind speed at two different heights in m/s, air temperature in two different heights in K, surface roughness length in m and air pressure in Pa.\n",
63+
"\n",
64+
"To find out which weather data in which units need to be provided in order to use the ModelChain or other functions of the windpowerlib see the individual function documentation."
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": null,
70+
"metadata": {
71+
"collapsed": true
72+
},
73+
"outputs": [],
74+
"source": [
75+
"def read_weather_data(filename, datetime_column='Unnamed: 0',\n",
76+
" **kwargs):\n",
77+
" r\"\"\"\n",
78+
" Fetches weather data from a file.\n",
79+
"\n",
80+
" The files are located in the example folder of the windpowerlib.\n",
81+
"\n",
82+
" Parameters\n",
83+
" ----------\n",
84+
" filename : string\n",
85+
" Filename of the weather data file.\n",
86+
" datetime_column : string\n",
87+
" Name of the datetime column of the weather DataFrame.\n",
88+
"\n",
89+
" Other Parameters\n",
90+
" ----------------\n",
91+
" datapath : string, optional\n",
92+
" Path where the weather data file is stored.\n",
93+
" Default: 'windpowerlib/example'.\n",
94+
"\n",
95+
" Returns\n",
96+
" -------\n",
97+
" pandas.DataFrame\n",
98+
" Contains weather data time series.\n",
99+
"\n",
100+
" \"\"\"\n",
101+
" if 'datapath' not in kwargs:\n",
102+
" kwargs['datapath'] = os.path.join(os.path.split(\n",
103+
" os.path.dirname(__file__))[0], 'example')\n",
104+
"\n",
105+
" file = os.path.join(kwargs['datapath'], filename)\n",
106+
" df = pd.read_csv(file)\n",
107+
" return df.set_index(pd.to_datetime(df[datetime_column])).tz_localize(\n",
108+
" 'UTC').tz_convert('Europe/Berlin').drop(datetime_column, 1)\n",
109+
"\n",
110+
"\n",
111+
"# Read weather data from csv\n",
112+
"weather = read_weather_data('weather.csv')"
113+
]
114+
},
115+
{
116+
"cell_type": "markdown",
117+
"metadata": {},
118+
"source": [
119+
"Along with the weather data you have to provide a dataframe or dictionary specifying the height for which the data applies."
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": null,
125+
"metadata": {
126+
"collapsed": true
127+
},
128+
"outputs": [],
129+
"source": [
130+
"data_height = {\n",
131+
" 'pressure': 0,\n",
132+
" 'temp_air': 2,\n",
133+
" 'v_wind': 10,\n",
134+
" 'temp_air_2': 10,\n",
135+
" 'v_wind_2': 80}"
136+
]
137+
},
138+
{
139+
"cell_type": "markdown",
140+
"metadata": {},
141+
"source": [
142+
"### Initialise wind turbine"
143+
]
144+
},
145+
{
146+
"cell_type": "markdown",
147+
"metadata": {},
148+
"source": [
149+
"There are two ways to initialize a WindTurbine object. You can either specify your own turbine, as done below or use"
150+
]
151+
},
152+
{
153+
"cell_type": "code",
154+
"execution_count": null,
155+
"metadata": {
156+
"collapsed": true
157+
},
158+
"outputs": [],
159+
"source": [
160+
"wt.get_turbine_types()\n",
161+
"turbines[turbines[\"turbine_id\"].str.contains(\"VESTAS\")]"
162+
]
163+
},
164+
{
165+
"cell_type": "code",
166+
"execution_count": null,
167+
"metadata": {
168+
"collapsed": true
169+
},
170+
"outputs": [],
171+
"source": [
172+
"# Specifications of the wind turbines\n",
173+
"enerconE126 = {\n",
174+
" 'hub_height': 135,\n",
175+
" 'd_rotor': 127,\n",
176+
" 'fetch_curve': 'P', # 'P' vor p-curve and 'cp' for cp-curve\n",
177+
" 'turbine_name': 'ENERCON E 126 7500'} # Turbine name as in register. Use\n",
178+
" # wind_turbine.get_turbine_types()\n",
179+
"P_data = {0:\n",
180+
" 3:\n",
181+
" 5:\n",
182+
" 10:\n",
183+
" 15:\n",
184+
" 20:\n",
185+
" 25:}\n",
186+
"# for a full list.\n",
187+
"vestasV90 = {\n",
188+
" 'hub_height': 105,\n",
189+
" 'd_rotor': 90,\n",
190+
" 'fetch_curve': 'P',\n",
191+
" 'turbine_name': 'VESTAS V 90 3000'}\n",
192+
"\n",
193+
"# Initialize WindTurbine objects\n",
194+
"e126 = wt.WindTurbine(**enerconE126)\n",
195+
"v90 = wt.WindTurbine(**vestasV90)"
196+
]
197+
},
198+
{
199+
"cell_type": "markdown",
200+
"metadata": {},
201+
"source": [
202+
"### Use the ModelChain to generate feedin timeseries"
203+
]
204+
},
205+
{
206+
"cell_type": "code",
207+
"execution_count": null,
208+
"metadata": {
209+
"collapsed": true
210+
},
211+
"outputs": [],
212+
"source": [
213+
"# Specifications of the modelchain data\n",
214+
"modelchain_data = {\n",
215+
" 'obstacle_height': 0,\n",
216+
" 'wind_model': 'logarithmic', # 'logarithmic' or 'hellman'\n",
217+
" 'rho_model': 'ideal_gas', # 'barometric' or 'ideal_gas'\n",
218+
" 'power_output_model': 'p_values', # 'p_values' or 'cp_values'\n",
219+
" 'density_corr': False, # True or False\n",
220+
" 'hellman_exp': None} # Float or None\n",
221+
"\n",
222+
"# Calculate turbine power output\n",
223+
"mc_e126 = modelchain.ModelChain(e126, **modelchain_data).run_model(\n",
224+
" weather, coastDat2)\n",
225+
"e126.power_output = mc_e126.power_output\n",
226+
"mc_v90 = modelchain.ModelChain(v90, **modelchain_data).run_model(\n",
227+
" weather, coastDat2)\n",
228+
"v90.power_output = mc_v90.power_output\n",
229+
"\n",
230+
"\n"
231+
]
232+
},
233+
{
234+
"cell_type": "markdown",
235+
"metadata": {},
236+
"source": [
237+
"### Plot results"
238+
]
239+
},
240+
{
241+
"cell_type": "code",
242+
"execution_count": null,
243+
"metadata": {
244+
"collapsed": true
245+
},
246+
"outputs": [],
247+
"source": [
248+
"try:\n",
249+
" from matplotlib import pyplot as plt\n",
250+
"except ImportError:\n",
251+
" plt = None\n",
252+
"# Plot turbine power output\n",
253+
"if plt:\n",
254+
" e126.power_output.plot(legend=True, label='Enercon E126')\n",
255+
" v90.power_output.plot(legend=True, label='Vestas V90')\n",
256+
" plt.show()\n",
257+
"else:\n",
258+
" print(e126.power_output)\n",
259+
" print(v90.power_output)\n",
260+
"\n",
261+
"# Plot power (coefficient) curves\n",
262+
"if plt:\n",
263+
" if e126.cp_values is not None:\n",
264+
" e126.cp_values.plot(style='*', title='Enercon E126')\n",
265+
" plt.show()\n",
266+
" if e126.p_values is not None:\n",
267+
" e126.p_values.plot(style='*', title='Enercon E126')\n",
268+
" plt.show()\n",
269+
" if v90.cp_values is not None:\n",
270+
" v90.cp_values.plot(style='*', title='Vestas V90')\n",
271+
" plt.show()\n",
272+
" if v90.p_values is not None:\n",
273+
" v90.p_values.plot(style='*', title='Vestas V90')\n",
274+
" plt.show()\n",
275+
"else:\n",
276+
" if e126.cp_values is not None:\n",
277+
" print(\"The cp value at a wind speed of 5 m/s: {0}\".format(\n",
278+
" e126.cp_values.cp[5.0]))\n",
279+
" if e126.p_values is not None:\n",
280+
" print(\"The P value at a wind speed of 5 m/s: {0}\".format(\n",
281+
" e126.p_values.P[5.0]))\n",
282+
"logging.info('Done!')"
283+
]
284+
}
285+
],
286+
"metadata": {
287+
"kernelspec": {
288+
"display_name": "Python 3",
289+
"language": "python",
290+
"name": "python3"
291+
},
292+
"language_info": {
293+
"codemirror_mode": {
294+
"name": "ipython",
295+
"version": 3
296+
},
297+
"file_extension": ".py",
298+
"mimetype": "text/x-python",
299+
"name": "python",
300+
"nbconvert_exporter": "python",
301+
"pygments_lexer": "ipython3",
302+
"version": "3.4.2"
303+
}
304+
},
305+
"nbformat": 4,
306+
"nbformat_minor": 2
307+
}

0 commit comments

Comments
 (0)