Skip to content

Commit c0f5626

Browse files
committed
Utils method to parse coordinates
1 parent dca9873 commit c0f5626

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

astroquery/mast/collections.py

+2-2
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def query_region_async(self, coordinates, *, radius=3*u.arcmin, limit=5000, offs
183183
self._validate_criteria(**criteria)
184184

185185
# Put coordinates and radius into consistent format
186-
coordinates = commons.parse_coordinates(coordinates)
186+
coordinates = utils.parse_coordinates(coordinates)
187187

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

astroquery/mast/observations.py

+3-3
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

+30
Original file line numberDiff line numberDiff line change
@@ -1063,3 +1063,33 @@ def test_zcut_get_cutouts(patch_post, tmpdir):
10631063
assert isinstance(cutout_list, list)
10641064
assert len(cutout_list) == 1
10651065
assert isinstance(cutout_list[0], fits.HDUList)
1066+
1067+
1068+
################
1069+
# Utils tests #
1070+
################
1071+
1072+
1073+
def test_utils_parse_coordinates(patch_post):
1074+
1075+
def compare_coords(coords1, coords2):
1076+
assert coords1.ra.deg == coords2.ra.deg
1077+
assert coords1.dec.deg == coords2.dec.deg
1078+
assert coords1.frame.name == 'icrs'
1079+
assert coords2.frame.name == 'icrs'
1080+
1081+
# Expected result
1082+
expected = SkyCoord('266.40498829 -28.93617776', unit='deg')
1083+
1084+
# Parse a string
1085+
coords = mast.utils.parse_coordinates('266.40498829 -28.93617776')
1086+
compare_coords(coords, expected)
1087+
1088+
# Parse a SkyCoord in ICRS frame
1089+
coords = mast.utils.parse_coordinates(expected)
1090+
compare_coords(coords, expected)
1091+
1092+
# Parse a SkyCoord in galactic frame
1093+
galactic = SkyCoord('0 0', unit='deg', frame='galactic')
1094+
coords = mast.utils.parse_coordinates(galactic)
1095+
compare_coords(coords, galactic.transform_to('icrs'))

astroquery/mast/utils.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,32 @@ def resolve_object(objectname):
121121
return coordinates
122122

123123

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

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

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

183+
# Parse coordinates, if given
155184
if coordinates:
156-
obj_coord = commons.parse_coordinates(coordinates)
185+
obj_coord = parse_coordinates(coordinates)
157186

158187
return obj_coord
159188

0 commit comments

Comments
 (0)