diff --git a/docs/sphinx/source/whatsnew/v0.13.1.rst b/docs/sphinx/source/whatsnew/v0.13.1.rst index 2109b117d5..84dfaf0a9d 100644 --- a/docs/sphinx/source/whatsnew/v0.13.1.rst +++ b/docs/sphinx/source/whatsnew/v0.13.1.rst @@ -16,7 +16,8 @@ Deprecations Bug fixes ~~~~~~~~~ - +* Fix :py:func:`~pvlib.clearsky.detect_clearsky` to use inferred window length when + ``infer_limits=True``. (:issue:`2542`, :pull:`2550`) Enhancements ~~~~~~~~~~~~ @@ -74,3 +75,4 @@ Contributors * Rodrigo Amaro e Silva (:ghuser:`ramaroesilva`) * Kevin Anderson (:ghuser:`kandersolar`) * Mikaella Brewer (:ghuser:`brwerx`) +* Will Holmgren (:ghuser:`wholmgren`) diff --git a/pvlib/clearsky.py b/pvlib/clearsky.py index be75ecd47a..8cc867e020 100644 --- a/pvlib/clearsky.py +++ b/pvlib/clearsky.py @@ -818,22 +818,31 @@ def detect_clearsky(measured, clearsky, times=None, infer_limits=False, sample_interval, samples_per_window = \ tools._get_sample_intervals(times, window_length) - if samples_per_window < 3: - raise ValueError(f"Samples per window of {samples_per_window}" - " found. Each window must contain at least 3 data" - " points." - f" Window length of {window_length} found; increase" - f" window length to {3*sample_interval} or longer.") - # if infer_limits, find threshold values using the sample interval if infer_limits: - window_length, mean_diff, max_diff, lower_line_length, \ - upper_line_length, var_diff, slope_dev = \ - _clearsky_get_threshold(sample_interval) + ( + window_length, + mean_diff, + max_diff, + lower_line_length, + upper_line_length, + var_diff, + slope_dev, + ) = _clearsky_get_threshold(sample_interval) # recalculate samples_per_window using returned window_length - _, samples_per_window = \ - tools._get_sample_intervals(times, window_length) + sample_interval, samples_per_window = tools._get_sample_intervals( + times, window_length + ) + + if samples_per_window < 3: + raise ValueError( + f"Samples per window of {samples_per_window}" + " found. Each window must contain at least 3 data" + " points." + f" Window length of {window_length} found. Increase" + f" window length to {3 * sample_interval} or longer." + ) # check that we have enough data to produce a nonempty hankel matrix if len(times) < samples_per_window: diff --git a/tests/test_clearsky.py b/tests/test_clearsky.py index 0ef5dfeacd..296b96b345 100644 --- a/tests/test_clearsky.py +++ b/tests/test_clearsky.py @@ -688,6 +688,14 @@ def test_detect_clearsky_window_too_short(detect_clearsky_data): clearsky.detect_clearsky(expected['GHI'], cs['ghi'], window_length=2) +def test_detect_clearsky_infer_checks(detect_clearsky_threshold_data): + # GH 2542 + expected, cs = detect_clearsky_threshold_data + expected = expected.resample('10min').mean() + cs = cs.resample('10min').mean() + clearsky.detect_clearsky(expected['GHI'], cs['ghi'], infer_limits=True) + + @pytest.mark.parametrize("window_length", [5, 10, 15, 20, 25]) def test_detect_clearsky_optimizer_not_failed( detect_clearsky_data, window_length