Skip to content

Commit 641e096

Browse files
authored
refactor ModelChain._prep_inputs_solar_pos for wx in poa solpos (#1140)
* refactor ModelChain._prep_inputs_solar_pos for wx in poa solpos * pr number * add tests * compat with py 3.6 and 3.7
1 parent 6b92d21 commit 641e096

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

docs/sphinx/source/whatsnew/v0.9.0.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ Enhancements
9090

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

9497
Testing
9598
~~~~~~~

pvlib/modelchain.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,10 +1210,19 @@ def _complete_irradiance(self, weather):
12101210
weather.ghi - weather.dni *
12111211
tools.cosd(self.results.solar_position.zenith))
12121212

1213-
def _prep_inputs_solar_pos(self, kwargs={}):
1213+
def _prep_inputs_solar_pos(self, weather):
12141214
"""
12151215
Assign solar position
12161216
"""
1217+
# build weather kwargs for solar position calculation
1218+
kwargs = _build_kwargs(['pressure', 'temp_air'],
1219+
weather[0] if isinstance(weather, tuple)
1220+
else weather)
1221+
try:
1222+
kwargs['temperature'] = kwargs.pop('temp_air')
1223+
except KeyError:
1224+
pass
1225+
12171226
self.results.solar_position = self.location.get_solarposition(
12181227
self.times, method=self.solar_position_method,
12191228
**kwargs)
@@ -1363,16 +1372,7 @@ def prepare_inputs(self, weather):
13631372
self._assign_weather(weather)
13641373
self._assign_times()
13651374

1366-
# build kwargs for solar position calculation
1367-
try:
1368-
press_temp = _build_kwargs(['pressure', 'temp_air'],
1369-
weather[0] if isinstance(weather, tuple)
1370-
else weather)
1371-
press_temp['temperature'] = press_temp.pop('temp_air')
1372-
except KeyError:
1373-
pass
1374-
1375-
self._prep_inputs_solar_pos(press_temp)
1375+
self._prep_inputs_solar_pos(weather)
13761376
self._prep_inputs_airmass()
13771377

13781378
# PVSystem.get_irradiance and SingleAxisTracker.get_irradiance
@@ -1470,7 +1470,7 @@ def prepare_inputs_from_poa(self, data):
14701470
'poa_diffuse'])
14711471
self._assign_total_irrad(data)
14721472

1473-
self._prep_inputs_solar_pos()
1473+
self._prep_inputs_solar_pos(data)
14741474
self._prep_inputs_airmass()
14751475

14761476
if isinstance(self.system, SingleAxisTracker):

pvlib/tests/test_modelchain.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,20 @@ def test_temperature_models_arrays_multi_weather(
886886
!= mc.results.cell_temperature[1]).all()
887887

888888

889+
def test_run_model_solar_position_weather(
890+
pvwatts_dc_pvwatts_ac_system, location, weather, mocker):
891+
mc = ModelChain(pvwatts_dc_pvwatts_ac_system, location,
892+
aoi_model='no_loss', spectral_model='no_loss')
893+
weather['pressure'] = 90000
894+
weather['temp_air'] = 25
895+
m = mocker.spy(location, 'get_solarposition')
896+
mc.run_model(weather)
897+
# assert_called_once_with cannot be used with series, so need to use
898+
# assert_series_equal on call_args
899+
assert_series_equal(m.call_args[1]['temperature'], weather['temp_air'])
900+
assert_series_equal(m.call_args[1]['pressure'], weather['pressure'])
901+
902+
889903
def test_run_model_from_poa(sapm_dc_snl_ac_system, location, total_irrad):
890904
mc = ModelChain(sapm_dc_snl_ac_system, location, aoi_model='no_loss',
891905
spectral_model='no_loss')
@@ -909,6 +923,24 @@ def test_run_model_from_poa_arrays(sapm_dc_snl_ac_system_Array, location,
909923
assert_frame_equal(mc.results.dc[0], mc.results.dc[1])
910924

911925

926+
def test_run_model_from_poa_arrays_solar_position_weather(
927+
sapm_dc_snl_ac_system_Array, location, weather, total_irrad, mocker):
928+
data = weather.copy()
929+
data[['poa_global', 'poa_diffuse', 'poa_direct']] = total_irrad
930+
data['pressure'] = 90000
931+
data['temp_air'] = 25
932+
data2 = data.copy()
933+
data2['pressure'] = 95000
934+
data2['temp_air'] = 30
935+
mc = ModelChain(sapm_dc_snl_ac_system_Array, location, aoi_model='no_loss',
936+
spectral_model='no_loss')
937+
m = mocker.spy(location, 'get_solarposition')
938+
mc.run_model_from_poa((data, data2))
939+
# mc uses only the first weather data for solar position corrections
940+
assert_series_equal(m.call_args[1]['temperature'], data['temp_air'])
941+
assert_series_equal(m.call_args[1]['pressure'], data['pressure'])
942+
943+
912944
def test_run_model_from_poa_tracking(sapm_dc_snl_ac_system, location,
913945
total_irrad):
914946
system = SingleAxisTracker(

0 commit comments

Comments
 (0)