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.

0 commit comments

Comments
 (0)