Skip to content

Commit 62fc86e

Browse files
committed
add rectangle helper function
1 parent 90ab03e commit 62fc86e

File tree

2 files changed

+68
-13
lines changed

2 files changed

+68
-13
lines changed

astroquery/sdss/core.py

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,6 @@ class = 'galaxy' \
610610
0.3000027 256.99461 25.566255 1237661387086693265
611611
0.300003 175.65125 34.37548 1237665128003731630
612612
613-
614613
Returns
615614
-------
616615
result : `~astropy.table.Table`
@@ -683,6 +682,14 @@ def get_spectra_async(self, *, coordinates=None, radius=2. * u.arcsec,
683682
show_progress : bool, optional
684683
If False, do not display download progress.
685684
685+
Returns
686+
-------
687+
list : list
688+
A list of context-managers that yield readable file-like objects.
689+
The function returns the spectra for only one of ``matches``, or
690+
``coordinates`` and ``radius``, or ``plate``, ``mjd`` and
691+
``fiberID``.
692+
686693
Examples
687694
--------
688695
Using results from a call to `query_region`:
@@ -701,14 +708,6 @@ def get_spectra_async(self, *, coordinates=None, radius=2. * u.arcsec,
701708
702709
>>> specs = SDSS.get_spectra(plate=751, mjd=52251)
703710
704-
Returns
705-
-------
706-
list : list
707-
A list of context-managers that yield readable file-like objects.
708-
The function returns the spectra for only one of ``matches``, or
709-
``coordinates`` and ``radius``, or ``plate``, ``mjd`` and
710-
``fiberID``.
711-
712711
"""
713712

714713
if not matches:
@@ -852,6 +851,10 @@ def get_images_async(self, coordinates=None, radius=2. * u.arcsec,
852851
show_progress : bool, optional
853852
If False, do not display download progress.
854853
854+
Returns
855+
-------
856+
list : List of `~astropy.io.fits.HDUList` objects.
857+
855858
Examples
856859
--------
857860
Using results from a call to `query_region`:
@@ -874,10 +877,6 @@ def get_images_async(self, coordinates=None, radius=2. * u.arcsec,
874877
875878
>>> imgs = SDSS.get_images(run=1904, camcol=3, field=164)
876879
877-
Returns
878-
-------
879-
list : List of `~astropy.io.fits.HDUList` objects.
880-
881880
"""
882881
if not matches:
883882
if coordinates is None:
@@ -1276,5 +1275,55 @@ def _get_crossid_url(self, data_release):
12761275
self._last_url = url
12771276
return url
12781277

1278+
def _rectangle_sql(self, ra, dec, width, height=None, cosdec=False):
1279+
"""Generate SQL for a rectangular query centered on `ra`, `dec`.
1280+
1281+
This assumes that RA is defined on the range ``[0, 360)``, and Dec on
1282+
``[-90, 90]``.
1283+
1284+
Parameters
1285+
----------
1286+
ra : float
1287+
Right Ascension in degrees.
1288+
dec : float
1289+
Declination in degrees.
1290+
width : float
1291+
Width of rectangle in degrees.
1292+
height : float, optional
1293+
Height of rectangle in degrees. If not specified, `width` is used.
1294+
cosdec : bool, optional
1295+
If ``True`` apply ``cos(dec)`` correction to the rectangle.
1296+
1297+
Returns
1298+
-------
1299+
:class:`str`
1300+
A string defining the rectangle in SQL notation.
1301+
"""
1302+
if height is None:
1303+
height = width
1304+
dr = width/2.0
1305+
dd = height/2.0
1306+
d0 = dec - dd
1307+
if d0 < -90:
1308+
d0 = -90.0
1309+
d1 = dec + dd
1310+
if d1 > 90.0:
1311+
d1 = 90.0
1312+
ra_wrap = False
1313+
r0 = ra - dr
1314+
if r0 < 0:
1315+
ra_wrap = True
1316+
r0 += 360.0
1317+
r1 = ra + dr
1318+
if r1 > 360.0:
1319+
ra_wrap = True
1320+
r1 -= 360.0
1321+
# BETWEEN is inclusive, so it is equivalent to the <=, >= operators.
1322+
if ra_wrap:
1323+
sql = f"(((p.ra >= {r0:g}) OR (p.ra <= {r1:g}))"
1324+
else:
1325+
sql = f"((p.ra BETWEEN {r0:g} AND {r1:g})"
1326+
return sql + f" AND (p.dec BETWEEN {d0:g} AND {d1:g}))"
1327+
12791328

12801329
SDSS = SDSSClass()

astroquery/sdss/tests/test_sdss.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,3 +593,9 @@ def test_field_help_region(patch_request):
593593

594594
assert len(non_existing_field) == 2
595595
assert set(non_existing_field.keys()) == set(('photoobj_all', 'specobj_all'))
596+
597+
598+
def test_rectangle_sql():
599+
sql = sdss.SDSS._rectangle_sql(0, 0, 1)
600+
assert sql == '(((p.ra >= 359.5) OR (p.ra <= 0.5)) AND (p.dec BETWEEN -0.5 AND 0.5))
601+
# assert sql == '((p.ra BETWEEN -0.5 AND 0.5) AND (p.dec BETWEEN -0.5 AND 0.5))

0 commit comments

Comments
 (0)