Skip to content

Commit 1c0c3e7

Browse files
cedricleroywholmgren
authored andcommitted
lookup_linke_turbidity speed improvement, mat to h5 (#442)
lookup_linke_turbidity speed improvement, mat to h5
1 parent 923dc0e commit 1c0c3e7

File tree

12 files changed

+47
-29
lines changed

12 files changed

+47
-29
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ install:
2828
- cmd: conda info -a
2929

3030
# install depenencies
31-
- cmd: conda create -n test_env --yes --quiet python=%PYTHON_VERSION% pip numpy scipy pandas nose pytest pytz ephem numba siphon -c conda-forge
31+
- cmd: conda create -n test_env --yes --quiet python=%PYTHON_VERSION% pip numpy scipy pytables pandas nose pytest pytz ephem numba siphon -c conda-forge
3232
- cmd: activate test_env
3333
- cmd: python --version
3434
- cmd: conda list

ci/requirements-py27.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dependencies:
66
- python=2.7
77
- numpy
88
- scipy
9+
- pytables
910
- pandas
1011
- pytz
1112
- ephem

ci/requirements-py34.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dependencies:
66
- python=3.4
77
- numpy
88
- scipy
9+
- pytables
910
- pandas
1011
- pytz
1112
- ephem

ci/requirements-py35.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dependencies:
66
- python=3.5
77
- numpy
88
- scipy
9+
- pytables
910
- pandas
1011
- pytz
1112
- ephem

ci/requirements-py36.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ dependencies:
66
- python=3.6
77
- numpy
88
- scipy
9+
- pytables
910
- pandas
1011
- pytz
1112
#- ephem

docs/environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ dependencies:
77
- mock # needed for local python 2.7 builds
88
- numpy=1.11.2
99
- scipy
10+
- tables
1011
- pandas=0.19.1
1112
- pytz
1213
- ephem

docs/sphinx/source/clearsky.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,24 +136,24 @@ the year. You could run it in a loop to create plots for all months.
136136

137137
In [1]: import os
138138

139-
In [1]: import scipy.io
139+
In [1]: import tables
140140

141141
In [1]: pvlib_path = os.path.dirname(os.path.abspath(pvlib.clearsky.__file__))
142142

143-
In [1]: filepath = os.path.join(pvlib_path, 'data', 'LinkeTurbidities.mat')
144-
145-
In [1]: mat = scipy.io.loadmat(filepath)
143+
In [1]: filepath = os.path.join(pvlib_path, 'data', 'LinkeTurbidities.h5')
146144

147145
# data is in units of 20 x turbidity
148-
In [1]: linke_turbidity_table = mat['LinkeTurbidity'] # / 20. # crashes on rtd
146+
In [1]: lt_h5_file = tables.open_file(filepath)
149147

150148
In [1]: def plot_turbidity_map(month, vmin=1, vmax=100):
151149
...: plt.figure();
152-
...: plt.imshow(linke_turbidity_table[:, :, month-1], vmin=vmin, vmax=vmax);
150+
...: plt.imshow(lt_h5_file.root.LinkeTurbidity[:, :, month-1], vmin=vmin, vmax=vmax);
153151
...: plt.title('Linke turbidity x 20, ' + calendar.month_name[month]);
154152
...: plt.colorbar(shrink=0.5);
155153
...: plt.tight_layout();
156154

155+
In [1]: lt_h5_file.close()
156+
157157
@savefig turbidity-1.png width=10in
158158
In [1]: plot_turbidity_map(1)
159159

docs/sphinx/source/whatsnew/v0.5.2.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ API Changes
99

1010
Enhancements
1111
~~~~~~~~~~~~
12-
*
12+
* Improve clearsky.lookup_linke_turbidity speed, changing .mat source
13+
file to .h5 (:issue:`437`)
1314

1415
Bug fixes
1516
~~~~~~~~~
@@ -42,5 +43,4 @@ Contributors
4243
* Will Holmgren
4344
* KonstantinTr
4445
* Anton Driesse
45-
46-
46+
* Cedric Leroy

pvlib/clearsky.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def ineichen(apparent_zenith, airmass_absolute, linke_turbidity,
153153
def lookup_linke_turbidity(time, latitude, longitude, filepath=None,
154154
interp_turbidity=True):
155155
"""
156-
Look up the Linke Turibidity from the ``LinkeTurbidities.mat``
156+
Look up the Linke Turibidity from the ``LinkeTurbidities.h5``
157157
data file supplied with pvlib.
158158
159159
Parameters
@@ -165,18 +165,18 @@ def lookup_linke_turbidity(time, latitude, longitude, filepath=None,
165165
longitude : float
166166
167167
filepath : None or string, default None
168-
The path to the ``.mat`` file.
168+
The path to the ``.h5`` file.
169169
170170
interp_turbidity : bool, default True
171171
If ``True``, interpolates the monthly Linke turbidity values
172-
found in ``LinkeTurbidities.mat`` to daily values.
172+
found in ``LinkeTurbidities.h5`` to daily values.
173173
174174
Returns
175175
-------
176176
turbidity : Series
177177
"""
178178

179-
# The .mat file 'LinkeTurbidities.mat' contains a single 2160 x 4320 x 12
179+
# The .h5 file 'LinkeTurbidities.h5' contains a single 2160 x 4320 x 12
180180
# matrix of type uint8 called 'LinkeTurbidity'. The rows represent global
181181
# latitudes from 90 to -90 degrees; the columns represent global longitudes
182182
# from -180 to 180; and the depth (third dimension) represents months of
@@ -194,18 +194,15 @@ def lookup_linke_turbidity(time, latitude, longitude, filepath=None,
194194
# 1st column: 179.9583 W, 2nd column: 179.875 W
195195

196196
try:
197-
import scipy.io
197+
import tables
198198
except ImportError:
199-
raise ImportError('The Linke turbidity lookup table requires scipy. '
199+
raise ImportError('The Linke turbidity lookup table requires tables. '
200200
'You can still use clearsky.ineichen if you '
201201
'supply your own turbidities.')
202202

203203
if filepath is None:
204204
pvlib_path = os.path.dirname(os.path.abspath(__file__))
205-
filepath = os.path.join(pvlib_path, 'data', 'LinkeTurbidities.mat')
206-
207-
mat = scipy.io.loadmat(filepath)
208-
linke_turbidity_table = mat['LinkeTurbidity']
205+
filepath = os.path.join(pvlib_path, 'data', 'LinkeTurbidities.h5')
209206

210207
latitude_index = (
211208
np.around(_linearly_scale(latitude, 90, -90, 0, 2160))
@@ -214,7 +211,14 @@ def lookup_linke_turbidity(time, latitude, longitude, filepath=None,
214211
np.around(_linearly_scale(longitude, -180, 180, 0, 4320))
215212
.astype(np.int64))
216213

217-
lts = linke_turbidity_table[latitude_index][longitude_index]
214+
lt_h5_file = tables.open_file(filepath)
215+
try:
216+
lts = lt_h5_file.root.LinkeTurbidity[latitude_index, longitude_index, :]
217+
except IndexError:
218+
raise IndexError('Latitude should be between 90 and -90, '
219+
'longitude between -180 and 180.')
220+
finally:
221+
lt_h5_file.close()
218222

219223
if interp_turbidity:
220224
linke_turbidity = _interpolate_turbidity(lts, time)
Binary file not shown.

pvlib/test/conftest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@
1919
requires_scipy = pytest.mark.skipif(not has_scipy, reason='requires scipy')
2020

2121

22+
try:
23+
import tables
24+
has_tables = True
25+
except ImportError:
26+
has_tables = False
27+
28+
requires_tables = pytest.mark.skipif(not has_tables, reason='requires tables')
29+
30+
2231
try:
2332
import ephem
2433
has_ephem = True

pvlib/test/test_clearsky.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from pvlib import atmosphere
1717
from pvlib import irradiance
1818

19-
from conftest import requires_scipy
19+
from conftest import requires_scipy, requires_tables
2020

2121

2222
def test_ineichen_series():
@@ -158,7 +158,7 @@ def test_ineichen_altitude():
158158
assert_frame_equal(expected, out)
159159

160160

161-
@requires_scipy
161+
@requires_tables
162162
def test_lookup_linke_turbidity():
163163
times = pd.date_range(start='2014-06-24', end='2014-06-25',
164164
freq='12h', tz='America/Phoenix')
@@ -171,7 +171,7 @@ def test_lookup_linke_turbidity():
171171
assert_series_equal(expected, out)
172172

173173

174-
@requires_scipy
174+
@requires_tables
175175
def test_lookup_linke_turbidity_leapyear():
176176
times = pd.date_range(start='2016-06-24', end='2016-06-25',
177177
freq='12h', tz='America/Phoenix')
@@ -184,7 +184,7 @@ def test_lookup_linke_turbidity_leapyear():
184184
assert_series_equal(expected, out)
185185

186186

187-
@requires_scipy
187+
@requires_tables
188188
def test_lookup_linke_turbidity_nointerp():
189189
times = pd.date_range(start='2014-06-24', end='2014-06-25',
190190
freq='12h', tz='America/Phoenix')
@@ -195,7 +195,7 @@ def test_lookup_linke_turbidity_nointerp():
195195
assert_series_equal(expected, out)
196196

197197

198-
@requires_scipy
198+
@requires_tables
199199
def test_lookup_linke_turbidity_months():
200200
times = pd.date_range(start='2014-04-01', end='2014-07-01',
201201
freq='1M', tz='America/Phoenix')
@@ -206,7 +206,7 @@ def test_lookup_linke_turbidity_months():
206206
assert_series_equal(expected, out)
207207

208208

209-
@requires_scipy
209+
@requires_tables
210210
def test_lookup_linke_turbidity_months_leapyear():
211211
times = pd.date_range(start='2016-04-01', end='2016-07-01',
212212
freq='1M', tz='America/Phoenix')
@@ -217,7 +217,7 @@ def test_lookup_linke_turbidity_months_leapyear():
217217
assert_series_equal(expected, out)
218218

219219

220-
@requires_scipy
220+
@requires_tables
221221
def test_lookup_linke_turbidity_nointerp_months():
222222
times = pd.date_range(start='2014-04-10', end='2014-07-10',
223223
freq='1M', tz='America/Phoenix')
@@ -448,7 +448,7 @@ def test_simplified_solis_nans_series():
448448
assert_frame_equal(expected, out)
449449

450450

451-
@requires_scipy
451+
@requires_tables
452452
def test_linke_turbidity_corners():
453453
"""Test Linke turbidity corners out of bounds."""
454454
months = pd.DatetimeIndex('%d/1/2016' % (m + 1) for m in range(12))

0 commit comments

Comments
 (0)