Skip to content

Commit 1bdf695

Browse files
authored
Merge pull request #3096 from snbianco/ASB-21585-unique-products
Get list of unique products
2 parents e8fff0b + 3f75c3b commit 1bdf695

File tree

4 files changed

+83
-3
lines changed

4 files changed

+83
-3
lines changed

CHANGES.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,9 @@ mast
206206
- Support for case-insensitive criteria keyword arguments in ``mast.Observations.query_criteria`` and
207207
``mast.Catalogs.query_criteria``. [#3087]
208208

209+
- Added function ``mast.Observations.get_unique_product_list`` to return the unique data products associated with
210+
given observations. [#3096]
211+
209212
mpc
210213
^^^
211214

astroquery/mast/observations.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ def get_product_list_async(self, observations):
457457
Note that obsid is NOT the same as obs_id, and inputting obs_id values will result in
458458
an error. See column documentation `here <https://masttest.stsci.edu/api/v0/_productsfields.html>`__.
459459
460+
To return unique data products, use ``Observations.get_unique_product_list``.
461+
460462
Parameters
461463
----------
462464
observations : str or `~astropy.table.Row` or list/Table of same
@@ -959,8 +961,32 @@ def _remove_duplicate_products(self, data_products):
959961
number_unique = len(unique_products)
960962
if number_unique < number:
961963
log.info(f"{number - number_unique} of {number} products were duplicates. "
962-
f"Only downloading {number_unique} unique product(s).")
964+
f"Only returning {number_unique} unique product(s).")
965+
966+
return unique_products
967+
968+
def get_unique_product_list(self, observations):
969+
"""
970+
Given a "Product Group Id" (column name obsid), returns a list of associated data products with
971+
unique dataURIs. Note that obsid is NOT the same as obs_id, and inputting obs_id values will result in
972+
an error. See column documentation `here <https://masttest.stsci.edu/api/v0/_productsfields.html>`__.
973+
974+
Parameters
975+
----------
976+
observations : str or `~astropy.table.Row` or list/Table of same
977+
Row/Table of MAST query results (e.g. output from `query_object`)
978+
or single/list of MAST Product Group Id(s) (obsid).
979+
See description `here <https://masttest.stsci.edu/api/v0/_c_a_o_mfields.html>`__.
963980
981+
Returns
982+
-------
983+
unique_products : `~astropy.table.Table`
984+
Table containing products with unique dataURIs.
985+
"""
986+
products = self.get_product_list(observations)
987+
unique_products = self._remove_duplicate_products(products)
988+
if len(unique_products) < len(products):
989+
log.info("To return all products, use `Observations.get_product_list`")
964990
return unique_products
965991

966992

astroquery/mast/tests/test_mast_remote.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from requests.models import Response
99

10-
from astropy.table import Table
10+
from astropy.table import Table, unique
1111
from astropy.coordinates import SkyCoord
1212
from astropy.io import fits
1313
import astropy.units as u
@@ -380,6 +380,32 @@ def test_observations_get_product_list_tess_tica(self, caplog):
380380
assert len(prods) > 0
381381
assert (np.char.find(prods['obs_id'], '429031146') != -1).all()
382382

383+
def test_observations_get_unique_product_list(self, caplog):
384+
# Check that no rows are filtered out when all products are unique
385+
obsids = ['24832668']
386+
products = Observations.get_product_list(obsids)
387+
unique_products = Observations.get_unique_product_list(obsids)
388+
389+
# Should have the same length
390+
assert len(products) == len(unique_products)
391+
# No INFO messages should be logged
392+
with caplog.at_level('INFO', logger='astroquery'):
393+
assert caplog.text == ''
394+
395+
# Check that rows are filtered out when products are not unique
396+
obsids.append('26421364')
397+
products = Observations.get_product_list(obsids)
398+
unique_products = Observations.get_unique_product_list(obsids)
399+
400+
# Unique product list should have fewer rows
401+
assert len(products) > len(unique_products)
402+
# Rows should be unique based on dataURI
403+
assert (unique_products == unique(unique_products, keys='dataURI')).all()
404+
# Check that INFO messages were logged
405+
with caplog.at_level('INFO', logger='astroquery'):
406+
assert 'products were duplicates' in caplog.text
407+
assert 'To return all products' in caplog.text
408+
383409
def test_observations_filter_products(self):
384410
observations = Observations.query_object("M8", radius=".04 deg")
385411
obsLoc = np.where(observations["obs_id"] == 'ktwo200071160-c92_lc')

docs/mast/mast_obsquery.rst

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,31 @@ Using "obs_id" instead of "obsid" from the previous example will result in the f
291291
...
292292
RemoteServiceError: Error converting data type varchar to bigint.
293293

294+
To return only unique data products for an observation, use `~astroquery.mast.ObservationsClass.get_unique_product_list`.
295+
296+
.. doctest-remote-data::
297+
>>> obs = Observations.query_criteria(obs_collection='HST',
298+
... filters='F606W',
299+
... instrument_name='ACS/WFC',
300+
... proposal_id=['12062'],
301+
... dataRights='PUBLIC')
302+
>>> unique_products = Observations.get_unique_product_list(obs)
303+
INFO: 180 of 370 products were duplicates. Only returning 190 unique product(s). [astroquery.mast.observations]
304+
INFO: To return all products, use `Observations.get_product_list` [astroquery.mast.observations]
305+
>>> print(unique_products[:10]['dataURI'])
306+
dataURI
307+
-------------------------------------------------------------------
308+
mast:HST/product/hst_12062_eo_acs_wfc_f606w_jbeveo_drc.fits
309+
mast:HST/product/hst_12062_eo_acs_wfc_f606w_jbeveo_drc.jpg
310+
mast:HST/product/hst_12062_eo_acs_wfc_f606w_jbeveo_point-cat.ecsv
311+
mast:HST/product/hst_12062_eo_acs_wfc_f606w_jbeveo_segment-cat.ecsv
312+
mast:HST/product/hst_12062_eo_acs_wfc_f606w_jbeveo_trl.txt
313+
mast:HST/product/hst_12062_eo_acs_wfc_f606w_jbeveoes_drc.fits
314+
mast:HST/product/hst_12062_eo_acs_wfc_f606w_jbeveoes_drc.jpg
315+
mast:HST/product/hst_12062_eo_acs_wfc_f606w_jbeveoes_flc.fits
316+
mast:HST/product/hst_12062_eo_acs_wfc_f606w_jbeveoes_hlet.fits
317+
mast:HST/product/hst_12062_eo_acs_wfc_f606w_jbeveoes_trl.txt
318+
294319
Filtering
295320
---------
296321

@@ -481,7 +506,7 @@ This approach is recommended for code brevity. Query criteria are supplied as ke
481506
... proposal_id=['12062'],
482507
... dataRights='PUBLIC',
483508
... filter_products={'productSubGroupDescription': 'DRZ'})
484-
INFO: 2 of 4 products were duplicates. Only downloading 2 unique product(s). [astroquery.mast.observations]
509+
INFO: 2 of 4 products were duplicates. Only returning 2 unique product(s). [astroquery.mast.observations]
485510
>>> print(s3_uris)
486511
['s3://stpubdata/hst/public/jbev/jbeveo010/jbeveo010_drz.fits', 's3://stpubdata/hst/public/jbev/jbevet010/jbevet010_drz.fits']
487512
>>> Observations.disable_cloud_dataset()

0 commit comments

Comments
 (0)