def gridtemplate(imagename10, image_length, direction_ra, direction_dec): """ myim02 """ # get native grid information num_x_pix = imhead(imagename10, mode="list")["shape"][0] num_y_pix = imhead(imagename10, mode="list")["shape"][1] pix_radian = imhead(imagename10, mode="list")["cdelt2"] obsfreq = 115.27120 # imhead(imagename10,mode="list")["restfreq"][0]/1e9 pix_arcsec = round(pix_radian * 3600 * 180 / np.pi, 3) # create tempalte image blc_ra_tmp = imstat(imagename10)["blcf"].split(", ")[0] blc_dec_tmp = imstat(imagename10)["blcf"].split(", ")[1] blc_ra = blc_ra_tmp.replace(":", "h", 1).replace(":", "m", 1) + "s" blc_dec = blc_dec_tmp.replace(".", "d", 1).replace(".", "m", 1) + "s" beamsize = round(imhead(imagename10, "list")["beammajor"]["value"], 2) pix_size = round(beamsize / 4.53, 2) size_x = int(image_length / pix_size) size_y = size_x c = SkyCoord(blc_ra, blc_dec) ra_dgr = str(c.ra.degree) dec_dgr = str(c.dec.degree) direction = "J2000 " + direction_ra + " " + direction_dec mycl.done() mycl.addcomponent(dir=direction, flux=1.0, fluxunit="Jy", freq="230.0GHz", shape="Gaussian", majoraxis="0.1arcmin", minoraxis="0.05arcmin", positionangle="45.0deg") myia.fromshape("template.im", [size_x, size_y, 1, 1], overwrite=True) mycs = myia.coordsys() mycs.setunits(["rad", "rad", "", "Hz"]) cell_rad = myqa.convert(myqa.quantity(str(pix_size) + "arcsec"), "rad")["value"] mycs.setincrement([-cell_rad, cell_rad], "direction") mycs.setreferencevalue([ myqa.convert(direction_ra, "rad")["value"], myqa.convert(direction_dec, "rad")["value"] ], type="direction") mycs.setreferencevalue(str(obsfreq) + "GHz", "spectral") mycs.setincrement("1GHz", "spectral") myia.setcoordsys(mycs.torecord()) myia.setbrightnessunit("Jy/pixel") myia.modify(mycl.torecord(), subtract=False) exportfits(imagename="template.im", fitsimage="template.fits", overwrite=True) os.system("rm -rf template.image") importfits(fitsimage="template.fits", imagename="template.image") myia.close() mycl.close()
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")
""" """ import os from os.path import join from imstat import imstat from makecube_defines import cube_dir, freq_min, freq_max print """cube_dir = {0}""".format(cube_dir) clean_number = int(os.getenv("CLEAN_NUMBER", -1)) cube_dir = join(cube_dir, "{0}".format(clean_number)) output_image = join(cube_dir, "cube_{0}~{1}".format(freq_min, freq_max)) print """output_image = {0}""".format(output_image) imstat(imagename=output_image + ".image") imstat(imagename=output_image + ".residual")
def selfcal(vis, spwn=6, doplots=True, INTERACTIVE=False, reclean=True, field='W51 Ku', outdir_template="spw%i_selfcal_iter/", statsbox='170,50,229,97', ant1list=['ea14','ea05'], ant2list=['ea16','ea07'], avgchannel_wide='128', avgchannel_narrow='8', cleanboxes=cleanboxes, refant='ea27', solint='30s', niter=2, multiscale=[0,5,10,15,25,50], imsize=512, robust=0.0 ): """ Docstring incomplete """ spw = int(spwn) outdir = outdir_template % spwn try: os.mkdir(outdir) except OSError: pass # you're supposed to pass in avg_data as input avg_data = vis mytb.open(vis+"/ANTENNA") antnames = mytb.getcol("NAME") # plot each antenna's ampl vs time for flagging purposes for ant2 in ant2list: for ant in ant1list: plotms(vis=vis, spw=str(spwn), xaxis='time', yaxis='amp', avgchannel=avgchannel_wide, avgscan=F, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, antenna=ant+"&"+ant2, title='Amp vs Time before averaging for spw %i ant %s-%s' % (spwn,ant,ant2), plotfile=outdir+'ampvstime_spw%i_ant%s-%s.png' % (spwn,ant,ant2), field=field, overwrite=True, ) plotms(vis=vis, spw=str(spwn), xaxis='freq', yaxis='phase', avgtime='1e8', avgscan=T, coloraxis='corr', iteraxis='baseline', xselfscale=T, yselfscale=T, antenna=ant+"&"+ant2, title='Phase vs Freq with time averaging for spw %i ant %s-%s' % (spwn,ant,ant2), plotfile=outdir+'phasevsfreq_spw%i_ant%s-%s.png' % (spwn,ant,ant2), field=field, overwrite=True, ) plotms(vis=vis, spw=str(spwn), xaxis='amp', yaxis='phase', avgtime='1e8', avgscan=T, coloraxis='corr', iteraxis='baseline', xselfscale=T, yselfscale=T, antenna=ant+"&"+ant2, title='Phase vs Amp with time averaging for spw %i ant %s-%s' % (spwn,ant,ant2), plotfile=outdir+'phasevsamp_spw%i_ant%s-%s.png' % (spwn,ant,ant2), field=field, overwrite=True, ) # imagename = "noaverage_spw%i" % spwn # os.system("rm -rf "+imagename+".image") # os.system("rm -rf "+imagename+".model") # os.system("rm -rf "+imagename+".flux") # os.system("rm -rf "+imagename+".psf") # os.system("rm -rf "+imagename+".residual") # clean(vis=vis, field=field, imagename=imagename, mode='mfs', # weighting='briggs', robust=robust, niter=500, imsize=512) # viewer(imagename+".image", # outfile=outdir+imagename+".image.png", # outformat='png', # gui=False) # exportfits(imagename=imagename+".image", fitsimage=imagename+".fits", overwrite=True) #width = 10 # for TW Hydra # width = 4 # for NGC 3256 # (0) Using your split-off, calibrated data, plot the "model" in this MS using # plotms. It should be unit-valued for all data. If not, run delmod to get # rid of any model that might still be lurking in the header, and/or clearcal # to set to 1 any MODEL data. plotms(vis=avg_data, spw='0', xaxis='time', yaxis='amp', avgchannel=avgchannel_wide, xdatacolumn='model', ydatacolumn='model', avgscan=F, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Model Amp vs Time after split for spw %i. Should be all 1s' % spwn, plotfile=outdir+'ampvstime_model_shouldbe1.png', field=field, overwrite=True,) delmod(vis=avg_data) plotms(vis=avg_data, spw='0', xaxis='phase', yaxis='amp', avgchannel=avgchannel_wide, xdatacolumn='data', ydatacolumn='data', avgscan=F, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Corrected Phase vs Amp after split', plotfile=outdir+'ampvsphase_corrected_avg_spw%i.png' % spwn, field=field, overwrite=True,) # (0.5) Run clean non-interactively with some set number of iterations, and be # sure to keep the image around for comparison later. Run delmod to get rid of # the model it saved to the MS header. #if reclean: # imagename="average_spw%i_shallowclean" % spwn # for suffix in clean_output_suffixes: # os.system("rm -rf "+imagename+suffix) # clean(vis=avg_data, field=field, imagename=imagename, mode='mfs', # weighting='briggs', robust=robust, niter=100, imsize=512) # viewer(imagename+".image", # outfile=outdir+imagename+".image.png", # outformat='png', # gui=False) # exportfits(imagename=imagename+".image", fitsimage=imagename+".fits", overwrite=True) # delmod(avg_data,scr=True) # (1) Clean a single SPW *interactively*, boxing the brightest regions and not # cleaning very deeply (maybe 100 iterations). Keep this model in the header # -- it's what you'll use for the first round of self-calibration. if reclean: imagename="average_spw%i_shallowclean_masked" % spwn for suffix in clean_output_suffixes: os.system("rm -rf "+imagename+suffix) clean(vis=avg_data, field=field, imagename=imagename, mode='mfs', psfmode='hogbom',multiscale=multiscale, weighting='briggs', robust=robust, niter=100, imsize=imsize, mask=cleanboxes, nterms=2, usescratch=True) viewer(imagename+".image.tt0", outfile=outdir+imagename+".image.tt0.png", outformat='png', gui=False) exportfits(imagename=imagename+".image.tt0", fitsimage=imagename+".fits", overwrite=True) imrms = [imstat(imagename+".image.tt0",box=statsbox)['rms']] # FAILS!!!! #plotms(vis=avg_data, spw='0', xaxis='time', yaxis='amp', # avgchannel='128', xdatacolumn='model', ydatacolumn='model', avgscan=F, # coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, # title='Model Amp vs Time after shallow clean for spw %i.' % spwn, # plotfile=outdir+'ampvstime_model_shallowclean_spw%i.png' % spwn, field=field, # overwrite=True,) for calnum in xrange(niter): # for Ku D W51 Ku spw 2 if reclean: first_image = 'spw%i_C_C_firstim_selfcal%i' % (spwn,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+first_image+suffix) clean(vis=avg_data,imagename=first_image,field=field, mode='mfs', psfmode='hogbom',multiscale=multiscale, weighting='briggs', robust=robust, niter=100, imsize=imsize, mask=cleanboxes, nterms=2, usescratch=True) exportfits(imagename=first_image+".image.tt0", fitsimage=first_image+".fits", overwrite=True) viewer(first_image+".image.tt0", outfile=outdir+first_image+".image.tt0.png", outformat='png', gui=False) # this fails? #plotms(vis=avg_data, spw='0', xaxis='time', yaxis='amp', # avgchannel='128', xdatacolumn='model', ydatacolumn='model', avgscan=F, # coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, # title='Model Amp vs Time after shallow clean for spw %i iter %i.' % (spwn,calnum), # plotfile=outdir+'ampvstime_model_shallowclean_spw%i_iter%i.png' % (spwn,calnum), field=field, # overwrite=True,) # DONE avg/split ing caltable = 'selfcal%i_%s_spw%i.gcal' % (calnum,field.replace(" ",""),spwn) if reclean: os.system('rm -rf '+caltable) gaincal(vis=avg_data, field='', caltable=caltable, spw='', # gaintype = 'T' could reduce failed fit errors by averaging pols... gaintype='G', # 'G' from http://casaguides.nrao.edu/index.php?title=EVLA_Advanced_Topics_3C391 solint=solint, refant=refant, calmode='p', combine='scan', minblperant=4) # # Watch out for failed solutions noted in the terminal during this # solution. If you see a large fraction (really more than 1 or 2) of # your antennas failing to converge in many time intervals then you # may need to lengthen the solution interval. # # =%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=% # INSPECT THE CALIBRATION # =%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=% # # After you have run the gaincal, you want to inspect the # solution. Use PLOTCAL to look at the solution (here broken into # panels by SPW with individual antennas mapped to colors). Look at # the overall magnitude of the correction to get an idea of how # important the selfcal is and at how quickly it changes with time to # get an idea of how stable the instrument and atmosphere were. # if doplots: for ant2 in ant2list: for ant in ant1list: # (4) Have a look at the gain solutions by antenna. Which antennas # have the largest phase corrections? Before applying the # calibration, use plotms to display the corrected phase vs. amp # for these antennas, to compare with *after* the correction is # applied. plotcal(caltable=caltable, xaxis='time', yaxis='phase', showgui=False, antenna=ant+'&'+ant2, figfile=outdir+'selfcal%i_spw%i_phasevstime_ant%s-%s.png' % (calnum,spwn,ant,ant2), iteration='')#, subplot = 221) #plotcal(caltable=caltable, xaxis='amp', yaxis='phase', # showgui=False, # antenna=ant, # figfile=outdir+'selfcal%i_spw%i_phasevsamp_ant%s.png' % (calnum,spwn,ant), # iteration='')#, subplot = 221) if calnum == 0: datacol='data' else: datacol='corrected' plotms(vis=avg_data, xaxis='time', yaxis='phase', xdatacolumn=datacol, ydatacolumn=datacol, avgtime='15s', avgchannel=avgchannel_narrow, coloraxis='corr', antenna=ant+'&'+ant2, overwrite=True, title='Iteration %i for spw %i and ant %s-%s. datacol=%s' % (calnum,spwn,ant,ant2,datacol), plotfile=outdir+'selfcal%i_spw%i_ant%s-%s_phasetime.png' % (calnum,spwn,ant,ant2),) plotms(vis=avg_data, xaxis='time', yaxis='amp', xdatacolumn=datacol, ydatacolumn=datacol, avgtime='15s', avgchannel=avgchannel_narrow, coloraxis='corr', antenna=ant+'&'+ant2, overwrite=True, title='Iteration %i for spw %i and ant %s-%s. datacol=%s' % (calnum,spwn,ant,ant2,datacol), plotfile=outdir+'selfcal%i_spw%i_ant%s-%s_amptime.png' % (calnum,spwn,ant,ant2),) plotms(vis=avg_data, xaxis='phase', yaxis='amp', xdatacolumn=datacol, ydatacolumn=datacol, avgtime='60s', avgchannel=avgchannel_narrow, coloraxis='corr', antenna=ant+'&'+ant2, overwrite=True, title='Iteration %i for spw %i and ant %s-%s. datacol=%s' % (calnum,spwn,ant,ant2,datacol), plotfile=outdir+'selfcal%i_spw%i_ant%s-%s_phaseamp.png' % (calnum,spwn,ant,ant2),) plotcal(caltable=caltable, xaxis='time', yaxis='phase', plotrange=[0,0,-180,180], showgui=INTERACTIVE, figfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_phasevstime.png' % (calnum,spwn), iteration='spw' if INTERACTIVE else '')#, subplot = 221) plotcal(caltable=caltable, xaxis='antenna', yaxis='phase', showgui=INTERACTIVE, figfile=outdir+'selfcal%i_spw%i_phasevsantenna.png' % (calnum,spwn), iteration='') plotcal(caltable=caltable, xaxis='time', yaxis='amp', plotrange=[0,0,0.5,1.5], showgui=INTERACTIVE, figfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_ampvstime.png' % (calnum,spwn), iteration='spw' if INTERACTIVE else '')#, subplot = 221) #plotcal(caltable=caltable, # xaxis='phase', yaxis='amp', # plotrange=[-50,50,0.5,1.5], # showgui=INTERACTIVE, # figfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_ampvsphase.png' % (calnum,spwn), # iteration='spw' if INTERACTIVE else '')#, subplot = 221) # THERE WILL BE WEIRD "LUSTRE" ERRORS GENERATED BY THE FILE SYSTEM. DO # NOT FREAK OUT. These are just a feature of our fast file # system. Plotcal will still work. # It can be useful useful to plot the X-Y solutions (i.e., differences # between polarizations) as an indicator of the noise in the # solutions. plotcal(caltable=caltable, xaxis='time', yaxis='phase', plotrange=[0,0,-25, 25], poln = '/', showgui=INTERACTIVE, iteration='spw,antenna' if INTERACTIVE else '', figfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_poldiff.png' % (calnum,spwn), subplot = 221 if INTERACTIVE else 111) plotms(vis=avg_data, xaxis='uvdist', yaxis='amp', xdatacolumn='corrected', ydatacolumn='corrected', avgtime='1e8s', avgchannel=avgchannel_narrow, coloraxis='baseline', overwrite=True, title='Iteration %i for spw %i' % (calnum,spw), plotfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_uvdistamp.png' % (calnum,spwn), ) #plotms(vis=avg_data, # xaxis='phase', # yaxis='amp', # xdatacolumn='corrected', # ydatacolumn='corrected', # avgtime='60s', # avgchannel=avgchannel_narrow, # coloraxis='corr', # overwrite=True, # title='Iteration %i for spw %i' % (calnum,spw), # plotfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_phaseamp.png' % (calnum,spwn), # ) plotms(vis=avg_data, xaxis='time', yaxis='amp', xdatacolumn='corrected', ydatacolumn='corrected', avgtime='10s', avgchannel=avgchannel_narrow, coloraxis='baseline', overwrite=True, title='Iteration %i for spw %i' % (calnum,spw), plotfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_amptime.png' % (calnum,spwn), ) # The rms noise is about 4 to 8 deg, depending on antenna, but the # phase changes are considerably larger. This indicates that the # application of this solution will improve the image. # =%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=% # APPLY THE CALIBRATION # =%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=% # # If you are satisfied with your solution, you can now apply it to the # data to generate a new corrected data column, which you can then # image. Be sure to save the previous flags before you do so because # applycal will flag data without good solutions. The commented # command after the applycal will roll back to the saved solution in # case you get in trouble. # # flagmanager(vis=avg_data, # mode='save', # versionname='before_selfcal_apply') # 2013-03-04 19:53:37 SEVERE agentflagger:: (file /opt/casa/stable-2013-02/gcwrap/tools/flagging/agentflagger_cmpt.cc, line 37) Exception Reported: Invalid Table operation: ArrayColumn::setShape; shape cannot be changed for row 0 column FLAG # *** Error *** Invalid Table operation: ArrayColumn::setShape; shape cannot be changed for row 0 column FLAG if reclean: applycal(vis=avg_data, gaintable=caltable, interp='linear', flagbackup=True) # was False when flagmanager was used # (6) Plot corrected phase vs. amp for the antennas you picked out in (4), # to check that in fact the corrections have been applied as expected. for ant2 in ant2list: for ant in ant1list: plotms(vis=avg_data, xaxis='time', yaxis='phase', xdatacolumn='corrected', ydatacolumn='corrected', avgtime='15s', avgchannel=avgchannel_narrow, coloraxis='corr', antenna=ant+'&'+ant2, overwrite=True, title='Iteration %i for spw %i and ant %s-%s' % (calnum,spwn,ant,ant2), plotfile=outdir+'selfcal%i_spw%i_ant%s-%s_phasetime_applied.png' % (calnum,spwn,ant,ant2),) plotms(vis=avg_data, xaxis='time', yaxis='amp', xdatacolumn='corrected', ydatacolumn='corrected', avgtime='60s', avgchannel=avgchannel_narrow, coloraxis='corr', antenna=ant+'&'+ant2, overwrite=True, title='Iteration %i for spw %i and ant %s-%s' % (calnum,spwn,ant,ant2), plotfile=outdir+'selfcal%i_spw%i_ant%s-%s_amptime_applied.png' % (calnum,spwn,ant,ant2),) plotms(vis=avg_data, xaxis='phase', yaxis='amp', xdatacolumn='corrected', ydatacolumn='corrected', avgtime='60s', avgchannel=avgchannel_narrow, coloraxis='corr', antenna=ant+'&'+ant2, overwrite=True, title='Iteration %i for spw %i and ant %s-%s' % (calnum,spwn,ant,ant2), plotfile=outdir+'selfcal%i_spw%i_ant%s-%s_phaseamp_applied.png' % (calnum,spwn,ant,ant2),) plotms(vis=vis, spw='0', xaxis='freq', yaxis='phase', avgtime='1e8', avgscan=T, coloraxis='corr', iteraxis='baseline', xselfscale=T, yselfscale=T, antenna=ant+'&'+ant2, title='Phase vs Freq with time averaging for spw %i ant %s-%s iter %i' % (spwn,ant,ant2,calnum), plotfile=outdir+'phasevsfreq_spw%i_ant%s-%s_selfcal%i.png' % (spwn,ant,ant2,calnum), field=field, overwrite=True, ) # Use this command to roll back to the previous flags in the event of # an unfortunate applycal. #flagmanager(vis=avg_data, # mode='restore', # versionname='before_selfcal_apply') if reclean: selfcal_image = 'spw%i_C_C_selfcal%i' % (spwn,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+selfcal_image+suffix) clean(vis=avg_data,imagename=selfcal_image,field=field, mode='mfs', psfmode='hogbom',multiscale=multiscale, weighting='briggs', robust=robust, niter=1000, imsize=imsize, nterms=2, mask=cleanboxes, usescratch=True) exportfits(imagename=selfcal_image+".image.tt0", fitsimage=selfcal_image+".fits", overwrite=True) plotms(vis=avg_data, spw='0', xaxis='baseline', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Baseline after CSCLEAN iter %i' % calnum, plotfile=outdir+'post_selfcal%i_spw%i_residVSbaseline.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=avg_data, spw='0', xaxis='time', yaxis='amp', avgtime='5s', ydatacolumn='corrected-model', coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Time after CSCLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVStime.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=avg_data, spw='0', xaxis='uvdist', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. UVDIST after CSCLEAN iter %i' % (calnum) , plotfile=outdir+'post_selfcal%i_spw%i_residVSuvdist.png' % (calnum,spwn), field=field, overwrite=True, ) imrms.append(imstat(selfcal_image+".image.tt0",box=statsbox)['rms']) viewer(selfcal_image+".image.tt0", outfile=outdir+selfcal_image+".image.tt0.png", outformat='png', gui=False) print "FINISHED ITERATION %i" % calnum print "FINISHED ITERATING!!! YAY!" # final phase + gain cal: # http://casaguides.nrao.edu/index.php?title=Calibrating_a_VLA_5_GHz_continuum_survey#One_Last_Iteration:_Amplitude_.26_Phase_Self_Calibration aptable = 'selfcal_ap_%s_spw%i.gcal' % (field.replace(" ",""),spwn) gaincal(vis=avg_data, field='', caltable=aptable, gaintable=caltable, spw='', solint='inf', refant=refant, calmode='ap', combine='', minblperant=4) plotcal(caltable=aptable, xaxis='phase', yaxis='amp', plotrange=[-50,50,0.5,1.5], showgui=INTERACTIVE, figfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_ampvsphase_final.png' % (calnum,spwn), iteration='spw' if INTERACTIVE else '')#, subplot = 221) applycal(vis=avg_data, gaintable=[aptable,caltable], interp='linear', flagbackup=True) # was False when flagmanager was used selfcal_image = 'spw%i_C_C_selfcal%i_final' % (spwn,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+selfcal_image+suffix) clean(vis=avg_data,imagename=selfcal_image,field=field, mode='mfs', mask=cleanboxes, weighting='briggs', robust=robust, niter=10000, imsize=imsize, nterms=2, usescratch=True) exportfits(imagename=selfcal_image+".image.tt0", fitsimage=selfcal_image+".fits", overwrite=True) plotms(vis=avg_data, spw='0', xaxis='baseline', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Baseline after CSCLEAN iter %i' % calnum, plotfile=outdir+'post_selfcal%i_spw%i_residVSbaseline.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=avg_data, spw='0', xaxis='time', yaxis='amp', avgtime='5s', ydatacolumn='corrected-model', coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Time after CSCLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVStime.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=avg_data, spw='0', xaxis='uvdist', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. UVDIST after CSCLEAN iter %i' % (calnum) , plotfile=outdir+'post_selfcal%i_spw%i_residVSuvdist.png' % (calnum,spwn), field=field, overwrite=True, ) selfcal_image = 'spw%i_C_C_selfcal%i_final_multiscale' % (spwn,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+selfcal_image+suffix) clean(vis=avg_data,imagename=selfcal_image,field=field, mode='mfs', imagermode='csclean',# mask=cleanboxes, multiscale=multiscale, psfmode='hogbom', nterms=2, weighting='briggs', robust=robust, niter=10000, imsize=imsize, usescratch=True) exportfits(imagename=selfcal_image+".image.tt0", fitsimage=selfcal_image+".fits", overwrite=True) plotms(vis=avg_data, spw='0', xaxis='baseline', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Baseline after multiscale CLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVSbaseline_multiscale.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=avg_data, spw='0', xaxis='time', yaxis='amp', avgtime='5s', ydatacolumn='corrected-model', coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Time after multiscale CLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVStime_multiscale.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=avg_data, spw='0', xaxis='uvdist', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. UVDIST after multiscale CLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVSuvdist_multiscale.png' % (calnum,spwn), field=field, overwrite=True, ) return imrms
def CMEuvmodelfit(MS, args, truth=True, qs=None, dqs=None, largestBL=1.5e4): ''' This function fits a uvmodel fit to a MS and compares the fit with actual values from the truth image. MS: the name of teh MS file sourceInitial: a text file which contains initial estimates of the component to be fitted truthPos: actual position of the component in the truth image ''' print 'start of uvmodelfit' res = 1024 #No breaky if not truth raimout = -1. decimout = -1. majimout = -1. minimout = -1. paimout = -1. diff = defaultdict(list) ##get the info for truth image if truth: ### xmax, ymax = imstat('CME-' + MS[9:18] + '.truth')['maxposf'].split(',')[:2] raTruth = qa.toangle(xmax)['value'] * np.pi / 180. decTruth = qa.toangle(ymax)['value'] * np.pi / 180. print imstat('CME-' + MS[9:18] + '.truth')['maxposf'] ##Imstat truth stuff into log?? CMEimstatst = imstat('CME-' + MS[9:18] + '.truth') xmaxp, ymaxp = CMEimstatst['maxpos'][:2] boxhalfwidth = 100 bx = str(max(xmaxp - boxhalfwidth, 0)) + ', ' + str( max(ymaxp - boxhalfwidth, 0)) + ', ' + str( min(xmaxp + boxhalfwidth, res - 1)) + ', ' + str( min(ymaxp + boxhalfwidth, res - 1)) ##addcomponent try: imfitout = imfit(imagename='CME-' + MS[9:18] + '.truth', box=bx) except: print 'failed imfit on truth image ' + MS return raimout = imfitout['results']['component0']['shape']['direction'][ 'm0']['value'] decimout = imfitout['results']['component0']['shape']['direction'][ 'm1']['value'] majimout = imfitout['results']['component0']['shape']['majoraxis'][ 'value'] minimout = imfitout['results']['component0']['shape']['minoraxis'][ 'value'] paimout = imfitout['results']['component0']['shape']['positionangle'][ 'value'] ##ADDing imfitout diff[MS].append([raimout, decimout]) diff[MS].append([majimout, minimout]) diff[MS].append([paimout, 0.0]) ### if not truth: #Hegedus add in initial pos prediction from brightest point in dirty image if args.correlate: #create baseline plots #plots print 'blah1' polarfig = figure() print 'figure created' absq = abs(qs.flatten()) angq = arcsin(qs.flatten().imag) todel = [] for i in range(len(angq)): if math.isnan(abs(angq[i])): todel.append(i) print i, ' wah bad angle' continue polar([0, angq[i]], [0, absq[i]], marker='o') for i in range(len(todel) - 1, -1, -1): angq = np.delete(angq, todel[i]) absq = np.delete(absq, todel[i]) title( "Ratios of CASA Computed and Correlated Visibilities\n 1.0 at 0 deg is perfect" ) # xlabel('Relative Error') # ylabel('Bin Count') savefig('VisRelDiff%s.png' % MS, bbox_inches='tight', pad_inches=1) hist(angq, bins=4) title("Histogram of Visibility Ratios, 0 deg is perfect") # savefig('VisRelDiffHist%s.png' % MS) plt.close(polarfig) #plot hist of absq histplot = figure() hist(absq, bins=20) title("Histogram of abs(Visibility) Ratios, 1.0 is perfect") xlabel("abs(casa vis/correlated vis)") savefig('VisAbsRatioHist%s.png' % MS) plt.close(histplot) # #create dirty baseline plots #plots polarfig = figure() absq = abs(dqs.flatten()) angq = arcsin(dqs.flatten().imag) todel = [] for i in range(len(angq)): if math.isnan(abs(angq[i])): todel.append(i) print i, ' wah bad angle' continue polar([0, angq[i]], [0, absq[i]], marker='o') for i in range(len(todel) - 1, -1, -1): angq = np.delete(angq, todel[i]) absq = np.delete(absq, todel[i]) title( "Ratios of CASA Computed and Noisy Correlated Visibilities \n 1.0 at 0 deg is perfect" ) # xlabel('Relative Error') # ylabel('Bin Count') savefig('NoisyVisRelDiff%s.png' % MS, bbox_inches='tight', pad_inches=1) hist(angq) title("Histogram of Noisy Visibility Ratios, 0 deg is perfect") # savefig('NoisyVisRelDiffHist%s.png' % MS) plt.close(polarfig) #plot hist of absq histplot = figure() hist(absq, bins=20) title("Histogram of Noisy abs(Visibility) Ratios, 1.0 is perfect") savefig('NoisyVisAbsRatioHist%s.png' % MS) plt.close(histplot) print 'cleaning dirty image' hz = float(MS[11:16]) * 10**6 wavelen = 3e8 / hz cellsizerad = wavelen / largestBL / 16. csas = cellsizerad * 180. / np.pi * 3600 imWidth = 1024. * 60 #arcsec, 1024 res built in #csas = cellsizerad imsize = int(imWidth / csas) #int(1024/60.*3600./csas) print 'pre imsize is ' + str(imsize) if imsize < 1: imsize = 1024 #use next power of 2 imsize = int(pow(2, math.ceil(math.log(imsize, 2)))) / 2 print 'imsize used is ' + str(imsize) print 'cell size for ' + MS + ' is ' + str(csas) + ' arcsec' try: clean( vis=MS, imagename=MS + '.dirty', outlierfile='', field='', spw='', selectdata=False, mode='mfs', nterms=1, gridmode='', #widefield', wprojplanes = -1, facets = 1, niter=0, gain=0.1, threshold='0.0mJy', interactive=False, mask=[], imsize=[imsize, imsize], #[1024, 1024], cell=[str(csas) + 'arcsec', str(csas) + 'arcsec'], #cell=['1arcsec', '1arcsec'], phasecenter='', stokes='I', weighting='briggs', robust=-0.5, uvtaper=False, usescratch=False, allowchunk=False) except: print 'Clean wanted to crash on ' + MS pass print 'done cleaning, now getting brightest dirty point' try: CMEimstats = imstat(MS + '.dirty.image') except: print "Error, Cannot read image, exiting on " + str(MS) return if type(CMEimstats) == type(None) or type(CMEimstats) == type(True): print "Error Couldn't clean to image, exiting on " + MS return print CMEimstats['maxposf'] #Get importuvfits xmaxp, ymaxp = CMEimstats['maxpos'][:2] print 'max x y of dirty is ' + str(xmaxp) + ', ' + str(ymaxp) boxhalfwidth = int(imsize / 40) #[25, 25, 10] # boxhalfwidth = boxhalfwidths[ind] print 'boxhalfwidth ', boxhalfwidth bx = str(max(xmaxp - boxhalfwidth, 0)) + ', ' + str( max(ymaxp - boxhalfwidth, 0)) + ', ' + str( min(xmaxp + boxhalfwidth, imsize - 1)) + ', ' + str( min(ymaxp + boxhalfwidth, imsize - 1)) print 'box is ' + bx try: imfitout = imfit(imagename=MS + '.dirty.image', box=bx) except: print "Error, Cannot fit dirty image, exiting on " + str(MS) xmax, ymax = CMEimstats['maxposf'].split(',')[:2] raTruth = qa.toangle(xmax)['value'] * np.pi / 180. decTruth = qa.toangle(ymax)['value'] * np.pi / 180. diff[MS].append([raTruth, decTruth]) diff[MS].append([-1., -1.]) diff[MS].append([0.0, 0.0]) np.savetxt('uvfits_' + MS + '.txt', diff[MS]) return try: raimout = imfitout['results']['component0']['shape']['direction'][ 'm0']['value'] except: print "Error, Cannot fit dirty image get component 0 or output, exiting on " + str( MS) xmax, ymax = CMEimstats['maxposf'].split(',')[:2] raTruth = qa.toangle(xmax)['value'] * np.pi / 180. decTruth = qa.toangle(ymax)['value'] * np.pi / 180. diff[MS].append([raTruth, decTruth]) diff[MS].append([-1., -1.]) diff[MS].append([0.0, 0.0]) np.savetxt('uvfits_' + MS + '.txt', diff[MS]) return raimout = imfitout['results']['component0']['shape']['direction'][ 'm0']['value'] decimout = imfitout['results']['component0']['shape']['direction'][ 'm1']['value'] majimout = imfitout['results']['component0']['shape']['majoraxis'][ 'value'] minimout = imfitout['results']['component0']['shape']['minoraxis'][ 'value'] paimout = imfitout['results']['component0']['shape']['positionangle'][ 'value'] cl.close() diff[MS].append([raimout, decimout]) diff[MS].append([majimout, minimout]) diff[MS].append([paimout, 0.0]) np.savetxt('uvfits_' + MS + '.txt', diff[MS]) return
def runFiltering(self, antennaCfg, trackDuration, frequency, dec, imageTotalSize, resolutionStart, resolutionEnd, resolutionStep): """ use the disk100.fits image of a uniform disk of 100 pixel size (diameter) Run a set of simulations to account for the filtering at different scales. antennaCfg: antenna configuration file trackDuration : trackDuration resolutionStart,resolutionEnd,resolutionStep: range for the resolution to simulate dec: declination OUTPUT: resolution, flux: resolution and flux output (1 Jy in entry) """ maskClean = [70, 70, 180, 180] projectName = "tempCurveFiltering" nStep = math.floor((resolutionEnd - resolutionStart) / resolutionStep) resolutionArr = np.arange(nStep) * resolutionStep + resolutionStart flux = np.zeros(nStep) boxStat = '%d,%d,%d,%d' % (maskClean[0], maskClean[1], maskClean[2], maskClean[3]) os.system("rm -Rf *%s*" % (projectName)) if dec <= -10: decString = "J2000 10h00m00s %3dd00m00s" % (dec) elif dec < 0 and dec > -10: decString = "J2000 10h00m00s -0%1dd00m00s" % (-dec) elif dec >= 0 and dec < 10: decString = "J2000 10h00m00s +0%1dd00m00s" % (dec) elif dec >= 10: decString = "J2000 10h00m00s +%2dd00m00s" % (dec) # simulayion with one arcsec component ##os.system("rm -Rf *%s*"%(projectName)) print decString index = 0 print resolutionArr for res in resolutionArr: resolutionDisk = "%4.2farcsec" % (res / 100.) print resolutionDisk print antennaCfg so.sim_observe( project=projectName + "%d" % (index), skymodel='disk100.fits', inbright='1Jy/pixel', indirection=decString, incell=resolutionDisk, incenter=frequency, inwidth='8GHz', antennalist=antennaCfg, totaltime=trackDuration, integration='10s', direction=decString, maptype='ALMA', # mapsize = ['2arcmin','2arcmin'], # pointingspacing = "2arcmin" ) sa.sim_analyze( project=projectName + "%d" % (index), image=True, imsize=imageTotalSize, # cell = cellsize, mask=maskClean, niter=2000, threshold='0.1mJy', graphics='file') imageName = projectName + "%d/" % ( index) + projectName + "%d.%s.image" % ( index, antennaCfg.split('.')[0]) # imageName=projectName+"%d/"%(index)+projectName+"%d.image"%(index) print "Read %s" % (imageName) ia.open(imageName) # flux0=ia.statistics()['flux'][0] stat = ims.imstat( imagename=imageName, box=boxStat, ) flux0 = stat['flux'][0] print flux0 if res == resolutionArr[0]: fluxNormalization = flux0 fluxRes = flux0 / fluxNormalization print "Flux: %4.3f" % (fluxRes) flux[index] = fluxRes ia.close() index += 1 return (resolutionArr, flux)
imWidthDeg=10., nIter=0, Threshold='3000000.0Jy') #add noise addThermalNoise(lunarMS, SEFD=-1., dv=10000., dt=60.) #image imageData(lunarMS, truthImage, imWidthDeg=10., nIter=0, Threshold='3000000.0Jy') #fit data truthData = imstat(truthImage) brightPoint = truthData['maxposf'].split(',') bFlux = truthData['max'][0] rastring = brightPoint[0] decstring = brightPoint[1] brightDirTruth = me.direction('J2000', rastring, decstring) ravaltruth = brightDirTruth['m0']['value'] #returns radians decvaltruth = brightDirTruth['m1']['value'] #returns radians print( 'True Input Params (flux (Jy), ra (rad), dec (rad), major (arcsec), minor (arcsec), PA (degrees))' ) print( '%2.4f, %2.4f, %2.4f, %2.4f, %2.4f, %2.4f' % (bFlux, ravaltruth, decvaltruth, 0., 0., 0.)
def selfcal(vis, spwn=6, doplots=True, INTERACTIVE=False, reclean=True, field='W51 Ku', outdir_template="spw%i_selfcal_iter/", statsbox='170,50,229,97', ant1list=['ea14','ea05'], ant2list=['ea16','ea07'], avgchannel_wide='128', avgchannel_narrow='8', cleanboxes=cleanboxes, refant='ea27', solint='30s', niter=2, multiscale=[0,5,10,15,25,50], imsize=512, ): """ Docstring incomplete """ spw = int(spwn) outdir = outdir_template % spwn try: os.mkdir(outdir) except OSError: pass # you're supposed to pass in avg_data as input avg_data = vis mytb.open(vis+"/ANTENNA") antnames = mytb.getcol("NAME") # (1) Clean a single SPW *interactively*, boxing the brightest regions and not # cleaning very deeply (maybe 100 iterations). Keep this model in the header # -- it's what you'll use for the first round of self-calibration. if reclean: imagename="average_spw%i_shallowclean_masked" % spwn for suffix in clean_output_suffixes: os.system("rm -rf "+imagename+suffix) clean(vis=avg_data, field=field, imagename=imagename, mode='mfs', psfmode='hogbom',multiscale=multiscale, weighting='briggs', robust=0.0, niter=100, imsize=imsize, mask=cleanboxes, nterms=2, usescratch=True) viewer(imagename+".image.tt0", outfile=outdir+imagename+".image.tt0.png", outformat='png', gui=False) exportfits(imagename=imagename+".image.tt0", fitsimage=imagename+".fits", overwrite=True) imrms = [imstat(imagename+".image.tt0",box=statsbox)['rms']] for calnum in xrange(niter): # for Ku D W51 Ku spw 2 if reclean: first_image = 'spw%i_ku_d_firstim_selfcal%i' % (spwn,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+first_image+suffix) clean(vis=avg_data,imagename=first_image,field=field, mode='mfs', psfmode='hogbom',multiscale=multiscale, weighting='briggs', robust=0.0, niter=100, imsize=imsize, mask=cleanboxes, nterms=2, usescratch=True) exportfits(imagename=first_image+".image.tt0", fitsimage=first_image+".fits", overwrite=True) viewer(first_image+".image.tt0", outfile=outdir+first_image+".image.tt0.png", outformat='png', gui=False) caltable = 'selfcal%i_%s_spw%i.pcal' % (calnum,field.replace(" ",""),spwn) if reclean: os.system('rm -rf '+caltable) gaincal(vis=avg_data, field='', caltable=caltable, spw='', # gaintype = 'T' could reduce failed fit errors by averaging pols... gaintype='G', # 'G' from http://casaguides.nrao.edu/index.php?title=EVLA_Advanced_Topics_3C391 solint=solint, refant=refant, calmode='p', combine='scan', minblperant=4) if reclean: applycal(vis=avg_data, gaintable=caltable, interp='linear', flagbackup=True) # was False when flagmanager was used if reclean: selfcal_image = 'spw%i_ku_d_selfcal%i' % (spwn,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+selfcal_image+suffix) clean(vis=avg_data,imagename=selfcal_image,field=field, mode='mfs', psfmode='hogbom',multiscale=multiscale, weighting='briggs', robust=0.5, niter=1000, imsize=imsize, nterms=2, mask=cleanboxes, usescratch=True) exportfits(imagename=selfcal_image+".image.tt0", fitsimage=selfcal_image+".fits", overwrite=True) imrms.append(imstat(selfcal_image+".image.tt0",box=statsbox)['rms']) viewer(selfcal_image+".image.tt0", outfile=outdir+selfcal_image+".image.tt0.png", outformat='png', gui=False) print "FINISHED ITERATION %i" % calnum print "FINISHED ITERATING!!! YAY!" # final phase + gain cal: # http://casaguides.nrao.edu/index.php?title=Calibrating_a_VLA_5_GHz_continuum_survey#One_Last_Iteration:_Amplitude_.26_Phase_Self_Calibration aptable = 'selfcal_ap_%s_spw%i.gcal' % (field.replace(" ",""),spwn) gaincal(vis=avg_data, field='', caltable=aptable, gaintable=caltable, spw='', solint='inf', refant=refant, calmode='ap', combine='', minblperant=4) plotcal(caltable=aptable, xaxis='phase', yaxis='amp', plotrange=[-50,50,0.5,1.5], showgui=INTERACTIVE, figfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_ampvsphase_final.png' % (calnum,spwn), iteration='spw' if INTERACTIVE else '')#, subplot = 221) applycal(vis=avg_data, gaintable=[aptable,caltable], interp='linear', flagbackup=True) # was False when flagmanager was used selfcal_image = 'spw%i_ku_d_selfcal%i_final' % (spwn,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+selfcal_image+suffix) clean(vis=avg_data,imagename=selfcal_image,field=field, mode='mfs', mask=cleanboxes, weighting='briggs', robust=0.5, niter=10000, imsize=imsize, nterms=2, usescratch=True) exportfits(imagename=selfcal_image+".image.tt0", fitsimage=selfcal_image+".fits", overwrite=True) plotms(vis=avg_data, spw='0', xaxis='baseline', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Baseline after CSCLEAN iter %i' % calnum, plotfile=outdir+'post_selfcal%i_spw%i_residVSbaseline.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=avg_data, spw='0', xaxis='time', yaxis='amp', avgtime='5s', ydatacolumn='corrected-model', coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Time after CSCLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVStime.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=avg_data, spw='0', xaxis='uvdist', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. UVDIST after CSCLEAN iter %i' % (calnum) , plotfile=outdir+'post_selfcal%i_spw%i_residVSuvdist.png' % (calnum,spwn), field=field, overwrite=True, ) selfcal_image = 'spw%i_ku_d_selfcal%i_final_multiscale' % (spwn,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+selfcal_image+suffix) clean(vis=avg_data,imagename=selfcal_image,field=field, mode='mfs', imagermode='csclean',# mask=cleanboxes, multiscale=multiscale, psfmode='hogbom', nterms=2, weighting='briggs', robust=0.5, niter=10000, imsize=imsize, usescratch=True) exportfits(imagename=selfcal_image+".image.tt0", fitsimage=selfcal_image+".fits", overwrite=True) plotms(vis=avg_data, spw='0', xaxis='baseline', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Baseline after multiscale CLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVSbaseline_multiscale.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=avg_data, spw='0', xaxis='time', yaxis='amp', avgtime='5s', ydatacolumn='corrected-model', coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Time after multiscale CLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVStime_multiscale.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=avg_data, spw='0', xaxis='uvdist', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. UVDIST after multiscale CLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVSuvdist_multiscale.png' % (calnum,spwn), field=field, overwrite=True, ) return imrms
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
outfile=dir_image + name_line + ".moment0_tmp") immoments( imagename=cubeimage + ".pbcor.masked", moments=[1], #includepix = [noises[i]*3.0,10000.], outfile=dir_image + name_line + ".moment1_tmp") immoments( imagename=cubeimage + ".pbcor.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 * percents[i]) + ", IM1, 0.0)", outfile=dir_image + name_line + ".moment0") immath(imagename=[ dir_image + name_line + ".moment0_tmp", dir_image + name_line + ".moment1_tmp" ], expr="iif( IM0>=" + str(peak * percents[i]) + ", IM1, 0.0)", outfile=dir_image + name_line + ".moment1")
title( 'RMSE as a function of Time with %i Spacecraft and cleaned for %i iterations' % (numsc, iters)) ylabel("RMSE") xlabel("Minutes used") savefig('rmses%iiters%i.png' % (numsc, iters)) plt.close(fig1) np.savetxt('rmses' + str(numsc) + 'iters%d.txt' % iters, np.array(rmses).flatten(), fmt='%.7f') #now analyze pointing errors truthMaxPos = imstat(imageName)['maxposf'].split() maxRA = truthMaxPos[0][:-1] maxDec = truthMaxPos[1][:-1] tFeatDir = me.direction('J2000', maxRA, maxDec) maxRArad = tFeatDir['m0']['value'] maxDecrad = tFeatDir['m1']['value'] dirtyMaxPos = imstat(RelicMergeMS + '.' + str(iters) + 'dirty' + str(length - 1) + '.image')['maxposf'].split() dmaxRA = dirtyMaxPos[0][:-1] dmaxDec = dirtyMaxPos[1][:-1] dFeatDir = me.direction('J2000', dmaxRA, dmaxDec)
def selfcal(vis, spw='6', doplots=True, INTERACTIVE=False, reclean=True, field='W51 Ku', outdir_template="spw%i_selfcal_iter/", statsbox='170,50,229,97', ant1list=['ea14','ea05'], ant2list=['ea16','ea07'], avgchannel_wide='128', avgchannel_narrow='8', cleanboxes="", refant='ea27', solint='30s', niter=2, multiscale=[0,3,6,12,24,48,96], imsize=512, cell='0.1arcsec', weighting='uniform', robust=0.0, minsnr=3, psfmode='clark', openviewer=False, shallowniter=100, midniter=1000, deepniter=1e4, minblperant=4, gaintype='G'): """ Docstring incomplete """ # Jan 2016: the fact that I have to make these declarations indicates that # this code never actually worked. spwn = int(spw) split(vis=vis, outputvis="selfcal_copy_{0}".format(vis)) vis_for_selfcal = "selfcal_copy_{0}".format(vis) outdir = outdir_template % spwn if not os.path.exists(outdir): os.mkdir(outdir) shallowniter = int(shallowniter) midniter = int(midniter) deepniter = int(deepniter) fieldstr = field.replace(" ","") # (1) Clean a single SPW *interactively*, boxing the brightest regions and not # cleaning very deeply (maybe 100 iterations). Keep this model in the header # -- it's what you'll use for the first round of self-calibration. # # Those are the official directions. They are nonsense when dealing with # the extended emission of W51. if reclean: imagename="selfcal_spw%i_shallowclean_iter0" % int(spw) for suffix in clean_output_suffixes: os.system("rm -rf "+imagename+suffix) clean(vis=vis_for_selfcal, field=field, imagename=imagename, mode='mfs', psfmode=psfmode, multiscale=multiscale, weighting=weighting, robust=robust, niter=shallowniter, imsize=imsize, cell=cell, mask=cleanboxes, nterms=1, interactive=INTERACTIVE, usescratch=True) exportfits(imagename=imagename+".image", fitsimage=imagename+".fits", overwrite=True, dropdeg=True) imrms = [imstat(imagename+".image",box=statsbox)['rms']] for calnum in xrange(niter): if reclean: first_image = 'selfcal_{0}_{1}_firstim_selfcal{2}'.format(fieldstr, spw, calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+first_image+suffix) clean(vis=vis_for_selfcal,imagename=first_image,field=field, mode='mfs', psfmode=psfmode, multiscale=multiscale, weighting=weighting, robust=robust, niter=shallowniter, imsize=imsize, mask=cleanboxes, cell=cell, nterms=1, usescratch=True) exportfits(imagename=first_image+".image", fitsimage=first_image+".fits", overwrite=True, dropdeg=True) caltable = 'selfcal%i_%s_spw%i.pcal' % (calnum,fieldstr,spwn) if reclean: os.system('rm -rf '+caltable) gaincal(vis=vis_for_selfcal, field='', caltable=caltable, spw='', # gaintype = 'T' could reduce failed fit errors by averaging pols... gaintype=gaintype, # 'G' from http://casaguides.nrao.edu/index.php?title=EVLA_Advanced_Topics_3C391 solint=solint, refant=refant, calmode='p', combine='scan', minsnr=minsnr, minblperant=minblperant) # Watch out for failed solutions noted in the terminal during this # solution. If you see a large fraction (really more than 1 or 2) of # your antennas failing to converge in many time intervals then you # may need to lengthen the solution interval. # =%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=% # INSPECT THE CALIBRATION # =%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=% # After you have run the gaincal, you want to inspect the # solution. Use PLOTCAL to look at the solution (here broken into # panels by SPW with individual antennas mapped to colors). Look at # the overall magnitude of the correction to get an idea of how # important the selfcal is and at how quickly it changes with time to # get an idea of how stable the instrument and atmosphere were. # if doplots: for ant2 in ant2list: for ant in ant1list: # (4) Have a look at the gain solutions by antenna. Which antennas # have the largest phase corrections? Before applying the # calibration, use plotms to display the corrected phase vs. amp # for these antennas, to compare with *after* the correction is # applied. plotcal(caltable=caltable, xaxis='time', yaxis='phase', showgui=INTERACTIVE, antenna=ant+'&'+ant2, figfile=outdir+'selfcal%i_spw%i_phasevstime_ant%s-%s.png' % (calnum,spwn,ant,ant2), iteration='')#, subplot = 221) #plotcal(caltable=caltable, xaxis='amp', yaxis='phase', # showgui=INTERACTIVE, # antenna=ant, # figfile=outdir+'selfcal%i_spw%i_phasevsamp_ant%s.png' % (calnum,spwn,ant), # iteration='')#, subplot = 221) if calnum == 0: datacol='data' else: datacol='corrected' plotms(vis=vis_for_selfcal, xaxis='time', yaxis='phase', xdatacolumn=datacol, ydatacolumn=datacol, avgtime='15s', avgchannel=avgchannel_narrow, coloraxis='corr', antenna=ant+'&'+ant2, overwrite=True, title='Iteration %i for spw %i and ant %s-%s. datacol=%s' % (calnum,spwn,ant,ant2,datacol), showgui=INTERACTIVE, plotfile=outdir+'selfcal%i_spw%i_ant%s-%s_phasetime.png' % (calnum,spwn,ant,ant2),) plotms(vis=vis_for_selfcal, xaxis='time', yaxis='amp', xdatacolumn=datacol, ydatacolumn=datacol, avgtime='15s', avgchannel=avgchannel_narrow, coloraxis='corr', antenna=ant+'&'+ant2, overwrite=True, title='Iteration %i for spw %i and ant %s-%s. datacol=%s' % (calnum,spwn,ant,ant2,datacol), showgui=INTERACTIVE, plotfile=outdir+'selfcal%i_spw%i_ant%s-%s_amptime.png' % (calnum,spwn,ant,ant2),) plotms(vis=vis_for_selfcal, xaxis='phase', yaxis='amp', xdatacolumn=datacol, ydatacolumn=datacol, avgtime='60s', avgchannel=avgchannel_narrow, coloraxis='corr', antenna=ant+'&'+ant2, overwrite=True, title='Iteration %i for spw %i and ant %s-%s. datacol=%s' % (calnum,spwn,ant,ant2,datacol), showgui=INTERACTIVE, plotfile=outdir+'selfcal%i_spw%i_ant%s-%s_phaseamp.png' % (calnum,spwn,ant,ant2),) plotcal(caltable=caltable, xaxis='time', yaxis='phase', plotrange=[0,0,-180,180], showgui=INTERACTIVE, figfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_phasevstime.png' % (calnum,spwn), iteration='spw' if INTERACTIVE else '')#, subplot = 221) plotcal(caltable=caltable, xaxis='antenna', yaxis='phase', showgui=INTERACTIVE, figfile=outdir+'selfcal%i_spw%i_phasevsantenna.png' % (calnum,spwn), iteration='') plotcal(caltable=caltable, xaxis='time', yaxis='amp', plotrange=[0,0,0.5,1.5], showgui=INTERACTIVE, figfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_ampvstime.png' % (calnum,spwn), iteration='spw' if INTERACTIVE else '')#, subplot = 221) #plotcal(caltable=caltable, # xaxis='phase', yaxis='amp', # plotrange=[-50,50,0.5,1.5], # showgui=INTERACTIVE, # figfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_ampvsphase.png' % (calnum,spwn), # iteration='spw' if INTERACTIVE else '')#, subplot = 221) # THERE WILL BE WEIRD "LUSTRE" ERRORS GENERATED BY THE FILE SYSTEM. DO # NOT FREAK OUT. These are just a feature of our fast file # system. Plotcal will still work. # It can be useful useful to plot the X-Y solutions (i.e., differences # between polarizations) as an indicator of the noise in the # solutions. plotcal(caltable=caltable, xaxis='time', yaxis='phase', plotrange=[0,0,-25, 25], poln = '/', showgui=INTERACTIVE, iteration='spw,antenna' if INTERACTIVE else '', figfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_poldiff.png' % (calnum,spwn), subplot = 221 if INTERACTIVE else 111) plotms(vis=vis_for_selfcal, xaxis='uvdist', yaxis='amp', xdatacolumn='corrected', ydatacolumn='corrected', avgtime='1e8s', avgchannel=avgchannel_narrow, coloraxis='baseline', overwrite=True, showgui=INTERACTIVE, title='Iteration %i for spw %i' % (calnum,spwn), plotfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_uvdistamp.png' % (calnum,spwn), ) #plotms(vis=vis_for_selfcal, # xaxis='phase', # yaxis='amp', # xdatacolumn='corrected', # ydatacolumn='corrected', # avgtime='60s', # avgchannel=avgchannel_narrow, # coloraxis='corr', # overwrite=True, # title='Iteration %i for spw %i' % (calnum,spw), # plotfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_phaseamp.png' % (calnum,spwn), # ) plotms(vis=vis_for_selfcal, xaxis='time', yaxis='amp', xdatacolumn='corrected', ydatacolumn='corrected', avgtime='10s', avgchannel=avgchannel_narrow, coloraxis='baseline', overwrite=True, showgui=INTERACTIVE, title='Iteration %i for spw %i' % (calnum,spwn), plotfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_amptime.png' % (calnum,spwn), ) # The rms noise is about 4 to 8 deg, depending on antenna, but the # phase changes are considerably larger. This indicates that the # application of this solution will improve the image. # =%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=% # APPLY THE CALIBRATION # =%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=% # # If you are satisfied with your solution, you can now apply it to the # data to generate a new corrected data column, which you can then # image. Be sure to save the previous flags before you do so because # applycal will flag data without good solutions. The commented # command after the applycal will roll back to the saved solution in # case you get in trouble. # # flagmanager(vis=vis_for_selfcal, # mode='save', # versionname='before_selfcal_apply') # 2013-03-04 19:53:37 SEVERE agentflagger:: (file /opt/casa/stable-2013-02/gcwrap/tools/flagging/agentflagger_cmpt.cc, line 37) Exception Reported: Invalid Table operation: ArrayColumn::setShape; shape cannot be changed for row 0 column FLAG # *** Error *** Invalid Table operation: ArrayColumn::setShape; shape cannot be changed for row 0 column FLAG if reclean: flagmanager(vis=vis_for_selfcal, mode='backup', versionname='pre_applycal1') applycal(vis=vis_for_selfcal, gaintable=caltable, interp='linear', flagbackup=False) # no need when using flagmanager flagmanager(vis=vis_for_selfcal, mode='restore', versionname='pre_applycal_1') # (6) Plot corrected phase vs. amp for the antennas you picked out in (4), # to check that in fact the corrections have been applied as expected. if doplots: for ant2 in ant2list: for ant in ant1list: plotms(vis=vis_for_selfcal, xaxis='time', yaxis='phase', xdatacolumn='corrected', ydatacolumn='corrected', avgtime='15s', avgchannel=avgchannel_narrow, coloraxis='corr', antenna=ant+'&'+ant2, overwrite=True, title='Iteration %i for spw %i and ant %s-%s' % (calnum,spwn,ant,ant2), showgui=INTERACTIVE, plotfile=outdir+'selfcal%i_spw%i_ant%s-%s_phasetime_applied.png' % (calnum,spwn,ant,ant2),) plotms(vis=vis_for_selfcal, xaxis='time', yaxis='amp', xdatacolumn='corrected', ydatacolumn='corrected', avgtime='60s', avgchannel=avgchannel_narrow, coloraxis='corr', antenna=ant+'&'+ant2, overwrite=True, title='Iteration %i for spw %i and ant %s-%s' % (calnum,spwn,ant,ant2), showgui=INTERACTIVE, plotfile=outdir+'selfcal%i_spw%i_ant%s-%s_amptime_applied.png' % (calnum,spwn,ant,ant2),) plotms(vis=vis_for_selfcal, xaxis='phase', yaxis='amp', xdatacolumn='corrected', ydatacolumn='corrected', avgtime='60s', avgchannel=avgchannel_narrow, coloraxis='corr', antenna=ant+'&'+ant2, overwrite=True, title='Iteration %i for spw %i and ant %s-%s' % (calnum,spwn,ant,ant2), showgui=INTERACTIVE, plotfile=outdir+'selfcal%i_spw%i_ant%s-%s_phaseamp_applied.png' % (calnum,spwn,ant,ant2),) plotms(vis=vis, spw='0', xaxis='freq', yaxis='phase', avgtime='1e8', avgscan=T, coloraxis='corr', iteraxis='baseline', xselfscale=T, yselfscale=T, antenna=ant+'&'+ant2, title='Phase vs Freq with time averaging for spw %i ant %s-%s iter %i' % (spwn,ant,ant2,calnum), plotfile=outdir+'phasevsfreq_spw%i_ant%s-%s_selfcal%i.png' % (spwn,ant,ant2,calnum), field=field, overwrite=True, showgui=INTERACTIVE, ) # Use this command to roll back to the previous flags in the event of # an unfortunate applycal. #flagmanager(vis=vis_for_selfcal, # mode='restore', # versionname='before_selfcal_apply') if reclean: selfcal_image = 'selfcal_{0} {1}_selfcal{2}'.format(fieldstr,spwn,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+selfcal_image+suffix) clean(vis=vis_for_selfcal, imagename=selfcal_image, field=field, mode='mfs', psfmode=psfmode, multiscale=multiscale, weighting=weighting, robust=robust, niter=midniter, imsize=imsize, cell=cell, nterms=1, mask=cleanboxes, psfmode=psfmode, usescratch=True) exportfits(imagename=selfcal_image+".image", fitsimage=selfcal_image+".fits", overwrite=True) if doplots: plotms(vis=vis_for_selfcal, spw='0', xaxis='baseline', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Baseline after CSCLEAN iter %i' % calnum, plotfile=outdir+'post_selfcal%i_spw%i_residVSbaseline.png' % (calnum,spwn), field=field, overwrite=True, showgui=INTERACTIVE, ) plotms(vis=vis_for_selfcal, spw='0', xaxis='time', yaxis='amp', avgtime='5s', ydatacolumn='corrected-model', coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Time after CSCLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVStime.png' % (calnum,spwn), field=field, overwrite=True, showgui=INTERACTIVE, ) plotms(vis=vis_for_selfcal, spw='0', xaxis='uvdist', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. UVDIST after CSCLEAN iter %i' % (calnum) , plotfile=outdir+'post_selfcal%i_spw%i_residVSuvdist.png' % (calnum,spwn), field=field, overwrite=True, showgui=INTERACTIVE, ) imrms.append(imstat(selfcal_image+".image",box=statsbox)['rms']) print "FINISHED ITERATION %i" % calnum print "FINISHED ITERATING!!! YAY!" # final phase + gain cal: # http://casaguides.nrao.edu/index.php?title=Calibrating_a_VLA_5_GHz_continuum_survey#One_Last_Iteration:_Amplitude_.26_Phase_Self_Calibration aptable = 'selfcal_ap_%s_spw%i.gcal' % (field.replace(" ",""),spwn) gaincal(vis=vis_for_selfcal, field='', caltable=aptable, gaintable=caltable, spw='', solint='inf', refant=refant, calmode='ap', combine='', minblperant=4, gaintype=gaintype, minsnr=minsnr) if doplots: plotcal(caltable=aptable, xaxis='phase', yaxis='amp', plotrange=[-50,50,0.5,1.5], showgui=INTERACTIVE, figfile='' if INTERACTIVE else outdir+'selfcal%i_spw%i_ampvsphase_final.png' % (calnum,spwn), iteration='spw' if INTERACTIVE else '')#, subplot = 221) applycal(vis=vis_for_selfcal, gaintable=[aptable,caltable], interp='linear', flagbackup=True) # was False when flagmanager was used flagmanager(vis=vis_for_selfcal, mode='restore', versionname='applycal_1') selfcal_image = 'spw%i_ku_d_selfcal%i_final' % (spwn,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+selfcal_image+suffix) clean(vis=vis_for_selfcal,imagename=selfcal_image,field=field, mode='mfs', mask=cleanboxes, weighting=weighting, robust=robust, niter=deepniter, psfmode=psfmode, imsize=imsize, cell=cell, nterms=1, usescratch=True) exportfits(imagename=selfcal_image+".image", fitsimage=selfcal_image+".fits", overwrite=True) if doplots: plotms(vis=vis_for_selfcal, spw='0', xaxis='baseline', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Baseline after CSCLEAN iter %i' % calnum, plotfile=outdir+'post_selfcal%i_spw%i_residVSbaseline.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=vis_for_selfcal, spw='0', xaxis='time', yaxis='amp', avgtime='5s', ydatacolumn='corrected-model', coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Time after CSCLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVStime.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=vis_for_selfcal, spw='0', xaxis='uvdist', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. UVDIST after CSCLEAN iter %i' % (calnum) , plotfile=outdir+'post_selfcal%i_spw%i_residVSuvdist.png' % (calnum,spwn), field=field, overwrite=True, ) selfcal_image = 'spw%i_ku_d_selfcal%i_final_multiscale' % (spwn,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+selfcal_image+suffix) clean(vis=vis_for_selfcal,imagename=selfcal_image,field=field, mode='mfs', psfmode=psfmode, nterms=1, weighting=weighting, robust=robust, niter=deepniter, imsize=imsize, cell=cell, usescratch=True) exportfits(imagename=selfcal_image+".image", fitsimage=selfcal_image+".fits", overwrite=True) if doplots: plotms(vis=vis_for_selfcal, spw='0', xaxis='baseline', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Baseline after multiscale CLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVSbaseline_multiscale.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=vis_for_selfcal, spw='0', xaxis='time', yaxis='amp', avgtime='5s', ydatacolumn='corrected-model', coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. Time after multiscale CLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVStime_multiscale.png' % (calnum,spwn), field=field, overwrite=True, ) plotms(vis=vis_for_selfcal, spw='0', xaxis='uvdist', yaxis='amp', avgtime='1e8', ydatacolumn='corrected-model', avgscan=T, coloraxis='baseline', iteraxis='', xselfscale=T, yselfscale=T, title='Residual vs. UVDIST after multiscale CLEAN iter %i' % (calnum), plotfile=outdir+'post_selfcal%i_spw%i_residVSuvdist_multiscale.png' % (calnum,spwn), field=field, overwrite=True, ) return imrms
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 selfcal(vis, spw='6', spwname=None, INTERACTIVE=False, field='W51 Ku', statsbox='170,50,229,97', cleanboxes="", refant='ea27', solint='30s', niter=2, multiscale=[0,3,6,12,24,48,96], imsize=512, cell='0.1arcsec', weighting='uniform', robust=0.0, minsnr=3, psfmode='clark', shallowniter=100, midniter=1000, deepniter=1e4, threshold='0.0mJy', pointclean=False, minblperant=4, gaintype='G', **kwargs): """ Docstring incomplete """ # Jan 2016: the fact that I have to make these declarations indicates that # this code never actually worked. spwn = int(spw) if spwname is None: spwname = str(spw) vis_for_selfcal = "selfcal_copy_{0}".format(vis) os.system('rm -rf {0}'.format(vis_for_selfcal)) os.system('rm -rf {0}.flagversions'.format(vis_for_selfcal)) assert split(vis=vis, outputvis=vis_for_selfcal) flagmanager(vis=vis_for_selfcal, mode='save', versionname='original') shallowniter = int(shallowniter) midniter = int(midniter) deepniter = int(deepniter) fieldstr = field.replace(" ","") # (1) Clean a single SPW *interactively*, boxing the brightest regions and not # cleaning very deeply (maybe 100 iterations). Keep this model in the header # -- it's what you'll use for the first round of self-calibration. # # Those are the official directions. They are nonsense when dealing with # the extended emission of W51. # This first image is effectively discarded imagename="selfcal_{1}_spw{0}_shallowclean_iter0".format(spwname, fieldstr) for suffix in clean_output_suffixes: os.system("rm -rf "+imagename+suffix) clean(vis=vis_for_selfcal, field=field, imagename=imagename, threshold=threshold, mode='mfs', psfmode=psfmode, multiscale=multiscale, weighting=weighting, robust=robust, niter=shallowniter, imsize=imsize, cell=cell, mask=cleanboxes, nterms=1, interactive=INTERACTIVE, usescratch=True, **kwargs) if pointclean: clean(vis=vis_for_selfcal, field=field, imagename=imagename, threshold=threshold, mode='mfs', psfmode=psfmode, weighting=weighting, robust=robust, niter=shallowniter, imsize=imsize, cell=cell, mask=cleanboxes, nterms=1, interactive=INTERACTIVE, usescratch=True, **kwargs) exportfits(imagename=imagename+".image", fitsimage=imagename+".fits", overwrite=True, dropdeg=True) exportfits(imagename=imagename+".model", fitsimage=imagename+".model.fits", overwrite=True, dropdeg=True) exportfits(imagename=imagename+".residual", fitsimage=imagename+".residual.fits", overwrite=True, dropdeg=True) imrms = [imstat(imagename+".image",box=statsbox)['rms']] for calnum in xrange(niter): first_image = 'selfcal_{0}_{1}_firstim_selfcal{2}'.format(fieldstr, spwname, calnum) clearcal(vis=vis_for_selfcal) for suffix in clean_output_suffixes: os.system("rm -rf "+first_image+suffix) clean(vis=vis_for_selfcal, imagename=first_image, field=field, threshold=threshold, mode='mfs', psfmode=psfmode, multiscale=multiscale, weighting=weighting, robust=robust, niter=midniter, imsize=imsize, mask=cleanboxes, cell=cell, nterms=1, usescratch=True, interactive=INTERACTIVE, **kwargs) if pointclean: clean(vis=vis_for_selfcal, imagename=first_image, field=field, threshold=threshold, mode='mfs', psfmode=psfmode, weighting=weighting, robust=robust, niter=midniter, imsize=imsize, mask=cleanboxes, cell=cell, nterms=1, usescratch=True, interactive=INTERACTIVE, **kwargs) exportfits(imagename=first_image+".image", fitsimage=first_image+".fits", overwrite=True, dropdeg=True) exportfits(imagename=first_image+".model", fitsimage=first_image+".model.fits", overwrite=True, dropdeg=True) exportfits(imagename=first_image+".residual", fitsimage=first_image+".residual.fits", overwrite=True, dropdeg=True) caltable = 'selfcal{0}_{1}_spw{2}.pcal'.format(calnum,fieldstr,spwname) os.system('rm -rf '+caltable) gaincal(vis=vis_for_selfcal, field=field, caltable=caltable, spw='', # gaintype = 'T' could reduce failed fit errors by averaging pols... gaintype=gaintype, # 'G' from http://casaguides.nrao.edu/index.php?title=EVLA_Advanced_Topics_3C391 solint=solint, refant=refant, calmode='p', combine='scan', minsnr=minsnr, minblperant=minblperant) # Watch out for failed solutions noted in the terminal during this # solution. If you see a large fraction (really more than 1 or 2) of # your antennas failing to converge in many time intervals then you # may need to lengthen the solution interval. applycal(vis=vis_for_selfcal, gaintable=caltable, interp='linear', flagbackup=False) # no need when using flagmanager flagmanager(vis=vis_for_selfcal, mode='restore', versionname='original') new_vis_for_selfcal = "selfcal{1}_copy_{0}".format(vis, calnum) os.system('rm -rf {0}'.format(new_vis_for_selfcal)) os.system('rm -rf {0}.flagversions'.format(new_vis_for_selfcal)) split(vis=vis_for_selfcal, outputvis=new_vis_for_selfcal, datacolumn='corrected') flagmanager(vis=new_vis_for_selfcal, mode='save', versionname='original') vis_for_selfcal = new_vis_for_selfcal # (6) Plot corrected phase vs. amp for the antennas you picked out in (4), # to check that in fact the corrections have been applied as expected. selfcal_image = 'selfcal_{0}_{1}_selfcal{2}'.format(fieldstr,spwname,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+selfcal_image+suffix) clean(vis=vis_for_selfcal, imagename=selfcal_image, field=field, threshold=threshold, mode='mfs', psfmode=psfmode, multiscale=multiscale, weighting=weighting, robust=robust, niter=midniter, imsize=imsize, cell=cell, nterms=1, mask=cleanboxes, usescratch=False, interactive=INTERACTIVE, **kwargs) if pointclean: clean(vis=vis_for_selfcal, imagename=selfcal_image, field=field, threshold=threshold, mode='mfs', psfmode=psfmode, weighting=weighting, robust=robust, niter=midniter, imsize=imsize, cell=cell, nterms=1, mask=cleanboxes, usescratch=False, interactive=INTERACTIVE, **kwargs) exportfits(imagename=selfcal_image+".image", fitsimage=selfcal_image+".fits", overwrite=True, dropdeg=True) exportfits(imagename=selfcal_image+".model", fitsimage=selfcal_image+".model.fits", overwrite=True, dropdeg=True) exportfits(imagename=selfcal_image+".residual", fitsimage=selfcal_image+".residual.fits", overwrite=True, dropdeg=True) imrms.append(imstat(selfcal_image+".image",box=statsbox)['rms']) print "FINISHED ITERATION %i" % calnum print "FINISHED ITERATING!!! YAY!" # final phase + gain cal: # http://casaguides.nrao.edu/index.php?title=Calibrating_a_VLA_5_GHz_continuum_survey#One_Last_Iteration:_Amplitude_.26_Phase_Self_Calibration aptable = 'selfcal_ap_{0}_spw{1}.gcal'.format(field.replace(" ",""),spwname) os.system('rm -rf '+aptable) gaincal(vis=vis_for_selfcal, field=field, caltable=aptable, gaintable=caltable, spw='', solint='inf', refant=refant, calmode='ap', combine='', minblperant=minblperant, gaintype=gaintype, minsnr=minsnr) applycal(vis=vis_for_selfcal, gaintable=[aptable], interp='linear', flagbackup=False) flagmanager(vis=vis_for_selfcal, mode='restore', versionname='original') new_vis_for_selfcal = "selfcal{1}{2}_copy_{0}".format(vis, "final", calnum) os.system('rm -rf {0}'.format(new_vis_for_selfcal)) os.system('rm -rf {0}.flagversions'.format(new_vis_for_selfcal)) split(vis=vis_for_selfcal, outputvis=new_vis_for_selfcal, datacolumn='corrected') flagmanager(vis=new_vis_for_selfcal, mode='save', versionname='original') vis_for_selfcal = new_vis_for_selfcal # pointsource cleaning only (this doesn't work very well for W51) # don't bother doing it if multiscale is empty if multiscale: selfcal_image = 'selfcal_{0}_{1}_final'.format(fieldstr,spwname,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+selfcal_image+suffix) clean(vis=vis_for_selfcal,imagename=selfcal_image,field=field, mode='mfs', threshold=threshold, mask=cleanboxes, weighting=weighting, robust=robust, niter=deepniter, psfmode=psfmode, imsize=imsize, cell=cell, nterms=1, usescratch=False, **kwargs) exportfits(imagename=selfcal_image+".image", fitsimage=selfcal_image+".fits", overwrite=True, dropdeg=True) exportfits(imagename=selfcal_image+".model", fitsimage=selfcal_image+".model.fits", overwrite=True, dropdeg=True) exportfits(imagename=selfcal_image+".residual", fitsimage=selfcal_image+".residual.fits", overwrite=True, dropdeg=True) selfcal_image = 'selfcal_{0}_{1}_final_multiscale'.format(fieldstr,spwname,calnum) for suffix in clean_output_suffixes: os.system("rm -rf "+selfcal_image+suffix) clean(vis=vis_for_selfcal,imagename=selfcal_image,field=field, mode='mfs', threshold=threshold, psfmode=psfmode, nterms=1, weighting=weighting, robust=robust, multiscale=multiscale, mask=cleanboxes, niter=deepniter, imsize=imsize, cell=cell, usescratch=False, **kwargs) if pointclean: clean(vis=vis_for_selfcal,imagename=selfcal_image,field=field, mode='mfs', threshold=threshold, psfmode=psfmode, nterms=1, weighting=weighting, robust=robust, mask=cleanboxes, niter=deepniter, imsize=imsize, cell=cell, usescratch=False, **kwargs) exportfits(imagename=selfcal_image+".image", fitsimage=selfcal_image+".fits", overwrite=True, dropdeg=True) exportfits(imagename=selfcal_image+".model", fitsimage=selfcal_image+".model.fits", overwrite=True, dropdeg=True) exportfits(imagename=selfcal_image+".residual", fitsimage=selfcal_image+".residual.fits", overwrite=True, dropdeg=True) return imrms
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)
easy_imsmooth(image_cube_depb,beams[i],delete_original=True) image_cube_depb_sm = image_cube_depb + ".smooth" # mv to galname/ directory os.system("mkdir "+dir_gal) easy_imregrid(image_cube_sm,temp_rebinpix,axes=[0,1],delete_original=True) easy_imregrid(image_cube_depb_sm,temp_rebinpix,axes=[0,1],delete_original=True) image_cube2 = dir_gal+galnames[i]+"_co21_"+suffix+".cube" os.system("rm -rf "+image_cube2) os.system("cp -r "+image_cube_sm+".regrid "+image_cube2) 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)