Skip to content

Commit

Permalink
Merge pull request #3073 from esdc-esac-esa-int/ESA_jwst-new_astroque…
Browse files Browse the repository at this point in the history
…ry_method_to_get_all_products_by_program_id

Method to get all products by program id (proposal id)
  • Loading branch information
bsipocz authored Jul 22, 2024
2 parents 2a27dff + 2019702 commit 91d160f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^

Expand Down
2 changes: 2 additions & 0 deletions astroquery/esa/jwst/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
34 changes: 34 additions & 0 deletions astroquery/esa/jwst/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
7 changes: 7 additions & 0 deletions astroquery/esa/jwst/tests/test_jwsttap.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion docs/esa/jwst/jwst.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down

0 comments on commit 91d160f

Please sign in to comment.