Skip to content

Commit 49ddf2c

Browse files
Added the requested changes
1 parent 804a2c2 commit 49ddf2c

File tree

2 files changed

+79
-101
lines changed

2 files changed

+79
-101
lines changed

astroquery/esa/hubble/core.py

Lines changed: 55 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,12 @@
1616
1717
1818
"""
19-
from astroquery.utils import commons
2019
from astropy import units
2120
from astropy.coordinates import SkyCoord
2221
from astropy.coordinates import Angle
23-
from astropy.units import Quantity
2422
from astroquery.utils.tap.core import TapPlus
25-
from astroquery.utils.tap.model import modelutils
2623
from astroquery.query import BaseQuery
27-
from astropy.table import Table
28-
from io import BytesIO
2924
import shutil
30-
import os
31-
import json
3225

3326
from . import conf
3427
from astroquery import log
@@ -49,6 +42,7 @@ class ESAHubbleClass(BaseQuery):
4942
3: "PRODUCT"}
5043
product_types = ["PRODUCT", "SCIENCE_PRODUCT", "POSTCARD"]
5144
copying_string = "Copying file to {0}..."
45+
obs_id_none = "Please input an observation id"
5246

5347
def __init__(self, tap_handler=None):
5448
super(ESAHubbleClass, self).__init__()
@@ -125,22 +119,20 @@ def get_member_observations(self, observation_id):
125119
126120
Parameters
127121
----------
128-
observation_id : str, mandatory
122+
observation_id : str
129123
Observation identifier.
130124
131125
Returns
132126
-------
133127
A list of strings with the observation_id of the associated
134128
observations
135129
"""
136-
if observation_id is None:
137-
raise ValueError(self.REQUESTED_OBSERVATION_ID)
138130
observation_type = self.get_observation_type(observation_id)
139131

140132
if 'Composite' in observation_type:
141-
oids = self._select_members(observation_id)
133+
oids = self._select_related_members(observation_id)
142134
elif 'Simple' in observation_type:
143-
oids = self._select_composite(observation_id)
135+
oids = self._select_related_composite(observation_id)
144136
else:
145137
raise ValueError("Invalid observation id")
146138
return oids
@@ -161,13 +153,11 @@ def get_hap_hst_link(self, observation_id):
161153
A list of strings with the observation_id of the associated
162154
observations
163155
"""
164-
if observation_id is None:
165-
raise ValueError(self.REQUESTED_OBSERVATION_ID)
166156
observation_type = self.get_observation_type(observation_id)
167157
if 'Composite' in observation_type:
168158
raise ValueError("HAP-HST link is only available for simple observations. Input observation is Composite.")
169159
elif 'HAP' in observation_type:
170-
oids = self._select_members(observation_id)
160+
oids = self._select_related_members(observation_id)
171161
elif 'HST' in observation_type:
172162
query = f"select observation_id from ehst.observation where obs_type='HAP Simple' and members like '%{observation_id}%'"
173163
job = self.query_hst_tap(query=query)
@@ -192,23 +182,23 @@ def get_observation_type(self, observation_id):
192182
String with the observation type
193183
"""
194184
if observation_id is None:
195-
raise ValueError(self.REQUESTED_OBSERVATION_ID)
185+
raise ValueError(self.obs_id_none)
196186

197187
query = f"select obs_type from ehst.observation where observation_id='{observation_id}'"
198188
job = self.query_hst_tap(query=query)
199189
if any(job["obs_type"]):
200-
obs_type = ESAHubbleClass.get_decoded_string(job["obs_type"][0])
190+
obs_type = self._get_decoded_string(string=job["obs_type"][0])
201191
else:
202192
raise ValueError("Invalid Observation ID")
203193
return obs_type
204194

205-
def _select_members(self, observation_id):
195+
def _select_related_members(self, observation_id):
206196
query = f"select members from ehst.observation where observation_id='{observation_id}'"
207197
job = self.query_hst_tap(query=query)
208-
oids = ESAHubbleClass.get_decoded_string(job["members"][0]).replace("caom:HST/", "").split(" ")
198+
oids = self._get_decoded_string(string=job["members"][0]).replace("caom:HST/", "").split(" ")
209199
return oids
210200

211-
def _select_composite(self, observation_id):
201+
def _select_related_composite(self, observation_id):
212202
query = f"select observation_id from ehst.observation where members like '%{observation_id}%'"
213203
job = self.query_hst_tap(query=query)
214204
oids = job["observation_id"].pformat(show_name=False)
@@ -358,27 +348,27 @@ def cone_search(self, coordinates, radius, filename=None,
358348
radius_in_grades = radius.to(units.deg).value
359349
ra = coord.ra.deg
360350
dec = coord.dec.deg
361-
query = "select o.observation_id, " \
362-
"o.start_time, o.end_time, o.start_time_mjd, " \
363-
"o.end_time_mjd, o.exposure_duration, o.release_date, " \
364-
"o.run_id, o.program_id, o.set_id, o.collection, " \
365-
"o.members_number, o.instrument_configuration, " \
366-
"o.instrument_name, o.obs_type, o.target_moving, " \
367-
"o.target_name, o.target_description, o.proposal_id, " \
368-
"o.pi_name, prop.title, pl.metadata_provenance, " \
369-
"pl.data_product_type, pl.software_version, pos.ra, " \
370-
"pos.dec, pos.gal_lat, pos.gal_lon, pos.ecl_lat, " \
371-
"pos.ecl_lon, pos.fov_size, en.wave_central, " \
372-
"en.wave_bandwidth, en.wave_max, en.wave_min, " \
373-
"en.filter from ehst.observation o join ehst.proposal " \
374-
"prop on o.proposal_id=prop.proposal_id join ehst.plane " \
375-
"pl on pl.observation_id=o.observation_id join " \
376-
"ehst.position pos on pos.plane_id = pl.plane_id join " \
377-
"ehst.energy en on en.plane_id=pl.plane_id where " \
378-
"pl.main_science_plane='true' and 1=CONTAINS(POINT('ICRS', " \
379-
"pos.ra, pos.dec),CIRCLE('ICRS', {0}, {1}, {2})) order " \
380-
"by prop.proposal_id desc".format(str(ra), str(dec),
381-
str(radius_in_grades))
351+
query = ("select o.observation_id, "
352+
"o.start_time, o.end_time, o.start_time_mjd, "
353+
"o.end_time_mjd, o.exposure_duration, o.release_date, "
354+
"o.run_id, o.program_id, o.set_id, o.collection, "
355+
"o.members_number, o.instrument_configuration, "
356+
"o.instrument_name, o.obs_type, o.target_moving, "
357+
"o.target_name, o.target_description, o.proposal_id, "
358+
"o.pi_name, prop.title, pl.metadata_provenance, "
359+
"pl.data_product_type, pl.software_version, pos.ra, "
360+
"pos.dec, pos.gal_lat, pos.gal_lon, pos.ecl_lat, "
361+
"pos.ecl_lon, pos.fov_size, en.wave_central, "
362+
"en.wave_bandwidth, en.wave_max, en.wave_min, "
363+
"en.filter from ehst.observation o join ehst.proposal "
364+
"prop on o.proposal_id=prop.proposal_id join ehst.plane "
365+
"pl on pl.observation_id=o.observation_id join "
366+
"ehst.position pos on pos.plane_id = pl.plane_id join "
367+
"ehst.energy en on en.plane_id=pl.plane_id where "
368+
"pl.main_science_plane='true' and 1=CONTAINS(POINT('ICRS', "
369+
f"pos.ra, pos.dec),CIRCLE('ICRS', {str(ra)}, {str(dec)}, {str(radius_in_grades)})) order "
370+
"by prop.proposal_id desc")
371+
print("type: " + str(type(query)))
382372
if verbose:
383373
log.info(query)
384374
table = self.query_hst_tap(query=query, async_job=async_job,
@@ -475,7 +465,11 @@ def cone_search_criteria(self, radius, target=None,
475465
raise TypeError("Please use only target or coordinates as"
476466
"parameter.")
477467
if target:
478-
ra, dec = self._query_tap_target(target)
468+
coord = self._query_tap_target(target)
469+
ra = coord.get('RA_DEGREES')
470+
dec = coord.get('DEC_DEGREES')
471+
472+
# ra, dec = self._query_tap_target(target)
479473
else:
480474
coord = self._getCoordInput(coordinates)
481475
ra = coord.ra.deg
@@ -510,15 +504,16 @@ def _query_tap_target(self, target):
510504
target_result = target_response.json()['data'][0]
511505
ra = target_result['RA_DEGREES']
512506
dec = target_result['DEC_DEGREES']
513-
return ra, dec
507+
return SkyCoord(ra=ra, dec=dec, unit="deg")
508+
# return ra, dec
514509
except KeyError as e:
515510
raise ValueError("This target cannot be resolved")
516511

517512
def query_metadata(self, output_format='votable', verbose=False):
518513
return
519514

520-
def query_target(self, name, filename=None, async_job=False,
521-
output_format='votable', radius=None):
515+
def query_target(self, name, *, filename=None, output_format='votable',
516+
verbose=False, async_job=False, radius=7):
522517
"""
523518
It executes a query over EHST and download the xml with the results.
524519
@@ -528,28 +523,26 @@ def query_target(self, name, filename=None, async_job=False,
528523
target name to be requested, mandatory
529524
filename : string
530525
file name to be used to store the metadata, optional, default None
531-
async_job : bool, optional, default 'False'
532-
executes the query (job) in asynchronous/synchronous mode (default
533-
synchronous)
534526
output_format : string
535527
optional, default 'votable'
536528
output format of the query
529+
verbose : bool
530+
optional, default 'False'
531+
Flag to display information about the process
532+
async_job : bool, optional, default 'False'
533+
executes the query (job) in asynchronous/synchronous mode (default
534+
synchronous)
537535
radius : int
538-
optional, default None
536+
optional, default 7
539537
radius in arcmin (int, float) or quantity of the cone_search
540538
541539
Returns
542540
-------
543541
Table with the result of the query. It downloads metadata as a file.
544542
"""
545-
546-
if radius is None:
547-
radius = 7
548-
549-
ra, dec = self._query_tap_target(name)
550-
coordinates = SkyCoord(ra=str(ra), dec=str(dec), unit="deg", frame='icrs')
543+
coordinates = self._query_tap_target(name)
551544
table = self.cone_search(coordinates, radius, filename=filename, output_format=output_format,
552-
async_job=async_job)
545+
verbose=verbose, async_job=async_job)
553546

554547
return table
555548

@@ -580,13 +573,12 @@ def query_hst_tap(self, query, async_job=False, output_file=None,
580573
job = self._tap.launch_job_async(query=query,
581574
output_file=output_file,
582575
output_format=output_format,
583-
verbose=False,
584-
dump_to_file=output_file
585-
is not None)
576+
verbose=verbose,
577+
dump_to_file=output_file is not None)
586578
else:
587579
job = self._tap.launch_job(query=query, output_file=output_file,
588580
output_format=output_format,
589-
verbose=False,
581+
verbose=verbose,
590582
dump_to_file=output_file is not None)
591583
table = job.get_results()
592584
return table
@@ -781,13 +773,11 @@ def _getCoordInput(self, value):
781773
else:
782774
return value
783775

784-
@staticmethod
785-
def get_decoded_string(str):
776+
def _get_decoded_string(self, string):
786777
try:
787-
return str.decode('utf-8')
788-
# return str
778+
return string.decode('utf-8')
789779
except (UnicodeDecodeError, AttributeError):
790-
return str
780+
return string
791781

792782

793783
ESAHubble = ESAHubbleClass()

0 commit comments

Comments
 (0)