13
13
import json
14
14
import os
15
15
import os .path
16
+ import sys
16
17
import pickle
17
18
import re
18
19
import shutil
35
36
36
37
from astroquery import log , cache_conf
37
38
from . import conf
38
- from ..exceptions import RemoteServiceError , NoResultsWarning , LoginError
39
+ from ..exceptions import RemoteServiceError , LoginError , \
40
+ NoResultsWarning , MaxResultsWarning
39
41
from ..query import QueryWithLogin
40
42
from ..utils import schema
41
43
from .utils import py2adql , _split_str_as_list_of_str , \
@@ -242,35 +244,45 @@ def query_tap_service(self,
242
244
Example use:
243
245
eso._query_tap_service("Select * from ivoa.ObsCore")
244
246
"""
247
+ maxrec = sys .maxsize
248
+ if self .ROW_LIMIT :
249
+ if self .ROW_LIMIT > 0 :
250
+ maxrec = self .ROW_LIMIT
251
+
245
252
if cache is None : # Global caching not overridden
246
253
cache = cache_conf .cache_active
247
254
248
255
tap = pyvo .dal .TAPService (EsoClass .tap_url ())
249
256
table_to_return = None
250
-
257
+ log . debug ( f"querystr = { query_str } " )
251
258
try :
252
259
if not cache :
253
260
with cache_conf .set_temp ("cache_active" , False ):
254
- table_to_return = tap .search (query_str ).to_table ()
261
+ table_to_return = tap .search (query_str , maxrec = maxrec ).to_table ()
255
262
else :
256
263
table_to_return = self .from_cache (query_str , cache_conf .cache_timeout )
257
264
if not table_to_return :
258
- table_to_return = tap .search (query_str ).to_table ()
265
+ table_to_return = tap .search (query_str , maxrec = maxrec ).to_table ()
259
266
to_cache (table_to_return , self .request_file (query_str = query_str ))
260
267
261
268
except pyvo .dal .exceptions .DALQueryError as e :
262
- raise pyvo .dal .exceptions .DALQueryError (f"\n \n \
263
- Error executing the following query:\n \n { query_str } \n \n \
264
- See examples here: https://archive.eso.org/tap_obs/examples\n \n " ) from e
269
+ raise pyvo .dal .exceptions .DALQueryError (
270
+ f"Error executing the following query:\n \n "
271
+ f"{ query_str } \n \n "
272
+ "See examples here: https://archive.eso.org/tap_obs/examples\n \n " ) from e
273
+
265
274
except Exception as e :
266
- raise RuntimeError (f"\n \n \
267
- Unknown exception { e } while executing the\
268
- following query: \n \n { query_str } \n \n \
269
- See examples here: https://archive.eso.org/tap_obs/examples\n \n " ) from e
275
+ raise RuntimeError (
276
+ f"Unhandled exception { type (e )} \n "
277
+ "While executing the following query:\n \n "
278
+ f"{ query_str } \n \n "
279
+ "See examples here: https://archive.eso.org/tap_obs/examples\n \n " ) from e
270
280
271
281
if len (table_to_return ) < 1 :
272
282
warnings .warn ("Query returned no results" , NoResultsWarning )
273
- return None
283
+
284
+ if len (table_to_return ) == maxrec :
285
+ warnings .warn (f"Results truncated to { maxrec } " , MaxResultsWarning )
274
286
275
287
return table_to_return
276
288
@@ -285,12 +297,17 @@ def list_instruments(self, *, cache=True) -> List[str]:
285
297
See :ref:`caching documentation <astroquery_cache>`.
286
298
287
299
"""
288
- if self ._instruments is None :
289
- self ._instruments = []
290
- query_str = ("select table_name from TAP_SCHEMA.tables "
291
- "where schema_name='ist' order by table_name" )
292
- res = self .query_tap_service (query_str , cache = cache )["table_name" ].data
293
- self ._instruments = list (map (lambda x : x .split ("." )[1 ], res ))
300
+ tmpvar = self .ROW_LIMIT
301
+ self .ROW_LIMIT = sys .maxsize
302
+ try :
303
+ if self ._instruments is None :
304
+ self ._instruments = []
305
+ query_str = ("select table_name from TAP_SCHEMA.tables "
306
+ "where schema_name='ist' order by table_name" )
307
+ res = self .query_tap_service (query_str , cache = cache )["table_name" ].data
308
+ self ._instruments = list (map (lambda x : x .split ("." )[1 ], res ))
309
+ finally :
310
+ self .ROW_LIMIT = tmpvar
294
311
return self ._instruments
295
312
296
313
def list_collections (self , * , cache = True ) -> List [str ]:
@@ -303,14 +320,19 @@ def list_collections(self, *, cache=True) -> List[str]:
303
320
Defaults to True. If set overrides global caching behavior.
304
321
See :ref:`caching documentation <astroquery_cache>`.
305
322
"""
306
- if self ._collections is None :
307
- self ._collections = []
308
- t = EsoNames .phase3_table
309
- c = EsoNames .phase3_collections_column
310
- query_str = f"select distinct { c } from { t } "
311
- res = self .query_tap_service (query_str , cache = cache )[c ].data
312
-
313
- self ._collections = list (res )
323
+ tmpvar = self .ROW_LIMIT
324
+ self .ROW_LIMIT = sys .maxsize
325
+ try :
326
+ if self ._collections is None :
327
+ self ._collections = []
328
+ t = EsoNames .phase3_table
329
+ c = EsoNames .phase3_collections_column
330
+ query_str = f"select distinct { c } from { t } "
331
+ res = self .query_tap_service (query_str , cache = cache )[c ].data
332
+
333
+ self ._collections = list (res )
334
+ finally :
335
+ self .ROW_LIMIT = tmpvar
314
336
return self ._collections
315
337
316
338
def print_table_help (self , table_name : str ) -> None :
@@ -328,14 +350,18 @@ def print_table_help(self, table_name: str) -> None:
328
350
log .info (logmsg )
329
351
astropy .conf .max_lines = n_
330
352
331
- def _query_on_allowed_values (self ,
332
- table_name : str ,
333
- column_name : str ,
334
- allowed_values : Union [List [str ], str ] = None , * ,
335
- ra : float = None , dec : float = None , radius : float = None ,
336
- columns : Union [List , str ] = None ,
337
- print_help = False , cache = True ,
338
- ** kwargs ) -> astropy .table .Table :
353
+ def _query_on_allowed_values (
354
+ self ,
355
+ table_name : str ,
356
+ column_name : str ,
357
+ allowed_values : Union [List [str ], str ] = None , * ,
358
+ ra : float = None , dec : float = None , radius : float = None ,
359
+ columns : Union [List , str ] = None ,
360
+ top : int = None ,
361
+ count_only : bool = False ,
362
+ print_help : bool = False ,
363
+ cache : bool = True ,
364
+ ** kwargs ) -> Union [astropy .table .Table , int ]:
339
365
"""
340
366
Query instrument- or collection-specific data contained in the ESO archive.
341
367
- instrument-specific data is raw
@@ -372,50 +398,71 @@ def _query_on_allowed_values(self,
372
398
query = py2adql (table = table_name , columns = columns ,
373
399
ra = ra , dec = dec , radius = radius ,
374
400
where_constraints = where_constraints ,
375
- top = self .ROW_LIMIT )
401
+ count_only = count_only ,
402
+ top = top )
403
+
404
+ table_to_return = self .query_tap_service (query_str = query , cache = cache )
376
405
377
- return self .query_tap_service (query_str = query , cache = cache )
406
+ if count_only : # this below is an int, not a table
407
+ table_to_return = list (table_to_return [0 ].values ())[0 ]
408
+
409
+ return table_to_return
378
410
379
- def query_collections (self ,
380
- collections : Union [List [str ], str ], * ,
381
- ra : float = None , dec : float = None , radius : float = None ,
382
- columns : Union [List , str ] = None ,
383
- print_help = False , cache = True ,
384
- ** kwargs ) -> astropy .table .Table :
411
+ def query_collections (
412
+ self ,
413
+ collections : Union [List [str ], str ], * ,
414
+ ra : float = None , dec : float = None , radius : float = None ,
415
+ columns : Union [List , str ] = None ,
416
+ top : int = None ,
417
+ count_only : bool = False ,
418
+ print_help = False , cache = True ,
419
+ ** kwargs ) -> Union [astropy .table .Table , int ]:
385
420
return self ._query_on_allowed_values (table_name = EsoNames .phase3_table ,
386
421
column_name = EsoNames .phase3_collections_column ,
387
422
allowed_values = collections ,
388
423
ra = ra , dec = dec , radius = radius ,
389
424
columns = columns ,
425
+ top = top ,
426
+ count_only = count_only ,
390
427
print_help = print_help , cache = cache ,
391
428
** kwargs )
392
429
393
- def query_main (self ,
394
- instruments : Union [List [str ], str ] = None , * ,
395
- ra : float = None , dec : float = None , radius : float = None ,
396
- columns : Union [List , str ] = None ,
397
- print_help = False , cache = True ,
398
- ** kwargs ) -> astropy .table .Table :
430
+ def query_main (
431
+ self ,
432
+ instruments : Union [List [str ], str ] = None , * ,
433
+ ra : float = None , dec : float = None , radius : float = None ,
434
+ columns : Union [List , str ] = None ,
435
+ top : int = None ,
436
+ count_only : bool = False ,
437
+ print_help = False , cache = True ,
438
+ ** kwargs ) -> Union [astropy .table .Table , int ]:
399
439
return self ._query_on_allowed_values (table_name = EsoNames .raw_table ,
400
440
column_name = EsoNames .raw_instruments_column ,
401
441
allowed_values = instruments ,
402
442
ra = ra , dec = dec , radius = radius ,
403
443
columns = columns ,
444
+ top = top ,
445
+ count_only = count_only ,
404
446
print_help = print_help , cache = cache ,
405
447
** kwargs )
406
448
407
449
# ex query_instrument
408
- def query_instrument (self ,
409
- instrument : str , * ,
410
- ra : float = None , dec : float = None , radius : float = None ,
411
- columns : Union [List , str ] = None ,
412
- print_help = False , cache = True ,
413
- ** kwargs ) -> astropy .table .Table :
450
+ def query_instrument (
451
+ self ,
452
+ instrument : str , * ,
453
+ ra : float = None , dec : float = None , radius : float = None ,
454
+ columns : Union [List , str ] = None ,
455
+ top : int = None ,
456
+ count_only : bool = False ,
457
+ print_help = False , cache = True ,
458
+ ** kwargs ) -> Union [astropy .table .Table , int ]:
414
459
return self ._query_on_allowed_values (table_name = EsoNames .ist_table (instrument ),
415
460
column_name = None ,
416
461
allowed_values = None ,
417
462
ra = ra , dec = dec , radius = radius ,
418
463
columns = columns ,
464
+ top = top ,
465
+ count_only = count_only ,
419
466
print_help = print_help , cache = cache ,
420
467
** kwargs )
421
468
@@ -713,7 +760,7 @@ def retrieve_data(self, datasets, *, continuation=False, destination=None,
713
760
714
761
associated_files = []
715
762
if with_calib :
716
- logmsg = ( f"Retrieving associated '{ with_calib } ' calibration files ..." )
763
+ logmsg = f"Retrieving associated '{ with_calib } ' calibration files ..."
717
764
log .info (logmsg )
718
765
try :
719
766
# batch calselector requests to avoid possible issues on the ESO server
0 commit comments