Skip to content

Commit

Permalink
GAIAPCR-1302 Now for output format json a table is returned
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorge Fernandez Hernandez authored and Jorge Fernandez Hernandez committed Jan 21, 2024
1 parent aa7f848 commit ede05b6
Show file tree
Hide file tree
Showing 9 changed files with 747 additions and 39 deletions.
31 changes: 24 additions & 7 deletions astroquery/gaia/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
Created on 30 jun. 2016
Modified on 18 Ene. 2022 by mhsarmiento
"""
import json
import os
import shutil
import zipfile
from collections.abc import Iterable
from datetime import datetime, timezone
import json

from pathlib import Path

from astropy import units
from astropy import units as u
Expand Down Expand Up @@ -215,7 +215,7 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
By default, this value will be set to False. If it is set to 'true'
the Datalink items tags will not be checked.
format : str, optional, default 'votable_gzip'
loading format. Other available formats are 'votable', 'csv', 'ecsv','json','votable_plain' and 'fits'
loading format. Other available formats are 'votable', 'csv', 'ecsv','votable_plain' and 'fits'
output_file : string, optional, default None
file where the results are saved.
If it is not provided, the http response contents are returned.
Expand All @@ -238,6 +238,14 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
output_file = os.path.join(os.getcwd(), temp_dirname, downloadname_formated)
else:
output_file_specified = True

if isinstance(output_file, str):
if not output_file.lower().endswith('.zip'):
output_file = output_file + '.zip'
elif isinstance(output_file, Path):
if not output_file.suffix.endswith('.zip'):
output_file.with_suffix('.zip')

output_file = os.path.abspath(output_file)
if not overwrite_output_file and os.path.exists(output_file):
raise ValueError(f"{output_file} file already exists. Please use overwrite_output_file='True' to "
Expand Down Expand Up @@ -327,7 +335,7 @@ def __get_data_files(output_file, path):
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if '.fits' in file or '.xml' in file or '.csv' in file:
if file.lower().endswith(('.fits', '.xml', '.csv', '.ecsv')):
files[file] = os.path.join(r, file)

for key, value in files.items():
Expand Down Expand Up @@ -361,10 +369,19 @@ def __get_data_files(output_file, path):

if data.get('data') and data.get('metadata'):

tables.append(Table.read(json.dumps({"data": data['data']}), format='pandas.json'))
tables.append(Table.read(json.dumps({"metadata": data['metadata']}), format='pandas.json'))
column_name = []
for name in data['metadata']:
column_name.append(name['name'])

files[key] = tables
result = Table(rows=data['data'], names=column_name, masked=True)

for v in data['metadata']:
col_name = v['name']
result[col_name].unit = v['unit']
result[col_name].description = v['description']
result[col_name].meta = {'metadata': v}

files[key] = result
else:
tables.append(Table.read(value, format='pandas.json'))
files[key] = tables
Expand Down
210 changes: 210 additions & 0 deletions astroquery/gaia/tests/data/cone_search.json

Large diffs are not rendered by default.

210 changes: 210 additions & 0 deletions astroquery/gaia/tests/data/cone_search_async.json

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions astroquery/gaia/tests/data/launch_job.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{"metadata":
[
{"name": "source_id", "datatype": "long", "xtype": null, "arraysize": null, "description": "Unique source identifier (unique within a particular Data Release)", "unit": null, "ucd": "meta.id", "utype": null},
{"name": "ra", "datatype": "double", "xtype": null, "arraysize": null, "description": "Right ascension", "unit": "deg", "ucd": "pos.eq.ra;meta.main", "utype": "stc:AstroCoords.Position3D.Value3.C1"},
{"name": "dec", "datatype": "double", "xtype": null, "arraysize": null, "description": "Declination", "unit": "deg", "ucd": "pos.eq.dec;meta.main", "utype": "stc:AstroCoords.Position3D.Value3.C2"},
{"name": "parallax", "datatype": "double", "xtype": null, "arraysize": null, "description": "Parallax", "unit": "mas", "ucd": "pos.parallax.trig", "utype": null}
],
"data":
[
[5937144193345094528,251.46088614856114,-50.77147594086373,0.029992199777711163]
]
}
12 changes: 12 additions & 0 deletions astroquery/gaia/tests/data/launch_job_async.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{"metadata":
[
{"name": "source_id", "datatype": "long", "xtype": null, "arraysize": null, "description": "Unique source identifier (unique within a particular Data Release)", "unit": null, "ucd": "meta.id", "utype": null},
{"name": "ra", "datatype": "double", "xtype": null, "arraysize": null, "description": "Right ascension", "unit": "deg", "ucd": "pos.eq.ra;meta.main", "utype": "stc:AstroCoords.Position3D.Value3.C1"},
{"name": "dec", "datatype": "double", "xtype": null, "arraysize": null, "description": "Declination", "unit": "deg", "ucd": "pos.eq.dec;meta.main", "utype": "stc:AstroCoords.Position3D.Value3.C2"},
{"name": "parallax", "datatype": "double", "xtype": null, "arraysize": null, "description": "Parallax", "unit": "mas", "ucd": "pos.parallax.trig", "utype": null}
],
"data":
[
[5937144193345094528,251.46088614856114,-50.77147594086373,0.029992199777711163]
]
}
Loading

0 comments on commit ede05b6

Please sign in to comment.