Skip to content

Commit f0aed87

Browse files
Jorge Fernandez HernandezJorge Fernandez Hernandez
authored andcommitted
GAIAPCR-1308 Fix the astroquery method launch_job to retrieve the Table from the job when a json format is used
1 parent ab7fdaa commit f0aed87

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

astroquery/gaia/tests/test_gaiatap.py

Lines changed: 23 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

@@ -389,6 +389,28 @@ def test_launch_job_json_format(tmp_path_factory, column_attrs_launch_json, mock
389389
assert results[colname].unit == attrs.unit
390390
assert results[colname].dtype == attrs.dtype
391391

392+
def test_launch_job_json_format_no_dump(tmp_path_factory, column_attrs_launch_json, mock_querier_json):
393+
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
392414

393415
def test_cone_search_and_changing_MAIN_GAIA_TABLE(mock_querier_async):
394416
# Regression test for #2093 and #2099 - changing the MAIN_GAIA_TABLE

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(data)
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)