Skip to content

Commit 82c7615

Browse files
committed
Merge pull request #541 from bsipocz/sdss_add_no_result_found_message
Check whether no result is returned from the query (SDSS)
2 parents eb4ca1c + 271450c commit 82c7615

File tree

8 files changed

+187
-165
lines changed

8 files changed

+187
-165
lines changed

astroquery/besancon/core.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
import sys
77
import re
88
import os
9+
import warnings
910
from astropy.io import ascii
1011
from astropy.extern.six.moves.urllib_error import URLError
11-
from astropy.extern.six import StringIO
1212
from collections import OrderedDict
1313
from ..query import BaseQuery
1414
from ..utils import commons
@@ -428,9 +428,10 @@ def parse_besancon_model_string(bms,):
428428

429429
for cn in besancon_table.columns:
430430
if besancon_table[cn].dtype.kind in ('s', 'S'):
431-
print("WARNING: The Besancon table did not parse properly. "
432-
"Some columns are likely to have invalid values and others incorrect values. "
433-
"Please report this error.")
431+
warnings.warn("The Besancon table did not parse properly. "
432+
"Some columns are likely to have invalid "
433+
"values and others incorrect values. "
434+
"Please report this error.")
434435
break
435436

436437
return besancon_table

astroquery/cosmosim/core.py

+128-131
Large diffs are not rendered by default.

astroquery/eso/core.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from astropy.table import Table, Column
1616
from astropy import log
1717

18-
from ..exceptions import LoginError, RemoteServiceError
18+
from ..exceptions import LoginError, RemoteServiceError, NoResultsWarning
1919
from ..utils import schema, system_tools
2020
from ..query import QueryWithLogin, suspend_cache
2121
from . import conf
@@ -283,7 +283,7 @@ def query_survey(self, survey, cache=True, **kwargs):
283283
raise ex
284284
return table
285285
else:
286-
warnings.warn("Query returned no results")
286+
warnings.warn("Query returned no results", NoResultsWarning)
287287

288288
def query_instrument(self, instrument, column_filters={}, columns=[],
289289
open_form=False, help=False, cache=True, **kwargs):
@@ -366,8 +366,7 @@ def query_instrument(self, instrument, column_filters={}, columns=[],
366366
raise ex
367367
return table
368368
else:
369-
warnings.warn("Query returned no results")
370-
369+
warnings.warn("Query returned no results", NoResultsWarning)
371370

372371
def get_headers(self, product_ids, cache=True):
373372
"""

astroquery/exceptions.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
Custom exceptions used in the astroquery query classes
44
"""
55

6+
from astropy.utils.exceptions import AstropyWarning
7+
68
__all__ = ['TimeoutError', 'InvalidQueryError', 'RemoteServiceError',
7-
'TableParseError', 'LoginError']
9+
'TableParseError', 'LoginError', 'NoResultsWarning']
810

911

1012
class TimeoutError(Exception):
@@ -43,3 +45,9 @@ class LoginError(Exception):
4345
a login is a prerequisite for the requested action
4446
"""
4547
pass
48+
49+
50+
class NoResultsWarning(AstropyWarning):
51+
"""
52+
Astroquery warning class to be issued when a query returns no result.
53+
"""

astroquery/irsa/core.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
from ..query import BaseQuery
102102
from ..utils import commons
103103
from . import conf
104-
from ..exceptions import TableParseError
104+
from ..exceptions import TableParseError, NoResultsWarning
105105

106106
__all__ = ['Irsa', 'IrsaClass']
107107

@@ -365,7 +365,8 @@ def _parse_result(self, response, verbose=False):
365365

366366
# Check if table is empty
367367
if len(table) == 0:
368-
warnings.warn("Query returned no results, so the table will be empty")
368+
warnings.warn("Query returned no results, so the table will "
369+
"be empty", NoResultsWarning)
369370

370371
return table
371372

astroquery/lcogt/core.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@
5757
5858
order Optional Results ordered by this column.
5959
60-
selcols Optional Select specific columns to be returned. The default
61-
action is to return all columns in the queried catalog.
62-
To find the names of the columns in the LCOGT Archive databases,
63-
please read Photometry Table column descriptions [http://lcogtarchive.ipac.caltech.edu/docs/lco_cat_dd.html]
60+
selcols Optional Select specific columns to be returned. The default
61+
action is to return all columns in the queried catalog.
62+
To find the names of the columns in the LCOGT Archive databases,
63+
please read Photometry Table column descriptions [http://lcogtarchive.ipac.caltech.edu/docs/lco_cat_dd.html]
6464
and Image Table column descriptions [http://lcogtarchive.ipac.caltech.edu/docs/lco_img_dd.html].
6565
6666
constraint Optional User defined query constraint(s)
@@ -70,7 +70,6 @@
7070
from __future__ import print_function, division
7171

7272
import warnings
73-
import xml.etree.ElementTree as tree
7473
import logging
7574

7675
from astropy.extern import six
@@ -81,7 +80,7 @@
8180
from ..query import BaseQuery
8281
from ..utils import commons, async_to_sync
8382
from . import conf
84-
from ..exceptions import TableParseError
83+
from ..exceptions import TableParseError, NoResultsWarning
8584

8685
__all__ = ['Lcogt', 'LcogtClass']
8786

@@ -347,7 +346,8 @@ def _parse_result(self, response, verbose=False):
347346

348347
# Check if table is empty
349348
if len(table) == 0:
350-
warnings.warn("Query returned no results, so the table will be empty")
349+
warnings.warn("Query returned no results, so the table will "
350+
"be empty", NoResultsWarning)
351351

352352
return table
353353

astroquery/sdss/core.py

+29-13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from __future__ import print_function
1313
import io
14+
import warnings
1415
import numpy as np
1516
from astropy import units as u
1617
import astropy.coordinates as coord
@@ -19,7 +20,7 @@
1920
from . import conf
2021
from ..utils import commons, async_to_sync
2122
from ..utils.docstr_chompers import prepend_docstr_noreturns
22-
from ..exceptions import RemoteServiceError
23+
from ..exceptions import RemoteServiceError, NoResultsWarning
2324

2425
__all__ = ['SDSS', 'SDSSClass']
2526

@@ -513,9 +514,12 @@ def get_spectra_async(self, coordinates=None, radius=2. * u.arcsec,
513514
r = commons.send_request(SDSS.QUERY_URL, request_payload, timeout,
514515
request_type='GET')
515516
matches = self._parse_result(r)
517+
if matches is None:
518+
warnings.warn("Query returned no results.", NoResultsWarning)
519+
return
516520

517521
if not isinstance(matches, Table):
518-
raise TypeError("Matches must be an astropy Table.")
522+
raise TypeError("'matches' must be an astropy Table.")
519523

520524
results = []
521525
for row in matches:
@@ -549,7 +553,11 @@ def get_spectra(self, coordinates=None, radius=2. * u.arcsec,
549553
plate=plate, fiberID=fiberID,
550554
mjd=mjd, timeout=timeout)
551555

552-
return [obj.get_fits() for obj in readable_objs]
556+
if readable_objs is not None:
557+
if isinstance(readable_objs, dict):
558+
return readable_objs
559+
else:
560+
return [obj.get_fits() for obj in readable_objs]
553561

554562
def get_images_async(self, coordinates=None, radius=2. * u.arcsec,
555563
matches=None, run=None, rerun=301, camcol=None,
@@ -640,9 +648,11 @@ def get_images_async(self, coordinates=None, radius=2. * u.arcsec,
640648
r = commons.send_request(SDSS.QUERY_URL, request_payload, timeout,
641649
request_type='GET')
642650
matches = self._parse_result(r)
643-
651+
if matches is None:
652+
warnings.warn("Query returned no results.", NoResultsWarning)
653+
return
644654
if not isinstance(matches, Table):
645-
raise ValueError
655+
raise ValueError("'matches' must be an astropy Table")
646656

647657
results = []
648658
for row in matches:
@@ -665,7 +675,8 @@ def get_images_async(self, coordinates=None, radius=2. * u.arcsec,
665675
@prepend_docstr_noreturns(get_images_async.__doc__)
666676
def get_images(self, coordinates=None, radius=2. * u.arcsec,
667677
matches=None, run=None, rerun=301, camcol=None, field=None,
668-
band='g', timeout=TIMEOUT, cache=True):
678+
band='g', timeout=TIMEOUT, cache=True,
679+
get_query_payload=False):
669680
"""
670681
Returns
671682
-------
@@ -678,9 +689,13 @@ def get_images(self, coordinates=None, radius=2. * u.arcsec,
678689
run=run, rerun=rerun,
679690
camcol=camcol, field=field,
680691
band=band, timeout=timeout,
681-
get_query_payload=False)
692+
get_query_payload=get_query_payload)
682693

683-
return [obj.get_fits() for obj in readable_objs]
694+
if readable_objs is not None:
695+
if isinstance(readable_objs, dict):
696+
return readable_objs
697+
else:
698+
return [obj.get_fits() for obj in readable_objs]
684699

685700
def get_spectral_template_async(self, kind='qso', timeout=TIMEOUT):
686701
"""
@@ -744,7 +759,8 @@ def get_spectral_template(self, kind='qso', timeout=TIMEOUT):
744759
readable_objs = self.get_spectral_template_async(kind=kind,
745760
timeout=timeout)
746761

747-
return [obj.get_fits() for obj in readable_objs]
762+
if readable_objs is not None:
763+
return [obj.get_fits() for obj in readable_objs]
748764

749765
def _parse_result(self, response, verbose=False):
750766
"""
@@ -831,7 +847,7 @@ def _args_to_payload(self, coordinates=None, radius=2. * u.arcsec,
831847
SpecObj quantities to return. If photoobj_fields is None and
832848
specobj_fields is None then the value of fields is used
833849
field_help: str or bool, optional
834-
Field name to check whether a valid PhotoObjAll or SpecObjAll
850+
Field name to check whether it is a valid PhotoObjAll or SpecObjAll
835851
field name. If `True` or it is an invalid field name all the valid
836852
field names are returned as a dict.
837853
obj_names : str, or list or `~astropy.table.Column`, optional
@@ -857,9 +873,9 @@ def _args_to_payload(self, coordinates=None, radius=2. * u.arcsec,
857873
return
858874
else:
859875
if field_help is not True:
860-
print("{0} isn't a valid 'photobj_field' or "
861-
"'specobj_field' field, valid fields are "
862-
"returned.".format(field_help))
876+
warnings.warn("{0} isn't a valid 'photobj_field' or "
877+
"'specobj_field' field, valid fields are"
878+
"returned.".format(field_help))
863879
return {'photoobj_all': photoobj_all,
864880
'specobj_all': specobj_all}
865881

astroquery/ukidss/core.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import astropy.io.votable as votable
1515

1616
from ..query import QueryWithLogin
17-
from ..exceptions import InvalidQueryError, TimeoutError
17+
from ..exceptions import InvalidQueryError, TimeoutError, NoResultsWarning
1818
from ..utils import commons
1919
from . import conf
2020
from ..exceptions import TableParseError
@@ -549,7 +549,8 @@ def _parse_result(self, response, verbose=False):
549549
first_table = parsed_table.get_first_table()
550550
table = first_table.to_table()
551551
if len(table) == 0:
552-
warnings.warn("Query returned no results, so the table will be empty")
552+
warnings.warn("Query returned no results, so the table will "
553+
"be empty", NoResultsWarning)
553554
return table
554555
except Exception as ex:
555556
self.response = content
@@ -559,7 +560,6 @@ def _parse_result(self, response, verbose=False):
559560
"in self.response, and the error in self.table_parse_error. "
560561
"Exception: " + str(self.table_parse_error))
561562

562-
563563
def list_catalogs(self, style='short'):
564564
"""
565565
Returns a list of available catalogs in UKIDSS.

0 commit comments

Comments
 (0)