Friday, October 28, 2011

Querying with a Python script

I have written a Python script/module to query the spectral line catalog compilation. It does not rely on any SPLAT-VO protocol at all. It queries the database through the normal web interface, which makes it possible to query with the same parameters as through the web interface. Yay! The instructions below is for Ubuntu Linux, but is probably easily adapted to other platforms.

EDIT2: I have rewritten the module and the updated version is now part of astropy.astroquery ( However, the CLI interface is stripped out of this version. Furthermore, according to, the php interface will be replace by a java onlye sometime this year (, see Milestone no 4.) which would mean that I have to update it.  
The code is hosted on my trunk (bazaar versioning):

EDIT: I have moved my code to github (!)

the file splatsearch (.py, in the "bin" folder on github) is all that is needed from Launchpad. The prerequisite modules are UrlLib2 and ClientForm, for the form submission/HTML parsing.

In Ubuntu the ClientForm ( module is installed by running

sudo apt-get install python-clientform

in a terminal. UrlLib should come packed with Python. There is a plan to move the splatsearch script over to use some newer module, like the Mechanize module (

EDIT: Mechanize now actually takes care the clientform and urllib (linked) part. I haven't had time to move the script over to it yet. (Branch and fix perhaps?)

It is runnable from the command line (set it to be runnable "chmod +x"). After this, running --help

without any input, from the terminal should give you the usage rundown. Although, first you have to add the path to to your environment variables. Alternatively run it from the same catalog as the file with:

./ [parameters]

It can also be used inside your favorite Python script/interactive session with:

Python : from splatsearch import splatsearch

Yes, I guess it is bad programming to name them the same (module and function). After this initial import it is easy to get the idea of how to search and get it to return what/how you want (print on screen, list or dictionary) with:

Python : help splatsearch

My page also hosts other astronomical data analysis and visualization scripts to do different things on interferometric spectral cubes (small FOV, <60 arcsec). The ADAVIS module has e.g. a function for plotting spectrum and at the same time fit Gaussians, take the fitted frequencies and query for line identification. It is far from done though, and I have a huge to-do list of things to fix, but most things work well enough.

 Happy line identification!


  1. Just an update: your code in astroquery was significantly modified, but a pretty stable & useful version is now available:

    1. Yes I saw that last week after I updated my code, great work! I updated my splatalogue code to not use mechanize (only standard Python 2 and 3 libs) and to fetch the export file directly