def moment_maps(imagename, chans, mask, thres, output_mom=[0, 1, 2, 8]): """ - 8 """ # modify the header of the mask bmaj = imhead(imagename, "list")["beammajor"]["value"] bmin = imhead(imagename, "list")["beamminor"]["value"] bpa = imhead(imagename, "list")["beampa"]["value"] imhead(mask, "put", "beammajor", str(bmaj) + "arcsec") imhead(mask, "put", "beamminor", str(bmin) + "arcsec") imhead(mask, "put", "beampa", str(bpa) + "deg") # create masked cube outfile = imagename + ".masked" os.system("rm -rf " + outfile) immath(imagename=[imagename, mask], mode="evalexpr", expr="iif(IM1 >= 1.0, IM0, 0.0)", outfile=outfile) #create moment maps using the masked cube for i in range(len(output_mom)): outfile = imagename + ".moment" + str(output_mom[i]) os.system("rm -rf " + outfile) immoments(imagename=imagename + ".masked", moments=[output_mom[i]], chans=chans, includepix=[thres, 100000.], outfile=outfile)
def _restore(self, model_fits="", residual_ms="", restored_image="restored"): qa = casacore.casac.quanta ia = casacore.casac.image residual_image = "residual" os.system("rm -rf *.log *.last " + residual_image + ".* mod_out convolved_mod_out convolved_mod_out.fits " + restored_image + " " + restored_image + ".fits") importfits(imagename="model_out", fitsimage=self.model_fits) shape = imhead(imagename="model_out", mode="get", hdkey="shape") pix_num = shape[0] cdelt = imhead(imagename="model_out", mode="get", hdkey="cdelt2") cdelta = qa.convert(v=cdelt, outunit="arcsec") cdeltd = qa.convert(v=cdelt, outunit="deg") pix_size = str(cdelta['value']) + "arcsec" tclean(vis=residual_ms, imagename=residual_image, specmode='mfs', deconvolver='hogbom', niter=0, stokes=self.stokes, weighting='briggs', nterms=1, robust=self.robust, imsize=[self.M, self.N], cell=self.cell, datacolumn='RESIDUAL') exportfits(imagename=residual_image + ".image", fitsimage=residual_image + ".image.fits", overwrite=True, history=False) ia.open(infile=residual_image + ".image") rbeam = ia.restoringbeam() ia.done() ia.close() bmaj = imhead(imagename=residual_image + ".image", mode="get", hdkey="beammajor") bmin = imhead(imagename=residual_image + ".image", mode="get", hdkey="beamminor") bpa = imhead(imagename=residual_image + ".image", mode="get", hdkey="beampa") minor = qa.convert(v=bmin, outunit="deg") pa = qa.convert(v=bpa, outunit="deg") ia.open(infile="model_out") ia.convolve2d(outfile="convolved_model_out", axes=[ 0, 1], type='gauss', major=bmaj, minor=bmin, pa=bpa) ia.done() ia.close() exportfits(imagename="convolved_model_out", fitsimage="convolved_model_out.fits", overwrite=True, history=False) ia.open(infile="convolved_model_out.fits") ia.setrestoringbeam(beam=rbeam) ia.done() ia.close() imagearr = ["convolved_model_out.fits", residual_image + ".image.fits"] immath(imagename=imagearr, expr=" (IM0 + IM1) ", outfile=restored_image) exportfits(imagename=restored_image, fitsimage=restored_image + ".fits", overwrite=True, history=False) return residual_image + ".image.fits", restored_image + ".fits"
def convert_beam_to_str(infile, beam): ### str to beam beam = beam * u.arcsec # arcsec beamarea = (2 * np.pi / (8 * np.log(2))) * (beam**2).to(u.sr) # str os.system("rm -rf " + infile.replace(".beam", ".smooth")) immath(imagename=infile, outfile=infile.replace(".beam", ".smooth"), expr="IM0*1e6*" + str(beamarea.value))
def createmask(imagename, thres, outmask): """ for moment map creation """ os.system("rm -rf " + outmask) immath(imagename=imagename, mode="evalexpr", expr="iif(IM0 >= " + str(thres) + ", 1.0, 0.0)", outfile=outmask)
def moment_maps(dir_data, imagename, chans, mask, thres): #modify the header of the mask maskname = dir_data + "mask2.image" os.system("rm -rf " + maskname) os.system("cp -r " + dir_data + mask + " " + maskname) bmaj = imhead(imagename=imagename, mode="list")["beammajor"]["value"] bmin = imhead(imagename=imagename, mode="list")["beamminor"]["value"] bpa = imhead(imagename=imagename, mode="list")["beampa"]["value"] imhead(imagename=maskname, mode="put", hdkey="beammajor", hdvalue=str(bmaj) + "arcsec") imhead(imagename=maskname, mode="put", hdkey="beamminor", hdvalue=str(bmin) + "arcsec") imhead(imagename=maskname, mode="put", hdkey="beampa", hdvalue=str(bpa) + "deg") #create masked cube outfile = imagename + ".masked" os.system("rm -rf " + outfile) immath(imagename=[imagename, maskname], mode="evalexpr", expr="iif(IM1 >= 1.0, IM0, 0.0)", outfile=outfile) #create moment maps using the masked cube outfile = imagename + ".moment0" os.system("rm -rf " + outfile) immoments(imagename=imagename + ".masked", moments=[0], chans=chans, includepix=[thres, 100000.], outfile=outfile) outfile = imagename + ".moment1" os.system("rm -rf " + outfile) immoments(imagename=imagename + ".masked", moments=[1], chans=chans, includepix=[thres, 100000.], outfile=outfile) outfile = imagename + ".moment2" os.system("rm -rf " + outfile) immoments(imagename=imagename + ".masked", moments=[2], chans=chans, includepix=[thres, 100000.], outfile=outfile) outfile = imagename + ".moment8" os.system("rm -rf " + outfile) immoments(imagename=imagename + ".masked", moments=[8], chans=chans, includepix=[thres, 100000.], outfile=outfile)
def tscreatemask(imagename, thres, outmask): """ myim03 """ os.system("rm -rf " + outmask) immath(imagename=imagename, mode="evalexpr", expr="iif(IM0 >= " + str(thres) + ", 1.0, 0.0)", outfile=outmask) imhead(imagename=outmask, mode="del", hdkey="beammajor")
def easy_K2Jy(imagename, synsbeam, freq): """ - 2 """ imhead(imagename=imagename, mode="put", hdkey="bunit", hdvalue="Jy/beam") expr_coeff = synsbeam * synsbeam * freq * freq / 1.222e6 os.system("rm -rf " + imagename + ".jy") immath(imagename=imagename, mode="evalexpr", expr="IM0*" + str(expr_coeff), outfile=imagename + ".jy") os.system("rm -rf " + imagename)
def createmask(dir_data, imagename, thres, outmask="mask1.image", pixelmin=5.): #create mask with thres outfile = dir_data + outmask os.system("rm -rf " + outfile) immath(imagename=dir_data + imagename, mode="evalexpr", expr="iif(IM0 >= " + str(thres) + ", 1.0, 0.0)", outfile=outfile) imhead(imagename=outfile, mode="del", hdkey="beammajor") makemask(mode="copy", inpimage=outfile, inpmask=outfile, output=outfile + ":mask0", overwrite=True)
def makeratio(im0, im1, name_im0, name_im1, cliplevel1, factor): os.system("rm -rf " + dir_proj + "image_ratio/ratio_" + name_im0 + "_" + name_im1 + ".image_tmp") peak1 = imstat(im1)["max"][0] os.system("rm -rf " + im1 + ".complete") immath(imagename=im1, expr="iif(IM0 >= " + str(peak1 * cliplevel1) + ", IM0, 0.0)", outfile=im1 + ".complete") ratio_image = dir_proj + "image_ratio/ratio_" + name_im0 + "_" + name_im1 + ".image" os.system("rm -rf " + ratio_image) immath(imagename=[im0, im1 + ".complete"], expr="iif(IM0>=0,IM0/IM1/" + str(factor) + ", 0.0)", outfile=ratio_image) os.system("rm -rf " + im1 + ".complete")
def tsJy2Kelvin(imagename, outfile): """ myim01 """ myunit = imhead(imagename, mode="list")["bunit"] if myunit == "Jy/beam": os.system("mv " + imagename + " " + outfile) else: headlist = imhead(imagename, mode="list") beamsize = headlist["beammajor"]["value"] restfreq = headlist["restfreq"][0] / 1e9 factor = 1.222e6 / beamsize / beamsize / restfreq / restfreq os.system("rm -rf " + outfile) immath(imagename=imagename, expr="IM0/" + str(factor), outfile=outfile) imhead(outfile, mode="put", hdkey="bunit", hdvalue="Jy/beam") os.system("rm -rf " + imagename)
def convert_str_to_beam(infile, beam): """ round beam diameter in arcsec """ ### import casa images if ".fits" in infile: done = glob.glob(infile.replace(".fits", ".image")) if not done: eazy_importfits(infile, defaultaxes=False) imagename = infile.replace(".fits", ".image") else: imagename = infile.replace(".fits", ".image") ### str to beam beam = beam * u.arcsec # arcsec beamarea = (2 * np.pi / (8 * np.log(2))) * (beam**2).to(u.sr) # str os.system("rm -rf " + imagename + ".beam") immath(imagename=imagename, outfile=imagename + ".beam", expr="IM0*1e6*" + str(beamarea.value)) imhead(imagename=imagename + ".beam", mode="put", hdkey="beammajor", hdvalue=str(beam.value) + "arcsec") imhead(imagename=imagename + ".beam", mode="put", hdkey="beamminor", hdvalue=str(beam.value) + "arcsec") imhead(imagename=imagename + ".beam", mode="put", hdkey="beampa", hdvalue="0.0deg") imhead(imagename=imagename + ".beam", mode="put", hdkey="bunit", hdvalue="Jy/beam")
def line_ratio(dir_data, im1, im2, outfile, diff, mask=[]): """ """ os.system("rm -rf " + dir_data + outfile) os.system("rm -rf " + dir_data + outfile + ".beforemask") if not mask: immath(imagename=[dir_data + im1, dir_data + im2], mode="evalexpr", expr="IM0/IM1/" + diff, outfile=dir_data + outfile) else: #modify the header of the mask maskname = dir_data + mask bmaj = imhead(imagename=dir_data + im1, mode="list")["beammajor"]["value"] bmin = imhead(imagename=dir_data + im1, mode="list")["beamminor"]["value"] bpa = imhead(imagename=dir_data + im1, mode="list")["beampa"]["value"] imhead(imagename=maskname, mode="put", hdkey="beammajor", hdvalue=str(bmaj) + "arcsec") imhead(imagename=maskname, mode="put", hdkey="beamminor", hdvalue=str(bmin) + "arcsec") imhead(imagename=maskname, mode="put", hdkey="beampa", hdvalue=str(bpa) + "deg") immath(imagename=[dir_data + im1, dir_data + im2], mode="evalexpr", expr="IM0/IM1/" + diff, outfile=dir_data + outfile + ".beforemask") immath(imagename=[dir_data + outfile + ".beforemask", maskname], mode="evalexpr", expr="iif(IM1 >= 1.0, IM0, 0.0)", outfile=dir_data + outfile) os.system("rm -rf " + dir_data + outfile + ".beforemask")
os.system("rm -rf " + outmask) immath(imagename=imagename, mode="evalexpr", expr="iif(IM0 >= " + str(thres) + ", 1.0, 0.0)", outfile=outmask) ##################### ### Main ##################### os.system("rm -rf " + image_co10 + ".mask") createmask(image_co10, 0.1, image_co10 + ".mask") os.system("rm -rf " + image_co10 + ".smooth") imsmooth(imagename=image_co10 + ".mask", targetres=True, major="2.1arcsec", minor="2.1arcsec", pa="0deg", outfile=image_co10 + ".smooth") createmask(image_co10 + ".smooth", 1.0, image_co10 + ".smooth.mask") os.system("rm -rf " + image_co10 + ".mask") os.system("rm -rf " + image_co10 + ".smooth") imagenames = glob.glob(dir_proj + "*/*.moment0") for i in range(len(imagenames)): immath(imagename=[imagenames[i], image_co10 + ".smooth.mask"], expr="IM0*IM1", outfile=imagenames[i] + ".masked")
def eazy_immoments(dir_proj, imagename, galname, noise, beamp, snr_mom, percent, nchan, maskname=None, myim="03"): """ myim03, myim05 use co10 mask for co10, co21 mask for co21 This is be replace by eazy_immoments_r21. """ if myim == "03": # prepare workinf directory e.g., ngc0628_co10 name_line = imagename.split(galname + "_")[1].split("_")[0] dir_image = dir_proj + "../" + galname + "_" + name_line + "/" cubeimage = dir_image + name_line + "_cube.image" os.system("mkdir " + dir_image) os.system("cp -r " + imagename + " " + cubeimage) elif myim == "05": name_line = imagename.split(galname + "_")[1].split("/")[0] dir_image = dir_proj cubeimage = imagename print("### woking on " + galname + " " + name_line + " " + beamp) if maskname == None: os.system("rm -rf " + cubeimage + ".masked") #os.system("rm -rf " + dir_image+"*.noise") os.system("rm -rf " + dir_image + "*.mask*") # imsmooth cubesmooth1 = cubeimage.replace(".image", ".smooth1") # 4.0 mJy bmaj = imhead(cubeimage, "list")["beammajor"]["value"] imsmooth( imagename=cubeimage, targetres=True, major=str(bmaj * 3.0) + "arcsec", #1.2) + "arcsec", minor=str(bmaj * 3.0) + "arcsec", #1.2) + "arcsec", pa="0deg", outfile=cubesmooth1) cubesmooth2 = cubeimage.replace(".image", ".smooth2") # 10 mJy imsmooth(imagename=cubeimage, targetres=True, major=str(bmaj * 5.0) + "arcsec", minor=str(bmaj * 5.0) + "arcsec", pa="0deg", outfile=cubesmooth2) cubesmooth3 = cubeimage.replace(".image", ".smooth3") # 10 mJy imsmooth(imagename=cubeimage, targetres=True, major=str(bmaj * 7.0) + "arcsec", minor=str(bmaj * 7.0) + "arcsec", pa="0deg", outfile=cubesmooth3) # noise noisesmooth1 = noisehist(cubesmooth1, 0.02, "test", 3.0, bins=200, thres=0.0001, plotter=False) noisesmooth2 = noisehist(cubesmooth2, 0.02, "test", 3.0, bins=200, thres=0.0001, plotter=False) noisesmooth3 = noisehist(cubesmooth3, 0.02, "test", 3.0, bins=200, thres=0.0001, plotter=False) # create mask #tscreatemask(cubeimage,noise*1.*2.,dir_image+name_line+"_mask0.image") tscreatemask(cubesmooth1, noisesmooth1 * 0.0, dir_image + name_line + "_mask1.image") tscreatemask(cubesmooth2, noisesmooth2 * 0.0, dir_image + name_line + "_mask2.image") tscreatemask(cubesmooth3, noisesmooth3 * 0.0, dir_image + name_line + "_mask3.image") immath(imagename=[ dir_image + name_line + "_mask1.image", dir_image + name_line + "_mask2.image", dir_image + name_line + "_mask3.image" ], expr="iif(IM0+IM1 >= 2.0, 1.0, 0.0)", outfile=dir_image + name_line + "_" + beamp + "_mask.image") os.system("rm -rf " + cubesmooth1) os.system("rm -rf " + cubesmooth2) os.system("rm -rf " + cubesmooth3) os.system("rm -rf " + dir_image + name_line + "_mask0.image") os.system("rm -rf " + dir_image + name_line + "_mask1.image") os.system("rm -rf " + dir_image + name_line + "_mask2.image") mask_use_here = dir_image + name_line + "_" + beamp + "_mask.image" else: mask_use_here = dir_image + name_line + "_" + beamp + "_mask.image" os.system("cp -r " + maskname + " " + mask_use_here) immath(imagename=[cubeimage, mask_use_here], expr="iif( IM0>=" + str(noise * snr_mom) + ", IM0*IM1, 0.0)", outfile=cubeimage + ".masked") #os.system("rm -rf " + mask_use_here) vch = abs(imhead(cubeimage, mode="list")["cdelt4"]) / 115.27120e9 * 299792.458 immath(imagename=cubeimage + ".masked", expr="iif( IM0>0, 1.0/" + str(vch) + ", 0.0)", outfile=cubeimage + ".maskedTF") immoments(imagename=cubeimage + ".maskedTF", moments=[0], outfile=dir_image + name_line + ".moment0.noise_tmp") # Nch beamarea_pix = tsbeam_area(dir_image + name_line + ".moment0.noise_tmp") immath( dir_image + name_line + ".moment0.noise_tmp", expr=str(vch) + "*sqrt(IM0)*" + str(noise), #+"/"+str(np.sqrt(beamarea_pix)), outfile=dir_image + name_line + "_" + beamp + ".moment0.noise") immoments( imagename=cubeimage + ".masked", moments=[0], #includepix = [noises[i]*3.0,10000.], outfile=dir_image + name_line + ".moment0_tmp") immoments( imagename=cubeimage + ".masked", moments=[1], #includepix = [noises[i]*3.0,10000.], outfile=dir_image + name_line + ".moment1_tmp") immoments( imagename=cubeimage + ".masked", moments=[8], #includepix = [noises[i]*3.0,10000.], outfile=dir_image + name_line + ".moment8_tmp") # masking peak = imstat(dir_image + name_line + ".moment0_tmp")["max"][0] # immath(imagename=[ dir_image + name_line + ".moment0_tmp", dir_image + name_line + ".moment0_tmp" ], expr="iif( IM0>=" + str(peak * percent) + ", IM1, 0.0)", outfile=dir_image + name_line + "_" + beamp + ".moment0_tmp2") immath(imagename=[ dir_image + name_line + "_" + beamp + ".moment0_tmp2", dir_image + name_line + ".moment0.noise_tmp" ], expr="iif( IM1>=" + str(nchan) + ", IM0, 0.0)", outfile=dir_image + name_line + "_" + beamp + ".moment0") # immath(imagename=[ dir_image + name_line + ".moment0_tmp", dir_image + name_line + ".moment1_tmp" ], expr="iif( IM0>=" + str(peak * percent) + ", IM1, 0.0)", outfile=dir_image + name_line + "_" + beamp + ".moment1_tmp2") immath(imagename=[ dir_image + name_line + "_" + beamp + ".moment1_tmp2", dir_image + name_line + ".moment0.noise_tmp" ], expr="iif( IM1>=" + str(nchan) + ", IM0, 0.0)", outfile=dir_image + name_line + "_" + beamp + ".moment1") # immath(imagename=[ dir_image + name_line + ".moment0_tmp", dir_image + name_line + ".moment8_tmp" ], expr="iif( IM0>=" + str(peak * percent) + ", IM1, 0.0)", outfile=dir_image + name_line + "_" + beamp + ".moment8_tmp2") immath(imagename=[ dir_image + name_line + "_" + beamp + ".moment8_tmp2", dir_image + name_line + ".moment0.noise_tmp" ], expr="iif( IM1>=" + str(nchan) + ", IM0, 0.0)", outfile=dir_image + name_line + "_" + beamp + ".moment8") # immath(imagename=[ dir_image + name_line + "_" + beamp + ".moment0", dir_image + name_line + "_" + beamp + ".moment0.noise" ], expr="IM0/IM1", outfile=dir_image + name_line + "_" + beamp + ".moment0.snratio_tmp") immath(imagename=[ dir_image + name_line + "_" + beamp + ".moment0.snratio_tmp", dir_image + name_line + ".moment0.noise_tmp" ], expr="iif( IM1>=" + str(nchan) + ", IM0, 0.0)", outfile=dir_image + name_line + "_" + beamp + ".moment0.snratio") os.system("rm -rf " + cubeimage + ".maskedTF") os.system("rm -rf " + dir_image + name_line + ".moment0.noise_tmp") os.system("rm -rf " + dir_image + name_line + "_" + beamp + ".moment0.snratio_tmp") os.system("rm -rf " + dir_image + name_line + ".moment0_tmp") os.system("rm -rf " + dir_image + name_line + ".moment1_tmp") os.system("rm -rf " + dir_image + name_line + ".moment8_tmp") os.system("rm -rf " + dir_image + name_line + "_" + beamp + ".moment0_tmp2") os.system("rm -rf " + dir_image + name_line + "_" + beamp + ".moment1_tmp2") os.system("rm -rf " + dir_image + name_line + "_" + beamp + ".moment8_tmp2") return mask_use_here
os.system("cp -r " + glob.glob(dir_proj + "*" + name_line + "*" + "*flux*")[0] + " " + pbimage) print("### woking on " + name_line) if "hogehoge" in cubeimage: # "13co21" os.system("cp -r " + cubeimage + " " + cubeimage + ".pbcor") else: impbcor(imagename=cubeimage, pbimage=pbimage, cutoff=pbcuts[i], outfile=cubeimage + ".pbcor") immath(imagename=[cubeimage + ".pbcor", maskimage], expr="iif( IM0>=" + str(noises[i] * snr_mom) + ", IM0*IM1, 0.0)", outfile=cubeimage + ".pbcor.masked") immath( imagename=cubeimage + ".pbcor.masked", expr="iif( IM0>0, 0.1, 0.0)", # 0.1 = 1/Vch outfile=cubeimage + ".pbcor.maskedTF") immoments(imagename=cubeimage + ".pbcor.maskedTF", moments=[0], outfile=dir_image + name_line + ".moment0.noise_tmp") # Nch beamarea_pix = beam_area(dir_image + name_line + ".moment0.noise_tmp") immath(dir_image + name_line + ".moment0.noise_tmp", expr="10*sqrt(IM0)*" + str(noises[i]) + "/" + str(np.sqrt(beamarea_pix)),
cubesmooth2 = cubeimage.replace(".image",".smooth2") # 10 mJy imsmooth(imagename = cubeimage, targetres = True, major = "5.5arcsec", minor = "5.5arcsec", pa = "0deg", outfile = cubesmooth2) # create mask createmask(cubeimage,noises[i]*1.*2.5,dir_image+name_line+"_mask0.image") createmask(cubesmooth1,noises[i]*2.*3.5,dir_image+name_line+"_mask1.image") createmask(cubesmooth2,noises[i]*5.*5.5,dir_image+name_line+"_mask2.image") immath(imagename = [dir_image+name_line+"_mask0.image", dir_image+name_line+"_mask1.image", dir_image+name_line+"_mask2.image"], expr = "iif(IM0+IM1+IM2 >= 2.0, 1.0, 0.0)", outfile = dir_image+name_line+"_mask_tmp.image") immath(imagename = [dir_image+name_line+"_mask_tmp.image", dir_image+name_line+"_cube.pb"], expr = "iif(IM1 >= "+str(pbcuts[i])+", IM0, 0.0)", outfile = "ngc3110_mask.cube") os.system("rm -rf "+dir_image+name_line+"_cube.smooth1") os.system("rm -rf "+dir_image+name_line+"_cube.smooth2") os.system("rm -rf "+dir_image+name_line+"_mask0.image") os.system("rm -rf "+dir_image+name_line+"_mask1.image") os.system("rm -rf "+dir_image+name_line+"_mask2.image") os.system("rm -rf "+dir_image+name_line+"_mask_tmp.image") os.system("rm -rf *.last")
def imsmooth3(imagename,outmask,chans,beam_size,template,pixelmin=10): chnoise = ch_noise(imagename,chans) factor1, factor2, factor3 = 3, 9, 15 # imsmooth os.system("rm -rf "+imagename+".smooth1") imsmooth(imagename=imagename, targetres=True, major=str(beam_size*factor1)+"arcsec", minor=str(beam_size*factor1)+"arcsec", pa="0deg", outfile=imagename+".smooth1") rms1 = imstat(imagename+".smooth1",chans=chnoise)["rms"][0] os.system("rm -rf "+imagename+".smooth2") imsmooth(imagename=imagename, targetres=True, major=str(beam_size*factor2)+"arcsec", minor=str(beam_size*factor2)+"arcsec", pa="0deg", outfile=imagename+".smooth2") rms2 = imstat(imagename+".smooth2",chans=chnoise)["rms"][0] os.system("rm -rf "+imagename+".smooth3") imsmooth(imagename=imagename, targetres=True, major=str(beam_size*factor3)+"arcsec", minor=str(beam_size*factor3)+"arcsec", pa="0deg", outfile=imagename+".smooth3") rms3 = imstat(imagename+".smooth3",chans=chnoise)["rms"][0] # imregrid easy_imregrid(imagename+".smooth1",template,axes=[0,1]) easy_imregrid(imagename+".smooth2",template,axes=[0,1]) easy_imregrid(imagename+".smooth3",template,axes=[0,1]) # immath: snr should be 4, but 8 for iras13120 os.system("rm -rf "+imagename+".smooth1.mask") immath(imagename=imagename+".smooth1.regrid", mode="evalexpr", expr="iif(IM0>="+str(rms1*4)+",1.0,0.0)", outfile=imagename+".smooth1.mask") os.system("rm -rf "+imagename+".smooth2.mask") immath(imagename=imagename+".smooth2.regrid", mode="evalexpr", expr="iif(IM0>="+str(rms2*4)+",1.0,0.0)", outfile=imagename+".smooth2.mask") os.system("rm -rf "+imagename+".smooth3.mask") immath(imagename=imagename+".smooth3.regrid", mode="evalexpr", expr="iif(IM0>="+str(rms3*4)+",1.0,0.0)", outfile=imagename+".smooth3.mask") os.system("rm -rf "+outmask) immath(imagename=[imagename+".smooth1.mask", imagename+".smooth2.mask", imagename+".smooth3.mask"], mode="evalexpr", expr="iif(IM0+IM1+IM2>=3.0,1.0,0.0)", outfile=outmask) os.system("rm -rf "+imagename+".smooth1.regrid") os.system("rm -rf "+imagename+".smooth2.regrid") os.system("rm -rf "+imagename+".smooth3.regrid") beamarea = beam_area(imagename) remove_smallmask(outmask,beamarea,pixelmin)
def moments_to_ratio(dir_data, galname, suffix, threesigma_co10, threesigma_co21, threesigma8_co10, threesigma8_co21): """ """ print("###########################") print("### running moments_to_ratio") print("###########################") ### setup dir_data1 = dir_data + galname + "/" im_co10 = glob.glob(dir_data1 + galname + "*co10*" + suffix + "*moment0")[0] im_co21 = glob.glob(dir_data1 + galname + "*co21*" + suffix + "*moment0")[0] outmask_co10 = im_co10.replace(".moment0", "_mom.mask") outmask_co21 = im_co21.replace(".moment0", "_mom.mask") m8_co10 = glob.glob(dir_data1 + galname + "*co10*" + suffix + "*moment8")[0] m8_co21 = glob.glob(dir_data1 + galname + "*co21*" + suffix + "*moment8")[0] outmask8_co10 = m8_co10.replace(".moment8", "_mom8.mask") outmask8_co21 = m8_co21.replace(".moment8", "_mom8.mask") ### create a combined mask # mom-0 peak = imstat(im_co10)["max"][0] createmask(im_co10, threesigma_co10, outmask_co10) peak = imstat(im_co21)["max"][0] createmask(im_co21, threesigma_co21, outmask_co21) outfile = dir_data1 + galname + "_r21_" + suffix + ".mask" os.system("rm -rf " + outfile) immath(imagename=[outmask_co10, outmask_co21], mode="evalexpr", expr="IM0*IM1", outfile=outfile) makemask(mode="copy", inpimage=outfile, inpmask=outfile, output=outfile + ":mask0", overwrite=True) #mom-8 peak = imstat(m8_co10)["max"][0] createmask(m8_co10, threesigma8_co10, outmask8_co10) peak = imstat(im_co21)["max"][0] createmask(m8_co21, threesigma8_co21, outmask8_co21) outfile = dir_data1 + galname + "_r21_" + suffix + "_m8.mask" os.system("rm -rf " + outfile) immath(imagename=[outmask8_co10, outmask8_co21], mode="evalexpr", expr="IM0*IM1", outfile=outfile) makemask(mode="copy", inpimage=outfile, inpmask=outfile, output=outfile + ":mask0", overwrite=True) ### create line ratio map #mom-0 outfile = dir_data1 + galname + "_r21_" + suffix + ".image" mask = dir_data1 + galname + "_r21_" + suffix + ".mask" line_ratio(dir_data="", im1=im_co21, im2=im_co10, outfile=outfile, diff="4.", mask=mask) #mom-8 outfile = dir_data1 + galname + "_r21_" + suffix + "_m8.image" mask = dir_data1 + galname + "_r21_" + suffix + "_m8.mask" line_ratio(dir_data="", im1=m8_co21, im2=m8_co10, outfile=outfile, diff="4.", mask=mask)
def cube_to_moments(pixelmin, increment_mask, thres_masking, thres_mom, pbcut, dir_data, galname, chans, suffix, beam_size, rms_co10, rms_co21, ra, decl): """ """ print("###########################") print("### running cube_to_moments") print("###########################") os.system("rm -rf " + dir_data + galname + "/*" + suffix + "*.moment*") os.system("rm -rf " + dir_data + galname + "/*" + suffix + "*.mask") os.system("rm -rf " + dir_data + galname + "/*" + suffix + "*.cube") ### step 1/10: importfits print("### momentmaps 1/10: importfits") fitsimages = glob.glob(dir_data + "data/" + galname + "*.fits") for i in range(len(fitsimages)): eazy_importfits(fitsimages[i]) # find imported images image_co10 = glob.glob(dir_data + "data/" + galname + "*co10*image*")[0] image_co21 = glob.glob(dir_data + "data/" + galname + "*co21*image*")[0] ### step 2/10: Kelvin to Jansky conversion print("### momentmaps 2/10: Kelvin to Jansky conversion") bunit = imhead(image_co10, "list")["bunit"] synsbeam10 = imhead(image_co10, "list")["beammajor"]["value"] if bunit == "K": # CO(1-0) conversion if bunit = K easy_K2Jy(image_co10, synsbeam10, 115.27120) image_co10 = image_co10 + ".jy" else: print("# skip K2Jy for the CO(1-0) data") bunit = imhead(image_co21, "list")["bunit"] synsbeam21 = imhead(image_co21, "list")["beammajor"]["value"] if bunit == "K": # CO(2-1) conversion if bunit = K easy_K2Jy(image_co21, synsbeam21, 230.53800) image_co21 = image_co21 + ".jy" else: print("# skip K2Jy for the CO(2-1) data") ### step 3/10: imregrid print("### momentmaps 3/10: imregrid") ### step new: make template for imregrid easy_imsmooth(image_co21, beam_size, False) # co21 image2smooth21 = glob.glob(dir_data + "data/" + galname + "*21*smooth")[0] os.system("rm -rf " + image_co21 + ".template") rebinpix(image2smooth21, image_co21 + ".template", ra, decl) pbimage = glob.glob(dir_data + "data/" + galname + "*.pb")[0] easy_imregrid(pbimage, image_co21 + ".template", axes=[0, 1], delete_original=False) pbimage = pbimage + ".regrid" ### step 4/10: imsmooth print("### momentmaps 4/10: imsmooth") beam_mask = beam_size * increment_mask # beam size for the masking easy_imsmooth(image_co10, beam_mask, False) # co10 easy_imsmooth(image_co21, beam_mask, False) # co21 image2smooth21 = glob.glob(dir_data + "data/" + galname + "*21*smooth")[0] easy_imregrid(image2smooth21, image_co21 + ".template", axes=[0, 1], delete_original=True) image2smooth21 = image2smooth21 + ".regrid" image2smooth10 = glob.glob(dir_data + "data/" + galname + "*10*smooth")[0] easy_imregrid(image2smooth10, image2smooth21, delete_original=True) image2smooth10 = image2smooth10 + ".regrid" ### mv the cubes to the working directory os.system("mkdir " + dir_data + galname) os.system("mv "+image2smooth10+" "\ +dir_data+galname+"/"+galname+"_co10_"+suffix+".cube") os.system("mv "+image2smooth21+" "\ +dir_data+galname+"/"+galname+"_co21_"+suffix+".cube") ### step 5/10: create CO(1-0) cube mask print("### momentmaps 5/10: create CO(1-0) cube mask") cube_co10 = glob.glob(dir_data+galname+"/"\ +galname+"*_co10_"+suffix+".cube")[0] thres_co10 = rms_co10 * increment_mask * thres_masking outmask_co10 = cube_co10.replace(".cube", ".mask") createmask(cube_co10, thres_co10, outmask_co10) ### step 6/10: create CO(2-1) cube mask print("### momentmaps 6/10: create CO(2-1) cube mask") cube_co21 = glob.glob(dir_data+galname+"/"\ +galname+"*_co21_"+suffix+".cube")[0] thres_co21 = rms_co21 * increment_mask * thres_masking outmask_co21 = cube_co21.replace(".cube", ".mask") createmask(cube_co21, thres_co21, outmask_co21) ### step 7/10: combine masks print("### momentmaps 7/10: combine masks") mask_combine = dir_data + galname + "/" + galname + "_combine_" + suffix + ".mask" os.system("rm -rf " + mask_combine) immath(imagename=[outmask_co10, outmask_co21], mode="evalexpr", expr="IM0*IM1", outfile=mask_combine) beamarea = beam_area(image_co21, increment_mask) remove_smallmask(mask_combine, beamarea, pixelmin) ### step 8/10: imsmooth print("### step 8/10: imsmooth") easy_imsmooth(image_co10, beam_size, False) # co10 easy_imsmooth(image_co21, beam_size, False) # co21 image2smooth21 = glob.glob(dir_data + "data/" + galname + "*21*smooth")[0] #os.system("rm -rf "+image_co21+".template") #rebinpix(image2smooth21,image_co21+".template",ra,decl) easy_imregrid(image2smooth21, image_co21 + ".template", axes=[0, 1], delete_original=True) image2smooth21 = image2smooth21 + ".regrid" image2smooth10 = glob.glob(dir_data + "data/" + galname + "*10*smooth")[0] easy_imregrid(image2smooth10, image2smooth21, delete_original=True) image2smooth10 = image2smooth10 + ".regrid" # mv to working directory os.system("rm -rf " + cube_co10) os.system("rm -rf " + cube_co21) os.system("mv " + image2smooth10 + " " + cube_co10) os.system("mv " + image2smooth21 + " " + cube_co21) print("### step 9/10: immoments") moment_maps(cube_co10, chans, mask_combine, rms_co10 * thres_mom) moment_maps(cube_co21, chans, mask_combine, rms_co21 * thres_mom) ### pbmask print("### step 10/10: pb mask at " + str(pbcut)) mask_pb = dir_data + galname + "/" + galname + "_pb_" + suffix + ".mask" peak = imhead(pbimage, mode="list")["datamax"] createmask(pbimage, peak * pbcut, mask_pb) images_moment = glob.glob(cube_co10 + ".moment*") images_moment.extend(glob.glob(cube_co21 + ".moment*")) for i in range(len(images_moment)): outfile = images_moment[i].replace(".cube", "") if galname == "ngc3110": os.system("mv " + images_moment[i] + " " \ + images_moment[i].replace(".cube","")) else: os.system("rm -rf " + outfile) immath(imagename=[images_moment[i], mask_pb], mode="evalexpr", expr="IM0*IM1", outfile=outfile) os.system("rm -rf " + images_moment[i]) """
def simanalyze( project=None, image=None, # if image==False: imagename=None, skymodel=None, # else: vis=None, modelimage=None, imsize=None, imdirection=None, cell=None, interactive=None, niter=None, threshold=None, weighting=None, mask=None, outertaper=None, pbcor=None, stokes=None, featherimage=None, # endif analyze=None, showuv=None, showpsf=None, showmodel=None, showconvolved=None, showclean=None, showresidual=None, showdifference=None, showfidelity=None, graphics=None, verbose=None, overwrite=None, dryrun=False, logfile=None ): #def simanalyze(project='sim', image=True, imagename='default', skymodel='', vis='default', modelimage='', imsize=[128, 128], imdirection='', cell='', interactive=False, niter=0, threshold='0.1mJy', weighting='natural', mask=[], outertaper=[''], stokes='I', featherimage='', analyze=False, showuv=True, showpsf=True, showmodel=True, showconvolved=False, showclean=True, showresidual=False, showdifference=True, showfidelity=True, graphics='both', verbose=False, overwrite=True, dryrun=False): # Collect a list of parameter values to save inputs in_params = locals() import re import glob casalog.origin('simanalyze') if verbose: casalog.filter(level="DEBUG2") a = inspect.stack() stacklevel = 0 for k in range(len(a)): if (string.find(a[k][1], 'ipython console') > 0): stacklevel = k myf = sys._getframe(stacklevel).f_globals # create the utility object: myutil = simutil() if logfile: myutil.reportfile=logfile myutil.openreport() if verbose: myutil.verbose = True msg = myutil.msg from simutil import is_array_type # put output in directory called "project" fileroot = project if not os.path.exists(fileroot): msg(fileroot+" directory doesn't exist - the task expects to find results from creating the datasets there, like the skymodel.",priority="error") # msg should raise an exception for priority=error saveinputs = myf['saveinputs'] saveinputs('simanalyze',fileroot+"/"+project+".simanalyze.last") # myparams=in_params) if (not image) and (not analyze): casalog.post("No operation to be done. Exiting from task.", "WARN") return True grscreen = False grfile = False if graphics == "both": grscreen = True grfile = True if graphics == "screen": grscreen = True if graphics == "file": grfile = True try: # Predefined parameters pbcoeff = 1.13 ## PB defined as pbcoeff*lambda/d # handle '$project' in modelimage modelimage = modelimage.replace('$project',project) featherimage = featherimage.replace('$project',project) #========================= # things we need: model_cell, model_direction if user doesn't specify - # so find those first, and get information using util.modifymodel # with skymodel=newmodel # we need to parse either the mslist or imagename (if image=False) # first, so that we can pick the appropriate skymodel, # if there are several. skymodel_searchstring="NOT SPECIFIED" if not (image or dryrun): user_imagename=imagename if user_imagename=="default" or len(user_imagename)<=0: images= glob.glob(fileroot+"/*image") if len(images)<1: msg("can't find any image in project directory",priority="error") return False if len(images)>1: msg("found multiple images in project directory",priority="warn") msg("using "+images[0],priority="warn") imagename=images[0] # trim .image suffix: imagename= imagename.replace(".image","") # if the user hasn't specified a sky model image, we can try to # see if their imagename contains something like the project and # configuration, as it would if simobserve created it. user_skymodel=skymodel if not os.path.exists(user_skymodel): if os.path.exists(fileroot+"/"+user_skymodel): user_skymodel=fileroot+"/"+user_skymodel elif len(user_skymodel)>0: raise Exception,"Can't find your specified skymodel "+user_skymodel # try to strip a searchable identifier tmpstring=user_skymodel.split("/")[-1] skymodel_searchstring=tmpstring.replace(".image","") if image: # check for default measurement sets: default_mslist = glob.glob(fileroot+"/*ms") n_default=len(default_mslist) # is the user requesting this ms? default_requested=[] for i in range(n_default): default_requested.append(False) # parse ms parameter and check for existance; # initial ms list if vis=="default" or len(vis)==0: mslist0=default_mslist else: mslist0 = vis.split(',') # verified found ms list mslist = [] mstype = [] mstoimage=[] tpmstoimage=None for ms0 in mslist0: if not len(ms0): continue ms1 = ms0.replace('$project',project) # MSes in fileroot/ have priority if os.path.exists(fileroot+"/"+ms1): ms1 = fileroot + "/" + ms1 if os.path.exists(ms1)or dryrun: mslist.append(ms1) # mark as requested if default_mslist.count(ms1): i=default_mslist.index(ms1) default_requested[i]=True # check if noisy in name if re.search('noisy.',ms1): ms1_raw=str.join("",re.split('noisy.',ms1)) if default_mslist.count(ms1_raw): i=default_mslist.index(ms1_raw) default_requested[i]=True else: # not noisy if ms1.endswith(".sd.ms"): ms1_noisy=re.split('.sd.ms',ms1)[0]+'.noisy.sd.ms' else: ms1_noisy=re.split('.ms',ms1)[0]+'.noisy.ms' if default_mslist.count(ms1_noisy): i=default_mslist.index(ms1_noisy) default_requested[i]=True if vis == "default": continue msg("You requested "+ms1+" but there is a noisy version of the ms in your project directory - if your intent is to model noisy data you may want to check inputs",priority="warn") # check if the ms is tp data or not. if dryrun: # HACK mstype.append('INT') mstoimage.append(ms1) elif myutil.ismstp(ms1,halt=False): mstype.append('TP') tpmstoimage = ms1 # XXX TODO more than one TP ms will not be handled # correctly msg("Found a total power measurement set, %s." % ms1,origin='simanalyze') else: mstype.append('INT') mstoimage.append(ms1) msg("Found a synthesis measurement set, %s." % ms1,origin='simanalyze') else: msg("measurement set "+ms1+" not found -- removing from imaging list") # check default mslist for unrequested ms: for i in range(n_default): if not default_requested[i]: msg("Project directory contains "+default_mslist[i]+" but you have not requested to include it in your simulated image.") if not mstoimage and len(tpmstoimage) == 0: raise Exception,"No MS found to image" # now try to parse the mslist for an identifier string that # we can use to find the right skymodel if there are several if len(mstoimage) == 0 and len(tpmstoimage) > 0: tmpstring = tpmstoimage.split("/")[-1] else: tmpstring=(mstoimage[0]).split("/")[-1] skymodel_searchstring=tmpstring.replace(".ms","") # more than one to image? if len(mstoimage) > 1: msg("Multiple interferometric ms found:",priority="info",origin='simanalyze') for i in range(len(mstoimage)): msg(" "+mstoimage[i],priority="info",origin='simanalyze') msg(" will be concated and simultaneously deconvolved; if something else is desired, please specify vis, or image manually and use image=F",priority="info",origin='simanalyze') concatms=project+"/"+project+".concat.ms" from concat import concat weights = get_concatweights(mstoimage) msg(" concat("+str(mstoimage)+",concatvis='"+concatms+"',visweightscale="+str(weights)+")",origin='simanalyze') if not dryrun: concat(mstoimage,concatvis=concatms,visweightscale=weights) mstoimage=[concatms] #======================================================== # now we can search for skymodel, and if there are several, # pick the one that is closest to either the imagename, # or the first MS if there are several MS to image. components_only=False # first look for skymodel, if not then compskymodel skymodels=glob.glob(fileroot+"/"+project+"*.skymodel")+glob.glob(fileroot+"/"+project+"*.newmodel") nmodels=len(skymodels) skymodel_index=0 if nmodels>1: msg("Found %i sky model images:" % nmodels,origin='simanalyze') # use the skymodel_searchstring to try to pick the right one # print them out for the user while we're at it. for i in range(nmodels): msg(" "+skymodels[i]) if skymodels[i].count(skymodel_searchstring)>0: skymodel_index=i msg("Using skymodel "+skymodels[skymodel_index],origin='simanalyze') if nmodels>=1: skymodel=skymodels[skymodel_index] else: skymodel="" if os.path.exists(skymodel): msg("Sky model image "+skymodel+" found.",origin='simanalyze') else: skymodels=glob.glob(fileroot+"/"+project+"*.compskymodel") nmodels=len(skymodels) if nmodels>1: msg("Found %i sky model images:" % nmodels,origin='simanalyze') for ff in skymodels: msg(" "+ff) msg("Using "+skymodels[0],origin='simanalyze') if nmodels>=1: skymodel=skymodels[0] else: skymodel="" if os.path.exists(skymodel): msg("Sky model image "+skymodel+" found.",origin='simanalyze') components_only=True elif not dryrun: msg("Can't find a model image in your project directory, named skymodel or compskymodel - output image will be created, but comparison with the input model is not possible.",priority="warn",origin='simanalyze') analyze=False modelflat = skymodel+".flat" if os.path.exists(skymodel): if not (os.path.exists(modelflat) or dryrun): myutil.flatimage(skymodel,verbose=verbose) # modifymodel just collects info if skymodel==newmodel (model_refdir,model_cell,model_size, model_nchan,model_center,model_width, model_stokes) = myutil.modifymodel(skymodel,skymodel, "","","","","",-1, flatimage=False) cell_asec=qa.convert(model_cell[0],'arcsec')['value'] ##################################################################### # clean if desired, use noisy image for further calculation if present # todo suggest a cell size from psf? ##################################################################### if image: # make sure cell is defined if is_array_type(cell): if len(cell) > 0: cell0 = cell[0] else: cell0 = "" else: cell0 = cell if len(cell0)<=0: cell = model_cell if is_array_type(cell): if len(cell) == 1: cell = [cell[0],cell[0]] else: cell = [cell,cell] # cells are positive by convention cell = [qa.abs(cell[0]),qa.abs(cell[1])] # and imsize if is_array_type(imsize): if len(imsize) > 0: imsize0 = imsize[0] if len(imsize) > 1: imsize1 = imsize[1] else: imsize1 = imsize0 else: imsize0 = -1 else: imsize0 = imsize if imsize0 <= 0: imsize = [int(pl.ceil(qa.convert(qa.div(model_size[0],cell[0]),"")['value'])), int(pl.ceil(qa.convert(qa.div(model_size[1],cell[1]),"")['value']))] else: imsize=[imsize0,imsize1] if len(mstoimage) == 0: if tpmstoimage: sd_only = True else: msg("no measurement sets found to image",priority="error",origin='simanalyze') else: sd_only = False # get some quantities from the interferometric ms # TODO use something like aU.baselineStats for this, and the 90% baseline maxbase=0. if len(mstoimage)>1 and dryrun: msg("imaging multiple ms not possible in dryrun mode",priority="warn",origin="simanalyze") # TODO make work better for multiple MS for msfile in mstoimage: if os.path.exists(msfile): tb.open(msfile) rawdata = tb.getcol("UVW") tb.done() maxbase = max([max(rawdata[0,]),max(rawdata[1,])]) # in m psfsize = 0.3/qa.convert(qa.quantity(model_center),'GHz')['value']/maxbase*3600.*180/pl.pi # lambda/b converted to arcsec minimsize = 8* int(psfsize/cell_asec) elif dryrun: minimsize = min(imsize) psfsize = qa.mul(cell[0],3) # HACK else: raise Exception,mstoimage+" not found." if imsize[0] < minimsize: msg("The number of image pixel in x-axis, %d, is small to cover 8 x PSF. Setting x pixel number, %d." % (imsize[0], minimsize), priority='warn',origin='simanalyze') imsize[0] = minimsize if imsize[1] < minimsize: msg("The number of image pixel in y-axis, %d, is small to cover 8 x PSF. Setting y pixel number, %d" % (imsize[1], minimsize), priority='warn',origin='simanalyze') imsize[1] = minimsize tpimage=None # Do single dish imaging first if tpmstoimage exists. if tpmstoimage and os.path.exists(tpmstoimage): msg('creating image from ms: '+tpmstoimage,origin='simanalyze') #if len(mstoimage): # tpimage = project + '.sd.image' #else: # tpimage = project + '.image' tpimage = project + '.sd.image' tpimage = fileroot + "/" + tpimage if len(mstoimage): if len(modelimage) and tpimage != modelimage and \ tpimage != fileroot+"/"+modelimage: msg("modelimage parameter set to "+modelimage+" but also creating a new total power image "+tpimage,priority="warn",origin='simanalyze') msg("assuming you know what you want, and using modelimage="+modelimage+" in deconvolution",priority="warn",origin='simanalyze') elif len(featherimage) and tpimage != featherimage and \ tpimage != fileroot+"/"+featherimage: msg("featherimage parameter set to "+featherimage+" but also creating a new total power image "+tpimage,priority="warn",origin='simanalyze') msg("assuming you know what you want, and using featherimage="+featherimage+" in feather",priority="warn",origin='simanalyze') # Get PB size of TP Antenna # !! aveant will only be set if modifymodel or setpointings and in # any case it will the the aveant of the INTERFM array - we want the SD if os.path.exists(tpmstoimage): # antenna diameter tb.open(tpmstoimage+"/ANTENNA") diams = tb.getcol("DISH_DIAMETER") tb.close() aveant = pl.mean(diams) # theoretical antenna beam size import sdbeamutil pb_asec = sdbeamutil.primaryBeamArcsec(qa.tos(qa.convert(qa.quantity(model_center),'GHz')),aveant,(0.75 if aveant==12.0 else 0.0),10.0) elif dryrun: aveant = 12.0 pb_asec = pbcoeff*0.29979/qa.convert(qa.quantity(model_center),'GHz')['value']/aveant*3600.*180/pl.pi else: raise Exception, tpmstoimage+" not found." # default PSF from PB of antenna imbeam = {'major': qa.quantity(pb_asec,'arcsec'), 'minor': qa.quantity(pb_asec,'arcsec'), 'positionangle': qa.quantity(0.0,'deg')} # Common imaging parameters sdim_param = dict(infiles=[tpmstoimage], overwrite=overwrite, phasecenter=model_refdir, mode='channel', nchan=model_nchan, start=0, width=1) if True: #SF gridding msg("Generating TP image using 'SF' kernel.",origin='simanalyze') beamsamp = 9 sfcell_asec = pb_asec/beamsamp sfcell = qa.tos(qa.quantity(sfcell_asec, "arcsec")) cell_asec = [qa.convert(cell[0],"arcsec")['value'], qa.convert(cell[1],"arcsec")['value']] if cell_asec[0] > sfcell_asec or \ cell_asec[1] > sfcell_asec: # imregrid() may not work properly for regrid of # small to large cell msg("The requested cell size is too large to invoke SF gridding. Please set cell size <= %f arcsec or grid TP MS '%s' manually" % (sfcell_asec, tpmstoimage),priority="error",origin='simanalyze') sfsupport = 6 temp_out = tpimage+"0" temp_cell = [sfcell, sfcell] # too small - is imsize too small to start with? # needs to cover all pointings. temp_imsize = [int(pl.ceil(cell_asec[0]/sfcell_asec*imsize[0])), int(pl.ceil(cell_asec[1]/sfcell_asec*imsize[1]))] msg("Using predefined algorithm to define grid parameters.",origin='simanalyze') msg("SF gridding summary",origin='simanalyze') msg("- Antenna primary beam: %f arcsec" % pb_asec,origin='simanalyze') msg("- Image pixels per antenna PB (predefined): %f" % beamsamp,origin='simanalyze') msg("- Cell size (arcsec): [%s, %s]" % (temp_cell[0], temp_cell[1]),origin='simanalyze') msg("- Imsize to cover final TP image area: [%d, %d] (type: %s)" % (temp_imsize[0], temp_imsize[1], type(temp_imsize[0])),origin='simanalyze') msg("- convolution support: %d" % sfsupport,origin='simanalyze') # kernel specific imaging parameters sdim_param['gridfunction'] = 'SF' sdim_param['convsupport'] = sfsupport sdim_param['outfile'] = temp_out sdim_param['imsize'] = temp_imsize sdim_param['cell'] = temp_cell msg(get_taskstr('sdimaging', sdim_param), priority="info") if not dryrun: sdimaging(**sdim_param) if not os.path.exists(temp_out): raise RuntimeError, "TP imaging failed." # Scale image by convolved beam / antenna primary beam ia.open(temp_out) imbeam = ia.restoringbeam() ia.close() beam_area_ratio = qa.getvalue(qa.convert(imbeam['major'], "arcsec")) \ * qa.getvalue(qa.convert(imbeam['minor'], "arcsec")) \ / pb_asec**2 msg("Scaling TP image intensity by %f." % (beam_area_ratio),origin='simanalyze') temp_in = temp_out temp_out = temp_out + ".scaled" immath(imagename=temp_in, mode='evalexpr', expr="IM0*%f" % (beam_area_ratio), outfile=temp_out) if not os.path.exists(temp_out): raise RuntimeError, "TP image scaling failed." # Regrid TP image to final resolution msg("Regridding TP image to final resolution",origin='simanalyze') msg("- cell size (arecsec): [%s, %s]" % (cell[0], cell[1]),origin='simanalyze') msg("- imsize: [%d, %d]" % (imsize[0], imsize[1]),origin='simanalyze') if not dryrun: ia.open(temp_out) newcsys = ia.coordsys() ia.close() dir_idx = newcsys.findcoordinate("direction")['world'] newcsys.setreferencepixel([imsize[0]/2., imsize[1]/2.], type="direction") incr = newcsys.increment(type='direction')['numeric'] newincr = [incr[0]*cell_asec[0]/sfcell_asec, incr[1]*cell_asec[1]/sfcell_asec,] newcsys.setincrement(newincr, type="direction") # sdtemplate = imregrid(imagename=temp_out, template="get") sdtemplate['csys'] = newcsys.torecord() for idx in range(len(dir_idx)): sdtemplate['shap'][ dir_idx[idx] ] = imsize[idx] imregrid(imagename=temp_out, interpolation="cubic", template=sdtemplate, output=tpimage, overwrite=overwrite) del newcsys, sdtemplate, incr, newincr, dir_idx del temp_out, temp_cell, temp_imsize, sfcell_asec, cell_asec else: #PB grid msg("Generating TP image using 'PB' kernel.",origin='simanalyze') # Final TP cell and image size. # imsize and cell are already int and quantum arrays sdimsize = imsize sdcell = [qa.tos(cell[0]), qa.tos(cell[1])] ### TODO: need to set phasecenter properly based on imdirection # kernel specific imaging parameters sdim_param['gridfunction'] = 'PB' sdim_param['outfile'] = tpimage sdim_param['imsize'] = sdimsize sdim_param['cell'] = sdcell msg(get_taskstr('sdimaging', sdim_param), priority="info") if not dryrun: sdimaging(**sdim_param) del sdimsize, sdcell # TODO: Define PSF of image here # for now use default # get image beam size form TP image if os.path.exists(tpimage): ia.open(tpimage) beam = ia.restoringbeam() ia.close() if sd_only: bmarea = beam['major']['value']*beam['minor']['value']*1.1331 #arcsec2 bmarea = bmarea/(cell[0]['value']*cell[1]['value']) # bm area in pix else: del beam #del beam msg('generation of total power image '+tpimage+' complete.',origin='simanalyze') # update TP ms name the for following steps sdmsfile = tpmstoimage sd_any = True imagename = re.split('.image$',tpimage)[0] # End of single dish imaging part outflat_current = False convsky_current = False if image and len(mstoimage) > 0: # for reruns foo=mstoimage[0] foo=foo.replace(".ms","") foo=foo.replace(project,"") foo=foo.replace("/","") project=project+foo imagename = fileroot + "/" + project # get nfld, sourcefieldlist, from (interfm) ms if it was not just created # TODO make work better for multiple mstoimage (figures below) if os.path.exists(mstoimage[0]): tb.open(mstoimage[0]+"/SOURCE") code = tb.getcol("CODE") sourcefieldlist = pl.where(code=='OBJ')[0] nfld = len(sourcefieldlist) tb.done() elif dryrun: nfld=1 # HACK msfile = mstoimage[0] # set cleanmode automatically (for interfm) if nfld == 1: cleanmode = "csclean" else: cleanmode = "mosaic" # clean insists on using an existing model if its present if os.path.exists(imagename+".image"): shutil.rmtree(imagename+".image") if os.path.exists(imagename+".model"): shutil.rmtree(imagename+".model") # An image in fileroot/ has priority if len(modelimage) > 0 and os.path.exists(fileroot+"/"+modelimage): modelimage = fileroot + "/" + modelimage msg("Found modelimage, %s." % modelimage,origin='simanalyze') # in simdata we use imdirection instead of model_refdir if not myutil.isdirection(imdirection,halt=False): imdirection=model_refdir myutil.imclean(mstoimage,imagename, cleanmode,cell,imsize,imdirection, interactive,niter,threshold,weighting, outertaper,pbcor,stokes, #sourcefieldlist=sourcefieldlist, modelimage=modelimage,mask=mask,dryrun=dryrun) # create imagename.flat and imagename.residual.flat: if not dryrun: myutil.flatimage(imagename+".image",verbose=verbose) myutil.flatimage(imagename+".residual",verbose=verbose) outflat_current = True # feather if featherimage: if not os.path.exists(featherimage): raise Exception,"Could not find featherimage "+featherimage else: featherimage="" if tpimage: # if you set modelimage, then it won't force tpimage into # featherimage. this could be hard to explain # to the user. if os.path.exists(tpimage) and not os.path.exists(modelimage): featherimage=tpimage if os.path.exists(featherimage): msg("feathering the interfermetric image "+imagename+".image with "+featherimage,origin='simanalyze',priority="info") from feather import feather # TODO call with params? msg("feather('"+imagename+".feather.image','"+imagename+".image','"+featherimage+"')",priority="info") if not dryrun: feather(imagename+".feather.image",imagename+".image",featherimage) # copy residual flat image shutil.copytree(imagename+".residual.flat",imagename+".feather.residual.flat") imagename=imagename+".feather" # but replace combined flat image myutil.flatimage(imagename+".image",verbose=verbose) if verbose: msg(" ") msg("done inverting and cleaning",origin='simanalyze') if not is_array_type(cell): cell = [cell,cell] if len(cell) <= 1: cell = [qa.quantity(cell[0]),qa.quantity(cell[0])] else: cell = [qa.quantity(cell[0]),qa.quantity(cell[1])] cell = [qa.abs(cell[0]),qa.abs(cell[0])] # get beam from output clean image if verbose: msg("getting beam from "+imagename+".image",origin='simanalyze') if os.path.exists(imagename+".image"): ia.open(imagename+".image") beam = ia.restoringbeam() ia.close() # model has units of Jy/pix - calculate beam area from clean image # (even if we are not plotting graphics) bmarea = beam['major']['value']*beam['minor']['value']*1.1331 #arcsec2 bmarea = bmarea/(cell[0]['value']*cell[1]['value']) # bm area in pix msg("synthesized beam area in output pixels = %f" % bmarea,origin='simanalyze') if image: # show model, convolved model, clean image, and residual if grfile: file = fileroot + "/" + project + ".image.png" else: file = "" else: mslist=[] if dryrun: grscreen=False grfile=False analyze=False if image and len(mstoimage) > 0: if grscreen or grfile: myutil.newfig(multi=[2,2,1],show=grscreen) # create regridded and convolved sky model image myutil.convimage(modelflat,imagename+".image.flat") convsky_current = True # don't remake this for analysis in this run disprange = [] # passing empty list causes return of disprange # original sky regridded to output pixels but not convolved with beam discard = myutil.statim(modelflat+".regrid",disprange=disprange,showstats=False) myutil.nextfig() # convolved sky model - units of Jy/bm disprange = [] discard = myutil.statim(modelflat+".regrid.conv",disprange=disprange) myutil.nextfig() # clean image - also in Jy/beam # although because of DC offset, better to reset disprange disprange = [] discard = myutil.statim(imagename+".image.flat",disprange=disprange) myutil.nextfig() if len(mstoimage) > 0: myutil.nextfig() # clean residual image - Jy/bm discard = myutil.statim(imagename+".residual.flat",disprange=disprange) myutil.endfig(show=grscreen,filename=file) ##################################################################### # analysis if analyze: if not os.path.exists(imagename+".image"): if os.path.exists(fileroot+"/"+imagename+".image"): imagename=fileroot+"/"+imagename else: msg("Can't find a simulated image - expecting "+imagename,priority="error") return False # we should have skymodel.flat created above if not image: if not os.path.exists(imagename+".image"): msg("you must image before analyzing.",priority="error") return False # get beam from output clean image if verbose: msg("getting beam from "+imagename+".image",origin="analysis") ia.open(imagename+".image") beam = ia.restoringbeam() ia.close() # model has units of Jy/pix - calculate beam area from clean image cell = myutil.cellsize(imagename+".image") cell= [ qa.convert(cell[0],'arcsec'), qa.convert(cell[1],'arcsec') ] # (even if we are not plotting graphics) bmarea = beam['major']['value']*beam['minor']['value']*1.1331 #arcsec2 bmarea = bmarea/(cell[0]['value']*cell[1]['value']) # bm area in pix msg("synthesized beam area in output pixels = %f" % bmarea) # flat output:? if the user manually cleaned, this may not exist outflat = imagename + ".image.flat" if (not outflat_current) or (not os.path.exists(outflat)): # create imagename.flat and imagename.residual.flat myutil.flatimage(imagename+".image",verbose=verbose) if os.path.exists(imagename+".residual"): myutil.flatimage(imagename+".residual",verbose=verbose) else: if showresidual: msg(imagename+".residual not found -- residual will not be plotted",priority="warn") showresidual = False outflat_current = True # regridded and convolved input:? if not convsky_current: myutil.convimage(modelflat,imagename+".image.flat") convsky_current = True # now should have all the flat, convolved etc even if didn't run "image" # make difference image. # immath does Jy/bm if image but only if ia.setbrightnessunit("Jy/beam") in convimage() convolved = modelflat + ".regrid.conv" difference = imagename + '.diff' diff_ia = ia.imagecalc(difference, "'%s' - '%s'" % (convolved, outflat), overwrite=True) diff_ia.setbrightnessunit("Jy/beam") # get rms of difference image for fidelity calculation #ia.open(difference) diffstats = diff_ia.statistics(robust=True, verbose=False,list=False) diff_ia.close() del diff_ia maxdiff = diffstats['medabsdevmed'] if maxdiff != maxdiff: maxdiff = 0. if type(maxdiff) != type(0.): if maxdiff.__len__() > 0: maxdiff = maxdiff[0] else: maxdiff = 0. # Make fidelity image. absdiff = imagename + '.absdiff' calc_ia = ia.imagecalc(absdiff, "max(abs('%s'), %f)" % (difference, maxdiff/pl.sqrt(2.0)), overwrite=True) calc_ia.close() fidelityim = imagename + '.fidelity' calc_ia = ia.imagecalc(fidelityim, "abs('%s') / '%s'" % (convolved, absdiff), overwrite=True) calc_ia.close() msg("fidelity image calculated",origin="analysis") # scalar fidelity absconv = imagename + '.absconv' calc_ia = ia.imagecalc(absconv, "abs('%s')" % convolved, overwrite=True) if ia.isopen(): ia.close() #probably not necessary calc_ia.close() del calc_ia ia.open(absconv) modelstats = ia.statistics(robust=True, verbose=False,list=False) maxmodel = modelstats['max'] if maxmodel != maxmodel: maxmodel = 0. if type(maxmodel) != type(0.): if maxmodel.__len__() > 0: maxmodel = maxmodel[0] else: maxmodel = 0. ia.close() scalarfidel = maxmodel/maxdiff msg("fidelity range (max model / rms difference) = "+str(scalarfidel),origin="analysis") # now, what does the user want to actually display? # need MS for showuv and showpsf if not image: msfile = fileroot + "/" + project + ".ms" elif sd_only: # imaged and single dish only msfile = tpmstoimage # psf is not available for SD only sim if os.path.exists(msfile) and myutil.ismstp(msfile,halt=False): if showpsf: msg("single dish simulation -- psf will not be plotted",priority='warn') showpsf = False if (not image) and (not os.path.exists(msfile)): if showpsf or showuv: msg("No image is generated in this run. Default MS, '%s', does not exist -- uv and psf will not be plotted" % msfile,priority='warn') showpsf = False showuv = False # if the order in the task input changes, change it here too figs = [showuv,showpsf,showmodel,showconvolved,showclean,showresidual,showdifference,showfidelity] nfig = figs.count(True) if nfig > 6: msg("only displaying first 6 selected panels in graphic output",priority="warn") if nfig <= 0: return True if nfig < 4: multi = [1,nfig,1] else: if nfig == 4: multi = [2,2,1] else: multi = [2,3,1] if grfile: file = fileroot + "/" + project + ".analysis.png" else: file = "" if grscreen or grfile: myutil.newfig(multi=multi,show=grscreen) # if order in task parameters changes, change here too if showuv: # TODO loop over all ms - show all UV including zero if len(mslist)>1: msg("Using only "+msfile+" for uv plot",priority="warn",origin='simanalyze') tb.open(msfile) rawdata = tb.getcol("UVW") tb.done() pl.box() maxbase = max([max(rawdata[0,]),max(rawdata[1,])]) # in m klam_m = 300/qa.convert(model_center,'GHz')['value'] pl.plot(rawdata[0,]/klam_m,rawdata[1,]/klam_m,'b,') pl.plot(-rawdata[0,]/klam_m,-rawdata[1,]/klam_m,'b,') ax = pl.gca() ax.yaxis.LABELPAD = -4 pl.xlabel('u[klambda]',fontsize='x-small') pl.ylabel('v[klambda]',fontsize='x-small') pl.axis('equal') # Add zero-spacing (single dish) if not yet plotted # TODO make this a check over all ms # if predict_sd and not myutil.ismstp(msfile,halt=False): # pl.plot([0.],[0.],'r,') myutil.nextfig() if showpsf: if image: psfim = imagename + ".psf" else: psfim = project + ".quick.psf" if not os.path.exists(psfim): if len(mslist)>1: msg("Using only "+msfile+" for psf generation",priority="warn") im.open(msfile) # TODO spectral parms im.defineimage(cellx=qa.tos(model_cell[0]),nx=max([minimsize,128])) if os.path.exists(psfim): shutil.rmtree(psfim) im.approximatepsf(psf=psfim) # beam is set above (even in "analyze" only) # note that if image, beam has fields 'major' whereas if not, it # has fields like 'bmaj'. # beam=im.fitpsf(psf=psfim) im.done() ia.open(psfim) beamcs = ia.coordsys() beam_array = ia.getchunk(axes=[beamcs.findcoordinate("spectral")['pixel'][0],beamcs.findcoordinate("stokes")['pixel'][0]],dropdeg=True) nn = beam_array.shape xextent = nn[0]*cell_asec*0.5 xextent = [xextent,-xextent] yextent = nn[1]*cell_asec*0.5 yextent = [-yextent,yextent] flipped_array = beam_array.transpose() ttrans_array = flipped_array.tolist() ttrans_array.reverse() pl.imshow(ttrans_array,interpolation='bilinear',cmap=pl.cm.jet,extent=xextent+yextent,origin="bottom") psfim.replace(project+"/","") pl.title(psfim,fontsize="x-small") b = qa.convert(beam['major'],'arcsec')['value'] pl.xlim([-3*b,3*b]) pl.ylim([-3*b,3*b]) ax = pl.gca() pl.text(0.05,0.95,"bmaj=%7.1e\nbmin=%7.1e" % (beam['major']['value'],beam['minor']['value']),transform = ax.transAxes,bbox=dict(facecolor='white', alpha=0.7),size="x-small",verticalalignment="top") ia.close() myutil.nextfig() disprange = [] # first plot will define range if showmodel: discard = myutil.statim(modelflat+".regrid",incell=cell,disprange=disprange,showstats=False) myutil.nextfig() disprange = [] if showconvolved: discard = myutil.statim(modelflat+".regrid.conv") # if disprange gets set here, it'll be Jy/bm myutil.nextfig() if showclean: # own scaling because of DC/zero spacing offset discard = myutil.statim(imagename+".image.flat") myutil.nextfig() if showresidual: # it gets its own scaling discard = myutil.statim(imagename+".residual.flat") myutil.nextfig() if showdifference: # it gets its own scaling. discard = myutil.statim(imagename+".diff") myutil.nextfig() if showfidelity: # it gets its own scaling. discard = myutil.statim(imagename+".fidelity",showstats=False) myutil.nextfig() myutil.endfig(show=grscreen,filename=file) sim_min,sim_max,sim_rms,sim_units = myutil.statim(imagename+".image.flat",plot=False) # if not displaying still print stats: # 20100505 ia.stats changed to return Jy/bm: msg('Simulation rms: '+str(sim_rms/bmarea)+" Jy/pix = "+ str(sim_rms)+" Jy/bm",origin="analysis") msg('Simulation max: '+str(sim_max/bmarea)+" Jy/pix = "+ str(sim_max)+" Jy/bm",origin="analysis") #msg('Simulation rms: '+str(sim_rms)+" Jy/pix = "+ # str(sim_rms*bmarea)+" Jy/bm",origin="analysis") #msg('Simulation max: '+str(sim_max)+" Jy/pix = "+ # str(sim_max*bmarea)+" Jy/bm",origin="analysis") msg('Beam bmaj: '+str(beam['major']['value'])+' bmin: '+str(beam['minor']['value'])+' bpa: '+str(beam['positionangle']['value']),origin="analysis") # cleanup - delete newmodel, newmodel.flat etc # flat kept by user request CAS-5509 # if os.path.exists(imagename+".image.flat"): # shutil.rmtree(imagename+".image.flat") if os.path.exists(imagename+".residual.flat"): shutil.rmtree(imagename+".residual.flat") # .flux.pbcoverage is nessesary for feather. #if os.path.exists(imagename+".flux.pbcoverage"): # shutil.rmtree(imagename+".flux.pbcoverage") absdiff = imagename + '.absdiff' if os.path.exists(absdiff): shutil.rmtree(absdiff) absconv = imagename + '.absconv' if os.path.exists(absconv): shutil.rmtree(absconv) # if os.path.exists(imagename+".diff"): # shutil.rmtree(imagename+".diff") if os.path.exists(imagename+".quick.psf") and os.path.exists(imagename+".psf"): shutil.rmtree(imagename+".quick.psf") finalize_tools() if myutil.isreport(): myutil.closereport() except TypeError, e: finalize_tools() #msg("simanalyze -- TypeError: %s" % e,priority="error") casalog.post("simanalyze -- TypeError: %s" % e, priority="ERROR") raise TypeError, e return
os.system("cp -r "+image_cube_depb_sm+".regrid "+image_cube2+".depb") # immoments chnoise = ch_noise(image_cube2,chanss[i]) rms = imstat(image_cube2,chans=chnoise)["rms"][0] moment_maps(image_cube2,chanss[i],outmask,rms*snr_moms[i],beams[i]) # pb masking mask_pb = dir_gal+galnames[i]+"_pb_"+suffix+".mask" peak = imhead(pb_map,mode="list")["datamax"] createmask(pb_map,peak*pbcuts[i],mask_pb) images_moment = glob.glob(image_cube2 + ".moment*") for k in range(len(images_moment)): outfile = images_moment[k].replace(".cube","") if galnames[i] == "ngc3110": os.system("mv " + images_moment[k] + " " \ + images_moment[k].replace(".cube","")) else: os.system("rm -rf " + outfile) immath(imagename = [images_moment[k],mask_pb], mode = "evalexpr", expr = "IM0*IM1", outfile = outfile) os.system("rm -rf " + images_moment[k]) # clean up os.system("rm -rf "+dir_data+galnames[i]+"*") os.system("rm -rf "+dir_gal+"*"+suffix+"*.mask*") os.system("rm -rf *.last")