6
6
import shutil
7
7
from pathlib import Path
8
8
9
+ from astropy import units as u
10
+ from ...utils import commons
9
11
from astroquery import log
10
12
from astroquery .exceptions import LoginError , InputWarning
11
13
from astroquery .query import BaseQuery
@@ -29,13 +31,9 @@ def __init__(self, tap_handler=None):
29
31
else :
30
32
self ._tap = tap_handler
31
33
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 ):
39
37
"""
40
38
Download data from Herschel
41
39
@@ -64,6 +62,8 @@ def download_data(self, *, retrieval_type=None,
64
62
product_level : string, optional
65
63
level to download
66
64
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
67
67
68
68
Returns
69
69
-------
@@ -72,9 +72,6 @@ def download_data(self, *, retrieval_type=None,
72
72
if filename is not None :
73
73
filename = os .path .splitext (filename )[0 ]
74
74
75
- if retrieval_type is None :
76
- retrieval_type = "OBSERVATION"
77
-
78
75
params = {'retrieval_type' : retrieval_type }
79
76
if observation_id is not None :
80
77
params ['observation_id' ] = observation_id
@@ -85,11 +82,9 @@ def download_data(self, *, retrieval_type=None,
85
82
if instrument_name is not None :
86
83
params ['instrument_name' ] = instrument_name
87
84
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 ())
90
86
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 ())
93
88
94
89
if verbose :
95
90
log .info (link )
@@ -107,7 +102,7 @@ def download_data(self, *, retrieval_type=None,
107
102
if observation_id is not None :
108
103
filename = observation_id
109
104
else :
110
- error = "Please set a filename for the output"
105
+ error = "Please set either 'obervation_id' or ' filename' for the output"
111
106
raise ValueError (error )
112
107
113
108
r_filename = params ["filename" ]
@@ -118,6 +113,8 @@ def download_data(self, *, retrieval_type=None,
118
113
else :
119
114
filename += "" .join (suffixes )
120
115
116
+ filename = os .path .join (download_dir , filename )
117
+
121
118
self ._download_file (link , filename , head_safe = True , cache = cache )
122
119
123
120
if verbose :
@@ -126,8 +123,7 @@ def download_data(self, *, retrieval_type=None,
126
123
return filename
127
124
128
125
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 ):
131
127
"""
132
128
Download observation from Herschel.
133
129
This consists of a .tar file containing:
@@ -141,11 +137,11 @@ def get_observation(self, observation_id, instrument_name, *, filename=None,
141
137
142
138
Parameters
143
139
----------
144
- observation_id : string, mandatory
140
+ observation_id : string
145
141
id of the observation to be downloaded
146
142
The identifies of the observation we want to retrieve, 10 digits
147
143
example: 1342195355
148
- instrument_name : string, mandatory
144
+ instrument_name : string
149
145
The instrument name
150
146
values: PACS, SPIRE, HIFI
151
147
filename : string, optional, default None
@@ -161,6 +157,8 @@ def get_observation(self, observation_id, instrument_name, *, filename=None,
161
157
product_level : string, optional
162
158
level to download
163
159
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
164
162
165
163
Returns
166
164
-------
@@ -173,11 +171,9 @@ def get_observation(self, observation_id, instrument_name, *, filename=None,
173
171
'observation_id' : observation_id ,
174
172
'instrument_name' : instrument_name }
175
173
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 ())
178
175
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 ())
181
177
182
178
if verbose :
183
179
log .info (link )
@@ -199,6 +195,8 @@ def get_observation(self, observation_id, instrument_name, *, filename=None,
199
195
200
196
filename += "" .join (suffixes )
201
197
198
+ filename = os .path .join (download_dir , filename )
199
+
202
200
self ._download_file (link , filename , head_safe = True , cache = cache )
203
201
204
202
if verbose :
@@ -207,18 +205,17 @@ def get_observation(self, observation_id, instrument_name, *, filename=None,
207
205
return filename
208
206
209
207
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 ):
212
209
"""
213
210
Download postcard from Herschel
214
211
215
212
Parameters
216
213
----------
217
- observation_id : string, mandatory
214
+ observation_id : string
218
215
id of the observation to be downloaded
219
216
The identifies of the observation we want to retrieve, 10 digits
220
217
example: 1342195355
221
- instrument_name : string, mandatory
218
+ instrument_name : string
222
219
The instrument name
223
220
values: PACS, SPIRE, HIFI
224
221
filename : string, optional, default None
@@ -237,6 +234,8 @@ def get_postcard(self, observation_id, instrument_name, *, filename=None,
237
234
postcard_single : string, optional
238
235
'true' to retrieve one single postcard (main one)
239
236
values: true, false
237
+ download_dir : string, optional
238
+ The directory in which the file will be downloaded
240
239
241
240
Returns
242
241
-------
@@ -249,11 +248,9 @@ def get_postcard(self, observation_id, instrument_name, *, filename=None,
249
248
'observation_id' : observation_id ,
250
249
'instrument_name' : instrument_name }
251
250
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 ())
254
252
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 ())
257
254
258
255
if verbose :
259
256
log .info (link )
@@ -270,6 +267,8 @@ def get_postcard(self, observation_id, instrument_name, *, filename=None,
270
267
271
268
filename += ext
272
269
270
+ filename = os .path .join (download_dir , filename )
271
+
273
272
shutil .move (local_filepath , filename )
274
273
275
274
if verbose :
@@ -284,7 +283,7 @@ def query_hsa_tap(self, query, *, output_file=None,
284
283
285
284
Parameters
286
285
----------
287
- query : string, mandatory
286
+ query : string
288
287
query (adql) to be executed
289
288
output_file : string, optional, default None
290
289
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):
332
331
333
332
Parameters
334
333
----------
335
- table_name : string, mandatory
334
+ table_name : string
336
335
table name of which, columns will be returned
337
336
only_names : bool, optional, default True
338
337
True to load column names only
339
338
verbose : bool, optional, default False
340
-
341
339
flag to display information about the process
340
+
342
341
Returns
343
342
-------
344
343
A list of columns
@@ -360,5 +359,33 @@ def get_columns(self, table_name, *, only_names=True, verbose=False):
360
359
else :
361
360
return columns
362
361
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
+
363
390
364
391
HSA = HSAClass ()
0 commit comments