forked from astropy/astroquery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim_queries.py
More file actions
326 lines (247 loc) · 9.38 KB
/
sim_queries.py
File metadata and controls
326 lines (247 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# Licensed under a 3-clause BSD style license - see LICENSE.rst
import urllib
import urllib2
from .sim_parameters import *
from .sim_result import *
from .sim_votable import *
__all__ = ['QueryId',
'QueryAroundId',
'QueryCat',
'QueryCoord',
'QueryBibobj',
'QueryMulti',
]
class _Query(object):
def execute(self, votabledef=None, limit=None, pedantic=False):
""" Execute the query, returning a :class:`SimbadResult` object.
Parameters
----------
votabledef: string or :class:`VoTableDef`, optional
Definition object for the output.
limit: int, optional
Limits the number of rows returned. None sets the limit to
SIMBAD's server maximum.
pedantic: bool, optional
The value to pass to the votable parser for the *pedantic*
parameters.
"""
return execute_query(self, votabledef=votabledef, limit=limit,
pedantic=pedantic)
@ValidatedAttribute('wildcard', _ScriptParameterWildcard)
class QueryId(_Query):
""" Query by identifier.
Parameters
----------
identifier: string
The identifier to query for.
wildcard: bool, optional
If True, specifies that `identifier` should be understood as an
expression with wildcards.
"""
__command = 'query id '
def __init__(self, identifier, wildcard=None):
self.identifier = identifier
self.wildcard = wildcard
def __str__(self):
return self.__command + (self.wildcard and 'wildcard ' or '') + \
str(self.identifier) + '\n'
def __repr__(self):
return '{%s(identifier=%s, wildcard=%s)}' % (self.__class__.__name__,
repr(self.identifier), repr(self.wildcard.value))
@ValidatedAttribute('radius', _ScriptParameterRadius)
class QueryAroundId(_Query):
""" Query around identifier.
Parameters
----------
identifier: string
The identifier around wich to query.
radius: string, optional
The value of the cone search radius. The value must be suffixed by
'd' (degrees), 'm' (arcminutes) or 's' (arcseconds).
If set to None the default value will be used.
"""
__command = 'query around '
def __init__(self, identifier, radius=None):
self.identifier = identifier
self.radius = radius
def __str__(self):
s = self.__command + str(self.identifier)
if self.radius:
s += ' radius=%s' % self.radius
return s + '\n'
def __repr__(self):
return '{%s(identifier=%s, radius=%s)}' % (self.__class__.__name__,
repr(self.identifier), repr(self.radius.value))
class QueryCat(_Query):
""" Query for a whole catalog.
Parameters
----------
catalog: string
The catalog identifier, for example 'm', 'ngc'.
"""
__command = 'query cat '
def __init__(self, catalog):
self.catalog = catalog
def __str__(self):
return self.__command + str(self.catalog) + '\n'
def __repr__(self):
return '{%s(catalog=%s)}' % (self.__class__.__name__,
repr(self.catalog))
@ValidatedAttribute('radius', _ScriptParameterRadius)
@ValidatedAttribute('frame', _ScriptParameterFrame)
@ValidatedAttribute('equinox', _ScriptParameterEquinox)
@ValidatedAttribute('epoch', _ScriptParameterEpoch)
class QueryCoord(_Query):
""" Query by coordinates.
Parameters
----------
ra: string
Right ascension, for example '+12 30'.
dec: string
Declination, for example '-20 17'.
radius: string, optional
The value of the cone search radius. The value must be suffixed by
'd' (degrees), 'm' (arcminutes) or 's' (arcseconds).
If set to None the default value will be used.
frame: string, optional
Frame of input coordinates.
equinox: string optional
Equinox of input coordinates.
epoch: string, optional
Epoch of input coordinates.
"""
__command = 'query coo '
def __init__(self, ra, dec, radius=None, frame=None, equinox=None,
epoch=None):
self.ra = ra
self.dec = dec
self.radius = radius
self.frame = frame
self.equinox = equinox
self.epoch = epoch
def __str__(self):
s = self.__command + str(self.ra) + ' ' + str(self.dec)
for item in ('radius', 'frame', 'equinox', 'epoch'):
if getattr(self, item):
s += ' %s=%s' % (item, str(getattr(self, item)))
return s + '\n'
def __repr__(self):
return '{%s(ra=%s, dec=%s, radius=%s, frame=%s, equinox=%s, ' \
'epoch=%s)}' % \
(self.__class__.__name__, repr(self.ra), repr(self.dec),
repr(self.radius), repr(self.frame), repr(self.equinox),
repr(self.epoch))
class QueryBibobj(_Query):
""" Query by bibcode objects. Used to fetch objects contained in the
given article.
Parameters
----------
bibcode: string
The bibcode of the article.
"""
__command = 'query bibobj '
def __init__(self, bibcode):
self.bibcode = bibcode
def __str__(self):
return self.__command + str(self.bibcode) + '\n'
def __repr__(self):
return '{%s(bibcode=%s)}' % (self.__class__.__name__,
repr(self.bibcode))
@ValidatedAttribute('radius', _ScriptParameterRadius)
@ValidatedAttribute('frame', _ScriptParameterFrame)
@ValidatedAttribute('epoch', _ScriptParameterEpoch)
@ValidatedAttribute('equinox', _ScriptParameterEquinox)
class QueryMulti(_Query):
__command_ids = ('radius', 'frame', 'epoch', 'equinox')
__queries = []
def __init__(self, queries=None, radius=None, frame=None, epoch=None,
equinox=None):
""" A type of Query used to aggregate the values of multiple simple
queries into a single result.
Parameters
----------
queries: iterable of Query objects
The list of Query objects to aggregate results for.
radius: string, optional
The value of the cone search radius. The value must be suffixed by
'd' (degrees), 'm' (arcminutes) or 's' (arcseconds).
If set to None the default value will be used.
frame: string, optional
Frame of input coordinates.
equinox: string optional
Equinox of input coordinates.
epoch: string, optional
Epoch of input coordinates.
.. note:: Each of the *radius*, *frame*, *equinox* et *epoch* arguments
acts as a default value for the whole MultiQuery object.
Individual queries may override these.
"""
self.radius = radius
self.frame = frame
self.epoch = epoch
self.equinox = equinox
if queries is not None:
if isinstance(queries, _Query) and \
not isinstance(queries, QueryMulti):
self.queries.append(queries)
elif iter(queries):
for query in queries:
self.queries.append(query)
elif isinstance(queries, QueryMulti):
for query in queries.queries:
self.queries.append(query)
@property
def __commands(self):
""" The list of commands which are not None for this script.
"""
return tuple([x for x in self.__command_ids if getattr(self, x)])
@property
def _header(self):
s = ''
for comm in self.__commands:
s += 'set %s %s\n' % (comm, str(getattr(self, comm)))
return s
@property
def queries(self):
return self.__queries
@property
def __queries_string(self):
s = ''
for query in self.queries:
s += str(query)
return s
def __str__(self):
return self._header + self.__queries_string
def __repr__(self):
return repr(self.queries)
def execute_query(query, votabledef, limit, pedantic):
limit2 = _ScriptParameterRowLimit(limit)
if votabledef is None:
# votabledef is None, use the module level default one
from . import votabledef as vodefault
if isinstance(vodefault, VoTableDef):
votabledef = vodefault
else:
votabledef = VoTableDef(vodefault)
elif not isinstance(votabledef, VoTableDef):
votabledef = VoTableDef(votabledef)
# Create the 'script' string
script = ''
if limit is not None:
script += 'set limit %s\n' % str(limit2)
if isinstance(query, QueryMulti):
script += query._header
script += votabledef.def_str
script += votabledef.open_str
script += str(query)
script += votabledef.close_str
script = urllib.quote(script)
from . import baseurl
req_str = baseurl + script
response = urllib2.urlopen(req_str)
result = b''.join(response.readlines())
result = result.decode('utf-8')
response.close()
if not result:
raise TypeError
return SimbadResult(result, pedantic=pedantic)