This website (www2.astrogrid.org) is now deprecated - please go to www.astrogrid.org for up to date information.
Python Scripting
This page describes the access of Astrogrid from Python using calls to the XMLRPC interface.
First steps
All python scripts need to include the next lines:
import xmlrpclib, os
prefix = file(os.path.expanduser("~/.astrogrid-desktop")).next().rstrip()
acr = xmlrpclib.Server(prefix + "xmlrpc")
From there on the acr will be available. Note that the ACR must be running prior to executing these commands.
Getting information
The information about the methods available is located in the ACR and XMLRPC API documentation. From python one can access a small version of this using the following commands:
# Return a list of all available methods
acr.system.listMethods()
# Print a method help
acr.system.methodHelp('ivoa.cone.execute')
The output of the last command is:
Method execute belonging to component cone in module ivoa
execute a DAL query, returning a datastructure
query : URL
query url to execute
unknown : List of : key-value map
A model the DAL query response as a list of of rows. Each row is represented is a map
between UCD keys or datamodel names and values from the response
Logging in and out
Defined the variables username, password and community the next commands authenticate the user in the system:
acr.astrogrid.community.login(username,password,community)
To log out:
acr.astrogrid.community.logout()
It is also possible to ask the user for the username and password using the GUI:
acr.astrogrid.community.guiLogin()
will show the login screen and wait for the user input.
Reading a VOTable
In order to read a VOTable you need to download the VOTable.py module. Then one can write:
import VOTable
# user_votable is the full URI to the votable to read
votString = acr.util.tables.convertFromFile(user_votable,"votable","votable")
vot = VOTable.VOTable()
vot.parseText(votString)
# Get column numbers for various columns
nameCol = vot.getColumnIdx('Name')
raCol = vot.getColumnIdx('POS_EQ_RA_MAIN')
decCol = vot.getColumnIdx('POS_EQ_DEC_MAIN')
rows = vot.getDataRows()
for row in rows:
rowData = vot.getData(row)
print rowData[nameCol], rowData[raCol], rowData[decCol]
in order to print out the name, ra, dec for each row.
Performing a cone search
The cone search interface is located in the acr.ivoa.cone method (look at the XMLRPC documentation for more information on how to call the different methods). So in order to perform a cone search, one has to execute the following commands:
# Service is the IVORN of the cone search service e.g. "ivo://sdss.jhu/services/DR4CONE"
# ra, dec and radius in degrees
# __REMEMBER__ to run the commands on "First Steps" before running this
query = acr.ivoa.cone.constructQuery(service,ra,dec,radius)
result = acr.ivoa.cone.execute(query)
open("table.vot", "w").save(result)
In order to save the output to MySpace one can use:
acr.ivoa.cone.executeAndSave(query, saveLocation)
where saveLocation is the full path of the file to save to (may be a file://, ivo:// or ftp:// reference).
Full cone search example
A python script is available which illustrates the cone search capabilities. It also shows how to parse a VOTable and read columns. In short this script accepts a VOTable as input and executes a list of cone searches for each row (i.e. for each object in the table) saving the results to the specified directory. The files needed are conesearch_orig.py, VOTable.py and sample.vot. In order to run the script, just save these three files to a directory and execute:
python conesearch_orig.py -f sample.vot
In order to get additional help on switches:
python conesearch_orig.py --help
Performing a SIAP call
The siap methods are located in ivoa.siap (see XMLRPC). A SIAP call is performed as follows:
# ra, dec, radius in degrees
# format is format of images e.g. 'ALL' or 'image/fits'
service='ivo://roe.ac.uk/services/SIAPDR4-images'
query = acr.ivoa.siap.constructQueryF(service, ra, dec, radius, format)
result = ivoa.siap.executeVotable(query)
open("siap.vot", "w").save(result)
In order to save the images (remember the SIAP call returns a list of images) one can just parse the table and do a GET on the URL column or use:
# saveLocation may be a file://, ivo:// or ftp:// reference acr.ivoa.siap.saveDatasets(query, saveLocation)
This last command is particularly useful to save the images to MySpace without having to download the data to the local computer.
More scripts
http://www2.astrogrid.org/desktop/download/acr-interface-2007.1.1.zip contains some examples of python and java scripts