Calculating daily hours of sunlight (sunset-sunrise) and finding the maximum for each year (generally on the solstice but not always), an interesting pattern emerges. About 5 seconds of sunlight are lost each century.
Is this an error factor within PyEphem? Is this accurate and PyEphem taking into account variations in Earth's orbit? some other reason?
import pandas as pd
import ephem
sun = ephem.Sun()
raleigh = ephem.Observer()
raleigh.lon, raleigh.lat = "-78.6382", '35.7796'
raleigh.horizon = '-0:34' # USNO standard atmospheric diffraction
raleigh.pressure = 0 # atmospheric refraction parameters
def riseset(date, f):
# compute passed function (sunrise or sunset)
raleigh.date = date
sun.compute(raleigh)
sr = ephem.localtime(f(sun))
return sr
def createdataframe(start, end):
# create a dataframe index by daily dates, add columns for the
# sunrise, sunset, and their delta
df = pd.DataFrame(index=pd.date_range(start=start, end=end, freq='D'))
df['date'] = df.index.map(lambda d: d.strftime("%b %d"))
df['sunrise'] = df.index.map(lambda d: riseset(d, raleigh.next_rising))
df['sunset'] = df.index.map(lambda d: riseset(d, raleigh.next_setting))
df['daylightdelta'] = df['sunset'] - df['sunrise']
return df
def outputmax(df, year):
i = df['daylightdelta'].idxmax() # index of the day where the sun is visible above the horizon for the most time
return "solstice: %s longest day sunrise: %s sunset: %s daylight: %s" % (
ephem.localtime(ephem.next_solstice(str(year))).strftime("%Y %b %d %X"),
df.loc[i]['sunrise'].strftime("%b %d %X"),
df.loc[i]['sunset'].strftime("%T"),
df.loc[i]['daylightdelta'])
if __name__ == "__main__":
for year in range(1900,2201):
# looping through 1900-2200, find the date with the most hours of sunlight
start = '%d-01-01 04:00:00' % year # compensating for UTC which can throw off pandas columnar math
end = '%d-12-31 23:59:00' % year
print outputmax(createdataframe(start, end), year)