Tuesday, July 27, 2010

PyEphem: an ephemeris computation library

I wanted a sunrise-sunset for the day information to be posted in my weather page at Digital Explorations and at Life-Research-Education blogs. I originally wanted to implement some algorithms used by say the US Naval Observatory, but on second thought, I was thinking there might be already Python libraries for doing that or something similar!

PyEphem is a Python library for Python versions < 3.0(Python 3.0 users should install ephem instead). The homepage is at http://rhodesmill.org/pyephem/. Here is our attempt to install pyephem on our Ubuntu 64 bit Lucid Lynx:
sudo easy_install pyephem /usr/lib/python2.6/dist-packages/setuptools/package_index.py:156: UserWarning: Unbuilt egg for setuptools [unknown version] (/usr/lib/python2.6/dist-packages) Environment.__init__(self,*args,**kw) /usr/lib/python2.6/dist-packages/setuptools/command/easy_install.py:216: UserWarning: Unbuilt egg for setuptools [unknown version] (/usr/lib/python2.6/dist-packages) self.local_index = Environment(self.shadow_path+sys.path) Searching for pyephem Reading http://pypi.python.org/simple/pyephem/ Reading http://rhodesmill.org/pyephem/ Best match: pyephem 3.7.3.4 Downloading http://pypi.python.org/packages/source/p/pyephem/pyephem-3.7.3.4.tar.gz Processing pyephem-3.7.3.4.tar.gz Running pyephem-3.7.3.4/setup.py -q bdist_egg --dist-dir /tmp/easy_install-aGswju/pyephem-3.7.3.4/egg-dist-tmp-ZGk2Ek libastro-3.7.3/magdecl.c: In function ‘E0000’: libastro-3.7.3/magdecl.c:150: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result libastro-3.7.3/magdecl.c:153: warning: ignoring return value of ‘fgets’, declared with attribute warn_unused_result In file included from libastro-3.7.3/deep.c:5: libastro-3.7.3/satspec.h:16: warning: function declaration isn’t a prototype In file included from libastro-3.7.3/sdp4.c:7: libastro-3.7.3/satspec.h:16: warning: function declaration isn’t a prototype In file included from libastro-3.7.3/sgp4.c:6: libastro-3.7.3/satspec.h:16: warning: function declaration isn’t a prototype zip_safe flag not set; analyzing archive contents... ephem.tests.test_jpl: module references __file__ ephem.tests.test_usno: module references __file__ ephem.tests.test_rst: module references __file__ Adding pyephem 3.7.3.4 to easy-install.pth file Installed /usr/local/lib/python2.6/dist-packages/pyephem-3.7.3.4-py2.6-linux-x86_64.egg


Note the warnings.


$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyephem
Traceback (most recent call last):
  File "", line 1, in 
ImportError: No module named pyephem
>>> 

Crazy!! Why is pyephem not installed?

To be continued ......

Ah ok. it should be import ephem and not pyephem! Nuts!

Ok. Here is an example from http://scienceoss.com/calculate-sunrise-and-sunset-with-pyephem/
to display graphically the day length for the whole year of 2008. (difference of sunset and sunrise times).

import ephem
import datetime

obs = ephem.Observer()
obs.lat = '38.8'
obs.long= '-75.2'

start_date = datetime.datetime(2008, 1, 1)
end_date = datetime.datetime(2008, 12, 31)
td = datetime.timedelta(days=1)

sun = ephem.Sun()

sunrises = []
sunsets = []
dates = []

date = start_date
while date < end_date:
date += td
dates.append(date)
obs.date = date

rise_time = obs.next_rising(sun).datetime()
sunrises.append(rise_time)

set_time = obs.next_setting(sun).datetime()
sunsets.append(set_time)

from pylab import *
daylens = []
for i in range(len(sunrises)):
timediff = sunsets[i] - sunrises[i]
hours = timediff.seconds / 60. / 60.  # to get it in hours
daylens.append(hours)

plot(dates, daylens)

# if you have an older version of matplotlib, you may need
# to convert dates into numbers before plotting:
# dates = [date2num(i) for i in dates]

xlabel('Date')
ylabel('Hours')
title('Day length in 2008')
show()

And here is the nice output courtesy of matplotlib:

No comments:

Post a Comment