Skip to content

Commit c7799d2

Browse files
committed
Change return & model_parameters behaviour
Now, even if not given parameters for a irradiation, a result is yield with value None.
1 parent 3ea366c commit c7799d2

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

pvlib/spectrum/mismatch.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,9 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
268268
upper case. If not specified, ``model_parameters`` must be provided.
269269
270270
model_parameters : dict-like, optional
271-
In case you computed the model parameters for any specified
272-
components. Result keys will be the same as the input keys with each
273-
component in ``model_parameters``, so it can easily be used when some
274-
parameters are unknown. Provide either a dict or a ``pd.DataFrame`` as
275-
follows:
271+
In case you computed the model parameters. In case any component is not
272+
specified, result will have a ``None`` value in its corresponding key.
273+
Provide either a dict or a ``pd.DataFrame`` as follows:
276274
277275
.. code-block:: python
278276
@@ -283,22 +281,24 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
283281
'sky_diffuse': {'c': c2, 'a': a2, 'b': b2},
284282
'ground_diffuse': {'c': c3, 'a': a3, 'b': b3}
285283
}
286-
# Using a pd.DataFrame and grouping sky and ground diffuse
287-
# Yields result with 'direct' & 'diffuse' keys only
284+
# Using a pd.DataFrame
288285
model_parameters = pd.DataFrame({
289286
'direct': [c1, a1, b1],
290-
'diffuse': [c2, a2, b2]},
287+
'sky_diffuse': [c2, a2, b2],
288+
'ground_diffuse': [c3, a3, b3]},
291289
index=('c', 'a', 'b'))
292290
293291
``c``, ``a`` and ``b`` must be scalar.
294292
295293
Returns
296294
-------
297-
Mismatch modifiers : pd.Series of numeric
295+
Mismatch modifiers : pd.Series of numeric or None
298296
Modifiers for direct, sky diffuse and ground diffuse irradiances, with
299297
indexes ``direct``, ``sky_diffuse``, ``ground_diffuse``.
300298
Each mismatch modifier should be multiplied by its corresponding
301299
POA component.
300+
Returns None for a component if provided ``model_parameters`` does not
301+
include its coefficients.
302302
303303
Raises
304304
------
@@ -333,11 +333,11 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
333333
"""
334334
# Note tests for this function are prefixed with test_martin_ruiz_mm_*
335335

336-
irrad_components = ('direct', 'sky_diffuse', 'ground_diffuse')
336+
IRRAD_COMPONENTS = ('direct', 'sky_diffuse', 'ground_diffuse')
337337
# Fitting parameters directly from [1]_
338338
MARTIN_RUIZ_PARAMS = pd.DataFrame(
339339
index=('monosi', 'polysi', 'asi'),
340-
columns=pd.MultiIndex.from_product([irrad_components,
340+
columns=pd.MultiIndex.from_product([IRRAD_COMPONENTS,
341341
('c', 'a', 'b')]),
342342
data=[ # Direct(c,a,b) | Sky diffuse(c,a,b) | Ground diffuse(c,a,b)
343343
[1.029, -.313, 524e-5, .764, -.882, -.0204, .970, -.244, .0129],
@@ -359,11 +359,9 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
359359
raise TypeError('You must pass at least "cell_type" '
360360
'or "model_parameters" as arguments!')
361361
elif model_parameters is not None: # Use user-defined model parameters
362-
# Overwrite components with user-specified ones
363-
irrad_components = model_parameters.keys()
364362
# Validate 'model_parameters' sub-dicts keys
365363
if any([{'a', 'b', 'c'} != set(model_parameters[component].keys())
366-
for component in irrad_components]):
364+
for component in model_parameters.keys()]):
367365
raise ValueError("You must specify model parameters with keys "
368366
"'a','b','c' for each irradiation component.")
369367

@@ -376,12 +374,15 @@ def martin_ruiz_spectral_modifier(clearness_index, airmass_absolute,
376374
kt_delta = clearness_index - 0.74
377375
am_delta = airmass_absolute - 1.5
378376

379-
modifiers = pd.Series()
377+
modifiers = pd.Series(index=IRRAD_COMPONENTS, data=[None, None, None])
380378

381379
# Calculate mismatch modifier for each irradiation
382-
for irrad_type in irrad_components:
380+
for irrad_type in IRRAD_COMPONENTS:
381+
# Skip irradiations not specified in 'model_params'
382+
if irrad_type not in _params.keys():
383+
continue
384+
# Else, calculate the mismatch modifier
383385
_coeffs = _params[irrad_type]
384-
385386
modifier = _coeffs['c'] * np.exp(_coeffs['a'] * kt_delta
386387
+ _coeffs['b'] * am_delta)
387388
modifiers[irrad_type] = modifier

pvlib/tests/test_spectrum.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ def martin_ruiz_mismatch_data():
198198
'ground_diffuse': {'c': 0.970, 'a': -2.44e-1, 'b': 1.29e-2}},
199199
'monosi_custom_params_df': pd.DataFrame({
200200
'direct': [1.029, -0.313, 0.00524],
201-
'diffuse_sky': [0.764, -0.882, -0.0204]},
201+
'sky_diffuse': [0.764, -0.882, -0.0204]},
202202
index=('c', 'a', 'b'))
203203
}
204204
return kwargs
@@ -292,7 +292,8 @@ def test_martin_ruiz_mm_model_df(martin_ruiz_mismatch_data):
292292
airmass_absolute,
293293
model_parameters=model_parameters)
294294
assert_allclose(result['direct'], expected['dir'], atol=1e-5)
295-
assert_allclose(result['diffuse_sky'], expected['sky'], atol=1e-5)
295+
assert_allclose(result['sky_diffuse'], expected['sky'], atol=1e-5)
296+
assert_equal(result['ground_diffuse'], None)
296297

297298

298299
def test_martin_ruiz_mm_userwarning(martin_ruiz_mismatch_data):

0 commit comments

Comments
 (0)