Skip to content

Commit d2373a5

Browse files
authored
Merge pull request #2947 from esdc-esac-esa-int/ESA_gaia_fix_launch_job_for_json_GAIAPCR-1308
Gaia: Fix the astroquery method launch_job to retrieve the Table from the job when a json format is used
2 parents f012164 + edeed3a commit d2373a5

File tree

4 files changed

+71
-22
lines changed

4 files changed

+71
-22
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ gaia
4949

5050
- The method ``get_datalinks`` can be used with the new parameter linking_parameter. It completes PR #2859. [#2936]
5151

52+
- Fix the exception thrown when the functions launch_job and launch_job_async retrieve the data for the json output_format but
53+
do not dump the results to a file . [#2947]
54+
5255

5356
hsa
5457
^^^

astroquery/gaia/tests/test_gaia_remote.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,51 +8,51 @@
88
@pytest.mark.remote_data
99
def test_query_object_columns_with_radius():
1010
# Regression test: `columns` were ignored if `radius` was provided [#2025]
11-
Gaia = GaiaClass()
12-
sc = SkyCoord(ra=0*u.deg, dec=0*u.deg)
13-
table = Gaia.query_object_async(sc, radius=10*u.arcsec, columns=['ra'])
11+
gaia = GaiaClass()
12+
sc = SkyCoord(ra=0 * u.deg, dec=0 * u.deg)
13+
table = gaia.query_object_async(sc, radius=10 * u.arcsec, columns=['ra'])
1414
assert table.colnames == ['ra', 'dist']
1515

1616

1717
@pytest.mark.remote_data
1818
def test_query_object_row_limit():
19-
Gaia = GaiaClass()
19+
gaia = GaiaClass()
2020
coord = SkyCoord(ra=280, dec=-60, unit=(u.degree, u.degree), frame='icrs')
2121
width = u.Quantity(0.1, u.deg)
2222
height = u.Quantity(0.1, u.deg)
23-
r = Gaia.query_object_async(coordinate=coord, width=width, height=height)
23+
r = gaia.query_object_async(coordinate=coord, width=width, height=height)
2424

25-
assert len(r) == Gaia.ROW_LIMIT
25+
assert len(r) == gaia.ROW_LIMIT
2626

27-
Gaia.ROW_LIMIT = 10
28-
r = Gaia.query_object_async(coordinate=coord, width=width, height=height)
27+
gaia.ROW_LIMIT = 10
28+
r = gaia.query_object_async(coordinate=coord, width=width, height=height)
2929

30-
assert len(r) == 10 == Gaia.ROW_LIMIT
30+
assert len(r) == 10 == gaia.ROW_LIMIT
3131

32-
Gaia.ROW_LIMIT = -1
33-
r = Gaia.query_object_async(coordinate=coord, width=width, height=height)
32+
gaia.ROW_LIMIT = -1
33+
r = gaia.query_object_async(coordinate=coord, width=width, height=height)
3434

3535
assert len(r) == 184
3636

3737

3838
@pytest.mark.remote_data
3939
def test_cone_search_row_limit():
40-
Gaia = GaiaClass()
40+
gaia = GaiaClass()
4141
coord = SkyCoord(ra=280, dec=-60, unit=(u.degree, u.degree), frame='icrs')
4242
radius = u.Quantity(0.1, u.deg)
43-
j = Gaia.cone_search_async(coord, radius=radius)
43+
j = gaia.cone_search_async(coord, radius=radius)
4444
r = j.get_results()
4545

46-
assert len(r) == Gaia.ROW_LIMIT
46+
assert len(r) == gaia.ROW_LIMIT
4747

48-
Gaia.ROW_LIMIT = 10
49-
j = Gaia.cone_search_async(coord, radius=radius)
48+
gaia.ROW_LIMIT = 10
49+
j = gaia.cone_search_async(coord, radius=radius)
5050
r = j.get_results()
5151

52-
assert len(r) == 10 == Gaia.ROW_LIMIT
52+
assert len(r) == 10 == gaia.ROW_LIMIT
5353

54-
Gaia.ROW_LIMIT = -1
55-
j = Gaia.cone_search_async(coord, radius=radius)
54+
gaia.ROW_LIMIT = -1
55+
j = gaia.cone_search_async(coord, radius=radius)
5656
r = j.get_results()
5757

5858
assert len(r) == 1218

astroquery/gaia/tests/test_gaiatap.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ def test_launch_job_async_json_format(tmp_path_factory, column_attrs_launch_json
362362
assert results[colname].dtype == attrs.dtype
363363

364364

365-
def test_launch_job_json_format(tmp_path_factory, column_attrs_launch_json, mock_querier_json, ):
365+
def test_launch_job_json_format(tmp_path_factory, column_attrs_launch_json, mock_querier_json):
366366
d = tmp_path_factory.mktemp("data") / 'launch_job.json'
367367
d.write_text(JOB_DATA_QUERIER_ASYNC_JSON, encoding="utf-8")
368368

@@ -390,6 +390,29 @@ def test_launch_job_json_format(tmp_path_factory, column_attrs_launch_json, mock
390390
assert results[colname].dtype == attrs.dtype
391391

392392

393+
def test_launch_job_json_format_no_dump(tmp_path_factory, column_attrs_launch_json, mock_querier_json):
394+
dump_to_file = False
395+
output_format = 'json'
396+
query = "SELECT TOP 1 source_id, ra, dec, parallax from gaiadr3.gaia_source"
397+
398+
job = mock_querier_json.launch_job(query, output_format=output_format, dump_to_file=dump_to_file)
399+
400+
assert job.async_ is False
401+
assert job.get_phase() == "COMPLETED"
402+
assert job.failed is False
403+
# results
404+
results = job.get_results()
405+
406+
assert type(results) is Table
407+
assert 1 == len(results), len(results)
408+
409+
for colname, attrs in column_attrs_launch_json.items():
410+
assert results[colname].name == attrs.name
411+
assert results[colname].description == attrs.description
412+
assert results[colname].unit == attrs.unit
413+
assert results[colname].dtype == attrs.dtype
414+
415+
393416
def test_cone_search_and_changing_MAIN_GAIA_TABLE(mock_querier_async):
394417
# Regression test for #2093 and #2099 - changing the MAIN_GAIA_TABLE
395418
# had no effect.

astroquery/utils/tap/xmlparser/utils.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616
"""
1717
import gzip
1818
import io
19+
import json
1920

2021
from astropy import units as u
2122
from astropy.table import Table as APTable
23+
from astropy.table.table import Table
2224

2325

2426
def util_create_string_from_buffer(buffer):
@@ -35,8 +37,29 @@ def read_http_response(response, output_format, *, correct_units=True):
3537
result = APTable.read(io.BytesIO(gzip.decompress(data.read())), format=astropy_format)
3638
except OSError:
3739
# data is not a valid gzip file by BadGzipFile.
38-
result = APTable.read(data, format=astropy_format)
39-
pass
40+
41+
if output_format == 'json':
42+
43+
data = json.load(response)
44+
45+
if data.get('data') and data.get('metadata'):
46+
47+
column_name = []
48+
for name in data['metadata']:
49+
column_name.append(name['name'])
50+
51+
result = Table(rows=data['data'], names=column_name, masked=True)
52+
53+
for v in data['metadata']:
54+
col_name = v['name']
55+
result[col_name].unit = v['unit']
56+
result[col_name].description = v['description']
57+
result[col_name].meta = {'metadata': v}
58+
59+
else:
60+
result = APTable.read(data, format=astropy_format)
61+
else:
62+
result = APTable.read(data, format=astropy_format)
4063

4164
if correct_units:
4265
modify_unrecognized_table_units(result)

0 commit comments

Comments
 (0)