Skip to content

Commit c484e2c

Browse files
committed
fix syntax problems. add comments and logging. improve bin table
1 parent d611f31 commit c484e2c

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

pvlib/clearsky.py

+27-11
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def disc(GHI, zenith, times, pressure=101325):
385385

386386

387387
def dirint(ghi, zenith, times, pressure, use_delta_kt_prime=True,
388-
temp_dew=None)
388+
temp_dew=None):
389389
"""
390390
Determine DNI from GHI using the DIRINT modification
391391
of the DISC model.
@@ -461,13 +461,15 @@ def dirint(ghi, zenith, times, pressure, use_delta_kt_prime=True,
461461
# Note that we calculate the AM pressure correction slightly differently
462462
# than Perez. He uses altitude, we use pressure (which we calculate
463463
# slightly differently)
464-
airmass = (1./(cosd(zenith) + 0.15*((93.885-zenith)**(-1.253))) *
464+
airmass = (1./(tools.cosd(zenith) + 0.15*((93.885-zenith)**(-1.253))) *
465465
pressure/101325)
466466

467467
coeffs = _get_dirint_coeffs()
468468

469469
kt_prime = kt / (1.031 * np.exp(-1.4/(0.9+9.4/airmass)) + 0.1)
470470
kt_prime[kt_prime > 0.82] = 0.82 # From SRRL code. consider np.NaN
471+
kt_prime.fillna(0, inplace=True)
472+
logger.debug('kt_prime:\n{}'.format(kt_prime))
471473

472474
# wholmgren:
473475
# the use_delta_kt_prime statement is a port of the MATLAB code.
@@ -486,33 +488,39 @@ def dirint(ghi, zenith, times, pressure, use_delta_kt_prime=True,
486488
else:
487489
w = pd.Series(-1, index=times)
488490

489-
# Error check KtPrime and create KtPrime bins
491+
# @wholmgren: the following bin assignments use MATLAB's 1-indexing.
492+
# Later, we'll subtract 1 to conform to Python's 0-indexing.
493+
494+
# Create kt_prime bins
490495
kt_prime_bin = pd.Series(index=times)
491496
kt_prime_bin[(kt_prime>=0) & (kt_prime<0.24)] = 1
492497
kt_prime_bin[(kt_prime>=0.24) & (kt_prime<0.4)] = 2
493498
kt_prime_bin[(kt_prime>=0.4) & (kt_prime<0.56)] = 3
494499
kt_prime_bin[(kt_prime>=0.56) & (kt_prime<0.7)] = 4
495500
kt_prime_bin[(kt_prime>=0.7) & (kt_prime<0.8)] = 5
496501
kt_prime_bin[(kt_prime>=0.8) & (kt_prime<=1)] = 6
502+
logger.debug('kt_prime_bin:\n{}'.format(kt_prime_bin))
497503

498-
# Create Zenith angle bins
504+
# Create zenith angle bins
499505
zenith_bin = pd.Series(index=times)
500506
zenith_bin[(zenith>=0) & (zenith<25)] = 1
501507
zenith_bin[(zenith>=25) & (zenith<40)] = 2
502508
zenith_bin[(zenith>=40) & (zenith<55)] = 3
503509
zenith_bin[(zenith>=55) & (zenith<70)] = 4
504510
zenith_bin[(zenith>=70) & (zenith<80)] = 5
505511
zenith_bin[(zenith>=80)] = 6
512+
logger.debug('zenith_bin:\n{}'.format(zenith_bin))
506513

507-
# Create the bins for W based on dew point temperature
514+
# Create the bins for w based on dew point temperature
508515
w_bin = pd.Series(index=times)
509516
w_bin[(w>=0) & (w<1)] = 1
510517
w_bin[(w>=1) & (w<2)] = 2
511518
w_bin[(w>=2) & (w<3)] = 3
512519
w_bin[(w>=3)] = 4
513520
w_bin[(w == -1)] = 5
521+
logger.debug('w_bin:\n{}'.format(w_bin))
514522

515-
# Error check values of DelKtPrime, and create DelKtPrime binning.
523+
# Create delta_kt_prime binning.
516524
delta_kt_prime_bin = pd.Series(index=times)
517525
delta_kt_prime_bin[(delta_kt_prime>=0) & (delta_kt_prime<0.015)] = 1
518526
delta_kt_prime_bin[(delta_kt_prime>=0.015) & (delta_kt_prime<0.035)] = 2
@@ -521,9 +529,12 @@ def dirint(ghi, zenith, times, pressure, use_delta_kt_prime=True,
521529
delta_kt_prime_bin[(delta_kt_prime>=0.15) & (delta_kt_prime<0.3)] = 5
522530
delta_kt_prime_bin[(delta_kt_prime>=0.3) & (delta_kt_prime<=1)] = 6
523531
delta_kt_prime_bin[delta_kt_prime == -1] = 7
532+
logger.debug('delta_kt_prime_bin:\n{}'.format(delta_kt_prime_bin))
524533

525-
dirint_coeffs = coeffs[kt_prime_bin, zenith_bin,
526-
delta_kt_prime_bin, w_bin]
534+
# subtract 1 to account for difference between MATLAB-style bin
535+
# assignment and Python-style array lookup.
536+
dirint_coeffs = coeffs[kt_prime_bin-1, zenith_bin-1,
537+
delta_kt_prime_bin-1, w_bin-1]
527538

528539
dni = disc_out['DNI_gen_DISC'] * dirint_coeffs
529540

@@ -536,10 +547,15 @@ def _get_dirint_coeffs():
536547
537548
Returns
538549
-------
539-
np.array with shape ``(7, 7, 7, 5)``. The MATLAB indicies are preserved,
540-
meaning that 0 padding has been added to the "left" side of the array(s).
550+
np.array with shape ``(6, 6, 7, 5)``.
551+
Ordering is ``[kt_prime_bin, zenith_bin, delta_kt_prime_bin, w_bin]``
541552
"""
542553

554+
555+
# To allow for maximum copy/paste from the MATLAB 1-indexed code,
556+
# we create and assign values to an oversized array.
557+
# Then, we return the [1:, 1:, :, :] slice.
558+
543559
coeffs = np.zeros((7,7,7,5))
544560

545561
coeffs[1,1,:,:] = [
@@ -866,5 +882,5 @@ def _get_dirint_coeffs():
866882
[0.475230, 0.500000, 0.518640, 0.339970, 0.520230],
867883
[0.743440, 0.592190, 0.603060, 0.316930, 0.794390 ]]
868884

869-
return coeffs
885+
return coeffs[1:,1:,:,:]
870886

0 commit comments

Comments
 (0)