Skip to content

Commit 0663eac

Browse files
authored
Merge pull request #2927 from esdc-esac-esa-int/ESA_gaia_fix_cone_search_async_to_download_json_GAIAPCR-1302
Esa gaia fix the cone_search_async to download json files
2 parents 1f3c12f + 9afd2c8 commit 0663eac

File tree

11 files changed

+796
-47
lines changed

11 files changed

+796
-47
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ gaia
4343
new votable_gzip (which is now the default), to get a compressed votable file (Content-Encoding: gzip and Content-Type: application/x-votable+gzip).
4444
ecsv, to get an ecsv compressed file (Content-Encoding: gzip and Content-Type: text/ecsv+gzip). [#2907]
4545

46+
- For the functions cone_search, cone_search_async, launch_job and launch_job_async the data can be retrieved for the json output_format [##2927]
47+
4648

4749
hsa
4850
^^^

astroquery/gaia/core.py

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
Created on 30 jun. 2016
1414
Modified on 18 Ene. 2022 by mhsarmiento
1515
"""
16+
import json
1617
import os
1718
import shutil
1819
import zipfile
1920
from collections.abc import Iterable
2021
from datetime import datetime, timezone
22+
from pathlib import Path
2123

2224
from astropy import units
2325
from astropy import units as u
@@ -213,8 +215,8 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
213215
By default, this value will be set to False. If it is set to 'true'
214216
the Datalink items tags will not be checked.
215217
format : str, optional, default 'votable_gzip'
216-
loading format. Other available formats are 'votable', 'csv', 'ecsv','json','votable_plain' and 'fits'
217-
output_file : string, optional, default None
218+
loading format. Other available formats are 'votable', 'csv', 'ecsv','votable_plain' and 'fits'
219+
output_file : string or pathlib.PosixPath, optional, default None
218220
file where the results are saved.
219221
If it is not provided, the http response contents are returned.
220222
overwrite_output_file : boolean, optional, default False
@@ -236,6 +238,14 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
236238
output_file = os.path.join(os.getcwd(), temp_dirname, downloadname_formated)
237239
else:
238240
output_file_specified = True
241+
242+
if isinstance(output_file, str):
243+
if not output_file.lower().endswith('.zip'):
244+
output_file = output_file + '.zip'
245+
elif isinstance(output_file, Path):
246+
if not output_file.suffix.endswith('.zip'):
247+
output_file.with_suffix('.zip')
248+
239249
output_file = os.path.abspath(output_file)
240250
if not overwrite_output_file and os.path.exists(output_file):
241251
raise ValueError(f"{output_file} file already exists. Please use overwrite_output_file='True' to "
@@ -308,14 +318,9 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
308318
if output_file_specified:
309319
log.info("output_file = %s" % output_file)
310320

311-
log.debug("List of products available:")
312-
# for key, value in files.items():
313-
# print("Product =", key)
314-
315-
items = sorted([key for key in files.keys()])
316-
for item in items:
317-
# print(f'* {item}')
318-
if verbose:
321+
if log.isEnabledFor(20):
322+
log.debug("List of products available:")
323+
for item in sorted([key for key in files.keys()]):
319324
log.debug("Product = " + item)
320325

321326
return files
@@ -330,7 +335,7 @@ def __get_data_files(output_file, path):
330335
# r=root, d=directories, f = files
331336
for r, d, f in os.walk(path):
332337
for file in f:
333-
if '.fits' in file or '.xml' in file or '.csv' in file:
338+
if file.lower().endswith(('.fits', '.xml', '.csv', '.ecsv')):
334339
files[file] = os.path.join(r, file)
335340

336341
for key, value in files.items():
@@ -356,6 +361,31 @@ def __get_data_files(output_file, path):
356361
fast_reader=False)
357362
tables.append(table)
358363
files[key] = tables
364+
365+
elif '.json' in key:
366+
tables = []
367+
with open(value) as f:
368+
data = json.load(f)
369+
370+
if data.get('data') and data.get('metadata'):
371+
372+
column_name = []
373+
for name in data['metadata']:
374+
column_name.append(name['name'])
375+
376+
result = Table(rows=data['data'], names=column_name, masked=True)
377+
378+
for v in data['metadata']:
379+
col_name = v['name']
380+
result[col_name].unit = v['unit']
381+
result[col_name].description = v['description']
382+
result[col_name].meta = {'metadata': v}
383+
384+
files[key] = result
385+
else:
386+
tables.append(Table.read(value, format='pandas.json'))
387+
files[key] = tables
388+
359389
return files
360390

361391
def get_datalinks(self, ids, *, verbose=False):
@@ -539,7 +569,7 @@ def __cone_search(self, coordinate, radius, *, table_name=None,
539569
when the job is executed in asynchronous mode, this flag specifies
540570
whether the execution will wait until results are available
541571
output_file : str, optional, default None
542-
file name where the results are saved if dumpToFile is True.
572+
file name where the results are saved if dump_to_file is True.
543573
If this parameter is not provided, the jobid is used instead
544574
output_format : str, optional, default 'votable_gzip'
545575
results format. Available formats are: 'votable', 'votable_plain',
@@ -627,7 +657,7 @@ def cone_search(self, coordinate, *, radius=None,
627657
dec_column_name : str, optional, default dec column in main gaia table
628658
dec column doing the cone search against
629659
output_file : str, optional, default None
630-
file name where the results are saved if dumpToFile is True.
660+
file name where the results are saved if dump_to_file is True.
631661
If this parameter is not provided, the jobid is used instead
632662
output_format : str, optional, default 'votable_gzip'
633663
results format. Available formats are: 'votable', 'votable_plain',
@@ -681,7 +711,7 @@ def cone_search_async(self, coordinate, *, radius=None,
681711
specifies whether
682712
the execution will wait until results are available
683713
output_file : str, optional, default None
684-
file name where the results are saved if dumpToFile is True.
714+
file name where the results are saved if dump_to_file is True.
685715
If this parameter is not provided, the jobid is used instead
686716
output_format : str, optional, default 'votable_gzip'
687717
results format. Available formats are: 'votable', 'votable_plain',
@@ -847,7 +877,7 @@ def launch_job(self, query, *, name=None, output_file=None,
847877
name : str, optional, default None
848878
custom name defined by the user for the job that is going to be created
849879
output_file : str, optional, default None
850-
file name where the results are saved if dumpToFile is True.
880+
file name where the results are saved if dump_to_file is True.
851881
If this parameter is not provided, the jobid is used instead
852882
output_format : str, optional, default 'votable_gzip'
853883
results format. Available formats are: 'votable_gzip', 'votable', 'votable_plain',
@@ -892,7 +922,7 @@ def launch_job_async(self, query, *, name=None, output_file=None,
892922
name : str, optional, default None
893923
custom name defined by the user for the job that is going to be created
894924
output_file : str, optional, default None
895-
file name where the results are saved if dumpToFile is True.
925+
file name where the results are saved if dump_to_file is True.
896926
If this parameter is not provided, the jobid is used instead
897927
output_format : str, optional, default 'votable_gzip'
898928
results format. Available formats are: 'votable_gzip', 'votable', 'votable_plain',

0 commit comments

Comments
 (0)