Skip to content

Commit 1248c4c

Browse files
committed
post endpoint, documentation
1 parent e9b2edd commit 1248c4c

File tree

4 files changed

+281
-52
lines changed

4 files changed

+281
-52
lines changed

astroquery/mast/missions.py

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class MastMissionsClass(MastQueryWithLogin):
4040

4141
# Static class variables
4242
_search = 'search'
43-
_list_products = 'list_products'
43+
_list_products = 'post_list_products'
4444

4545
# Workaround so that observation_id is returned in ULLYSES queries that do not specify columns
4646
_default_ulysses_cols = ['target_name_ulysses', 'target_classification', 'targ_ra', 'targ_dec', 'host_galaxy_name',
@@ -59,8 +59,8 @@ def __init__(self, *, mission='hst', mast_token=None):
5959

6060
# Service attributes
6161
self.service = self._search # current API service
62-
self.service_dict = {self._search: {'path': 'search'},
63-
self._list_products: {'path': 'list_products'}}
62+
self.service_dict = {self._search: {'path': self._search},
63+
self._list_products: {'path': self._list_products}}
6464

6565
# Search attributes
6666
self._search_option_fields = ['limit', 'offset', 'sort_by', 'search_key', 'sort_desc', 'select_cols',
@@ -190,7 +190,7 @@ def query_region_async(self, coordinates, *, radius=3*u.arcmin, limit=5000, offs
190190

191191
# Dataset ID column should always be returned
192192
if select_cols:
193-
select_cols.append(self.dataset_kwds[self.mission])
193+
select_cols.append(self.dataset_kwds.get(self.mission, None))
194194
elif self.mission == 'ullyses':
195195
select_cols = self._default_ulysses_cols
196196

@@ -267,7 +267,7 @@ def query_criteria_async(self, *, coordinates=None, objectname=None, radius=3*u.
267267

268268
# Dataset ID column should always be returned
269269
if select_cols:
270-
select_cols.append(self.dataset_kwds[self.mission])
270+
select_cols.append(self.dataset_kwds.get(self.mission, None))
271271
elif self.mission == 'ullyses':
272272
select_cols = self._default_ulysses_cols
273273

@@ -349,32 +349,38 @@ def get_product_list_async(self, datasets):
349349

350350
self.service = self._list_products
351351

352+
if isinstance(datasets, Table) or isinstance(datasets, Row):
353+
dataset_kwd = self.get_dataset_kwd()
354+
if not dataset_kwd:
355+
log.warning('Please input dataset IDs as a string, list of strings, or `~astropy.table.Column`.')
356+
return None
357+
352358
# Extract dataset IDs based on input type and mission
353359
if isinstance(datasets, Table):
354-
datasets = datasets[self.dataset_kwds[self.mission]]
360+
datasets = datasets[dataset_kwd].tolist()
355361
elif isinstance(datasets, Row):
356-
datasets = np.array([datasets[self.dataset_kwds[self.mission]]])
357-
elif isinstance(datasets, str) or isinstance(datasets, Column):
358-
datasets = np.array([datasets])
359-
elif isinstance(datasets, list):
360-
datasets = np.array(datasets)
361-
else:
362+
datasets = [datasets[dataset_kwd]]
363+
elif isinstance(datasets, Column):
364+
datasets = datasets.tolist()
365+
elif isinstance(datasets, str):
366+
datasets = [datasets]
367+
elif not isinstance(datasets, list):
362368
raise TypeError('Unsupported data type for `datasets`. Expected string, '
363-
'list of strings, Astropy row, or Astropy Table.')
369+
'list of strings, Astropy Row, Astropy Column, or Astropy Table.')
364370

365371
# Filter out empty strings from IDs
366-
datasets = datasets[np.char.strip(datasets) != '']
367-
if datasets.size == 0:
372+
datasets = [item.strip() for item in datasets if item.strip() != '' and item is not None]
373+
if not len(datasets):
368374
raise InvalidQueryError("Dataset list is empty, no associated products.")
369375

370376
# Send async service request
371-
params = {'dataset_ids': ','.join(datasets)}
377+
params = {'dataset_ids': datasets}
372378
return self._service_api_connection.missions_request_async(self.service, params)
373379

374380
def get_unique_product_list(self, datasets):
375381
"""
376382
Given a dataset ID or list of dataset IDs, returns a list of associated data products with unique
377-
URIs.
383+
filenames.
378384
379385
Parameters
380386
----------
@@ -443,6 +449,7 @@ def filter_products(self, products, *, extension=None, **filters):
443449
def download_file(self, uri, *, local_path=None, cache=True, verbose=True):
444450
"""
445451
Downloads a single file based on the data URI.
452+
446453
Parameters
447454
----------
448455
uri : str
@@ -453,6 +460,7 @@ def download_file(self, uri, *, local_path=None, cache=True, verbose=True):
453460
Default is True. If file is found on disk, it will not be downloaded again.
454461
verbose : bool, optional
455462
Default is True. Whether to show download progress in the console.
463+
456464
Returns
457465
-------
458466
status: str
@@ -550,7 +558,7 @@ def _download_files(self, products, base_dir, *, flat=False, cache=True, verbose
550558
local_path=local_file_path,
551559
cache=cache,
552560
verbose=verbose)
553-
manifest_entries.append([local_file_path, status, msg or '', url or ''])
561+
manifest_entries.append([local_file_path, status, msg, url])
554562

555563
# Return manifest as Astropy Table
556564
manifest = Table(rows=manifest_entries, names=('Local Path', 'Status', 'Message', 'URL'))
@@ -627,7 +635,6 @@ def get_column_list(self):
627635
-------
628636
response : `~astropy.table.Table` that contains columns names, types, and descriptions
629637
"""
630-
631638
if not self.columns.get(self.mission):
632639
try:
633640
# Send server request to get column list for current mission
@@ -659,5 +666,20 @@ def get_column_list(self):
659666

660667
return self.columns[self.mission]
661668

669+
def get_dataset_kwd(self):
670+
"""
671+
Return the Dataset ID keyword for the selected mission. If the keyword is unknown, returns None.
672+
673+
Returns
674+
-------
675+
keyword : str or None
676+
Dataset ID keyword or None if unknown.
677+
"""
678+
if self.mission not in self.dataset_kwds:
679+
log.warning('The mission "%s" does not have a known dataset ID keyword.', self.mission)
680+
return None
681+
682+
return self.dataset_kwds[self.mission]
683+
662684

663685
MastMissions = MastMissionsClass()

astroquery/mast/services.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,9 @@ def missions_request_async(self, service, params):
354354
'Accept': 'application/json'
355355
}
356356

357-
# Determine request method and payload based on service
358-
method = 'POST' if service == 'search' else 'GET'
359-
data, params = (params, None) if method == 'POST' else (None, params)
360-
361357
# make request
362-
response = self._request(method=method,
358+
data, params = (params, None)
359+
response = self._request(method='POST',
363360
url=request_url,
364361
params=params,
365362
data=data,

astroquery/mast/tests/test_mast.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,30 @@ def test_missions_download_no_auth(patch_post, caplog):
406406
assert 'Please check your authentication token' in caplog.text
407407

408408

409+
def test_missions_get_dataset_kwd(patch_post, caplog):
410+
m = mast.MastMissions()
411+
412+
# Default is HST
413+
assert m.mission == 'hst'
414+
assert m.get_dataset_kwd() == 'sci_data_set_name'
415+
416+
# Switch to JWST
417+
m.mission = 'JWST' # case-insensitive
418+
assert m.mission == 'jwst'
419+
assert m.get_dataset_kwd() == 'fileSetName'
420+
421+
# Switch to an HLSP
422+
m.mission = 'Classy'
423+
assert m.mission == 'classy'
424+
assert m.get_dataset_kwd() == 'Target'
425+
426+
# Switch to an unknown
427+
m.mission = 'Unknown'
428+
assert m.mission == 'unknown'
429+
assert m.get_dataset_kwd() is None
430+
with caplog.at_level('WARNING', logger='astroquery'):
431+
assert 'The mission "unknown" does not have a known dataset ID keyword' in caplog.text
432+
409433
###################
410434
# MastClass tests #
411435
###################

0 commit comments

Comments
 (0)