16
16
17
17
18
18
"""
19
- from astroquery .utils import commons
20
19
from astropy import units
21
20
from astropy .coordinates import SkyCoord
22
21
from astropy .coordinates import Angle
23
- from astropy .units import Quantity
24
22
from astroquery .utils .tap .core import TapPlus
25
- from astroquery .utils .tap .model import modelutils
26
23
from astroquery .query import BaseQuery
27
- from astropy .table import Table
28
- from io import BytesIO
29
24
import shutil
30
- import os
31
- import json
32
25
33
26
from . import conf
34
27
from astroquery import log
@@ -49,6 +42,7 @@ class ESAHubbleClass(BaseQuery):
49
42
3 : "PRODUCT" }
50
43
product_types = ["PRODUCT" , "SCIENCE_PRODUCT" , "POSTCARD" ]
51
44
copying_string = "Copying file to {0}..."
45
+ obs_id_none = "Please input an observation id"
52
46
53
47
def __init__ (self , tap_handler = None ):
54
48
super (ESAHubbleClass , self ).__init__ ()
@@ -125,22 +119,20 @@ def get_member_observations(self, observation_id):
125
119
126
120
Parameters
127
121
----------
128
- observation_id : str, mandatory
122
+ observation_id : str
129
123
Observation identifier.
130
124
131
125
Returns
132
126
-------
133
127
A list of strings with the observation_id of the associated
134
128
observations
135
129
"""
136
- if observation_id is None :
137
- raise ValueError (self .REQUESTED_OBSERVATION_ID )
138
130
observation_type = self .get_observation_type (observation_id )
139
131
140
132
if 'Composite' in observation_type :
141
- oids = self ._select_members (observation_id )
133
+ oids = self ._select_related_members (observation_id )
142
134
elif 'Simple' in observation_type :
143
- oids = self ._select_composite (observation_id )
135
+ oids = self ._select_related_composite (observation_id )
144
136
else :
145
137
raise ValueError ("Invalid observation id" )
146
138
return oids
@@ -161,13 +153,11 @@ def get_hap_hst_link(self, observation_id):
161
153
A list of strings with the observation_id of the associated
162
154
observations
163
155
"""
164
- if observation_id is None :
165
- raise ValueError (self .REQUESTED_OBSERVATION_ID )
166
156
observation_type = self .get_observation_type (observation_id )
167
157
if 'Composite' in observation_type :
168
158
raise ValueError ("HAP-HST link is only available for simple observations. Input observation is Composite." )
169
159
elif 'HAP' in observation_type :
170
- oids = self ._select_members (observation_id )
160
+ oids = self ._select_related_members (observation_id )
171
161
elif 'HST' in observation_type :
172
162
query = f"select observation_id from ehst.observation where obs_type='HAP Simple' and members like '%{ observation_id } %'"
173
163
job = self .query_hst_tap (query = query )
@@ -192,23 +182,23 @@ def get_observation_type(self, observation_id):
192
182
String with the observation type
193
183
"""
194
184
if observation_id is None :
195
- raise ValueError (self .REQUESTED_OBSERVATION_ID )
185
+ raise ValueError (self .obs_id_none )
196
186
197
187
query = f"select obs_type from ehst.observation where observation_id='{ observation_id } '"
198
188
job = self .query_hst_tap (query = query )
199
189
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 ])
201
191
else :
202
192
raise ValueError ("Invalid Observation ID" )
203
193
return obs_type
204
194
205
- def _select_members (self , observation_id ):
195
+ def _select_related_members (self , observation_id ):
206
196
query = f"select members from ehst.observation where observation_id='{ observation_id } '"
207
197
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 (" " )
209
199
return oids
210
200
211
- def _select_composite (self , observation_id ):
201
+ def _select_related_composite (self , observation_id ):
212
202
query = f"select observation_id from ehst.observation where members like '%{ observation_id } %'"
213
203
job = self .query_hst_tap (query = query )
214
204
oids = job ["observation_id" ].pformat (show_name = False )
@@ -358,27 +348,27 @@ def cone_search(self, coordinates, radius, filename=None,
358
348
radius_in_grades = radius .to (units .deg ).value
359
349
ra = coord .ra .deg
360
350
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 ) ))
382
372
if verbose :
383
373
log .info (query )
384
374
table = self .query_hst_tap (query = query , async_job = async_job ,
@@ -475,7 +465,11 @@ def cone_search_criteria(self, radius, target=None,
475
465
raise TypeError ("Please use only target or coordinates as"
476
466
"parameter." )
477
467
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)
479
473
else :
480
474
coord = self ._getCoordInput (coordinates )
481
475
ra = coord .ra .deg
@@ -510,15 +504,16 @@ def _query_tap_target(self, target):
510
504
target_result = target_response .json ()['data' ][0 ]
511
505
ra = target_result ['RA_DEGREES' ]
512
506
dec = target_result ['DEC_DEGREES' ]
513
- return ra , dec
507
+ return SkyCoord (ra = ra , dec = dec , unit = "deg" )
508
+ # return ra, dec
514
509
except KeyError as e :
515
510
raise ValueError ("This target cannot be resolved" )
516
511
517
512
def query_metadata (self , output_format = 'votable' , verbose = False ):
518
513
return
519
514
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 ):
522
517
"""
523
518
It executes a query over EHST and download the xml with the results.
524
519
@@ -528,28 +523,26 @@ def query_target(self, name, filename=None, async_job=False,
528
523
target name to be requested, mandatory
529
524
filename : string
530
525
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)
534
526
output_format : string
535
527
optional, default 'votable'
536
528
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)
537
535
radius : int
538
- optional, default None
536
+ optional, default 7
539
537
radius in arcmin (int, float) or quantity of the cone_search
540
538
541
539
Returns
542
540
-------
543
541
Table with the result of the query. It downloads metadata as a file.
544
542
"""
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 )
551
544
table = self .cone_search (coordinates , radius , filename = filename , output_format = output_format ,
552
- async_job = async_job )
545
+ verbose = verbose , async_job = async_job )
553
546
554
547
return table
555
548
@@ -580,13 +573,12 @@ def query_hst_tap(self, query, async_job=False, output_file=None,
580
573
job = self ._tap .launch_job_async (query = query ,
581
574
output_file = output_file ,
582
575
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 )
586
578
else :
587
579
job = self ._tap .launch_job (query = query , output_file = output_file ,
588
580
output_format = output_format ,
589
- verbose = False ,
581
+ verbose = verbose ,
590
582
dump_to_file = output_file is not None )
591
583
table = job .get_results ()
592
584
return table
@@ -781,13 +773,11 @@ def _getCoordInput(self, value):
781
773
else :
782
774
return value
783
775
784
- @staticmethod
785
- def get_decoded_string (str ):
776
+ def _get_decoded_string (self , string ):
786
777
try :
787
- return str .decode ('utf-8' )
788
- # return str
778
+ return string .decode ('utf-8' )
789
779
except (UnicodeDecodeError , AttributeError ):
790
- return str
780
+ return string
791
781
792
782
793
783
ESAHubble = ESAHubbleClass ()
0 commit comments