In [1]:
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
    warnings.simplefilter("ignore")
    
    r = requests.post(tap_endpoint + '/sync', data={'query': adql_query, 'request': 'doQuery', 'lang': 'adql', 'format': 'votable', 'phase': 'run'})
    
    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 [9]:
endpoint = 'http://tapvizier.u-strasbg.fr/TAPVizieR/tap'
adql = """SELECT phot_g_mean_mag
          FROM "I/337/tgas"
       """

result = query_TAP(endpoint, adql)


phot_g_mean_mag [1]
        mag        
-------------------
             11.585
             11.791
             11.331
             10.272
             11.474
             11.398
              9.935
             10.528
              11.17
             11.498
                ...
             12.003
             10.732
             10.801
              9.682
             11.223
             11.943
             11.689
             12.169
             11.776
              9.597
Length = 2057050 rows

In [11]:
import matplotlib.pyplot as plt
%matplotlib inline
histvals, binvals, patches = plt.hist(result['phot_g_mean_mag'], bins=50, facecolor='g', alpha=0.75)

plt.xlabel('magnitude')
plt.ylabel('count')
plt.title('G mag histogram')
plt.grid(True)