Skip to content

Commit a1bb436

Browse files
Birgit SchachlerBirgit Schachler
authored andcommitted
Adapt examples
1 parent dae6634 commit a1bb436

File tree

3 files changed

+84
-73
lines changed

3 files changed

+84
-73
lines changed

example/basic_example.ipynb

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,23 @@
7171
{
7272
"cell_type": "code",
7373
"execution_count": null,
74-
"metadata": {
75-
"collapsed": true
76-
},
74+
"metadata": {},
7775
"outputs": [],
7876
"source": [
79-
"def read_weather_data(filename, datetime_column='time_index',\n",
80-
" **kwargs):\n",
77+
"def get_weather_data(filename='weather.csv', **kwargs):\n",
8178
" r\"\"\"\n",
82-
" Fetches weather data from a file.\n",
79+
" Imports weather data from a file.\n",
8380
"\n",
84-
" The files are located in the example folder of the windpowerlib.\n",
81+
" The data include wind speed at two different heights in m/s, air\n",
82+
" temperature in two different heights in K, surface roughness length in m\n",
83+
" and air pressure in Pa. The file is located in the example folder of the\n",
84+
" windpowerlib. The height in m for which the data applies is specified in\n",
85+
" the second row.\n",
8586
"\n",
8687
" Parameters\n",
8788
" ----------\n",
8889
" filename : string\n",
89-
" Filename of the weather data file.\n",
90-
" datetime_column : string\n",
91-
" Name of the datetime column of the weather DataFrame.\n",
90+
" Filename of the weather data file. Default: 'weather.csv'.\n",
9291
"\n",
9392
" Other Parameters\n",
9493
" ----------------\n",
@@ -98,46 +97,37 @@
9897
"\n",
9998
" Returns\n",
10099
" -------\n",
101-
" pandas.DataFrame\n",
102-
" Contains weather data time series.\n",
100+
" weather_df : pandas.DataFrame\n",
101+
" DataFrame with time series for wind speed `wind_speed` in m/s,\n",
102+
" temperature `temperature` in K, roughness length `roughness_length`\n",
103+
" in m, and pressure `pressure` in Pa.\n",
104+
" The columns of the DataFrame are a MultiIndex where the first level\n",
105+
" contains the variable name (e.g. wind_speed) and the second level\n",
106+
" contains the height at which it applies (e.g. 10, if it was\n",
107+
" measured at a height of 10 m).\n",
103108
"\n",
104109
" \"\"\"\n",
110+
"\n",
105111
" if 'datapath' not in kwargs:\n",
106112
" kwargs['datapath'] = os.path.join(os.path.split(\n",
107113
" os.path.dirname(__file__))[0], 'example')\n",
108-
"\n",
109114
" file = os.path.join(kwargs['datapath'], filename)\n",
110-
" df = pd.read_csv(file)\n",
111-
" return df.set_index(pd.to_datetime(df[datetime_column])).tz_localize(\n",
112-
" 'UTC').tz_convert('Europe/Berlin').drop(datetime_column, 1)\n",
115+
" # read csv file \n",
116+
" weather_df = pd.read_csv(file, index_col=0, header=[0, 1])\n",
117+
" # change type of index to datetime and set time zone\n",
118+
" weather_df.index = pd.to_datetime(weather_df.index).tz_localize(\n",
119+
" 'UTC').tz_convert('Europe/Berlin')\n",
120+
" # change type of height from str to int by resetting columns\n",
121+
" weather_df.columns = [weather_df.axes[1].levels[0][\n",
122+
" weather_df.axes[1].labels[0]],\n",
123+
" weather_df.axes[1].levels[1][\n",
124+
" weather_df.axes[1].labels[1]].astype(int)]\n",
125+
" return weather_df\n",
113126
"\n",
114127
"\n",
115128
"# Read weather data from csv\n",
116-
"weather = read_weather_data(filename='weather.csv', datapath='')"
117-
]
118-
},
119-
{
120-
"cell_type": "markdown",
121-
"metadata": {},
122-
"source": [
123-
"Along with the weather data you have to provide a dataframe or dictionary specifying the height for which the data applies."
124-
]
125-
},
126-
{
127-
"cell_type": "code",
128-
"execution_count": null,
129-
"metadata": {
130-
"collapsed": true
131-
},
132-
"outputs": [],
133-
"source": [
134-
"# height in meters\n",
135-
"data_height = {\n",
136-
" 'pressure': 0,\n",
137-
" 'temp_air': 2,\n",
138-
" 'v_wind': 10,\n",
139-
" 'temp_air_2': 10,\n",
140-
" 'v_wind_2': 80}"
129+
"weather = get_weather_data(filename='weather.csv', datapath='')\n",
130+
"print(weather[['wind_speed', 'temperature', 'pressure']][0:3])"
141131
]
142132
},
143133
{
@@ -180,9 +170,7 @@
180170
{
181171
"cell_type": "code",
182172
"execution_count": null,
183-
"metadata": {
184-
"collapsed": true
185-
},
173+
"metadata": {},
186174
"outputs": [],
187175
"source": [
188176
"# specification of own wind turbine (Note: power coefficient values and\n",
@@ -192,9 +180,9 @@
192180
" 'nominal_power': 3e6, # in W\n",
193181
" 'hub_height': 105, # in m\n",
194182
" 'rotor_diameter': 90, # in m\n",
195-
" 'p_values': pd.DataFrame(\n",
196-
" data={'p': [p * 1000 for p in\n",
197-
" [0.0, 26.0, 180.0, 1500.0, 3000.0, 3000.0]]}, # in W\n",
183+
" 'p_values': pd.Series(\n",
184+
" data=[p * 1000 for p in [\n",
185+
" 0.0, 26.0, 180.0, 1500.0, 3000.0, 3000.0]], # in W\n",
198186
" index=[0.0, 3.0, 5.0, 10.0, 15.0, 25.0]) # in m/s\n",
199187
" } \n",
200188
"# initialise WindTurbine object\n",
@@ -239,8 +227,7 @@
239227
"# power output calculation for my_turbine\n",
240228
"# initialise ModelChain with default parameters and use run_model\n",
241229
"# method to calculate power output\n",
242-
"mc_my_turbine = modelchain.ModelChain(my_turbine).run_model(\n",
243-
" weather, data_height)\n",
230+
"mc_my_turbine = modelchain.ModelChain(my_turbine).run_model(weather)\n",
244231
"# write power output timeseries to WindTurbine object\n",
245232
"my_turbine.power_output = mc_my_turbine.power_output"
246233
]
@@ -254,16 +241,22 @@
254241
"# power output calculation for e126\n",
255242
"# own specifications for ModelChain setup\n",
256243
"modelchain_data = {\n",
257-
" 'obstacle_height': 0, # default: 0\n",
258-
" 'wind_model': 'logarithmic', # 'logarithmic' (default) or 'hellman'\n",
259-
" 'rho_model': 'ideal_gas', # 'barometric' (default) or 'ideal_gas'\n",
260-
" 'power_output_model': 'p_values', # 'p_values' (default) or 'cp_values'\n",
261-
" 'density_corr': True, # False (default) or True\n",
262-
" 'hellman_exp': None} # None (default) or None\n",
244+
" 'obstacle_height': 0, # default: 0\n",
245+
" 'wind_speed_model': 'logarithmic', # 'logarithmic' (default),\n",
246+
" # 'hellman' or\n",
247+
" # 'interpolation_extrapolation'\n",
248+
" 'density_model': 'ideal_gas', # 'barometric' (default) or 'ideal_gas'\n",
249+
" 'temperature_model': 'linear_gradient', # 'linear_gradient' (def.) or\n",
250+
" # 'interpolation_extrapolation'\n",
251+
" 'power_output_model': 'power_curve', # 'power_curve' (default) or\n",
252+
" # 'power_coefficient_curve'\n",
253+
" 'density_correction': True, # False (default) or True\n",
254+
" 'hellman_exp': None} # None (default) or None\n",
255+
"\n",
263256
"# initialise ModelChain with own specifications and use run_model method to\n",
264257
"# calculate power output\n",
265258
"mc_e126 = modelchain.ModelChain(e126, **modelchain_data).run_model(\n",
266-
" weather, data_height)\n",
259+
" weather)\n",
267260
"# write power output timeseries to WindTurbine object\n",
268261
"e126.power_output = mc_e126.power_output"
269262
]
@@ -328,6 +321,15 @@
328321
" my_turbine.p_values.plot(style='*', title='myTurbine')\n",
329322
" plt.show()"
330323
]
324+
},
325+
{
326+
"cell_type": "code",
327+
"execution_count": null,
328+
"metadata": {
329+
"collapsed": true
330+
},
331+
"outputs": [],
332+
"source": []
331333
}
332334
],
333335
"metadata": {
@@ -339,7 +341,7 @@
339341
"language_info": {
340342
"codemirror_mode": {
341343
"name": "ipython",
342-
"version": 3.0
344+
"version": 3
343345
},
344346
"file_extension": ".py",
345347
"mimetype": "text/x-python",
@@ -350,5 +352,5 @@
350352
}
351353
},
352354
"nbformat": 4,
353-
"nbformat_minor": 0
354-
}
355+
"nbformat_minor": 1
356+
}

example/basic_example.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ def get_weather_data(filename='weather.csv', **kwargs):
5252
temperature `temperature` in K, roughness length `roughness_length`
5353
in m, and pressure `pressure` in Pa.
5454
The columns of the DataFrame are a MultiIndex where the first level
55-
contains the variable name (e.g. wind_speed) and the second level
56-
contains the height at which it applies (e.g. 10, if it was
57-
measured at a height of 10 m).
55+
contains the variable name as string (e.g. 'wind_speed') and the
56+
second level contains the height as integer at which it applies
57+
(e.g. 10, if it was measured at a height of 10 m).
5858
5959
"""
6060

@@ -67,6 +67,11 @@ def get_weather_data(filename='weather.csv', **kwargs):
6767
# change type of index to datetime and set time zone
6868
weather_df.index = pd.to_datetime(weather_df.index).tz_localize(
6969
'UTC').tz_convert('Europe/Berlin')
70+
# change type of height from str to int by resetting columns
71+
weather_df.columns = [weather_df.axes[1].levels[0][
72+
weather_df.axes[1].labels[0]],
73+
weather_df.axes[1].levels[1][
74+
weather_df.axes[1].labels[1]].astype(int)]
7075
return weather_df
7176

7277

@@ -96,9 +101,9 @@ def initialise_wind_turbines():
96101
'nominal_power': 3e6, # in W
97102
'hub_height': 105, # in m
98103
'rotor_diameter': 90, # in m
99-
'p_values': pd.DataFrame(
100-
data={'p': [p * 1000 for p in
101-
[0.0, 26.0, 180.0, 1500.0, 3000.0, 3000.0]]}, # in W
104+
'p_values': pd.Series(
105+
data=[p * 1000 for p in [
106+
0.0, 26.0, 180.0, 1500.0, 3000.0, 3000.0]], # in W
102107
index=[0.0, 3.0, 5.0, 10.0, 15.0, 25.0]) # in m/s
103108
}
104109
# initialise WindTurbine object
@@ -115,7 +120,7 @@ def initialise_wind_turbines():
115120
# initialise WindTurbine object
116121
e126 = wt.WindTurbine(**enerconE126)
117122

118-
return (my_turbine, e126)
123+
return my_turbine, e126
119124

120125

121126
def calculate_power_output(weather, my_turbine, e126):
@@ -151,11 +156,15 @@ def calculate_power_output(weather, my_turbine, e126):
151156
# own specifications for ModelChain setup
152157
modelchain_data = {
153158
'obstacle_height': 0, # default: 0
154-
'wind_model': 'logarithmic', # 'logarithmic' (default) or 'hellman'
155-
'rho_model': 'ideal_gas', # 'barometric' (default) or 'ideal_gas'
156-
'power_output_model': 'p_values', # 'p_values' (default) or
157-
# 'cp_values'
158-
'density_corr': True, # False (default) or True
159+
'wind_speed_model': 'logarithmic', # 'logarithmic' (default),
160+
# 'hellman' or
161+
# 'interpolation_extrapolation'
162+
'density_model': 'ideal_gas', # 'barometric' (default) or 'ideal_gas'
163+
'temperature_model': 'linear_gradient', # 'linear_gradient' (def.) or
164+
# 'interpolation_extrapolation'
165+
'power_output_model': 'power_curve', # 'power_curve' (default) or
166+
# 'power_coefficient_curve'
167+
'density_correction': True, # False (default) or True
159168
'hellman_exp': None} # None (default) or None
160169
# initialise ModelChain with own specifications and use run_model method
161170
# to calculate power output

example/weather.csv

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
time_index,pressure,temperature,wind_speed,roughness_length,temperature,wind_speed
2-
,0,2,10,0,10,80
1+
variable_name,pressure,temperature,wind_speed,roughness_length,temperature,wind_speed
2+
height,0,2,10,0,10,80
33
2010-01-01 00:00:00+01:00,98405.7,267.6,5.32697,0.15,267.57,7.80697
44
2010-01-01 01:00:00+01:00,98382.7,267.6,5.46199,0.15,267.55,7.86199
55
2010-01-01 02:00:00+01:00,98362.9,267.61,5.67899,0.15,267.54,8.59899

0 commit comments

Comments
 (0)