Skip to content

Commit 08bfe62

Browse files
echedey-lscwhanse
andcommitted
cwhanse review
Co-Authored-By: Cliff Hansen <[email protected]>
1 parent 6121759 commit 08bfe62

File tree

1 file changed

+32
-39
lines changed

1 file changed

+32
-39
lines changed

pvlib/modelchain.py

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -330,10 +330,8 @@ class ModelChain:
330330
'interp' and 'no_loss'. The ModelChain instance will be passed as the
331331
first argument to a user-defined function.
332332
333-
spectral_model : str or function, optional
334-
If not specified, the model will be inferred from the parameters that
335-
are common to all of system.arrays[i].module_parameters. Valid strings
336-
are:
333+
spectral_model : str or function, default ``'no_loss'``
334+
Valid strings are:
337335
338336
- ``'sapm'``
339337
- ``'first_solar'``
@@ -342,6 +340,9 @@ class ModelChain:
342340
The ModelChain instance will be passed as the first argument to
343341
a user-defined function.
344342
343+
See :py:func:`~pvlib.modelchain.ModelChain.infer_spectral_model` to
344+
infer the spectral model from system and weather information.
345+
345346
temperature_model : str or function, optional
346347
Valid strings are: 'sapm', 'pvsyst', 'faiman', 'fuentes', 'noct_sam'.
347348
The ModelChain instance will be passed as the first argument to a
@@ -870,14 +871,30 @@ def spectral_model(self, model):
870871
self._spectral_model = self.no_spectral_loss
871872
else:
872873
raise ValueError(model + ' is not a valid spectral loss model')
873-
elif model is None:
874-
# None is a valid value, model inference will be done later
875-
self._spectral_model = None
876874
else:
877875
self._spectral_model = partial(model, self)
878876

879-
def infer_spectral_model(self):
880-
"""Infer spectral model from system attributes."""
877+
def infer_spectral_model(self, weather=None):
878+
"""
879+
Infer spectral model from system attributes, and optionally from
880+
the input weather dataframe, and set it to the ``ModelChain`` instance.
881+
882+
Parameters
883+
----------
884+
weather : pd.DataFrame or collection of str, optional
885+
An object with columns of available input data to help infer
886+
the spectral model. If ``None``, the spectral model will be
887+
inferred from the system attributes only.
888+
889+
Returns
890+
-------
891+
Inferred spectral correction model : function
892+
893+
Examples
894+
--------
895+
>>> mc = ModelChain(system, location)
896+
>>> mc.spectral_model = mc.infer_spectral_model(weather=weather)
897+
"""
881898
module_parameters = tuple(
882899
array.module_parameters for array in self.system.arrays)
883900
params = _common_keys(module_parameters)
@@ -892,37 +909,13 @@ def infer_spectral_model(self):
892909
and (self.system._infer_cell_type() is not None)
893910
):
894911
# This suggests models that provide default parameters per cell
895-
# type can be used. However, some of them depend on other weather
912+
# type can be used. However, some models depend on other weather
896913
# parameters, so we need to check if they are available.
897-
# At this point, weather may not be available, so let's take that
898-
# into account.
899-
if self.results is not None and self.results.weather is not None:
900-
# weather is available
901-
if "precipitable_water" in self.results.weather:
914+
if weather is not None: # weather is available
915+
if "precipitable_water" in weather:
902916
return self.first_solar_spectral_loss
903-
else:
904-
return self.no_spectral_loss
905-
else:
906-
# weather is not available, so it's unknown what model to use.
907-
# Warn user about this and default to no_spectral_loss.
908-
warnings.warn(
909-
"Weather data is not available to infer spectral model. "
910-
"Defaulting to 'no_loss'. Explicitly set the "
911-
"spectral model with the 'spectral_model' kwarg to "
912-
"suppress this warning."
913-
)
914-
return self.no_spectral_loss
915-
else:
916-
raise ValueError(
917-
"could not infer spectral model from "
918-
"system.arrays[i].module_parameters. Check that "
919-
"the module_parameters for all Arrays in "
920-
"system.arrays contain valid "
921-
"valid spectral coefficients, a valid "
922-
"Material or Technology value, or a valid model "
923-
"string; explicitly set the model with the "
924-
"'spectral_model' kwarg."
925-
)
917+
918+
return self.no_spectral_loss
926919

927920
def first_solar_spectral_loss(self):
928921
self.results.spectral_modifier = self.system.first_solar_spectral_loss(
@@ -1715,7 +1708,7 @@ def run_model(self, weather):
17151708
# spectral model is optional, and weather may impact model inference
17161709
# check if spectral model inference is requested by means of "None"
17171710
if self._spectral_model is None:
1718-
self._spectral_model = self.infer_spectral_model()
1711+
self._spectral_model = self.infer_spectral_model(weather=weather)
17191712
self.spectral_model()
17201713

17211714
self.effective_irradiance_model()

0 commit comments

Comments
 (0)