Skip to content

Commit

Permalink
Merge pull request #2524 from esdc-esac-esa-int/ESA_eHST-improve-quer…
Browse files Browse the repository at this point in the history
…y-criteria

Refactored query criteria to use ehst.archive table
  • Loading branch information
bsipocz authored Sep 13, 2022
2 parents af8d510 + 42eea5f commit 01d75d6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 45 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ hsa
Service fixes and enhancements
------------------------------

esa.hubble
^^^^^^^^^^

- Refactored query_criteria to use ehst.archive table therefore making the query
a lot faster. [#2524]

alma
^^^^

Expand Down
37 changes: 17 additions & 20 deletions astroquery/esa/hubble/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ class ESAHubbleClass(BaseQuery):
metadata_url = conf.METADATA_ACTION
target_url = conf.TARGET_ACTION
TIMEOUT = conf.TIMEOUT
calibration_levels = {0: "AUXILIARY", 1: "RAW", 2: "CALIBRATED",
3: "PRODUCT"}
calibration_levels = {"AUXILIARY": 0, "RAW": 1, "CALIBRATED": 2,
"PRODUCT": 3}
product_types = ["PRODUCT", "SCIENCE_PRODUCT", "POSTCARD"]
copying_string = "Copying file to {0}..."

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

parameters = []
if calibration_level is not None:
parameters.append("p.calibration_level LIKE '%{}%'".format(
parameters.append("calibration_level={}".format(
self.__get_calibration_level(calibration_level)))
if data_product_type is not None:
if isinstance(data_product_type, str):
parameters.append("p.data_product_type LIKE '%{}%'".format(
parameters.append("data_product_type LIKE '%{}%'".format(
data_product_type))
else:
raise ValueError("data_product_type must be a string")
if intent is not None:
if isinstance(intent, str):
parameters.append("o.intent LIKE '%{}%'".format(intent))
parameters.append("intent LIKE '%{}%'".format(intent.lower()))
else:
raise ValueError("intent must be a string")
if self.__check_list_strings(obs_collection):
parameters.append("(o.collection LIKE '%{}%')".format(
"%' OR o.collection LIKE '%".join(obs_collection)
parameters.append("(collection LIKE '%{}%')".format(
"%' OR collection LIKE '%".join(obs_collection)
))
if self.__check_list_strings(instrument_name):
parameters.append("(o.instrument_name LIKE '%{}%')".format(
"%' OR o.instrument_name LIKE '%".join(instrument_name)
parameters.append("(instrument_name LIKE '%{}%')".format(
"%' OR instrument_name LIKE '%".join(instrument_name)
))
if self.__check_list_strings(filters):
parameters.append("(o.instrument_configuration LIKE '%{}%')"
.format("%' OR o.instrument_configuration "
parameters.append("(instrument_configuration LIKE '%{}%')"
.format("%' OR instrument_configuration "
"LIKE '%".join(filters)))
query = "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"
query = "select * from ehst.archive"
if parameters:
query += " where({})".format(" AND ".join(parameters))
if verbose:
Expand All @@ -673,13 +670,13 @@ def query_criteria(self, calibration_level=None,
def __get_calibration_level(self, calibration_level):
condition = ""
if (calibration_level is not None):
if isinstance(calibration_level, str):
condition = calibration_level
elif isinstance(calibration_level, int):
if isinstance(calibration_level, int):
if calibration_level < 4:
condition = self.calibration_levels[calibration_level]
condition = calibration_level
else:
raise KeyError("Calibration level must be between 0 and 3")
elif isinstance(calibration_level, str):
condition = self.calibration_levels[calibration_level]
else:
raise KeyError("Calibration level must be either "
"a string or an integer")
Expand Down
36 changes: 12 additions & 24 deletions astroquery/esa/hubble/tests/test_esa_hubble.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,18 +275,12 @@ def test_query_criteria(self):
'output_file': "output_test_query_by_criteria.vot.gz",
'output_format': "votable",
'verbose': False}
parameters3 = {'query': "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 '%HST%') AND (o.instrument_name LIKE "
"'%WFC3%') AND (o.instrument_configuration "
parameters3 = {'query': "select * from ehst.archive where("
"calibration_level=3 AND "
"data_product_type LIKE '%image%' AND "
"intent LIKE '%science%' AND (collection "
"LIKE '%HST%') AND (instrument_name LIKE "
"'%WFC3%') AND (instrument_configuration "
"LIKE '%F555W%'))",
'output_file': "output_test_query_by_criteria.vot.gz",
'output_format': "votable",
Expand Down Expand Up @@ -322,18 +316,12 @@ def test_query_criteria_numeric_calibration(self):
'output_file': "output_test_query_by_criteria.vot.gz",
'output_format': "votable",
'verbose': False}
parameters3 = {'query': "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 '%RAW%' AND "
"p.data_product_type LIKE '%image%' AND "
"o.intent LIKE '%SCIENCE%' AND (o.collection "
"LIKE '%HST%') AND (o.instrument_name LIKE "
"'%WFC3%') AND (o.instrument_configuration "
parameters3 = {'query': "select * from ehst.archive where("
"calibration_level=1 AND "
"data_product_type LIKE '%image%' AND "
"intent LIKE '%science%' AND (collection "
"LIKE '%HST%') AND (instrument_name LIKE "
"'%WFC3%') AND (instrument_configuration "
"LIKE '%F555W%'))",
'output_file': "output_test_query_by_criteria.vot.gz",
'output_format': "votable",
Expand Down
2 changes: 1 addition & 1 deletion docs/esa/hubble/hubble.rst
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ This last example will provide the ADQL query based on the criteria defined by t
... filters = ['F555W', 'F606W'],
... get_query = True)
>>> print(result)
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%'))
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%'))

--------------------------------------
6. Cone searches in the Hubble archive
Expand Down

0 comments on commit 01d75d6

Please sign in to comment.