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