Skip to content

Commit 7489f6e

Browse files
committed
Allow for custom irradiations in 'model_parameters'
1 parent bd6b1ed commit 7489f6e

File tree

2 files changed

+21
-40
lines changed

2 files changed

+21
-40
lines changed

pvlib/spectrum/mismatch.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,16 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
261261
airmass_absolute : numeric
262262
Absolute airmass. Give attention to algorithm used (``kasten1966`` is
263263
recommended for default parameters of ``monosi``, ``polysi`` and
264-
``asi``, as used in [1]_).
264+
``asi``, see reference [1]_).
265265
266266
cell_type : string or None, default None
267267
Specifies material of the cell in order to infer model parameters.
268268
Allowed types are ``monosi``, ``polysi`` and ``asi``, either lower or
269269
upper case. If None, the parameters must be provided.
270270
271271
model_parameters : dict-like or None, default None
272-
In case you want to specify your model parameters use a dict or a
273-
``pd.DataFrame`` as follows:
272+
In case you want to specify your model parameters and components
273+
use a dict or a ``pd.DataFrame`` as follows:
274274
275275
.. code-block:: python
276276
@@ -283,8 +283,7 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
283283
# Using a pd.DataFrame
284284
model_parameters = pd.DataFrame({
285285
'direct': [a1, b1, c1],
286-
'sky diffuse': [a2, b2, c2],
287-
'ground diffuse': [a3, b3, c3]},
286+
'diffuse': [a2, b2, c2]},
288287
index=('a', 'b', 'c'))
289288
290289
``a``, ``b`` and ``c`` must be scalar.
@@ -300,7 +299,7 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
300299
Raises
301300
------
302301
ValueError
303-
If ``model_parameters`` is not suitable. See example given above.
302+
If ``model_parameters`` is not suitable. See examples given above.
304303
TypeError
305304
If neither ``cell_type`` nor ``model_parameters`` are given.
306305
NotImplementedError
@@ -311,7 +310,7 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
311310
The mismatch modifier is defined as
312311
313312
.. math:: M = c \cdot \exp( a \cdot (K_t - 0.74) + b \cdot (AM - 1.5) )
314-
313+
315314
where ``a``, ``b`` and ``c`` are the model parameters, different for each
316315
irradiance component.
317316
@@ -329,11 +328,11 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
329328
pvlib.atmosphere.first_solar_spectral_correction
330329
"""
331330

332-
IRRAD_COMPONENTS = ('direct', 'sky_diffuse', 'ground_diffuse')
331+
irrad_components = ('direct', 'sky_diffuse', 'ground_diffuse')
333332
# Fitting parameters directly from [1]_
334333
MARTIN_RUIZ_PARAMS = pd.DataFrame(
335334
index=('monosi', 'polysi', 'asi'),
336-
columns=pd.MultiIndex.from_product([IRRAD_COMPONENTS,
335+
columns=pd.MultiIndex.from_product([irrad_components,
337336
('c', 'a', 'b')]),
338337
data=[ # Direct(c,a,b) | Sky diffuse(c,a,b) | Ground diffuse(c,a,b)
339338
[1.029, -.313, 524e-5, .764, -.882, -.0204, .970, -.244, .0129],
@@ -354,14 +353,13 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
354353
elif cell_type is None and model_parameters is None:
355354
raise TypeError('You must pass at least "cell_type" '
356355
'or "model_parameters" as arguments!')
357-
elif model_parameters is not None:
358-
# Use user-defined model parameters
359-
# Validate 'model_parameters' dict keys and sub-dicts keys
360-
if (set(IRRAD_COMPONENTS) != model_parameters.keys()):
361-
raise ValueError('You must specify model parameters for exact '
362-
f'irradiance components {IRRAD_COMPONENTS}')
363-
if any([{'a', 'b', 'c'} != model_parameters[irrad_type].keys()
364-
for irrad_type in IRRAD_COMPONENTS]):
356+
elif model_parameters is not None: # Use user-defined model parameters
357+
# Overwrite components, to later iterate over them,
358+
# in case user wants to specify their components
359+
irrad_components = model_parameters.keys()
360+
# Validate 'model_parameters' sub-dicts keys
361+
if any([{'a', 'b', 'c'} != set(model_parameters[component].keys())
362+
for component in irrad_components]):
365363
raise ValueError("You must specify model parameters with keys "
366364
"'a','b','c' for each irradiation component.")
367365

@@ -376,9 +374,8 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
376374

377375
modifiers = pd.Series()
378376

379-
# Order of irradiations shouldn't be changed
380377
# Values are tested in return order in test_martin_ruiz_spectral_modifier
381-
for irrad_type in IRRAD_COMPONENTS:
378+
for irrad_type in irrad_components:
382379
_coeffs = _params[irrad_type]
383380

384381
modifier = _coeffs['c'] * np.exp(_coeffs['a'] * kt_delta

pvlib/tests/test_spectrum.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -216,26 +216,23 @@ def test_martin_ruiz_spectral_modifier():
216216
assert_series_equal(result['ground_diffuse'], expected[2], atol=1e-5)
217217

218218
# test results when giving 'model_parameters' as DataFrame
219+
# test custom quantity of components and its names can be given
219220
clearness_index = np.array([0.56, 0.612, 0.664, 0.716, 0.768, 0.82])
220221
airmass_absolute = np.array([2, 1.8, 1.6, 1.4, 1.2, 1])
221222
model_parameters = pd.DataFrame({ # monosi values
222223
'direct': [1.029, -0.313, 0.00524],
223-
'sky diffuse': [0.764, -0.882, -0.0204],
224-
'ground diffuse': [0.97, -0.244, 0.0129]},
224+
'diffuse_sky': [0.764, -0.882, -0.0204]},
225225
index=('c', 'a', 'b'))
226226
expected = ( # Direct / Sky diffuse / Ground diffuse
227227
np.array([1.09149, 1.07274, 1.05432, 1.03621, 1.01841, 1.00092]),
228-
np.array([0.88636, 0.85009, 0.81530, 0.78193, 0.74993, 0.71924]),
229-
np.array([1.02011, 1.00465, 0.98943, 0.97443, 0.95967, 0.94513]))
228+
np.array([0.88636, 0.85009, 0.81530, 0.78193, 0.74993, 0.71924]))
230229

231230
result = spectrum.martin_ruiz_spectral_modifier(
232231
clearness_index,
233232
airmass_absolute,
234-
cell_type='asi',
235233
model_parameters=model_parameters)
236234
assert_allclose(result['direct'], expected[0], atol=1e-5)
237-
assert_allclose(result['sky_diffuse'], expected[1], atol=1e-5)
238-
assert_allclose(result['ground_diffuse'], expected[2], atol=1e-5)
235+
assert_allclose(result['diffuse_sky'], expected[1], atol=1e-5)
239236

240237
# test warning is raised with both 'cell_type' and 'model_parameters'
241238
# test results when giving 'model_parameters' as dict
@@ -279,22 +276,9 @@ def test_martin_ruiz_spectral_modifier_errors():
279276
'or "model_parameters" as arguments!'):
280277
_ = spectrum.martin_ruiz_spectral_modifier(clearness_index,
281278
airmass_absolute)
282-
# tests for inadequate "model_parameters"
279+
# test for error in params keys
283280
clearness_index = 0.74
284281
airmass_absolute = 1.5
285-
# test for error on irradiances keys
286-
model_parameters = {
287-
'direct': {'c': 1.029, 'a': -3.13e-1, 'b': 5.24e-3},
288-
'sky_diffuse': {'c': 0.764, 'a': -8.82e-1, 'b': -2.04e-2},
289-
':(': {'c': 0.970, 'a': -2.44e-1, 'b': 1.29e-2}}
290-
with pytest.raises(ValueError,
291-
match='You must specify model parameters for exact '
292-
'irradiance components '):
293-
_ = spectrum.martin_ruiz_spectral_modifier(
294-
clearness_index,
295-
airmass_absolute,
296-
model_parameters=model_parameters)
297-
# test for error on params keys
298282
model_parameters = {
299283
'direct': {'c': 1.029, 'a': -3.13e-1, 'b': 5.24e-3},
300284
'sky_diffuse': {'c': 0.764, 'a': -8.82e-1, 'b': -2.04e-2},

0 commit comments

Comments
 (0)