From b945e324028fd72f09923120ae3fd71cd0d223a4 Mon Sep 17 00:00:00 2001 From: vera-rubin <322284620+vera-rubin@users.noreply.github.com> Date: Tue, 13 Oct 2026 03:35:17 +0000 Subject: [PATCH] Fix dirint NaN at night and for missing temp_dew dirint returned NaN where solar_zenith is above 90 degrees: the DIRINT coefficient is NaN there because the airmass is undefined, and it turned the zero DNI from disc into NaN. Reapply the max_zenith cutoff after the coefficient multiplication. A NaN temp_dew value also left the precipitable water bin unassigned. Use the dew point unavailable bin for missing values, as the dirint, dirindex and gti_dirint docstrings describe. Add a regression test and a whatsnew entry. --- docs/sphinx/source/whatsnew/v0.16.2.rst | 5 +++++ pvlib/irradiance.py | 6 ++++++ tests/test_irradiance.py | 28 ++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/docs/sphinx/source/whatsnew/v0.16.2.rst b/docs/sphinx/source/whatsnew/v0.16.2.rst index f0b52494dc..9f6000d165 100644 --- a/docs/sphinx/source/whatsnew/v0.16.2.rst +++ b/docs/sphinx/source/whatsnew/v0.16.2.rst @@ -16,6 +16,10 @@ Bug fixes ~~~~~~~~~ * :py:func:`pvlib.tracking.calc_surface_orientation` returns scalar when input is scalar (:issue:`2881`, :pull:`2882`) +* :py:func:`pvlib.irradiance.dirint` now returns 0 instead of NaN for + ``solar_zenith`` greater than ``max_zenith`` when the airmass is + undefined, and uses the dew point unavailable coefficients for NaN + values of ``temp_dew``, as documented. (:issue:`2900`) Enhancements ~~~~~~~~~~~~ @@ -56,6 +60,7 @@ Contributors * Chen Yi (:ghuser:`chenyi6758-source`) * Rajiv Daxini (:ghuser:`rdaxini`) * jason-writer (:ghuser:`jason-writer`) +* vera-rubin (:ghuser:`vera-rubin`) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 38bf122b91..c2633c84a5 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -2147,6 +2147,10 @@ def dirint(ghi, solar_zenith, times, pressure=101325., use_delta_kt_prime=True, # Perez eqn 5 dni = disc_out['dni'] * dirint_coeffs + # dirint_coeffs is NaN where kt_prime is undefined (zenith > 90), so + # reapply the max_zenith cutoff from disc + dni[np.asarray(solar_zenith) > max_zenith] = 0 + return dni @@ -2196,6 +2200,8 @@ def _temp_dew_dirint(temp_dew, times): if temp_dew is not None: # Perez eqn 4 w = pd.Series(np.exp(0.07 * temp_dew - 0.075), index=times) + # missing temp_dew values use the dew point unavailable bin + w = w.fillna(-1) else: # do not change unless also modifying _dirint_bins w = pd.Series(-1, index=times) diff --git a/tests/test_irradiance.py b/tests/test_irradiance.py index b0d9863ad5..4a7fb8c5e0 100644 --- a/tests/test_irradiance.py +++ b/tests/test_irradiance.py @@ -890,7 +890,28 @@ def test_dirint_nans(): dirint_data = irradiance.dirint(ghi, zenith, times, pressure=pressure, temp_dew=temp_dew) assert_almost_equal(dirint_data.values, - np.array([np.nan, np.nan, np.nan, np.nan, 893.1]), 1) + np.array([np.nan, np.nan, np.nan, 888.1, 893.1]), 1) + + +def test_dirint_night_and_missing_temp_dew(): + # GH 2900: DNI is 0 above max_zenith, also where airmass is undefined + times = pd.date_range('2024-06-21T22:00Z', periods=3, freq='h') + ghi = pd.Series([0., 0., 0.], index=times) + zenith = pd.Series([91., 100., 110.], index=times) + out = irradiance.dirint(ghi, zenith, times) + expected = pd.Series([0., 0., 0.], index=times, name='dni') + assert_series_equal(out, expected) + + # GH 2900: a missing temp_dew value uses the dew point unavailable + # coefficients, the same as omitting temp_dew + times = pd.date_range('2024-06-21T11:00Z', periods=3, freq='h') + ghi = pd.Series([800., 800., 800.], index=times) + zenith = pd.Series([20., 20., 20.], index=times) + temp_dew = pd.Series([10., np.nan, 10.], index=times) + out = irradiance.dirint(ghi, zenith, times, temp_dew=temp_dew) + expected = pd.Series([437.595652, 515.527062, 437.595652], index=times, + name='dni') + assert_series_equal(out, expected) def test_delta_kt_prime_interior_nan(): @@ -1291,9 +1312,10 @@ def test_dirindex(times): assert np.allclose(out, expected_out, rtol=tolerance, atol=0, equal_nan=True) tol_dirint = 0.2 + # at night dirindex is nan (0/0) while dirint is 0, so skip that point assert np.allclose( - out.values, - dirint_close_values, + out.values[1:], + dirint_close_values[1:], rtol=tol_dirint, atol=0, equal_nan=True)