Search code examples
pythonpyephem

PyEphem: what should I set the time in an object's date attribute when finding sunrise/sunset time?


The attribute: .date, so for example

>>> gatech.date = '1984/5/30 16:22:56'   # 12:22:56 EDT

Looking at the documentation, it appears that due to astronomical changes, the location of various objects with respect to the observer changes with time and that's what the date account for.

However, I am interested in getting just the sunrise and sunset times at a particular location. In that case, I don't know what exactly I should put for the time in the date attribute.


Solution

  • The time component of gatech.date just needs to be a UTC value that is not close to any possible sunrise/sunset times. The safest bet is to pick local noon or midnight. It does not have to be EXACTLY noon or midnight, as long as it is far enough from sunrise/sunset to avoid confusing pyephem's next/previous functions - even when DST tries to make a mess.

    Using local midnight:

    gatech.date = '1984/5/30 4:00:00'   # 0:00:00 local time (ignoring DST)
    gatech.lat = str(33.775618)
    gatech.lon = str(-84.396285)
    
    sunrise = gatech.next_rising(ephem.Sun())  # local sunrise on 5/30/1984 in UTC
    sunset = gatech.next_setting(ephem.Sun())  # local sunset on 5/30/1984 in UTC
    

    If you were using local noon, you would use the previous_rising and next_setting functions.