00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 import copy
00023
00024 myname = 'cvel_regression'
00025
00026
00027 dataset_name_orig = 'W3OH_MC.UVFITS'
00028
00029
00030 mydict = locals()
00031 if mydict.has_key("dataset_name"):
00032 dataset_name_orig = mydict["dataset_name"]
00033
00034 def isnear(a,b,p):
00035 print " ", a, b
00036 if(a==b):
00037 print " exactly equal"
00038 return True
00039 dev = abs(a-b)
00040 print " deviation = ", dev
00041 if(dev<=p):
00042 return True
00043 return False
00044
00045 def isrnear(a,b,p):
00046 print " ", a, b
00047 if(a==b):
00048 print " exactly equal"
00049 return True
00050 rdev = abs(a-b)/abs(max(a,b))
00051 print " relative deviation = ", rdev
00052 if(rdev<=p):
00053 return True
00054 return False
00055
00056
00057
00058
00059 freqmodestart = { 'TOPO': '1.665627437e+09Hz',
00060 'LSRK': '1.665621980e+09Hz',
00061 'LSRD': '1.665630426e+09Hz',
00062 'BARY': '1.665644446e+09Hz',
00063 'GALACTO': '1.664750775e+09Hz',
00064 'LGROUP': '1.664162945e+09Hz',
00065 'CMB': '1.666500334e+09Hz'
00066 }
00067
00068
00069
00070 dohanning = { 'TOPO': False,
00071 'LSRK': True,
00072 'LSRD': False,
00073 'BARY': False,
00074 'GALACTO': False,
00075 'LGROUP': False,
00076 'CMB': False
00077 }
00078
00079
00080
00081 freqmodewidth = { 'TOPO': '1.52588e+03Hz',
00082 'LSRK': '1.52588e+03Hz',
00083 'LSRD': '1.52589e+03Hz',
00084 'BARY': '1.52590e+03Hz',
00085 'GALACTO': '1.52508e+03Hz',
00086 'LGROUP': '1.52454e+03Hz',
00087 'CMB': '1.52668e+03Hz'
00088 }
00089
00090
00091
00092 freqmodenchan = { 'TOPO': 45,
00093 'LSRK': 45,
00094 'LSRD': 45,
00095 'BARY': 45,
00096 'GALACTO': 45,
00097 'LGROUP': 45,
00098 'CMB': 45
00099 }
00100
00101
00102
00103 peakchan = '20'
00104 otherchan1 = '5'
00105 otherchan2 = '11'
00106 otherchan3 = '19'
00107 otherchan4 = '28'
00108
00109 testregion = '128,128,128,128'
00110
00111
00112 imstats = { 'TOPO': 0,'LSRK': 0, 'LSRD': 0, 'BARY': 0, 'GALACTO': 0, 'LGROUP': 0, 'CMB': 0 }
00113 mode_imstats = { peakchan: copy.deepcopy(imstats),
00114 otherchan1: copy.deepcopy(imstats),
00115 otherchan2: copy.deepcopy(imstats),
00116 otherchan3: copy.deepcopy(imstats),
00117 otherchan4: copy.deepcopy(imstats) }
00118 cvel_imstats = { 'frequency': copy.deepcopy(mode_imstats),
00119 'radio velocity': copy.deepcopy(mode_imstats),
00120 'optical velocity': copy.deepcopy(mode_imstats),
00121 'channel': copy.deepcopy(mode_imstats) }
00122 cleanonly_imstats = copy.deepcopy(cvel_imstats)
00123
00124 imvals = { 'TOPO': [],'LSRK': [], 'LSRD': [], 'BARY': [], 'GALACTO': [], 'LGROUP': [], 'CMB': [] }
00125 cvel_imvals = { 'frequency': copy.deepcopy(imvals),
00126 'radio velocity': copy.deepcopy(imvals),
00127 'optical velocity': copy.deepcopy(imvals),
00128 'channel': copy.deepcopy(imvals) }
00129 cleanonly_imvals = copy.deepcopy(cvel_imvals)
00130 cvel_chanfreqs = copy.deepcopy(cvel_imvals)
00131 cleanonly_chanfreqs = copy.deepcopy(cvel_imvals)
00132
00133
00134
00135
00136 dataset_name = dataset_name_orig+".ms"
00137
00138 importuvfits(fitsfile=dataset_name_orig, vis=dataset_name)
00139
00140 os.system('cp -RL '+dataset_name+' input.ms')
00141 os.system('chmod -R u+w input.ms')
00142 os.system('cp -RL '+dataset_name+' input2.ms')
00143 os.system('chmod -R u+w input2.ms')
00144 clean_inputvis_local_copy = 'input.ms'
00145
00146 clean_inputvis_local_copy2 = 'input2.ms'
00147 hanningsmooth(vis=clean_inputvis_local_copy2)
00148
00149
00150
00151
00152
00153 frames_to_do = ['TOPO','LSRK', 'LSRD', 'BARY', 'GALACTO', 'LGROUP', 'CMB']
00154
00155
00156
00157
00158
00159
00160
00161 for frame in frames_to_do:
00162
00163 restfrq = 1.6654018E9
00164 restfreqstr = str(restfrq)+'Hz'
00165
00166
00167
00168 outvis = 'W3OH_'+frame+'_cvel_freq.ms'
00169 os.system('rm -rf '+outvis)
00170
00171 casalog.post(outvis, 'INFO')
00172
00173 cvel(vis=dataset_name, outputvis=outvis,
00174 mode='frequency',nchan=freqmodenchan[frame],
00175 start=freqmodestart[frame],
00176 width=freqmodewidth[frame],
00177 interpolation='linear',
00178 phasecenter='',
00179 outframe=frame,
00180 hanning = dohanning[frame])
00181
00182 invis = 'W3OH_'+frame+'_cvel_freq.ms'
00183 iname = 'W3OH_'+frame+'_cvel_freq_clean'
00184 os.system('rm -rf '+iname+'.*')
00185
00186 casalog.post(iname, 'INFO')
00187
00188 clean(vis=invis,
00189 imagename=iname,
00190 field='',spw='',
00191 cell=[0.01,0.01],imsize=[256,256],
00192 stokes='I',
00193 mode='frequency',nchan=freqmodenchan[frame],
00194 start=freqmodestart[frame],
00195 width=freqmodewidth[frame],
00196 interpolation='linear',
00197 psfmode='clark',imagermode='csclean',
00198 scaletype='SAULT',
00199 niter=0,threshold='1.5mJy',
00200 restfreq=restfreqstr,
00201 phasecenter='',
00202 mask='',
00203 weighting='briggs',
00204 interactive=F,
00205 minpb=0.3,pbcor=F)
00206
00207 cvel_imstats['frequency'][peakchan][frame] = imstat(iname+'.image', box=testregion, chans=peakchan)
00208 cvel_imstats['frequency'][otherchan1][frame] = imstat(iname+'.image', box=testregion, chans=otherchan1)
00209 cvel_imstats['frequency'][otherchan2][frame] = imstat(iname+'.image', box=testregion, chans=otherchan2)
00210 cvel_imstats['frequency'][otherchan3][frame] = imstat(iname+'.image', box=testregion, chans=otherchan3)
00211 cvel_imstats['frequency'][otherchan4][frame] = imstat(iname+'.image', box=testregion, chans=otherchan4)
00212 cvel_imvals['frequency'][frame] = imval(iname+'.image', box=testregion)
00213
00214 ia.open(iname+'.image')
00215 chlist = range(freqmodenchan[frame])
00216 fqlist = []
00217
00218 for i in chlist:
00219 myw = ia.toworld([128,128,0,i],'n')
00220 fqlist.append(myw['numeric'][3])
00221 ia.close()
00222 cvel_chanfreqs['frequency'][frame] = fqlist
00223
00224
00225
00226 iname = 'W3OH_'+frame+'_freq_clean'
00227 os.system('rm -rf '+iname+'.*')
00228
00229 casalog.post(iname, 'INFO')
00230
00231 cvis = clean_inputvis_local_copy
00232 if(dohanning[frame]):
00233 casalog.post('Will Hanning smooth before cleaning ...', 'INFO')
00234 cvis = clean_inputvis_local_copy2
00235
00236 clean(vis=cvis,
00237 imagename=iname,
00238 field='', spw='',
00239 cell=[0.01,0.01],imsize=[256,256],
00240 stokes='I',
00241 mode='frequency',nchan=freqmodenchan[frame],
00242 start=freqmodestart[frame],
00243 width=freqmodewidth[frame],
00244 outframe=frame,
00245 interpolation='linear',
00246 psfmode='clark',imagermode='csclean',
00247 scaletype='SAULT',
00248 niter=0,threshold='1.5mJy',
00249 restfreq=restfreqstr,
00250 phasecenter='',
00251 mask='',
00252 weighting='briggs',
00253 interactive=F,
00254 minpb=0.3,pbcor=F)
00255
00256 cleanonly_imstats['frequency'][peakchan][frame] = imstat(iname+'.image', box=testregion, chans=peakchan)
00257 cleanonly_imstats['frequency'][otherchan1][frame] = imstat(iname+'.image', box=testregion, chans=otherchan1)
00258 cleanonly_imstats['frequency'][otherchan2][frame] = imstat(iname+'.image', box=testregion, chans=otherchan2)
00259 cleanonly_imstats['frequency'][otherchan3][frame] = imstat(iname+'.image', box=testregion, chans=otherchan3)
00260 cleanonly_imstats['frequency'][otherchan4][frame] = imstat(iname+'.image', box=testregion, chans=otherchan4)
00261 cleanonly_imvals['frequency'][frame] = imval(iname+'.image', box=testregion)
00262
00263 ia.open(iname+'.image')
00264 chlist = range(freqmodenchan[frame])
00265 fqlist = []
00266
00267 for i in chlist:
00268 myw = ia.toworld([128,128,0,i],'n')
00269 fqlist.append(myw['numeric'][3])
00270 ia.close()
00271 cleanonly_chanfreqs['frequency'][frame] = fqlist
00272
00273
00274
00275
00276 f1 = qa.quantity(freqmodestart[frame])['value']
00277 f2 = f1+qa.quantity(freqmodewidth[frame])['value']
00278
00279 vrads = (restfrq-f1)/restfrq * 2.99792E8
00280 vradstart = str(vrads)+'m/s'
00281 vradw = (restfrq-f2)/restfrq * 2.99792E8 - vrads
00282 vradwidth = str(vradw)+'m/s'
00283
00284 outvis = 'W3OH_'+frame+'_cvel_vrad.ms'
00285 os.system('rm -rf '+outvis)
00286
00287 casalog.post(outvis, 'INFO')
00288
00289 cvel(vis=dataset_name,outputvis=outvis,
00290 mode='velocity',nchan=freqmodenchan[frame],
00291 start=vradstart,
00292 width=vradwidth,
00293 interpolation='linear',
00294 phasecenter='',
00295 restfreq=restfreqstr,
00296 outframe=frame,
00297 hanning=dohanning[frame])
00298
00299 invis = 'W3OH_'+frame+'_cvel_vrad.ms'
00300 iname = 'W3OH_'+frame+'_cvel_vrad_clean'
00301 os.system('rm -rf '+iname+'.*')
00302
00303 casalog.post(iname, 'INFO')
00304
00305 clean(vis=invis,
00306 imagename=iname,
00307 field='',spw='',
00308 cell=[0.01,0.01],imsize=[256,256],
00309 stokes='I',
00310 mode='velocity',nchan=freqmodenchan[frame],
00311 start=vradstart,
00312 width=vradwidth,
00313 interpolation='linear',
00314 psfmode='clark',imagermode='csclean',
00315 scaletype='SAULT',
00316 niter=0,threshold='1.5mJy',
00317 restfreq=restfreqstr,
00318 phasecenter='',
00319 mask='',
00320 weighting='briggs',
00321 interactive=F,
00322 minpb=0.3,pbcor=F)
00323
00324 cvel_imstats['radio velocity'][peakchan][frame] = imstat(iname+'.image', box=testregion, chans=peakchan)
00325 cvel_imstats['radio velocity'][otherchan1][frame] = imstat(iname+'.image', box=testregion, chans=otherchan1)
00326 cvel_imstats['radio velocity'][otherchan2][frame] = imstat(iname+'.image', box=testregion, chans=otherchan2)
00327 cvel_imstats['radio velocity'][otherchan3][frame] = imstat(iname+'.image', box=testregion, chans=otherchan3)
00328 cvel_imstats['radio velocity'][otherchan4][frame] = imstat(iname+'.image', box=testregion, chans=otherchan4)
00329 cvel_imvals['radio velocity'][frame] = imval(iname+'.image', box=testregion)
00330
00331 ia.open(iname+'.image')
00332 chlist = range(freqmodenchan[frame])
00333 fqlist = []
00334
00335 for i in chlist:
00336 myw = ia.toworld([128,128,0,i],'n')
00337 fqlist.append(myw['numeric'][3])
00338 ia.close()
00339 cvel_chanfreqs['radio velocity'][frame] = fqlist
00340
00341
00342
00343 iname = 'W3OH_'+frame+'_vrad_clean'
00344 os.system('rm -rf '+iname+'.*')
00345
00346 casalog.post(iname, 'INFO')
00347
00348 cvis = clean_inputvis_local_copy
00349 if(dohanning[frame]):
00350 casalog.post('Will Hanning smooth before cleaning ...', 'INFO')
00351 cvis = clean_inputvis_local_copy2
00352
00353 clean(vis=cvis,
00354 imagename=iname,
00355 field='', spw='',
00356 cell=[0.01,0.01],imsize=[256,256],
00357 stokes='I',
00358 mode='velocity',nchan=freqmodenchan[frame],
00359 start=vradstart,
00360 width=vradwidth,
00361 outframe=frame,
00362 interpolation='linear',
00363 psfmode='clark',imagermode='csclean',
00364 scaletype='SAULT',
00365 niter=0,threshold='1.5mJy',
00366 restfreq=restfreqstr,
00367 phasecenter='',
00368 mask='',
00369 weighting='briggs',
00370 interactive=F,
00371 minpb=0.3,pbcor=F)
00372
00373 cleanonly_imstats['radio velocity'][peakchan][frame] = imstat(iname+'.image', box=testregion, chans=peakchan)
00374 cleanonly_imstats['radio velocity'][otherchan1][frame] = imstat(iname+'.image', box=testregion, chans=otherchan1)
00375 cleanonly_imstats['radio velocity'][otherchan2][frame] = imstat(iname+'.image', box=testregion, chans=otherchan2)
00376 cleanonly_imstats['radio velocity'][otherchan3][frame] = imstat(iname+'.image', box=testregion, chans=otherchan3)
00377 cleanonly_imstats['radio velocity'][otherchan4][frame] = imstat(iname+'.image', box=testregion, chans=otherchan4)
00378 cleanonly_imvals['radio velocity'][frame] = imval(iname+'.image', box=testregion)
00379
00380 ia.open(iname+'.image')
00381 chlist = range(freqmodenchan[frame])
00382 fqlist = []
00383
00384 for i in chlist:
00385 myw = ia.toworld([128,128,0,i],'n')
00386 fqlist.append(myw['numeric'][3])
00387 ia.close()
00388 cleanonly_chanfreqs['radio velocity'][frame] = fqlist
00389
00390
00391
00392 lambda0 = 2.99792E8/restfrq
00393 lambda1 = 2.99792E8/f1
00394 lambda2 = 2.99792E8/f2
00395 vopts = (lambda1-lambda0)/lambda0 * 2.99792E8
00396 voptw = (lambda2-lambda0)/lambda0 * 2.99792E8 - vopts
00397 voptstart = str(vopts)+'m/s'
00398 voptwidth = str(voptw)+'m/s'
00399
00400 outvis = 'W3OH_'+frame+'_cvel_vopt.ms'
00401 os.system('rm -rf '+outvis)
00402
00403 casalog.post(outvis, 'INFO')
00404
00405 cvel(vis=dataset_name, outputvis=outvis,
00406 mode='velocity',nchan=freqmodenchan[frame],
00407 start=voptstart,
00408 width=voptwidth,
00409 interpolation='linear',
00410 phasecenter='',
00411 restfreq=restfreqstr,
00412 outframe=frame,
00413 veltype='optical',
00414 hanning=dohanning[frame])
00415
00416 invis = 'W3OH_'+frame+'_cvel_vopt.ms'
00417 iname = 'W3OH_'+frame+'_cvel_vopt_clean'
00418 os.system('rm -rf '+iname+'.*')
00419
00420 casalog.post(iname, 'INFO')
00421
00422 clean(vis=invis,
00423 imagename=iname,
00424 field='',spw='',
00425 cell=[0.01,0.01],imsize=[256,256],
00426 stokes='I',
00427 mode='velocity',nchan=freqmodenchan[frame],
00428 start=voptstart,
00429 width=voptwidth,
00430 interpolation='linear',
00431 psfmode='clark',imagermode='csclean',
00432 scaletype='SAULT',
00433 niter=0,threshold='1.5mJy',
00434 restfreq=restfreqstr,
00435 phasecenter='',
00436 mask='',
00437 weighting='briggs',
00438 interactive=F,
00439 minpb=0.3,pbcor=F,
00440 veltype='optical')
00441
00442 cvel_imstats['optical velocity'][peakchan][frame] = imstat(iname+'.image', box=testregion, chans=peakchan)
00443 cvel_imstats['optical velocity'][otherchan1][frame] = imstat(iname+'.image', box=testregion, chans=otherchan1)
00444 cvel_imstats['optical velocity'][otherchan2][frame] = imstat(iname+'.image', box=testregion, chans=otherchan2)
00445 cvel_imstats['optical velocity'][otherchan3][frame] = imstat(iname+'.image', box=testregion, chans=otherchan3)
00446 cvel_imstats['optical velocity'][otherchan4][frame] = imstat(iname+'.image', box=testregion, chans=otherchan4)
00447 cvel_imvals['optical velocity'][frame] = imval(iname+'.image', box=testregion)
00448
00449 ia.open(iname+'.image')
00450 chlist = range(freqmodenchan[frame])
00451 fqlist = []
00452
00453 for i in chlist:
00454 myw = ia.toworld([128,128,0,i],'n')
00455 fqlist.append(myw['numeric'][3])
00456 ia.close()
00457 cvel_chanfreqs['optical velocity'][frame] = fqlist
00458
00459
00460
00461 iname = 'W3OH_'+frame+'_vopt_clean'
00462 os.system('rm -rf '+iname+'.*')
00463
00464 casalog.post(iname, 'INFO')
00465
00466 cvis = clean_inputvis_local_copy
00467 if(dohanning[frame]):
00468 casalog.post('Will Hanning smooth before cleaning ...', 'INFO')
00469 cvis = clean_inputvis_local_copy2
00470
00471 clean(vis=cvis,
00472 imagename=iname,
00473 field='', spw='',
00474 cell=[0.01,0.01],imsize=[256,256],
00475 stokes='I',
00476 mode='velocity',nchan=freqmodenchan[frame],
00477 start=voptstart,
00478 width=voptwidth,
00479 outframe=frame,
00480 interpolation='linear',
00481 psfmode='clark',imagermode='csclean',
00482 scaletype='SAULT',
00483 niter=0,threshold='1.5mJy',
00484 restfreq=restfreqstr,
00485 phasecenter='',
00486 mask='',
00487 weighting='briggs',
00488 interactive=F,
00489 minpb=0.3,pbcor=F,
00490 veltype='optical')
00491
00492 cleanonly_imstats['optical velocity'][peakchan][frame] = imstat(iname+'.image', box=testregion, chans=peakchan)
00493 cleanonly_imstats['optical velocity'][otherchan1][frame] = imstat(iname+'.image', box=testregion, chans=otherchan1)
00494 cleanonly_imstats['optical velocity'][otherchan2][frame] = imstat(iname+'.image', box=testregion, chans=otherchan2)
00495 cleanonly_imstats['optical velocity'][otherchan3][frame] = imstat(iname+'.image', box=testregion, chans=otherchan3)
00496 cleanonly_imstats['optical velocity'][otherchan4][frame] = imstat(iname+'.image', box=testregion, chans=otherchan4)
00497 cleanonly_imvals['optical velocity'][frame] = imval(iname+'.image', box=testregion)
00498
00499 ia.open(iname+'.image')
00500 chlist = range(freqmodenchan[frame])
00501 fqlist = []
00502
00503 for i in chlist:
00504 myw = ia.toworld([128,128,0,i],'n')
00505 fqlist.append(myw['numeric'][3])
00506 ia.close()
00507 cleanonly_chanfreqs['optical velocity'][frame] = fqlist
00508
00509
00510
00511
00512
00513
00514 passed = True
00515 tolerance = 0.001
00516 rtolerance = 0.07
00517 numpoints = 0.
00518 avdev = 0.
00519 maxdev = 0.
00520 maxdevat = " "
00521 problems = 0
00522 for frame in frames_to_do:
00523 for mode in ['frequency', 'radio velocity', 'optical velocity']:
00524
00525 sparr = cleanonly_imvals[mode][frame]['data']
00526 sparr_cvel = cvel_imvals[mode][frame]['data']
00527 fqarr = pl.array(cleanonly_chanfreqs[mode][frame]) * 1E6
00528 fqarr_cvel = pl.array(cvel_chanfreqs[mode][frame]) * 1E6
00529
00530 pl.plot(fqarr, sparr, 'b', linewidth=1.0)
00531 pl.plot(fqarr_cvel, sparr_cvel, 'r', linewidth=1.0)
00532
00533 pl.xlabel(frame+' Frequency (MHz)')
00534 pl.ylabel('Flux Density (Jy/beam)')
00535 pl.title('W3OH '+frame+', '+mode+'-mode cvel+clean = red, clean-only = blue')
00536 pl.savefig('testcvelclean'+frame+mode[0]+'.png',format='png')
00537 pl.close()
00538
00539 for chan in mode_imstats.keys():
00540 isok = true
00541 c1 = cleanonly_imstats[mode][chan][frame]['max']
00542 c2 = cvel_imstats[mode][chan][frame]['max']
00543 print "Testing ", frame, ", ", mode, ", Hanning ", dohanning[frame], ", box ", testregion, ", channel ", chan, " ..."
00544 if(abs(c1-c2) > maxdev):
00545 maxdev = abs(c1-c2)
00546 maxdevat = mode+" mode for output frame "+frame\
00547 +":\n cvel+clean finds max flux in channel "+str(chan)+" to be "+str(c2)\
00548 +"\n clean-only finds max flux in channel "+str(chan)+" to be "+str(c1)
00549 if not (isnear(c1,c2, tolerance) or isrnear(c1,c2, rtolerance)):
00550 print " ** Problem in ", mode, " mode for output frame ", frame, ":"
00551 print " cvel+clean finds max flux in channel ", chan, " to be ", c2
00552 print " clean-only finds max flux in channel ", chan, " to be ", c1
00553 passed = False
00554 isok = False
00555 problems +=1
00556
00557 avdev += abs(c1-c2)
00558 numpoints += 1.
00559
00560 s1 = cleanonly_imstats[mode][chan][frame]['maxposf']
00561 s2 = cvel_imstats[mode][chan][frame]['maxposf']
00562 if(not s1 == s2):
00563 print " ** Problem in ", mode, " mode for output frame ", frame, ":"
00564 print " cvel+clean finds world coordinates for channel ", chan, " to be ", s2
00565 print " clean-only finds world coordinates for channel ", chan, " to be ", s1
00566 passed = False
00567 isok = False
00568 problems +=1
00569 else:
00570 print " World coordinates identical == ", s2
00571
00572 if isok:
00573 print "... OK"
00574
00575 if(numpoints > 0.):
00576 print numpoints, " spectral points compared, average deviation = ", avdev/numpoints, " Jy"
00577 print " maximum deviation = ", maxdev, " in ", maxdevat
00578
00579 if passed:
00580 print "PASSED"
00581 else:
00582 print "Execution successful but found ", problems, " issues in analysis of results."
00583 print "FAILED"
00584 raise