Skip to content

Commit 6ab0362

Browse files
committed
Minor cleanup of python2, and usage of async_to_sync rather than duplicating methods. Making optional kwargs keyword only.
1 parent 6120d8e commit 6ab0362

File tree

3 files changed

+31
-92
lines changed

3 files changed

+31
-92
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ irsa
4242
- Adding ``cache`` kwarg to the class methods to be able to control the use
4343
of local cache. [#2092]
4444

45+
- Making optional kwargs keyword only. [#2092]
46+
4547
nasa_exoplanet_archive
4648
^^^^^^^^^^^^^^^^^^^^^^
4749

astroquery/irsa/core.py

Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -101,93 +101,33 @@
101101

102102

103103
import warnings
104+
from io import BytesIO
104105
import xml.etree.ElementTree as tree
105106

106-
import six
107107
import astropy.units as u
108108
import astropy.coordinates as coord
109109
import astropy.io.votable as votable
110+
from astropy.table import Table
110111

111-
from ..query import BaseQuery
112-
from ..utils import commons
113-
from . import conf
114-
from ..exceptions import TableParseError, NoResultsWarning, InvalidQueryError
112+
113+
from astroquery.query import BaseQuery
114+
from astroquery.utils import commons, async_to_sync
115+
from astroquery.irsa import conf
116+
from astroquery.exceptions import TableParseError, NoResultsWarning, InvalidQueryError
115117

116118

117119
__all__ = ['Irsa', 'IrsaClass']
118120

119121

122+
@async_to_sync
120123
class IrsaClass(BaseQuery):
121124
IRSA_URL = conf.server
122125
GATOR_LIST_URL = conf.gator_list_catalogs
123126
TIMEOUT = conf.timeout
124127
ROW_LIMIT = conf.row_limit
125128

126-
def query_region(self, coordinates=None, catalog=None, spatial='Cone',
127-
radius=10 * u.arcsec, width=None, polygon=None,
128-
get_query_payload=False, verbose=False, selcols=None,
129-
cache=True):
130-
"""
131-
This function can be used to perform either cone, box, polygon or
132-
all-sky search in the catalogs hosted by the NASA/IPAC Infrared
133-
Science Archive (IRSA).
134-
135-
Parameters
136-
----------
137-
coordinates : str, `astropy.coordinates` object
138-
Gives the position of the center of the cone or box if
139-
performing a cone or box search. The string can give coordinates
140-
in various coordinate systems, or the name of a source that will
141-
be resolved on the server (see `here
142-
<https://irsa.ipac.caltech.edu/search_help.html>`_ for more
143-
details). Required if spatial is ``'Cone'`` or ``'Box'``. Optional
144-
if spatial is ``'Polygon'``.
145-
catalog : str
146-
The catalog to be used. To list the available catalogs, use
147-
:meth:`~astroquery.irsa.IrsaClass.print_catalogs`.
148-
spatial : str
149-
Type of spatial query: ``'Cone'``, ``'Box'``, ``'Polygon'``, and
150-
``'All-Sky'``. If missing then defaults to ``'Cone'``.
151-
radius : str or `~astropy.units.Quantity` object, [optional for spatial is ``'Cone'``]
152-
The string must be parsable by `~astropy.coordinates.Angle`. The
153-
appropriate `~astropy.units.Quantity` object from
154-
`astropy.units` may also be used. Defaults to 10 arcsec.
155-
width : str, `~astropy.units.Quantity` object [Required for spatial is ``'Polygon'``.]
156-
157-
The string must be parsable by `~astropy.coordinates.Angle`. The
158-
appropriate `~astropy.units.Quantity` object from `astropy.units`
159-
may also be used.
160-
polygon : list, [Required for spatial is ``'Polygon'``]
161-
A list of ``(ra, dec)`` pairs (as tuples), in decimal degrees,
162-
outlining the polygon to search in. It can also be a list of
163-
`astropy.coordinates` object or strings that can be parsed by
164-
`astropy.coordinates.ICRS`.
165-
get_query_payload : bool, optional
166-
If `True` then returns the dictionary sent as the HTTP request.
167-
Defaults to `False`.
168-
verbose : bool, optional.
169-
If `True` then displays warnings when the returned VOTable does not
170-
conform to the standard. Defaults to `False`.
171-
selcols : str, optional
172-
Target column list with value separated by a comma(,)
173-
cache : bool, optional
174-
Use local cache when set to `True`.
175-
176-
Returns
177-
-------
178-
table : `~astropy.table.Table`
179-
A table containing the results of the query
180-
"""
181-
response = self.query_region_async(coordinates, catalog=catalog,
182-
spatial=spatial, radius=radius,
183-
width=width, polygon=polygon,
184-
get_query_payload=get_query_payload,
185-
selcols=selcols, cache=cache)
186-
if get_query_payload:
187-
return response
188-
return self._parse_result(response, verbose=verbose)
189129

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

397337
# Read it in using the astropy VO table reader
398338
try:
399-
first_table = votable.parse(six.BytesIO(response.content),
339+
first_table = votable.parse(BytesIO(response.content),
400340
pedantic=False).get_first_table()
401341
except Exception as ex:
402342
self.response = response
@@ -461,7 +401,7 @@ def print_catalogs(self, cache=False):
461401
def _parse_coordinates(coordinates):
462402
# borrowed from commons.parse_coordinates as from_name wasn't required in
463403
# this case
464-
if isinstance(coordinates, six.string_types):
404+
if isinstance(coordinates, str):
465405
try:
466406
c = coord.SkyCoord(coordinates, frame='icrs')
467407
warnings.warn("Coordinate string is being interpreted as an "

astroquery/irsa/tests/test_irsa.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
import astropy.coordinates as coord
1111
import astropy.units as u
1212

13-
from ...utils.testing_tools import MockResponse
14-
from ...utils import commons
15-
from ... import irsa
16-
from ...irsa import conf
13+
from astroquery.utils.testing_tools import MockResponse
14+
from astroquery.utils import commons
15+
from astroquery.irsa import Irsa, conf
16+
from astroquery import irsa
1717

1818
DATA_FILES = {'Cone': 'Cone.xml',
1919
'Box': 'Box.xml',
@@ -35,7 +35,7 @@ def patch_get(request):
3535
mp = request.getfixturevalue("monkeypatch")
3636
except AttributeError: # pytest < 3
3737
mp = request.getfuncargvalue("monkeypatch")
38-
mp.setattr(irsa.Irsa, '_request', get_mockreturn)
38+
mp.setattr(Irsa, '_request', get_mockreturn)
3939
return mp
4040

4141

@@ -78,45 +78,45 @@ def test_parse_coordinates(coordinates, expected):
7878

7979

8080
def test_args_to_payload():
81-
out = irsa.core.Irsa._args_to_payload("fp_psc")
81+
out = Irsa._args_to_payload("fp_psc")
8282
assert out == dict(catalog='fp_psc', outfmt=3, outrows=conf.row_limit,
8383
selcols='')
8484

8585

8686
@pytest.mark.parametrize(("coordinates"), OBJ_LIST)
8787
def test_query_region_cone_async(coordinates, patch_get):
88-
response = irsa.core.Irsa.query_region_async(
88+
response = Irsa.query_region_async(
8989
coordinates, catalog='fp_psc', spatial='Cone',
9090
radius=2 * u.arcmin, get_query_payload=True)
9191
assert response['radius'] == 2
9292
assert response['radunits'] == 'arcmin'
93-
response = irsa.core.Irsa.query_region_async(
93+
response = Irsa.query_region_async(
9494
coordinates, catalog='fp_psc', spatial='Cone', radius=2 * u.arcmin)
9595
assert response is not None
9696

9797

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

103103
assert isinstance(result, Table)
104104

105105

106106
@pytest.mark.parametrize(("coordinates"), OBJ_LIST)
107107
def test_query_region_box_async(coordinates, patch_get):
108-
response = irsa.core.Irsa.query_region_async(
108+
response = Irsa.query_region_async(
109109
coordinates, catalog='fp_psc', spatial='Box',
110110
width=2 * u.arcmin, get_query_payload=True)
111111
assert response['size'] == 120
112-
response = irsa.core.Irsa.query_region_async(
112+
response = Irsa.query_region_async(
113113
coordinates, catalog='fp_psc', spatial='Box', width=2 * u.arcmin)
114114
assert response is not None
115115

116116

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

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

131131

132-
@pytest.mark.parametrize(("polygon"),
133-
[poly1,
134-
poly2
135-
])
132+
@pytest.mark.parametrize(("polygon"), [poly1, poly2])
136133
def test_query_region_async_polygon(polygon, patch_get):
137-
response = irsa.core.Irsa.query_region_async(
134+
response = Irsa.query_region_async(
138135
"m31", catalog="fp_psc", spatial="Polygon",
139136
polygon=polygon, get_query_payload=True)
140137

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

148-
response = irsa.core.Irsa.query_region_async(
145+
response = Irsa.query_region_async(
149146
"m31", catalog="fp_psc", spatial="Polygon", polygon=polygon)
150147

151148
assert response is not None
@@ -156,7 +153,7 @@ def test_query_region_async_polygon(polygon, patch_get):
156153
poly2,
157154
])
158155
def test_query_region_polygon(polygon, patch_get):
159-
result = irsa.core.Irsa.query_region(
156+
result = Irsa.query_region(
160157
"m31", catalog="fp_psc", spatial="Polygon", polygon=polygon)
161158

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

0 commit comments

Comments
 (0)