Skip to content

Commit 8fe6a43

Browse files
authored
Correct slope calculation in clearsky.detect_clearsky (#1242)
* correct slope calculation, add test with time interval 2 minutes * remove unneeded test adjustment * add note to whatsnew * avoid diff twice
1 parent 7c0d704 commit 8fe6a43

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ Bug fixes
172172
calling :py:func:`~pvlib.pvsystem.calcparams_cec`. (:issue:`1215`, :pull:`1216`)
173173
* Corrected methodology error in :py:func:`~pvlib.scaling.wvm`. Tracks with
174174
fix in PVLib for MATLAB. (:issue:`1206`, :pull:`1213`)
175+
* Corrected an error affecting :py:func:`~pvlib.clearsky.detect_clearsky`
176+
when data time step is not one minute. Error was introduced in v0.8.1.
177+
(:issue:`1241`, :pull:`1242`)
175178

176179
Testing
177180
~~~~~~~

pvlib/clearsky.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,17 +652,18 @@ def _calc_stats(data, samples_per_window, sample_interval, H):
652652
# shift to get forward difference, .diff() is backward difference instead
653653
data_diff = data.diff().shift(-1)
654654
data_slope = data_diff / sample_interval
655-
data_slope_nstd = _slope_nstd_windowed(data, H, samples_per_window)
655+
data_slope_nstd = _slope_nstd_windowed(data_slope.values[:-1], data, H,
656+
samples_per_window, sample_interval)
656657
data_slope_nstd = data_slope_nstd
657658

658659
return data_mean, data_max, data_slope_nstd, data_slope
659660

660661

661-
def _slope_nstd_windowed(data, H, samples_per_window):
662+
def _slope_nstd_windowed(slopes, data, H, samples_per_window, sample_interval):
662663
with np.errstate(divide='ignore', invalid='ignore'):
663-
raw = np.diff(data)
664-
raw = raw[H[:-1, ]].std(ddof=1, axis=0) / data.values[H].mean(axis=0)
665-
return _to_centered_series(raw, data.index, samples_per_window)
664+
nstd = slopes[H[:-1, ]].std(ddof=1, axis=0) \
665+
/ data.values[H].mean(axis=0)
666+
return _to_centered_series(nstd, data.index, samples_per_window)
666667

667668

668669
def _max_diff_windowed(data, H, samples_per_window):

pvlib/tests/test_clearsky.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,17 @@ def test_detect_clearsky_window(detect_clearsky_data):
607607
check_dtype=False, check_names=False)
608608

609609

610+
def test_detect_clearsky_time_interval(detect_clearsky_data):
611+
expected, cs = detect_clearsky_data
612+
u = np.arange(0, len(cs), 2)
613+
cs2 = cs.iloc[u]
614+
expected2 = expected.iloc[u]
615+
clear_samples = clearsky.detect_clearsky(
616+
expected2['GHI'], cs2['ghi'], window_length=6)
617+
assert_series_equal(expected2['Clear or not'], clear_samples,
618+
check_dtype=False, check_names=False)
619+
620+
610621
def test_detect_clearsky_arrays(detect_clearsky_data):
611622
expected, cs = detect_clearsky_data
612623
clear_samples = clearsky.detect_clearsky(

0 commit comments

Comments
 (0)