Skip to content

Commit

Permalink
expanding coord search to hms format
Browse files Browse the repository at this point in the history
  • Loading branch information
havok2063 committed Oct 24, 2023
1 parent f0b4251 commit c7c33e3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
54 changes: 53 additions & 1 deletion python/valis/db/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,72 @@

# all resuable queries go here

from typing import Union
import peewee
import astropy.units as u
from astropy.coordinates import SkyCoord
from sdssdb.peewee.sdss5db import vizdb


def cone_search(ra: float, dec: float, radius: float, units: str = 'degree'):
def convert_coords(ra: Union[str, float], dec: Union[str, float]) -> tuple:
""" Convert sky coordinates to decimal degrees
Convert the input RA, Dec sky coordinates into decimal
degrees. Input format can either be decimal or hmsdms.
Parameters
----------
ra : str
The Right Ascension
dec : str
The Declination
Returns
-------
tuple
the converted (RA, Dec)
"""
is_hms = set('hms: ') & set(str(ra))
if is_hms:
ra = str(ra).replace(' ', ':')
dec = str(dec).replace(' ', ':')
unit = ('hourangle', 'degree') if is_hms else ('degree', 'degree')
coord = SkyCoord(f'{ra} {dec}', unit=unit)
ra = round(coord.ra.value, 5)
dec = round(coord.dec.value, 5)
return float(ra), float(dec)


def cone_search(ra: Union[str, float], dec: Union[str, float],
radius: float, units: str = 'degree') -> peewee.ModelSelect:
""" Perform a cone search against the vizdb sdss_id_stacked table
Perform a cone search using the peewee ORM for SDSS targets in the
vizdb sdss_id_stacked table. We return the peewee ModelSelect
directly here so it can be easily combined with other queries.
In the route endpoint itself, remember to return wrap this in a list.
Parameters
----------
ra : Union[str, float]
the Right Ascension coord
dec : Union[str, float]
the Declination coord
radius : float
the cone search radius
units : str, optional
the units of the search radius, by default 'degree'
Returns
-------
peewee.ModelSelect
the ORM query
"""

# convert ra, dec to decimal if in hms-dms
ra, dec = convert_coords(ra, dec)

# convert radial units to degrees
radius *= u.Unit(units)
radius = radius.to(u.degree).value
Expand Down
6 changes: 3 additions & 3 deletions python/valis/routes/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#

from enum import Enum
from typing import List
from typing import List, Union
from fastapi import APIRouter, Depends, Query
from fastapi_utils.cbv import cbv

Expand Down Expand Up @@ -43,8 +43,8 @@ class QueryRoutes(Base):
@router.get('/cone', summary='Perform a cone search for SDSS targets with sdss_ids',
response_model=List[SDSSidStackedBase], dependencies=[Depends(get_pw_db)])
async def cone_search(self,
ra: float = Query(..., description='Right Ascension in degrees', example=315.01417),
dec: float = Query(..., description='Declination in degrees', example=35.299),
ra: Union[float, str] = Query(..., description='Right Ascension in degrees', example=315.01417),
dec: Union[float, str] = Query(..., description='Declination in degrees', example=35.299),
radius: float = Query(..., description='Search radius in specified units', example=0.01),
units: SearchCoordUnits = Query('degree', description='Units of search radius', example='degree')):
""" Perform a cone search """
Expand Down
24 changes: 24 additions & 0 deletions tests/test_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# encoding: utf-8
#

import pytest
from valis.db.queries import convert_coords


@pytest.mark.parametrize('ra, dec, exp',
[('315.01417', '35.299', (315.01417, 35.299)),
('315.01417', '-35.299', (315.01417, -35.299)),
(315.01417, -35.299, (315.01417, -35.299)),
('21h00m03.4008s', '+35d17m56.4s', (315.01417, 35.299)),
('21:00:03.4008', '+35:17:56.4', (315.01417, 35.299)),
('21 00 03.4008', '+35 17 56.4', (315.01417, 35.299)),
],
ids=['dec1', 'dec2', 'dec3', 'hms1', 'hms2', 'hms3'])
def test_convert_coords(ra, dec, exp):
""" test we can convert coordinates correctly """
coord = convert_coords(ra, dec)
assert coord == exp




0 comments on commit c7c33e3

Please sign in to comment.