Skip to content

Commit 3e4b86d

Browse files
authored
Location.get_rise_set_transit (#606)
* Location.get_rise_set_transit * stickler, add import statements back * add golden as argument to test * add tests for spa, geometric, change test success to dataframe structure rather than content * fix datestring in test * add all condition to test * stickler * fix test for get_sun_rise_set_transit with _geometric * add test for method value * api.rst and whatsnew updates * Location.get_sun_rise_set_transit outputs dataframe from _geometric method * formatting, add requires_ephem to test_location
1 parent c165e94 commit 3e4b86d

File tree

4 files changed

+72
-3
lines changed

4 files changed

+72
-3
lines changed

docs/sphinx/source/api.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Functions for calculating sunrise, sunset and transit times.
6363
.. autosummary::
6464
:toctree: generated/
6565

66+
location.Location.get_sun_rise_set_transit
6667
solarposition.sun_rise_set_transit_ephem
6768
solarposition.sun_rise_set_transit_spa
6869
solarposition.sun_rise_set_transit_geometric

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ API Changes
2828
2929
Enhancements
3030
~~~~~~~~~~~~
31-
* :func:`~pvlib.solarposition.sun_rise_set_transit_ephem` returns sunrise, sunset
31+
* Add :py:func:`~pvlib.solarposition.sun_rise_set_transit_ephem`to calculate sunrise, sunset
3232
and transit times using pyephem (:issue:`114`)
3333
* Add geometric functions for sunrise, sunset, and sun transit times,
3434
:func:`~pvlib.solarposition.sun_rise_set_transit_geometric` (:issue:`114`)
35+
* Add `Location` class method :py:func:`~pvlib.location.Location.get_sun_rise_set_transit`
3536
* Created :py:func:`pvlib.iotools.read_srml` and
3637
:py:func:`pvlib.iotools.read_srml_month_from_solardat` to read University of
3738
Oregon Solar Radiation Monitoring Laboratory data. (:issue:`589`)

pvlib/location.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,42 @@ def get_airmass(self, times=None, solar_position=None,
276276
airmass['airmass_absolute'] = airmass_absolute
277277

278278
return airmass
279+
280+
def get_sun_rise_set_transit(self, times, method='pyephem', **kwargs):
281+
"""
282+
Calculate sunrise, sunset and transit times.
283+
284+
Parameters
285+
----------
286+
times : DatetimeIndex
287+
Must be localized to the Location
288+
method : str, default 'pyephem'
289+
'pyephem', 'spa', or 'geometric'
290+
291+
kwargs are passed to the relevant functions. See
292+
solarposition.sun_rise_set_transit_<method> for details.
293+
294+
Returns
295+
-------
296+
result : DataFrame
297+
Column names are: ``sunrise, sunset, transit``.
298+
"""
299+
300+
if method == 'pyephem':
301+
result = solarposition.sun_rise_set_transit_ephem(
302+
times, self.latitude, self.longitude, **kwargs)
303+
elif method == 'spa':
304+
result = solarposition.sun_rise_set_transit_spa(
305+
times, self.latitude, self.longitude, **kwargs)
306+
elif method == 'geometric':
307+
sr, ss, tr = solarposition.sun_rise_set_transit_geometric(
308+
times, self.latitude, self.longitude, **kwargs)
309+
result = pd.DataFrame(index=times,
310+
data={'sunrise': sr,
311+
'sunset': ss,
312+
'transit': tr})
313+
else:
314+
raise ValueError('{} is not a valid method. Must be '
315+
'one of pyephem, spa, geometric'
316+
.format(method))
317+
return result

pvlib/test/test_location.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717

1818
import pvlib
1919
from pvlib.location import Location
20-
20+
from pvlib.solarposition import declination_spencer71
21+
from pvlib.solarposition import equation_of_time_spencer71
2122
from test_solarposition import expected_solpos, golden_mst
23+
from test_solarposition import golden
2224

23-
from conftest import requires_scipy
25+
from conftest import requires_ephem, requires_scipy
2426

2527

2628
def test_location_required():
@@ -311,3 +313,29 @@ def test_Location___repr__():
311313
' tz: US/Arizona'
312314
])
313315
assert tus.__repr__() == expected
316+
317+
318+
@requires_ephem
319+
def test_get_sun_rise_set_transit(golden):
320+
times = pd.DatetimeIndex(['2015-01-01 07:00:00', '2015-01-01 23:00:00'],
321+
tz='MST')
322+
result = golden.get_sun_rise_set_transit(times, method='pyephem')
323+
assert all(result.columns == ['sunrise', 'sunset', 'transit'])
324+
325+
result = golden.get_sun_rise_set_transit(times, method='spa')
326+
assert all(result.columns == ['sunrise', 'sunset', 'transit'])
327+
328+
dayofyear = 1
329+
declination = declination_spencer71(dayofyear)
330+
eot = equation_of_time_spencer71(dayofyear)
331+
result = golden.get_sun_rise_set_transit(times, method='geometric',
332+
declination=declination,
333+
equation_of_time=eot)
334+
assert all(result.columns == ['sunrise', 'sunset', 'transit'])
335+
336+
337+
def test_get_sun_rise_set_transit_valueerror(golden):
338+
times = pd.DatetimeIndex(['2015-01-01 07:00:00', '2015-01-01 23:00:00'],
339+
tz='MST')
340+
with pytest.raises(ValueError):
341+
result = golden.get_sun_rise_set_transit(times, method='eyeball')

0 commit comments

Comments
 (0)