Skip to content

Commit b17a2a5

Browse files
jespinosaar“Javier
authored andcommitted
eHST - Remote data tests, units, backward compatibility
1 parent 692ca46 commit b17a2a5

File tree

4 files changed

+193
-25
lines changed

4 files changed

+193
-25
lines changed

astroquery/esa/hubble/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class Conf(_config.ConfigNamespace):
2525
"metadata-action",
2626
"Main url for retriving hst metadata")
2727
TARGET_ACTION = _config.ConfigItem("http://archives.esac.esa.int/"
28-
"ehst-sl-server/servlet/"
29-
"targetresolver-action",
30-
"Main url for solving targets")
28+
"ehst-sl-server/servlet/"
29+
"targetresolver-action",
30+
"Main url for solving targets")
3131
TIMEOUT = 60
3232

3333

astroquery/esa/hubble/core.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"""
1919
from astroquery.utils import commons
2020
from astropy import units
21+
from astropy.coordinates import SkyCoord
22+
from astropy.coordinates import Angle
2123
from astropy.units import Quantity
2224
from astroquery.utils.tap.core import TapPlus
2325
from astroquery.utils.tap.model import modelutils
@@ -58,7 +60,7 @@ def __init__(self, tap_handler=None):
5860
self._tap = tap_handler
5961

6062
def download_product(self, observation_id, calibration_level=None,
61-
product_type=None, filename=None, verbose=False):
63+
filename=None, verbose=False, **kwargs):
6264
"""
6365
Download products from EHST based on their observation ID and the
6466
calibration level or the product type.
@@ -74,16 +76,16 @@ def download_product(self, observation_id, calibration_level=None,
7476
The identifier of the data reduction/processing applied to the
7577
data. By default, the most scientifically relevant level will be
7678
chosen. RAW, CALIBRATED, PRODUCT or AUXILIARY
77-
product_type : string
78-
type of product retrieval, optional
79-
PRODUCT, SCIENCE_PRODUCT or POSTCARD
8079
filename : string
8180
file name to be used to store the artifact, optional, default
8281
None
8382
File name for the observation.
8483
verbose : bool
8584
optional, default 'False'
8685
flag to display information about the process
86+
product_type : string
87+
type of product retrieval, optional
88+
PRODUCT, SCIENCE_PRODUCT or POSTCARD
8789
8890
Returns
8991
-------
@@ -100,7 +102,8 @@ def download_product(self, observation_id, calibration_level=None,
100102
params["CALIBRATION_LEVEL"] = calibration_level
101103
url += "&CALIBRATION_LEVEL=" + calibration_level
102104

103-
if product_type:
105+
if "product_type" in kwargs:
106+
product_type = kwargs["product_type"]
104107
self.__validate_product_type(product_type)
105108
params["RETRIEVAL_TYPE"] = product_type
106109
filename = self._get_product_filename(product_type, filename)
@@ -249,11 +252,11 @@ def cone_search(self, coordinates, radius=0.0, filename=None,
249252
-------
250253
astropy.table.Table with the result of the cone_search
251254
"""
252-
coord = self._getCoordInput(coordinates, "coordinate")
253-
radius_in_grades = float(radius/60) # Converts to degrees
255+
coord = self._getCoordInput(coordinates)
256+
radius_in_grades = Angle(radius, units.arcmin).deg
254257

255-
ra_hours, dec = commons.coord_to_radec(coord)
256-
ra = ra_hours * 15.0 # Converts to degrees
258+
ra = coord.ra.deg
259+
dec = coord.dec.deg
257260

258261
query = "select o.observation_id, "\
259262
"o.start_time, o.end_time, o.start_time_mjd, "\
@@ -377,11 +380,11 @@ def cone_search_criteria(self, target=None, coordinates=None,
377380
except Exception:
378381
raise ValueError('This target cannot be resolved')
379382
else:
380-
coord = self._getCoordInput(coordinates, "coordinate")
381-
ra_hours, dec = commons.coord_to_radec(coord)
382-
ra = ra_hours * 15.0 # Converts to degrees
383+
coord = self._getCoordInput(coordinates)
384+
ra = coord.ra.deg
385+
dec = coord.dec.deg
383386

384-
radius_in_grades = float(radius/60) # Converts to degrees
387+
radius_in_grades = Angle(radius, units.arcmin).deg
385388
cone_query = "1=CONTAINS(POINT('ICRS', pos.ra, pos.dec),"\
386389
"CIRCLE('ICRS', {0}, {1}, {2}))".\
387390
format(str(ra), str(dec), str(radius_in_grades))
@@ -673,14 +676,13 @@ def get_columns(self, table_name, only_names=True, verbose=False):
673676
else:
674677
return columns
675678

676-
def _getCoordInput(self, value, msg):
677-
if not (isinstance(value, str) or isinstance(value,
678-
commons.CoordClasses)):
679-
raise ValueError(str(msg) + ""
679+
def _getCoordInput(self, value):
680+
if not (isinstance(value, str) or
681+
isinstance(value, SkyCoord)):
682+
raise ValueError("Coordinates" +
680683
" must be either a string or astropy.coordinates")
681684
if isinstance(value, str):
682-
coords = commons.parse_coordinates(value)
683-
return coords
685+
return SkyCoord(value)
684686
else:
685687
return value
686688

astroquery/esa/hubble/tests/test_esa_hubble.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ def test_query_target(self):
121121
verbose=parameters['verbose'])
122122

123123
def test_cone_search(self):
124-
coords = coordinates.SkyCoord("00h42m44.51s +41d16m08.45s", frame='icrs')
124+
coords = coordinates.SkyCoord("00h42m44.51s +41d16m08.45s",
125+
frame='icrs')
125126

126127
parameterst = {'query': "select top 10 * from hsc_v2.hubble_sc2",
127128
'output_file': "test2.vot",
@@ -172,8 +173,8 @@ def test_cone_search_coords(self):
172173
parameters['async_job'],
173174
parameters['cache'])
174175
with pytest.raises(ValueError) as err:
175-
ehst._getCoordInput(1234, 'coordinate')
176-
assert "coordinate must be either a string or "\
176+
ehst._getCoordInput(1234)
177+
assert "Coordinates must be either a string or "\
177178
"astropy.coordinates" in err.value.args[0]
178179

179180
def test_query_hst_tap(self):
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2+
"""
3+
4+
@author: Javier Espinosa
5+
6+
7+
European Space Astronomy Centre (ESAC)
8+
European Space Agency (ESA)
9+
10+
Created on 13 Jan. 2021
11+
12+
13+
"""
14+
import tempfile
15+
16+
import unittest
17+
import os
18+
19+
from astropy.tests.helper import remote_data
20+
from requests.models import Response
21+
from astroquery.esa.hubble import ESAHubble
22+
from astroquery.utils.testing_tools import MockResponse
23+
from astropy import coordinates
24+
from unittest.mock import MagicMock
25+
from astropy.table.table import Table
26+
import shutil
27+
import random
28+
from PIL import Image
29+
30+
31+
esa_hubble = ESAHubble()
32+
33+
34+
def data_path(filename):
35+
data_dir = os.path.join(os.path.dirname(__file__), 'data')
36+
return os.path.join(data_dir, filename)
37+
38+
39+
def create_temp_folder():
40+
return tempfile.TemporaryDirectory()
41+
42+
43+
def remove_last_job():
44+
jobs = esa_hubble._tap.list_async_jobs()
45+
if len(jobs) > 0:
46+
esa_hubble._tap.remove_jobs(jobs[-1].jobid)
47+
48+
49+
@remote_data
50+
class TestRemoteData(unittest.TestCase):
51+
52+
temp_file_vot = "/temp.vot"
53+
54+
obs_query = "select top 2050 o.observation_id from ehst.observation o"
55+
56+
top_obs_query = "select top 100 o.observation_id from ehst.observation o"
57+
58+
hst_query = "select top 50 o.observation_id from ehst.observation "\
59+
"o where o.collection='HST'"
60+
61+
top_artifact_query = "select top 50 a.artifact_id, a.observation_id "\
62+
" from ehst.artifact a"
63+
64+
def test_query_hst_tap(self):
65+
result = esa_hubble.query_hst_tap(self.obs_query)
66+
# assert len(result) <= 2000
67+
assert "observation_id" in result.keys()
68+
temp_folder = create_temp_folder()
69+
temp_file = temp_folder.name + self.temp_file_vot
70+
result = esa_hubble.query_hst_tap(query=self.obs_query,
71+
output_file=temp_file,
72+
output_format="votable")
73+
assert os.path.exists(temp_file)
74+
remove_last_job()
75+
76+
def test_query_hst_tap_async(self):
77+
result = esa_hubble.query_hst_tap(self.obs_query, async_job=True)
78+
assert len(result) > 2000
79+
remove_last_job()
80+
result = esa_hubble.query_hst_tap(self.top_obs_query, async_job=True)
81+
assert len(result) > 10
82+
assert "observation_id" in result.keys()
83+
remove_last_job()
84+
85+
def test_download_product(self):
86+
result = esa_hubble.query_hst_tap(self.hst_query)
87+
observation_id = random.choice(result['observation_id'])
88+
temp_folder = create_temp_folder()
89+
temp_file = temp_folder.name + "/" + observation_id + ".tar"
90+
esa_hubble.download_product(observation_id=observation_id,
91+
filename=temp_file)
92+
assert os.path.exists(temp_file)
93+
94+
def test_get_artifact(self):
95+
result = esa_hubble.query_hst_tap(self.top_artifact_query)
96+
assert "artifact_id" in result.keys()
97+
artifact_id = random.choice(result["artifact_id"])
98+
temp_folder = create_temp_folder()
99+
temp_file = temp_folder.name + "/" + artifact_id + ".gz"
100+
esa_hubble.get_artifact(artifact_id, temp_file)
101+
assert os.path.exists(temp_file)
102+
103+
def test_get_postcard(self):
104+
result = esa_hubble.query_hst_tap(self.top_artifact_query +
105+
" where a.archive_class = "
106+
"'preview'")
107+
assert "observation_id" in result.keys()
108+
observation_id = random.choice(result["observation_id"])
109+
temp_folder = create_temp_folder()
110+
temp_file = temp_folder.name + "/" + observation_id + ".jpg"
111+
esa_hubble.get_postcard(observation_id, "RAW", 256, temp_file)
112+
assert os.path.exists(temp_file)
113+
img = Image.open(temp_file)
114+
assert img.format == "JPEG"
115+
116+
def test_query_target(self):
117+
temp_folder = create_temp_folder()
118+
temp_file = temp_folder.name + "/" + "m31_query.xml"
119+
table = esa_hubble.query_target("m31", temp_file)
120+
assert os.path.exists(temp_file)
121+
assert 'OBSERVATION_ID' in table.columns
122+
assert len(table) > 0
123+
124+
def test_query_criteria(self):
125+
temp_folder = create_temp_folder()
126+
temp_file = temp_folder.name + "/output1.vot.gz"
127+
table = esa_hubble.query_criteria(calibration_level=1,
128+
data_product_type="image",
129+
intent="SCIENCE",
130+
async_job=False,
131+
output_file=temp_file,
132+
output_format="votable",
133+
verbose=True,
134+
get_query=False)
135+
assert os.path.exists(temp_file)
136+
assert 'observation_id' in table.columns
137+
assert len(table) > 0
138+
remove_last_job()
139+
140+
def test_cone_search(self):
141+
esa_hubble = ESAHubble()
142+
c = coordinates.SkyCoord("00h42m44.51s +41d16m08.45s", frame='icrs')
143+
temp_folder = create_temp_folder()
144+
temp_file = temp_folder.name + "/cone_search_m31_5.vot"
145+
table = esa_hubble.cone_search(c, 7, temp_file)
146+
assert os.path.exists(temp_file)
147+
assert 'observation_id' in table.columns
148+
assert len(table) > 0
149+
remove_last_job()
150+
151+
def test_cone_search_criteria(self):
152+
esa_hubble = ESAHubble()
153+
c = coordinates.SkyCoord("00h42m44.51s +41d16m08.45s", frame='icrs')
154+
temp_folder = create_temp_folder()
155+
temp_file = temp_folder.name + "/output1.vot.gz"
156+
table = esa_hubble.cone_search_criteria(target='m31', radius=7,
157+
obs_collection=['HST'],
158+
data_product_type='image',
159+
async_job=False,
160+
filename=temp_file,
161+
output_format="votable")
162+
assert os.path.exists(temp_file)
163+
assert 'observation_id' in table.columns
164+
assert len(table) > 0
165+
remove_last_job()

0 commit comments

Comments
 (0)