Skip to content

Commit 9bd2c06

Browse files
snbiancobsipocz
authored andcommitted
get_cloud_uris_query() method
1 parent 26050ed commit 9bd2c06

File tree

4 files changed

+102
-2
lines changed

4 files changed

+102
-2
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ mast
169169

170170
- Fix bug in ``Catalogs.query_criteria()`` to use ``page`` and ``pagesize`` parameters correctly. [#3065]
171171

172+
- Add ``mast.Observations.get_cloud_uris_query`` method so that given a set of query criteria and optional filters,
173+
the user receives a list of cloud data URIs for matching data products. [#3064]
172174

173175

174176
0.4.7 (2024-03-08)

astroquery/mast/observations.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,59 @@ def get_cloud_uri(self, data_product, *, include_bucket=True, full_url=False):
841841
# Query for product URIs
842842
return self._cloud_connection.get_cloud_uri(data_product, include_bucket, full_url)
843843

844+
def get_cloud_uris_query(self, *, pagesize=None, page=None, mrp_only=False, extension=None,
845+
filter_products={}, **criteria):
846+
"""
847+
Given a set of criteria and optional filters, get a list of matching data products and return their
848+
associated cloud data URIs.
849+
850+
Parameters
851+
----------
852+
pagesize : int, optional
853+
Default None. Can be used to override the default pagesize.
854+
E.g. when using a slow internet connection.
855+
page : int, optional
856+
Default None. Can be used to override the default behavior of all results being returned to obtain
857+
one specific page of results.
858+
mrp_only : bool, optional
859+
Default False. When set to True, only "Minimum Recommended Products" will be returned.
860+
extension : string or array, optional
861+
Default None. Option to filter by file extension.
862+
filter_products : dict, optional
863+
Filters to be applied. Valid filters are all products fields listed
864+
`here <https://masttest.stsci.edu/api/v0/_productsfields.html>`__.
865+
The column name as a string is the key. The corresponding value is one
866+
or more acceptable values for that parameter.
867+
Filter behavior is AND between the filters and OR within a filter set.
868+
For example: {"productType": "SCIENCE", "extension"=["fits","jpg"]}
869+
**criteria
870+
Criteria to apply. At least one non-positional criteria must be supplied.
871+
Valid criteria are coordinates, objectname, radius (as in `query_region` and `query_object`),
872+
and all observation fields returned by the ``get_metadata("observations")``.
873+
The Column Name is the keyword, with the argument being one or more acceptable values for that parameter,
874+
except for fields with a float datatype where the argument should be in the form [minVal, maxVal].
875+
For non-float type criteria wildcards maybe used (both * and % are considered wildcards), however
876+
only one wildcarded value can be processed per criterion.
877+
RA and Dec must be given in decimal degrees, and datetimes in MJD.
878+
For example: filters=["FUV","NUV"],proposal_pi="Ost*",t_max=[52264.4586,54452.8914]
879+
880+
Returns
881+
-------
882+
response : list
883+
884+
"""
885+
# Get table of observations based on query criteria
886+
obs = self.query_criteria(pagesize=pagesize, page=page, **criteria)
887+
888+
# Return list of associated data products
889+
prod = self.get_product_list(obs)
890+
891+
# Filter product list
892+
filt = self.filter_products(prod, mrp_only=mrp_only, extension=extension, **filter_products)
893+
894+
# Return list of cloud URIs
895+
return self.get_cloud_uris(filt)
896+
844897
def _remove_duplicate_products(self, data_products):
845898
"""
846899
Removes duplicate data products that have the same dataURI.

astroquery/mast/tests/test_mast_remote.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,27 @@ def test_get_cloud_uris(self):
544544

545545
assert len(uris) > 0, f'Products for OBSID {test_obs_id} were not found in the cloud.'
546546

547+
def test_get_cloud_uris_query(self):
548+
pytest.importorskip("boto3")
549+
550+
# enable access to public AWS S3 bucket
551+
Observations.enable_cloud_dataset()
552+
553+
# get uris with other functions
554+
obs = Observations.query_criteria(target_name=234295610,
555+
provenance_name="SPOC",
556+
sequence_number=[1, 2])
557+
prod = Observations.get_product_list(obs)
558+
filt = Observations.filter_products(prod, calib_level=[2])
559+
s3_uris = Observations.get_cloud_uris(filt)
560+
561+
# get uris with streamlined function
562+
uris = Observations.get_cloud_uris_query(target_name=234295610,
563+
provenance_name="SPOC",
564+
sequence_number=[1, 2],
565+
filter_products={'calib_level': [2]})
566+
assert s3_uris == uris
567+
547568
######################
548569
# CatalogClass tests #
549570
######################

docs/mast/mast_obsquery.rst

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,14 +427,16 @@ MAST until it is disabled with `~astroquery.mast.ObservationsClass.disable_cloud
427427
To directly access a list of cloud URIs for a given dataset, use the
428428
`~astroquery.mast.ObservationsClass.get_cloud_uris`
429429
function (Python will prompt you to enable cloud access if you haven't already).
430+
To return a list of cloud URIs based on query criteria and product filters, use the
431+
`~astroquery.mast.ObservationsClass.get_cloud_uris_query` function.
430432

431433
When cloud access is enabled, the standard download function
432434
`~astroquery.mast.ObservationsClass.download_products` preferentially pulls files from AWS when they
433435
are available. When set to `True`, the ``cloud_only`` parameter in
434436
`~astroquery.mast.ObservationsClass.download_products` skips all data products not available in the cloud.
435437

436438

437-
Getting a list of S3 URIs:
439+
To get a list of S3 URIs, use the following workflow:
438440

439441
.. doctest-skip::
440442

@@ -456,10 +458,32 @@ Getting a list of S3 URIs:
456458
... productSubGroupDescription='DRZ')
457459
>>> s3_uris = Observations.get_cloud_uris(filtered)
458460
>>> print(s3_uris)
459-
['s3://stpubdata/hst/public/jbev/jbeveo010/jbeveo010_drz.fits', 's3://stpubdata/hst/public/jbev/jbeveo010/jbeveo010_drz.fits', 's3://stpubdata/hst/public/jbev/jbevet010/jbevet010_drz.fits', 's3://stpubdata/hst/public/jbev/jbevet010/jbevet010_drz.fits']
461+
['s3://stpubdata/hst/public/jbev/jbeveo010/jbeveo010_drz.fits', 's3://stpubdata/hst/public/jbev/jbevet010/jbevet010_drz.fits']
460462
...
461463
>>> Observations.disable_cloud_dataset()
462464

465+
Alternatively, you can use the streamlined `~astroquery.mast.ObservationsClass.get_cloud_uris_query` function. This approach is recommended
466+
for code brevity. Query criteria are supplied as keyword arguments, and filters are supplied through the ``filter_products`` parameter.
467+
468+
.. doctest-skip::
469+
470+
>>> import os
471+
>>> from astroquery.mast import Observations
472+
...
473+
>>> Observations.enable_cloud_dataset(provider='AWS')
474+
INFO: Using the S3 STScI public dataset [astroquery.mast.core]
475+
...
476+
>>> # Getting the cloud URIs
477+
>>> s3_uris = Observations.get_cloud_uris_query(obs_collection='HST',
478+
filters='F606W',
479+
instrument_name='ACS/WFC',
480+
proposal_id=['12062'],
481+
dataRights='PUBLIC',
482+
filter_products={'productSubGroupDescription': 'DRZ'})
483+
>>> print(s3_uris)
484+
['s3://stpubdata/hst/public/jbev/jbeveo010/jbeveo010_drz.fits', 's3://stpubdata/hst/public/jbev/jbevet010/jbevet010_drz.fits']
485+
...
486+
>>> Observations.disable_cloud_dataset()
463487

464488
Downloading data products from S3:
465489

0 commit comments

Comments
 (0)