AstroGrid

Navigation
Log in


Forgot your password?
 

This website (www2.astrogrid.org) is now deprecated - please go to www.astrogrid.org for up to date information.

Document Actions

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 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.