Skip to content

Commit 730f48e

Browse files
anomamwholmgren
authored andcommitted
Return diffuse components from Perez transposition model (pvlib#259)
* Saving diffuse components into dict to be returned * dictionary returned depending on optional boolean input argument for backward compatibility * Wrote test for diffuse components * all tests are passing * Updated docstring and fixed test for Python3 * Now testing each calculated component * and not just the sum of them * Updated whatsnew file with changes * Cleaning changes * Defining components as ordered dict * Treating case where inputs can be scalar * Check that component col order is correct in test
1 parent e93b14a commit 730f48e

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

docs/sphinx/source/whatsnew/v0.4.2.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Enhancements
4646
separate pages for each function, class, or method. (:issue:`258`)
4747
* Improved Linke turbidity factor time interpolation with Python `calendar`
4848
month days and leap years (:issue:`265`)
49+
* Added option to return diffuse components from Perez transposition model.
4950

5051

5152
Other
@@ -61,3 +62,4 @@ Code Contributors
6162
* Will Holmgren
6263
* Volker Beutner
6364
* Mark Mikofski
65+
* Marc Anoma

pvlib/irradiance.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ def king(surface_tilt, dhi, ghi, solar_zenith):
886886

887887
def perez(surface_tilt, surface_azimuth, dhi, dni, dni_extra,
888888
solar_zenith, solar_azimuth, airmass,
889-
model='allsitescomposite1990'):
889+
model='allsitescomposite1990', return_components=False):
890890
'''
891891
Determine diffuse irradiance from the sky on a tilted surface using
892892
one of the Perez models.
@@ -953,6 +953,10 @@ def perez(surface_tilt, surface_azimuth, dhi, dni, dni_extra,
953953
* 'capecanaveral1988'
954954
* 'albany1988'
955955
956+
return_components: bool (optional, default=False)
957+
Flag used to decide whether to return the calculated diffuse components
958+
or not.
959+
956960
Returns
957961
--------
958962
sky_diffuse : numeric
@@ -1043,7 +1047,26 @@ def perez(surface_tilt, surface_azimuth, dhi, dni, dni_extra,
10431047
else:
10441048
sky_diffuse = np.where(np.isnan(airmass), 0, sky_diffuse)
10451049

1046-
return sky_diffuse
1050+
if return_components:
1051+
diffuse_components = OrderedDict()
1052+
1053+
# Calculate the different components
1054+
diffuse_components['isotropic'] = dhi * term1
1055+
diffuse_components['circumsolar'] = dhi * term2
1056+
diffuse_components['horizon'] = dhi * term3
1057+
1058+
# Set values of components to 0 when sky_diffuse is 0
1059+
mask = sky_diffuse == 0
1060+
if isinstance(sky_diffuse, pd.Series):
1061+
diffuse_components = pd.DataFrame(diffuse_components)
1062+
diffuse_components.ix[mask] = 0
1063+
else:
1064+
diffuse_components = {k: np.where(mask, 0, v) for k, v in diffuse_components.items()}
1065+
1066+
return sky_diffuse, diffuse_components
1067+
1068+
else:
1069+
return sky_diffuse
10471070

10481071

10491072
def disc(ghi, zenith, datetime_or_doy, pressure=101325):

pvlib/test/test_irradiance.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,29 @@ def test_perez():
158158
assert_series_equal(out, expected, check_less_precise=2)
159159

160160

161+
def test_perez_components():
162+
am = atmosphere.relativeairmass(ephem_data['apparent_zenith'])
163+
dni = irrad_data['dni'].copy()
164+
dni.iloc[2] = np.nan
165+
out, df_components = irradiance.perez(40, 180, irrad_data['dhi'], dni,
166+
dni_et, ephem_data['apparent_zenith'],
167+
ephem_data['azimuth'], am, return_components=True)
168+
expected = pd.Series(np.array(
169+
[ 0. , 31.46046871, np.nan, 45.45539877]),
170+
index=times)
171+
expected_components = pd.DataFrame(
172+
np.array([[ 0. , 26.84138589, np.nan, 31.72696071],
173+
[ 0. , 0. , np.nan, 4.47966439],
174+
[ 0. , 4.62212181, np.nan, 9.25316454]]).T,
175+
columns=['isotropic', 'circumsolar', 'horizon'],
176+
index=times
177+
)
178+
sum_components = df_components.sum(axis=1)
179+
180+
assert_series_equal(out, expected, check_less_precise=2)
181+
assert_frame_equal(df_components, expected_components)
182+
assert_series_equal(sum_components, expected, check_less_precise=2)
183+
161184
@needs_numpy_1_10
162185
def test_perez_arrays():
163186
am = atmosphere.relativeairmass(ephem_data['apparent_zenith'])

0 commit comments

Comments
 (0)