00001
00002
00003
00004
00005
00006
00007
00008
00009 import os, time
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 rt="cln_w3oh"
00022 pyonly=False
00023
00024 l=locals()
00025 if not l.has_key("analonly"):
00026 analonly=False
00027 if not analonly:
00028 os.system("rm -rf "+rt+"*")
00029
00030 startTime = time.time()
00031 startProc = time.clock()
00032
00033 print '--Running clean chan/spw test--'
00034
00035 import datetime
00036 datestring = datetime.datetime.isoformat(datetime.datetime.today())
00037 outfile = rt + datestring + '.log'
00038 logfile = open(outfile, 'w')
00039 print 'Writing output to ' + outfile + "\n"
00040
00041 l=locals()
00042 if not l.has_key("repodir"):
00043 repodir=os.getenv("CASAPATH").split(' ')[0]
00044
00045 print 'I think the data repository is at '+repodir
00046 datadir=repodir+"/data/regression/cvel/input/"
00047
00048
00049 if not l.has_key("pyonly"):
00050 pyonly=False
00051
00052
00053 if not os.path.exists(rt+".ms"):
00054 importuvfits(fitsfile=datadir+'W3OH_MC.UVFITS', vis=rt+'.ms')
00055
00056
00057
00058 tb.open(rt+'.ms/SPECTRAL_WINDOW')
00059 cf = tb.getvarcol('CHAN_FREQ')
00060 ch0_ms = cf['r1'][0][0] / 1e3
00061 chn_ms = cf['r1'][510][0] / 1e3
00062 del_ms = (cf['r1'][1][0] - 1e3*ch0_ms) /1e3
00063 del2_ms = (1e3*chn_ms - cf['r1'][509][0]) /1e3
00064 cw = tb.getvarcol('CHAN_WIDTH')
00065 wid_ms = cw['r1'][0][0] /1e3
00066 nch_ms = tb.getcol("NUM_CHAN")[0]
00067 reffreq_ms = tb.getcol("REF_FREQUENCY")[0] / 1e3
00068 tb.close()
00069
00070
00071 tb.open(rt+".ms")
00072 dat=tb.getcol("DATA")
00073 tb.done()
00074 spec=pl.zeros(nch_ms)
00075 for i in range(nch_ms):
00076 spec[i]=pl.mean(dat[0,i,])
00077
00078
00079 x=pl.array(range(21))+250
00080 y=spec[x]
00081 fitchan_ms = pl.mean((x+1)*y)/pl.mean(y)
00082 x0=floor(fitchan_ms)
00083 xf=fitchan_ms-x0
00084 fit_ms = cf['r1'][x0][0] + xf * (cf['r1'][x0+1][0]-cf['r1'][x0][0])
00085 fit_ms=fit_ms / 1e3
00086
00087 del dat
00088
00089 fit_ms = 1665658.566
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 if pyonly:
00106 stats=[(nch_ms,"%fkHz" % ch0_ms,"%fkHz" % del_ms)]
00107 else:
00108 stats=[(ch0_ms,del_ms,wid_ms,del2_ms,nch_ms,chn_ms,fit_ms)]
00109
00110
00111 lname=['ms']
00112 sname=['ms']
00113
00114
00115
00116
00117
00118 def imstats(image):
00119 ia.open(image)
00120 ch0 = (ia.toworld([n/2,n/2,0,0],'n')['numeric'][3])/ 1e3
00121 nch = ia.shape()[3]
00122 chn = (ia.toworld([n/2,n/2,0,nch-1],'n')['numeric'][3])/ 1e3
00123 del1 = (ia.toworld([n/2,n/2,0,1],'n')['numeric'][3] - 1e3*ch0) /1e3
00124 wid = (ia.summary()['incr'][3]) /1e3
00125 del2 = (1e3*chn - ia.toworld([n/2,n/2,0,nch-2],'n')['numeric'][3]) /1e3
00126 try:
00127 mylist = []
00128 for i in range(len(ia.shape())):
00129 mylist.append(0)
00130 mylist.append(0)
00131 t = tuple(mylist)
00132 center = ia.fitprofile(ngauss=1,poly=1)['gs']['center'].item(t)
00133 csys = ia.coordsys()
00134 fit = csys.velocitytofrequency(center)/1e3
00135 except:
00136 fit=0.
00137 return (ch0,del1,wid,del2,nch,chn,fit)
00138
00139
00140
00141 def swapvel(stats):
00142 return (stats[5], -stats[1], -stats[2], -stats[3], stats[4], stats[0], stats[6])
00143
00144
00145
00146
00147
00148 f0=reffreq_ms
00149 v1_ms=(f0-ch0_ms)/f0*299792.458
00150 v0_ms=(f0-chn_ms)/f0*299792.458
00151 incr = -del_ms/f0*299792.458
00152 w4="%fkm/s" % incr
00153
00154
00155 tests=[
00156 {'name':'avel', 'mode':'velocity', 'desc':'velo default'},
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167 {'name':'nvel', 'mode':'velocity', 'sta':"%fkm/s" % v1_ms,'wid':w4,'desc':'velo start=%5.2fkm/s wid=%5.2fkm/s' % (v1_ms,incr)},
00168
00169 {'name':'chan', 'mode':'channel', 'desc':'chan default'},
00170
00171
00172
00173
00174
00175
00176 {'name':'frm', 'mode':'frequency','wid':"-%fkHz" % wid_ms,'sta':'%fkHz' % chn_ms, 'desc':'freq wid=-%6.1fKHz' % wid_ms},
00177 {'name':'freq', 'mode':'frequency', 'desc':'freq default'},
00178
00179 {'name':'fr.5', 'mode':'frequency','shift':wid_ms/2, 'desc':'freq start=%5.3fkHz' % (wid_ms/2)},
00180
00181
00182 ]
00183
00184
00185
00186
00187 toplot=[]
00188
00189 if pyonly:
00190 from cleanhelper import *
00191 imCln=imtool()
00192 vis=rt+'.ms'
00193 imset=cleanhelper(imCln, vis, False, casalog)
00194 outframe=''
00195 else:
00196
00197 n=64
00198
00199
00200
00201
00202
00203
00204 j=0
00205 for te in tests:
00206 if te.has_key('spw'):
00207 spw=te['spw']
00208 else:
00209 spw=''
00210 if te.has_key('nchan'):
00211 nchan=te['nchan']
00212 else:
00213 nchan=-1
00214 if te.has_key('int'):
00215 interp=te['int']
00216 else:
00217 interp='nearest'
00218 if te.has_key('wid'):
00219 wid=te['wid']
00220 else:
00221 wid=''
00222
00223
00224
00225
00226 if te.has_key('veltype'):
00227 vtype=te['veltype']
00228 else:
00229 vtype='radio'
00230
00231 if te.has_key('sta'):
00232 sta=te['sta']
00233 elif te.has_key('shift'):
00234 start=ch0_ms + te['shift']
00235 sta="%f kHz" % start
00236 else:
00237 sta=''
00238
00239
00240
00241
00242
00243 if pyonly:
00244 try:
00245 print te['mode'],spw,"",nchan,sta,wid,outframe,vtype,str(reffreq_ms)+'kHz'
00246
00247 st = imset.setChannelizeDefault(te['mode'],spw,"",nchan,sta,wid,outframe,vtype,"", str(reffreq_ms)+'kHz')
00248
00249
00250 except:
00251 st = (-1,"","")
00252 else:
00253 try:
00254 if not analonly:
00255 print 'Running clean for name=%s, mode=%s'%(te['name'],te['mode'])
00256 clean(vis=rt+'.ms',
00257 imagename=rt+'_'+te['name'],
00258 cell="6arcsec",imsize=[n,n],
00259 spw=spw,
00260 imagermode='',
00261 mode=te['mode'],veltype=vtype,
00262 start=sta,width=wid,nchan=nchan,
00263 interpolation=interp,
00264 niter=100,threshold='1mJy',
00265 restfreq=str(reffreq_ms)+'kHz')
00266 if te['mode']=='velocity':
00267 st=swapvel(imstats(rt+'_'+te['name']+'.image'))
00268 else:
00269 st=imstats(rt+'_'+te['name']+'.image')
00270 if te.has_key('shift'):
00271
00272 shift=te['shift']
00273 st=(st[0]-shift, st[1],st[2],st[3],st[4], st[5]-shift, st[6]-shift)
00274
00275 if toplot.count(te['name'])>0:
00276 ia.open(rt+'_'+te['name']+'.image')
00277 foo=ia.getchunk(blc=[25,25,0,0],trc=[40,40,0,510],dropdeg=True,axes=[0,1])
00278 c=ia.coordsys()
00279 n=len(foo)
00280 f=pl.array(range(n))
00281 for i in range(n):
00282 f[i]=c.toworld([32,32,0,i])['numeric'][3]
00283 ia.done()
00284
00285 pl.plot(f,foo,label=te['desc'])
00286 j=j+1
00287 except:
00288 st=(reffreq_ms,0,0,0,-1,reffreq_ms,reffreq_ms)
00289 stats.append(st)
00290 lname.append(te['desc'])
00291 sname.append(te['name'])
00292
00293
00294
00295
00296
00297 lth=0
00298 for i in range(len(lname)):
00299 lth0=len(lname[i])
00300 if lth0>lth:
00301 lth=lth0
00302 fmt="%-"+str(lth)+"s"
00303
00304
00305 if pyonly:
00306 yo = fmt+" nchan chan0 width "
00307 else:
00308 yo = fmt+" chan0 (kHz) ch1-ch0 width chn-(n-1) n lastchan fit peak"
00309
00310
00311
00312
00313 print >> logfile, yo % " "
00314 for i in range(len(lname)):
00315 if pyonly:
00316
00317 print >> logfile, fmt % lname[i], "%4i %20s %20s" % stats[i]
00318 else:
00319 print >> logfile, fmt % lname[i], "%15.7f %11.7f %10.7f %10.7f %4i %16.7f %16.7f" % stats[i]
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332 print yo % " "
00333 if pyonly:
00334 del imCln
00335 regstate=True
00336
00337 print fmt % lname[0], "%4i %20s %20s" % stats[0]
00338
00339 for i in range(1,len(lname)):
00340 if stats[i][0]>0:
00341 if tests[i-1]['mode']=='velocity':
00342 if tests[i-1].has_key('veltype'):
00343 vtype=tests[i-1]['veltype']
00344 else:
00345 vtype='radio'
00346 v0=qa.convert(stats[i][1],'km/s')['value']
00347 v1=v0+qa.convert(stats[i][2],'km/s')['value']
00348 if vtype=='radio':
00349 f0 = reffreq_ms * ( 1 - v0/299792.458)
00350 f1 = reffreq_ms * ( 1 - v1/299792.458)
00351 else:
00352 f0 = reffreq_ms / ( 1 + v0/299792.458)
00353 f1 = reffreq_ms / ( 1 + v1/299792.458)
00354 w=f1-f0
00355 elif tests[i-1]['mode']=='channel':
00356 if type(stats[i][1])==str:
00357 f0=qa.convert(stats[i][1],'kHz')['value']
00358 w=qa.convert(stats[i][2],'kHz')['value']
00359 else:
00360 f0=ch0_ms + wid_ms*stats[i][1]
00361 w=wid_ms*stats[i][2]
00362 else:
00363 if type(stats[i][1])==str:
00364 f0 = qa.convert(stats[i][1],'kHz')['value']
00365 w = qa.convert(stats[i][2],'kHz')['value']
00366 else:
00367 f0=ch0_ms + wid_ms*stats[i][1]
00368 w=wid_ms*stats[i][2]
00369 print fmt % lname[i], "%4i ch %15f %16fkHz" % (stats[i][0], (f0-ch0_ms)/wid_ms, w)
00370 print >> logfile, fmt % lname[i], "%4i ch %15f %16fkHz" % (stats[i][0], (f0-ch0_ms)/wid_ms, w)
00371 else:
00372 print fmt % lname[i], " FAIL"
00373 print >> logfile, fmt % lname[i], " FAIL"
00374 else:
00375 print fmt % lname[0], "%15.7f %10.7f %10.7f %10.7f %4i %16.7f %16.7f" % stats[0]
00376 print >> logfile, fmt % lname[0], "%15.7f %10.7f %10.7f %10.7f %4i %16.7f %16.7f" % stats[0]
00377 for i in range(1,len(lname)):
00378 foo = pl.array(stats[i])
00379 if stats[i][4]>0:
00380 foo[0] = (foo[0]-ch0_ms)/wid_ms
00381 foo[5] = (foo[5]-ch0_ms)/wid_ms
00382 foo[6] = (foo[6]-fit_ms)/wid_ms
00383 print fmt % lname[i], "%15.7f %10.7f %10.7f %10.7f %4i %16.7f %16.7f" % (foo[0],foo[1],foo[2],foo[3],foo[4],foo[5],foo[6])
00384 print >> logfile, fmt % lname[i], "%15.7f %10.7f %10.7f %10.7f %4i %16.7f %16.7f" % (foo[0],foo[1],foo[2],foo[3],foo[4],foo[5],foo[6])
00385 else:
00386 print fmt % lname[i], " FAIL"
00387 print >> logfile, fmt % lname[i], " FAIL"
00388
00389
00390
00391 regstate=True
00392
00393
00394
00395 tol=0.005
00396
00397 tests=['ch0','ch0-ch1','width','chn-ch(n-1)','nchan','chn','fit']
00398 for run in range(1,len(lname)-1):
00399 for te in range(len(tests)):
00400 adiff=abs(stats[run][te]-stats[0][te])/stats[0][te]
00401 if tests[te]=='width' or tests[te]=='ch0-ch1' or tests[te]=='chn-ch(n-1)':
00402 if sname[run]=='nvel' or sname[run]=='frm':
00403 adiff=abs(-stats[run][te]-stats[0][te])/stats[0][te]
00404 if adiff < tol:
00405 print >> logfile, "* Passed %10s %-10s test, got %-11.5g expected %-11.5g" % (sname[run],tests[te],stats[run][te],stats[0][te])
00406 else:
00407 print >> logfile, "* FAILED %10s %-10s test, got %-11.5g expected %-11.5g " % (sname[run],tests[te],stats[run][te],stats[0][te])
00408 regstate = False
00409
00410
00411
00412
00413 print >> logfile,'---'
00414 if regstate:
00415 print >> logfile, 'Passed',
00416 print ''
00417 print 'Regression PASSED'
00418 print ''
00419 else:
00420 print >> logfile, 'FAILED',
00421 print ''
00422 print 'Regression FAILED'
00423 print ''
00424 print >> logfile, 'regression test for clean chan/spw'
00425 print >>logfile,'---'
00426 print >>logfile,'*********************************'
00427
00428 endTime = time.time()
00429 endProc = time.clock()
00430
00431 print >>logfile,''
00432 print >>logfile,'********** Benchmarking **************'
00433 print >>logfile,''
00434 print >>logfile,'Total wall clock time was: %8.3f s.' % (endTime - startTime)
00435 print >>logfile,'Total CPU time was: %8.3f s.' % (endProc - startProc)
00436 print >>logfile,'Wall processing rate was: %8.3f MB/s.' % (17896.0 /
00437 (endTime - startTime))
00438
00439
00440 logfile.close()
00441
00442
00443 print '--Finished clean chan/spw, written to '+outfile+' --'