Skip to content

Commit ade38dc

Browse files
authored
Merge pull request #3194 from cds-astro/fix-xmatch-error-message-on-wrong-catalog-name
Make the API more flexible (allow to ommit `vizier:` when crossmatching with a vizier catalog)
2 parents dca9873 + 6b0af57 commit ade38dc

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

CHANGES.rst

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ ipac.nexsci.nasa_exoplanet_archive
1313

1414
- Fixed InvalidTableError for DI_STARS_EXEP and TD tables. [#3189]
1515

16+
xmatch
17+
^^^^^^
18+
19+
- the API is more flexible: you can now ommit the ``vizier:`` before the catalog name
20+
when crossmatching with a vizier table [#3194]
21+
1622

1723
Infrastructure, Utility and Other Changes and Additions
1824
-------------------------------------------------------

astroquery/xmatch/core.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def query_async(self, cat1, cat2, max_distance, *, colRA1=None, colDec1=None,
9494
The HTTP response returned from the service.
9595
"""
9696
if max_distance > 180 * u.arcsec:
97-
raise ValueError('max_distance argument must not be greater than 180')
97+
raise ValueError('max_distance argument must not be greater than 180".')
9898
payload = {'request': 'xmatch',
9999
'distMaxArcsec': max_distance.to(u.arcsec).value,
100100
'RESPONSEFORMAT': 'votable',
@@ -126,6 +126,10 @@ def _prepare_sending_table(self, cat_index, payload, kwargs, cat, colRA, colDec)
126126
'''
127127
catstr = 'cat{0}'.format(cat_index)
128128
if isinstance(cat, str):
129+
if (self.is_table_available(cat) and not cat.startswith("vizier:")):
130+
# if we detect that the given name is a vizier table, we can make
131+
# it comply to the API, see issue #3191
132+
cat = f"vizier:{cat}"
129133
payload[catstr] = cat
130134
else:
131135
# create the dictionary of uploaded files
@@ -181,7 +185,7 @@ def is_table_available(self, table_id):
181185
if not isinstance(table_id, str):
182186
return False
183187

184-
if (table_id[:7] == 'vizier:'):
188+
if table_id.startswith('vizier:'):
185189
table_id = table_id[7:]
186190

187191
return table_id in self.get_available_tables()

astroquery/xmatch/tests/test_xmatch.py

+34-3
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,9 @@ def request_mockreturn(method, url, data, **kwargs):
4646

4747

4848
def test_xmatch_query_invalid_max_distance():
49-
with pytest.raises(ValueError) as ex:
49+
with pytest.raises(ValueError,
50+
match='max_distance argument must not be greater than 180"'):
5051
XMatch().query_async('', '', 181 * arcsec)
51-
assert str(ex.value) == (
52-
'max_distance argument must not be greater than 180')
5352

5453

5554
def test_get_available_tables(monkeypatch):
@@ -125,3 +124,35 @@ def test_table_not_available(monkeypatch):
125124
# reproduces #1464
126125
with pytest.raises(ValueError, match=f"'{re.escape(cat1)}' is not available *"):
127126
xm.query_async(cat1=cat1, cat2=cat2, max_distance=5 * arcsec)
127+
128+
129+
def test_prepare_sending_tables(monkeypatch):
130+
xm = XMatch()
131+
monkeypatch.setattr(xm, '_request', request_mockreturn)
132+
133+
# if it's a valid vizier table, prepend vizier:
134+
payload = {}
135+
xm._prepare_sending_table(1, payload, {}, "II/316/gps6", None, None)
136+
assert payload == {'cat1': 'vizier:II/316/gps6'}
137+
# also works if vizier: is already given by the user
138+
payload = {}
139+
xm._prepare_sending_table(1, payload, {}, "vizier:II/316/gps6", None, None)
140+
assert payload == {'cat1': 'vizier:II/316/gps6'}
141+
142+
# otherwise colRa1 and colDec1 have to be given
143+
with pytest.raises(ValueError, match="'test' is not available on the XMatch server."):
144+
xm._prepare_sending_table(1, payload, {}, "test", None, None)
145+
payload = {}
146+
# this mimics the url case
147+
xm._prepare_sending_table(1, payload, {}, "test", "ra", "dec")
148+
assert payload == {'cat1': 'test', 'colRA1': 'ra', 'colDec1': 'dec'}
149+
150+
# if cat is not a string, then the payload has to include the file
151+
payload = {}
152+
kwargs = {}
153+
cat = Table({'a': [0, 1, 2], 'b': [3, 4, 5]})
154+
xm._prepare_sending_table(1, payload, kwargs, cat, "a", "b")
155+
assert payload == {'colRA1': 'a', 'colDec1': 'b'}
156+
assert (kwargs == {'files': {'cat1': ('cat1.csv', 'a,b\n0,3\n1,4\n2,5\n')}}
157+
# for windows systems
158+
or kwargs == {'files': {'cat1': ('cat1.csv', 'a,b\r\n0,3\r\n1,4\r\n2,5\r\n')}})

astroquery/xmatch/tests/test_xmatch_remote.py

+6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ def test_xmatch_query_astropy_table(self, xmatch, remote_table):
112112
else:
113113
assert_allclose(table[col], remote_table[col])
114114

115+
def test_two_remote_tables(self, xmatch):
116+
# we also test that omitting vizier: works
117+
result = xmatch.query("simbad", "J/A+A/113/61/table2", max_distance=0.5*arcsec)
118+
assert {"main_id", "MH"}.issubset(result.colnames)
119+
assert all(result["angDist"] < 0.5)
120+
115121
@pytest.mark.skipif('regions' not in sys.modules,
116122
reason="requires astropy-regions")
117123
def test_xmatch_query_with_cone_area(self, xmatch):

0 commit comments

Comments
 (0)