Skip to content

Commit cc0aefe

Browse files
committed
bugfix for high frequency time series in scaling.py, regarding issue 1257
add pull request number
1 parent 699f591 commit cc0aefe

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ Bug fixes
175175
* Corrected an error affecting :py:func:`~pvlib.clearsky.detect_clearsky`
176176
when data time step is not one minute. Error was introduced in v0.8.1.
177177
(:issue:`1241`, :pull:`1242`)
178+
* Corrected error affecting :py:func:`~pvlib.scaling._compute_wavelet` when
179+
passing a pandas time series with a sampling rate faster than 1 second.
180+
(:issue:`1257`, :pull:`1258`)
178181

179182
Testing
180183
~~~~~~~

pvlib/scaling.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ def _compute_wavelet(clearsky_index, dt=None):
205205
raise ValueError("dt must be specified for numpy type inputs.")
206206
else: # flatten() succeeded, thus it's a pandas type, so get its dt
207207
try: # Assume it's a time series type index
208-
dt = (clearsky_index.index[1] - clearsky_index.index[0]).seconds
208+
dt = clearsky_index.index[1] - clearsky_index.index[0]
209+
dt = dt.seconds + dt.microseconds/1e6
209210
except AttributeError: # It must just be a numeric index
210211
dt = (clearsky_index.index[1] - clearsky_index.index[0])
211212

@@ -221,7 +222,7 @@ def _compute_wavelet(clearsky_index, dt=None):
221222
csi_mean = np.zeros([max_tmscale, len(cs_long)])
222223
# Skip averaging for the 0th scale
223224
csi_mean[0, :] = cs_long.values.flatten()
224-
tmscales[0] = 1
225+
tmscales[0] = dt
225226
# Loop for all time scales we will consider
226227
for i in np.arange(1, max_tmscale):
227228
tmscales[i] = 2**i * dt # Wavelet integration time scale

pvlib/tests/test_scaling.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ def time(clear_sky_index):
3737
return np.arange(0, len(clear_sky_index))
3838

3939

40+
@pytest.fixture
41+
def time_60s(clear_sky_index):
42+
# Sample time vector 60s resolution
43+
return np.arange(0, len(clear_sky_index))*60
44+
45+
46+
@pytest.fixture
47+
def time_500ms(clear_sky_index):
48+
# Sample time vector 0.5s resolution
49+
return np.arange(0, len(clear_sky_index))*0.5
50+
51+
4052
@pytest.fixture
4153
def positions():
4254
# Sample positions based on the previous lat/lon (calculated manually)
@@ -51,6 +63,18 @@ def expect_tmscale():
5163
return [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]
5264

5365

66+
@pytest.fixture
67+
def expect_tmscale_1min():
68+
# Expected timescales for dt = 60
69+
return [60, 120, 240, 480, 960, 1920, 3840]
70+
71+
72+
@pytest.fixture
73+
def expect_tmscale_500ms():
74+
# Expected timescales for dt = 0.5
75+
return [0.5, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]
76+
77+
5478
@pytest.fixture
5579
def expect_wavelet():
5680
# Expected wavelet for indices 5000:5004 for clear_sky_index above (Matlab)
@@ -109,6 +133,25 @@ def test_compute_wavelet_series_numindex(clear_sky_index, time,
109133
assert_almost_equal(wavelet[:, 5000:5005], expect_wavelet)
110134

111135

136+
def test_compute_wavelet_series_highres(clear_sky_index, time_500ms,
137+
expect_tmscale_500ms, expect_wavelet):
138+
dtindex = pd.to_datetime(time_500ms, unit='s')
139+
csi_series = pd.Series(clear_sky_index, index=dtindex)
140+
wavelet, tmscale = scaling._compute_wavelet(csi_series)
141+
assert_almost_equal(tmscale, expect_tmscale_500ms)
142+
assert_almost_equal(wavelet[:, 5000:5005].shape, (14, 5))
143+
144+
145+
def test_compute_wavelet_series_minuteres(clear_sky_index, time_60s,
146+
expect_tmscale_1min, expect_wavelet):
147+
dtindex = pd.to_datetime(time_60s, unit='s')
148+
csi_series = pd.Series(clear_sky_index, index=dtindex)
149+
wavelet, tmscale = scaling._compute_wavelet(csi_series)
150+
assert_almost_equal(tmscale, expect_tmscale_1min)
151+
assert_almost_equal(wavelet[:, 5000:5005].shape,
152+
expect_wavelet[0:len(tmscale), :].shape)
153+
154+
112155
def test_compute_wavelet_array(clear_sky_index,
113156
expect_tmscale, expect_wavelet):
114157
wavelet, tmscale = scaling._compute_wavelet(clear_sky_index, dt)

0 commit comments

Comments
 (0)