Skip to content

Commit 1eb2975

Browse files
committed
Utils method to parse coordinates
1 parent 78aee6f commit 1eb2975

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

astroquery/mast/collections.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from astropy.table import Table, Row
2121

22-
from ..utils import commons, async_to_sync
22+
from ..utils import async_to_sync
2323
from ..utils.class_or_instance import class_or_instance
2424
from ..exceptions import InvalidQueryError, MaxResultsWarning, InputWarning
2525

@@ -204,7 +204,7 @@ def query_region_async(self, coordinates, *, radius=0.2*u.deg, catalog="Hsc",
204204
"""
205205

206206
# Put coordinates and radius into consistent format
207-
coordinates = commons.parse_coordinates(coordinates)
207+
coordinates = utils.parse_coordinates(coordinates)
208208

209209
# if radius is just a number we assume degrees
210210
radius = coord.Angle(radius, u.deg)

astroquery/mast/missions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def query_region_async(self, coordinates, *, radius=3*u.arcmin, limit=5000, offs
184184
self._validate_criteria(**criteria)
185185

186186
# Put coordinates and radius into consistent format
187-
coordinates = commons.parse_coordinates(coordinates)
187+
coordinates = utils.parse_coordinates(coordinates)
188188

189189
# if radius is just a number we assume degrees
190190
radius = coord.Angle(radius, u.arcmin)

astroquery/mast/observations.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from astroquery import log
2424
from astroquery.mast.cloud import CloudAccess
2525

26-
from ..utils import commons, async_to_sync
26+
from ..utils import async_to_sync
2727
from ..utils.class_or_instance import class_or_instance
2828
from ..exceptions import (InvalidQueryError, RemoteServiceError,
2929
NoResultsWarning, InputWarning)
@@ -227,7 +227,7 @@ def query_region_async(self, coordinates, *, radius=0.2*u.deg, pagesize=None, pa
227227
"""
228228

229229
# Put coordinates and radius into consistent format
230-
coordinates = commons.parse_coordinates(coordinates)
230+
coordinates = utils.parse_coordinates(coordinates)
231231

232232
# if radius is just a number we assume degrees
233233
radius = coord.Angle(radius, u.deg)
@@ -346,7 +346,7 @@ def query_region_count(self, coordinates, *, radius=0.2*u.deg, pagesize=None, pa
346346
"""
347347

348348
# build the coordinates string needed by ObservationsClass._caom_filtered_position
349-
coordinates = commons.parse_coordinates(coordinates)
349+
coordinates = utils.parse_coordinates(coordinates)
350350

351351
# if radius is just a number we assume degrees
352352
radius = coord.Angle(radius, u.deg)

astroquery/mast/tests/test_mast.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1157,3 +1157,33 @@ def test_zcut_get_cutouts(patch_post, tmpdir):
11571157
assert isinstance(cutout_list, list)
11581158
assert len(cutout_list) == 1
11591159
assert isinstance(cutout_list[0], fits.HDUList)
1160+
1161+
1162+
################
1163+
# Utils tests #
1164+
################
1165+
1166+
1167+
def test_utils_parse_coordinates(patch_post):
1168+
1169+
def compare_coords(coords1, coords2):
1170+
assert coords1.ra.deg == coords2.ra.deg
1171+
assert coords1.dec.deg == coords2.dec.deg
1172+
assert coords1.frame.name == 'icrs'
1173+
assert coords2.frame.name == 'icrs'
1174+
1175+
# Expected result
1176+
expected = SkyCoord('266.40498829 -28.93617776', unit='deg')
1177+
1178+
# Parse a string
1179+
coords = mast.utils.parse_coordinates('266.40498829 -28.93617776')
1180+
compare_coords(coords, expected)
1181+
1182+
# Parse a SkyCoord in ICRS frame
1183+
coords = mast.utils.parse_coordinates(expected)
1184+
compare_coords(coords, expected)
1185+
1186+
# Parse a SkyCoord in galactic frame
1187+
galactic = SkyCoord('0 0', unit='deg', frame='galactic')
1188+
coords = mast.utils.parse_coordinates(galactic)
1189+
compare_coords(coords, galactic.transform_to('icrs'))

astroquery/mast/utils.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,32 @@ def resolve_object(objectname):
122122
return coordinates
123123

124124

125+
def parse_coordinates(coordinates):
126+
"""
127+
Convenience function to parse user input of coordinates.
128+
129+
Parameters
130+
----------
131+
coordinates : str or `astropy.coordinates` object, optional
132+
The target around which to search. It may be specified as a
133+
string or as the appropriate `astropy.coordinates` object.
134+
135+
Returns
136+
-------
137+
response : `~astropy.coordinates.SkyCoord`
138+
The given coordinates as an `~astropy.coordinates.SkyCoord` object in the ICRS frame.
139+
"""
140+
141+
# Parse into SkyCoord object
142+
coordinates = commons.parse_coordinates(coordinates)
143+
144+
# Convert to ICRS frame, if needed
145+
if coordinates.frame != 'icrs':
146+
coordinates = coordinates.transform_to('icrs')
147+
148+
return coordinates
149+
150+
125151
def parse_input_location(coordinates=None, objectname=None):
126152
"""
127153
Convenience function to parse user input of coordinates and objectname.
@@ -140,7 +166,8 @@ def parse_input_location(coordinates=None, objectname=None):
140166
Returns
141167
-------
142168
response : `~astropy.coordinates.SkyCoord`
143-
The given coordinates, or object's location as an `~astropy.coordinates.SkyCoord` object.
169+
The given coordinates, or object's location as an `~astropy.coordinates.SkyCoord` object
170+
in the ICRS frame.
144171
"""
145172

146173
# Checking for valid input
@@ -150,11 +177,13 @@ def parse_input_location(coordinates=None, objectname=None):
150177
if not (objectname or coordinates):
151178
raise InvalidQueryError("One of objectname and coordinates must be specified.")
152179

180+
# Resolve object, if given
153181
if objectname:
154182
obj_coord = resolve_object(objectname)
155183

184+
# Parse coordinates, if given
156185
if coordinates:
157-
obj_coord = commons.parse_coordinates(coordinates)
186+
obj_coord = parse_coordinates(coordinates)
158187

159188
return obj_coord
160189

0 commit comments

Comments
 (0)