From 24cc8c1db8790b05c21dbe1428d4c710d09bcc87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20S=C3=A1nchez=20de=20Le=C3=B3n=20Peque?= Date: Sun, 1 Sep 2019 00:17:15 +0200 Subject: [PATCH 1/2] Remove duplicate code in ModelChain --- pvlib/modelchain.py | 43 ++++++++----------------------------------- 1 file changed, 8 insertions(+), 35 deletions(-) diff --git a/pvlib/modelchain.py b/pvlib/modelchain.py index 3244dc2977..92a92e2abc 100644 --- a/pvlib/modelchain.py +++ b/pvlib/modelchain.py @@ -453,11 +453,11 @@ def sapm(self): return self - def desoto(self): + def _singlediode(self, calcparams_model_function): (photocurrent, saturation_current, resistance_series, resistance_shunt, nNsVth) = ( - self.system.calcparams_desoto(self.effective_irradiance, - self.cell_temperature)) + calcparams_model_function(self.effective_irradiance, + self.cell_temperature)) self.diode_params = (photocurrent, saturation_current, resistance_series, @@ -471,41 +471,14 @@ def desoto(self): return self - def cec(self): - (photocurrent, saturation_current, resistance_series, - resistance_shunt, nNsVth) = ( - self.system.calcparams_cec(self.effective_irradiance, - self.cell_temperature)) - - self.diode_params = (photocurrent, saturation_current, - resistance_series, - resistance_shunt, nNsVth) - - self.dc = self.system.singlediode( - photocurrent, saturation_current, resistance_series, - resistance_shunt, nNsVth) - - self.dc = self.system.scale_voltage_current_power(self.dc).fillna(0) + def desoto(self): + return self._singlediode(self.system.calcparams_desoto) - return self + def cec(self): + return self._singlediode(self.system.calcparams_cec) def pvsyst(self): - (photocurrent, saturation_current, resistance_series, - resistance_shunt, nNsVth) = ( - self.system.calcparams_pvsyst(self.effective_irradiance, - self.cell_temperature)) - - self.diode_params = (photocurrent, saturation_current, - resistance_series, - resistance_shunt, nNsVth) - - self.dc = self.system.singlediode( - photocurrent, saturation_current, resistance_series, - resistance_shunt, nNsVth) - - self.dc = self.system.scale_voltage_current_power(self.dc).fillna(0) - - return self + return self._singlediode(self.system.calcparams_pvsyst) def singlediode(self): """Deprecated""" From f07b4c2b003de40f0f771cf9fee04453005c710d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20S=C3=A1nchez=20de=20Le=C3=B3n=20Peque?= Date: Sun, 1 Sep 2019 00:17:54 +0200 Subject: [PATCH 2/2] Remove duplicate code in init methods --- pvlib/pvsystem.py | 42 ++++++++++++++++++++++++++---------------- pvlib/tracking.py | 22 ++++++---------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/pvlib/pvsystem.py b/pvlib/pvsystem.py index 5f429417fe..bebe2f1263 100644 --- a/pvlib/pvsystem.py +++ b/pvlib/pvsystem.py @@ -45,6 +45,27 @@ } +def _combine_localized_attributes(pvsystem=None, location=None, **kwargs): + """ + Get and combine attributes from the pvsystem and/or location + with the rest of the kwargs. + """ + if pvsystem is not None: + pv_dict = pvsystem.__dict__ + else: + pv_dict = {} + + if location is not None: + loc_dict = location.__dict__ + else: + loc_dict = {} + + new_kwargs = dict( + list(pv_dict.items()) + list(loc_dict.items()) + list(kwargs.items()) + ) + return new_kwargs + + # not sure if this belongs in the pvsystem module. # maybe something more like core.py? It may eventually grow to # import a lot more functionality from other modules. @@ -830,22 +851,11 @@ class LocalizedPVSystem(PVSystem, Location): """ def __init__(self, pvsystem=None, location=None, **kwargs): - # get and combine attributes from the pvsystem and/or location - # with the rest of the kwargs - - if pvsystem is not None: - pv_dict = pvsystem.__dict__ - else: - pv_dict = {} - - if location is not None: - loc_dict = location.__dict__ - else: - loc_dict = {} - - new_kwargs = dict(list(pv_dict.items()) + - list(loc_dict.items()) + - list(kwargs.items())) + new_kwargs = _combine_localized_attributes( + pvsystem=pvsystem, + location=location, + **kwargs, + ) PVSystem.__init__(self, **new_kwargs) Location.__init__(self, **new_kwargs) diff --git a/pvlib/tracking.py b/pvlib/tracking.py index 9859022bc4..40160cada8 100644 --- a/pvlib/tracking.py +++ b/pvlib/tracking.py @@ -2,6 +2,7 @@ import pandas as pd from pvlib.tools import cosd, sind +from pvlib.pvsystem import _combine_localized_attributes from pvlib.pvsystem import PVSystem from pvlib.location import Location from pvlib import irradiance, atmosphere @@ -227,22 +228,11 @@ class LocalizedSingleAxisTracker(SingleAxisTracker, Location): def __init__(self, pvsystem=None, location=None, **kwargs): - # get and combine attributes from the pvsystem and/or location - # with the rest of the kwargs - - if pvsystem is not None: - pv_dict = pvsystem.__dict__ - else: - pv_dict = {} - - if location is not None: - loc_dict = location.__dict__ - else: - loc_dict = {} - - new_kwargs = dict(list(pv_dict.items()) + - list(loc_dict.items()) + - list(kwargs.items())) + new_kwargs = _combine_localized_attributes( + pvsystem=pvsystem, + location=location, + **kwargs, + ) SingleAxisTracker.__init__(self, **new_kwargs) Location.__init__(self, **new_kwargs)