-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
308 lines (254 loc) · 9.06 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
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
from flask import Flask, render_template, request, make_response, session, redirect, url_for, jsonify
from flask_session import Session
from werkzeug.datastructures import CombinedMultiDict, ImmutableMultiDict
import webbrowser
from threading import Timer
from datetime import timedelta
import maskLayouts as ml
import configFile as cf
import json
import re
import numpy as np
#import dss2Header
import targs
import pdb
import calcmask
import utils
import plot
from targetSelector import TargetSelector
import dsimselector
def launchBrowser(host, portnr, path):
webbrowser.open(f"http://{host}:{portnr}/{path}", new=1)
def stripquote(string):
if string.count('"')==2:
string=re.findall(r'"([^"]*)"',string)
return string
def form_or_json():
if request.method == 'POST':
if request.files:
return CombinedMultiDict((request.files, request.form))
elif request.form:
return request.form
elif request.get_json():
return ImmutableMultiDict(request.get_json())
def fixType(params):
for p in params:
if p=='MaskIdfd':
params[p]=[int(params[p][0])]
elif p=='MinSlitLengthfd' or p=='MinSlitSeparationfd' or p=='SlitWidthfd' or p=='AlignBoxSizefd' or p=='BlueWaveLengthfd' or p=='RedWaveLengthfd' or p=='CenterWaveLengthfd' or p=='Temperaturefd' or p=='Pressurefd' or p=='MaskPAfd' or p=='SlitPAfd' or p=='MaskMarginfd' or p=='HourAnglefd':
params[p]=[float(params[p][0])]
else:
continue
return params
def dbprint(st):
print("\033[95m {}\033[00m" .format(st))
pass
app = Flask(__name__)
app.secret_key='dsf2315ewd'
app.config['SESSION_PERMANENT'] = True
app.config['SESSION_TYPE'] = 'filesystem'
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(hours=0.001)
# The maximum number of items the session stores
# before it starts deleting some, default 500
app.config['SESSION_FILE_THRESHOLD'] = 10000
Session(app)
#@app.route('/readparams')
def readparams():
dict={}
with open('params.cfg') as f:
for line in f:
try:
if len(line.split(','))==4:
sep=line.strip().split(',')
(k,v) = sep[0].split(' = ')
dict[k]=(stripquote(v),stripquote(sep[1]),stripquote(sep[2]),stripquote(sep[3]))
else:
continue
except Exception as e:
pass
print('Failed to load parameters',e)
return dict
@app.route('/setColumnValue',methods=["GET","POST"])
def setColumnValue():
global df
global prms
targets=df
params=prms
# pdb.set_trace()
values=json.loads(request.data.decode().split('=')[2].split('&')[0])
column=request.data.decode().split('=')[1].split('&')[0]
df=targs.updateColumn(targets,column,values)
outp=targs.toJsonWithInfo(params,df)
return outp
@app.route('/updateTarget',methods=["GET","POST"])
def updateTarget():
global df
global prms
targets=df
values=json.loads(request.data.decode().split('=')[1].split('}&')[0]+'}')
df,idx=targs.updateTarget(targets,values)
outp=targs.toJsonWithInfo(prms,df)
tgs = json.loads(outp)
outp=merged_dict = {**tgs, **{'idx':idx}}
return outp
# return json.dumps({'idx':idx})
@app.route('/updateSelection',methods=["GET","POST"])
def updateSelection():
global df
global prms
targets=df
values=json.loads(request.data.decode().split('=')[1].split('}&')[0]+'}')
df,idx=targs.updateTarget(targets,values)
outp=targs.toJsonWithInfo(prms,df)
tgs = json.loads(outp)
outp=merged_dict = {**tgs, **{'idx':idx}}
return outp
@app.route('/deleteTarget',methods=["GET","POST"])
def deleteTarget():
global df
global prms
targets=df
idx=int(request.args.get('idx',None))
df=targs.deleteTarget(targets,idx)
outp=targs.toJsonWithInfo(prms,df)
return outp
@app.route('/resetSelection',methods=["GET","POST"])
def resetSelection():
global df
global prms
df.selected=df.loadselected
outp=targs.toJsonWithInfo(prms,df)
return outp
@app.route('/getTargetsAndInfo')
def getTargetsAndInfo():
global prms
params=prms
global df
try:
print('newdf/getTargetsAndInfo')
newdf=calcmask.genObs(df,params)
newdf=targs.markInside(newdf)
mask = ml.MaskLayouts["deimos"]
minX, maxX = np.min(mask, axis=0)[0], np.max(mask, axis=0)[0]
#selector = TargetSelector(newdf, minX, maxX, float(params['MinSlitLengthfd'][0]), float(params['MinSlitSeparationfd'][0]))
#newdf = selector.performSelection(extendSlits=False)
outp=targs.toJsonWithInfo(params,newdf)
except Exception as e:
print('Exception',e)
outp=''
return outp
@app.route('/generateSlits',methods=["GET","POST"])
def generateSlits():
global df
global prms
params=prms
df=targs.markInside(df)
newdf=calcmask.genSlits(df,params,auto_sel=False)
mask = ml.MaskLayouts["deimos"]
minX, maxX = np.min(mask, axis=0)[0], np.max(mask, axis=0)[0]
df=newdf
outp=targs.toJsonWithInfo(params,newdf)
return outp
##Performs auto-selection of slits##
@app.route('/recalculateMask',methods=["GET","POST"])
def recalculateMask():
global df
global prms
params=prms
df=targs.markInside(df)
newdf=calcmask.genSlits(df,params,auto_sel=True)
mask = ml.MaskLayouts["deimos"]
minX, maxX = np.min(mask, axis=0)[0], np.max(mask, axis=0)[0]
# selector = TargetSelector(newdf, minX, maxX, float(params['MinSlitLengthfd'][0]), float(params['MinSlitSeparationfd'][0]))
# newdf = selector.performSelection(extendSlits=False)
df=newdf
outp=targs.toJsonWithInfo(params,newdf)
return outp
@app.route('/saveMaskDesignFile',methods=["GET","POST"])
def saveMaskDesignFile(): # should only save current rather than re-running everything!
global df
global prms
params=prms
df=targs.markInside(df)
mask = ml.MaskLayouts["deimos"]
minX, maxX = np.min(mask, axis=0)[0], np.max(mask, axis=0)[0]
# selector = TargetSelector(df, minX, maxX, float(params['MinSlitLengthfd'][0]), float(params['MinSlitSeparationfd'][0]))
# df = selector.performSelection(extendSlits=False)
newdf=calcmask.genMaskOut(df,params)
plot.makeplot(params['OutputFitsfd'][0])
df=newdf
outp=targs.toJsonWithInfo(params,newdf)
return outp
##Update Params Button, Load Targets Button
@app.route('/sendTargets2Server',methods=["GET","POST"])
def sendTargets2Server():
print('sendTargets2Server()')
global prms
prms=request.form.to_dict(flat=False)
params=prms
print(prms)
centerRADeg,centerDEC,positionAngle=15*utils.sexg2Float(params['InputRAfd'][0]),utils.sexg2Float(params['InputDECfd'][0]),float(params['MaskPAfd'][0])
fh=[]
session['params']=params
prms=params
print(request.files['targetList'])
uploaded_file = request.files['targetList']
if uploaded_file.filename != '':
input=uploaded_file.stream
for line in input:
fh.append(line.strip().decode('UTF-8'))
session['file']=fh
global df
df=targs.readRaw(session['file'],prms)
df['loadselected']=df.selected #Only backup selected targets on file load.
outp=targs.toJsonWithInfo(params,df)
return outp
# return ''
##Update Params Button
@app.route('/updateParams4Server',methods=["GET","POST"])
def updateParams4Server():
global prms
prms=request.form.to_dict(flat=False)
centerRADeg,centerDEC,positionAngle=15*utils.sexg2Float(prms['InputRAfd'][0]),utils.sexg2Float(prms['InputDECfd'][0]),float(prms['MaskPAfd'][0])
session['params']=prms
return ''
#Loads original params
@app.route('/getConfigParams')
def getConfigParams():
global prms
paramData=readparams()
prms=paramData
print('params:',paramData)
session['params']=paramData
prms=paramData
return json.dumps({"params": paramData})
@app.route('/getMaskLayout')
def getMaskLayout():
"""
Gets the mask layout, which is defined in maskLayout.py as a python data structure for convenience.
MaskLayoput, GuiderFOV and Badcolumns are defined in maskLayouts.py
Returns a JSON with mask, guiderFOC and badColumns
"""
try:
instrument = "deimos"
mask = ml.MaskLayouts[instrument] # a list of (x,y,flag), polygon vertices
guiderFOV = ml.GuiderFOVs[instrument] # list of (x, y, w, h, ang), boxes
badColumns = ml.BadColumns[instrument] # list of lines, as polygons
return {"mask": mask, "guiderFOV": guiderFOV, "badColumns": badColumns} ####might need to be jsonified
except Exception as e:
print(e)
return ((0, 0, 0),)
#deletes target
@app.route('/deleteTarget')
#def deleteTarget():
# return json.dumps({"params": paramData})
@app.route('/')
def home():
return render_template('dt.html')
@app.route("/targets", methods=["GET","POST"])
def LoadTargets():
return
if __name__ == '__main__':
t=Timer(1,launchBrowser,['localhost',9305,'/'])
t.start()
app.run(host='localhost',port=9305,debug=True,use_reloader=False)