Skip to content

Commit

Permalink
Minor cleanup of python2, and usage of async_to_sync rather than dupl…
Browse files Browse the repository at this point in the history
…icating methods. Making optional kwargs keyword only.
  • Loading branch information
bsipocz committed Jun 23, 2021
1 parent 6120d8e commit 6ab0362
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 92 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ irsa
- Adding ``cache`` kwarg to the class methods to be able to control the use
of local cache. [#2092]

- Making optional kwargs keyword only. [#2092]

nasa_exoplanet_archive
^^^^^^^^^^^^^^^^^^^^^^

Expand Down
82 changes: 11 additions & 71 deletions astroquery/irsa/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,93 +101,33 @@


import warnings
from io import BytesIO
import xml.etree.ElementTree as tree

import six
import astropy.units as u
import astropy.coordinates as coord
import astropy.io.votable as votable
from astropy.table import Table

from ..query import BaseQuery
from ..utils import commons
from . import conf
from ..exceptions import TableParseError, NoResultsWarning, InvalidQueryError

from astroquery.query import BaseQuery
from astroquery.utils import commons, async_to_sync
from astroquery.irsa import conf
from astroquery.exceptions import TableParseError, NoResultsWarning, InvalidQueryError


__all__ = ['Irsa', 'IrsaClass']


@async_to_sync
class IrsaClass(BaseQuery):
IRSA_URL = conf.server
GATOR_LIST_URL = conf.gator_list_catalogs
TIMEOUT = conf.timeout
ROW_LIMIT = conf.row_limit

def query_region(self, coordinates=None, catalog=None, spatial='Cone',
radius=10 * u.arcsec, width=None, polygon=None,
get_query_payload=False, verbose=False, selcols=None,
cache=True):
"""
This function can be used to perform either cone, box, polygon or
all-sky search in the catalogs hosted by the NASA/IPAC Infrared
Science Archive (IRSA).
Parameters
----------
coordinates : str, `astropy.coordinates` object
Gives the position of the center of the cone or box if
performing a cone or box search. The string can give coordinates
in various coordinate systems, or the name of a source that will
be resolved on the server (see `here
<https://irsa.ipac.caltech.edu/search_help.html>`_ for more
details). Required if spatial is ``'Cone'`` or ``'Box'``. Optional
if spatial is ``'Polygon'``.
catalog : str
The catalog to be used. To list the available catalogs, use
:meth:`~astroquery.irsa.IrsaClass.print_catalogs`.
spatial : str
Type of spatial query: ``'Cone'``, ``'Box'``, ``'Polygon'``, and
``'All-Sky'``. If missing then defaults to ``'Cone'``.
radius : str or `~astropy.units.Quantity` object, [optional for spatial is ``'Cone'``]
The string must be parsable by `~astropy.coordinates.Angle`. The
appropriate `~astropy.units.Quantity` object from
`astropy.units` may also be used. Defaults to 10 arcsec.
width : str, `~astropy.units.Quantity` object [Required for spatial is ``'Polygon'``.]
The string must be parsable by `~astropy.coordinates.Angle`. The
appropriate `~astropy.units.Quantity` object from `astropy.units`
may also be used.
polygon : list, [Required for spatial is ``'Polygon'``]
A list of ``(ra, dec)`` pairs (as tuples), in decimal degrees,
outlining the polygon to search in. It can also be a list of
`astropy.coordinates` object or strings that can be parsed by
`astropy.coordinates.ICRS`.
get_query_payload : bool, optional
If `True` then returns the dictionary sent as the HTTP request.
Defaults to `False`.
verbose : bool, optional.
If `True` then displays warnings when the returned VOTable does not
conform to the standard. Defaults to `False`.
selcols : str, optional
Target column list with value separated by a comma(,)
cache : bool, optional
Use local cache when set to `True`.
Returns
-------
table : `~astropy.table.Table`
A table containing the results of the query
"""
response = self.query_region_async(coordinates, catalog=catalog,
spatial=spatial, radius=radius,
width=width, polygon=polygon,
get_query_payload=get_query_payload,
selcols=selcols, cache=cache)
if get_query_payload:
return response
return self._parse_result(response, verbose=verbose)

def query_region_async(self, coordinates=None, catalog=None,
def query_region_async(self, coordinates=None, *, catalog=None,
spatial='Cone', radius=10 * u.arcsec, width=None,
polygon=None, get_query_payload=False,
selcols=None, cache=True):
Expand Down Expand Up @@ -396,7 +336,7 @@ def _parse_result(self, response, verbose=False):

# Read it in using the astropy VO table reader
try:
first_table = votable.parse(six.BytesIO(response.content),
first_table = votable.parse(BytesIO(response.content),
pedantic=False).get_first_table()
except Exception as ex:
self.response = response
Expand Down Expand Up @@ -461,7 +401,7 @@ def print_catalogs(self, cache=False):
def _parse_coordinates(coordinates):
# borrowed from commons.parse_coordinates as from_name wasn't required in
# this case
if isinstance(coordinates, six.string_types):
if isinstance(coordinates, str):
try:
c = coord.SkyCoord(coordinates, frame='icrs')
warnings.warn("Coordinate string is being interpreted as an "
Expand Down
39 changes: 18 additions & 21 deletions astroquery/irsa/tests/test_irsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import astropy.coordinates as coord
import astropy.units as u

from ...utils.testing_tools import MockResponse
from ...utils import commons
from ... import irsa
from ...irsa import conf
from astroquery.utils.testing_tools import MockResponse
from astroquery.utils import commons
from astroquery.irsa import Irsa, conf
from astroquery import irsa

DATA_FILES = {'Cone': 'Cone.xml',
'Box': 'Box.xml',
Expand All @@ -35,7 +35,7 @@ def patch_get(request):
mp = request.getfixturevalue("monkeypatch")
except AttributeError: # pytest < 3
mp = request.getfuncargvalue("monkeypatch")
mp.setattr(irsa.Irsa, '_request', get_mockreturn)
mp.setattr(Irsa, '_request', get_mockreturn)
return mp


Expand Down Expand Up @@ -78,45 +78,45 @@ def test_parse_coordinates(coordinates, expected):


def test_args_to_payload():
out = irsa.core.Irsa._args_to_payload("fp_psc")
out = Irsa._args_to_payload("fp_psc")
assert out == dict(catalog='fp_psc', outfmt=3, outrows=conf.row_limit,
selcols='')


@pytest.mark.parametrize(("coordinates"), OBJ_LIST)
def test_query_region_cone_async(coordinates, patch_get):
response = irsa.core.Irsa.query_region_async(
response = Irsa.query_region_async(
coordinates, catalog='fp_psc', spatial='Cone',
radius=2 * u.arcmin, get_query_payload=True)
assert response['radius'] == 2
assert response['radunits'] == 'arcmin'
response = irsa.core.Irsa.query_region_async(
response = Irsa.query_region_async(
coordinates, catalog='fp_psc', spatial='Cone', radius=2 * u.arcmin)
assert response is not None


@pytest.mark.parametrize(("coordinates"), OBJ_LIST)
def test_query_region_cone(coordinates, patch_get):
result = irsa.core.Irsa.query_region(
result = Irsa.query_region(
coordinates, catalog='fp_psc', spatial='Cone', radius=2 * u.arcmin)

assert isinstance(result, Table)


@pytest.mark.parametrize(("coordinates"), OBJ_LIST)
def test_query_region_box_async(coordinates, patch_get):
response = irsa.core.Irsa.query_region_async(
response = Irsa.query_region_async(
coordinates, catalog='fp_psc', spatial='Box',
width=2 * u.arcmin, get_query_payload=True)
assert response['size'] == 120
response = irsa.core.Irsa.query_region_async(
response = Irsa.query_region_async(
coordinates, catalog='fp_psc', spatial='Box', width=2 * u.arcmin)
assert response is not None


@pytest.mark.parametrize(("coordinates"), OBJ_LIST)
def test_query_region_box(coordinates, patch_get):
result = irsa.core.Irsa.query_region(
result = Irsa.query_region(
coordinates, catalog='fp_psc', spatial='Box', width=2 * u.arcmin)

assert isinstance(result, Table)
Expand All @@ -129,12 +129,9 @@ def test_query_region_box(coordinates, patch_get):
(10.0 * u.deg, 10.0 * u.deg)]


@pytest.mark.parametrize(("polygon"),
[poly1,
poly2
])
@pytest.mark.parametrize(("polygon"), [poly1, poly2])
def test_query_region_async_polygon(polygon, patch_get):
response = irsa.core.Irsa.query_region_async(
response = Irsa.query_region_async(
"m31", catalog="fp_psc", spatial="Polygon",
polygon=polygon, get_query_payload=True)

Expand All @@ -145,7 +142,7 @@ def test_query_region_async_polygon(polygon, patch_get):
b1 = float(b1)
np.testing.assert_almost_equal(a1, b1)

response = irsa.core.Irsa.query_region_async(
response = Irsa.query_region_async(
"m31", catalog="fp_psc", spatial="Polygon", polygon=polygon)

assert response is not None
Expand All @@ -156,7 +153,7 @@ def test_query_region_async_polygon(polygon, patch_get):
poly2,
])
def test_query_region_polygon(polygon, patch_get):
result = irsa.core.Irsa.query_region(
result = Irsa.query_region(
"m31", catalog="fp_psc", spatial="Polygon", polygon=polygon)

assert isinstance(result, Table)
Expand All @@ -166,7 +163,7 @@ def test_query_region_polygon(polygon, patch_get):
zip(('Cone', 'Box', 'Polygon', 'All-Sky'),
('Cone', 'Box', 'Polygon', 'NONE')))
def test_spatial_valdi(spatial, result):
out = irsa.core.Irsa._parse_spatial(
out = Irsa._parse_spatial(
spatial, coordinates='m31', radius=5 * u.deg, width=5 * u.deg,
polygon=[(5 * u.hour, 5 * u.deg)] * 3)
assert out['spatial'] == result
Expand All @@ -176,4 +173,4 @@ def test_spatial_valdi(spatial, result):
'All-sky', 'invalid', 'blah')])
def test_spatial_invalid(spatial):
with pytest.raises(ValueError):
irsa.core.Irsa._parse_spatial(spatial, coordinates='m31')
Irsa._parse_spatial(spatial, coordinates='m31')

0 comments on commit 6ab0362

Please sign in to comment.