1212import logging
1313import sys
1414import requests
15+ import os
1516
1617
1718class WindTurbine (object ):
@@ -264,10 +265,17 @@ def isfloat(x):
264265 return df , nominal_power
265266
266267
267- def get_turbine_data_from_oedb (turbine_type , fetch_curve ):
268+ def get_turbine_data_from_oedb (turbine_type , fetch_curve , overwrite = False ):
268269 r"""
269270 Fetches data for one wind turbine type from the OpenEnergy Database (oedb).
270271
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+
271279 Parameters
272280 ----------
273281 turbine_type : string
@@ -278,6 +286,9 @@ def get_turbine_data_from_oedb(turbine_type, fetch_curve):
278286 Parameter to specify whether a power or power coefficient curve
279287 should be retrieved from the provided turbine data. Valid options are
280288 '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.
281292
282293 Returns
283294 -------
@@ -288,8 +299,15 @@ def get_turbine_data_from_oedb(turbine_type, fetch_curve):
288299 power curve values in W with the corresponding wind speeds in m/s.
289300
290301 """
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 ()
293311 turbine_data .set_index ('turbine_type' , inplace = True )
294312 # Set `curve` depending on `fetch_curve` to match names in oedb
295313 curve = ('cp_curve' if fetch_curve == 'power_coefficient_curve'
@@ -320,6 +338,8 @@ def load_turbine_data_from_oedb():
320338 r"""
321339 Loads turbine data from the OpenEnergy Database (oedb).
322340
341+ Turbine data is saved to `filename` for offline usage of windpowerlib.
342+
323343 Returns
324344 -------
325345 turbine_data : pd.DataFrame
@@ -341,6 +361,13 @@ def load_turbine_data_from_oedb():
341361 "Response: [{}]" .format (result .status_code ))
342362 # extract data to data frame
343363 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 ))
344371 return turbine_data
345372
346373
0 commit comments