diff --git a/docs/sphinx/source/api.rst b/docs/sphinx/source/api.rst index 2ba6957349..7fd77d34cb 100644 --- a/docs/sphinx/source/api.rst +++ b/docs/sphinx/source/api.rst @@ -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 diff --git a/docs/sphinx/source/whatsnew/v0.6.1.rst b/docs/sphinx/source/whatsnew/v0.6.1.rst index c462a2c776..404507d9d6 100644 --- a/docs/sphinx/source/whatsnew/v0.6.1.rst +++ b/docs/sphinx/source/whatsnew/v0.6.1.rst @@ -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`) diff --git a/pvlib/location.py b/pvlib/location.py index df02858641..640dad430d 100644 --- a/pvlib/location.py +++ b/pvlib/location.py @@ -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_ 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 diff --git a/pvlib/test/test_location.py b/pvlib/test/test_location.py index ef2b44c180..1e3aa7a2ea 100644 --- a/pvlib/test/test_location.py +++ b/pvlib/test/test_location.py @@ -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(): @@ -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')