diff --git a/CHANGES.rst b/CHANGES.rst index b6e9cc50ca..aba8224a57 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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) ================== diff --git a/astroquery/xmatch/core.py b/astroquery/xmatch/core.py index 6d9768a305..57cc954995 100644 --- a/astroquery/xmatch/core.py +++ b/astroquery/xmatch/core.py @@ -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. @@ -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)): diff --git a/astroquery/xmatch/tests/test_xmatch.py b/astroquery/xmatch/tests/test_xmatch.py index d16f150793..d1c9899a86 100644 --- a/astroquery/xmatch/tests/test_xmatch.py +++ b/astroquery/xmatch/tests/test_xmatch.py @@ -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"]