Skip to content

Commit 01d75d6

Browse files
authored
Merge pull request #2524 from esdc-esac-esa-int/ESA_eHST-improve-query-criteria
Refactored query criteria to use ehst.archive table
2 parents af8d510 + 42eea5f commit 01d75d6

File tree

4 files changed

+36
-45
lines changed

4 files changed

+36
-45
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ hsa
2020
Service fixes and enhancements
2121
------------------------------
2222

23+
esa.hubble
24+
^^^^^^^^^^
25+
26+
- Refactored query_criteria to use ehst.archive table therefore making the query
27+
a lot faster. [#2524]
28+
2329
alma
2430
^^^^
2531

astroquery/esa/hubble/core.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ class ESAHubbleClass(BaseQuery):
3838
metadata_url = conf.METADATA_ACTION
3939
target_url = conf.TARGET_ACTION
4040
TIMEOUT = conf.TIMEOUT
41-
calibration_levels = {0: "AUXILIARY", 1: "RAW", 2: "CALIBRATED",
42-
3: "PRODUCT"}
41+
calibration_levels = {"AUXILIARY": 0, "RAW": 1, "CALIBRATED": 2,
42+
"PRODUCT": 3}
4343
product_types = ["PRODUCT", "SCIENCE_PRODUCT", "POSTCARD"]
4444
copying_string = "Copying file to {0}..."
4545

@@ -475,7 +475,7 @@ def cone_search_criteria(self, radius, target=None,
475475
radius_in_grades = Angle(radius, units.arcmin).deg
476476
else:
477477
radius_in_grades = radius.to(units.deg).value
478-
cone_query = "1=CONTAINS(POINT('ICRS', pos.ra, pos.dec)," \
478+
cone_query = "1=CONTAINS(POINT('ICRS', ra, dec)," \
479479
"CIRCLE('ICRS', {0}, {1}, {2}))". \
480480
format(str(ra), str(dec), str(radius_in_grades))
481481
query = "{}{})".format(crit_query, cone_query)
@@ -629,35 +629,32 @@ def query_criteria(self, calibration_level=None,
629629

630630
parameters = []
631631
if calibration_level is not None:
632-
parameters.append("p.calibration_level LIKE '%{}%'".format(
632+
parameters.append("calibration_level={}".format(
633633
self.__get_calibration_level(calibration_level)))
634634
if data_product_type is not None:
635635
if isinstance(data_product_type, str):
636-
parameters.append("p.data_product_type LIKE '%{}%'".format(
636+
parameters.append("data_product_type LIKE '%{}%'".format(
637637
data_product_type))
638638
else:
639639
raise ValueError("data_product_type must be a string")
640640
if intent is not None:
641641
if isinstance(intent, str):
642-
parameters.append("o.intent LIKE '%{}%'".format(intent))
642+
parameters.append("intent LIKE '%{}%'".format(intent.lower()))
643643
else:
644644
raise ValueError("intent must be a string")
645645
if self.__check_list_strings(obs_collection):
646-
parameters.append("(o.collection LIKE '%{}%')".format(
647-
"%' OR o.collection LIKE '%".join(obs_collection)
646+
parameters.append("(collection LIKE '%{}%')".format(
647+
"%' OR collection LIKE '%".join(obs_collection)
648648
))
649649
if self.__check_list_strings(instrument_name):
650-
parameters.append("(o.instrument_name LIKE '%{}%')".format(
651-
"%' OR o.instrument_name LIKE '%".join(instrument_name)
650+
parameters.append("(instrument_name LIKE '%{}%')".format(
651+
"%' OR instrument_name LIKE '%".join(instrument_name)
652652
))
653653
if self.__check_list_strings(filters):
654-
parameters.append("(o.instrument_configuration LIKE '%{}%')"
655-
.format("%' OR o.instrument_configuration "
654+
parameters.append("(instrument_configuration LIKE '%{}%')"
655+
.format("%' OR instrument_configuration "
656656
"LIKE '%".join(filters)))
657-
query = "select o.*, p.calibration_level, p.data_product_type, " \
658-
"pos.ra, pos.dec from ehst.observation AS o JOIN " \
659-
"ehst.plane as p on o.observation_uuid=p.observation_uuid " \
660-
"JOIN ehst.position as pos on p.plane_id = pos.plane_id"
657+
query = "select * from ehst.archive"
661658
if parameters:
662659
query += " where({})".format(" AND ".join(parameters))
663660
if verbose:
@@ -673,13 +670,13 @@ def query_criteria(self, calibration_level=None,
673670
def __get_calibration_level(self, calibration_level):
674671
condition = ""
675672
if (calibration_level is not None):
676-
if isinstance(calibration_level, str):
677-
condition = calibration_level
678-
elif isinstance(calibration_level, int):
673+
if isinstance(calibration_level, int):
679674
if calibration_level < 4:
680-
condition = self.calibration_levels[calibration_level]
675+
condition = calibration_level
681676
else:
682677
raise KeyError("Calibration level must be between 0 and 3")
678+
elif isinstance(calibration_level, str):
679+
condition = self.calibration_levels[calibration_level]
683680
else:
684681
raise KeyError("Calibration level must be either "
685682
"a string or an integer")

astroquery/esa/hubble/tests/test_esa_hubble.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -275,18 +275,12 @@ def test_query_criteria(self):
275275
'output_file': "output_test_query_by_criteria.vot.gz",
276276
'output_format': "votable",
277277
'verbose': False}
278-
parameters3 = {'query': "select o.*, p.calibration_level, "
279-
"p.data_product_type, pos.ra, pos.dec "
280-
"from ehst.observation "
281-
"AS o JOIN ehst.plane as p on "
282-
"o.observation_uuid=p.observation_uuid "
283-
"JOIN ehst.position as pos on "
284-
"p.plane_id = pos.plane_id where("
285-
"p.calibration_level LIKE '%PRODUCT%' AND "
286-
"p.data_product_type LIKE '%image%' AND "
287-
"o.intent LIKE '%SCIENCE%' AND (o.collection "
288-
"LIKE '%HST%') AND (o.instrument_name LIKE "
289-
"'%WFC3%') AND (o.instrument_configuration "
278+
parameters3 = {'query': "select * from ehst.archive where("
279+
"calibration_level=3 AND "
280+
"data_product_type LIKE '%image%' AND "
281+
"intent LIKE '%science%' AND (collection "
282+
"LIKE '%HST%') AND (instrument_name LIKE "
283+
"'%WFC3%') AND (instrument_configuration "
290284
"LIKE '%F555W%'))",
291285
'output_file': "output_test_query_by_criteria.vot.gz",
292286
'output_format': "votable",
@@ -322,18 +316,12 @@ def test_query_criteria_numeric_calibration(self):
322316
'output_file': "output_test_query_by_criteria.vot.gz",
323317
'output_format': "votable",
324318
'verbose': False}
325-
parameters3 = {'query': "select o.*, p.calibration_level, "
326-
"p.data_product_type, pos.ra, pos.dec"
327-
" from ehst.observation "
328-
"AS o JOIN ehst.plane as p on "
329-
"o.observation_uuid=p.observation_uuid "
330-
"JOIN ehst.position as pos on p.plane_id "
331-
"= pos.plane_id where("
332-
"p.calibration_level LIKE '%RAW%' AND "
333-
"p.data_product_type LIKE '%image%' AND "
334-
"o.intent LIKE '%SCIENCE%' AND (o.collection "
335-
"LIKE '%HST%') AND (o.instrument_name LIKE "
336-
"'%WFC3%') AND (o.instrument_configuration "
319+
parameters3 = {'query': "select * from ehst.archive where("
320+
"calibration_level=1 AND "
321+
"data_product_type LIKE '%image%' AND "
322+
"intent LIKE '%science%' AND (collection "
323+
"LIKE '%HST%') AND (instrument_name LIKE "
324+
"'%WFC3%') AND (instrument_configuration "
337325
"LIKE '%F555W%'))",
338326
'output_file': "output_test_query_by_criteria.vot.gz",
339327
'output_format': "votable",

docs/esa/hubble/hubble.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ This last example will provide the ADQL query based on the criteria defined by t
307307
... filters = ['F555W', 'F606W'],
308308
... get_query = True)
309309
>>> print(result)
310-
select o.*, p.calibration_level, p.data_product_type, pos.ra, pos.dec from ehst.observation AS o JOIN ehst.plane as p on o.observation_uuid=p.observation_uuid JOIN ehst.position as pos on p.plane_id = pos.plane_id where(p.calibration_level LIKE '%PRODUCT%' AND p.data_product_type LIKE '%image%' AND o.intent LIKE '%SCIENCE%' AND (o.collection LIKE '%HLA%') AND (o.instrument_name LIKE '%WFC3%') AND (o.instrument_configuration LIKE '%F555W%' OR o.instrument_configuration LIKE '%F606W%'))
310+
select * from ehst.archive where(calibration_level=3 AND data_product_type LIKE '%image%' AND intent LIKE '%science%' AND (collection LIKE '%HLA%') AND (instrument_name LIKE '%WFC3%') AND (instrument_configuration LIKE '%F555W%' OR instrument_configuration LIKE '%F606W%'))
311311

312312
--------------------------------------
313313
6. Cone searches in the Hubble archive

0 commit comments

Comments
 (0)