In [2]:
def query_TAP(tap_endpoint, adql_query, table_to_upload=None):
"""
Query a TAP service (designated by its tap_endpoint)
with a given ADQL query
Query is performed synchronously
Return an AstroPy Table object
"""
import requests
from astropy.table import Table
from astropy.io.votable import parse_single_table
import os
import tempfile
import warnings
r = requests.post(tap_endpoint + '/sync', data={'query': adql_query, 'request': 'doQuery', 'lang': 'adql', 'format': 'votable', 'phase': 'run'})
warnings.simplefilter("ignore")
tmp_vot = tempfile.NamedTemporaryFile(delete = False)
with open(tmp_vot.name, 'w') as h:
for line in r.iter_lines():
if line:
h.write(line.decode(r.encoding)+'\n')
table = parse_single_table(tmp_vot.name).to_table()
# finally delete temp files
os.unlink(tmp_vot.name)
return table
In [3]:
endpoint = 'http://tapvizier.u-strasbg.fr/TAPVizieR/tap'
x_col = 'ra'
y_col = 'dec'
x_binsize = 5
y_binsize = 5
x_name = '%s_floor' % (x_col)
y_name = '%s_floor' % (y_col)
adql = """SELECT floor(%s/%f)*%f as %s,
floor(%s/%f)*%f as %s,
count(*) as nb
FROM "I/337/tgas"
GROUP BY 1,2
""" % (x_col, x_binsize, x_binsize, x_name, y_col, y_binsize, y_binsize, y_name)
print('ADQL query: ' + adql)
result = query_TAP(endpoint, adql)
print(result)
In [7]:
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.colors import LogNorm
w = 16
h = 8
fig, ax = plt.subplots(figsize = (w, h))
ax.set_xlabel(x_col)
ax.set_ylabel(y_col)
x, y, count = result[x_name], result[y_name], result['nb']
idx = count.argsort()
x, y, count = x[idx], y[idx], count[idx]
plt.scatter(x, y, c=count, s=50, edgecolor='')
cbar = plt.colorbar()
cbar.ax.set_ylabel('Counts')
Out[7]:
In [ ]: