-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup2.py
175 lines (160 loc) · 6.24 KB
/
lookup2.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import cherrypy
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
import jinja2
lookups = []
for provider in ['ObsLog',
'Q3CTap', 'BoxTap',
'Vizier', 'VSA', 'WSA',
'GCPD',
'DASCH', 'OGLE',
'JPlus', 'ESO', 'ChinaVO', 'STSCI', 'CRTS2',
#'DECam',
'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
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')
#$('#alphBnt').on('click', function(){
# var alphabeticallyOrderedDivs = $divs.sort(function(a,b){
# return $(a).find("h1").text() > $(b).find("h1").text();
# });
# $("#container").html(alphabeticallyOrderedDivs);
#});
@cherrypy.expose
def search(self, coordinates, radius):
self.ra, self.dec = parse_arbitraty_coordinates(coordinates)
self.radius = float(radius)
main_template = jinja2.Template("""
<html>
<head>
<link rel="stylesheet" type="text/css" href="static/lookup.css">
<script type="text/javascript" charset="utf8" src="static/jquery.js"></script>
<script type="text/javascript" language="javascript" class="init">
function askCatalog(name){
var params = {catalog: name};
jQuery.post('get_info', params,
function(response, textStatus, jX){
if (jX.status == 200){
document.getElementById("header-"+name).innerHTML = " click to collapse";
document.getElementById("result-"+name).innerHTML = response;
document.getElementById("over-"+name).toggleAttribute('open');
}
else if (jX.status == 205){
document.getElementById("over-"+name).remove();
document.getElementById("notcovered").innerHTML += " " + name;
}
else if (jX.status == 204){
document.getElementById("over-"+name).remove();
document.getElementById("nodata").innerHTML += " " + name;
}
else if (jX.status == 203){
document.getElementById("over-"+name).remove();
document.getElementById("errors").innerHTML += " " + name;
}
}
);
}
jQuery(window).on('load', function(){
var catalogs = {{catalogs}};
var counter = catalogs.length;
for (var i = 0; i < catalogs.length; i++) {
askCatalog(catalogs[i]);
}
});
</script>
</head>
<body>
<h1>Search around: {{ra}} {{dec}}</h1>
{% for catalog in catalogs %}
<details id="over-{{catalog}}">
<summary>
<h2 class="head"> {{ catalog }}</h2> <div class="head" id="header-{{catalog}}">...pending</div>
</summary>
<div id="result-{{catalog}}">{{ catalog }}</div>
</details>
{% endfor %}
<div id="notcovered">Not covered by:</div>
<div id="nodata">No data from:</div>
<div id="errors">Error from:</div>
</body>
</html>
""")
return main_template.render(catalogs=list(self.catalogs.keys()),
ra=self.ra,
dec=self.dec)
@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
cherrypy.response.status = 205
return ""
try:
print(catalog, self.ra, self.dec, self.radius)
result = self.catalogs[catalog].load_data(catalog,
self.ra, self.dec,
self.radius)
cherrypy.response.status = result['status']
print(catalog, result['status'])
return result['html']
except Exception as exc:
cherrylog.error('Error for catalog: %s' % catalog)
cherrylog.error(str(exc))
cherrylog.error(traceback.format_exc())
cherrypy.response.status = 203
return ""
if __name__ == '__main__':
server = LookupServer()
server.start()