7
7
8
8
import pytest
9
9
10
- from astropy .table import Table
10
+ from astropy .table import Table , unique
11
11
from astropy .coordinates import SkyCoord
12
12
from astropy .io import fits
13
13
14
14
import astropy .units as u
15
15
16
16
from astroquery .mast .services import _json_to_table
17
17
from astroquery .utils .mocks import MockResponse
18
- from astroquery .exceptions import InvalidQueryError , InputWarning
18
+ from astroquery .exceptions import InvalidQueryError , InputWarning , MaxResultsWarning , NoResultsWarning
19
19
20
20
from astroquery import mast
21
21
22
22
DATA_FILES = {'Mast.Caom.Cone' : 'caom.json' ,
23
23
'Mast.Name.Lookup' : 'resolver.json' ,
24
24
'mission_search_results' : 'mission_results.json' ,
25
25
'mission_columns' : 'mission_columns.json' ,
26
+ 'mission_products' : 'mission_products.json' ,
26
27
'columnsconfig' : 'columnsconfig.json' ,
27
28
'ticcolumns' : 'ticcolumns.json' ,
28
29
'ticcol_filtered' : 'ticcolumns_filtered.json' ,
@@ -72,6 +73,7 @@ def patch_post(request):
72
73
mp .setattr (mast .Observations , '_download_file' , download_mockreturn )
73
74
mp .setattr (mast .Observations , 'download_file' , download_mockreturn )
74
75
mp .setattr (mast .Catalogs , '_download_file' , download_mockreturn )
76
+ mp .setattr (mast .MastMissions , '_download_file' , download_mockreturn )
75
77
mp .setattr (mast .Tesscut , '_download_file' , tesscut_download_mockreturn )
76
78
mp .setattr (mast .Zcut , '_download_file' , zcut_download_mockreturn )
77
79
@@ -108,7 +110,7 @@ def post_mockreturn(self, method="POST", url=None, data=None, timeout=10, **kwar
108
110
return [MockResponse (content )]
109
111
110
112
111
- def service_mockreturn (self , method = "POST" , url = None , data = None , timeout = 10 , use_json = False , ** kwargs ):
113
+ def service_mockreturn (self , method = "POST" , url = None , data = None , params = None , timeout = 10 , use_json = False , ** kwargs ):
112
114
if "panstarrs" in url :
113
115
filename = data_path (DATA_FILES ["panstarrs" ])
114
116
elif "tesscut" in url :
@@ -121,6 +123,8 @@ def service_mockreturn(self, method="POST", url=None, data=None, timeout=10, use
121
123
filename = data_path (DATA_FILES ['z_survey' ])
122
124
else :
123
125
filename = data_path (DATA_FILES ['z_cutout_fit' ])
126
+ elif use_json and 'list_products' in url :
127
+ filename = data_path (DATA_FILES ['mission_products' ])
124
128
elif use_json and data ['radius' ] == 300 :
125
129
filename = data_path (DATA_FILES ["mission_incorrect_results" ])
126
130
elif use_json :
@@ -211,7 +215,9 @@ def test_missions_query_object(patch_post):
211
215
212
216
213
217
def test_missions_query_region (patch_post ):
214
- result = mast .MastMissions .query_region (regionCoords , radius = 0.002 * u .deg )
218
+ result = mast .MastMissions .query_region (regionCoords ,
219
+ radius = 0.002 * u .deg ,
220
+ select_cols = ['sci_pep_id' ])
215
221
assert isinstance (result , Table )
216
222
assert len (result ) > 0
217
223
@@ -242,6 +248,134 @@ def test_missions_query_criteria_async_with_missing_results(patch_post):
242
248
_json_to_table (json .loads (responses ), 'results' )
243
249
244
250
251
+ def test_missions_query_criteria (patch_post ):
252
+ result = mast .MastMissions .query_criteria (
253
+ coordinates = regionCoords ,
254
+ radius = 3 ,
255
+ sci_pep_id = 12556 ,
256
+ sci_obs_type = 'SPECTRUM' ,
257
+ sci_instrume = 'stis,acs,wfc3,cos,fos,foc,nicmos,ghrs' ,
258
+ sci_aec = 'S' ,
259
+ select_cols = ['sci_pep_id' , 'sci_instrume' ]
260
+ )
261
+ assert isinstance (result , Table )
262
+ assert len (result ) > 0
263
+
264
+ # Raise error if non-positional criteria is not supplied
265
+ with pytest .raises (InvalidQueryError ):
266
+ mast .MastMissions .query_criteria (
267
+ coordinates = regionCoords ,
268
+ radius = 3
269
+ )
270
+
271
+ # Raise error if invalid criteria is supplied
272
+ with pytest .raises (InvalidQueryError ):
273
+ mast .MastMissions .query_criteria (
274
+ coordinates = regionCoords ,
275
+ invalid = True
276
+ )
277
+
278
+ # Maximum results warning
279
+ with pytest .warns (MaxResultsWarning ):
280
+ mast .MastMissions .query_criteria (
281
+ coordinates = regionCoords ,
282
+ sci_aec = 'S' ,
283
+ limit = 1
284
+ )
285
+
286
+
287
+ def test_missions_get_product_list_async (patch_post ):
288
+ # String input
289
+ result = mast .MastMissions .get_product_list_async ('Z14Z0104T' )
290
+ assert isinstance (result , MockResponse )
291
+
292
+ # List input
293
+ in_datasets = ['Z14Z0104T' , 'Z14Z0102T' ]
294
+ result = mast .MastMissions .get_product_list_async (in_datasets )
295
+ assert isinstance (result , MockResponse )
296
+
297
+ # Row input
298
+ datasets = mast .MastMissions .query_object ("M101" , radius = ".002 deg" )
299
+ result = mast .MastMissions .get_product_list_async (datasets [:3 ])
300
+ assert isinstance (result , MockResponse )
301
+
302
+ # Table input
303
+ result = mast .MastMissions .get_product_list_async (datasets [0 ])
304
+ assert isinstance (result , MockResponse )
305
+
306
+ # Unsupported data type for datasets
307
+ with pytest .raises (TypeError ) as err_type :
308
+ mast .MastMissions .get_product_list_async (1 )
309
+ assert 'Unsupported data type' in str (err_type .value )
310
+
311
+ # Empty dataset list
312
+ with pytest .raises (InvalidQueryError ) as err_empty :
313
+ mast .MastMissions .get_product_list_async ([' ' ])
314
+ assert 'Dataset list is empty' in str (err_empty .value )
315
+
316
+
317
+ def test_missions_get_product_list (patch_post ):
318
+ # String input
319
+ result = mast .MastMissions .get_product_list ('Z14Z0104T' )
320
+ assert isinstance (result , Table )
321
+
322
+ # List input
323
+ in_datasets = ['Z14Z0104T' , 'Z14Z0102T' ]
324
+ result = mast .MastMissions .get_product_list (in_datasets )
325
+ assert isinstance (result , Table )
326
+
327
+ # Row input
328
+ datasets = mast .MastMissions .query_object ("M101" , radius = ".002 deg" )
329
+ result = mast .MastMissions .get_product_list (datasets [:3 ])
330
+ assert isinstance (result , Table )
331
+
332
+ # Table input
333
+ result = mast .MastMissions .get_product_list (datasets [0 ])
334
+ assert isinstance (result , Table )
335
+
336
+
337
+ def test_missions_get_unique_product_list (patch_post , caplog ):
338
+ unique_products = mast .MastMissions .get_unique_product_list ('Z14Z0104T' )
339
+ assert isinstance (unique_products , Table )
340
+ assert (unique_products == unique (unique_products , keys = 'filename' )).all ()
341
+ # No INFO messages should be logged
342
+ with caplog .at_level ('INFO' , logger = 'astroquery' ):
343
+ assert caplog .text == ''
344
+
345
+
346
+ def test_missions_filter_products (patch_post ):
347
+ # Filter products list by column
348
+ products = mast .MastMissions .get_product_list ('Z14Z0104T' )
349
+ filtered = mast .MastMissions .filter_products (products ,
350
+ category = 'CALIBRATED' )
351
+ assert isinstance (filtered , Table )
352
+ assert all (filtered ['category' ] == 'CALIBRATED' )
353
+
354
+ # Filter by non-existing column
355
+ with pytest .warns (InputWarning ):
356
+ mast .MastMissions .filter_products (products ,
357
+ invalid = True )
358
+
359
+
360
+ def test_missions_download_products (patch_post , tmp_path ):
361
+ # Check string input
362
+ test_dataset_id = 'Z14Z0104T'
363
+ result = mast .MastMissions .download_products (test_dataset_id ,
364
+ download_dir = tmp_path )
365
+ assert isinstance (result , Table )
366
+
367
+ # Check Row input
368
+ prods = mast .MastMissions .get_product_list ('Z14Z0104T' )
369
+ result = mast .MastMissions .download_products (prods [0 ],
370
+ download_dir = tmp_path )
371
+ assert isinstance (result , Table )
372
+
373
+ # Warn about no products
374
+ with pytest .warns (NoResultsWarning ):
375
+ result = mast .MastMissions .download_products (test_dataset_id ,
376
+ extension = 'jpg' ,
377
+ download_dir = tmp_path )
378
+
245
379
###################
246
380
# MastClass tests #
247
381
###################
0 commit comments