Skip to content

Commit 3c84edd

Browse files
Pequewholmgren
authored andcommitted
refactor repeated code in ModelChain singlediode and LocalizedPVSystem init (#777)
* Remove duplicate code in ModelChain * Remove duplicate code in init methods
1 parent 678cead commit 3c84edd

File tree

3 files changed

+40
-67
lines changed

3 files changed

+40
-67
lines changed

pvlib/modelchain.py

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -453,11 +453,11 @@ def sapm(self):
453453

454454
return self
455455

456-
def desoto(self):
456+
def _singlediode(self, calcparams_model_function):
457457
(photocurrent, saturation_current, resistance_series,
458458
resistance_shunt, nNsVth) = (
459-
self.system.calcparams_desoto(self.effective_irradiance,
460-
self.cell_temperature))
459+
calcparams_model_function(self.effective_irradiance,
460+
self.cell_temperature))
461461

462462
self.diode_params = (photocurrent, saturation_current,
463463
resistance_series,
@@ -471,41 +471,14 @@ def desoto(self):
471471

472472
return self
473473

474-
def cec(self):
475-
(photocurrent, saturation_current, resistance_series,
476-
resistance_shunt, nNsVth) = (
477-
self.system.calcparams_cec(self.effective_irradiance,
478-
self.cell_temperature))
479-
480-
self.diode_params = (photocurrent, saturation_current,
481-
resistance_series,
482-
resistance_shunt, nNsVth)
483-
484-
self.dc = self.system.singlediode(
485-
photocurrent, saturation_current, resistance_series,
486-
resistance_shunt, nNsVth)
487-
488-
self.dc = self.system.scale_voltage_current_power(self.dc).fillna(0)
474+
def desoto(self):
475+
return self._singlediode(self.system.calcparams_desoto)
489476

490-
return self
477+
def cec(self):
478+
return self._singlediode(self.system.calcparams_cec)
491479

492480
def pvsyst(self):
493-
(photocurrent, saturation_current, resistance_series,
494-
resistance_shunt, nNsVth) = (
495-
self.system.calcparams_pvsyst(self.effective_irradiance,
496-
self.cell_temperature))
497-
498-
self.diode_params = (photocurrent, saturation_current,
499-
resistance_series,
500-
resistance_shunt, nNsVth)
501-
502-
self.dc = self.system.singlediode(
503-
photocurrent, saturation_current, resistance_series,
504-
resistance_shunt, nNsVth)
505-
506-
self.dc = self.system.scale_voltage_current_power(self.dc).fillna(0)
507-
508-
return self
481+
return self._singlediode(self.system.calcparams_pvsyst)
509482

510483
def singlediode(self):
511484
"""Deprecated"""

pvlib/pvsystem.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@
4545
}
4646

4747

48+
def _combine_localized_attributes(pvsystem=None, location=None, **kwargs):
49+
"""
50+
Get and combine attributes from the pvsystem and/or location
51+
with the rest of the kwargs.
52+
"""
53+
if pvsystem is not None:
54+
pv_dict = pvsystem.__dict__
55+
else:
56+
pv_dict = {}
57+
58+
if location is not None:
59+
loc_dict = location.__dict__
60+
else:
61+
loc_dict = {}
62+
63+
new_kwargs = dict(
64+
list(pv_dict.items()) + list(loc_dict.items()) + list(kwargs.items())
65+
)
66+
return new_kwargs
67+
68+
4869
# not sure if this belongs in the pvsystem module.
4970
# maybe something more like core.py? It may eventually grow to
5071
# import a lot more functionality from other modules.
@@ -830,22 +851,11 @@ class LocalizedPVSystem(PVSystem, Location):
830851
"""
831852
def __init__(self, pvsystem=None, location=None, **kwargs):
832853

833-
# get and combine attributes from the pvsystem and/or location
834-
# with the rest of the kwargs
835-
836-
if pvsystem is not None:
837-
pv_dict = pvsystem.__dict__
838-
else:
839-
pv_dict = {}
840-
841-
if location is not None:
842-
loc_dict = location.__dict__
843-
else:
844-
loc_dict = {}
845-
846-
new_kwargs = dict(list(pv_dict.items()) +
847-
list(loc_dict.items()) +
848-
list(kwargs.items()))
854+
new_kwargs = _combine_localized_attributes(
855+
pvsystem=pvsystem,
856+
location=location,
857+
**kwargs,
858+
)
849859

850860
PVSystem.__init__(self, **new_kwargs)
851861
Location.__init__(self, **new_kwargs)

pvlib/tracking.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pandas as pd
33

44
from pvlib.tools import cosd, sind
5+
from pvlib.pvsystem import _combine_localized_attributes
56
from pvlib.pvsystem import PVSystem
67
from pvlib.location import Location
78
from pvlib import irradiance, atmosphere
@@ -227,22 +228,11 @@ class LocalizedSingleAxisTracker(SingleAxisTracker, Location):
227228

228229
def __init__(self, pvsystem=None, location=None, **kwargs):
229230

230-
# get and combine attributes from the pvsystem and/or location
231-
# with the rest of the kwargs
232-
233-
if pvsystem is not None:
234-
pv_dict = pvsystem.__dict__
235-
else:
236-
pv_dict = {}
237-
238-
if location is not None:
239-
loc_dict = location.__dict__
240-
else:
241-
loc_dict = {}
242-
243-
new_kwargs = dict(list(pv_dict.items()) +
244-
list(loc_dict.items()) +
245-
list(kwargs.items()))
231+
new_kwargs = _combine_localized_attributes(
232+
pvsystem=pvsystem,
233+
location=location,
234+
**kwargs,
235+
)
246236

247237
SingleAxisTracker.__init__(self, **new_kwargs)
248238
Location.__init__(self, **new_kwargs)

0 commit comments

Comments
 (0)