19
19
import zipfile
20
20
from collections .abc import Iterable
21
21
from datetime import datetime , timezone
22
- from pathlib import Path
23
22
24
23
from astropy import units
25
24
from astropy import units as u
@@ -170,7 +169,7 @@ def logout(self, *, verbose=False):
170
169
171
170
def load_data (self , ids , * , data_release = None , data_structure = 'INDIVIDUAL' , retrieval_type = "ALL" ,
172
171
linking_parameter = 'SOURCE_ID' , valid_data = False , band = None , avoid_datatype_check = False ,
173
- format = "votable" , output_file = None , overwrite_output_file = False , verbose = False ):
172
+ format = "votable" , dump_to_file = False , overwrite_output_file = False , verbose = False ):
174
173
"""Loads the specified table
175
174
TAP+ only
176
175
@@ -217,10 +216,8 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
217
216
By default, this value will be set to False. If it is set to 'true'
218
217
the Datalink items tags will not be checked.
219
218
format : str, optional, default 'votable'
220
- loading format. Other available formats are 'csv', 'ecsv','votable_plain' and 'fits'
221
- output_file : string or pathlib.PosixPath, optional, default None
222
- file where the results are saved.
223
- If it is not provided, the http response contents are returned.
219
+ loading format. Other available formats are 'csv', 'ecsv','votable_plain', 'json' and 'fits'
220
+ dump_to_file: boolean, optional, default False
224
221
overwrite_output_file : boolean, optional, default False
225
222
To overwrite the output_file if it already exists.
226
223
verbose : bool, optional, default 'False'
@@ -235,26 +232,30 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
235
232
temp_dirname = "temp_" + now_formatted
236
233
downloadname_formated = "download_" + now_formatted
237
234
235
+ overwrite_output_file = True
238
236
output_file_specified = False
239
- if output_file is None :
237
+
238
+ if not dump_to_file :
240
239
output_file = os .path .join (os .getcwd (), temp_dirname , downloadname_formated )
241
240
else :
241
+ output_file = 'datalink_output.zip'
242
242
output_file_specified = True
243
-
244
- if isinstance (output_file , str ):
245
- if not output_file .lower ().endswith ('.zip' ):
246
- output_file = output_file + '.zip'
247
- elif isinstance (output_file , Path ):
248
- if not output_file .suffix .endswith ('.zip' ):
249
- output_file .with_suffix ('.zip' )
250
-
251
243
output_file = os .path .abspath (output_file )
252
244
if not overwrite_output_file and os .path .exists (output_file ):
253
- raise ValueError (f"{ output_file } file already exists. Please use overwrite_output_file='True' to "
254
- f"overwrite output file." )
245
+ print (f"{ output_file } file already exists and will be overwritten" )
255
246
256
247
path = os .path .dirname (output_file )
257
248
249
+ log .debug (f"Directory where the data will be saved: { path } " )
250
+
251
+ if path != '' :
252
+ try :
253
+ os .mkdir (path )
254
+ except FileExistsError :
255
+ log .error ("Path %s already exist" % path )
256
+ except OSError :
257
+ log .error ("Creation of the directory %s failed" % path )
258
+
258
259
if avoid_datatype_check is False :
259
260
# we need to check params
260
261
rt = str (retrieval_type ).upper ()
@@ -297,14 +298,7 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
297
298
if linking_parameter != 'SOURCE_ID' :
298
299
params_dict ['LINKING_PARAMETER' ] = linking_parameter
299
300
300
- if path != '' :
301
- try :
302
- os .mkdir (path )
303
- except FileExistsError :
304
- log .error ("Path %s already exist" % path )
305
- except OSError :
306
- log .error ("Creation of the directory %s failed" % path )
307
-
301
+ files = dict ()
308
302
try :
309
303
self .__gaiadata .load_data (params_dict = params_dict , output_file = output_file , verbose = verbose )
310
304
files = Gaia .__get_data_files (output_file = output_file , path = path )
@@ -313,6 +307,10 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
313
307
finally :
314
308
if not output_file_specified :
315
309
shutil .rmtree (path )
310
+ else :
311
+ for file in files .keys ():
312
+ os .remove (os .path .join (os .getcwd (), path , file )
313
+ )
316
314
317
315
if verbose :
318
316
if output_file_specified :
@@ -328,17 +326,21 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
328
326
@staticmethod
329
327
def __get_data_files (output_file , path ):
330
328
files = {}
331
- if zipfile .is_zipfile (output_file ):
332
- with zipfile .ZipFile (output_file , 'r' ) as zip_ref :
333
- zip_ref .extractall (os .path .dirname (output_file ))
329
+ extracted_files = []
330
+
331
+ with zipfile .ZipFile (output_file , "r" ) as zip_ref :
332
+ for name in zip_ref .namelist ():
333
+ local_file_path = zip_ref .extract (name , os .path .dirname (output_file ))
334
+ extracted_files .append (local_file_path )
334
335
335
336
# r=root, d=directories, f = files
336
337
for r , d , f in os .walk (path ):
337
338
for file in f :
338
- if file . lower (). endswith (( '.fits' , '.xml' , '.csv' , '.ecsv' )) :
339
+ if file in extracted_files :
339
340
files [file ] = os .path .join (r , file )
340
341
341
342
for key , value in files .items ():
343
+
342
344
if '.fits' in key :
343
345
tables = []
344
346
with fits .open (value ) as hduList :
@@ -348,6 +350,7 @@ def __get_data_files(output_file, path):
348
350
Gaia .correct_table_units (table )
349
351
tables .append (table )
350
352
files [key ] = tables
353
+
351
354
elif '.xml' in key :
352
355
tables = []
353
356
for table in votable .parse (value ).iter_tables ():
0 commit comments