Skip to content

move ModelChain.weather and times to ModelChainResult #1197

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 7 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions docs/sphinx/source/whatsnew/v0.9.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ Deprecations
* ``ModelChain.spectral_modifier``
* ``ModelChain.total_irrad``
* ``ModelChain.tracking``
* ``ModelChain.weather``
* ``ModelChain.times``

Enhancements
~~~~~~~~~~~~
Expand Down
88 changes: 50 additions & 38 deletions pvlib/modelchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ class ModelChainResult:
_singleton_tuples: bool = field(default=False)
_per_array_fields = {'total_irrad', 'aoi', 'aoi_modifier',
'spectral_modifier', 'cell_temperature',
'effective_irradiance', 'dc', 'diode_params'}
'effective_irradiance', 'dc', 'diode_params',
'weather'}

# system-level information
solar_position: Optional[pd.DataFrame] = field(default=None)
Expand All @@ -276,6 +277,9 @@ class ModelChainResult:
field(default=None)
diode_params: Optional[PerArray[pd.DataFrame]] = field(default=None)

weather: Optional[PerArray[pd.DataFrame]] = None
times: Optional[pd.DatetimeIndex] = None

def _result_type(self, value):
"""Coerce `value` to the correct type according to
``self._singleton_tuples``."""
Expand Down Expand Up @@ -368,7 +372,8 @@ class ModelChain:
_deprecated_attrs = ['solar_position', 'airmass', 'total_irrad',
'aoi', 'aoi_modifier', 'spectral_modifier',
'cell_temperature', 'effective_irradiance',
'dc', 'ac', 'diode_params', 'tracking']
'dc', 'ac', 'diode_params', 'tracking',
'weather', 'times']

def __init__(self, system, location,
clearsky_model='ineichen',
Expand Down Expand Up @@ -397,9 +402,6 @@ def __init__(self, system, location,

self.losses_model = losses_model

self.weather = None
self.times = None

self.results = ModelChainResult()

def __getattr__(self, key):
Expand Down Expand Up @@ -899,7 +901,7 @@ def infer_spectral_model(self):

def first_solar_spectral_loss(self):
self.results.spectral_modifier = self.system.first_solar_spectral_loss(
_tuple_from_dfs(self.weather, 'precipitable_water'),
_tuple_from_dfs(self.results.weather, 'precipitable_water'),
self.results.airmass['airmass_absolute']
)
return self
Expand Down Expand Up @@ -992,8 +994,8 @@ def _set_celltemp(self, model):

poa = _irrad_for_celltemp(self.results.total_irrad,
self.results.effective_irradiance)
temp_air = _tuple_from_dfs(self.weather, 'temp_air')
wind_speed = _tuple_from_dfs(self.weather, 'wind_speed')
temp_air = _tuple_from_dfs(self.results.weather, 'temp_air')
wind_speed = _tuple_from_dfs(self.results.weather, 'wind_speed')
self.results.cell_temperature = model(poa, temp_air, wind_speed)
return self

Expand Down Expand Up @@ -1121,16 +1123,16 @@ def complete_irradiance(self, weather):
self._check_multiple_input(weather)
# Don't use ModelChain._assign_weather() here because it adds
# temperature and wind-speed columns which we do not need here.
self.weather = _copy(weather)
self.results.weather = _copy(weather)
self._assign_times()
self.results.solar_position = self.location.get_solarposition(
self.times, method=self.solar_position_method)
self.results.times, method=self.solar_position_method)

if isinstance(weather, tuple):
for w in self.weather:
for w in self.results.weather:
self._complete_irradiance(w)
else:
self._complete_irradiance(self.weather)
self._complete_irradiance(self.results.weather)

return self

Expand Down Expand Up @@ -1175,7 +1177,7 @@ def _prep_inputs_solar_pos(self, weather):
pass

self.results.solar_position = self.location.get_solarposition(
self.times, method=self.solar_position_method,
self.results.times, method=self.solar_position_method,
**kwargs)
return self

Expand Down Expand Up @@ -1238,15 +1240,23 @@ def _verify(data, index=None):
for (i, array_data) in enumerate(data):
_verify(array_data, i)

def _configure_results(self):
"""Configure the type used for per-array fields in ModelChainResult.
def _configure_results(self, per_array_data):
"""Configure the type used for per-array fields in
ModelChainResult.

If ``per_array_data`` is True and the number of arrays in the
system is 1, then per-array results are stored as length-1
tuples. This overrides the PVSystem defaults of unpacking a 1
length tuple into a singleton.

Must be called after ``self.weather`` has been assigned. If
``self.weather`` is a tuple and the number of arrays in the system
is 1, then per-array results are stored as length-1 tuples.
Parameters
----------
per_array_data : bool
If input data is provided for each array, pass True. If a
single input data is provided for all arrays, pass False.
"""
self.results._singleton_tuples = (
self.system.num_arrays == 1 and isinstance(self.weather, tuple)
self.system.num_arrays == 1 and per_array_data
)

def _assign_weather(self, data):
Expand All @@ -1258,13 +1268,13 @@ def _build_weather(data):
if weather.get('temp_air') is None:
weather['temp_air'] = 20
return weather
if not isinstance(data, tuple):
self.weather = _build_weather(data)
if isinstance(data, tuple):
weather = tuple(_build_weather(wx) for wx in data)
self._configure_results(per_array_data=True)
else:
self.weather = tuple(
_build_weather(weather) for weather in data
)
self._configure_results()
weather = _build_weather(data)
self._configure_results(per_array_data=False)
self.results.weather = weather
self._assign_times()
return self

Expand All @@ -1281,18 +1291,20 @@ def _build_irrad(data):
return self

def _assign_times(self):
"""Assign self.times according the the index of self.weather.

If there are multiple DataFrames in self.weather then the index
of the first one is assigned. It is assumed that the indices of
each data frame in `weather` are the same. This can be verified
by calling :py:func:`_all_same_index` or
:py:meth:`self._check_multiple_weather` before calling this method.
"""Assign self.results.times according the the index of
self.results.weather.

If there are multiple DataFrames in self.results.weather then
the index of the first one is assigned. It is assumed that the
indices of each DataFrame in self.results.weather are the same.
This can be verified by calling :py:func:`_all_same_index` or
:py:meth:`self._check_multiple_weather` before calling this
method.
"""
if isinstance(self.weather, tuple):
self.times = self.weather[0].index
if isinstance(self.results.weather, tuple):
self.results.times = self.results.weather[0].index
else:
self.times = self.weather.index
self.results.times = self.results.weather.index

def prepare_inputs(self, weather):
"""
Expand Down Expand Up @@ -1358,9 +1370,9 @@ def prepare_inputs(self, weather):
self.results.solar_position['azimuth'])

self.results.total_irrad = get_irradiance(
_tuple_from_dfs(self.weather, 'dni'),
_tuple_from_dfs(self.weather, 'ghi'),
_tuple_from_dfs(self.weather, 'dhi'),
_tuple_from_dfs(self.results.weather, 'dni'),
_tuple_from_dfs(self.results.weather, 'ghi'),
_tuple_from_dfs(self.results.weather, 'dhi'),
airmass=self.results.airmass['airmass_relative'],
model=self.transposition_model
)
Expand Down
41 changes: 23 additions & 18 deletions pvlib/tests/test_modelchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,10 +557,10 @@ def test_ModelChain_times_arrays(sapm_dc_snl_ac_system_Array, location):
weather_one = pd.DataFrame(irradiance_one, index=times)
weather_two = pd.DataFrame(irradiance_two, index=times)
mc.prepare_inputs((weather_one, weather_two))
assert mc.times.equals(times)
assert mc.results.times.equals(times)
mc = ModelChain(sapm_dc_snl_ac_system_Array, location)
mc.prepare_inputs(weather_one)
assert mc.times.equals(times)
assert mc.results.times.equals(times)


@pytest.mark.parametrize("missing", ['dhi', 'ghi', 'dni'])
Expand Down Expand Up @@ -755,7 +755,7 @@ def test_prepare_inputs_from_poa(sapm_dc_snl_ac_system, location,
weather_expected = weather_expected[
['ghi', 'dhi', 'dni', 'wind_speed', 'temp_air']]
# weather attribute
assert_frame_equal(mc.weather, weather_expected)
assert_frame_equal(mc.results.weather, weather_expected)
# total_irrad attribute
assert_frame_equal(mc.results.total_irrad, total_irrad)
assert not pd.isnull(mc.results.solar_position.index[0])
Expand Down Expand Up @@ -815,7 +815,8 @@ def test__prepare_temperature(sapm_dc_snl_ac_system, location, weather,
data[['poa_global', 'poa_diffuse', 'poa_direct']] = total_irrad
mc = ModelChain(sapm_dc_snl_ac_system, location, aoi_model='no_loss',
spectral_model='no_loss')
# prepare_temperature expects mc.total_irrad and mc.weather to be set
# prepare_temperature expects mc.total_irrad and mc.results.weather
# to be set
mc._assign_weather(data)
mc._assign_total_irrad(data)
mc._prepare_temperature(data)
Expand Down Expand Up @@ -870,7 +871,8 @@ def test__prepare_temperature_arrays_weather(sapm_dc_snl_ac_system_same_arrays,
data_two = data.copy()
mc = ModelChain(sapm_dc_snl_ac_system_same_arrays, location,
aoi_model='no_loss', spectral_model='no_loss')
# prepare_temperature expects mc.total_irrad and mc.weather to be set
# prepare_temperature expects mc.total_irrad and mc.results.weather
# to be set
mc._assign_weather((data, data_two))
mc._assign_total_irrad((data, data_two))
mc._prepare_temperature((data, data_two))
Expand Down Expand Up @@ -1126,6 +1128,7 @@ def test_run_model_singleton_weather_single_array(cec_dc_snl_ac_system,
mc = ModelChain(cec_dc_snl_ac_system, location,
aoi_model="no_loss", spectral_model="no_loss")
mc.run_model([weather])
assert isinstance(mc.results.weather, tuple)
assert isinstance(mc.results.total_irrad, tuple)
assert isinstance(mc.results.aoi, tuple)
assert isinstance(mc.results.aoi_modifier, tuple)
Expand All @@ -1144,6 +1147,7 @@ def test_run_model_from_poa_singleton_weather_single_array(
ac = mc.run_model_from_poa([total_irrad]).results.ac
expected = pd.Series(np.array([149.280238, 96.678385]),
index=total_irrad.index)
assert isinstance(mc.results.weather, tuple)
assert isinstance(mc.results.cell_temperature, tuple)
assert len(mc.results.cell_temperature) == 1
assert isinstance(mc.results.cell_temperature[0], pd.Series)
Expand All @@ -1160,6 +1164,7 @@ def test_run_model_from_effective_irradiance_weather_single_array(
ac = mc.run_model_from_effective_irradiance([data]).results.ac
expected = pd.Series(np.array([149.280238, 96.678385]),
index=data.index)
assert isinstance(mc.results.weather, tuple)
assert isinstance(mc.results.cell_temperature, tuple)
assert len(mc.results.cell_temperature) == 1
assert isinstance(mc.results.cell_temperature[0], pd.Series)
Expand Down Expand Up @@ -1697,11 +1702,11 @@ def test_complete_irradiance_clean_run(sapm_dc_snl_ac_system, location):

mc.complete_irradiance(i)

assert_series_equal(mc.weather['dni'],
assert_series_equal(mc.results.weather['dni'],
pd.Series([2, 3], index=times, name='dni'))
assert_series_equal(mc.weather['dhi'],
assert_series_equal(mc.results.weather['dhi'],
pd.Series([4, 6], index=times, name='dhi'))
assert_series_equal(mc.weather['ghi'],
assert_series_equal(mc.results.weather['ghi'],
pd.Series([9, 5], index=times, name='ghi'))


Expand All @@ -1716,18 +1721,18 @@ def test_complete_irradiance(sapm_dc_snl_ac_system, location):

with pytest.warns(UserWarning):
mc.complete_irradiance(i[['ghi', 'dni']])
assert_series_equal(mc.weather['dhi'],
assert_series_equal(mc.results.weather['dhi'],
pd.Series([356.543700, 465.44400],
index=times, name='dhi'))

with pytest.warns(UserWarning):
mc.complete_irradiance(i[['dhi', 'dni']])
assert_series_equal(mc.weather['ghi'],
assert_series_equal(mc.results.weather['ghi'],
pd.Series([372.103976116, 497.087579068],
index=times, name='ghi'))

mc.complete_irradiance(i[['dhi', 'ghi']])
assert_series_equal(mc.weather['dni'],
assert_series_equal(mc.results.weather['dni'],
pd.Series([49.756966, 62.153947],
index=times, name='dni'))

Expand All @@ -1748,7 +1753,7 @@ def test_complete_irradiance_arrays(
match=r"Input DataFrames must have same index\."):
mc.complete_irradiance(input_type((weather, weather[1:])))
mc.complete_irradiance(input_type((weather, weather)))
for mc_weather in mc.weather:
for mc_weather in mc.results.weather:
assert_series_equal(mc_weather['dni'],
pd.Series([2, 3], index=times, name='dni'))
assert_series_equal(mc_weather['dhi'],
Expand All @@ -1758,16 +1763,16 @@ def test_complete_irradiance_arrays(
mc = ModelChain(sapm_dc_snl_ac_system_same_arrays, location)
mc.complete_irradiance(input_type((weather[['ghi', 'dhi']],
weather[['dhi', 'dni']])))
assert 'dni' in mc.weather[0].columns
assert 'ghi' in mc.weather[1].columns
assert 'dni' in mc.results.weather[0].columns
assert 'ghi' in mc.results.weather[1].columns
mc.complete_irradiance(input_type((weather, weather[['ghi', 'dni']])))
assert_series_equal(mc.weather[0]['dhi'],
assert_series_equal(mc.results.weather[0]['dhi'],
pd.Series([4, 6], index=times, name='dhi'))
assert_series_equal(mc.weather[0]['ghi'],
assert_series_equal(mc.results.weather[0]['ghi'],
pd.Series([9, 5], index=times, name='ghi'))
assert_series_equal(mc.weather[0]['dni'],
assert_series_equal(mc.results.weather[0]['dni'],
pd.Series([2, 3], index=times, name='dni'))
assert 'dhi' in mc.weather[1].columns
assert 'dhi' in mc.results.weather[1].columns


@pytest.mark.parametrize("input_type", [tuple, list])
Expand Down