Overview

Most commonly used functions will be available from the top pyvo module

In [1]:
import pyvo as vo

Building a catalog of images

We want to create a catalog of available X-ray images of our favorite source (supernova remnant, Cas A).

First we look for archives that have x-ray images:

In [2]:
# find archives with x-ray images
archives = vo.regsearch(servicetype='image', waveband='xray') 

archives now contains a list of archives with data

In [3]:
len(archives)
Out[3]:
20

Next, we'll search each archive to find out if they have images of our source. For that, we need to get its position:

In [4]:
pos = vo.object2pos('Cas A')
pos
Out[4]:
(350.85000000000002, 58.814999999999998)

As we search, we will write our results to a CSV file. We need to make sure that be prepared for failures by catching the DALAccessError. This example was run in October the day after the US goverment shutdown and NASA went off-line; consequently, most of the archive queries failed.

In [5]:
# find images and list them in a CSV file
with open('cas-a.csv', 'w') as csv:
    csv.write("Archive short name,Archive title,Image title,RA,Dec,URL\n")
    
    for arch in archives:
        print("searching %s..." % arch.shortname)
        
        try:
            matches = arch.search(pos=pos, size=0.25)
        except vo.DALAccessError as ex:
            print("Trouble accesing %s archive (%s)" % 
                  (arch.shortname, str(ex)))
            continue
            
        print("...found %d images" % matches.nrecs)
        for image in matches:
            csv.write(','.join( (arch.shortname, arch.title, image.title,
                                 str(image.ra), str(image.dec),
                                 image.getdataurl()) )) 
searching ROSAT SIA...
WARNING
WARNING:astropy:W22: dal_query:3:2: W22: The DEFINITIONS element is deprecated in VOTable 1.1.  Ignoring
: W22: dal_query:3:2: W22: The DEFINITIONS element is deprecated in VOTable 1.1.  Ignoring [astropy.io.votable.exceptions]
WARNING
WARNING:astropy:W01: dal_query:40:16: W01: Array uses commas rather than whitespace
WARNING:astropy:W01: dal_query:41:16: W01: Array uses commas rather than whitespace
WARNING:astropy:W01: dal_query:46:16: W01: Array uses commas rather than whitespace
WARNING:astropy:W01: dal_query:47:16: W01: Array uses commas rather than whitespace
WARNING:astropy:W01: dal_query:48:16: W01: Array uses commas rather than whitespace
WARNING:astropy:W01: dal_query:66:16: W01: Array uses commas rather than whitespace
WARNING:astropy:W01: dal_query:67:16: W01: Array uses commas rather than whitespace
WARNING:astropy:W01: dal_query:72:16: W01: Array uses commas rather than whitespace
WARNING:astropy:W01: dal_query:73:16: W01: Array uses commas rather than whitespace
WARNING:astropy:W01: dal_query:74:16: W01: Array uses commas rather than whitespace (suppressing further warnings of this type...)
: W01: dal_query:40:16: W01: Array uses commas rather than whitespace [astropy.io.votable.exceptions]
WARNING: W01: dal_query:41:16: W01: Array uses commas rather than whitespace [astropy.io.votable.exceptions]
WARNING: W01: dal_query:46:16: W01: Array uses commas rather than whitespace [astropy.io.votable.exceptions]
WARNING: W01: dal_query:47:16: W01: Array uses commas rather than whitespace [astropy.io.votable.exceptions]
WARNING: W01: dal_query:48:16: W01: Array uses commas rather than whitespace [astropy.io.votable.exceptions]
WARNING: W01: dal_query:66:16: W01: Array uses commas rather than whitespace [astropy.io.votable.exceptions]
WARNING: W01: dal_query:67:16: W01: Array uses commas rather than whitespace [astropy.io.votable.exceptions]
WARNING: W01: dal_query:72:16: W01: Array uses commas rather than whitespace [astropy.io.votable.exceptions]
WARNING: W01: dal_query:73:16: W01: Array uses commas rather than whitespace [astropy.io.votable.exceptions]
WARNING: W01: dal_query:74:16: W01: Array uses commas rather than whitespace (suppressing further warnings of this type...) [astropy.io.votable.exceptions]
...found 82 images
searching NED(images)...
Trouble accesing NED(images) archive (E19: E19)
searching SkyView...
...found 215 images
searching HEAVENS @ ISDC...
WARNING
WARNING:astropy:W50: dal_query:41:6: W50: Invalid unit string 'bytes'
: W50: dal_query:41:6: W50: Invalid unit string 'bytes' [astropy.io.votable.exceptions]
...found 4 images
searching TGCat SIA...
WARNING
WARNING:astropy:W50: dal_query:20:0: W50: Invalid unit string 'degree'
WARNING:astropy:W50: dal_query:21:0: W50: Invalid unit string 'degree'
WARNING:astropy:W50: dal_query:27:0: W50: Invalid unit string 'pixel'
WARNING:astropy:W50: dal_query:28:0: W50: Invalid unit string 'degree/pixel'
WARNING:astropy:W50: dal_query:29:0: W50: Invalid unit string 'kB'
: W50: dal_query:20:0: W50: Invalid unit string 'degree' [astropy.io.votable.exceptions]
WARNING: W50: dal_query:21:0: W50: Invalid unit string 'degree' [astropy.io.votable.exceptions]
WARNING: W50: dal_query:27:0: W50: Invalid unit string 'pixel' [astropy.io.votable.exceptions]
WARNING: W50: dal_query:28:0: W50: Invalid unit string 'degree/pixel' [astropy.io.votable.exceptions]
WARNING: W50: dal_query:29:0: W50: Invalid unit string 'kB' [astropy.io.votable.exceptions]
...found 9 images
searching RASS.25keV [1]...
...found 0 images
searching RASSBCK [1]...
...found 0 images
searching BATSIG [1]...
...found 0 images
searching GRANAT [1]...
...found 0 images
searching HEAO1A [1]...
...found 0 images
searching HRI [1]...
...found 5 images
searching INTEGRALSPI_gc [1]...
...found 0 images
searching INTGAL [1]...
...found 0 images
searching PSPC1 [1]...
...found 5 images
searching PSPC2 [1]...
...found 5 images
searching PSPC6 [1]...
...found 0 images
searching RASSALL [1]...
...found 15 images
searching RXTE [1]...
...found 0 images
searching CSC...
...found 0 images
searching CDA...
...found 454 images

PyVO Design Features

Iteration as a natural, pythonic way to process results

  • Dataset discovery: access tends to be row-based
  • Catalog querying: row- or column-based

The interface tries to be self-explanatory

  • functions and result objects are documented
  • important metadata in a response record are available as properties
  • minimize required knowledge of protocols for most common queries
In [6]:
archives[0].title
Out[6]:
'SIA Service for ROSAT Archive'
In [7]:
archives[0].publisher
Out[7]:
'German Astrophysical Virtual Observatory'

An archive service is queried via its access URL.

In [8]:
archives[0].accessurl
Out[8]:
'http://www.g-vo.org/rosat/SIAP?action=queryImage&siap=siap.service.rosat&'

(slide)

Querying a catalog

In [10]:
usnob = 'http://www.nofs.navy.mil/cgi-bin/vo_cone.cgi?CAT=USNO-B1&'
srcs = vo.conesearch(usnob, pos=(45.31, 74.0), radius=0.05)  
In [11]:
len(srcs)
Out[11]:
203
In [12]:
srcs.fieldnames()
Out[12]:
[u'id',
 u'RA',
 u'DEC',
 u'sRa',
 u'sDec',
 u'sRaEp',
 u'sDeEp',
 u'MuRA',
 u'MuDEC',
 u'sMuRA',
 u'sMuDE',
 u'B1',
 u'B1_S/G',
 u'R1',
 u'R1_S/G',
 u'B2',
 u'B2_S/G',
 u'R2',
 u'R2_S/G',
 u'I2',
 u'I2_S/G',
 u'Xi',
 u'Eta',
 u'Gal_L',
 u'Gal_B']
In [13]:
srcs[0]['sRa']
Out[13]:
192
In [14]:
at = srcs.votable.to_table()
b1 = at['B1']
b1
Out[14]:
<MaskedColumn name='B1' units='mag' format=u'%.3f' description=None>
masked_array(data = [21.09 20.94 21.15 20.89 0.0 0.0 0.0 19.96 19.51 0.0 21.18 0.0 20.58 0.0
 19.38 21.16 20.16 19.69 0.0 0.0 0.0 21.14 16.69 0.0 20.79 0.0 20.98 0.0
 0.0 0.0 0.0 0.0 0.0 0.0 21.04 20.93 21.02 0.0 0.0 0.0 0.0 0.0 21.11 0.0
 0.0 0.0 0.0 0.0 16.58 20.2 0.0 20.83 0.0 0.0 20.32 20.37 19.33 0.0 0.0
 21.18 19.15 0.0 0.0 20.38 21.02 21.17 19.36 0.0 0.0 19.45 0.0 0.0 0.0
 18.61 20.66 0.0 0.0 20.78 20.95 0.0 0.0 0.0 0.0 0.0 19.2 0.0 0.0 0.0 0.0
 21.15 0.0 0.0 0.0 20.9 17.52 0.0 20.08 0.0 20.54 20.61 18.53 0.0 20.15
 20.69 0.0 17.88 19.07 21.18 0.0 21.08 18.33 18.01 20.67 20.13 0.0 0.0
 21.04 18.68 20.33 20.71 18.9 0.0 0.0 0.0 20.84 0.0 19.26 19.07 0.0 20.98
 0.0 0.0 0.0 20.07 0.0 19.87 19.25 19.59 15.81 0.0 0.0 21.18 21.17 20.0 0.0
 0.0 19.9 0.0 20.73 21.18 0.0 16.96 18.75 18.54 20.79 21.17 17.59 19.17
 21.0 21.08 20.47 20.47 18.54 21.15 17.85 20.65 0.0 0.0 20.9 20.93 21.07
 18.36 0.0 20.92 0.0 20.35 20.5 17.73 0.0 20.88 0.0 0.0 0.0 21.05 0.0 19.83
 0.0 21.17 0.0 21.04 21.18 0.0 18.11 18.78 0.0 21.18 20.92 0.0 0.0 0.0 0.0
 20.3 20.11],
             mask = [False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False False
 False False False False False False False False False False False],
       fill_value = 1e+20)

Creating image cutouts for a list of sources

In [15]:
# obtain your list of positions from somewhere
sourcenames = ["ngc4258", "m101", "m51"]
mysources = {}
for src in sourcenames:
    mysources[src] = vo.object2pos(src) 
In [ ]:
# create an output directory for cutouts
import os
os.mkdir("NVSSimages")

We will use a query object to save and re-use query constraints

In [16]:
# setup a query object for NVSS
nvss = "http://skyview.gsfc.nasa.gov/cgi-bin/vo/sia.pl?survey=nvss&"
query = vo.sia.SIAQuery(nvss)
query.size = 0.2    # degrees square
query.format = 'image/fits' 
In [ ]:
for name, pos in mysources.items():
    query.pos = pos
    results = query.execute()
    for image in results:
        image.cachedataset(filename="NVSSimages/%s.fits" % name) 
In [17]:
query.pos = mysources['m101']

print query.getqueryurl() 
http://skyview.gsfc.nasa.gov/cgi-bin/vo/sia.pl?survey=nvss&POS=210.8024292,54.34875&SIZE=0.2,0.2&FORMAT=image%2Ffits
In [ ]: