source: trunk/monitor/cgi-bin/asapmon.py@ 1822

Last change on this file since 1822 was 1419, checked in by Malte Marquarding, 16 years ago

some updates to make the asapmonitor work again

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 9.7 KB
RevLine 
[703]1#!/usr/bin/python
2import sys,os
3import cgi
[712]4#cgi debug
[703]5import cgitb; cgitb.enable()
6
7from simpletal import simpleTAL, simpleTALES
8
[712]9from obsconfig import *
10
[1419]11tmppath = os.path.join(asapmonhome,"tmp/")
12tmppath = os.path.join(asapmonrel,"tmp/")
13
[712]14# a redirection object for stdout/stderr
15class WritableObject:
16 def __init__(self):
17 self.content = []
18 def write(self, string):
19 self.content.append(string)
20
21logsink = WritableObject()
22logsink2 = WritableObject()
[1295]23sys.stdout = logsink
[712]24sys.stderr = logsink2
[703]25import asap
[1419]26asap.rc('scantable', storage="memory")
[703]27sys.stdout = sys.__stdout__
28sys.stderr = sys.__stderr__
29
30def resetstd():
31 sys.stdout = sys.__stdout__
32 sys.stderr = sys.__stderr__
33
34
35class myForm:
36 def __init__(self):
37 self.fields = {}
38 self.form = cgi.FieldStorage()
39 self.context = simpleTALES.Context(allowPythonPath=1)
40 self.logsink = WritableObject()
41
[712]42 def decodePath(self):
43 pi = self.fields['cdir']
44 fi = self.fields['cfile']
45 p = observatory['rpfpath'][pi]
[703]46 from filelist import FileList
47 fl = FileList(pi)
48 if fl.error:
49 return None
[712]50 out = []
51 for i in fi:
[1295]52 out.append(os.path.join(p,fl.files[i]))
[712]53 return out
[703]54
[712]55 def decodeWindow(self,window):
56 if not len(window.strip()): return None,None
[1419]57 x = window.split(",")
[1295]58 return [float(x[0].strip()), float(x[1].strip())]
[712]59
[1295]60 def decodeWindows(self, window):
[712]61 import re
62 p = re.compile("(\\[*\d+\\.*\d*,\d+\\.*\d*\\]*)")
63 r = re.compile("[\\[\\]]")
[1295]64 return [self.decodeWindow(re.sub(r, '', s)) for s in re.findall(p, window)]
[712]65
66 def setDefaultFields(self):
67 self.fields['directories'] = observatory['rpfpath']
68 self.fields['cdir'] = len(self.fields['directories'])-1
69 from filelist import FileList
70 files = []
71 fl = FileList(len(observatory['rpfpath'])-1)
72 if not fl.error:
73 self.fields['files'] = fl.files
74 self.fields['cfile'] = len(fl.files)-1
75 self.fields['restfreqs'] = observatory['lines'].keys()
76 self.fields['border'] = range(10)
77 self.fields['imagename'] = ""
78 self.fields['cunit'] = 0
[1295]79 self.fields['units'] = ["channel", "km/s", "GHz"]
[712]80 self.fields['baseline'] = 0
81 self.fields['cpolyorder'] = 0
82 self.fields['quotient'] = 0
83 self.fields['average'] = 0
84 self.fields['doppler'] = "RADIO"
85 self.fields['frame'] = "LSRK"
[1295]86 self.fields['restn'] = [11, 1]
[712]87 self.fields['stokes'] = 0
88 self.fields['summary'] = ""
89 self.fields['bin'] = 0
90 self.fields['brangewindow'] = ""
91 self.fields['nif'] = []
92 self.fields['sourcenames'] = []
93 self.fields['csource'] = ""
94
95
96 def getFormFields(self):
[1295]97 self.fields['cunit'] = int(self.form.getfirst("unit", 0))
98 self.fields['frame'] = self.form.getfirst("frame", "TOPO")
99 self.fields['doppler'] = self.form.getfirst("doppler", "RADIO")
[712]100 self.fields['restn'] = []
101
[1295]102 self.fields['plotwindow'] = self.form.getfirst("plotwindow", "")
[712]103 self.fields['baseline'] = int(self.form.has_key("baseline"))
[1295]104 self.fields['cpolyorder'] = int(self.form.getfirst("polyorder", 0))
[712]105 self.fields['quotient'] = int(self.form.has_key("quotient"))
[1295]106 self.fields['doppler'] = self.form.getfirst("doppler", "RADIO")
107 self.fields['frame'] = self.form.getfirst("frame", "LSRK")
108 self.fields['cdir'] = int(self.form.getfirst("dlist", None))
[712]109 self.fields['cfile'] = [int(k) for k in self.form.getlist("list")]
110 self.fields['average'] = int(self.form.has_key("average"))
111 self.fields['stokes'] = int(self.form.has_key("stokes"))
112 self.fields['bin'] = int(self.form.has_key("bin"))
113 self.fields['debug'] = ""#self.fields['restn']
[1295]114 self.fields['csource'] = self.form.getfirst("csource", "")
[712]115
116 def getRest(self):
117 alllines = observatory['lines'].values()
118 lines = []
119 for i in self.fields['restn']:
120 lines.append(alllines[i])
121 return lines
122
[703]123 def plotForm(self):
[712]124 self.getFormFields()
125 # decode file location
[703]126 from filelist import FileList
[712]127 fl = FileList(self.fields['cdir'])
[703]128 self.fields['files'] = fl.files
[712]129 files = self.decodePath()
130 # catch all stdout/err
131 sys.stdout = logsink
132 sys.stderr = logsink2
[703]133 try:
[990]134 s = asap.scantable(files)
135 outscans = None
[1083]136 self.fields['nif'] = range(s.nif())
[712]137 for i in self.fields['nif']:
138 name = "rest%d" % i
[1295]139 self.fields['restn'].append(int(self.form.getfirst(name, 0)))
[712]140 restfs = self.getRest()
[703]141
[712]142 # source name selection
143 import re
[990]144 srcnames = s.get_sourcename()
145 for i in srcnames:
146 # only add the names once
147 i = not i in self.fields['sourcenames'] and i
148 if i:
149 # filter off scans
[1295]150 i = not re.search(re.compile("_[R, e, w]$"), i) and i
[712]151 if i:
[990]152 self.fields['sourcenames'].append(i)
153 # form quotient
154 if self.form.has_key("quotient"):
155 s = s.auto_quotient()
156 # get source by name
157 cs = self.fields['csource']
158 if len(cs) > 0:
159 if cs in self.fields['sourcenames']:
160 ss = s.get_scan(self.fields['csource'])
[1295]161 if isinstance(ss, asap.scantable):
[990]162 s = ss
163 del ss
164 else:
165 # get only the last source in the table if not averaging
[1419]166 # This doesn't work anymore, but works on the command-line??
167 # Use explict selection
168 #s = s.get_scan(str(self.fields['sourcenames'][-1]))
169 sel = asap.selector()
170 sel.set_name(str(self.fields['sourcenames'][-1]))
171 s.set_selection(sel)
172 s = s.copy()
[990]173 self.fields['csource'] = s.get_sourcename()[-1]
[1083]174 if self.fields['cunit'] == 1:
[1419]175 s.set_restfreqs(restfs, unit="GHz")
[1083]176 s.set_unit(self.fields['units'][self.fields['cunit']])
[1295]177 s.set_freqframe(self.form.getfirst("frame", "LSRK"))
178 s.set_doppler(self.form.getfirst("doppler", "RADIO"))
[1083]179
[990]180 # baseline
181 if self.form.has_key('baseline'):
182 order = self.fields['cpolyorder']
183 brstr = self.form.getfirst('brangewindow','').strip()
184 # auto baseline or user window
185 if brstr:
186 self.fields['brangewindow'] = brstr
187 brange = self.decodeWindows(brstr)
188 if len(brange):
189 self.fields['brangewindow'] = brstr
190 m = s.create_mask(brange)
[1295]191 s.poly_baseline(mask=m, order=order)
[712]192 else:
[990]193 s.auto_poly_baseline(order=order)
194 outscans = None
[712]195
196 if self.fields['average']:
[990]197 outscans = s.average_time(weight='tsys')
[712]198 else:
[990]199 outscans = s
200 del s
[712]201
202 if self.fields['bin']:
203 outscans.bin()
204
205 self.fields['summary'] = str(outscans)
206 asap.rcParams['plotter.decimate'] = True
207 asap.rcParams['plotter.ganged'] = False
208 from matplotlib import rcParams as rcp
[1083]209 rcp['xtick.labelsize'] = 8
210 rcp['ytick.labelsize'] = 8
[712]211 rcp['axes.labelsize'] = 8
[1083]212 rcp['axes.titlesize'] = 12
[712]213 rcp['figure.subplot.wspace'] = 0.3
214 rcp['figure.subplot.hspace'] = 0.3
[1419]215 #del asap.plotter
[990]216 # plotter without GUI
[712]217 asap.plotter = asap.asapplotter(False)
[1083]218 if outscans.nif() > 1:
[1295]219 asap.plotter.set_mode("p", "i")
[703]220 else:
[1083]221 if outscans.npol() > 2:
[1295]222 asap.plotter.set_mode("t", "p")
[712]223 else:
[1295]224 asap.plotter.set_mode("p", "t")
[712]225 asap.plotter.plot(outscans)
226 if self.fields['stokes']:
227 pols = "I"
228 if outscans.npol() > 2:
229 pols += " Q U V"
[990]230 sel = asap.selector()
231 sel.set_polarisations(pols)
232 asap.plotter.set_selection(sel)
[1295]233 x0, x1 = self.decodeWindow(self.fields['plotwindow'])
234 asap.plotter.set_range(x0, x1)
[712]235 imname = tmppath+"plot.png"
[1295]236 asap.plotter.save(imname, dpi=96)
[712]237 self.fields['imagename'] = tmploc+"plot.png"
[1295]238 except RuntimeError, e:
[712]239 self.fields['debug'] = e
[703]240 return
241
242
243 def buildContext (self, title):
244 self.context.addGlobal("fields", self.fields)
245 self.context.addGlobal("title", title)
246
247 def expandTemplate (self, templateName):
248 sys.stdout.write ("Content-Type: text/html\n")
249 sys.stdout.write ("\n")
250 # Expand the template and print it out
251 templateFile = open(templateName, 'r')
252 template = simpleTAL.compileHTMLTemplate(templateFile)
253 templateFile.close()
254 # Expand the template as HTML using this context
255 template.expand(self.context, sys.stdout)
256 sys.exit(0)
257
258 def main(self):
[712]259 self.setDefaultFields()
260 title = "ASAP %s Online Monitor" % (observatory['name'])
[1419]261 tmplname = asapmonhome+"asapmon.html.template"
[712]262 if ( self.form.has_key("plot")):
[703]263 self.plotForm()
[712]264 self.buildContext(title)
265 resetstd()
266 #os.system('rm -rf /var/www/asapmon/tmp/.matplotlib')
267 self.expandTemplate(tmplname)
[703]268
269
270f = myForm()
271f.main()
Note: See TracBrowser for help on using the repository browser.