Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_obs_products method supports product_type parameter as string or list #2995

Merged
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ gama

- Change URL to https and thus making the module functional again. [#3056]

esa.jwst
^^^^^^^^

- get_obs_products method supports product_type parameter as string or list [#2995]

mpc
^^^

Expand Down
19 changes: 14 additions & 5 deletions astroquery/esa/jwst/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -968,10 +968,11 @@ def get_obs_products(self, *, observation_id=None, cal_level="ALL",
composite products based on level 2 products). To request upper
levels, please use get_related_observations functions first.
Possible values: 'ALL', 3, 2, 1, -1
product_type : str, optional, default None
List only products of the given type. If None, all products are \
listed. Possible values: 'thumbnail', 'preview', 'auxiliary', \
'science'.
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.
output_file : str, optional
Output file. If no value is provided, a temporary one is created.

Expand All @@ -981,6 +982,8 @@ def get_obs_products(self, *, observation_id=None, cal_level="ALL",
Returns the local path where the product(s) are saved.
"""

if (isinstance(product_type, list) and '' in product_type) or not product_type:
product_type = None
if observation_id is None:
raise ValueError(self.REQUESTED_OBSERVATION_ID)
plane_ids, max_cal_level = self._get_plane_id(observation_id=observation_id)
Expand All @@ -997,10 +1000,16 @@ def get_obs_products(self, *, observation_id=None, cal_level="ALL",
max_cal_level=max_cal_level,
is_url=True)
params_dict['planeid'] = plane_ids

if type(product_type) is list:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if type(product_type) is list:
if isinstance(product_type, list):

tap_product_type = ",".join(str(elem) for elem in product_type)
else:
tap_product_type = product_type

self.__set_additional_parameters(param_dict=params_dict,
cal_level=cal_level,
max_cal_level=max_cal_level,
product_type=product_type)
product_type=tap_product_type)
output_file_full_path, output_dir = self.__set_dirs(output_file=output_file,
observation_id=observation_id)
# Get file name only
Expand Down
31 changes: 31 additions & 0 deletions astroquery/esa/jwst/tests/test_jwsttap.py
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,37 @@ def test_get_obs_products(self):
finally:
shutil.rmtree(output_file_full_path_dir)

# Test product_type paramater with a list
output_file_full_path_dir = os.getcwd() + os.sep + "temp_test_jwsttap_get_obs_products_1"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that all these are inherited from above, so these are generic cleanup comments, not necessary to address them for merging this particular PR:

  • os.path.join does this much nicer than creating the output string like this.
  • the try except below is not really necessary for the tests. If the test fail with OSError, then I presume the developer/maintainer knows how to read the traceback.
  • It may be worth to do parametrization of the product_type instead of copying practically the same code (e.g. look for the usage of pytest.mark.parametrize in e.g the esasky tests)
  • overall, it is better if tests are made smaller, so e.g. here if there is copied code, it may instead land in a separate test as opposed to be added after an already existing one.

try:
os.makedirs(output_file_full_path_dir, exist_ok=True)
except OSError as err:
print(f"Creation of the directory {output_file_full_path_dir} failed: {err.strerror}")
raise err

file = data_path('single_product_retrieval.tar')
output_file_full_path = output_file_full_path_dir + os.sep + os.path.basename(file)
shutil.copy(file, output_file_full_path)
parameters['output_file'] = output_file_full_path

expected_files = []
extracted_file_1 = output_file_full_path_dir + os.sep + 'single_product_retrieval_1.fits'
expected_files.append(extracted_file_1)
product_type_as_list = ['science', 'info']
try:
files_returned = (jwst.get_obs_products(
observation_id=observation_id,
cal_level='ALL',
product_type=product_type_as_list,
output_file=output_file_full_path))
parameters['params_dict']['product_type'] = 'science,info'
dummyTapHandler.check_call('load_data', parameters)
self.__check_extracted_files(files_expected=expected_files,
files_returned=files_returned)
finally:
shutil.rmtree(output_file_full_path_dir)
del parameters['params_dict']['product_type']

# Test single file
output_file_full_path_dir = os.getcwd() + os.sep +\
"temp_test_jwsttap_get_obs_products_2"
Expand Down
14 changes: 12 additions & 2 deletions docs/esa/jwst/jwst.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,24 @@ To download a data product
>>> output_file = Jwst.get_product(artifact_id='6ab73824-6587-4bca-84a8-eb48ac7251be') # doctest: +SKIP
>>> output_file = Jwst.get_product(file_name='jw01166091001_02102_00002_nrca3_cal.fits') # doctest: +SKIP


To download products by observation identifier, it is possible to use the get_obs_products function, with the same parameters
than get_product_list.
than get_product_list, it also supports product_type parameter as string or list. product_type as string:

.. 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') # doctest: +SKIP
>>> results = Jwst.get_obs_products(observation_id=observation_id, cal_level=2, product_type='science')


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'])

A temporary directory is created with the files and a list of the them is provided.

Expand Down
Loading