From 31ffc5565f46cf06fa34199c82a06e5d5e8aef6d Mon Sep 17 00:00:00 2001 From: Sam Bianco Date: Mon, 23 Dec 2024 14:33:58 -0600 Subject: [PATCH] Utils method to parse coordinates --- astroquery/mast/collections.py | 4 ++-- astroquery/mast/missions.py | 4 ++-- astroquery/mast/observations.py | 6 +++--- astroquery/mast/tests/test_mast.py | 30 +++++++++++++++++++++++++++ astroquery/mast/utils.py | 33 ++++++++++++++++++++++++++++-- 5 files changed, 68 insertions(+), 9 deletions(-) diff --git a/astroquery/mast/collections.py b/astroquery/mast/collections.py index 3eea6eb806..955172eb68 100644 --- a/astroquery/mast/collections.py +++ b/astroquery/mast/collections.py @@ -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 @@ -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) diff --git a/astroquery/mast/missions.py b/astroquery/mast/missions.py index 0d56fcda11..6829c3d468 100644 --- a/astroquery/mast/missions.py +++ b/astroquery/mast/missions.py @@ -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 @@ -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) diff --git a/astroquery/mast/observations.py b/astroquery/mast/observations.py index a0e595c6ba..93afc335e7 100644 --- a/astroquery/mast/observations.py +++ b/astroquery/mast/observations.py @@ -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) @@ -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) @@ -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) diff --git a/astroquery/mast/tests/test_mast.py b/astroquery/mast/tests/test_mast.py index 282d9f0648..3811453468 100644 --- a/astroquery/mast/tests/test_mast.py +++ b/astroquery/mast/tests/test_mast.py @@ -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')) diff --git a/astroquery/mast/utils.py b/astroquery/mast/utils.py index 56bfa5810a..ac7e708dcb 100644 --- a/astroquery/mast/utils.py +++ b/astroquery/mast/utils.py @@ -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. @@ -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 @@ -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