Skip to content

refactor repeated code in ModelChain singlediode and LocalizedPVSystem init #777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 8 additions & 35 deletions pvlib/modelchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"""
Expand Down
42 changes: 26 additions & 16 deletions pvlib/pvsystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 6 additions & 16 deletions pvlib/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down