Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve generating Gaia ADQL queries #2831

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 47 additions & 65 deletions astroquery/gaia/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from datetime import datetime
import shutil
from collections.abc import Iterable
from textwrap import dedent

from astropy import units
from astropy.coordinates import Angle
Expand Down Expand Up @@ -397,44 +398,33 @@ def __query_object(self, coordinate, *, radius=None, width=None, height=None,
else:
raHours, dec = commons.coord_to_radec(coord)
ra = raHours * 15.0 # Converts to degrees
widthQuantity = self.__getQuantityInput(width, "width")
heightQuantity = self.__getQuantityInput(height, "height")
widthDeg = widthQuantity.to(units.deg)
heightDeg = heightQuantity.to(units.deg)

if columns:
columns = ','.join(map(str, columns))
else:
columns = "*"

query = """
SELECT
{row_limit}
DISTANCE(
POINT('ICRS', {ra_column}, {dec_column}),
POINT('ICRS', {ra}, {dec})
) as dist,
{columns}
FROM
{table_name}
WHERE
1 = CONTAINS(
POINT('ICRS', {ra_column}, {dec_column}),
BOX(
'ICRS',
{ra},
{dec},
{width},
{height}
)
)
ORDER BY
dist ASC
""".format(**{'row_limit': "TOP {0}".format(self.ROW_LIMIT) if self.ROW_LIMIT > 0 else "",
'ra_column': self.MAIN_GAIA_TABLE_RA, 'dec_column': self.MAIN_GAIA_TABLE_DEC,
'columns': columns, 'table_name': self.MAIN_GAIA_TABLE or conf.MAIN_GAIA_TABLE,
'ra': ra, 'dec': dec,
'width': widthDeg.value, 'height': heightDeg.value})
query = dedent(
f"""\
SELECT
{f"TOP {self.ROW_LIMIT}" if self.ROW_LIMIT > 0 else ""}
DISTANCE(
POINT('ICRS', {self.MAIN_GAIA_TABLE_RA}, {self.MAIN_GAIA_TABLE_DEC}),
POINT('ICRS', {ra}, {dec})
) as dist,
{",".join(columns) or "*"}
FROM
{self.MAIN_GAIA_TABLE or conf.MAIN_GAIA_TABLE}
WHERE
1 = CONTAINS(
POINT('ICRS', {self.MAIN_GAIA_TABLE_RA}, {self.MAIN_GAIA_TABLE_DEC}),
BOX(
'ICRS',
{ra},
{dec},
{self.__getQuantityInput(width, "width").to_value(u.deg)},
{self.__getQuantityInput(height, "height").to_value(u.deg)}
)
)
ORDER BY
dist ASC\
"""
)
if async_job:
job = self.launch_job_async(query, verbose=verbose)
else:
Expand Down Expand Up @@ -548,34 +538,26 @@ def __cone_search(self, coordinate, radius, *, table_name=None,
if radius is not None:
radiusDeg = Angle(self.__getQuantityInput(radius, "radius")).to_value(u.deg)

if columns:
columns = ','.join(map(str, columns))
else:
columns = "*"

query = """
SELECT
{row_limit}
{columns},
DISTANCE(
POINT('ICRS', {ra_column}, {dec_column}),
POINT('ICRS', {ra}, {dec})
) AS dist
FROM
{table_name}
WHERE
1 = CONTAINS(
POINT('ICRS', {ra_column}, {dec_column}),
CIRCLE('ICRS', {ra}, {dec}, {radius})
)
ORDER BY
dist ASC
""".format(**{'ra_column': ra_column_name,
'row_limit': "TOP {0}".format(self.ROW_LIMIT) if self.ROW_LIMIT > 0 else "",
'dec_column': dec_column_name, 'columns': columns, 'ra': ra, 'dec': dec,
'radius': radiusDeg,
'table_name': table_name or self.MAIN_GAIA_TABLE or conf.MAIN_GAIA_TABLE})

query = dedent(
f"""\
SELECT
{f"TOP {self.ROW_LIMIT}" if self.ROW_LIMIT > 0 else ""}
{",".join(columns) or "*"},
DISTANCE(
POINT('ICRS', {ra_column_name}, {dec_column_name}),
POINT('ICRS', {ra}, {dec})
) AS dist
FROM
{table_name or self.MAIN_GAIA_TABLE or conf.MAIN_GAIA_TABLE}
WHERE
1 = CONTAINS(
POINT('ICRS', {ra_column_name}, {dec_column_name}),
CIRCLE('ICRS', {ra}, {dec}, {radiusDeg})
)
ORDER BY
dist ASC\
"""
)
if async_job:
return self.launch_job_async(query=query,
output_file=output_file,
Expand Down