Skip to content

Commit 9faae2b

Browse files
authored
Merge pull request #3116 from cds-astro/fix-xmatch-two-local-files
[XMatch] fix: upload of two local tables is now possible
2 parents 88b6409 + 5c4a02b commit 9faae2b

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

CHANGES.rst

+6
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ mpc
217217
- Rename ``MPC.get_mpc_object_endpoint`` to ``MPC._get_mpc_object_endpoint`` to
218218
indicate that it is a private method. [#3089]
219219

220+
xmatch
221+
^^^^^^
222+
223+
- Fix xmatch query for two local tables. The second table was written over the first one,
224+
resulting in a confusing "missing cat1" error. [#3116]
225+
220226

221227
0.4.7 (2024-03-08)
222228
==================

astroquery/xmatch/core.py

+16-11
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def query(self, cat1, cat2, max_distance, *,
4242
If the table is uploaded or accessed through a URL, it must be
4343
in VOTable or CSV format with the positions in J2000
4444
equatorial frame and as decimal degrees numbers.
45-
cat2 : str or file
45+
cat2 : str, file or `~astropy.table.Table`
4646
Identifier of the second table. Follows the same rules as *cat1*.
4747
max_distance : `~astropy.units.Quantity`
4848
Maximum distance to look for counterparts.
@@ -127,17 +127,22 @@ def _prepare_sending_table(self, cat_index, payload, kwargs, cat, colRA, colDec)
127127
catstr = 'cat{0}'.format(cat_index)
128128
if isinstance(cat, str):
129129
payload[catstr] = cat
130-
elif isinstance(cat, Table):
131-
# write the Table's content into a new, temporary CSV-file
132-
# so that it can be pointed to via the `files` option
133-
# file will be closed when garbage-collected
134-
fp = StringIO()
135-
cat.write(fp, format='ascii.csv')
136-
fp.seek(0)
137-
kwargs['files'] = {catstr: ('cat1.csv', fp.read())}
138130
else:
139-
# assume it's a file-like object, support duck-typing
140-
kwargs['files'] = {catstr: ('cat1.csv', cat.read())}
131+
# create the dictionary of uploaded files
132+
if "files" not in kwargs:
133+
kwargs["files"] = {}
134+
if isinstance(cat, Table):
135+
# write the Table's content into a new, temporary CSV-file
136+
# so that it can be pointed to via the `files` option
137+
# file will be closed when garbage-collected
138+
139+
fp = StringIO()
140+
cat.write(fp, format='ascii.csv')
141+
fp.seek(0)
142+
kwargs['files'].update({catstr: (f'cat{cat_index}.csv', fp.read())})
143+
else:
144+
# assume it's a file-like object, support duck-typing
145+
kwargs['files'].update({catstr: (f'cat{cat_index}.csv', cat.read())})
141146

142147
if not self.is_table_available(cat):
143148
if ((colRA is None) or (colDec is None)):

astroquery/xmatch/tests/test_xmatch.py

+10
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,13 @@ 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+
def test_two_local_tables():
110+
table1 = Table({'a': [1], 'b': [1], 'c': [1]})
111+
table2 = Table({'a': [1], 'b': [1], 'c': [1]})
112+
payload = XMatch().query(cat1=table1, cat2=table2,
113+
colRA1="a", colDec1="b", colRA2="a", colDec2="b",
114+
max_distance=1 * arcsec,
115+
get_query_payload=True)
116+
assert 'cat1' in payload[1]["files"] and 'cat2' in payload[1]["files"]

0 commit comments

Comments
 (0)