Skip to content

Commit 9d6e95d

Browse files
authored
Merge pull request #45 from wind-python/make_windpowerlib_work_offline
Make windpowerlib work offline to gain independence from OEP in case of down times.
2 parents c8e21dc + 5ee908d commit 9d6e95d

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,6 @@ ENV/
9696

9797
# tests
9898
.pytest_cache
99+
100+
# oedb data dump
101+
/windpowerlib/data/turbine_data_oedb.h5

doc/whatsnew/v0-1-2.txt

+3-22
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,13 @@
11
v0.1.2 ()
22
++++++++++++++++++++++++++++++
33

4-
5-
New functions
6-
#############
7-
8-
9-
10-
Testing
11-
#######
12-
13-
14-
15-
Documentation
16-
#############
17-
18-
19-
20-
API changes
21-
###########
22-
23-
24-
254
Other changes
265
#############
6+
* Make windpowerlib work offline: turbine data from oedb is stored in a hdf5 file for offline usage
277
* Make :py:func:`~windpowerlib.wind_turbine.get_turbine_types` also accessible via `get_turbine_types()` --> from windpowerlib import get_turbine_types
288

299

3010
Contributors
3111
############
32-
* Sabine Haas
12+
* Sabine Haas
13+

setup.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ def read(fname):
1818
'windpowerlib': [os.path.join('data', '*.csv')]},
1919
long_description=read('README.rst'),
2020
zip_safe=False,
21-
install_requires=['pandas >= 0.19.1',
22-
'requests'])
21+
install_requires=['numpy <= 1.15.4', # remove after PyTables release 3.4.5
22+
'pandas >= 0.19.1',
23+
'requests',
24+
'tables']) # PyTables needed for pandas.HDFStore

windpowerlib/wind_turbine.py

+30-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import logging
1313
import sys
1414
import requests
15+
import os
1516

1617

1718
class 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

Comments
 (0)