Skip to content

Commit

Permalink
difficult fix to xmatch: apparently files weren't being passed in a
Browse files Browse the repository at this point in the history
format acceptable to the requests package, which makes me question how
it ever worked.  There are now some serious validation tests included
too
  • Loading branch information
keflavich committed Nov 21, 2016
1 parent cb155d4 commit a93e0fa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
3 changes: 3 additions & 0 deletions astroquery/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ def hash(self):
key=_replace_none_iterable)))
entry = tuple((k_,v_.read()) if hasattr(v_,'read')
else (k_,v_) for k_,v_ in entry)
for k_,v_ in entry:
if hasattr(v_,'read') and hasattr(v_,'seek'):
v_.seek(0)

request_key += entry
elif isinstance(k, tuple) or isinstance(k, list):
Expand Down
19 changes: 14 additions & 5 deletions astroquery/xmatch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class XMatchClass(BaseQuery):
TIMEOUT = conf.timeout

def query(self, cat1, cat2, max_distance, colRA1=None, colDec1=None,
colRA2=None, colDec2=None, cache=True):
colRA2=None, colDec2=None, cache=True, get_query_payload=False):
"""
Query the `CDS cross-match service
<http://cdsxmatch.u-strasbg.fr/xmatch>`_ by finding matches between
Expand Down Expand Up @@ -58,12 +58,16 @@ def query(self, cat1, cat2, max_distance, colRA1=None, colDec1=None,
Query results table
"""
response = self.query_async(cat1, cat2, max_distance, colRA1, colDec1,
colRA2, colDec2, cache=cache)
colRA2, colDec2, cache=cache,
get_query_payload=get_query_payload)
if get_query_payload:
return response
return ascii.read(response.text, format='csv')

@prepend_docstr_noreturns("\n" + query.__doc__)
def query_async(self, cat1, cat2, max_distance, colRA1=None, colDec1=None,
colRA2=None, colDec2=None, cache=True):
colRA2=None, colDec2=None, cache=True,
get_query_payload=False):
"""
Returns
-------
Expand All @@ -83,8 +87,13 @@ def query_async(self, cat1, cat2, max_distance, colRA1=None, colDec1=None,
self._prepare_sending_table(1, payload, kwargs, cat1, colRA1, colDec1)
self._prepare_sending_table(2, payload, kwargs, cat2, colRA2, colDec2)

if get_query_payload:
return payload, kwargs

response = self._request(method='POST', url=self.URL, data=payload,
timeout=self.TIMEOUT, cache=cache, **kwargs)
response.raise_for_status()

return response

def _prepare_sending_table(self, i, payload, kwargs, cat, colRA, colDec):
Expand All @@ -101,10 +110,10 @@ def _prepare_sending_table(self, i, payload, kwargs, cat, colRA, colDec):
fp = six.StringIO()
cat.write(fp, format='ascii.csv')
fp.seek(0)
kwargs['files'] = {catstr: fp}
kwargs['files'] = {catstr: ('cat1.csv', fp.read())}
else:
# assume it's a file-like object, support duck-typing
kwargs['files'] = {catstr: cat}
kwargs['files'] = {catstr: ('cat1.csv', cat.read())}
if not self.is_table_available(cat):
if ((colRA is None) or (colDec is None)):
raise ValueError('Specify the name of the RA/Dec columns in' +
Expand Down
19 changes: 18 additions & 1 deletion astroquery/xmatch/tests/test_xmatch_remote.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import os.path
import os

from astropy.tests.helper import pytest, remote_data
from astropy.table import Table
from astropy.units import arcsec
from astropy.io import ascii

from ...xmatch import XMatch

Expand Down Expand Up @@ -38,7 +40,7 @@ def test_xmatch_is_avail_table(xmatch):

@remote_data
def test_xmatch_query(xmatch):
with open(os.path.join(DATA_DIR, 'posList.csv')) as pos_list:
with open(os.path.join(DATA_DIR, 'posList.csv'), 'r') as pos_list:
table = xmatch.query(
cat1=pos_list, cat2='vizier:II/246/out', max_distance=5 * arcsec,
colRA1='ra', colDec1='dec')
Expand All @@ -49,6 +51,8 @@ def test_xmatch_query(xmatch):
'e_Jmag', 'e_Hmag', 'e_Kmag', 'Qfl', 'Rfl', 'X', 'MeasureJD']
assert len(table) == 11

http_test_table = http_test()
assert all(table == http_test_table)

@remote_data
def test_xmatch_query_astropy_table(xmatch):
Expand All @@ -63,3 +67,16 @@ def test_xmatch_query_astropy_table(xmatch):
'errHalfMaj', 'errHalfMin', 'errPosAng', 'Jmag', 'Hmag', 'Kmag',
'e_Jmag', 'e_Hmag', 'e_Kmag', 'Qfl', 'Rfl', 'X', 'MeasureJD']
assert len(table) == 11

http_test_table = http_test()
assert all(table == http_test_table)

@remote_data
def http_test():
# this can be used to check that the API is still functional & doing as expected
infile = os.path.join(DATA_DIR, 'posList.csv')
outfile = os.path.join(DATA_DIR, 'http_result.csv')
os.system('curl -X POST -F request=xmatch -F distMaxArcsec=5 -F RESPONSEFORMAT=csv -F cat1=@{1} -F colRA1=ra -F colDec1=dec -F cat2=vizier:II/246/out http://cdsxmatch.u-strasbg.fr/xmatch/api/v1/sync > {0}'.format(outfile,
infile))
table = ascii.read(outfile, format='csv')
return table

0 comments on commit a93e0fa

Please sign in to comment.