Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/sphinx/source/whatsnew/v0.13.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
~~~~~~~~~~~~
Expand Down Expand Up @@ -74,3 +75,4 @@ Contributors
* Rodrigo Amaro e Silva (:ghuser:`ramaroesilva`)
* Kevin Anderson (:ghuser:`kandersolar`)
* Mikaella Brewer (:ghuser:`brwerx`)
* Will Holmgren (:ghuser:`wholmgren`)
33 changes: 21 additions & 12 deletions pvlib/clearsky.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flake8 complains about this semi colon despite it being within a string. notably ruff format does not complain about it. In any case I changed it to a period.

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:
Expand Down
8 changes: 8 additions & 0 deletions tests/test_clearsky.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down