Skip to content

refactor ModelChain._prep_inputs_solar_pos for wx in poa solpos #1140

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 4 commits into from
Jan 22, 2021
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
3 changes: 3 additions & 0 deletions docs/sphinx/source/whatsnew/v0.9.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ Enhancements

Bug fixes
~~~~~~~~~
* Pass weather data to solar position calculations in
:ref:meth:`~pvlib.modelchain.ModelChain.prepare_inputs_from_poa`.
(:issue:`1065`, :pull:`1140`)

Testing
~~~~~~~
Expand Down
24 changes: 12 additions & 12 deletions pvlib/modelchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1210,10 +1210,19 @@ def _complete_irradiance(self, weather):
weather.ghi - weather.dni *
tools.cosd(self.results.solar_position.zenith))

def _prep_inputs_solar_pos(self, kwargs={}):
def _prep_inputs_solar_pos(self, weather):
"""
Assign solar position
"""
# build weather kwargs for solar position calculation
kwargs = _build_kwargs(['pressure', 'temp_air'],
weather[0] if isinstance(weather, tuple)
else weather)
try:
kwargs['temperature'] = kwargs.pop('temp_air')
except KeyError:
pass

self.results.solar_position = self.location.get_solarposition(
self.times, method=self.solar_position_method,
**kwargs)
Expand Down Expand Up @@ -1363,16 +1372,7 @@ def prepare_inputs(self, weather):
self._assign_weather(weather)
self._assign_times()

# build kwargs for solar position calculation
try:
press_temp = _build_kwargs(['pressure', 'temp_air'],
weather[0] if isinstance(weather, tuple)
else weather)
press_temp['temperature'] = press_temp.pop('temp_air')
except KeyError:
pass

self._prep_inputs_solar_pos(press_temp)
self._prep_inputs_solar_pos(weather)
self._prep_inputs_airmass()

# PVSystem.get_irradiance and SingleAxisTracker.get_irradiance
Expand Down Expand Up @@ -1470,7 +1470,7 @@ def prepare_inputs_from_poa(self, data):
'poa_diffuse'])
self._assign_total_irrad(data)

self._prep_inputs_solar_pos()
self._prep_inputs_solar_pos(data)
self._prep_inputs_airmass()

if isinstance(self.system, SingleAxisTracker):
Expand Down
32 changes: 32 additions & 0 deletions pvlib/tests/test_modelchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,20 @@ def test_temperature_models_arrays_multi_weather(
!= mc.results.cell_temperature[1]).all()


def test_run_model_solar_position_weather(
pvwatts_dc_pvwatts_ac_system, location, weather, mocker):
mc = ModelChain(pvwatts_dc_pvwatts_ac_system, location,
aoi_model='no_loss', spectral_model='no_loss')
weather['pressure'] = 90000
weather['temp_air'] = 25
m = mocker.spy(location, 'get_solarposition')
mc.run_model(weather)
# assert_called_once_with cannot be used with series, so need to use
# assert_series_equal on call_args
assert_series_equal(m.call_args[1]['temperature'], weather['temp_air'])
assert_series_equal(m.call_args[1]['pressure'], weather['pressure'])


def test_run_model_from_poa(sapm_dc_snl_ac_system, location, total_irrad):
mc = ModelChain(sapm_dc_snl_ac_system, location, aoi_model='no_loss',
spectral_model='no_loss')
Expand All @@ -909,6 +923,24 @@ def test_run_model_from_poa_arrays(sapm_dc_snl_ac_system_Array, location,
assert_frame_equal(mc.results.dc[0], mc.results.dc[1])


def test_run_model_from_poa_arrays_solar_position_weather(
sapm_dc_snl_ac_system_Array, location, weather, total_irrad, mocker):
data = weather.copy()
data[['poa_global', 'poa_diffuse', 'poa_direct']] = total_irrad
data['pressure'] = 90000
data['temp_air'] = 25
data2 = data.copy()
data2['pressure'] = 95000
data2['temp_air'] = 30
mc = ModelChain(sapm_dc_snl_ac_system_Array, location, aoi_model='no_loss',
spectral_model='no_loss')
m = mocker.spy(location, 'get_solarposition')
mc.run_model_from_poa((data, data2))
# mc uses only the first weather data for solar position corrections
assert_series_equal(m.call_args[1]['temperature'], data['temp_air'])
assert_series_equal(m.call_args[1]['pressure'], data['pressure'])


def test_run_model_from_poa_tracking(sapm_dc_snl_ac_system, location,
total_irrad):
system = SingleAxisTracker(
Expand Down