13
13
Created on 30 jun. 2016
14
14
Modified on 18 Ene. 2022 by mhsarmiento
15
15
"""
16
+ import datetime
16
17
import json
17
18
import os
18
19
import shutil
19
20
import zipfile
20
21
from collections .abc import Iterable
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
28
27
from astropy .io import votable
29
28
from astropy .table import Table
30
29
from astropy .units import Quantity
30
+ from astropy .utils .decorators import deprecated_renamed_argument
31
31
from requests import HTTPError
32
32
33
33
from astroquery import log
@@ -168,9 +168,11 @@ def logout(self, *, verbose=False):
168
168
except HTTPError :
169
169
log .error ("Error logging out data server" )
170
170
171
+ @deprecated_renamed_argument ("output_file" , None , since = "0.4.8" )
171
172
def load_data (self , ids , * , data_release = None , data_structure = 'INDIVIDUAL' , retrieval_type = "ALL" ,
172
173
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 ):
174
+ format = "votable" , dump_to_file = False , overwrite_output_file = False , verbose = False ,
175
+ output_file = None ):
174
176
"""Loads the specified table
175
177
TAP+ only
176
178
@@ -218,44 +220,53 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
218
220
By default, this value will be set to False. If it is set to 'true'
219
221
the Datalink items tags will not be checked.
220
222
format : str, optional, default 'votable'
221
- loading format. Other available formats are 'csv', 'ecsv','votable_plain' and 'fits'
222
- output_file : string or pathlib.PosixPath, optional, default None
223
- file where the results are saved.
224
- If it is not provided, the http response contents are returned.
223
+ loading format. Other available formats are 'csv', 'ecsv','votable_plain', 'json' and 'fits'
224
+ dump_to_file: boolean, optional, default False.
225
+ If it is true, a compressed directory named "datalink_output_<time_stamp>.zip" with all the DataLink
226
+ files is made in the current working directory. The <time_stamp> format follows the ISO 8601 standard:
227
+ "yyyymmddThhmmss".
225
228
overwrite_output_file : boolean, optional, default False
226
- To overwrite the output_file if it already exists.
229
+ To overwrite the output file ("datalink_output.zip") if it already exists.
227
230
verbose : bool, optional, default 'False'
228
231
flag to display information about the process
229
232
230
233
Returns
231
234
-------
232
235
A dictionary where the keys are the file names and its value is a list of astropy.table.table.Table objects
233
236
"""
234
- now = datetime .now (timezone .utc )
235
- now_formatted = now .strftime ("%Y%m%d_%H%M%S" )
236
- temp_dirname = "temp_" + now_formatted
237
- downloadname_formated = "download_" + now_formatted
238
237
239
238
output_file_specified = False
240
- if output_file is None :
239
+
240
+ now = datetime .datetime .now (datetime .timezone .utc )
241
+ if not dump_to_file :
242
+ now_formatted = now .strftime ("%Y%m%d_%H%M%S" )
243
+ temp_dirname = "temp_" + now_formatted
244
+ downloadname_formated = "download_" + now_formatted
241
245
output_file = os .path .join (os .getcwd (), temp_dirname , downloadname_formated )
246
+
242
247
else :
248
+ output_file = 'datalink_output_' + now .strftime ("%Y%m%dT%H%M%S" ) + '.zip'
243
249
output_file_specified = True
244
-
245
- if isinstance (output_file , str ):
246
- if not output_file .lower ().endswith ('.zip' ):
247
- output_file = output_file + '.zip'
248
- elif isinstance (output_file , Path ):
249
- if not output_file .suffix .endswith ('.zip' ):
250
- output_file .with_suffix ('.zip' )
251
-
252
250
output_file = os .path .abspath (output_file )
251
+ log .info (f"DataLink products will be stored in the { output_file } file" )
252
+
253
253
if not overwrite_output_file and os .path .exists (output_file ):
254
254
raise ValueError (f"{ output_file } file already exists. Please use overwrite_output_file='True' to "
255
255
f"overwrite output file." )
256
256
257
257
path = os .path .dirname (output_file )
258
258
259
+ log .debug (f"Directory where the data will be saved: { path } " )
260
+
261
+ if path != '' :
262
+ if not os .path .isdir (path ):
263
+ try :
264
+ os .mkdir (path )
265
+ except FileExistsError :
266
+ log .warn ("Path %s already exist" % path )
267
+ except OSError :
268
+ log .error ("Creation of the directory %s failed" % path )
269
+
259
270
if avoid_datatype_check is False :
260
271
# we need to check params
261
272
rt = str (retrieval_type ).upper ()
@@ -298,14 +309,7 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
298
309
if linking_parameter != 'SOURCE_ID' :
299
310
params_dict ['LINKING_PARAMETER' ] = linking_parameter
300
311
301
- if path != '' :
302
- try :
303
- os .mkdir (path )
304
- except FileExistsError :
305
- log .error ("Path %s already exist" % path )
306
- except OSError :
307
- log .error ("Creation of the directory %s failed" % path )
308
-
312
+ files = dict ()
309
313
try :
310
314
self .__gaiadata .load_data (params_dict = params_dict , output_file = output_file , verbose = verbose )
311
315
files = Gaia .__get_data_files (output_file = output_file , path = path )
@@ -314,6 +318,9 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
314
318
finally :
315
319
if not output_file_specified :
316
320
shutil .rmtree (path )
321
+ else :
322
+ for file in files .keys ():
323
+ os .remove (os .path .join (os .getcwd (), path , file ))
317
324
318
325
if verbose :
319
326
if output_file_specified :
@@ -329,18 +336,21 @@ def load_data(self, ids, *, data_release=None, data_structure='INDIVIDUAL', retr
329
336
@staticmethod
330
337
def __get_data_files (output_file , path ):
331
338
files = {}
332
- if zipfile .is_zipfile (output_file ):
333
- with zipfile .ZipFile (output_file , 'r' ) as zip_ref :
334
- zip_ref .extractall (os .path .dirname (output_file ))
339
+ extracted_files = []
340
+
341
+ with zipfile .ZipFile (output_file , "r" ) as zip_ref :
342
+ extracted_files .extend (zip_ref .namelist ())
343
+ zip_ref .extractall (os .path .dirname (output_file ))
335
344
336
345
# r=root, d=directories, f = files
337
346
for r , d , f in os .walk (path ):
338
347
for file in f :
339
- if file . lower (). endswith (( '.fits' , '.xml' , '.csv' , '.ecsv' )) :
348
+ if file in extracted_files :
340
349
files [file ] = os .path .join (r , file )
341
350
342
351
for key , value in files .items ():
343
- if '.fits' in key :
352
+
353
+ if key .endswith ('.fits' ):
344
354
tables = []
345
355
with fits .open (value ) as hduList :
346
356
num_hdus = len (hduList )
@@ -349,19 +359,20 @@ def __get_data_files(output_file, path):
349
359
Gaia .correct_table_units (table )
350
360
tables .append (table )
351
361
files [key ] = tables
352
- elif '.xml' in key :
362
+
363
+ elif key .endswith ('.xml' ):
353
364
tables = []
354
365
for table in votable .parse (value ).iter_tables ():
355
366
tables .append (table )
356
367
files [key ] = tables
357
368
358
- elif '.csv' in key :
369
+ elif key . endswith ( '.csv' ) :
359
370
tables = []
360
371
table = Table .read (value , format = 'ascii.csv' , fast_reader = False )
361
372
tables .append (table )
362
373
files [key ] = tables
363
374
364
- elif '.json' in key :
375
+ elif key . endswith ( '.json' ) :
365
376
tables = []
366
377
with open (value ) as f :
367
378
data = json .load (f )
0 commit comments