From 72af610c0e683e7af47067dc420359dc463bc4bc Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Tue, 10 May 2022 17:51:59 +0200 Subject: [PATCH 01/16] add a todo in disc function --- pvlib/irradiance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index c5c3b1c095..d08ce6d8e0 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1394,7 +1394,7 @@ def disc(ghi, solar_zenith, datetime_or_doy, pressure=101325, -------- dirint """ - + # TODO: this function should be vectorized # this is the I0 calculation from the reference # SSC uses solar constant = 1367.0 (checked 2018 08 15) I0 = get_extra_radiation(datetime_or_doy, 1370., 'spencer') From e37c4bba8e96311cd134787f883e17281483e40a Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Thu, 12 May 2022 10:53:54 +0200 Subject: [PATCH 02/16] vectorize disc --- pvlib/irradiance.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index d08ce6d8e0..0ea68ba8f6 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1394,11 +1394,13 @@ def disc(ghi, solar_zenith, datetime_or_doy, pressure=101325, -------- dirint """ - # TODO: this function should be vectorized # this is the I0 calculation from the reference # SSC uses solar constant = 1367.0 (checked 2018 08 15) I0 = get_extra_radiation(datetime_or_doy, 1370., 'spencer') + # Considering the extra radiation is only time dependent, broadcast it to ghi's dimensions + I0 = np.broadcast_to(I0, ghi.shape).astype(np.float32) + kt = clearness_index(ghi, solar_zenith, I0, min_cos_zenith=min_cos_zenith, max_clearness_index=1) From a3f3f8ecf590ff6216fc6bd3d0a1c92cb9ffde4d Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Thu, 12 May 2022 10:55:47 +0200 Subject: [PATCH 03/16] vectorize _delta_kt_prime_dirint --- pvlib/irradiance.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 0ea68ba8f6..fb032f2a10 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1588,20 +1588,26 @@ def _delta_kt_prime_dirint(kt_prime, use_delta_kt_prime, times): Calculate delta_kt_prime (Perez eqn 2 and eqn 3), or return a default value for use with :py:func:`_dirint_bins`. """ + shape = kt_prime.shape # times as last dimension if use_delta_kt_prime: # Perez eqn 2 - kt_next = kt_prime.shift(-1) - kt_previous = kt_prime.shift(1) - # replace nan with values that implement Perez Eq 3 for first and last - # positions. Use kt_previous and kt_next to handle series of length 1 - kt_next.iloc[-1] = kt_previous.iloc[-1] - kt_previous.iloc[0] = kt_next.iloc[0] - delta_kt_prime = 0.5 * ((kt_prime - kt_next).abs().add( - (kt_prime - kt_previous).abs(), - fill_value=0)) + kt_diffp1 = np.abs(np.diff(kt_prime, axis=-1)) + kt_diffm1 = np.flip(np.abs(np.diff(np.flip(kt_prime, axis=-1), axis=-1)), axis=-1) + + kt_next = np.empty(shape, dtype=np.float32) + # work only on last dimension + kt_next[..., :-1] = kt_diffp1 + kt_next[..., -1] = kt_diffm1[..., -1] + + kt_previous = np.empty(shape, dtype=np.float32) + # work only on last dimension + kt_previous[..., 1:] = kt_diffm1 + kt_previous[..., 0] = kt_diffp1[..., 0] + + delta_kt_prime = 0.5 * (np.abs(kt_prime - kt_next) + np.abs(kt_prime - kt_previous)) else: # do not change unless also modifying _dirint_bins - delta_kt_prime = pd.Series(-1, index=times) + delta_kt_prime = np.full(shape, -1) return delta_kt_prime From 273ee88d79f6492ac5d2655c967fa381bcac9eda Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Thu, 12 May 2022 10:58:30 +0200 Subject: [PATCH 04/16] put shape as function argument in _delte_kt_prime_dirint --- pvlib/irradiance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index fb032f2a10..af5e0897f4 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1583,7 +1583,7 @@ def _dirint_from_dni_ktprime(dni, kt_prime, solar_zenith, use_delta_kt_prime, return dni_dirint -def _delta_kt_prime_dirint(kt_prime, use_delta_kt_prime, times): +def _delta_kt_prime_dirint(kt_prime, use_delta_kt_prime, shape): """ Calculate delta_kt_prime (Perez eqn 2 and eqn 3), or return a default value for use with :py:func:`_dirint_bins`. From bb510823aef9d24b91412c624403dd778f43b5e7 Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Thu, 12 May 2022 10:59:14 +0200 Subject: [PATCH 05/16] vectorize _temp_dew_dirint --- pvlib/irradiance.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index af5e0897f4..3738d6e051 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1588,7 +1588,6 @@ def _delta_kt_prime_dirint(kt_prime, use_delta_kt_prime, shape): Calculate delta_kt_prime (Perez eqn 2 and eqn 3), or return a default value for use with :py:func:`_dirint_bins`. """ - shape = kt_prime.shape # times as last dimension if use_delta_kt_prime: # Perez eqn 2 kt_diffp1 = np.abs(np.diff(kt_prime, axis=-1)) @@ -1611,17 +1610,17 @@ def _delta_kt_prime_dirint(kt_prime, use_delta_kt_prime, shape): return delta_kt_prime -def _temp_dew_dirint(temp_dew, times): +def _temp_dew_dirint(temp_dew, shape): """ Calculate precipitable water from surface dew point temp (Perez eqn 4), or return a default value for use with :py:func:`_dirint_bins`. """ if temp_dew is not None: # Perez eqn 4 - w = pd.Series(np.exp(0.07 * temp_dew - 0.075), index=times) + w = np.exp(0.07 * temp_dew - 0.075) else: # do not change unless also modifying _dirint_bins - w = pd.Series(-1, index=times) + w = np.full(shape, -1) return w From 6632758e09ac7581f5a404d467176abb22f26cb7 Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Thu, 12 May 2022 11:12:26 +0200 Subject: [PATCH 06/16] improve docstrings --- pvlib/irradiance.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 3738d6e051..a023b98b96 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1587,6 +1587,17 @@ def _delta_kt_prime_dirint(kt_prime, use_delta_kt_prime, shape): """ Calculate delta_kt_prime (Perez eqn 2 and eqn 3), or return a default value for use with :py:func:`_dirint_bins`. + + Parameters + ---------- + kt_prime : Zenith-independent clearness index + use_delta_kt_prime : Boolean flag whether to use calculate the stability + index or to use the default value + shape : Shape of the input data + + Returns + ---------- + delta_kt_prime : Stability index """ if use_delta_kt_prime: # Perez eqn 2 @@ -1614,6 +1625,15 @@ def _temp_dew_dirint(temp_dew, shape): """ Calculate precipitable water from surface dew point temp (Perez eqn 4), or return a default value for use with :py:func:`_dirint_bins`. + + Parameters + ---------- + temp_dew : Surface dew point temperature array + shape : Shape of the input data + + Returns + ---------- + w : Precipitable water estimated from surface dew-point temperature """ if temp_dew is not None: # Perez eqn 4 From f941da579cb42f08dcd7e651943e35f47c5a80f6 Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Thu, 12 May 2022 11:12:53 +0200 Subject: [PATCH 07/16] vectorize _dirint_coeffs --- pvlib/irradiance.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index a023b98b96..b8831931ec 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1644,7 +1644,7 @@ def _temp_dew_dirint(temp_dew, shape): return w -def _dirint_coeffs(times, kt_prime, solar_zenith, w, delta_kt_prime): +def _dirint_coeffs(kt_prime, solar_zenith, w, delta_kt_prime): """ Determine the DISC to DIRINT multiplier `dirint_coeffs`. @@ -1652,7 +1652,6 @@ def _dirint_coeffs(times, kt_prime, solar_zenith, w, delta_kt_prime): Parameters ---------- - times : pd.DatetimeIndex kt_prime : Zenith-independent clearness index solar_zenith : Solar zenith angle w : precipitable water estimated from surface dew-point temperature @@ -1662,21 +1661,28 @@ def _dirint_coeffs(times, kt_prime, solar_zenith, w, delta_kt_prime): ------- dirint_coeffs : array-like """ - kt_prime_bin, zenith_bin, w_bin, delta_kt_prime_bin = \ - _dirint_bins(times, kt_prime, solar_zenith, w, delta_kt_prime) + kt_prime_bin, zenith_bin, w_bin, delta_kt_prime_bin = _dirint_bins( + kt_prime=kt_prime, + solar_zenith=solar_zenith, + w=w, + delta_kt_prime=delta_kt_prime, + ) # get the coefficients - coeffs = _get_dirint_coeffs() + coeffs = _get_dirint_coeffs().astype(np.float32) # subtract 1 to account for difference between MATLAB-style bin # assignment and Python-style array lookup. - dirint_coeffs = coeffs[kt_prime_bin-1, zenith_bin-1, - delta_kt_prime_bin-1, w_bin-1] - - # convert unassigned bins to nan - dirint_coeffs = np.where((kt_prime_bin == 0) | (zenith_bin == 0) | - (w_bin == 0) | (delta_kt_prime_bin == 0), - np.nan, dirint_coeffs) + dirint_coeffs = coeffs[kt_prime_bin - 1, zenith_bin - 1, + delta_kt_prime_bin - 1, w_bin - 1] + + # convert unassigned bins to 1, instead of putting nan originally + # whenever the dirint coeff is not known it should be preferred to fall back + # to disc values and therefore have a coeff of 1. + dirint_coeffs[ + (kt_prime_bin == 0) | (zenith_bin == 0) | + (w_bin == 0) | (delta_kt_prime_bin == 0) + ] = 1 return dirint_coeffs From e0c8d9a13a2fadf07deec49d889726c31bdc32c1 Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Thu, 12 May 2022 11:16:50 +0200 Subject: [PATCH 08/16] vectorize _dirint_bins --- pvlib/irradiance.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index b8831931ec..440d59e3ba 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1686,13 +1686,12 @@ def _dirint_coeffs(kt_prime, solar_zenith, w, delta_kt_prime): return dirint_coeffs -def _dirint_bins(times, kt_prime, zenith, w, delta_kt_prime): +def _dirint_bins(kt_prime, zenith, w, delta_kt_prime): """ Determine the bins for the DIRINT coefficients. Parameters ---------- - times : pd.DatetimeIndex kt_prime : Zenith-independent clearness index zenith : Solar zenith angle w : precipitable water estimated from surface dew-point temperature @@ -1702,11 +1701,12 @@ def _dirint_bins(times, kt_prime, zenith, w, delta_kt_prime): ------- tuple of kt_prime_bin, zenith_bin, w_bin, delta_kt_prime_bin """ + shape = kt_prime.shape # @wholmgren: the following bin assignments use MATLAB's 1-indexing. # Later, we'll subtract 1 to conform to Python's 0-indexing. # Create kt_prime bins - kt_prime_bin = pd.Series(0, index=times, dtype=np.int64) + kt_prime_bin = np.zeros(shape, dtype=np.int8) kt_prime_bin[(kt_prime >= 0) & (kt_prime < 0.24)] = 1 kt_prime_bin[(kt_prime >= 0.24) & (kt_prime < 0.4)] = 2 kt_prime_bin[(kt_prime >= 0.4) & (kt_prime < 0.56)] = 3 @@ -1715,7 +1715,7 @@ def _dirint_bins(times, kt_prime, zenith, w, delta_kt_prime): kt_prime_bin[(kt_prime >= 0.8) & (kt_prime <= 1)] = 6 # Create zenith angle bins - zenith_bin = pd.Series(0, index=times, dtype=np.int64) + zenith_bin = np.zeros(shape, dtype=np.int8) zenith_bin[(zenith >= 0) & (zenith < 25)] = 1 zenith_bin[(zenith >= 25) & (zenith < 40)] = 2 zenith_bin[(zenith >= 40) & (zenith < 55)] = 3 @@ -1724,7 +1724,7 @@ def _dirint_bins(times, kt_prime, zenith, w, delta_kt_prime): zenith_bin[(zenith >= 80)] = 6 # Create the bins for w based on dew point temperature - w_bin = pd.Series(0, index=times, dtype=np.int64) + w_bin = np.zeros(shape, dtype=np.int8) w_bin[(w >= 0) & (w < 1)] = 1 w_bin[(w >= 1) & (w < 2)] = 2 w_bin[(w >= 2) & (w < 3)] = 3 @@ -1732,7 +1732,7 @@ def _dirint_bins(times, kt_prime, zenith, w, delta_kt_prime): w_bin[(w == -1)] = 5 # Create delta_kt_prime binning. - delta_kt_prime_bin = pd.Series(0, index=times, dtype=np.int64) + delta_kt_prime_bin = np.zeros(shape, dtype=np.int8) delta_kt_prime_bin[(delta_kt_prime >= 0) & (delta_kt_prime < 0.015)] = 1 delta_kt_prime_bin[(delta_kt_prime >= 0.015) & (delta_kt_prime < 0.035)] = 2 From 105c49f65bbd24d7829bd50cd8bb94b2e4cc299f Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Thu, 12 May 2022 11:22:11 +0200 Subject: [PATCH 09/16] vectorize dirint --- pvlib/irradiance.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 440d59e3ba..e77d8b72ee 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1545,7 +1545,7 @@ def dirint(ghi, solar_zenith, times, pressure=101325., use_delta_kt_prime=True, Global Horizontal to Direct Normal Insolation", Technical Report No. SERI/TR-215-3087, Golden, CO: Solar Energy Research Institute, 1987. """ - + shape = ghi.shape disc_out = disc(ghi, solar_zenith, times, pressure=pressure, min_cos_zenith=min_cos_zenith, max_zenith=max_zenith) airmass = disc_out['airmass'] @@ -1554,10 +1554,10 @@ def dirint(ghi, solar_zenith, times, pressure=101325., use_delta_kt_prime=True, kt_prime = clearness_index_zenith_independent( kt, airmass, max_clearness_index=1) delta_kt_prime = _delta_kt_prime_dirint(kt_prime, use_delta_kt_prime, - times) - w = _temp_dew_dirint(temp_dew, times) + shape) + w = _temp_dew_dirint(temp_dew, shape) - dirint_coeffs = _dirint_coeffs(times, kt_prime, solar_zenith, w, + dirint_coeffs = _dirint_coeffs(kt_prime, solar_zenith, w, delta_kt_prime) # Perez eqn 5 From 5ab066a2758f3718b7d5b55b61f047382de10caf Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Thu, 12 May 2022 11:26:06 +0200 Subject: [PATCH 10/16] avoid dividing by zero in dirindex --- pvlib/irradiance.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index e77d8b72ee..63ad09974a 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1833,9 +1833,13 @@ def dirindex(ghi, ghi_clearsky, dni_clearsky, zenith, times, pressure=101325., min_cos_zenith=min_cos_zenith, max_zenith=max_zenith) - dni_dirindex = dni_clearsky * dni_dirint / dni_dirint_clearsky + # Avoid dividing by zero with clearsky. Whenever the dni_dirint_clearsky is zero, the array defaults + # to dni_dirint (which logically should be also zero) + nonzero = dni_dirint_clearsky != 0 + dni_dirindex = dni_dirint + dni_dirindex[nonzero] = dni_clearsky[nonzero] * dni_dirint[nonzero] / dni_dirint_clearsky[nonzero] - dni_dirindex[dni_dirindex < 0] = 0. + dni_dirindex[dni_dirindex < 0] = 0.0 return dni_dirindex From 17bee9bd8432b701c7a4223247965bf19da8dd3c Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Thu, 19 May 2022 17:25:50 +0200 Subject: [PATCH 11/16] add PVLIB_IRR_DTYPE env variable to choose precision --- pvlib/irradiance.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 63ad09974a..d73d3d9ff1 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -29,6 +29,7 @@ 'fresh steel': 0.35, 'dirty steel': 0.08, 'sea': 0.06} +PVLIB_IRR_DTYPE = getenv('PVLIB_IRR_DTYPE', default=np.float64) def get_extra_radiation(datetime_or_doy, solar_constant=1366.1, @@ -1399,7 +1400,7 @@ def disc(ghi, solar_zenith, datetime_or_doy, pressure=101325, I0 = get_extra_radiation(datetime_or_doy, 1370., 'spencer') # Considering the extra radiation is only time dependent, broadcast it to ghi's dimensions - I0 = np.broadcast_to(I0, ghi.shape).astype(np.float32) + I0 = np.broadcast_to(I0, ghi.shape).astype(PVLIB_IRR_DTYPE) kt = clearness_index(ghi, solar_zenith, I0, min_cos_zenith=min_cos_zenith, max_clearness_index=1) @@ -1604,12 +1605,12 @@ def _delta_kt_prime_dirint(kt_prime, use_delta_kt_prime, shape): kt_diffp1 = np.abs(np.diff(kt_prime, axis=-1)) kt_diffm1 = np.flip(np.abs(np.diff(np.flip(kt_prime, axis=-1), axis=-1)), axis=-1) - kt_next = np.empty(shape, dtype=np.float32) + kt_next = np.empty(shape, dtype=PVLIB_IRR_DTYPE) # work only on last dimension kt_next[..., :-1] = kt_diffp1 kt_next[..., -1] = kt_diffm1[..., -1] - kt_previous = np.empty(shape, dtype=np.float32) + kt_previous = np.empty(shape, dtype=PVLIB_IRR_DTYPE) # work only on last dimension kt_previous[..., 1:] = kt_diffm1 kt_previous[..., 0] = kt_diffp1[..., 0] @@ -1669,7 +1670,7 @@ def _dirint_coeffs(kt_prime, solar_zenith, w, delta_kt_prime): ) # get the coefficients - coeffs = _get_dirint_coeffs().astype(np.float32) + coeffs = _get_dirint_coeffs().astype(PVLIB_IRR_DTYPE) # subtract 1 to account for difference between MATLAB-style bin # assignment and Python-style array lookup. From b8631e4bd3fc3b779b1cf2fea6a1d499db669cfe Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Thu, 19 May 2022 17:29:54 +0200 Subject: [PATCH 12/16] add getenv import --- pvlib/irradiance.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index d73d3d9ff1..7f053c8bf4 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -7,6 +7,7 @@ import datetime from collections import OrderedDict from functools import partial +from os import getenv import numpy as np import pandas as pd From 09c1b42ef837f524858470dc109dd6387e2a5b78 Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Thu, 19 May 2022 18:15:26 +0200 Subject: [PATCH 13/16] fix keyword argument call in _dirint_coeffs --- pvlib/irradiance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 7f053c8bf4..8de22d1b7b 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1665,7 +1665,7 @@ def _dirint_coeffs(kt_prime, solar_zenith, w, delta_kt_prime): """ kt_prime_bin, zenith_bin, w_bin, delta_kt_prime_bin = _dirint_bins( kt_prime=kt_prime, - solar_zenith=solar_zenith, + zenith=solar_zenith, w=w, delta_kt_prime=delta_kt_prime, ) From 201e2b0564582ae19c360dd11fa769992d02ef6d Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Fri, 20 May 2022 11:44:54 +0200 Subject: [PATCH 14/16] stickler code quality --- pvlib/irradiance.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 8de22d1b7b..6ff92604a0 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1400,7 +1400,8 @@ def disc(ghi, solar_zenith, datetime_or_doy, pressure=101325, # SSC uses solar constant = 1367.0 (checked 2018 08 15) I0 = get_extra_radiation(datetime_or_doy, 1370., 'spencer') - # Considering the extra radiation is only time dependent, broadcast it to ghi's dimensions + # Considering the extra radiation is only time dependent, + # broadcast it to ghi's dimensions I0 = np.broadcast_to(I0, ghi.shape).astype(PVLIB_IRR_DTYPE) kt = clearness_index(ghi, solar_zenith, I0, min_cos_zenith=min_cos_zenith, @@ -1593,7 +1594,7 @@ def _delta_kt_prime_dirint(kt_prime, use_delta_kt_prime, shape): Parameters ---------- kt_prime : Zenith-independent clearness index - use_delta_kt_prime : Boolean flag whether to use calculate the stability + use_delta_kt_prime : Boolean flag whether to use calculate the stability index or to use the default value shape : Shape of the input data @@ -1604,7 +1605,9 @@ def _delta_kt_prime_dirint(kt_prime, use_delta_kt_prime, shape): if use_delta_kt_prime: # Perez eqn 2 kt_diffp1 = np.abs(np.diff(kt_prime, axis=-1)) - kt_diffm1 = np.flip(np.abs(np.diff(np.flip(kt_prime, axis=-1), axis=-1)), axis=-1) + kt_diffm1 = np.flip( + np.abs(np.diff(np.flip(kt_prime, axis=-1), axis=-1)), axis=-1 + ) kt_next = np.empty(shape, dtype=PVLIB_IRR_DTYPE) # work only on last dimension @@ -1616,7 +1619,9 @@ def _delta_kt_prime_dirint(kt_prime, use_delta_kt_prime, shape): kt_previous[..., 1:] = kt_diffm1 kt_previous[..., 0] = kt_diffp1[..., 0] - delta_kt_prime = 0.5 * (np.abs(kt_prime - kt_next) + np.abs(kt_prime - kt_previous)) + delta_kt_prime = 0.5 * ( + np.abs(kt_prime - kt_next) + np.abs(kt_prime - kt_previous) + ) else: # do not change unless also modifying _dirint_bins delta_kt_prime = np.full(shape, -1) @@ -1663,12 +1668,9 @@ def _dirint_coeffs(kt_prime, solar_zenith, w, delta_kt_prime): ------- dirint_coeffs : array-like """ - kt_prime_bin, zenith_bin, w_bin, delta_kt_prime_bin = _dirint_bins( - kt_prime=kt_prime, - zenith=solar_zenith, - w=w, - delta_kt_prime=delta_kt_prime, - ) + kt_prime_bin, zenith_bin, w_bin, delta_kt_prime_bin = \ + _dirint_bins(kt_prime=kt_prime, zenith=solar_zenith, + w=w, delta_kt_prime=delta_kt_prime) # get the coefficients coeffs = _get_dirint_coeffs().astype(PVLIB_IRR_DTYPE) @@ -1679,10 +1681,11 @@ def _dirint_coeffs(kt_prime, solar_zenith, w, delta_kt_prime): delta_kt_prime_bin - 1, w_bin - 1] # convert unassigned bins to 1, instead of putting nan originally - # whenever the dirint coeff is not known it should be preferred to fall back + # whenever the dirint coeff is not known + # it should be preferred to fall back # to disc values and therefore have a coeff of 1. dirint_coeffs[ - (kt_prime_bin == 0) | (zenith_bin == 0) | + (kt_prime_bin == 0) | (zenith_bin == 0) | (w_bin == 0) | (delta_kt_prime_bin == 0) ] = 1 return dirint_coeffs @@ -1835,11 +1838,14 @@ def dirindex(ghi, ghi_clearsky, dni_clearsky, zenith, times, pressure=101325., min_cos_zenith=min_cos_zenith, max_zenith=max_zenith) - # Avoid dividing by zero with clearsky. Whenever the dni_dirint_clearsky is zero, the array defaults - # to dni_dirint (which logically should be also zero) + # Avoid dividing by zero with clearsky. + # Whenever the dni_dirint_clearsky is zero, the array defaults + # to dni_dirint (which logically should be also zero) nonzero = dni_dirint_clearsky != 0 dni_dirindex = dni_dirint - dni_dirindex[nonzero] = dni_clearsky[nonzero] * dni_dirint[nonzero] / dni_dirint_clearsky[nonzero] + dni_dirindex[nonzero] = \ + dni_clearsky[nonzero] * dni_dirint[nonzero] / \ + dni_dirint_clearsky[nonzero] dni_dirindex[dni_dirindex < 0] = 0.0 From a35746531e6f3ca85c62a75eb5173e95da30f58b Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Fri, 20 May 2022 11:46:26 +0200 Subject: [PATCH 15/16] stickler code quality #2 --- pvlib/irradiance.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 6ff92604a0..c26d0de2c8 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1678,7 +1678,7 @@ def _dirint_coeffs(kt_prime, solar_zenith, w, delta_kt_prime): # subtract 1 to account for difference between MATLAB-style bin # assignment and Python-style array lookup. dirint_coeffs = coeffs[kt_prime_bin - 1, zenith_bin - 1, - delta_kt_prime_bin - 1, w_bin - 1] + delta_kt_prime_bin - 1, w_bin - 1] # convert unassigned bins to 1, instead of putting nan originally # whenever the dirint coeff is not known From 5bf90492899551a4773b9955b2debc2c34f52bd9 Mon Sep 17 00:00:00 2001 From: Alexis Saint Georges-Chaumet Date: Fri, 20 May 2022 11:49:11 +0200 Subject: [PATCH 16/16] stickler --- pvlib/irradiance.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index c26d0de2c8..96fdedf591 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -1677,8 +1677,8 @@ def _dirint_coeffs(kt_prime, solar_zenith, w, delta_kt_prime): # subtract 1 to account for difference between MATLAB-style bin # assignment and Python-style array lookup. - dirint_coeffs = coeffs[kt_prime_bin - 1, zenith_bin - 1, - delta_kt_prime_bin - 1, w_bin - 1] + dirint_coeffs = coeffs[kt_prime_bin-1, zenith_bin-1, + delta_kt_prime_bin-1, w_bin-1] # convert unassigned bins to 1, instead of putting nan originally # whenever the dirint coeff is not known