Skip to content

Commit c238c48

Browse files
authored
fix _delta_kt_prime_dirint end values (#638)
* fix _delta_kt_prime_dirint end values * syntax, test fixes: * syntax and test fixes * more test fixes * more test fixes * and more test fixes * and still more test fixes * add comments, update whatsnew * delete space
1 parent d61ca1d commit c238c48

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

docs/sphinx/source/whatsnew/v0.6.1.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ Bug fixes
7373
* Fix documentation errors when using IPython >= 7.0.
7474
* Fix error in :func:`pvlib.modelchain.ModelChain.infer_spectral_model` (:issue:`619`)
7575
* Fix error in ``pvlib.spa`` when using Python 3.7 on some platforms.
76+
* Fix error in :func:`pvlib.irradiance._delta_kt_prime_dirint` (:issue:`637`). The error affects
77+
the first and last values of DNI calculated by the function :func:`pvlib.irradiance.dirint`
7678

7779

7880
Testing

pvlib/irradiance.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,14 +1558,20 @@ def _dirint_from_dni_ktprime(dni, kt_prime, solar_zenith, use_delta_kt_prime,
15581558

15591559
def _delta_kt_prime_dirint(kt_prime, use_delta_kt_prime, times):
15601560
"""
1561-
Calculate delta kt prime (Perez eqn 2), or return a default value
1561+
Calculate delta_kt_prime (Perez eqn 2 and eqn 3), or return a default value
15621562
for use with :py:func:`_dirint_bins`.
15631563
"""
15641564
if use_delta_kt_prime:
15651565
# Perez eqn 2
1566-
delta_kt_prime = 0.5*((kt_prime - kt_prime.shift(1)).abs().add(
1567-
(kt_prime - kt_prime.shift(-1)).abs(),
1568-
fill_value=0))
1566+
kt_next = kt_prime.shift(-1)
1567+
kt_previous = kt_prime.shift(1)
1568+
# replace nan with values that implement Perez Eq 3 for first and last
1569+
# positions. Use kt_previous and kt_next to handle series of length 1
1570+
kt_next.iloc[-1] = kt_previous.iloc[-1]
1571+
kt_previous.iloc[0] = kt_next.iloc[0]
1572+
delta_kt_prime = 0.5 * ((kt_prime - kt_next).abs().add(
1573+
(kt_prime - kt_previous).abs(),
1574+
fill_value=0))
15691575
else:
15701576
# do not change unless also modifying _dirint_bins
15711577
delta_kt_prime = pd.Series(-1, index=times)

pvlib/test/test_irradiance.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -466,13 +466,13 @@ def test_dirint_value():
466466
pressure = 93193.
467467
dirint_data = irradiance.dirint(ghi, zenith, times, pressure=pressure)
468468
assert_almost_equal(dirint_data.values,
469-
np.array([ 888. , 683.7]), 1)
469+
np.array([868.8, 699.7]), 1)
470470

471471

472472
def test_dirint_nans():
473473
times = pd.DatetimeIndex(start='2014-06-24T12-0700', periods=5, freq='6H')
474474
ghi = pd.Series([np.nan, 1038.62, 1038.62, 1038.62, 1038.62], index=times)
475-
zenith = pd.Series([10.567, np.nan, 10.567, 10.567, 10.567,], index=times)
475+
zenith = pd.Series([10.567, np.nan, 10.567, 10.567, 10.567], index=times)
476476
pressure = pd.Series([93193., 93193., np.nan, 93193., 93193.], index=times)
477477
temp_dew = pd.Series([10, 10, 10, np.nan, 10], index=times)
478478
dirint_data = irradiance.dirint(ghi, zenith, times, pressure=pressure,
@@ -489,7 +489,7 @@ def test_dirint_tdew():
489489
dirint_data = irradiance.dirint(ghi, zenith, times, pressure=pressure,
490490
temp_dew=10)
491491
assert_almost_equal(dirint_data.values,
492-
np.array([892.9, 636.5]), 1)
492+
np.array([882.1, 672.6]), 1)
493493

494494

495495
def test_dirint_no_delta_kt():
@@ -559,7 +559,7 @@ def test_gti_dirint():
559559
expected = pd.DataFrame(array(
560560
[[ 21.05796198, 0. , 21.05796198],
561561
[ 288.22574368, 60.59964218, 245.37532576],
562-
[ 930.85454521, 695.8504884 , 276.96897609]]),
562+
[ 931.04078010, 695.94965324, 277.06172442]]),
563563
columns=expected_col_order, index=times)
564564

565565
assert_frame_equal(output, expected)
@@ -583,7 +583,7 @@ def test_gti_dirint():
583583
expected = pd.DataFrame(array(
584584
[[ 21.05796198, 0. , 21.05796198],
585585
[ 289.81109139, 60.52460392, 247.01373353],
586-
[ 932.22047435, 647.68716072, 323.59362885]]),
586+
[ 932.46756378, 648.05001357, 323.49974813]]),
587587
columns=expected_col_order, index=times)
588588

589589
assert_frame_equal(output, expected)
@@ -595,9 +595,9 @@ def test_gti_dirint():
595595
albedo=albedo)
596596

597597
expected = pd.DataFrame(array(
598-
[[ 21.3592591 , 0. , 21.3592591 ],
599-
[ 292.5162373 , 64.42628826, 246.95997198],
600-
[ 941.47847463, 727.07261187, 258.25370648]]),
598+
[[ 21.3592591, 0. , 21.3592591 ],
599+
[ 292.5162373, 64.42628826, 246.95997198],
600+
[ 941.6753031, 727.16311901, 258.36548605]]),
601601
columns=expected_col_order, index=times)
602602

603603
assert_frame_equal(output, expected)
@@ -611,7 +611,7 @@ def test_gti_dirint():
611611
expected = pd.DataFrame(array(
612612
[[ 21.05796198, 0. , 21.05796198],
613613
[ 292.40468994, 36.79559287, 266.3862767 ],
614-
[ 930.72198876, 712.36063132, 261.32196017]]),
614+
[ 931.79627208, 689.81549269, 283.5817439]]),
615615
columns=expected_col_order, index=times)
616616

617617
assert_frame_equal(output, expected)

0 commit comments

Comments
 (0)