12
12
import logging
13
13
import sys
14
14
import requests
15
+ import os
15
16
16
17
17
18
class WindTurbine (object ):
@@ -264,10 +265,17 @@ def isfloat(x):
264
265
return df , nominal_power
265
266
266
267
267
- def get_turbine_data_from_oedb (turbine_type , fetch_curve ):
268
+ def get_turbine_data_from_oedb (turbine_type , fetch_curve , overwrite = False ):
268
269
r"""
269
270
Fetches data for one wind turbine type from the OpenEnergy Database (oedb).
270
271
272
+ If turbine data exists in local repository it is loaded from this file. The
273
+ file is created when turbine data was loaded from oedb in
274
+ :py:func:`~.load_turbine_data_from_oedb`. Use this function with
275
+ `overwrite=True` to overwrite your file with newly fetched data.
276
+ Use :py:func:`~.check_local_turbine_data` to check
277
+ weather your local file is up to date.
278
+
271
279
Parameters
272
280
----------
273
281
turbine_type : string
@@ -278,6 +286,9 @@ def get_turbine_data_from_oedb(turbine_type, fetch_curve):
278
286
Parameter to specify whether a power or power coefficient curve
279
287
should be retrieved from the provided turbine data. Valid options are
280
288
'power_curve' and 'power_coefficient_curve'. Default: None.
289
+ overwrite : boolean
290
+ If True local file is overwritten by newly fetch data from oedb, if
291
+ False turbine data is fetched from previously saved file.
281
292
282
293
Returns
283
294
-------
@@ -288,8 +299,15 @@ def get_turbine_data_from_oedb(turbine_type, fetch_curve):
288
299
power curve values in W with the corresponding wind speeds in m/s.
289
300
290
301
"""
291
- # Extract data
292
- turbine_data = load_turbine_data_from_oedb ()
302
+ # hdf5 filename
303
+ filename = os .path .join (os .path .dirname (__file__ ), 'data' ,
304
+ 'turbine_data_oedb.h5' )
305
+ if os .path .isfile (filename ) and not overwrite :
306
+ logging .debug ("Turbine data is fetched from {}" .format (filename ))
307
+ with pd .HDFStore (filename ) as hdf_store :
308
+ turbine_data = hdf_store .get ('turbine_data' )
309
+ else :
310
+ turbine_data = load_turbine_data_from_oedb ()
293
311
turbine_data .set_index ('turbine_type' , inplace = True )
294
312
# Set `curve` depending on `fetch_curve` to match names in oedb
295
313
curve = ('cp_curve' if fetch_curve == 'power_coefficient_curve'
@@ -320,6 +338,8 @@ def load_turbine_data_from_oedb():
320
338
r"""
321
339
Loads turbine data from the OpenEnergy Database (oedb).
322
340
341
+ Turbine data is saved to `filename` for offline usage of windpowerlib.
342
+
323
343
Returns
324
344
-------
325
345
turbine_data : pd.DataFrame
@@ -341,6 +361,13 @@ def load_turbine_data_from_oedb():
341
361
"Response: [{}]" .format (result .status_code ))
342
362
# extract data to data frame
343
363
turbine_data = pd .DataFrame (result .json ())
364
+ # store data as hdf5
365
+ filename = os .path .join (os .path .dirname (__file__ ), 'data' ,
366
+ 'turbine_data_oedb.h5' )
367
+ with pd .HDFStore (filename ) as hdf_store :
368
+ hdf_store .put ('turbine_data' , turbine_data )
369
+ logging .debug ("Turbine data is fetched from oedb and saved "
370
+ "to {}" .format (filename ))
344
371
return turbine_data
345
372
346
373
0 commit comments