Skip to content
1 change: 1 addition & 0 deletions docs/sphinx/source/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Functions for calculating sunrise, sunset and transit times.
.. autosummary::
:toctree: generated/

location.Location.get_sun_rise_set_transit
solarposition.sun_rise_set_transit_ephem
solarposition.sun_rise_set_transit_spa
solarposition.sun_rise_set_transit_geometric
Expand Down
3 changes: 2 additions & 1 deletion docs/sphinx/source/whatsnew/v0.6.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ API Changes

Enhancements
~~~~~~~~~~~~
* :func:`~pvlib.solarposition.sun_rise_set_transit_ephem` returns sunrise, sunset
* Add :py:func:`~pvlib.solarposition.sun_rise_set_transit_ephem`to calculate sunrise, sunset
and transit times using pyephem (:issue:`114`)
* Add geometric functions for sunrise, sunset, and sun transit times,
:func:`~pvlib.solarposition.sun_rise_set_transit_geometric` (:issue:`114`)
* Add `Location` class method :py:func:`~pvlib.location.Location.get_sun_rise_set_transit`
* Created :py:func:`pvlib.iotools.read_srml` and
:py:func:`pvlib.iotools.read_srml_month_from_solardat` to read University of
Oregon Solar Radiation Monitoring Laboratory data. (:issue:`589`)
Expand Down
39 changes: 39 additions & 0 deletions pvlib/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,42 @@ def get_airmass(self, times=None, solar_position=None,
airmass['airmass_absolute'] = airmass_absolute

return airmass

def get_sun_rise_set_transit(self, times, method='pyephem', **kwargs):
"""
Calculate sunrise, sunset and transit times.

Parameters
----------
times : DatetimeIndex
Must be localized to the Location
method : str, default 'pyephem'
'pyephem', 'spa', or 'geometric'

kwargs are passed to the relevant functions. See
solarposition.sun_rise_set_transit_<method> for details.

Returns
-------
result : DataFrame
Column names are: ``sunrise, sunset, transit``.
"""

if method == 'pyephem':
result = solarposition.sun_rise_set_transit_ephem(
times, self.latitude, self.longitude, **kwargs)
elif method == 'spa':
result = solarposition.sun_rise_set_transit_spa(
times, self.latitude, self.longitude, **kwargs)
elif method == 'geometric':
sr, ss, tr = solarposition.sun_rise_set_transit_geometric(
times, self.latitude, self.longitude, **kwargs)
result = pd.DataFrame(index=times,
data={'sunrise': sr,
'sunset': ss,
'transit': tr})
else:
raise ValueError('{} is not a valid method. Must be '
'one of pyephem, spa, geometric'
.format(method))
return result
32 changes: 30 additions & 2 deletions pvlib/test/test_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

import pvlib
from pvlib.location import Location

from pvlib.solarposition import declination_spencer71
from pvlib.solarposition import equation_of_time_spencer71
from test_solarposition import expected_solpos, golden_mst
from test_solarposition import golden

from conftest import requires_scipy
from conftest import requires_ephem, requires_scipy


def test_location_required():
Expand Down Expand Up @@ -311,3 +313,29 @@ def test_Location___repr__():
' tz: US/Arizona'
])
assert tus.__repr__() == expected


@requires_ephem
def test_get_sun_rise_set_transit(golden):
times = pd.DatetimeIndex(['2015-01-01 07:00:00', '2015-01-01 23:00:00'],
tz='MST')
result = golden.get_sun_rise_set_transit(times, method='pyephem')
assert all(result.columns == ['sunrise', 'sunset', 'transit'])

result = golden.get_sun_rise_set_transit(times, method='spa')
assert all(result.columns == ['sunrise', 'sunset', 'transit'])

dayofyear = 1
declination = declination_spencer71(dayofyear)
eot = equation_of_time_spencer71(dayofyear)
result = golden.get_sun_rise_set_transit(times, method='geometric',
declination=declination,
equation_of_time=eot)
assert all(result.columns == ['sunrise', 'sunset', 'transit'])


def test_get_sun_rise_set_transit_valueerror(golden):
times = pd.DatetimeIndex(['2015-01-01 07:00:00', '2015-01-01 23:00:00'],
tz='MST')
with pytest.raises(ValueError):
result = golden.get_sun_rise_set_transit(times, method='eyeball')