13
13
Created on 30 jun. 2016
14
14
Modified on 18 Ene. 2022 by mhsarmiento
15
15
"""
16
+ import json
16
17
import os
17
18
import shutil
18
19
import zipfile
19
20
from collections .abc import Iterable
20
21
from datetime import datetime , timezone
22
+ from pathlib import Path
21
23
22
24
from astropy import units
23
25
from astropy import units as u
@@ -213,8 +215,8 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
213
215
By default, this value will be set to False. If it is set to 'true'
214
216
the Datalink items tags will not be checked.
215
217
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
218
220
file where the results are saved.
219
221
If it is not provided, the http response contents are returned.
220
222
overwrite_output_file : boolean, optional, default False
@@ -236,6 +238,14 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
236
238
output_file = os .path .join (os .getcwd (), temp_dirname , downloadname_formated )
237
239
else :
238
240
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
+
239
249
output_file = os .path .abspath (output_file )
240
250
if not overwrite_output_file and os .path .exists (output_file ):
241
251
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
308
318
if output_file_specified :
309
319
log .info ("output_file = %s" % output_file )
310
320
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 ()]):
319
324
log .debug ("Product = " + item )
320
325
321
326
return files
@@ -330,7 +335,7 @@ def __get_data_files(output_file, path):
330
335
# r=root, d=directories, f = files
331
336
for r , d , f in os .walk (path ):
332
337
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' )) :
334
339
files [file ] = os .path .join (r , file )
335
340
336
341
for key , value in files .items ():
@@ -356,6 +361,31 @@ def __get_data_files(output_file, path):
356
361
fast_reader = False )
357
362
tables .append (table )
358
363
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
+
359
389
return files
360
390
361
391
def get_datalinks (self , ids , * , verbose = False ):
@@ -539,7 +569,7 @@ def __cone_search(self, coordinate, radius, *, table_name=None,
539
569
when the job is executed in asynchronous mode, this flag specifies
540
570
whether the execution will wait until results are available
541
571
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.
543
573
If this parameter is not provided, the jobid is used instead
544
574
output_format : str, optional, default 'votable_gzip'
545
575
results format. Available formats are: 'votable', 'votable_plain',
@@ -627,7 +657,7 @@ def cone_search(self, coordinate, *, radius=None,
627
657
dec_column_name : str, optional, default dec column in main gaia table
628
658
dec column doing the cone search against
629
659
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.
631
661
If this parameter is not provided, the jobid is used instead
632
662
output_format : str, optional, default 'votable_gzip'
633
663
results format. Available formats are: 'votable', 'votable_plain',
@@ -681,7 +711,7 @@ def cone_search_async(self, coordinate, *, radius=None,
681
711
specifies whether
682
712
the execution will wait until results are available
683
713
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.
685
715
If this parameter is not provided, the jobid is used instead
686
716
output_format : str, optional, default 'votable_gzip'
687
717
results format. Available formats are: 'votable', 'votable_plain',
@@ -847,7 +877,7 @@ def launch_job(self, query, *, name=None, output_file=None,
847
877
name : str, optional, default None
848
878
custom name defined by the user for the job that is going to be created
849
879
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.
851
881
If this parameter is not provided, the jobid is used instead
852
882
output_format : str, optional, default 'votable_gzip'
853
883
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,
892
922
name : str, optional, default None
893
923
custom name defined by the user for the job that is going to be created
894
924
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.
896
926
If this parameter is not provided, the jobid is used instead
897
927
output_format : str, optional, default 'votable_gzip'
898
928
results format. Available formats are: 'votable_gzip', 'votable', 'votable_plain',
0 commit comments