Skip to content

Commit 113148f

Browse files
authored
Merge pull request #1375 from bsipocz/xmatch_votable
Switching to use VOtable format for XMatch
2 parents e5d2c8e + 47d993e commit 113148f

File tree

5 files changed

+33
-39
lines changed

5 files changed

+33
-39
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ mast
186186
- Changed warning to error for authentication failure. [#1874]
187187

188188

189+
xmatch
190+
^^^^^^
191+
192+
- Minor internal change to use VOTable as the response format that include
193+
units, too. [#1375]
194+
189195

190196
Infrastructure, Utility and Other Changes and Additions
191197
-------------------------------------------------------

astroquery/xmatch/core.py

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
from io import StringIO, BytesIO
44

5-
from astropy.io import ascii, votable
5+
from astropy.io import votable
66
import astropy.units as u
77
from astropy.table import Table
88
from requests import HTTPError
99

10-
1110
from astroquery.query import BaseQuery
1211
from astroquery.exceptions import InvalidQueryError
1312
from astroquery.utils import url_helpers, prepend_docstr_nosections, async_to_sync
@@ -77,7 +76,9 @@ def query(self, cat1, cat2, max_distance,
7776
**kwargs)
7877
if get_query_payload:
7978
return response
80-
return self._parse_text(response.text)
79+
80+
content = BytesIO(response.content)
81+
return Table.read(content, format='votable', use_names_over_ids=True)
8182

8283
@prepend_docstr_nosections("\n" + query.__doc__)
8384
def query_async(self, cat1, cat2, max_distance, colRA1=None, colDec1=None,
@@ -92,12 +93,11 @@ def query_async(self, cat1, cat2, max_distance, colRA1=None, colDec1=None,
9293
if max_distance > 180 * u.arcsec:
9394
raise ValueError(
9495
'max_distance argument must not be greater than 180')
95-
payload = {
96-
'request': 'xmatch',
97-
'distMaxArcsec': max_distance.to(u.arcsec).value,
98-
'RESPONSEFORMAT': 'csv',
99-
**kwargs
100-
}
96+
payload = {'request': 'xmatch',
97+
'distMaxArcsec': max_distance.to(u.arcsec).value,
98+
'RESPONSEFORMAT': 'votable',
99+
**kwargs}
100+
101101
kwargs = {}
102102

103103
self._prepare_sending_table(1, payload, kwargs, cat1, colRA1, colDec1)
@@ -190,22 +190,5 @@ def get_available_tables(self, cache=True):
190190
content = response.text
191191
return content.splitlines()
192192

193-
def _parse_text(self, text):
194-
"""
195-
Parse a CSV text file that has potentially duplicated header names
196-
"""
197-
header = text.split("\n")[0]
198-
colnames = header.split(",")
199-
for column in colnames:
200-
if colnames.count(column) > 1:
201-
counter = 1
202-
while colnames.count(column) > 0:
203-
colnames[colnames.index(column)] = column + "_{counter}".format(counter=counter)
204-
counter += 1
205-
new_text = ",".join(colnames) + "\n" + "\n".join(text.split("\n")[1:])
206-
result = ascii.read(new_text, format='csv', fast_reader=False)
207-
208-
return result
209-
210193

211194
XMatch = XMatchClass()

astroquery/xmatch/tests/test_xmatch.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,3 @@ def test_xmatch_query_cat1_table_local(monkeypatch):
104104
'errHalfMaj', 'errHalfMin', 'errPosAng', 'Jmag', 'Hmag', 'Kmag',
105105
'e_Jmag', 'e_Hmag', 'e_Kmag', 'Qfl', 'Rfl', 'X', 'MeasureJD']
106106
assert len(table) == 11
107-
108-
109-
@pytest.mark.parametrize('datafile', DATA_FILES.values())
110-
def test_parse_text(datafile):
111-
xm = XMatch()
112-
xm._parse_text(datafile)

astroquery/xmatch/tests/test_xmatch_remote.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
import pytest
66
import requests
77
from requests import ReadTimeout
8+
from numpy.testing import assert_allclose
89

910
from astropy.table import Table
1011
from astropy.units import arcsec, arcmin
11-
from astropy.io import ascii
1212

1313
from astropy.coordinates import SkyCoord
1414

@@ -37,12 +37,15 @@ def test_is_xmatch_up():
3737
def remote_table(tmp_path_factory):
3838
# this can be used to check that the API is still functional & doing as expected
3939
infile = DATA_DIR / "posList.csv"
40-
outfile = tmp_path_factory.mktemp("remote_data") / "http_result.csv"
41-
os.system("curl -X POST -F request=xmatch -F distMaxArcsec=5 -F RESPONSEFORMAT=csv "
40+
outfile = tmp_path_factory.mktemp("remote_data") / "http_result.vot"
41+
os.system("curl -X POST -F request=xmatch -F distMaxArcsec=5 -F RESPONSEFORMAT=votable "
4242
"-F cat1=@{1} -F colRA1=ra -F colDec1=dec -F cat2=vizier:II/246/out "
4343
"http://cdsxmatch.u-strasbg.fr/xmatch/api/v1/sync > {0}".
4444
format(outfile, infile))
45-
return ascii.read(outfile, format="csv", fast_reader=False)
45+
remote_table = Table.read(outfile, format="votable")
46+
remote_table.rename_column('_2MASS', '2MASS')
47+
48+
return remote_table
4649

4750

4851
@pytest.mark.remote_data
@@ -83,7 +86,11 @@ def test_xmatch_query(self, xmatch, remote_table):
8386
'e_Jmag', 'e_Hmag', 'e_Kmag', 'Qfl', 'Rfl', 'X', 'MeasureJD']
8487
assert len(table) == 11
8588

86-
assert all(table == remote_table)
89+
for col in table.colnames:
90+
if remote_table[col].dtype.kind == 'U':
91+
assert all(table[col] == remote_table[col])
92+
else:
93+
assert_allclose(table[col], remote_table[col])
8794

8895
def test_xmatch_query_astropy_table(self, xmatch, remote_table):
8996
input_table = Table.read(DATA_DIR / "posList.csv", format="ascii.csv")
@@ -99,8 +106,11 @@ def test_xmatch_query_astropy_table(self, xmatch, remote_table):
99106
'errHalfMaj', 'errHalfMin', 'errPosAng', 'Jmag', 'Hmag', 'Kmag',
100107
'e_Jmag', 'e_Hmag', 'e_Kmag', 'Qfl', 'Rfl', 'X', 'MeasureJD']
101108
assert len(table) == 11
102-
103-
assert all(table == remote_table)
109+
for col in table.colnames:
110+
if remote_table[col].dtype.kind == 'U':
111+
assert all(table[col] == remote_table[col])
112+
else:
113+
assert_allclose(table[col], remote_table[col])
104114

105115
@pytest.mark.skipif('regions' not in sys.modules,
106116
reason="requires astropy-regions")

docs/xmatch/xmatch.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ in the resulting table for demonstration purposes. Finally, ``colRa1`` and
5555
<class 'astropy.table.table.Table'>
5656
>>> print(table)
5757
angDist ra dec 2MASS ... Qfl Rfl X MeasureJD
58+
arcsec ... d
5859
-------- --------- --------- ---------------- ... --- --- --- ------------
5960
1.352044 267.22029 -20.35869 17485281-2021323 ... EEU 226 2 2450950.8609
6061
1.578188 267.22029 -20.35869 17485288-2021328 ... UUB 662 2 2450950.8609

0 commit comments

Comments
 (0)