-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlookup.py
156 lines (136 loc) · 5.14 KB
/
lookup.py
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
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 30 16:24:10 2015
@author: mints
"""
import cherrypy
from cherrypy import _cperror
from cherrypy import log as cherrylog
from cherrypy.lib.reprconf import Config
from astropy.coordinates import SkyCoord
from astropy import units as u
import pickle
from importlib import import_module
import traceback
lookups = []
for provider in ['ObsLog','Q3CTap', 'BoxTap', 'Vizier', 'VSA', 'WSA', #'SSA',
'GCPD', 'DASCH', 'OGLE',
'JPlus', 'ESO', 'ChinaVO', 'STSCI', 'CRTS2',
#'DECam', 'NOAO',
'CasJobs'
]:
try:
print('Importing %s class' % provider)
print('providers.%s' % provider.lower(), '%sLookup' % provider)
class_ = getattr(import_module('providers.%s' % provider.lower()),
'%sLookup' % provider)
lookups.append(class_())
except Exception as err:
print('Import failed for %s' % provider)
traceback.print_tb(err.__traceback__)
pass
def parse_arbitraty_coordinates(text):
"""
Convert text with coordinates into ra/dec in degrees.
Supports decimal and HMS/DMS inputs.
"""
if '+' in text:
ra, dec = text.split('+')
elif '-' in text:
ra, dec = text.split('-')
dec = '-%s' % dec
else:
ra, dec = text.split(' ')
if ('.' in ra and ' ' not in ra) or ('.' in dec and ' ' not in dec):
return float(ra), float(dec)
elif 'h' in ra:
coord = SkyCoord(ra, dec)
return coord.ra.degree, coord.dec.degree
else:
coord = SkyCoord(text, unit=(u.hourangle, u.deg))
return coord.ra.degree, coord.dec.degree
def handle_error():
cherrypy.response.status = 500
cherrypy.response.body = [
"""<html><body><pre>Sorry, an error occured %s</pre><br>
</body></html>""" % _cperror.format_exc()
]
cherrypy.config.update({'request.error_response': handle_error})
class LookupServer(object):
def __init__(self):
self.mocs = pickle.load(open('all_mocs.pickle', 'rb'))
self.catalogs = {}
self.config = Config('lookup.conf')
for look in lookups:
look.force_config_reload()
for catalog in look.CATALOGS:
print(catalog, look)
self.catalogs[catalog] = look
def start(self):
cherrypy.config.update(self.config)
cherrypy.tree.mount(self, '/', config=self.config)
cherrypy.tree.mount(self, '/lookup', config=self.config)
cherrypy.engine.start()
cherrypy.engine.block()
@cherrypy.expose
def index(self):
return open('index.html', 'r')
@cherrypy.expose
def reload_config(self):
for look in lookups:
look.force_config_reload()
return """<html>Config reloaded</html>"""
@cherrypy.expose
def search(self, coordinates, radius):
if float(radius) > 30:
return """<html><body>
<h2>Error: radius (%s) is too large</h2><br>
<div>Maximum allowed radius is 30 arcseconds</div>
</body></html>""" % radius
try:
self.ra, self.dec = parse_arbitraty_coordinates(coordinates)
print(coordinates, self.ra, self.dec)
except ValueError:
# TODO: move to html file
return """<html><body>Coordinates You gave cannot be interpreted.<br>
Coordinates should be one of:
<ul>
<li>RA[+-\s]DE (in decimal degrees)</li>
<li>Or any text understood by <a href="http://docs.astropy.org/en/stable/coordinates/">astropy coordinates</a></li>
</ul>
</body></html>
"""
if self.dec < -90. or self.dec > 90. or self.ra < 0 or self.ra > 360.:
return """<html><body>Coordinates You gave cannot be interpreted.<br>
Coordinates should be one of:
<ul>
<li>RA[+-\s]DE (in decimal degrees)</li>
<li>Or any text understood by <a href="http://docs.astropy.org/en/stable/coordinates/">astropy coordinates</a></li>
</ul>
</body></html>
"""
catalog_list = ["'%s'" % cat for cat in self.catalogs.keys()]
ahtml = open('results.html', 'r').readlines()
ahtml = ' '.join(ahtml)
ahtml = ahtml % (', '.join(catalog_list))
self.radius = float(radius)
return ahtml
@cherrypy.expose
def get_info(self, catalog):
if catalog.lower() in self.mocs:
if not self.mocs[catalog.lower()].is_in(self.ra, self.dec):
# Not covered
return '0%s' % catalog
try:
result = self.catalogs[catalog].load_data(catalog,
self.ra, self.dec,
self.radius)
return result
except Exception as exc:
cherrylog.error('Error for catalog: %s' % catalog)
cherrylog.error(str(exc))
if __name__ == '__main__':
server = LookupServer()
server.start()
#if __name__ == '__main__':
# cherrypy.quickstart(LookupServer(), config='lookup.conf')