Skip to content

Commit b3c3b03

Browse files
committed
addressing review
1 parent 753dbc8 commit b3c3b03

File tree

11 files changed

+267
-175
lines changed

11 files changed

+267
-175
lines changed
File renamed without changes.

astroquery/hsa/core.py renamed to astroquery/esa/hsa/core.py

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import shutil
77
from pathlib import Path
88

9+
from astropy import units as u
10+
from ...utils import commons
911
from astroquery import log
1012
from astroquery.exceptions import LoginError, InputWarning
1113
from astroquery.query import BaseQuery
@@ -29,13 +31,9 @@ def __init__(self, tap_handler=None):
2931
else:
3032
self._tap = tap_handler
3133

32-
def download_data(self, *, retrieval_type=None,
33-
observation_id=None,
34-
instrument_name=None,
35-
filename=None,
36-
verbose=False,
37-
cache=True,
38-
**kwargs):
34+
def download_data(self, *, retrieval_type="OBSERVATION", observation_id=None,
35+
instrument_name=None, filename=None, verbose=False,
36+
download_dir="", cache=True, **kwargs):
3937
"""
4038
Download data from Herschel
4139
@@ -64,6 +62,8 @@ def download_data(self, *, retrieval_type=None,
6462
product_level : string, optional
6563
level to download
6664
values: ALL, AUXILIARY, CALIBRATION, LEVEL0, LEVEL0_5, LEVEL1, LEVEL2, LEVEL2_5, LEVEL3, ALL-LEVEL3
65+
download_dir : string, optional
66+
The directory in which the file will be downloaded
6767
6868
Returns
6969
-------
@@ -72,9 +72,6 @@ def download_data(self, *, retrieval_type=None,
7272
if filename is not None:
7373
filename = os.path.splitext(filename)[0]
7474

75-
if retrieval_type is None:
76-
retrieval_type = "OBSERVATION"
77-
7875
params = {'retrieval_type': retrieval_type}
7976
if observation_id is not None:
8077
params['observation_id'] = observation_id
@@ -85,11 +82,9 @@ def download_data(self, *, retrieval_type=None,
8582
if instrument_name is not None:
8683
params['instrument_name'] = instrument_name
8784

88-
link = self.data_url + "".join("&{0}={1}".format(key, val)
89-
for key, val in params.items())
85+
link = self.data_url + "".join("&{0}={1}".format(key, val) for key, val in params.items())
9086

91-
link += "".join("&{0}={1}".format(key, val)
92-
for key, val in kwargs.items())
87+
link += "".join("&{0}={1}".format(key, val) for key, val in kwargs.items())
9388

9489
if verbose:
9590
log.info(link)
@@ -107,7 +102,7 @@ def download_data(self, *, retrieval_type=None,
107102
if observation_id is not None:
108103
filename = observation_id
109104
else:
110-
error = "Please set a filename for the output"
105+
error = "Please set either 'obervation_id' or 'filename' for the output"
111106
raise ValueError(error)
112107

113108
r_filename = params["filename"]
@@ -118,6 +113,8 @@ def download_data(self, *, retrieval_type=None,
118113
else:
119114
filename += "".join(suffixes)
120115

116+
filename = os.path.join(download_dir, filename)
117+
121118
self._download_file(link, filename, head_safe=True, cache=cache)
122119

123120
if verbose:
@@ -126,8 +123,7 @@ def download_data(self, *, retrieval_type=None,
126123
return filename
127124

128125
def get_observation(self, observation_id, instrument_name, *, filename=None,
129-
verbose=False,
130-
cache=True, **kwargs):
126+
verbose=False, download_dir="", cache=True, **kwargs):
131127
"""
132128
Download observation from Herschel.
133129
This consists of a .tar file containing:
@@ -141,11 +137,11 @@ def get_observation(self, observation_id, instrument_name, *, filename=None,
141137
142138
Parameters
143139
----------
144-
observation_id : string, mandatory
140+
observation_id : string
145141
id of the observation to be downloaded
146142
The identifies of the observation we want to retrieve, 10 digits
147143
example: 1342195355
148-
instrument_name : string, mandatory
144+
instrument_name : string
149145
The instrument name
150146
values: PACS, SPIRE, HIFI
151147
filename : string, optional, default None
@@ -161,6 +157,8 @@ def get_observation(self, observation_id, instrument_name, *, filename=None,
161157
product_level : string, optional
162158
level to download
163159
values: ALL, AUXILIARY, CALIBRATION, LEVEL0, LEVEL0_5, LEVEL1, LEVEL2, LEVEL2_5, LEVEL3, ALL-LEVEL3
160+
download_dir : string, optional
161+
The directory in which the file will be downloaded
164162
165163
Returns
166164
-------
@@ -173,11 +171,9 @@ def get_observation(self, observation_id, instrument_name, *, filename=None,
173171
'observation_id': observation_id,
174172
'instrument_name': instrument_name}
175173

176-
link = self.data_url + "".join("&{0}={1}".format(key, val)
177-
for key, val in params.items())
174+
link = self.data_url + "".join("&{0}={1}".format(key, val) for key, val in params.items())
178175

179-
link += "".join("&{0}={1}".format(key, val)
180-
for key, val in kwargs.items())
176+
link += "".join("&{0}={1}".format(key, val) for key, val in kwargs.items())
181177

182178
if verbose:
183179
log.info(link)
@@ -199,6 +195,8 @@ def get_observation(self, observation_id, instrument_name, *, filename=None,
199195

200196
filename += "".join(suffixes)
201197

198+
filename = os.path.join(download_dir, filename)
199+
202200
self._download_file(link, filename, head_safe=True, cache=cache)
203201

204202
if verbose:
@@ -207,18 +205,17 @@ def get_observation(self, observation_id, instrument_name, *, filename=None,
207205
return filename
208206

209207
def get_postcard(self, observation_id, instrument_name, *, filename=None,
210-
verbose=False,
211-
cache=True, **kwargs):
208+
verbose=False, download_dir="", cache=True, **kwargs):
212209
"""
213210
Download postcard from Herschel
214211
215212
Parameters
216213
----------
217-
observation_id : string, mandatory
214+
observation_id : string
218215
id of the observation to be downloaded
219216
The identifies of the observation we want to retrieve, 10 digits
220217
example: 1342195355
221-
instrument_name : string, mandatory
218+
instrument_name : string
222219
The instrument name
223220
values: PACS, SPIRE, HIFI
224221
filename : string, optional, default None
@@ -237,6 +234,8 @@ def get_postcard(self, observation_id, instrument_name, *, filename=None,
237234
postcard_single : string, optional
238235
'true' to retrieve one single postcard (main one)
239236
values: true, false
237+
download_dir : string, optional
238+
The directory in which the file will be downloaded
240239
241240
Returns
242241
-------
@@ -249,11 +248,9 @@ def get_postcard(self, observation_id, instrument_name, *, filename=None,
249248
'observation_id': observation_id,
250249
'instrument_name': instrument_name}
251250

252-
link = self.data_url + "".join("&{0}={1}".format(key, val)
253-
for key, val in params.items())
251+
link = self.data_url + "".join("&{0}={1}".format(key, val) for key, val in params.items())
254252

255-
link += "".join("&{0}={1}".format(key, val)
256-
for key, val in kwargs.items())
253+
link += "".join("&{0}={1}".format(key, val) for key, val in kwargs.items())
257254

258255
if verbose:
259256
log.info(link)
@@ -270,6 +267,8 @@ def get_postcard(self, observation_id, instrument_name, *, filename=None,
270267

271268
filename += ext
272269

270+
filename = os.path.join(download_dir, filename)
271+
273272
shutil.move(local_filepath, filename)
274273

275274
if verbose:
@@ -284,7 +283,7 @@ def query_hsa_tap(self, query, *, output_file=None,
284283
285284
Parameters
286285
----------
287-
query : string, mandatory
286+
query : string
288287
query (adql) to be executed
289288
output_file : string, optional, default None
290289
file name where the results are saved if dumpToFile is True.
@@ -332,13 +331,13 @@ def get_columns(self, table_name, *, only_names=True, verbose=False):
332331
333332
Parameters
334333
----------
335-
table_name : string, mandatory
334+
table_name : string
336335
table name of which, columns will be returned
337336
only_names : bool, optional, default True
338337
True to load column names only
339338
verbose : bool, optional, default False
340-
341339
flag to display information about the process
340+
342341
Returns
343342
-------
344343
A list of columns
@@ -360,5 +359,33 @@ def get_columns(self, table_name, *, only_names=True, verbose=False):
360359
else:
361360
return columns
362361

362+
def query_observations(self, coordinate, radius, n_obs=10):
363+
"""
364+
Get the observation IDs from a given region
365+
366+
Parameters
367+
----------
368+
coordinate : string / `astropy.coordinates`
369+
the identifier or coordinates around which to query
370+
radius : int / `~astropy.units.Quantity`
371+
the radius of the region
372+
n_obs : int, optional
373+
the number of observations
374+
375+
Returns
376+
-------
377+
A table object with the list of observations in the region
378+
"""
379+
r = radius
380+
if not isinstance(radius, u.Quantity):
381+
r = radius*u.deg
382+
coord = commons.parse_coordinates(coordinate).icrs
383+
384+
query = ("select top {} observation_id from hsa.v_active_observation "
385+
"where contains(point('ICRS', hsa.v_active_observation.ra, "
386+
"hsa.v_active_observation.dec), circle('ICRS', {},{},{}))=1")\
387+
.format(n_obs, coord.ra.degree, coord.dec.degree, r.to(u.deg).value)
388+
return self.query_hsa_tap(query)
389+
363390

364391
HSA = HSAClass()

astroquery/hsa/tests/dummy_tap_handler.py renamed to astroquery/esa/hsa/tests/dummy_tap_handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

3-
from ...utils.tap.model.taptable import TapTableMeta
4-
from ...utils.tap.model.job import Job
3+
from ....utils.tap.model.taptable import TapTableMeta
4+
from ....utils.tap.model.job import Job
55

66

77
class DummyHSATapHandler(object):

astroquery/hsa/tests/setup_package.py renamed to astroquery/esa/hsa/tests/setup_package.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22

3-
from __future__ import absolute_import
3+
#from __future__ import absolute_import
44

55
import os
66

astroquery/hsa/tests/test_hsa.py renamed to astroquery/esa/hsa/tests/test_hsa.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
22
import pytest
33

4+
from astropy import units as u
5+
from astropy.coordinates import SkyCoord
46
from ..core import HSAClass
57
from ..tests.dummy_handler import DummyHandler
68
from ..tests.dummy_tap_handler import DummyHSATapHandler
@@ -46,3 +48,12 @@ def test_get_columns(self):
4648
hsa = HSAClass(self.get_dummy_tap_handler())
4749
hsa.get_columns(**parameters)
4850
dummyTapHandler.check_call("get_columns", parameters)
51+
52+
def test_query_observations(self):
53+
c = SkyCoord(ra=100.2417*u.degree, dec=9.895*u.degree, frame='icrs')
54+
parameters = {'coordinate': c,
55+
'radius': 0.5}
56+
dummyTapHandler = DummyHSATapHandler("query_observations",parameters)
57+
hsa = HSAClass(self.get_dummy_tap_handler())
58+
hsa.query_observations(**parameters)
59+
dummyTapHandler.check_call("query_observations", parameters)

0 commit comments

Comments
 (0)