Fix delta_kt_prime halving single valid neighbor difference (gh-1847)#2821
Open
adi-IL wants to merge 1 commit into
Open
Fix delta_kt_prime halving single valid neighbor difference (gh-1847)#2821adi-IL wants to merge 1 commit into
adi-IL wants to merge 1 commit into
Conversation
…1847) In irradiance._delta_kt_prime_dirint, when an interior point has only one valid neighbor (a missing value elsewhere in the series), the previous code substituted 0 for the missing neighbor difference and then multiplied by 0.5, halving the remaining valid difference. This produced an incorrect stability index delta_kt_prime and, downstream, incorrect DIRINT beam irradiance. Replace the manual endpoint patching with a row-wise mean of the next and previous absolute differences. pandas skips NaN values, so Perez eqn 2 (two neighbors), eqn 3 for endpoints, and interior gaps with one missing neighbor are all handled correctly. Add a regression test and a whatsnew entry.
Hey @adi-IL! 🎉Thanks for opening your first pull request! We appreciate your If AI is used for any portion of this PR, you must vet the content |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes a logic error in
irradiance._delta_kt_prime_dirint(used bydirint) when the input series contains a missing value in the interior.Background
The DIRINT model computes the stability index
delta_kt_primefrom the zenith independent clearness indexkt_primeusing Perez eqn 2 for interior points and Perez eqn 3 for boundary points. Before this change the code patched only the very first and very last positions to supply a missing neighbor, and for every other point it did:delta_kt_prime = 0.5 * ((kt - kt_next).abs() + (kt - kt_prev).abs())When an interior point has only one valid neighbor (because a gap exists elsewhere in the series), the missing neighbor difference was treated as 0 and the remaining valid difference was then multiplied by 0.5. That halved a single valid difference, which is not what Perez eqn 3 specifies. The correct value is the absolute difference to the one available neighbor.
The fix
Replace the manual endpoint patching with a row wise mean of the next and previous absolute differences:
delta_kt_prime = pd.DataFrame({ next : (kt - kt_next).abs(), prev : (kt - kt_prev).abs() }).mean(axis=1)pandas skips NaN values when taking the mean, so the computation automatically handles zero, one, or two valid neighbors. This covers interior points (eqn 2), the endpoints (eqn 3), and interior gaps where exactly one neighbor is present, all with the same expression.
Example
For
kt_prime = [0.5, 0.6, NaN, 0.7, 0.4]the neighbors of the gap now read:[0.10, 0.05, NaN, 0.15, 0.30](the 0.05 and 0.15 are halved and wrong)[0.10, 0.10, NaN, 0.30, 0.30](correct single neighbor differences)The NaN at the gap position itself is unchanged, since
kt_primeis undefined there.Testing
Added a regression test
test_delta_kt_prime_interior_nan. The fulltests/test_irradiance.pysuite passes (124 passed, 9 remote data tests skipped, which need network access).Closes #1847