diff --git a/CHANGES.rst b/CHANGES.rst index 2953d907f5..c13db98529 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -35,6 +35,8 @@ esa.jwst - get_obs_products method supports product_type parameter as string or list [#2995] +- Add download_files_from_program method to get all products by program id [#3073] + mpc ^^^ diff --git a/astroquery/esa/jwst/__init__.py b/astroquery/esa/jwst/__init__.py index 4d1680ab5b..39dbbb0564 100644 --- a/astroquery/esa/jwst/__init__.py +++ b/astroquery/esa/jwst/__init__.py @@ -45,6 +45,8 @@ class Conf(_config.ConfigNamespace): "Name of Dec parameter " "in table") + JWST_ARCHIVE_TABLE = _config.ConfigItem("jwst.archive", "JWST archive table") + conf = Conf() diff --git a/astroquery/esa/jwst/core.py b/astroquery/esa/jwst/core.py index 45293e2995..b9347e5f4d 100644 --- a/astroquery/esa/jwst/core.py +++ b/astroquery/esa/jwst/core.py @@ -1035,6 +1035,40 @@ def get_obs_products(self, *, observation_id=None, cal_level="ALL", return files + def download_files_from_program(self, proposal_id, *, product_type=None, verbose=False): + """Get JWST products given its proposal ID. + + Parameters + ---------- + proposal_id : int, mandatory + Program or Proposal ID associated to the observations. + product_type : str or list, optional, default None + If the string or at least one element of the list is empty, + the value is replaced by None. + With None, all products will be downloaded. + Possible string values: 'thumbnail', 'preview', 'auxiliary', 'science' or 'info'. + Posible list values: any combination of string values. + verbose : bool, optional, default 'False' + flag to display information about the process + + Returns + ------- + allobs : list + Returns the observationsid included into the proposal_id. + """ + + query = (f"SELECT observationid " + f"FROM {str(conf.JWST_ARCHIVE_TABLE)} " + f"WHERE proposal_id='{str(proposal_id)}'") + if verbose: + print(query) + job = self.__jwsttap.launch_job_async(query=query, verbose=verbose) + allobs = set(JwstClass.get_decoded_string(job.get_results()['observationid'])) + for oid in allobs: + log.info(f"Downloading products for Observation ID: {oid}") + self.get_obs_products(observation_id=oid, product_type=product_type) + return list(allobs) + def __check_file_number(self, output_dir, output_file_name, output_file_full_path, files): num_files_in_dir = len(os.listdir(output_dir)) diff --git a/astroquery/esa/jwst/tests/test_jwsttap.py b/astroquery/esa/jwst/tests/test_jwsttap.py index 15f8605f8a..d627c7b8e2 100644 --- a/astroquery/esa/jwst/tests/test_jwsttap.py +++ b/astroquery/esa/jwst/tests/test_jwsttap.py @@ -684,6 +684,13 @@ def test_get_products_list_error(self): jwst.get_product_list(observation_id=observation_id, product_type='test') assert "product_type must be one of" in err.value.args[0] + def test_download_files_from_program(self): + dummyTapHandler = DummyTapHandler() + jwst = JwstClass(tap_plus_handler=dummyTapHandler, data_handler=dummyTapHandler, show_messages=False) + with pytest.raises(TypeError) as err: + jwst.download_files_from_program() + assert "missing 1 required positional argument: 'proposal_id'" in err.value.args[0] + def test_get_obs_products(self): dummyTapHandler = DummyTapHandler() jwst = JwstClass(tap_plus_handler=dummyTapHandler, data_handler=dummyTapHandler, show_messages=False) diff --git a/docs/esa/jwst/jwst.rst b/docs/esa/jwst/jwst.rst index f0429aa652..e0f5842a79 100644 --- a/docs/esa/jwst/jwst.rst +++ b/docs/esa/jwst/jwst.rst @@ -289,7 +289,7 @@ than get_product_list, it also supports product_type parameter as string or list Here product_type as list: .. doctest-remote-data:: - + >>> from astroquery.esa.jwst import Jwst >>> observation_id = 'jw01122001001_0210r_00001_nrs2' >>> results = Jwst.get_obs_products(observation_id=observation_id, cal_level=2, product_type=['science', 'preview']) @@ -326,6 +326,18 @@ Using the observation ID as input parameter, this function will retrieve the obs 'jw02739001001_02105_00002_nrcblong', 'jw02739001001_02105_00003_nrcalong'] +To query the data products associated with a certain Proposal ID and filtered by product_type. + +.. doctest-remote-data:: + + >>> from astroquery.esa.jwst import Jwst + >>> observation_list = Jwst.download_files_from_program(proposal_id='6651', product_type='preview') # doctest: +IGNORE_OUTPUT + INFO: Query finished. [astroquery.utils.tap.core] + INFO: Downloading products for Observation ID: jw06651001001_05201_00001_nis [astroquery.esa.jwst.core] + INFO: Downloading products for Observation ID: jw06651002001_05201_00001_nis [astroquery.esa.jwst.core] + >>> print(observation_list) # doctest: +IGNORE_OUTPUT + ['jw06651001001_05201_00001_nis', 'jw06651002001_05201_00001_nis'] + 1.5 Getting public tables ~~~~~~~~~~~~~~~~~~~~~~~~~