This website (www2.astrogrid.org) is now deprecated - please go to www.astrogrid.org for up to date information.
Using Astrogrid from Python
Astrogrid from PythonYou can access the services and tools provided by Astrogrid from several scripting/programming languages. In this document we describe how this is done from Python. Python is a high level programming language which is becoming very popular in astronomy. 1 Requirements1.1 PythonIn order to access the Astrogrid services from Python you need a recent version of Python installed. Python versions 2.4 (released on November 30, 2004) and greater come with all needed libraries but the latest version of Python is recommended (2.5 released on September 19th, 2006). In order to know which python version you are running you can type from the command line: python -c 'import sys; print sys.version' If you do not have a version greater or equal to 2.4 then you will need to install a more recent Python. Find below some instructions on installing Python in different system. More details are available in the Beginners Guide Downloads Section. If you are running Windows just go to the Python download page (http://www.python.org/download/) and choose Python 2.5 Windows Installer. By default Python will be installed in the directory C:Python25. If you use Linux then Python is included in all main distributions by default. Refer to your package install instructions to install it if it is not already in your system. Generally something like apt-get install python or yum install python will be enough. Mac OS X (10.4) comes with Python 2.3 installed, but you will need to use Python 2.4 or later. There are several ways of getting it:
OSX 10.5 already comes with Python 2.5 installed so you do not need to do anything else additional. 1.2 Astrogrid VO DesktopDownload from http://www.astrogrid.org 2 Installation and Configuration2.1 Quick installationFor a quick installation which does not involve any third party packages follow the instructions below. Note however that if you plan to use Python seriously you should consider installing packages like numpy (for numerical processing), pyfits/pcfitsio (to read fits files), matplotlib (to create plots) and then the setuptools route discussed below is more appropriate. Also updates are handled easier. Anyway to quickly start with Python and Astrogrid:
Then go to section 2.3 directly. 2.2 Installation using setuptoolsIf you do not have setuptools already installed then download the latest ez_setup.py and run it; this will download and install the appropriate setuptools egg for your Python version. Then you just need to type: easy_install -f http://code.google.com/p/pyacr/downloads/list astrogrid To upgrade a previously installed package type: easy_install -Uf http://code.google.com/p/pyacr/downloads/list astrogrid For more information using easy_install look at the documentation and in custom locations if you do not have administrator access to the Python installation directory. 2.3 ConfigurationThis module expects a configuration in $HOME/.python-acr (or $HOME/_python-acr for Windows Users). In order to create one just import the astrogrid module from python, it will automatically detect that you do not have one and will create a skeleton one for you: python -c 'import astrogrid' This default configuration file looks like: debug = True verbose = True autologin = True plastic = True [community] default=leicester [[leicester]] username = username1 password = password1 community = uk.ac.le.star [[ukidss]] username = username2 password = password2 community = ukidss.roe.ac.uk where username[1,2] and password[1,2] are your particular credentials for logging into your AstroGrid accounts. With this configuration you get maximum verbosity (debug, verbose) when running your applications. Also you will not be asked about your login credentials since these will be read from the configuration file when needed. In the second example you will be logged into in your ukidss account by default but you can select the leicester account as well. Make sure that the configuration file has right properties, i.e. it is only readable for the owner. In Unix you do chmod 0600 ~/.python_acr. 2.3.1 Encrypting the configuration fileFor an extra layer of security you have the option to encrypt your configuration file. Of course this means that you will be prompted for the password used to encrypt it when you use acr. To do this, inside python: from astrogrid.config import cryptconf cryptconf() and type a password. The same command will decrypt a previously encrypted file. 3 Getting StartedIn order to use AR from Python you need the AstroGrid VO Desktop running. Once you need to do then is: $ python >>> from astrogrid import acr 3.1 How To...3.1.1 Log In and OutOnce you have a configuration file as described above you can login into your different accounts using:
# Login in the default account
acr.login()
# Login in your ukidss account
acr.login('ukidss')
# Login user particular credentials
acr.login(username, password, community)
To log out simply type: acr.logout() 3.1.2 Getting helpSuppose that we want to perform a conesearch, so we first import the ConeSearch class: from astrogrid import ConeSearch You can get help about the usage of the class and its methods by typing: help(ConeSearch) 3.1.3 Query the RegistryThe registry holds the information about all services available within the VO. Below some examples on querying the registry for resources.
from astrogrid import Registry
reg = Registry()
# search for Cone services related to SDSS
# and print their ids
list = reg.searchCone('SDSS')
print [p['id'] for p in list]
# Print the description of service number 5 of the list
print list[5]['content']['description']
3.1.4 Perform a SIAP QueryThis are the basic lines to perform a SIAP search. For a more complete example and link to a full working script look below in the examples section:
from astrogrid import SiapSearch
siap = SiapSearch('ivo://roe.ac.uk/services/SIAPDR4-images')
result = siap.execute(180.0, 2.0, 1.0)
3.1.5 Query a catalogue using ADQLIn order to query a service which provides ADQL capabilities we use the Data Set Access class (DSA). The following example illustrates the basic commands to query the 2MASS PSC catalogue:
from astrogrid import DSA
db = DSA('ivo://wfau.roe.ac.uk/twomass-dsa/ceaApplication')
app = db.query('SELECT TOP 10 * FROM twomass_psc AS x')
app.status()
result = app.results[0]
3.1.6 Work with MySpaceThis is how you list the contents of your root directory of MySpace: from astrogrid import MySpace m = MySpace() m.ls() You can delete files or folders:
m.rm('#test/file.vot')
m.rm('#test/', recursive=True)
and read file from MySpace:
img = m.readfile('#sdss/image.fits')
File names in MySpace always start with the hash key (#) or with 'ivo://'. For instance when broadcasting a file to plastic listening applications:
# A file in local disk
broadcast('image.fits')
# A file in MySpace
broadcast('#image.fits')
3.1.7 Send results to TOPCAT or AladinIf TOPCAT or Aladin (or any other Plastic listening application) are running t hen you can send the results from your queries or tasks directly to them. This is how:
# Start plastic connection
acr.startplastic()
# Send a local file to Aladin
acr.plastic.broadcast('image.fits', 'Aladin')
# Send a file in MySpace to all applications
acr.plastic.broadcast('#sdss/catalogue.vot')
# Send the result from a cone search to Topcat
result = cone.execute(250.0, 54.0, 0.3)
acr.plastic.broadcast(result, 'Topcat')
3.1.8 Run processes in the backgroundUsing IPython it is possible to run any process in the background. E.g.:
cone = ConeSearch("ivo://ned.ipac/Basic_Data_Near_Position")
%bg cone.execute(180.0, 1.0, 0.1)
%bg cone.execute(180.0, 2.0, 0.1)
%bg cone.execute(180.0, 3.0, 1)
jobs.status()
Running jobs:
2 : cone.execute(180.0, 3.0, 1)
Completed jobs:
0 : cone.execute(180.0, 1.0, 0.1)
1 : cone.execute(180.0, 2.0, 0.1)
jobs[1].status()
'Completed'
votable = jobs[1].result
|