Skip to content

Commit d0e0a82

Browse files
committed
Avail: 'top', 'count_only', 'maxrec', 'MaxResultsWarning'
1 parent e5fa8f6 commit d0e0a82

File tree

4 files changed

+175
-102
lines changed

4 files changed

+175
-102
lines changed

astroquery/eso/core.py

Lines changed: 102 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import json
1414
import os
1515
import os.path
16+
import sys
1617
import pickle
1718
import re
1819
import shutil
@@ -35,7 +36,8 @@
3536

3637
from astroquery import log, cache_conf
3738
from . import conf
38-
from ..exceptions import RemoteServiceError, NoResultsWarning, LoginError
39+
from ..exceptions import RemoteServiceError, LoginError, \
40+
NoResultsWarning, MaxResultsWarning
3941
from ..query import QueryWithLogin
4042
from ..utils import schema
4143
from .utils import py2adql, _split_str_as_list_of_str, \
@@ -242,35 +244,45 @@ def query_tap_service(self,
242244
Example use:
243245
eso._query_tap_service("Select * from ivoa.ObsCore")
244246
"""
247+
maxrec = sys.maxsize
248+
if self.ROW_LIMIT:
249+
if self.ROW_LIMIT > 0:
250+
maxrec = self.ROW_LIMIT
251+
245252
if cache is None: # Global caching not overridden
246253
cache = cache_conf.cache_active
247254

248255
tap = pyvo.dal.TAPService(EsoClass.tap_url())
249256
table_to_return = None
250-
257+
log.debug(f"querystr = {query_str}")
251258
try:
252259
if not cache:
253260
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()
255262
else:
256263
table_to_return = self.from_cache(query_str, cache_conf.cache_timeout)
257264
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()
259266
to_cache(table_to_return, self.request_file(query_str=query_str))
260267

261268
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+
265274
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
270280

271281
if len(table_to_return) < 1:
272282
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)
274286

275287
return table_to_return
276288

@@ -285,12 +297,17 @@ def list_instruments(self, *, cache=True) -> List[str]:
285297
See :ref:`caching documentation <astroquery_cache>`.
286298
287299
"""
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
294311
return self._instruments
295312

296313
def list_collections(self, *, cache=True) -> List[str]:
@@ -303,14 +320,19 @@ def list_collections(self, *, cache=True) -> List[str]:
303320
Defaults to True. If set overrides global caching behavior.
304321
See :ref:`caching documentation <astroquery_cache>`.
305322
"""
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
314336
return self._collections
315337

316338
def print_table_help(self, table_name: str) -> None:
@@ -328,14 +350,18 @@ def print_table_help(self, table_name: str) -> None:
328350
log.info(logmsg)
329351
astropy.conf.max_lines = n_
330352

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]:
339365
"""
340366
Query instrument- or collection-specific data contained in the ESO archive.
341367
- instrument-specific data is raw
@@ -372,50 +398,71 @@ def _query_on_allowed_values(self,
372398
query = py2adql(table=table_name, columns=columns,
373399
ra=ra, dec=dec, radius=radius,
374400
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)
376405

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
378410

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]:
385420
return self._query_on_allowed_values(table_name=EsoNames.phase3_table,
386421
column_name=EsoNames.phase3_collections_column,
387422
allowed_values=collections,
388423
ra=ra, dec=dec, radius=radius,
389424
columns=columns,
425+
top=top,
426+
count_only=count_only,
390427
print_help=print_help, cache=cache,
391428
**kwargs)
392429

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]:
399439
return self._query_on_allowed_values(table_name=EsoNames.raw_table,
400440
column_name=EsoNames.raw_instruments_column,
401441
allowed_values=instruments,
402442
ra=ra, dec=dec, radius=radius,
403443
columns=columns,
444+
top=top,
445+
count_only=count_only,
404446
print_help=print_help, cache=cache,
405447
**kwargs)
406448

407449
# 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]:
414459
return self._query_on_allowed_values(table_name=EsoNames.ist_table(instrument),
415460
column_name=None,
416461
allowed_values=None,
417462
ra=ra, dec=dec, radius=radius,
418463
columns=columns,
464+
top=top,
465+
count_only=count_only,
419466
print_help=print_help, cache=cache,
420467
**kwargs)
421468

@@ -713,7 +760,7 @@ def retrieve_data(self, datasets, *, continuation=False, destination=None,
713760

714761
associated_files = []
715762
if with_calib:
716-
logmsg = (f"Retrieving associated '{with_calib}' calibration files ...")
763+
logmsg = f"Retrieving associated '{with_calib}' calibration files ..."
717764
log.info(logmsg)
718765
try:
719766
# batch calselector requests to avoid possible issues on the ESO server

0 commit comments

Comments
 (0)