Skip to content

Commit b6deaac

Browse files
committed
Move logic into commons
1 parent 8e5000a commit b6deaac

File tree

7 files changed

+51
-13
lines changed

7 files changed

+51
-13
lines changed

CHANGES.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ gaia
3636
- New method cross_match_basic that simplifies the positional x-match method [#3320]
3737
- new DR4 datalink retrieve type MEAN_SPECTRUM_RVS [#3342]
3838

39-
linelists.cdms
40-
^^^^^^^^^^^^^^
39+
mast
40+
^^^^
4141

4242
- Add a keyword to control writing of new species cache files. This is needed to prevent tests from overwriting those files. [#3297]
4343

44-
heasarc
45-
^^^^^^^
44+
simbad
45+
^^^^^^
4646

4747
- Add support for astropy.table.Row in Heasarc.download_data and Heasarc.locate_data. [#3270]
4848
- Heasarc.locate_data returns empty rows with an error in the error_message column if there are
@@ -2263,4 +2263,4 @@ Infrastructure, Utility and Other Changes and Additions
22632263
0.1 (2013-09-19)
22642264
================
22652265

2266-
- Initial release. Includes features!
2266+
- Initial release. Includes features!

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 async_to_sync
22+
from ..utils import commons, 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 = utils.parse_coordinates(coordinates)
207+
coordinates = commons.parse_coordinates(coordinates, 'icrs')
208208

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

astroquery/mast/missions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from requests import HTTPError, RequestException
2020

2121
from astroquery import log
22-
from astroquery.utils import async_to_sync
22+
from astroquery.utils import commons, async_to_sync
2323
from astroquery.utils.class_or_instance import class_or_instance
2424
from astropy.utils.console import ProgressBarOrSpinner
2525
from astroquery.exceptions import InvalidQueryError, MaxResultsWarning, InputWarning, NoResultsWarning
@@ -219,7 +219,7 @@ def query_region_async(self, coordinates, *, radius=3*u.arcmin, limit=5000, offs
219219
self._validate_criteria(**criteria)
220220

221221
# Put coordinates and radius into consistent format
222-
coordinates = utils.parse_coordinates(coordinates)
222+
coordinates = commons.parse_coordinates(coordinates, 'icrs')
223223

224224
# If radius is just a number, assume arcminutes
225225
radius = coord.Angle(radius, u.arcmin)

astroquery/mast/observations.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from astropy.table import Table, Row, vstack
2323
from astroquery import log
2424
from astroquery.mast.cloud import CloudAccess
25+
from astroquery.utils import commons
2526

2627
from ..utils import async_to_sync
2728
from ..utils.class_or_instance import class_or_instance
@@ -234,7 +235,7 @@ def query_region_async(self, coordinates, *, radius=0.2*u.deg, pagesize=None, pa
234235
"""
235236

236237
# Put coordinates and radius into consistent format
237-
coordinates = utils.parse_coordinates(coordinates)
238+
coordinates = commons.parse_coordinates(coordinates, 'icrs')
238239

239240
# if radius is just a number we assume degrees
240241
radius = coord.Angle(radius, u.deg)
@@ -363,7 +364,7 @@ def query_region_count(self, coordinates, *, radius=0.2*u.deg, pagesize=None, pa
363364
"""
364365

365366
# build the coordinates string needed by ObservationsClass._caom_filtered_position
366-
coordinates = utils.parse_coordinates(coordinates)
367+
coordinates = commons.parse_coordinates(coordinates, 'icrs')
367368

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

astroquery/mast/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ def parse_input_location(*, coordinates=None, objectname=None, resolver=None):
235235

236236
# Parse coordinates, if given
237237
if coordinates:
238-
obj_coord = commons.parse_coordinates(coordinates)
238+
obj_coord = commons.parse_coordinates(coordinates, 'icrs')
239239

240240
return obj_coord
241241

astroquery/utils/commons.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
ASTROPY_LT_7_1_1 = not minversion('astropy', '7.1.1')
4141

4242

43-
def parse_coordinates(coordinates):
43+
def parse_coordinates(coordinates, return_frame=None):
4444
"""
4545
Takes a string or astropy.coordinates object. Checks if the
4646
string is parsable as an `astropy.coordinates`
@@ -51,6 +51,10 @@ def parse_coordinates(coordinates):
5151
----------
5252
coordinates : str or `astropy.coordinates` object
5353
Astronomical coordinate
54+
return_frame : `astropy.coordinates` frame
55+
The frame to return the coordinates in. If None and `coordinates` is
56+
a string, the frame will be ICRS. If `coordinates` is an `astropy.coordinates` object, the
57+
frame will be the same as the input object.
5458
5559
Returns
5660
-------
@@ -93,6 +97,14 @@ def parse_coordinates(coordinates):
9397
c = SkyCoord(coordinates)
9498
else:
9599
raise TypeError("Argument cannot be parsed as a coordinate")
100+
101+
# Convert to requested frame, if given
102+
if return_frame and c.frame.name != return_frame.lower():
103+
try:
104+
c = c.transform_to(return_frame)
105+
except ValueError:
106+
warnings.warn(f"Failed to transform coordinates to requested frame: {return_frame}.", InputWarning)
107+
96108
return c
97109

98110

astroquery/utils/tests/test_utils.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import astropy.utils.data as aud
1616
from astropy.logger import log
1717

18+
from ...exceptions import InputWarning
1819
from ...utils import chunk_read, chunk_report, class_or_instance, commons
1920
from ...utils.process_asyncs import async_to_sync_docstr, async_to_sync
2021
from ...utils.docstr_chompers import remove_sections, prepend_docstr_nosections
@@ -80,6 +81,30 @@ def test_parse_coordinates_4():
8081
assert c.to_string() == coordinates
8182

8283

84+
def test_parse_coordinates_return_frame():
85+
# String input should return in icrs frame
86+
coords = commons.parse_coordinates('266.40498829 -28.93617776')
87+
assert coords.frame.name == 'icrs'
88+
89+
# SkyCoord input without return_frame should return in original frame
90+
galactic = coord.SkyCoord('0 0', unit='deg', frame='galactic')
91+
coords = commons.parse_coordinates(galactic)
92+
assert coords.frame.name == 'galactic'
93+
94+
# Parse a SkyCoord in galactic frame into icrs frame
95+
galactic = coord.SkyCoord('0 0', unit='deg', frame='galactic')
96+
icrs = galactic.transform_to('icrs') # Expected result
97+
coords = commons.parse_coordinates(galactic, return_frame='icrs')
98+
assert icrs.ra.deg == coords.ra.deg
99+
assert icrs.dec.deg == coords.dec.deg
100+
assert galactic.frame.name == 'galactic'
101+
assert coords.frame.name == 'icrs'
102+
103+
# Warn if transformation fails
104+
with pytest.warns(InputWarning, match='Failed to transform coordinates'):
105+
coords = commons.parse_coordinates(galactic, return_frame='invalid')
106+
107+
83108
col_1 = [1, 2, 3]
84109
col_2 = [0, 1, 0, 1, 0, 1]
85110
col_3 = ['v', 'w', 'x', 'y', 'z']

0 commit comments

Comments
 (0)