-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
156 lines (117 loc) · 4 KB
/
app.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
import os
import glob
import numpy as np
from flask import Flask, request, send_file
from grizli import utils
import json
from parse_coords import parse_coords
app = Flask(__name__)
app.debug = True
def get_hashroot():
rnd = int(np.random.rand()*100000)
return f'{rnd:05d}'
@app.route("/")
def show_table():
"""
Dump jwst-sources.html
"""
with open('jwst-sources.html') as fp:
lines = fp.readlines()
return ''.join(lines)
@app.route("/help")
def show_help():
"""
Dump jwst-sources.html
"""
with open('help.html') as fp:
lines = fp.readlines()
return ''.join(lines)
@app.route("/csv")
def show_csv():
"""
Dump jwst-sources.html
"""
with open('jwst-sources.csv') as fp:
lines = fp.readlines()
return ''.join(lines)
@app.route("/jname")
def make_jname():
"""
matches close to a position
"""
kwargs = dict(ra=214.914500, dec=52.94304, sep=1)
for k in kwargs:
if request.args.get(k) is not None:
#kwargs[k] = request.args[k]
if request.args[k].lower() in ['false','true']:
kwargs[k] = request.args[k].lower() == 'true'
else:
kwargs[k] = float(request.args[k])
coo = request.args.get('coords')
if coo is not None:
kwargs['ra'], kwargs['dec'] = parse_coords(coo)
src = utils.read_catalog('jwst-sources.csv')
this = utils.GTable()
this['ra'] = [kwargs['ra']]
this['dec'] = kwargs['dec']
idx, dr = this.match_to_catalog_sky(src)
hasm = dr.value < kwargs['sep']
if hasm.sum() > 0:
jname = src['jname'][hasm][0]
else:
jname = utils.radec_to_targname(kwargs['ra'], kwargs['dec'],
round_arcsec=(0.00001, 0.00001),
precision=2,
targstr='j{rah}{ram}{ras}.{rass}{sign}{ded}{dem}{des}.{dess}')
res = {'jname':jname, 'nmatch':int(hasm.sum()),
'ra':kwargs['ra'], 'dec':kwargs['dec']}
return json.dumps(res)
@app.route("/match")
def nearest_matches():
"""
matches close to a position
"""
kwargs = dict(ra=214.914500, dec=52.94304, sep=1, output='table')
for k in kwargs:
if request.args.get(k) is not None:
#kwargs[k] = request.args[k]
if request.args[k].lower() in ['false','true']:
kwargs[k] = request.args[k].lower() == 'true'
else:
try:
kwargs[k] = float(request.args[k])
except ValueError:
kwargs[k] = request.args[k]
coo = request.args.get('coords')
if coo is not None:
kwargs['ra'], kwargs['dec'] = parse_coords(coo)
src = utils.read_catalog('jwst-sources.csv')
this = utils.GTable()
this['ra'] = [kwargs['ra']]
this['dec'] = kwargs['dec']
idx, dr = this.match_to_catalog_sky(src)
hasm = dr.value < kwargs['sep']
if hasm.sum() == 0:
return 'No sources found within {sep:.1}" of {ra:.6f},{dec:.5f}'.format(**kwargs)
src['dr'] = dr.value
src = src[hasm]
src['dr'].format= '.2f'
src['dr'].description = 'Offset from {ra:.6f}, {dec:.5f} in arcsec'.format(**kwargs)
if kwargs['output'] == 'csv':
out = 'tmp'+get_hashroot()+'.csv'
src.write(out)
with open(out) as fp:
lines = fp.readlines()
os.remove(out)
return ''.join(lines)
sub = src['jname','count','F200W','F444W','ra','dec','dr',
'id','zphot','zspec','arxiv','author']
out = 'tmp'+get_hashroot()
sub.write_sortable_html(out,
localhost=False, max_lines=100000,
filter_columns=['count','ra','dec',
'dr','zphot','zspec'])
with open(out) as fp:
lines = fp.readlines()
os.remove(out)
return ''.join(lines)