Skip to content

Commit

Permalink
Utils method to parse coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
snbianco committed Dec 23, 2024
1 parent 45b93c0 commit 31ffc55
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 9 deletions.
4 changes: 2 additions & 2 deletions astroquery/mast/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from astropy.table import Table, Row

from ..utils import commons, async_to_sync
from ..utils import async_to_sync
from ..utils.class_or_instance import class_or_instance
from ..exceptions import InvalidQueryError, MaxResultsWarning, InputWarning

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

# Put coordinates and radius into consistent format
coordinates = commons.parse_coordinates(coordinates)
coordinates = utils.parse_coordinates(coordinates)

# if radius is just a number we assume degrees
radius = coord.Angle(radius, u.deg)
Expand Down
4 changes: 2 additions & 2 deletions astroquery/mast/missions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import astropy.coordinates as coord
from requests import RequestException

from astroquery.utils import commons, async_to_sync
from astroquery.utils import async_to_sync
from astroquery.utils.class_or_instance import class_or_instance
from astroquery.exceptions import InvalidQueryError, MaxResultsWarning

Expand Down Expand Up @@ -142,7 +142,7 @@ def query_region_async(self, coordinates, *, radius=3*u.arcmin, limit=5000, offs
self._validate_criteria(**criteria)

# Put coordinates and radius into consistent format
coordinates = commons.parse_coordinates(coordinates)
coordinates = utils.parse_coordinates(coordinates)

# if radius is just a number we assume degrees
radius = coord.Angle(radius, u.arcmin)
Expand Down
6 changes: 3 additions & 3 deletions astroquery/mast/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from astroquery import log
from astroquery.mast.cloud import CloudAccess

from ..utils import commons, async_to_sync
from ..utils import async_to_sync
from ..utils.class_or_instance import class_or_instance
from ..exceptions import (InvalidQueryError, RemoteServiceError,
NoResultsWarning, InputWarning)
Expand Down Expand Up @@ -227,7 +227,7 @@ def query_region_async(self, coordinates, *, radius=0.2*u.deg, pagesize=None, pa
"""

# Put coordinates and radius into consistent format
coordinates = commons.parse_coordinates(coordinates)
coordinates = utils.parse_coordinates(coordinates)

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

# build the coordinates string needed by ObservationsClass._caom_filtered_position
coordinates = commons.parse_coordinates(coordinates)
coordinates = utils.parse_coordinates(coordinates)

# if radius is just a number we assume degrees
radius = coord.Angle(radius, u.deg)
Expand Down
30 changes: 30 additions & 0 deletions astroquery/mast/tests/test_mast.py
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,33 @@ def test_zcut_get_cutouts(patch_post, tmpdir):
assert isinstance(cutout_list, list)
assert len(cutout_list) == 1
assert isinstance(cutout_list[0], fits.HDUList)


################
# Utils tests #
################


def test_utils_parse_coordinates(patch_post):

def compare_coords(coords1, coords2):
assert coords1.ra.deg == coords2.ra.deg
assert coords1.dec.deg == coords2.dec.deg
assert coords1.frame.name == 'icrs'
assert coords2.frame.name == 'icrs'

# Expected result
expected = SkyCoord('266.40498829 -28.93617776', unit='deg')

# Parse a string
coords = mast.utils.parse_coordinates('266.40498829 -28.93617776')
compare_coords(coords, expected)

# Parse a SkyCoord in ICRS frame
coords = mast.utils.parse_coordinates(expected)
compare_coords(coords, expected)

# Parse a SkyCoord in galactic frame
galactic = SkyCoord('0 0', unit='deg', frame='galactic')
coords = mast.utils.parse_coordinates(galactic)
compare_coords(coords, galactic.transform_to('icrs'))
33 changes: 31 additions & 2 deletions astroquery/mast/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,32 @@ def resolve_object(objectname):
return coordinates


def parse_coordinates(coordinates):
"""
Convenience function to parse user input of coordinates.
Parameters
----------
coordinates : str or `astropy.coordinates` object, optional
The target around which to search. It may be specified as a
string or as the appropriate `astropy.coordinates` object.
Returns
-------
response : `~astropy.coordinates.SkyCoord`
The given coordinates as an `~astropy.coordinates.SkyCoord` object in the ICRS frame.
"""

# Parse into SkyCoord object
coordinates = commons.parse_coordinates(coordinates)

# Convert to ICRS frame, if needed
if coordinates.frame != 'icrs':
coordinates = coordinates.transform_to('icrs')

return coordinates


def parse_input_location(coordinates=None, objectname=None):
"""
Convenience function to parse user input of coordinates and objectname.
Expand All @@ -137,7 +163,8 @@ def parse_input_location(coordinates=None, objectname=None):
Returns
-------
response : `~astropy.coordinates.SkyCoord`
The given coordinates, or object's location as an `~astropy.coordinates.SkyCoord` object.
The given coordinates, or object's location as an `~astropy.coordinates.SkyCoord` object
in the ICRS frame.
"""

# Checking for valid input
Expand All @@ -147,11 +174,13 @@ def parse_input_location(coordinates=None, objectname=None):
if not (objectname or coordinates):
raise InvalidQueryError("One of objectname and coordinates must be specified.")

# Resolve object, if given
if objectname:
obj_coord = resolve_object(objectname)

# Parse coordinates, if given
if coordinates:
obj_coord = commons.parse_coordinates(coordinates)
obj_coord = parse_coordinates(coordinates)

return obj_coord

Expand Down

0 comments on commit 31ffc55

Please sign in to comment.