-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths2n_param.py
More file actions
157 lines (121 loc) · 3.97 KB
/
s2n_param.py
File metadata and controls
157 lines (121 loc) · 3.97 KB
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
from __future__ import print_function
from __future__ import print_function
import subprocess as sub
import re
import os
def bad_obj(objcts):
bad = False
nbad = 0
print(objcts)
for pair in objcts:
_,oc = pair
if oc < 0:
nbad += 1
if nbad > 5:
bad = True
print(bad)
return bad
def parse_return(output,idlout):
lines = idlout.splitlines()
inline = 0
curkey = ''
for line in lines:
match = re.search(r'\A(\w+)\:',line)
if match:
if match.group(1) in output.keys():
curkey = match.group(1)
inline = 1
output[curkey]= line.split()[1:]
elif inline == 1:
output[curkey].extend(line.split())
temp = []
for i,wv in enumerate(output['wave']):
temp.append([float(wv),float(output['obj'][i]),float(output['sky'][i]),float(output['noise'][i]),float(output['s2n'][i])])
output['cts'] = temp
for key in ['s2n','sky','obj','noise']:
temp = []
jtemp = []
jkey = "j%s" % (key)
for i,wv in enumerate(output['wave']):
temp.append([float(wv),float(output[key][i])])
jtemp.append(float(output[key][i]))
output[key] = temp
output[jkey] = jtemp
nwave = []
for wv in output['wave']:
nwave.append(float(wv))
output['wave']=nwave
if bad_obj(output['obj']):
output["errormsg"] = "The selected filter and template do not overlap. Please select another filter, another template, or redshift the template. "
return output
def parse_request(value,regexp,name):
msg = ""
match = re.search(regexp,value)
if match:
cleanvalue = match.group(0)
else:
cleanvalue = -1
msg = "Inappropriate value for the input parameter %s" % name
return cleanvalue,msg
def paramandvals(param,paramval):
ret_str=""
match = re.search(r"\A\d+\.?\d*\Z",paramval)
if match:
ret_str= "%s=%s," % (param,paramval)
else:
ret_str= "%s='%s'," % (param,paramval)
return ret_str
def strip_badchar(com):
ncom,nvals = re.subn("[^a-zA-Z0-9_/,'=.]","",com)
return ncom,nvals
def build_exec_str(com,paramregexp,prettyparam,params):
output = dict(wave = [],
s2n = [],
obj = [],
objperwavesec = [],
noise = [],
sky = [],
com = "",
dich = "",
msg = '',
errormsg = '',
i2counts = None,
exp = None,
precision = None
)
for param in prettyparam.keys():
if param in params.keys() and params[param]:
paramval,output['msg'] = parse_request(params[param],paramregexp[param],prettyparam[param])
if param == 'dichroic':
output['dich'] = paramval
if output['msg']:
return "",output
else:
com += paramandvals(param,paramval)
com = com.rstrip(',')
return com,output
def gen_s2n(com,output,verbose,wsgi_dir):
com,nvals = strip_badchar(com)
if nvals:
output['msg'] = "Input values had some unexpected characters"
if len(com) == 0:
output['msg'] = "Command string has no characters"
return output
output['com'] = com
runargs = []
runargs.append(os.path.join(wsgi_dir,'idl_wrapper') )
if verbose:
print("path:",os.path.join(wsgi_dir,'idl_wrapper'))
print("com:",com)
print("runargs:", runargs)
try:
p = sub.Popen(runargs,stdin=sub.PIPE,stdout=sub.PIPE,stderr=sub.PIPE)
# we can rethink this now that we use idl_wrapper
idlout,idlerr=p.communicate(com)
if verbose:
print(idlout)
print(idlerr)
output = parse_return(output,idlout)
except:
output['msg'] = "Server side error, email holden@ucolick.org."
return output