Skip to content

Commit 68407bd

Browse files
committed
EHSTPCR-1113: review checks included
1 parent 1bc13cd commit 68407bd

File tree

5 files changed

+58
-40
lines changed

5 files changed

+58
-40
lines changed

CHANGES.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ esa.hubble
3131

3232
- Update to TAP url to query data and download files, aligned with the new eHST Science Archive. [#2567][#2597]
3333
- Status and maintenance messages from eHST TAP when the module is instantiated. get_status_messages method to retrieve them. [#2597]
34-
- New methods to download single files and download FITS associated to an observation. [#2797]
34+
- New methods to download single files ``download_file`` and download FITS associated to an observation ``download_fits_files``. [#2797]
3535
- New function to retrieve all the files associated to an observation. [#2797]
3636

3737
solarsystem.neodys
@@ -66,7 +66,7 @@ esa.hubble
6666
a lot faster. [#2524]
6767
- Method query_hst_tap has been deprecated and is replaced with query_tap, with the same arguments. [#2597]
6868
- Product types in download_product method have been modified to: PRODUCT, SCIENCE_PRODUCT or POSTCARD. [#2597]
69-
- query_criteria method now allows user to filter by Proposal ID. [#2797]
69+
- Added `proposal` keyword argument to several methods now allows to filter by Proposal ID. [#2797]
7070

7171
alma
7272
^^^^

astroquery/esa/hubble/core.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@
2828
__all__ = ['ESAHubble', 'ESAHubbleClass']
2929

3030

31+
def check_rename_to_gz(filename):
32+
rename = False
33+
if os.path.exists(filename):
34+
with open(filename, 'rb') as test_f:
35+
if test_f.read(2) == b'\x1f\x8b' and not filename.endswith('.fits.gz'):
36+
rename = True
37+
38+
if rename:
39+
output = os.path.splitext(filename)[0] + '.fits.gz'
40+
os.rename(filename, output)
41+
return output
42+
else:
43+
return filename
44+
45+
3146
class ESAHubbleClass(BaseQuery):
3247
"""
3348
Class to init ESA Hubble Module and communicate with eHST TAP
@@ -108,21 +123,7 @@ def download_product(self, observation_id, *, calibration_level=None,
108123
filename = self._get_product_filename(product_type, filename)
109124
self._tap.load_data(params_dict=params, output_file=filename, verbose=verbose)
110125

111-
return self.check_rename_to_gz(filename=filename)
112-
113-
def check_rename_to_gz(self, filename):
114-
rename = False
115-
if os.path.exists(filename):
116-
with open(filename, 'rb') as test_f:
117-
if test_f.read(2) == b'\x1f\x8b' and not filename.endswith('.fits.gz'):
118-
rename = True
119-
120-
if rename:
121-
output = os.path.splitext(filename)[0] + '.fits.gz'
122-
os.rename(filename, output)
123-
return output
124-
else:
125-
return filename
126+
return check_rename_to_gz(filename=filename)
126127

127128
def __set_product_type(self, product_type):
128129
if product_type:
@@ -311,7 +312,7 @@ def download_fits_files(self, observation_id, *, verbose=False):
311312

312313
def download_file(self, file, *, filename=None, verbose=False):
313314
"""
314-
Download a file from EHST based on its filename. Similar to get_a
315+
Download a file from eHST based on its filename.
315316
316317
Parameters
317318
----------
@@ -335,7 +336,7 @@ def download_file(self, file, *, filename=None, verbose=False):
335336

336337
self._tap.load_data(params_dict=params, output_file=filename, verbose=verbose)
337338

338-
return self.check_rename_to_gz(filename=filename)
339+
return check_rename_to_gz(filename=filename)
339340

340341
def get_postcard(self, observation_id, *, calibration_level="RAW",
341342
resolution=256, filename=None, verbose=False):

astroquery/esa/hubble/tests/test_esa_hubble.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
from requests.models import Response
2424

2525
from astroquery.esa.hubble import ESAHubbleClass
26+
from astroquery.esa.hubble.core import check_rename_to_gz
2627
from astroquery.esa.hubble.tests.dummy_tap_handler import DummyHubbleTapHandler
2728
from astropy.utils.exceptions import AstropyDeprecationWarning
2829

@@ -270,7 +271,7 @@ def test_download_fits(self, mock_associated_files):
270271
def test_is_not_gz(self, tmp_path):
271272
target_file = data_path('cone_search.vot')
272273
ehst = ESAHubbleClass(tap_handler=self.get_dummy_tap_handler(), show_messages=False)
273-
assert ehst.check_rename_to_gz(target_file) == target_file
274+
assert check_rename_to_gz(target_file) == target_file
274275

275276
def test_is_gz(self, tmp_path):
276277
ehst = ESAHubbleClass(tap_handler=self.get_dummy_tap_handler(), show_messages=False)
@@ -281,7 +282,7 @@ def test_is_gz(self, tmp_path):
281282
f.write(b'')
282283
# with open(test_file, 'rb') as f_in, gzip.open(target_file, 'wb') as f_out:
283284
# f_out.writelines(f_in)
284-
assert ehst.check_rename_to_gz(target_file) == target_file + '.fits.gz'
285+
assert check_rename_to_gz(target_file) == target_file + '.fits.gz'
285286

286287
def test_get_columns(self):
287288
parameters = {'table_name': "table",

astroquery/esa/hubble/tests/test_esa_hubble_remote.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def test_query_tap_async(self):
5959
def test_download_product(self):
6060
result = esa_hubble.query_tap(query=self.hst_query)
6161
observation_id = np.random.choice((result['observation_id']))
62-
temp_file = self.temp_folder.name + "/" + observation_id
62+
temp_file = os.path.join(self.temp_folder.name, observation_id)
6363
esa_hubble.download_product(observation_id=observation_id,
6464
filename=temp_file)
6565
possible_values = [os.path.exists(temp_file + '.jpg'),
@@ -71,7 +71,7 @@ def test_get_artifact(self):
7171
result = esa_hubble.query_tap(query=self.top_artifact_query)
7272
assert "artifact_id" in result.keys()
7373
artifact_id = np.random.choice(result["artifact_id"])
74-
temp_file = self.temp_folder.name + "/" + artifact_id
74+
temp_file = os.path.join(self.temp_folder.name, artifact_id)
7575
esa_hubble.get_artifact(artifact_id=artifact_id, filename=temp_file)
7676
possible_values = [os.path.exists(temp_file),
7777
os.path.exists(temp_file + '.zip'),
@@ -81,7 +81,7 @@ def test_get_artifact(self):
8181
def test_cone_search(self):
8282
esa_hubble = ESAHubble()
8383
c = coordinates.SkyCoord("00h42m44.51s +41d16m08.45s", frame='icrs')
84-
compressed_temp_file = self.temp_folder.name + "/cone_search_m31_5.vot.gz"
84+
compressed_temp_file = os.path.join(self.temp_folder.name, "cone_search_m31_5.vot.gz")
8585
# open & extracting the file
8686
table = esa_hubble.cone_search(coordinates=c, radius=7, filename=compressed_temp_file, verbose=True)
8787
assert 'observation_id' in table.columns
@@ -120,6 +120,6 @@ def test_hst_simple_to_hap_simple(self):
120120
assert result == ['hst_16316_71_acs_sbc_f150lp_jec071i9']
121121

122122
def test_query_target(self):
123-
compressed_temp_file = self.temp_folder.name + "/" + "m31_query.xml.gz"
123+
compressed_temp_file = os.path.join(self.temp_folder.name, "m31_query.xml.gz")
124124
table = esa_hubble.query_target(name="m3", filename=compressed_temp_file)
125125
assert 'observation_id' in table.columns

docs/esa/hubble/hubble.rst

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ in the EHST are synchronised with the MAST services for HST reprocessed
1919
public data and corresponding metadata. Therefore, excluding proprietary
2020
data, all HST data in the EHST are identical to those in MAST.
2121

22-
========
23-
Examples
24-
========
25-
2622
It is highly recommended checking the status of eHST TAP before executing this module. To do this:
2723

2824
.. doctest-remote-data::
@@ -34,6 +30,19 @@ It is highly recommended checking the status of eHST TAP before executing this m
3430
This method will retrieve the same warning messages shown in eHST Science Archive with information about
3531
service degradation.
3632

33+
========
34+
Examples
35+
========
36+
37+
.. note::
38+
The recommended steps to work with eHST Astroquery module are described below:
39+
#. Retrieve the desired observations, fulfilling the user requirements, using one of the following methods: ``query_target``, ``query_criteria``, ``cone_search`` or ``cone_search_criteria``. In the results, the user will allways find a column named 'observation_id' that will be used as a reference.
40+
#. If all the products associated to an observation are required, then use ``download_product``.
41+
#. If only FITS files associated to an observation are required, then use ``download_fits_files``.
42+
#. It is possible to retrieve the name of the files associated to an observation using ``get_associated_files``, together with their calibration level, size and type.
43+
#. Users can filter the previous list to get the specific files to download and use them in ``download_file`` function.
44+
#. Use your algorithms and code to process the data.
45+
3746
----------------------------------------------
3847
1. Querying target names in the Hubble archive
3948
----------------------------------------------
@@ -374,10 +383,12 @@ After retrieving the metadata, the user can filter the result table and get the
374383
The most important column is 'observation_id' and it is possible to use it to retrieve all the
375384
associated files.
376385

377-
In eHST is it possible to download products based on their observation ID (mandatory) and
378-
a required calibration_level (RAW, CALIBRATED, PRODUCT or AUXILIARY) and/or product type (SCIENCE, PREVIEW, THUMBNAIL or AUXILIARY).
386+
.. note::
387+
In eHST is it possible to download products based on their observation ID (mandatory) and
388+
a required calibration_level (RAW, CALIBRATED, PRODUCT or AUXILIARY) and/or product type (SCIENCE, PREVIEW, THUMBNAIL or AUXILIARY).
379389

380-
Deprecation Warning: product types PRODUCT, SCIENCE_PRODUCT or POSTCARD are no longer supported. Please modify your scripts accordingly.
390+
.. warning::
391+
Deprecation Warning: product types PRODUCT, SCIENCE_PRODUCT or POSTCARD are no longer supported. Please modify your scripts accordingly.
381392

382393
For instance, next commands will download all files for the raw calibration level
383394
of the observation 'j6fl25s4q' and it will store them in a file called
@@ -388,7 +399,8 @@ of the observation 'j6fl25s4q' and it will store them in a file called
388399
>>> from astroquery.esa.hubble import ESAHubble
389400
>>> esahubble = ESAHubble()
390401
>>> esahubble.download_product(observation_id="j6fl25s4q", calibration_level="RAW",
391-
... filename="raw_data_for_j6fl25s4q") # doctest: +IGNORE_OUTPUT
402+
... filename="raw_data_for_j6fl25s4q")
403+
'raw_data_for_j6fl25s4q.zip'
392404

393405
This second example will download the science files associated to the observation 'j6fl25s4q' and it will store them in a file called
394406
'science_data_for_j6fl25s4q.zip', modifying the filename provided to ensure that the extension of the file is correct.
@@ -398,17 +410,20 @@ This second example will download the science files associated to the observatio
398410
>>> from astroquery.esa.hubble import ESAHubble
399411
>>> esahubble = ESAHubble()
400412
>>> esahubble.download_product(observation_id="j6fl25s4q", product_type="SCIENCE",
401-
... filename="science_data_for_j6fl25s4q") # doctest: +IGNORE_OUTPUT
413+
... filename="science_data_for_j6fl25s4q")
414+
'science_data_for_j6fl25s4q.zip'
402415

403416
This third case will download the science files associated to the observation 'j6fl25s4q' in raw calibration level and it will store them in a file called
404-
'science_raw_data_for_j6fl25s4q.fits.gz', modifying the filename provided to ensure that the extension of the file is correct.
417+
'science_raw_data_for_j6fl25s4q.fits.gz', modifying the filename provided to ensure that the extension of the file is correct. There is only
418+
one file fulfilling these conditions and it is a FITS file, so the extension is adapted to the contents of the request.
405419

406420
.. doctest-remote-data::
407421

408422
>>> from astroquery.esa.hubble import ESAHubble
409423
>>> esahubble = ESAHubble()
410424
>>> esahubble.download_product(observation_id="j6fl25s4q", calibration_level="RAW",
411-
... filename="science_raw_data_for_j6fl25s4q", product_type="SCIENCE") # doctest: +IGNORE_OUTPUT
425+
... filename="science_raw_data_for_j6fl25s4q", product_type="SCIENCE")
426+
'science_raw_data_for_j6fl25s4q.fits.gz'
412427

413428
If the user wants to filter the files to be downloaded, this module provides additional mechanisms.
414429

@@ -448,7 +463,7 @@ In case the user is only interested in FITS files, this module contains a specif
448463
.. doctest-remote-data::
449464
>>> from astroquery.esa.hubble import ESAHubble
450465
>>> esahubble = ESAHubble()
451-
>>> esahubble.download_fits_files(observation_id='w0ji0v01t')
466+
>>> esahubble.download_fits_files(observation_id='w0ji0v01t') # doctest: +IGNORE_OUTPUT
452467

453468
---------------------------
454469
6. Getting Hubble postcards
@@ -458,7 +473,8 @@ In case the user is only interested in FITS files, this module contains a specif
458473

459474
>>> from astroquery.esa.hubble import ESAHubble
460475
>>> esahubble = ESAHubble()
461-
>>> esahubble.get_postcard(observation_id="j6fl25s4q", calibration_level="RAW", resolution=256, filename="raw_postcard_for_j6fl25s4q.jpg") # doctest: +IGNORE_OUTPUT
476+
>>> esahubble.get_postcard(observation_id="j6fl25s4q", calibration_level="RAW", resolution=256, filename="raw_postcard_for_j6fl25s4q.jpg")
477+
'raw_postcard_for_j6fl25s4q.jpg'
462478

463479
This will download the postcard for the observation 'J8VP03010' with low
464480
resolution (256) and it will stored in a jpg called
@@ -509,7 +525,7 @@ method returns the simple observations that make it up.
509525
>>> from astroquery.esa.hubble import ESAHubble
510526
>>> esahubble = ESAHubble()
511527
>>> result = esahubble.get_member_observations(observation_id="jdrz0c010")
512-
>>> print(result)
528+
>>> result
513529
['jdrz0cjxq', 'jdrz0cjyq']
514530

515531

@@ -525,7 +541,7 @@ returns the corresponding HAP or HST observation
525541
>>> from astroquery.esa.hubble import ESAHubble
526542
>>> esahubble = ESAHubble()
527543
>>> result = esahubble.get_hap_hst_link(observation_id="hst_16316_71_acs_sbc_f150lp_jec071i9")
528-
>>> print(result)
544+
>>> result
529545
['jec071i9q']
530546

531547

0 commit comments

Comments
 (0)