Skip to content

Commit 0bab155

Browse files
Refactored query_target and added unit tests
1 parent 8809f00 commit 0bab155

File tree

3 files changed

+50
-61
lines changed

3 files changed

+50
-61
lines changed

astroquery/esa/hubble/core.py

Lines changed: 41 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,13 @@ def _select_composite(self, observation_id):
185185
return oids
186186

187187
def __validate_product_type(self, product_type):
188-
if(product_type not in self.product_types):
188+
if (product_type not in self.product_types):
189189
raise ValueError("This product_type is not allowed")
190190

191191
def _get_product_filename(self, product_type, filename):
192-
if(product_type == "PRODUCT"):
192+
if (product_type == "PRODUCT"):
193193
return filename
194-
elif(product_type == "SCIENCE_PRODUCT"):
194+
elif (product_type == "SCIENCE_PRODUCT"):
195195
log.info("This is a SCIENCE_PRODUCT, the filename will be "
196196
"renamed to " + filename + ".fits.gz")
197197
return filename + ".fits.gz"
@@ -328,25 +328,25 @@ def cone_search(self, coordinates, radius, filename=None,
328328
radius_in_grades = radius.to(units.deg).value
329329
ra = coord.ra.deg
330330
dec = coord.dec.deg
331-
query = "select o.observation_id, "\
332-
"o.start_time, o.end_time, o.start_time_mjd, "\
333-
"o.end_time_mjd, o.exposure_duration, o.release_date, "\
334-
"o.run_id, o.program_id, o.set_id, o.collection, "\
335-
"o.members_number, o.instrument_configuration, "\
336-
"o.instrument_name, o.obs_type, o.target_moving, "\
337-
"o.target_name, o.target_description, o.proposal_id, "\
338-
"o.pi_name, prop.title, pl.metadata_provenance, "\
339-
"pl.data_product_type, pl.software_version, pos.ra, "\
340-
"pos.dec, pos.gal_lat, pos.gal_lon, pos.ecl_lat, "\
341-
"pos.ecl_lon, pos.fov_size, en.wave_central, "\
342-
"en.wave_bandwidth, en.wave_max, en.wave_min, "\
343-
"en.filter from ehst.observation o join ehst.proposal "\
344-
"prop on o.proposal_id=prop.proposal_id join ehst.plane "\
345-
"pl on pl.observation_id=o.observation_id join "\
346-
"ehst.position pos on pos.plane_id = pl.plane_id join "\
347-
"ehst.energy en on en.plane_id=pl.plane_id where "\
348-
"pl.main_science_plane='true' and 1=CONTAINS(POINT('ICRS', "\
349-
"pos.ra, pos.dec),CIRCLE('ICRS', {0}, {1}, {2})) order "\
331+
query = "select o.observation_id, " \
332+
"o.start_time, o.end_time, o.start_time_mjd, " \
333+
"o.end_time_mjd, o.exposure_duration, o.release_date, " \
334+
"o.run_id, o.program_id, o.set_id, o.collection, " \
335+
"o.members_number, o.instrument_configuration, " \
336+
"o.instrument_name, o.obs_type, o.target_moving, " \
337+
"o.target_name, o.target_description, o.proposal_id, " \
338+
"o.pi_name, prop.title, pl.metadata_provenance, " \
339+
"pl.data_product_type, pl.software_version, pos.ra, " \
340+
"pos.dec, pos.gal_lat, pos.gal_lon, pos.ecl_lat, " \
341+
"pos.ecl_lon, pos.fov_size, en.wave_central, " \
342+
"en.wave_bandwidth, en.wave_max, en.wave_min, " \
343+
"en.filter from ehst.observation o join ehst.proposal " \
344+
"prop on o.proposal_id=prop.proposal_id join ehst.plane " \
345+
"pl on pl.observation_id=o.observation_id join " \
346+
"ehst.position pos on pos.plane_id = pl.plane_id join " \
347+
"ehst.energy en on en.plane_id=pl.plane_id where " \
348+
"pl.main_science_plane='true' and 1=CONTAINS(POINT('ICRS', " \
349+
"pos.ra, pos.dec),CIRCLE('ICRS', {0}, {1}, {2})) order " \
350350
"by prop.proposal_id desc".format(str(ra), str(dec),
351351
str(radius_in_grades))
352352
if verbose:
@@ -455,9 +455,9 @@ def cone_search_criteria(self, radius, target=None,
455455
radius_in_grades = Angle(radius, units.arcmin).deg
456456
else:
457457
radius_in_grades = radius.to(units.deg).value
458-
cone_query = "1=CONTAINS(POINT('ICRS', pos.ra, pos.dec),"\
459-
"CIRCLE('ICRS', {0}, {1}, {2}))".\
460-
format(str(ra), str(dec), str(radius_in_grades))
458+
cone_query = "1=CONTAINS(POINT('ICRS', pos.ra, pos.dec)," \
459+
"CIRCLE('ICRS', {0}, {1}, {2}))". \
460+
format(str(ra), str(dec), str(radius_in_grades))
461461
query = "{}{})".format(crit_query, cone_query)
462462
if verbose:
463463
log.info(query)
@@ -487,8 +487,8 @@ def _query_tap_target(self, target):
487487
def query_metadata(self, output_format='votable', verbose=False):
488488
return
489489

490-
def query_target(self, name, filename=None, output_format='votable',
491-
verbose=False):
490+
def query_target(self, name, filename=None, async_job=False,
491+
output_format='votable', radius=None):
492492
"""
493493
It executes a query over EHST and download the xml with the results.
494494
@@ -510,28 +510,18 @@ def query_target(self, name, filename=None, output_format='votable',
510510
Table with the result of the query. It downloads metadata as a file.
511511
"""
512512

513-
params = {"RESOURCE_CLASS": "OBSERVATION",
514-
"USERNAME": "ehst-astroquery",
515-
"SELECTED_FIELDS": "OBSERVATION",
516-
"QUERY": "(TARGET.TARGET_NAME=='" + name + "')",
517-
"RETURN_TYPE": str(output_format)}
518-
response = self._request('GET', self.metadata_url, save=True,
519-
cache=True,
520-
params=params)
513+
if radius is None:
514+
radius = 7
521515

522-
if verbose:
523-
log.info(self.metadata_url + "?RESOURCE_CLASS=OBSERVATION&"
524-
"SELECTED_FIELDS=OBSERVATION&QUERY=(TARGET.TARGET_NAME"
525-
"=='" + name + "')&USERNAME=ehst-astroquery&"
526-
"RETURN_TYPE=" + str(output_format))
527-
log.info(self.copying_string.format(filename))
528-
if filename is None:
529-
filename = "target.xml"
516+
ra, dec = self._query_tap_target(name)
517+
coordinates = SkyCoord(ra=str(ra), dec=str(dec), unit="deg", frame='icrs')
530518

531-
shutil.move(response, filename)
519+
if async_job:
520+
table = self.cone_search(coordinates, radius, filename=filename, output_format=output_format, async_job=True)
521+
else:
522+
table = self.cone_search(coordinates, radius, filename=filename, output_format=output_format)
532523

533-
return modelutils.read_results_table_from_file(filename,
534-
str(output_format))
524+
return table
535525

536526
def query_hst_tap(self, query, async_job=False, output_file=None,
537527
output_format="votable", verbose=False):
@@ -562,7 +552,7 @@ def query_hst_tap(self, query, async_job=False, output_file=None,
562552
output_format=output_format,
563553
verbose=False,
564554
dump_to_file=output_file
565-
is not None)
555+
is not None)
566556
else:
567557
job = self._tap.launch_job(query=query, output_file=output_file,
568558
output_format=output_format,
@@ -647,9 +637,9 @@ def query_criteria(self, calibration_level=None,
647637
parameters.append("(o.instrument_configuration LIKE '%{}%')"
648638
.format("%' OR o.instrument_configuration "
649639
"LIKE '%".join(filters)))
650-
query = "select o.*, p.calibration_level, p.data_product_type, "\
651-
"pos.ra, pos.dec from ehst.observation AS o JOIN "\
652-
"ehst.plane as p on o.observation_uuid=p.observation_uuid "\
640+
query = "select o.*, p.calibration_level, p.data_product_type, " \
641+
"pos.ra, pos.dec from ehst.observation AS o JOIN " \
642+
"ehst.plane as p on o.observation_uuid=p.observation_uuid " \
653643
"JOIN ehst.position as pos on p.plane_id = pos.plane_id"
654644
if parameters:
655645
query += " where({})".format(" AND ".join(parameters))
@@ -665,7 +655,7 @@ def query_criteria(self, calibration_level=None,
665655

666656
def __get_calibration_level(self, calibration_level):
667657
condition = ""
668-
if(calibration_level is not None):
658+
if (calibration_level is not None):
669659
if isinstance(calibration_level, str):
670660
condition = calibration_level
671661
elif isinstance(calibration_level, int):

astroquery/esa/hubble/tests/test_esa_hubble.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,14 @@ def test_get_postcard(self):
132132
filename="X0MC5101T.vot",
133133
verbose=True)
134134

135-
def test_query_target(self):
136-
parameters = {'name': "m31",
137-
'verbose': True}
135+
@patch.object(ESAHubbleClass, 'cone_search')
136+
@patch.object(ESAHubbleClass, '_query_tap_target')
137+
def test_query_target(self, mock_query_tap_target, mock_cone_search):
138+
mock_query_tap_target.return_value = 10, 10
139+
mock_cone_search.return_value = "test"
138140
ehst = ESAHubbleClass(self.get_dummy_tap_handler())
139-
ehst.query_target(name=parameters['name'],
140-
verbose=parameters['verbose'])
141+
table = ehst.query_target(name="test")
142+
assert table == "test"
141143

142144
def test_cone_search(self):
143145
coords = coordinates.SkyCoord("00h42m44.51s +41d16m08.45s",

astroquery/esa/hubble/tests/test_esa_hubble_remote.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,8 @@ def test_hst_simple_to_hap_simple(self):
115115
result = esa_hubble.get_hap_hst_link('jec071i9q')
116116
assert result == ['hst_16316_71_acs_sbc_f150lp_jec071i9']
117117

118-
119-
"""
120118
def test_query_target(self):
121119
temp_file = self.temp_folder.name + "/" + "m31_query.xml"
122-
table = esa_hubble.query_target("m3", temp_file)
120+
table = esa_hubble.query_target(name="m3", filename=temp_file)
123121
assert os.path.exists(temp_file)
124-
assert 'OBSERVATION_ID' in table.columns
125-
"""
122+
assert 'observation_id' in table.columns

0 commit comments

Comments
 (0)