Skip to content

Commit 774edfe

Browse files
committed
Infer a string, key for setter
1 parent f06bbf4 commit 774edfe

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

pvlib/modelchain.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -873,8 +873,9 @@ def spectral_model(self, model):
873873
else:
874874
raise ValueError(model + ' is not a valid spectral loss model')
875875
elif model is None:
876-
self._spectral_model = self.infer_spectral_model(weather=None)
877-
else:
876+
# uses recursive setter to infer model, which returns a string
877+
self.spectral_model = self.infer_spectral_model(weather=None)
878+
else: # assume model is a callable
878879
self._spectral_model = partial(model, self)
879880

880881
def infer_spectral_model(self, weather=None):
@@ -891,21 +892,22 @@ def infer_spectral_model(self, weather=None):
891892
892893
Returns
893894
-------
894-
Inferred spectral correction model : function
895+
Inferred spectral correction model : string key for model setter
895896
896897
Examples
897898
--------
898899
>>> mc = ModelChain(system, location)
899900
>>> mc.spectral_model = mc.infer_spectral_model(weather=weather)
900901
"""
901902
module_parameters = tuple(
902-
array.module_parameters for array in self.system.arrays)
903+
array.module_parameters for array in self.system.arrays
904+
)
903905
params = _common_keys(module_parameters)
904-
if {'A4', 'A3', 'A2', 'A1', 'A0'} <= params:
905-
return self.sapm_spectral_loss
906+
if {"A4", "A3", "A2", "A1", "A0"} <= params:
907+
return "sapm"
906908
elif "first_solar_spectral_coefficients" in params:
907909
# user explicitly sets spectral coefficients
908-
return self.first_solar_spectral_loss
910+
return "first_solar"
909911
elif (
910912
# cell type is known or can be inferred
911913
("Technology" in params or "Material" in params)
@@ -916,9 +918,9 @@ def infer_spectral_model(self, weather=None):
916918
# parameters, so we need to check if they are available.
917919
if weather is not None: # weather is available
918920
if "precipitable_water" in weather:
919-
return self.first_solar_spectral_loss
921+
return "first_solar"
920922

921-
return self.no_spectral_loss
923+
return "no_loss"
922924

923925
def first_solar_spectral_loss(self):
924926
self.results.spectral_modifier = self.system.first_solar_spectral_loss(

pvlib/tests/test_modelchain.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,28 @@ def test_infer_spectral_model(location, sapm_dc_snl_ac_system,
12711271
assert isinstance(mc, ModelChain)
12721272

12731273

1274+
def test_infer_spectral_model_with_weather(location, sapm_dc_snl_ac_system,
1275+
cec_dc_snl_ac_system, weather):
1276+
# instantiate example ModelChain to get the default spectral model
1277+
# inferred without weather available by default
1278+
# - should resolve to sapm
1279+
mc = ModelChain(sapm_dc_snl_ac_system, location, aoi_model='physical')
1280+
assert mc.spectral_model == mc.sapm_spectral_loss
1281+
# - should resolve to no loss
1282+
mc = ModelChain(cec_dc_snl_ac_system, location, aoi_model='physical')
1283+
assert mc.spectral_model == mc.no_spectral_loss
1284+
1285+
# infer spectral model from weather
1286+
# - without precipitable water in it, should resolve to no loss
1287+
mc.spectral_model = mc.infer_spectral_model(weather=weather)
1288+
assert mc.spectral_model == mc.no_spectral_loss
1289+
# - with precipitable water in it, should resolve to first solar
1290+
weather['precipitable_water'] = 1.42
1291+
mc.spectral_model = mc.infer_spectral_model(weather=weather)
1292+
assert mc.spectral_model == mc.first_solar_spectral_loss
1293+
assert isinstance(mc, ModelChain)
1294+
1295+
12741296
@pytest.mark.parametrize('temp_model', [
12751297
'sapm_temp', 'faiman_temp', 'pvsyst_temp', 'fuentes_temp',
12761298
'noct_sam_temp'])

0 commit comments

Comments
 (0)