Quickstart guideΒΆ

Right click here and 'save as' to download this notebook.

Combined with the API reference docs, this notebook gives an overview of voeventdb.remote, for those who are already familiar with Python, VOEvents, and the basic ideas around web-based API usage. A gentler introduction can be found in the rest of the tutorial notebooks.

In [1]:
from __future__ import print_function
import voeventdb.remote as vr
import voeventdb.remote.apiv1 as apiv1
from voeventdb.remote.apiv1 import FilterKeys, OrderValues
from voeventdb.remote.helpers import Synopsis
from datetime import datetime
import pytz

In voeventdb.remote endpoints are represented by functions, and can be found under the apiv1 namespace:

In [2]:
#Get a count of VOEvents in the database
apiv1.count()
Out[2]:
1412936
In [3]:
#List the IVORNs of the 2 VOEvents most recently inserted into the database
apiv1.list_ivorn(order=OrderValues.id_desc,
                 n_max=2)
Out[3]:
[u'ivo://nasa.gsfc.gcn/SWIFT#BAT_Known_Pos_1925684296-116',
 u'ivo://nasa.gsfc.gcn/SWIFT#BAT_Known_Pos_1925684296-115']

We can apply filters to all endpoints except those returning a single packet. If no filters are passed then the result-set is effectively the entire database.

Filter-sets are defined as dictionaries:

In [4]:
#Filter to only 'observation' packets authored since 2015/12/01.
my_filters = {
    FilterKeys.role:'observation', 
    FilterKeys.authored_since:datetime(2015,12,1,tzinfo=pytz.UTC),
}
In [5]:
#The `map_` endpoints return dictionaries summarising the matching result-set:
apiv1.map_stream_count(filters=my_filters)
Out[5]:
{u'gaia.cam.uk/alerts': 589,
 u'nasa.gsfc.gcn/AMON': 8,
 u'nasa.gsfc.gcn/CALET': 113,
 u'nasa.gsfc.gcn/CAlet': 1,
 u'nasa.gsfc.gcn/COUNTERPART': 61,
 u'nasa.gsfc.gcn/Fermi': 2863,
 u'nasa.gsfc.gcn/INTEGRAL': 395,
 u'nasa.gsfc.gcn/IPN': 186,
 u'nasa.gsfc.gcn/KONUS': 186,
 u'nasa.gsfc.gcn/MAXI': 103,
 u'nasa.gsfc.gcn/MOA': 600,
 u'nasa.gsfc.gcn/SWIFT': 439374,
 u'voevent.4pisky.org/ASASSN': 1919,
 u'voevent.4pisky.org/GAIA': 1368}
In [6]:
#Get the IVORN of the most recently authored SWIFT GRB alert:
my_filters = {
    FilterKeys.role:'observation', 
    FilterKeys.ivorn_contains:'BAT_GRB', #case sensitive
}

swift_bat_grb_list = apiv1.list_ivorn(
    filters=my_filters,
    order=OrderValues.author_datetime_desc,
    n_max=1
)
swift_bat_grb_ivorn = swift_bat_grb_list[0]
swift_bat_grb_ivorn
Out[6]:
u'ivo://nasa.gsfc.gcn/SWIFT#BAT_GRB_Pos_735188-658'

To get more detail on a VOEvent packet, you can use the 'packet_synopsis' function:

In [7]:
# Retrieve a 'synopsis' (nested dictionary) for the VOEvent packet,
grb_nested_dict = apiv1.packet_synopsis(swift_bat_grb_ivorn)
# And convert it to a user-friendly class-object:
grb_synopsis = Synopsis(grb_nested_dict)
print(grb_synopsis.ivorn)
print(grb_synopsis.author_ivorn)
print(grb_synopsis.author_datetime)
print(grb_synopsis.sky_events)
ivo://nasa.gsfc.gcn/SWIFT#BAT_GRB_Pos_735188-658
ivo://nasa.gsfc.tan/gcn
2017-01-26 11:30:56+00:00
[<SkyCoord (ICRS): (ra, dec) in deg
    ( 263.579, -64.7575)> +/- 0.05 @ 2017-01-26T11:30:42.280000+00:00]

voeventdb.remote makes use of Astropy coordinates . So for example, the Synopsis helper class parses the event-position co-ordinates into an Astropy SkyCoord:

In [8]:
sky_event=grb_synopsis.sky_events[0]

sky_event.position
Out[8]:
<SkyCoord (ICRS): (ra, dec) in deg
    ( 263.579, -64.7575)>
In [9]:
print(sky_event.position.ra.deg)
print(sky_event.position.ra.hms)
263.579
hms_tuple(h=17.0, m=34.0, s=18.960000000014645)

We can search for VOEvents with nearby co-ordinates to the Swift event we just looked up:

In [10]:
from astropy.coordinates import SkyCoord, Angle
my_filters = {
    FilterKeys.role:'observation', 
    FilterKeys.cone: (sky_event.position, Angle(1.0, unit='deg'))
    }
apiv1.map_stream_count(my_filters)
Out[10]:
{u'nasa.gsfc.gcn/Fermi': 1,
 u'nasa.gsfc.gcn/SWIFT': 87,
 u'voevent.4pisky.org/GAIA': 1}

Or we run a search at co-ordinates of our choice (see the SkyCoord docs for parseable formats):

In [11]:
from astropy.coordinates import SkyCoord, Angle

chosen_position = SkyCoord(45,45, unit='deg')
radius = Angle(1.0, unit='deg')
my_filters = {
    FilterKeys.role:'observation', 
    FilterKeys.cone: (chosen_position,radius)
    }
apiv1.map_stream_count(my_filters)
Out[11]:
{u'nasa.gsfc.gcn/SWIFT': 44, u'voevent.4pisky.org/ASASSN': 1}

Note we can also retrieve the original XML for the packet (and save it to disk, etc):

In [12]:
raw_xml = apiv1.packet_xml(swift_bat_grb_ivorn)
print(raw_xml[:72])
print(' ... ')
with open('swift_voevent.xml','w') as f:
    f.write(raw_xml)
<?xml version='1.0' encoding='UTF-8'?>
<voe:VOEvent xmlns:voe="http://ww
 ...