-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathotool.py
executable file
·375 lines (310 loc) · 12.6 KB
/
otool.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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
from flask import Flask, render_template, send_from_directory, url_for, request, redirect
import ephem
from pylab import *
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import numpy as np
import mpld3
from json import JSONEncoder
import logging
#from io import BytesIO
#import base64
'''
import bokeh.plotting as plt
from bokeh.embed import components
from bokeh.resources import CDN
from bokeh.embed import components
'''
app = Flask(__name__)
logger = logging.getLogger(__name__)
@app.route('/otool/calculate/', methods=['POST', 'GET'])
def calculate():
"""Main method."""
fightml = ""
if request.method == 'POST':
lofar = ephem.Observer()
lofar.lon = '6.869882'
lofar.lat = '52.915129'
lofar.elevation = 15.
lofar.pressure = 0. # no refraction at horizon
lofar.horizon = '-0:34' # idem
dateTokens = []
if request.form['date'] != None and request.form['date'] != "":
dateTokens = request.form['date'].split('/')
lofar.date = str(dateTokens[2] + '/' + dateTokens[0] + '/' + dateTokens[1])
endOfDay = lofar.date
endOfDay += 1.
targets = []
aTeam = []
solSystem = []
calibrators = []
time = []
lst = []
targetList = ""
minElevation = 0.
if request.form['elevation'] != None and request.form['elevation'] != "":
minElevation = np.double(request.form['elevation'])
if request.form['targetList'] != None and request.form['targetList'] != "":
targetList = request.form['targetList']
if request.form['names'] != None and request.form['names'] != "" and request.form['ra'] != None and request.form['ra'] != "" and request.form['dec'] != None and request.form['dec'] != "":
for name, ra, dec in zip(request.form['names'].split(","), request.form['ra'].split(","), request.form['dec'].split(",")):
target = Target()
target.name = name
target.elevation = []
target.body = createFixedBody(str(ra), str(dec), '2000')
target.body.compute(lofar)
target.note = "</br>" + name + " and "
targets.append(target)
if request.form.get('CygA') != None:
cygA = Target()
cygA.name = 'CygA'
cygA.elevation = []
cygA.body = createFixedBody('19:59:28:3566', '+40:44:02.096', '2000')
cygA.body.compute(lofar)
for target in targets:
target.note += "<li>Cyg A: " + str(ephem.separation(target.body, cygA.body)) + "</li>"
aTeam.append(cygA)
if request.form.get('CasA') != None:
casA = Target()
casA.name = 'CasA'
casA.elevation = []
casA.body = createFixedBody('23:23:27.94', '+58:48:42.4', '2000')
casA.body.compute(lofar)
for target in targets:
target.note += "<li>Cas A: " + str(ephem.separation(target.body, casA.body)) + "</li>"
aTeam.append(casA)
if request.form.get('TauA') != None:
tauA = Target()
tauA.name = 'TauA'
tauA.elevation = []
tauA.body = createFixedBody('05:34:31.971', '+22:00:52.06', '2000')
tauA.body.compute(lofar)
for target in targets:
target.note += "<li>Tau A: " + str(ephem.separation(target.body, tauA.body)) + "</li>"
aTeam.append(tauA)
if request.form.get('VirA') != None:
virA = Target()
virA.name = 'VirA'
virA.elevation = []
virA.body = createFixedBody('12:30:49.4233', '+12:23:28.043', '2000')
virA.body.compute(lofar)
for target in targets:
target.note += "<li>Vir A: " + str(ephem.separation(target.body, virA.body)) + "</li>"
aTeam.append(virA)
if request.form.get('Sun') != None:
sun = Target()
sun.name = 'Sun'
sun.elevation = []
sun.body = ephem.Sun()
sun.body.compute(lofar)
for target in targets:
target.note += "<li>Sun: " + str(ephem.separation(target.body, sun.body)) + "</li>"
solSystem.append(sun)
if request.form.get('Jupiter') != None:
jup = Target()
jup.name = 'Jupiter'
jup.elevation = []
jup.body = ephem.Jupiter()
jup.body.compute(lofar)
for target in targets:
target.note += "<li>Jupiter: " + str(ephem.separation(target.body, jup.body)) + "</li>"
solSystem.append(jup)
if request.form.get('Saturn') != None:
sat = Target()
sat.name = 'Saturn'
sat.elevation = []
sat.body = ephem.Saturn()
sat.body.compute(lofar)
for target in targets:
target.note += "<li>Saturn: " + str(ephem.separation(target.body, sat.body)) + "</li>"
solSystem.append(sat)
if request.form.get('3C48') != None:
c48 = Target()
c48.name = '3C 48'
c48.elevation = []
c48.body = createFixedBody('01:37:41.2994', '+33:09:35.134', '2000')
c48.body.compute(lofar)
for target in targets:
target.note += "<li>3C 48: " + str(ephem.separation(target.body, c48.body)) + "</li>"
calibrators.append(c48)
if request.form.get('3C147') != None:
c147 = Target()
c147.name = '3C 147'
c147.elevation = []
c147.body = createFixedBody('05:42:36.1379', '+49:51:07.234', '2000')
c147.body.compute(lofar)
for target in targets:
target.note += "<li>3C 147: " + str(ephem.separation(target.body, c147.body)) + "</li>"
calibrators.append(c147)
if request.form.get('3C295') != None:
c295 = Target()
c295.name = '3C 295'
c295.elevation = []
c295.body = createFixedBody('14:11:20.519', '+52:12:09.97', '2000')
c295.body.compute(lofar)
for target in targets:
target.note += "<li>3C 295: " + str(ephem.separation(target.body, c295.body)) + "</li>"
calibrators.append(c295)
if request.form.get('3C196') != None:
c196 = Target()
c196.name = '3C196'
c196.elevation = []
c196.body = createFixedBody('08:13:36.033', '+48:13:02.56', '2000')
c196.body.compute(lofar)
for target in targets:
target.note += "<li>3C 196: " + str(ephem.separation(target.body, c196.body)) + "</li>"
calibrators.append(c196)
if request.form.get('3C380') != None:
c380 = Target()
c380.name = '3C 380'
c380.elevation = []
c380.body = createFixedBody('18:29:31.7809', '+48:44:46.160', '2000')
c380.body.compute(lofar)
for target in targets:
target.note += "<li>3C 380: " + str(ephem.separation(target.body, c380.body)) + "</li>"
calibrators.append(c380)
if request.form.get('3C286') != None:
c286 = Target()
c286.name = '3C 286'
c286.elevation = []
c286.body = createFixedBody('13:31:08.28', '+30:30:32.9', '2000')
c286.body.compute(lofar)
for target in targets:
target.note += "<li>3C 286: " + str(ephem.separation(target.body, c286.body)) + "</li>"
calibrators.append(c286)
if request.form.get('CTD93') != None:
c93 = Target()
c93.name = 'CTD 93'
c93.elevation = []
c93.body = createFixedBody('16:09:13.32', '+26:41:29.0', '2000')
c93.body.compute(lofar)
for target in targets:
target.note += "<li>CTD 93: " + str(ephem.separation(target.body, c93.body)) + "</li>"
calibrators.append(c93)
previous = None
lock = 0
while lofar.date < endOfDay - ephem.minute:
lofar.date += ephem.minute
for target in targets:
target.body.compute(lofar)
target.elevation.append(ephem.degrees(target.body.alt) * (180. / math.pi))
for at in aTeam:
at.body.compute(lofar)
at.elevation.append(ephem.degrees(at.body.alt) * (180. / math.pi))
for calibrator in calibrators:
calibrator.body.compute(lofar)
calibrator.elevation.append(ephem.degrees(calibrator.body.alt) * (180. / math.pi))
for sol in solSystem:
sol.body.compute(lofar)
sol.elevation.append(ephem.degrees(sol.body.alt) * (180. / math.pi))
time.append(lofar.date.datetime())
if previous > lofar.sidereal_time():
lock = 1
if lock:
lst.append(ephem.Date(str(lofar.date).split(' ')[0].split("/")[0] + "/" + str(lofar.date).split(' ')[0].split("/")[1] + "/" + str(int(str(lofar.date).split(' ')[0].split("/")[2]) + 1) + ' ' + str(lofar.sidereal_time())).datetime())
else:
lst.append(ephem.Date(str(lofar.date).split(' ')[0] + ' ' + str(lofar.sidereal_time())).datetime())
previous = lofar.sidereal_time()
fig, ax = plt.subplots(2, 1, figsize=(15, 10),sharex=False, sharey=False)
fig.canvas.draw()
fig.subplots_adjust(hspace=0.5)
ax[0].set_yticks([20, 60, 80])
for target in targets:
ax[0].plot(lst, target.elevation, '--', label=target.name)
sun = ephem.Sun()
sun.compute(lofar)
ax[0].hlines(minElevation, lst[0], lst[len(lst) - 1], colors='k', linestyles='solid', lw=2)
# Does not work with mpld3
#axis1.axvspan(ephem.Date(lofar.previous_rising(sun) - ephem.hour).datetime(), ephem.Date(lofar.previous_rising(sun) + ephem.hour).datetime(), facecolor='0.5', alpha=0.5)
#trans = transforms.blended_transform_factory(axis1.transData, axis1.transAxes)
#rect = patches.Rectangle((ephem.Date(lofar.previous_rising(sun) - ephem.hour).datetime(), 20), width=ephem.Date(ephem.hour).datetime(), height=90, transform=trans, color='yellow', alpha=0.5)
#axis1.add_patch(rect)
ax[0].set_ylabel(r'Elevation [degrees]', fontsize=18, fontweight='bold', color='#000000')
ax[0].set_title('LOFAR target visibility', fontsize=20, fontweight='bold')
time = np.array(time)
cnt = 1
for target in targets:
ax[1].plot(time, cnt * produceSegments(np.array(target.elevation), minElevation), '--', solid_capstyle='round', lw=5)
cnt+=1
for calibrator in calibrators:
ax[0].plot(lst, calibrator.elevation, label=calibrator.name)
cnt+=1
ax[1].plot(time, cnt * produceSegments(np.array(calibrator.elevation), minElevation), solid_capstyle='round', lw=5)
for at in aTeam:
ax[0].plot(lst, at.elevation, label=at.name)
cnt+=1
ax[1].plot(time, cnt * produceSegments(np.array(at.elevation), minElevation), solid_capstyle='round', lw=5)
for sol in solSystem:
ax[0].plot(lst, sol.elevation, label=sol.name)
cnt+=1
ax[1].plot(time, cnt * produceSegments(np.array(sol.elevation), minElevation), solid_capstyle='round', lw=5)
sep_message = "The angular distance on the sky (dd:mm:ss.s) between: "
for target in targets:
sep_message+=target.note
sep_message+= "<ul>"
for calibrator in calibrators:
sep_message+=calibrator.note
for at in aTeam:
sep_message+=at.note
for sol in solSystem:
sep_message+=sol.note
sep_message+= "</ul>"
ax[1].vlines((lofar.previous_rising(sun)).datetime(), 0, cnt + 1, colors='r', linestyles='dashed', lw=2)
ax[1].vlines((lofar.previous_setting(sun)).datetime(), 0, cnt + 1, colors='r', linestyles='dashed', lw=2)
ax[1].vlines(ephem.Date(lofar.previous_rising(sun) - ephem.hour).datetime(), 0, cnt + 1, colors='k', linestyles='dashdot', lw=2)
ax[1].vlines(ephem.Date(lofar.previous_rising(sun) + ephem.hour).datetime(), 0, cnt + 1, colors='k', linestyles='dashdot', lw=2)
ax[1].vlines(ephem.Date(lofar.previous_setting(sun) - ephem.hour).datetime(), 0, cnt + 1, colors='k', linestyles='dashdot', lw=2)
ax[1].vlines(ephem.Date(lofar.previous_setting(sun) + ephem.hour).datetime(), 0, cnt + 1, colors='k', linestyles='dashdot', lw=2)
ax[1].text(ephem.Date(lofar.previous_rising(sun) - 2. * ephem.hour).datetime(), cnt + 1.5, "Sunrise +/- 1hr")
ax[1].text(ephem.Date(lofar.previous_setting(sun) - 2. * ephem.hour).datetime(), cnt + 1.5, "Sunset +/- 1hr")
ax[1].set_ylabel(r'Sources', fontsize=18, fontweight='bold', color='#000000', labelpad=30)
plt.ylim(0, cnt + 3)
ax[0].grid()
ax[0].set_xlabel('LST')
ax[0].xaxis.labelpad = 20
ax[0].yaxis.labelpad = 20
ax[1].axes.get_yaxis().set_ticks([])
ax[1].set_xlabel('UT')
ax[1].xaxis.tick_top()
ax[1].xaxis.set_label_position('top')
ax[1].xaxis.labelpad = 20
legend1 = ax[0].legend(loc='upper center', shadow=True, labelspacing=0.01, ncol=4, bbox_to_anchor=(0.5, 1.))
fightml = mpld3.fig_to_html(fig)
logger.info("Finished computations, rendering...")
return render_template('otool.html', form=request.form, plot=fightml, info=sep_message)
# Array masks don't plot with mpld3
def produceSegments(elevArray, minElevation):
"""Replace entries in the elevation array with NaNs if they are below the elevation selected by the user."""
targetVisibility = np.ones(len(elevArray))
for i, elev in enumerate(elevArray):
if elev < minElevation:
targetVisibility[i] = np.nan
return targetVisibility
def createFixedBody(ra, dec, epoch):
"""Fixed body adapted to us."""
fixedBody = ephem.FixedBody()
fixedBody._ra = ra
fixedBody._dec = dec
fixedBody._epoch = epoch
return fixedBody
class Target(object):
"""Encapsulates celestial bodies."""
def __init__(self, name="", body=None, elevation=[], note = ""):
self.name = name
self.body = body
self.elevation = elevation
self.note = note
@app.route('/otool/static/<path:path>')
def send_js(path):
return send_from_directory('static', path)
# Home page
@app.route('/otool/')
def otool():
"""The landing page"""
return render_template('otool.html')
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
app.run(debug=True)
logger.info("Server running.")
#app.run()
#app.run(host='0.0.0.0')