Skip to content

Commit

Permalink
Merge pull request #3116 from cds-astro/fix-xmatch-two-local-files
Browse files Browse the repository at this point in the history
[XMatch] fix: upload of two local tables is now possible
  • Loading branch information
bsipocz authored Oct 4, 2024
2 parents 88b6409 + 5c4a02b commit 9faae2b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ mpc
- Rename ``MPC.get_mpc_object_endpoint`` to ``MPC._get_mpc_object_endpoint`` to
indicate that it is a private method. [#3089]

xmatch
^^^^^^

- Fix xmatch query for two local tables. The second table was written over the first one,
resulting in a confusing "missing cat1" error. [#3116]


0.4.7 (2024-03-08)
==================
Expand Down
27 changes: 16 additions & 11 deletions astroquery/xmatch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def query(self, cat1, cat2, max_distance, *,
If the table is uploaded or accessed through a URL, it must be
in VOTable or CSV format with the positions in J2000
equatorial frame and as decimal degrees numbers.
cat2 : str or file
cat2 : str, file or `~astropy.table.Table`
Identifier of the second table. Follows the same rules as *cat1*.
max_distance : `~astropy.units.Quantity`
Maximum distance to look for counterparts.
Expand Down Expand Up @@ -127,17 +127,22 @@ def _prepare_sending_table(self, cat_index, payload, kwargs, cat, colRA, colDec)
catstr = 'cat{0}'.format(cat_index)
if isinstance(cat, str):
payload[catstr] = cat
elif isinstance(cat, Table):
# write the Table's content into a new, temporary CSV-file
# so that it can be pointed to via the `files` option
# file will be closed when garbage-collected
fp = StringIO()
cat.write(fp, format='ascii.csv')
fp.seek(0)
kwargs['files'] = {catstr: ('cat1.csv', fp.read())}
else:
# assume it's a file-like object, support duck-typing
kwargs['files'] = {catstr: ('cat1.csv', cat.read())}
# create the dictionary of uploaded files
if "files" not in kwargs:
kwargs["files"] = {}
if isinstance(cat, Table):
# write the Table's content into a new, temporary CSV-file
# so that it can be pointed to via the `files` option
# file will be closed when garbage-collected

fp = StringIO()
cat.write(fp, format='ascii.csv')
fp.seek(0)
kwargs['files'].update({catstr: (f'cat{cat_index}.csv', fp.read())})
else:
# assume it's a file-like object, support duck-typing
kwargs['files'].update({catstr: (f'cat{cat_index}.csv', cat.read())})

if not self.is_table_available(cat):
if ((colRA is None) or (colDec is None)):
Expand Down
10 changes: 10 additions & 0 deletions astroquery/xmatch/tests/test_xmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,13 @@ def test_xmatch_query_cat1_table_local(monkeypatch):
'errHalfMaj', 'errHalfMin', 'errPosAng', 'Jmag', 'Hmag', 'Kmag',
'e_Jmag', 'e_Hmag', 'e_Kmag', 'Qfl', 'Rfl', 'X', 'MeasureJD']
assert len(table) == 11


def test_two_local_tables():
table1 = Table({'a': [1], 'b': [1], 'c': [1]})
table2 = Table({'a': [1], 'b': [1], 'c': [1]})
payload = XMatch().query(cat1=table1, cat2=table2,
colRA1="a", colDec1="b", colRA2="a", colDec2="b",
max_distance=1 * arcsec,
get_query_payload=True)
assert 'cat1' in payload[1]["files"] and 'cat2' in payload[1]["files"]

0 comments on commit 9faae2b

Please sign in to comment.