Fix delta_kt_prime halving single valid neighbor difference (gh-1847)#2821
Fix delta_kt_prime halving single valid neighbor difference (gh-1847)#2821adi-IL wants to merge 2 commits 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 |
@adi-IL thank you for contributing. |
docs/sphinx/source/referencefor API changes.docs/sphinx/source/whatsnewfor all changes. Includes link to the GitHub Issue with:issue:`num`or this Pull Request with:pull:`num`. Includes contributor name and/or GitHub username (link with:ghuser:`user`).remote-data) and Milestone are assigned to the Pull Request and linked Issue.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