Skip to content

Commit 7fd6402

Browse files
committed
addressing comments, second iteration
1 parent 1a80f5d commit 7fd6402

File tree

8 files changed

+83
-95
lines changed

8 files changed

+83
-95
lines changed

astroquery/desi/__init__.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ class Conf(_config.ConfigNamespace):
2020
"""
2121
Configuration parameters for `astroquery.desi`.
2222
"""
23-
server = _config.ConfigItem(
24-
['https://portal.nersc.gov/cfs/cosmo/data/legacysurvey/',
23+
legacysurvey_service_url = _config.ConfigItem(
24+
['https://www.legacysurvey.org/viewer/fits-cutout',
2525
],
26-
'base url')
26+
'url for the LegacySurvey service')
2727

28-
timeout = _config.ConfigItem(
29-
30,
30-
'Time limit for connecting to template_module server.')
28+
tap_service_url = _config.ConfigItem(
29+
['https://datalab.noirlab.edu/tap',
30+
],
31+
'url for the TAP service')
3132

3233

3334
conf = Conf()

astroquery/desi/core.py

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22
import urllib.error
33
import requests
4+
import warnings
45

56
import pyvo as vo
67
import numpy as np
8+
import astropy.coordinates as coord
79

810
from astroquery.exceptions import NoResultsWarning
911
from astroquery.query import BaseQuery
1012
from astroquery.utils import commons, async_to_sync
11-
12-
from . import conf
13-
13+
from astroquery.desi import conf
1414

1515
__all__ = ['DESILegacySurvey', 'DESILegacySurveyClass']
1616

1717

18-
@async_to_sync
1918
class DESILegacySurveyClass(BaseQuery):
20-
URL = conf.server
21-
TIMEOUT = conf.timeout
2219

23-
def query_region(self, coordinates, radius, data_release=9):
20+
def query_region(self, coordinates, radius, *, data_release=9):
2421
"""
2522
Queries a region around the specified coordinates.
2623
2724
Parameters
2825
----------
29-
coordinates : str or `astropy.coordinates`.
30-
coordinates around which to query
31-
radius : str or `astropy.units.Quantity`.
32-
the radius of the cone search
26+
coordinates : `astropy.coordinates`
27+
coordinates around which to query.
28+
radius : `astropy.units.Quantity`
29+
the radius of the cone search.
30+
data_release: int
31+
the data release of the LegacySurvey to use.
3332
3433
Returns
3534
-------
3635
response : `astropy.table.Table`
3736
"""
3837

39-
url = 'https://datalab.noirlab.edu/tap'
40-
tap_service = vo.dal.TAPService(url)
41-
qstr = "SELECT all * FROM ls_dr" + str(data_release) + ".tractor WHERE dec>" + str(coordinates.dec.deg - radius.deg) + " and dec<" + str(
42-
coordinates.dec.deg + radius.deg) + " and ra>" + str(coordinates.ra.deg - radius.deg / np.cos(coordinates.dec.deg * np.pi / 180.)) + " and ra<" + str(
43-
coordinates.ra.deg + radius.deg / np.cos(coordinates.dec.deg * np.pi / 180))
38+
tap_service = vo.dal.TAPService(conf.tap_service_url)
39+
coordinates_transformed = coordinates.transform_to(coord.ICRS)
40+
41+
qstr = (f"SELECT all * FROM ls_dr{data_release}.tractor WHERE "
42+
f"dec>{coordinates_transformed.dec.deg - radius.deg} and "
43+
f"dec<{coordinates_transformed.dec.deg + radius.deg} and "
44+
f"ra>{coordinates_transformed.ra.deg - radius.deg / np.cos(coordinates_transformed.dec.deg * np.pi / 180.)} and "
45+
f"ra<{coordinates_transformed.ra.deg + radius.deg / np.cos(coordinates_transformed.dec.deg * np.pi / 180)}")
4446

4547
tap_result = tap_service.run_sync(qstr)
4648
tap_result = tap_result.to_table()
@@ -50,26 +52,44 @@ def query_region(self, coordinates, radius, data_release=9):
5052

5153
return filtered_table
5254

53-
def get_images(self, position, data_release=9, pixels=None, radius=None, show_progress=True, image_band='g'):
55+
def get_images(self, position, pixels, radius, *, data_release=9, show_progress=True, image_band='g'):
5456
"""
57+
Downloads the images for a certain region of interest.
58+
59+
Parameters
60+
-------
61+
position: `astropy.coordinates`.
62+
coordinates around which we define our region of interest.
63+
radius: `astropy.units.Quantity`.
64+
the radius of the cone search.
65+
data_release: int
66+
the data release of the LegacySurvey to use.
67+
5568
Returns
5669
-------
57-
A list of `astropy.io.fits.HDUList` objects.
70+
list: A list of `astropy.io.fits.HDUList` objects.
5871
"""
5972

73+
position_transformed = position.transform_to(coord.ICRS)
74+
6075
image_size_arcsec = radius.arcsec
6176
pixsize = 2 * image_size_arcsec / pixels
6277

63-
image_url = 'https://www.legacysurvey.org/viewer/fits-cutout?ra=' + str(position.ra.deg) + '&dec=' + str(position.dec.deg) + '&size=' + str(
64-
pixels) + '&layer=ls-dr' + str(data_release) + '&pixscale=' + str(pixsize) + '&bands=' + image_band
78+
image_url = (f"{conf.legacysurvey_service_url}?"
79+
f"ra={position_transformed.ra.deg}&"
80+
f"dec={position_transformed.dec.deg}&"
81+
f"size={pixels}&"
82+
f"layer=ls-dr{data_release}&"
83+
f"pixscale={pixsize}&"
84+
f"bands={image_band}")
6585

6686
file_container = commons.FileContainer(image_url, encoding='binary', show_progress=show_progress)
6787

6888
try:
6989
fits_file = file_container.get_fits()
7090
except (requests.exceptions.HTTPError, urllib.error.HTTPError) as e:
71-
# TODO not sure this is the most suitable exception
72-
raise NoResultsWarning(f"{str(e)} - Problem retrieving the file at the url: {str(image_url)}")
91+
fits_file = None
92+
warnings.warn(f"{str(e)} - Problem retrieving the file at the url: {image_url}", NoResultsWarning)
7393

7494
return [fits_file]
7595

astroquery/desi/tests/setup_package.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
def get_package_data():
66
paths = [os.path.join('data', '*.txt'),
77
os.path.join('data', '*.fits'),
8-
os.path.join('data', '*.fits.gz'),
98
]
109
return {'astroquery.desi.tests': paths}

astroquery/desi/tests/test_module.py renamed to astroquery/desi/tests/test_desi.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
from astropy.table import Table
88
from astropy.io import fits
9-
from pyvo.dal import TAPResults
9+
from astropy import coordinates as coord
10+
from astroquery.utils.mocks import MockResponse
11+
from astroquery.utils import commons
12+
from astroquery import desi
1013
from urllib import parse
1114
from contextlib import contextmanager
1215

13-
from ... import desi
14-
from ...utils.mocks import MockResponse
15-
from ...utils import commons
1616

1717
DATA_FILES = {
1818
'dummy_tap_table': 'dummy_table.txt',
@@ -21,18 +21,15 @@
2121
}
2222

2323
coords = commons.ICRSCoord('11h04m27s +38d12m32s')
24-
radius = commons.ArcminRadiusGenerator(0.5)
24+
radius = coord.Angle(0.5, unit='arcmin')
2525
pixels = 60
2626
data_release = 9
2727
emispheres_list = ['north', 'south']
2828

2929

3030
@pytest.fixture
3131
def patch_get(request):
32-
try:
33-
mp = request.getfixturevalue("monkeypatch")
34-
except AttributeError: # pytest < 3
35-
mp = request.getfuncargvalue("monkeypatch")
32+
mp = request.getfixturevalue("monkeypatch")
3633

3734
mp.setattr(desi.DESILegacySurvey, '_request', get_mockreturn)
3835
return mp
@@ -68,10 +65,7 @@ def get_readable_fileobj_mockreturn(filename, **kwargs):
6865

6966
@pytest.fixture
7067
def patch_tap(request):
71-
try:
72-
mp = request.getfixturevalue("monkeypatch")
73-
except AttributeError: # pytest < 3
74-
mp = request.getfuncargvalue("monkeypatch")
68+
mp = request.getfixturevalue("monkeypatch")
7569

7670
mp.setattr(vo.dal.TAPService, 'run_sync', tap_mockreturn)
7771
return mp
@@ -95,7 +89,7 @@ def tap_mockreturn(url, params=None, timeout=10, **kwargs):
9589
content_table = Table.read(data_path(DATA_FILES['dummy_tap_table']),
9690
format='ascii.csv', comment='#')
9791
votable_table = astropy.io.votable.from_table(content_table)
98-
return TAPResults(votable_table)
92+
return vo.dal.TAPResults(votable_table)
9993

10094

10195
def data_path(filename):

astroquery/desi/tests/test_module_remote.py renamed to astroquery/desi/tests/test_desi_remote.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2-
31
import pytest
2+
import astroquery.desi
3+
44
from astropy.io.fits import HDUList
5+
from astropy.coordinates import SkyCoord
6+
from astropy.coordinates import Angle
7+
from astropy.table import Table
58
from astroquery.exceptions import NoResultsWarning
69

710

811
@pytest.mark.remote_data
912
class TestLegacySurveyClass:
1013

1114
def test_query_region(self):
12-
import astroquery.desi
13-
from astropy.coordinates import SkyCoord
14-
from astropy.coordinates import Angle
15-
from astropy.table import Table
1615

1716
ra = Angle('11h04m27s', unit='hourangle').degree
1817
dec = Angle('+38d12m32s', unit='hourangle').degree
@@ -26,9 +25,6 @@ def test_query_region(self):
2625

2726
@pytest.mark.parametrize("valid_inputs", [True, False])
2827
def test_get_images(self, valid_inputs):
29-
import astroquery.desi
30-
from astropy.coordinates import SkyCoord
31-
from astropy.coordinates import Angle
3228

3329
if valid_inputs:
3430
ra = Angle('11h04m27s', unit='hourangle').degree
@@ -45,9 +41,9 @@ def test_get_images(self, valid_inputs):
4541
radius = Angle(radius_input, unit='arcmin')
4642

4743
if valid_inputs:
48-
query1 = astroquery.desi.DESILegacySurvey.get_images(pos, data_release=9, radius=radius, pixels=pixels)
44+
query1 = astroquery.desi.DESILegacySurvey.get_images(pos, pixels, radius, data_release=9)
4945
assert isinstance(query1, list)
5046
assert isinstance(query1[0], HDUList)
5147
else:
5248
with pytest.raises(NoResultsWarning):
53-
astroquery.desi.DESILegacySurvey.get_images(pos, data_release=9, radius=radius, pixels=pixels)
49+
astroquery.desi.DESILegacySurvey.get_images(pos, pixels, radius, data_release=9)

astroquery/utils/commons.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,6 @@ def FK4CoordGenerator(*args, **kwargs):
4242
return coord.SkyCoord(*args, frame='fk4', **kwargs)
4343

4444

45-
def ArcminRadiusGenerator(*args, **kwargs):
46-
return coord.Angle(*args, unit='arcmin', **kwargs)
47-
48-
4945
ICRSCoord = coord.SkyCoord
5046
CoordClasses = (coord.SkyCoord, BaseCoordinateFrame)
5147

docs/desi/desi.rst

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.. _astroquery.desi:
22

3-
************************************
3+
*********************************************
44
DESI LegacySurvey Queries (`astroquery.desi`)
5-
************************************
5+
*********************************************
66

77
Getting started
88
===============
@@ -12,13 +12,13 @@ Presented below are examples that illustrate the different types of queries
1212
that can be formulated.
1313

1414
Query a region
15-
===============
15+
==============
1616

1717
This example shows how to query a certain region with DesiLegacySurvey.
1818
We'll use a set of coordinates that define the region of interest,
1919
and search within a 5 arcmin radius.
2020

21-
.. code-block:: python
21+
.. doctest-remote-data::
2222

2323
>>> from astroquery.desi import DESILegacySurvey
2424
>>> from astropy.coordinates import Angle, SkyCoord
@@ -28,41 +28,43 @@ and search within a 5 arcmin radius.
2828
>>> radius = Angle(5, unit='arcmin')
2929
>>> table_out = DESILegacySurvey.query_region(coordinates, radius, data_release=9)
3030
>>> print(table_out[:5])
31-
32-
ls_id dec ra ... type wise_coadd_id
33-
---------------- ------------------ ------------------ ... ---- -------------
31+
ls_id dec ra ... type wise_coadd_id
32+
...
33+
---------------- ----------------- ------------------ ... ---- -------------
3434
9907734382838387 38.12628495570797 166.0838654387131 ... PSF 1667p378
3535
9907734382838488 38.12798336424771 166.0922968862182 ... PSF 1667p378
3636
9907734382838554 38.12858283671958 166.0980673954384 ... REX 1667p378
37-
9907734382838564 38.12840803351445 166.09921863682337 ... EXP 1667p378
38-
9907734382838584 38.12836885301038 166.10070750146636 ... REX 1667p378
37+
9907734382838564 38.12840803351445 166.09921863682337 ... EXP 1667p378
38+
9907734382838584 38.12836885301038 166.10070750146636 ... REX 1667p378
3939

4040
The result is an astropy.Table.
4141

4242
Get images
43-
===============
43+
==========
4444

4545
To download images for a certain region of interest,
4646
we can define our region in the same way used in the example above.
4747

48-
.. code-block:: python
48+
.. doctest-remote-data::
4949

5050
>>> from astroquery.desi import DESILegacySurvey
5151
>>> from astropy.coordinates import Angle, SkyCoord
52+
>>>
5253
>>> ra = Angle('11h04m27s', unit='hourangle').degree
5354
>>> dec = Angle('+38d12m32s', unit='hourangle').degree
54-
>>> pos = SkyCoord(ra, dec, unit='degree')
55-
>>> radius = Angle(0.5, unit='arcmin')
55+
>>> radius_input = 0.5
5656
>>> pixels = 60
57-
>>> im = DESILegacySurvey.get_images(pos, data_release=9, radius=radius, pixels=pixels)
57+
>>>
58+
>>> pos = SkyCoord(ra, dec, unit='degree')
59+
>>> radius = Angle(radius_input, unit='arcmin')
60+
>>> im = DESILegacySurvey.get_images(pos, pixels, radius, data_release=9)
5861

5962
All the information we need can be found within the object "im".
6063

61-
.. code-block:: python
64+
.. doctest-remote-data::
6265

6366
>>> hdul = im[0]
6467
>>> hdul[0].header
65-
6668
SIMPLE = T / file does conform to FITS standard
6769
BITPIX = -32 / number of bits per data pixel
6870
NAXIS = 2 / number of data axes
@@ -88,24 +90,3 @@ All the information we need can be found within the object "im".
8890

8991
The variable "im" is a list of `~astropy.io.fits.HDUList` objects, one entry for
9092
each corresponding object.
91-
92-
In case a set of not valid coordinates is provided, then a `astroquery.exceptions.NoResultsWarning`
93-
exception is raised, as shown in the example below.
94-
95-
.. code-block:: python
96-
97-
>>> from astroquery.desi import DESILegacySurvey
98-
>>> from astropy.coordinates import Angle, SkyCoord
99-
>>> ra = Angle('86.633212', unit='degree').degree
100-
>>> dec = Angle('22.01446', unit='degree').degree
101-
>>> radius_input = 3
102-
>>> pixels = 1296000
103-
104-
>>> pos = SkyCoord(ra, dec, unit='degree')
105-
>>> radius = Angle(radius_input, unit='arcmin')
106-
>>> pixels = 60
107-
>>> im = DESILegacySurvey.get_images(pos, data_release=9, radius=radius, pixels=pixels)
108-
109-
.. code-block:: python
110-
111-
astroquery.exceptions.NoResultsWarning: HTTP Error 500: Internal Server Error - Problem retrieving the file at the url: https://www.legacysurvey.org/viewer/fits-cutout?ra=86.633212&dec=22.01446&size=1296000&layer=ls-dr9&pixscale=0.0002777777777777778&bands=g

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ The following modules have been completed using a common API:
195195
cds/cds.rst
196196
linelists/cdms/cdms.rst
197197
dace/dace.rst
198+
desi/desi.rst
198199
esa/hsa/hsa.rst
199200
esa/hubble/hubble.rst
200201
esa/iso/iso.rst

0 commit comments

Comments
 (0)