def run_makedirtycube(vis, imagename, imsize, pixelsize, phasecenter='', restfreq='', specmode='cube', nchan=-1, width='', start=0, datacolumn='data', outframe='LSRK', gridder='mosaic', deconvolver='multiscale', scales=[0, 7, 21, 63], parallel=False): """ Creates a dirty cube Parameters ---------- vis : casa ms visibility file input visibilities imagename : string w/o extension output file name for the dirty cube. Will be appended with _dirty imsize : array array or x,y list for the size of the output image pixelsize : number size of the pixels. Hard coded for arcseconds remaining parameters are those sent to tclean """ import os import masking import numpy as np from tasks import tclean, imhead, imstat #Makes dirty image dirtyimage = '%s_dirty' % imagename print '[INFO] Making dirty image: %s' % dirtyimage tclean(vis=vis, datacolumn=datacolumn, imagename=dirtyimage, imsize=imsize, cell=str(pixelsize) + 'arcsec', phasecenter=phasecenter, specmode=specmode, nchan=nchan, start=start, width=width, outframe=outframe, restfreq=restfreq, gridder=gridder, deconvolver=deconvolver, scales=scales, niter=0, interactive=False, parallel=parallel)
def populate_model_column(imname, selfcal_ms, field, impars_thisiter, phasecenter, maskname, antennae, startmodel=''): # run tclean to repopulate the modelcolumn prior to gaincal # (force niter = 0 so we don't clean any more) # bugfix: for no reason at all, the reference frequency can change. # tclean chokes if it gets the wrong reffreq. ia.open(imname + ".image.tt0") reffreq = "{0}Hz".format(ia.coordsys().referencevalue()['numeric'][3]) ia.close() # have to remove mask for tclean to work os.system('rm -r {0}.mask'.format(imname)) impars_thisiter['niter'] = 0 logprint("(dirty) Imaging parameters are: {0}".format(impars_thisiter), origin='almaimf_cont_selfcal') logprint("This tclean run with zero iterations is only being done to " "populate the model column from image {0}.".format(imname), origin='almaimf_cont_selfcal') try: tclean( vis=selfcal_ms, field=field.encode(), imagename=imname, phasecenter=phasecenter, outframe='LSRK', veltype='radio', mask=maskname, interactive=False, antenna=antennae, #reffreq=reffreq, startmodel=startmodel, savemodel='modelcolumn', datacolumn='corrected', pbcor=True, calcres=True, calcpsf=False, **impars_thisiter) test_tclean_success() except Exception as ex: print(ex) logprint("tclean FAILED with reffreq unspecified." " Trying again with reffreq={0}.".format(reffreq), origin='almaimf_cont_selfcal') tclean(vis=selfcal_ms, field=field.encode(), imagename=imname, phasecenter=phasecenter, outframe='LSRK', veltype='radio', mask=maskname, interactive=False, antenna=antennae, reffreq=reffreq, startmodel=startmodel, savemodel='modelcolumn', datacolumn='corrected', pbcor=True, calcres=True, calcpsf=False, **impars_thisiter) test_tclean_success()
"imaging run for this file exists. It will not be " "imaged this time; please check what is happening. " "(this warning issued /before/ dirty imaging)".format( lineimagename), origin='almaimf_line_imaging') continue # first iteration makes a dirty image to estimate the RMS impars_dirty = impars.copy() impars_dirty['niter'] = 0 logprint( "Dirty imaging parameters are {0}".format(impars_dirty), origin='almaimf_line_imaging') tclean( vis=vis, imagename=lineimagename, restoringbeam='', # do not use restoringbeam='common' # it results in bad edge channels dominating the beam **impars_dirty) for suffix in ('image', 'residual', 'model'): ia.open(lineimagename + "." + suffix) ia.sethistory(origin='almaimf_line_imaging', history=[ "{0}: {1}".format(key, val) for key, val in impars_dirty.items() ]) ia.sethistory(origin='almaimf_line_imaging', history=[ "git_version: {0}".format(git_version), "git_date: {0}".format(git_date) ]) ia.close()
tclean( vis=[brick2tile1_ms, brick3tile1_ms], field='M33', spw=[myspw_num], intent='OBSERVE_TARGET#ON_SOURCE', datacolumn='corrected', imagename='brick2_brick3_til5.M33_sci.spw{}.cube_speclim'.format( myspw_num), imsize=[1024, 1024], cell=['1arcsec'], phasecenter= 'J2000 01:33:33.70 +030.38.11.70', # Half-way between the two mosaics stokes='I', specmode='cube', # nchan=500, nchan=105, start="-280km/s", # nchan=100, # start=800, # width=1, width="2.0km/s", outframe='LSRK', gridder='mosaic', chanchunks=-1, usepointing=False, mosweight=True, pblimit=0.2, deconvolver='multiscale', # scales=[0, 7, 14], scales=[0, 5, 10], restoration=True, restoringbeam='common', pbcor=False, weighting='briggs', robust=0.5, niter=100000, cycleniter=100, # Force many major cycles nsigma=5., # usemask='auto-multithresh', usemask='pb', sidelobethreshold=1.25, noisethreshold=3.0, lownoisethreshold=1.5, negativethreshold=0.0, minbeamfrac=0.1, growiterations=75, dogrowprune=True, minpercentchange=1.0, threshold='0.0mJy', interactive=0, savemodel='none', parallel=False, calcres=True, calcpsf=True, smallscalebias=0.6)
" This likely implies that an ongoing or incomplete " "imaging run for this file exists. It will not be " "imaged this time; please check what is happening. " "(this warning issued /before/ dirty imaging)".format( lineimagename), origin='almaimf_line_imaging') continue # first iteration makes a dirty image to estimate the RMS impars_dirty = impars.copy() impars_dirty['niter'] = 0 logprint("Dirty imaging parameters are {0}".format(impars_dirty), origin='almaimf_line_imaging') tclean( vis=vis, imagename=lineimagename, restoringbeam='', # do not use restoringbeam='common' # it results in bad edge channels dominating the beam **impars_dirty) for suffix in ('image', 'residual', 'model'): ia.open(lineimagename + "." + suffix) ia.sethistory(origin='almaimf_line_imaging', history=[ "{0}: {1}".format(key, val) for key, val in impars_dirty.items() ]) ia.sethistory(origin='almaimf_line_imaging', history=[ "git_version: {0}".format(git_version), "git_date: {0}".format(git_date) ]) ia.close()
''' Using tclean to image the regridded 14B-088 HI data and the archival data together. ''' from tasks import tclean vis = ["14B-088_HI_LSRK_AT0206_regrid.ms.contsub", "M33_b_c_LSRK.ms"] imagename = "M33_AT0206_14B-088_HI.clean" model = "M33_model.image" mask = "M33_newmask.image" tclean(vis=vis, imagename=imagename, field="M33*", imsize=[4096, 4096], cell="1.5arcsec", phasecenter="J2000 01h33m50.904 +30d39m35.79", restfreq="1420.40575177MHz", startmodel=model, specmode='cube', nchan=255, start=1, width=1, veltype='radio', gridder='mosaic', pblimit=0.2, deconvolver="multiscale", scales=[0, 4, 8, 20, 40, 80], weighting='natural', niter=80000, threshold="2mJy/beam", cycleniter=5000, mask=mask, parallel=True)
tclean( vis=brick3_ms, field='M33', spw=[myspw_num], intent='OBSERVE_TARGET#ON_SOURCE', datacolumn='corrected', imagename=os.path.join( imaging_path, 'brick3.spw{}.12co_21.cube_2kms_dirty_2'.format(myspw_num)), imsize=[1200, 700], cell=['1arcsec'], phasecenter='J2000 01:33:52.20 +030.33.40.70', stokes='I', specmode='cube', nchan=70, start="-220km/s", width="2.0km/s", outframe='LSRK', gridder='mosaic', chanchunks=-1, usepointing=False, mosweight=True, pblimit=0.2, deconvolver='multiscale', # scales=[0, 7, 14], scales=[0, 5, 10], restoration=False, # restoringbeam='common', pbcor=False, weighting='briggs', robust=0.5, niter=0, cycleniter=100, # Force many major cycles nsigma=5., # usemask='auto-multithresh', usemask='pb', sidelobethreshold=1.25, noisethreshold=3.0, lownoisethreshold=1.5, negativethreshold=0.0, minbeamfrac=0.1, growiterations=75, dogrowprune=True, minpercentchange=1.0, threshold='0.0mJy', interactive=0, savemodel='none', parallel=False, calcres=True, calcpsf=True, smallscalebias=0.6)
dirty_tclean_dict = \ tclean(vis=ms_names, field='M33', spw=spw_str, intent='OBSERVE_TARGET#ON_SOURCE', datacolumn='corrected', imagename=dirtyimage_name, imsize=imaging_params['imsize'], cell=imaging_params['cellsize'], phasecenter=ptg_centre, nchan=1, specmode='mfs', outframe='LSRK', gridder='mosaic', chanchunks=-1, mosweight=True, pblimit=pb_limit, pbmask=pb_limit, restoration=False, pbcor=False, weighting='briggs', robust=0.5, niter=0, usemask='pb', interactive=0, savemodel='none', parallel=False, calcres=True, calcpsf=True, smallscalebias=0.6 )
tclean(vis=ms_names, field='M33', spw='0', intent='OBSERVE_TARGET#ON_SOURCE', datacolumn='corrected', imagename=dirtyimage_name, imsize=imaging_params['imsize'], cell=imaging_params['cellsize'], phasecenter=ptg_centre, nchan=imaging_params['nchan'], start=imaging_params['start'], width=imaging_params['width'], specmode='cube', outframe='LSRK', gridder='mosaic', chanchunks=-1, mosweight=True, pblimit=pb_limit, pbmask=pb_limit, restoration=False, pbcor=False, weighting='uniform', robust=0.5, niter=0, usemask='pb', interactive=0, savemodel='none', parallel=False, calcres=True, calcpsf=True, smallscalebias=0.6 )
out_root = "{0}.CASAVer_{1}.Model_{2}.Mask_{3}.AllFields_{4}.MScale_{5}" \ ".Tclean_{6}".format(vis[:-3], casa_version, "T" if model is not None else "F", "T" if mask is not None else "F", "T" if use_all_fields else "F", "T" if use_multiscale else "F", "T" if use_tclean else "F") if use_tclean: from tasks import tclean tclean(vis=vis, imagename=out_root + '.clean', field=field, restfreq='1420.40575177MHz', specmode='cube', nchan=1, start=1, width=1, cell='3arcsec', scales=multiscale, niter=200000, threshold="1.8mJy/bm", gain=0.1, imsize=[2560, 2560], gridder='mosaic', weighting='natural', veltype='radio', pblimit=0.2, interpolation='linear', startmodel=model, usemask='user', mask=mask, phasecenter='J2000 01h33m50.904 +30d39m35.79', ) else: clean(vis=vis, imagename=out_root + '.clean', field=field, restfreq='1420.40575177MHz', mode='channel', width=1, nchan=1, start=1, cell='3arcsec', multiscale=multiscale, threshold='1.8mJy/beam', imagermode='mosaic', gain=0.1, imsize=[2560, 2560], weighting='natural', robust=0.0, niter=200000, pbcor=True, minpb=0.2, interpolation='linear', usescratch=False, phasecenter='J2000 01h33m50.904 +30d39m35.79', veltype='radio', modelimage=model, mask=mask) # Run feathering with the model
chan_num) if not os.path.exists(startmodel): raise ValueError("Given startmodel does not exist") # The naming scheme should split name.mask to name_channel_{}.mask # The file MUST end in ".mask" if mask is not None and len(mask) > 0 and usemask == "user": mask = "{0}_channel_{1}.mask" (mask.split(".image")[0], chan_num) if not os.path.exists(mask): raise ValueError("Given mask name ({0}) does not exist".format(mask)) # Only update a few parameters, as needed start = "{0}{1}".format(start_vel, spec_unit) width = "{0}{1}".format(chan_width, spec_unit) nchan = 1 restart = True calcres = do_calcres calcpsf = do_calcpsf interactive = 0 # Returns a summary dictionary out_dict = tclean() # Save the output dictionary. Numpy should be fine for this as the individual # channels will get concatenated together np.save(imagename + ".results_dict.npy", out_dict)
for robust in (-2, 0, 2): imname = contimagename + "_robust{0}".format(robust) if not os.path.exists(imname + ".image.tt0"): tclean( vis=continuum_ms, field=field.encode(), imagename=imname, gridder='mosaic', specmode='mfs', deconvolver='mtmfs', scales=[0, 3, 9, 27, 81], nterms=2, outframe='LSRK', veltype='radio', niter=10000, usemask='auto-multithresh', interactive=False, cell=cellsize, imsize=imsize, weighting='briggs', robust=robust, pbcor=True, antenna=antennae, ) exportfits(imname + ".image.tt0", imname + ".image.tt0.fits") exportfits(imname + ".image.tt0.pbcor", imname + ".image.tt0.pbcor.fits")
# start with cube imaging if not os.path.exists(lineimagename+".image") and not os.path.exists(lineimagename+".residual"): # json is in unicode by default, but CASA rejects unicode # first iteration makes a dirty image to estimate the RMS tclean(vis=vis, imagename=lineimagename, field=[field.encode()]*len(vis), specmode='cube', outframe='LSRK', veltype='radio', niter=0, phasecenter=phasecenter, # don't use these for dirty: #usemask='auto-multithresh', #scales=[0,3,9,27,81], deconvolver='multiscale', interactive=False, cell=cellsize, imsize=imsize, weighting='briggs', robust=robust, gridder='mosaic', restoringbeam='', # do not use restoringbeam='common' # it results in bad edge channels dominating the beam chanchunks=chanchunks) if os.path.exists(lineimagename+".image"): # tclean with niter=0 is not supposed to produce a .image file, # but if it does (and it appears to have done so on at # least one run), we still want to clean the cube dirty_tclean_made_residual = True
for robust in (-2, 0, 2): imname = contimagename + "_robust{0}".format(robust) if not os.path.exists(imname + ".image.tt0"): tclean( vis=vis, field=field.encode(), imagename=imname, gridder='mosaic', specmode='mfs', phasecenter=phasecenter, deconvolver='mtmfs', scales=[0, 3, 9, 27, 81], nterms=2, outframe='LSRK', veltype='radio', niter=10000, usemask='auto-multithresh', interactive=False, cell=cellsize, imsize=imsize, weighting='briggs', robust=robust, pbcor=True, # do not save model; these are line data savemodel='none', ) exportfits(imname + ".image.tt0", imname + ".image.tt0.fits") exportfits(imname + ".image.tt0.pbcor", imname + ".image.tt0.pbcor.fits")
def run_makecleancube(vis, imagename, imsize, pixelsize, phasecenter='', restfreq='', specmode='cube', nchan=-1, width='', start=0, datacolumn='data', outframe='LSRK', gridder='mosaic', deconvolver='multiscale', scales=[0, 7, 21, 63], niter=100000, tp_model='', usetpmodel=False, n_cycles=5, nsigma_max=10, nsigma_min=1, parallel=False): """ Code for staggered non-interactive CLEANing in casa. This code employs an automasking technique to identify data above a given threshold. CLEANing commences and stops when this threshold is hit. In the next iteration the previous CLEAN is used as a model for the next cycle. The number of steps in the CLEANing process can be finetuned to avoid divergence. Note that this code requires a dirty cube to be located in the same directory as "imagename". This can be produced using run_makedirtycube. Parameters ---------- vis : casa ms visibility file input visibilities imagename : string w/o extension output file name for the dirty cube. Will be appended with _dirty imsize : array array or x,y list for the size of the output image pixelsize : number size of the pixels. Hard coded for arcseconds tp_model : CASA image single dish model to use for the CLEANing. Must already be tweaked into a useable format usetpmodel : bool Do you want to use this as a model for the CLEANing? default=no - Will use the previous CLEAN image as an input model n_cycles : number number of cycles for the CLEANing nsigma_max : number starting threshold for mask creation. Given as an integer multiple of the rms nsigma_min : number end threshold for mask creation. i.e. CLEAN down to nsigma_min * rms remaining parameters are those sent to tclean """ import os import masking import numpy as np from tasks import tclean, imhead, imstat # define thresholds, from 10 to 1 threshs = np.linspace(nsigma_max, nsigma_min, n_cycles) dirtyimage = '%s_dirty' % imagename #Makes mask and cleans for cycle in range(n_cycles): print '' if usetpmodel: previmage = '%s_cycle%i_tpmodel' % (imagename, cycle - 1) outimage = '%s_cycle%i_tpmodel' % (imagename, cycle) else: previmage = '%s_cycle%i' % (imagename, cycle - 1) outimage = '%s_cycle%i' % (imagename, cycle) print '[INFO] Cleaning cycle %i' % cycle print '[INFO] Making image: %s' % outimage print '' header = imhead(imagename=dirtyimage + '.image', mode='list') major = header['perplanebeams']['median area beam']['major']['value'] minor = header['perplanebeams']['median area beam']['minor']['value'] beam_area = major * minor pixel_area = pixelsize**2 beam_pixel_ratio = beam_area / pixel_area thresh = threshs[cycle] print '' print '[INFO] Cycle thresh: %0.2f rms' % thresh print '' if cycle == 0: dirtyimage_ = '%s.image' % dirtyimage stats = imstat(imagename=dirtyimage_) mad = stats['medabsdevmed'][0] print '' print '[INFO] Cycle rms: %g Jy/beam' % mad print '' mask = masking.make_mask_3d(imagename=dirtyimage, thresh=thresh * mad, fl=False, useimage=True, pixelmin=beam_pixel_ratio * 3, major=major, minor=minor, pixelsize=pixelsize, line=True, overwrite_old=False) if usetpmodel: startmodel = [tp_model] else: startmodel = '' print '[INFO] No model - okay?' else: print '' previmage_ = '%s.image' % previmage stats = imstat(imagename=previmage_) mad = stats['medabsdevmed'][0] print '' print '[INFO] Cycle rms: %g Jy/beam' % mad print '' mask = masking.make_mask_3d(imagename=previmage, thresh=thresh * mad, fl=True, useimage=False, pixelmin=beam_pixel_ratio * 3, major=major, minor=minor, pixelsize=pixelsize, line=True, overwrite_old=False) if usetpmodel: startmodel = [tp_model] else: startmodel = ['%s.model' % previmage] print '' print '[INFO] Using model: %s' % startmodel print '' tclean(vis=vis, datacolumn=datacolumn, imagename=outimage, imsize=imsize, cell=str(pixelsize) + 'arcsec', phasecenter=phasecenter, specmode=specmode, nchan=nchan, start=start, width=width, outframe=outframe, restfreq=restfreq, gridder=gridder, deconvolver=deconvolver, scales=scales, niter=niter, threshold=thresh * mad, interactive=False, mask=mask, startmodel=startmodel, parallel=parallel) #os.system('rm -rf %s.weight' %outimage) #os.system('rm -rf %s.model' %outimage) #os.system('rm -rf %s.psf' %outimage) #os.system('rm -rf %s.sumwt' %outimage) #os.system('rm -rf %s.threshmask' %previmage) #os.system('rm -rf %s.fullmask' %previmage) #os.system('rm -rf %s.fullmask.nopb' %previmage) return
if not os.path.exists(imname + ".image.tt0"): tclean( vis=continuum_ms, field=field.encode(), imagename=imname, gridder='mosaic', specmode='mfs', phasecenter=phasecenter, deconvolver='mtmfs', scales=[0, 3, 9, 27, 81], nterms=2, outframe='LSRK', veltype='radio', niter=10000, # If you want to use auto-multithreshold, # these parameters work more or less: # growiterations=75, # sidelobethreshold=0.2, # minbeamfrac=0.5, #3 # negativethreshold=0.0, # noisethreshold=3.0,lownoisethreshold=0.5, # usemask='auto-multithresh', usemask='pb', interactive=False, #True, cell=cellsize, imsize=imsize, weighting='briggs', robust=robust, pbcor=True, antenna=antennae, pblimit=0.1)
arrayname, robust)] impars = copy.copy(impars) dirty_impars = copy.copy(impars) dirty_impars['niter'] = 0 imname = contimagename+"_robust{0}_dirty".format(robust) if not os.path.exists(imname+".image.tt0"): tclean(vis=continuum_ms, field=field.encode(), imagename=imname, phasecenter=phasecenter, outframe='LSRK', veltype='radio', usemask='pb', interactive=False, cell=cellsize, imsize=imsize, antenna=antennae, pbcor=True, **dirty_impars ) ia.open(imname+".residual.tt0") ia.sethistory(origin='almaimf_cont_imaging', history=["{0}: {1}".format(key, val) for key, val in impars.items()]) ia.close() try: maskname = make_custom_mask(field, imname+".residual.tt0",
modelname = [contimagename+"_robust{1}_selfcal{0}_finaliter.model.tt0".format(selfcaliter, robust_startmod), contimagename+"_robust{1}_selfcal{0}_finaliter.model.tt1".format(selfcaliter, robust_startmod)] first = False else: modelname = [contimagename+"_robust{1}_selfcal{0}_finaliter_pb_{2}.model.tt0".format(selfcaliter, robust_startmod, order), contimagename+"_robust{1}_selfcal{0}_finaliter_pb_{2}.model.tt1".format(selfcaliter, robust_startmod, order)] if not dryrun: logprint("Final imaging parameters are: {0} for image name {1}".format(impars_finaliter, finaliterimname), origin='almaimf_cont_selfcal') tclean(vis=selfcal_ms, field=field.encode(), imagename=finaliterimname, phasecenter=phasecenter, startmodel=modelname, outframe='LSRK', veltype='radio', interactive=False, antenna=antennae, savemodel='none', datacolumn='corrected', pbcor=True, **impars_finaliter ) test_tclean_success() sethistory(finaliterimname, impars=impars_finaliter, selfcalpars=selfcalpars, selfcaliter=selfcaliter) # overwrite=True because these could already exist exportfits(finaliterimname+".image.tt0", finaliterimname+".image.tt0.fits", overwrite=True) exportfits(finaliterimname+".image.tt0.pbcor", finaliterimname+".image.tt0.pbcor.fits", overwrite=True) robust_startmod = robust
if isinstance(val, dict): del dirty_impars[key] imname = contimagename + "_robust{0}_dirty_preselfcal".format(robust) if not os.path.exists(imname + ".image.tt0"): logprint( "(dirty, pre-) Imaging parameters are: {0}".format(dirty_impars), origin='almaimf_cont_selfcal') if not dryrun: tclean(vis=selfcal_ms, field=field, imagename=imname, phasecenter=phasecenter, outframe='LSRK', veltype='radio', interactive=False, pbcor=True, antenna=antennae, datacolumn='data', **dirty_impars) test_tclean_success() sethistory(imname, impars=dirty_impars, selfcalpars=selfcalpars, selfcaliter=0) if 'maskname' not in locals(): # either use the reclean-based mask or the dirty mask try:
def imaging(vis, trial_name, interactive, imsize, cellsize, glx_ctr, restfreq, specmode, outframe, veltype, restoringbeam, weighting, robust, scales, smallscalebias, dogrowprune, growiterations, noisethreshold, minbeamfrac, sidelobethreshold, gridder, pbmask, pblimit, threshold, niter_in, nsigma, cyclefactor, minpsffraction, gain, w, nchan): # titles = ['.dirty', '', '.2.strong'] # for i in range(2): # niter niter = 0 if i == 0 else niter_in # masking method usemask = 'auto-multithresh' if i == 2 else 'pb' # deconvolver deconvolver = 'multiscale' # tclean(vis=vis, imagename=trial_name, interactive=interactive, intent='*TARGET*', # datacolumn='data', nchan=nchan, start=str(w * nchan / (-2)) + 'km/s', width=str(w) + 'km/s', # Image dimension imsize=imsize, cell=cellsize, phasecenter=glx_ctr, restfreq=restfreq, specmode=specmode, outframe=outframe, veltype=veltype, # Restore to common beam? restoringbeam=restoringbeam, # Weighting weighting=weighting, robust=robust, # Methods deconvolver=deconvolver, scales=scales, gain=gain, smallscalebias=smallscalebias, usemask=usemask, dogrowprune=dogrowprune, growiterations=growiterations, noisethreshold=noisethreshold, minbeamfrac=minbeamfrac, sidelobethreshold=sidelobethreshold, gridder=gridder, pbmask=pbmask, pblimit=pblimit, pbcor=True, # Stopping criteria threshold=threshold, niter=niter, nsigma=nsigma, cyclefactor=cyclefactor, minpsffraction=minpsffraction) # nonpbcube = trial_name + '.image' pbcube = trial_name + '.image.pbcor' cubename = trial_name + '.image.pb' tempcube = trial_name + titles[i] + '.cube.fits' residualname = trial_name + '.residual' tempresidual = trial_name + titles[i] + '.residual.fits' m0name = cubename + '.integrated' tempm0 = trial_name + titles[i] + '.m0.fits' m1name = cubename + '.weighted_coord' tempm1 = trial_name + titles[i] + '.m1.fits' m2name = cubename + '.weighted_dispersion_coord' tempm2 = trial_name + titles[i] + '.m2.fits' for mxname in [m0name, m1name, m2name]: if os.path.isdir(mxname): rmtree(mxname) # print("...Calculating moment maps") if i == 0: rms = imstat(pbcube)['rms'][0] excludepix = [-1000, (2 * rms)] immoments(imagename=pbcube, moments=[0], axis='spectral', excludepix=excludepix, outfile=m0name) else: # calculate mask here # change masked_spec_width according to w if w > 10: spec_width = 1 else: spec_width = 3 mask_dir, work_rms = \ make_mask_python(nonpbcube, masked_beam=2.0, masked_spec_width=spec_width, masked_snr=2.0, reject_area_in_beams=2) # Use the PB corrected image! immoments(imagename=pbcube, moments=[0, 1, 2], axis='spectral', mask=mask_dir, excludepix=-1, outfile=cubename) print("#\n") print("...Exporting fits files of " + titles[i]) for (mxname, fitsname) in zip([pbcube, residualname, m0name, m1name, m2name], [tempcube, tempresidual, tempm0, tempm1, tempm2]): if (mxname in [residualname, m1name, m2name]) and i == 0: continue exportfits(imagename=mxname, fitsimage=fitsname) # Blocking nan pixels if mxname == pbcube: data = fits.getdata(tempcube) nanmask = np.any(np.isnan(data), axis=(0, 1)) del data if mxname in [m0name, m1name, m2name]: print("...Setting " + mxname + " NaNs to zeros") data, hdr = fits.getdata(fitsname, header=True) data[np.isnan(data)] = 0.0 data[0, 0][nanmask] = np.nan fits.writeto(fitsname, data, hdr, overwrite=True) with open(fitsname, 'rb') as f_in: with gzip.open(fitsname + '.gz', 'wb') as f_out: shutil.copyfileobj(f_in, f_out) os.remove(fitsname) if mxname in [m0name, m1name, m2name]: rmtree(mxname) """ print("...Plotting weighted m1 image") plt.ioff() m0 = fits.getdata(tempm0 + '.gz')[0, 0] m1 = fits.getdata(tempm1 + '.gz')[0, 0] # cmap = cm.bwr_r norm = mpl.colors.Normalize(vmin=np.nanmin(m1), vmax=np.nanmax(m1)) m = cm.ScalarMappable(norm=norm, cmap=cmap) with np.errstate(invalid='ignore'): mm = m.to_rgba(m1)[:, :, :3] temp = np.sum(mm, axis=2) for i in range(3): mm[:, :, i] /= temp m0[m0 <= 0] = np.min(m0[m0 > 0]) m0 = np.log10(m0) m0 -= m0.min() m0 /= m0.max() for i in range(3): mm[:, :, i] *= m0 mm /= mm.max() # fig1, ax1 = plt.subplots(figsize=(12, 10)) fig2, ax2 = plt.subplots() mpb = ax2.imshow(m1, cmap='bwr_r', origin='lower') ax1.imshow(mm, origin='lower') ax1.set_xticklabels([]) ax1.set_yticklabels([]) plt.colorbar(mpb, ax=ax1) fig1.savefig(trial_name + titles[i] + '.m1.m0-weighted.png') plt.close('all') """ if i == 1: target = os.getcwd().split('/')[-1] no_detection_check(image=pbcube, pbimage=trial_name + '.pb', target=target, work_rms=work_rms, chanwidth=w)
if not os.path.exists(maskname): raise IOError("Mask {0} not found".format(maskname)) imname = contimagename + "_robust{0}_dirty_preselfcal".format(robust) if not os.path.exists(imname + ".image.tt0"): logprint( "(dirty, pre-) Imaging parameters are: {0}".format(dirty_impars), origin='almaimf_cont_selfcal') tclean(vis=selfcal_ms, field=field.encode(), imagename=imname, phasecenter=phasecenter, outframe='LSRK', veltype='radio', usemask='pb', interactive=False, cell=cellsize, imsize=imsize, pbcor=True, antenna=antennae, datacolumn='data', **dirty_impars) test_tclean_success() ia.open(imname + ".image.tt0") ia.sethistory(origin='almaimf_cont_selfcal', history=[ "{0}: {1}".format(key, val) for key, val in dirty_impars.items() ]) ia.sethistory(origin='almaimf_cont_imaging',
# first iteration makes a dirty image to estimate the RMS impars_dirty = impars.copy() impars_dirty['niter'] = 0 if 'startmodel' in impars_dirty: del impars_dirty['startmodel'] impars_dirty['parallel'] = parallel # use the same mask as specified for the main run # impars_dirty['usemask'] = None logprint( "Dirty imaging parameters are {0}".format(impars_dirty), origin='almaimf_line_imaging') tclean( vis=concatvis, imagename=lineimagename, restoringbeam='', # do not use restoringbeam='common' # it results in bad edge channels dominating the beam **impars_dirty) sethistory(lineimagename, impars=impars_dirty, suffixes=(".image", ".residual")) for suffix in ("mask", "model"): bad_fn = lineimagename + "." + suffix if os.path.exists(bad_fn): logprint( "Removing {0} from dirty clean".format(bad_fn), origin='almaimf_line_imaging') shutil.rmtree(bad_fn) if os.path.exists(lineimagename + ".image"): # tclean with niter=0 is not supposed to produce a .image file,
if not os.path.exists(lineimagename + ".image") and not os.path.exists( lineimagename + ".residual"): # json is in unicode by default, but CASA rejects unicode # first iteration makes a dirty image to estimate the RMS tclean( vis=vis, imagename=lineimagename, field=[field.encode()] * len(vis), specmode='cube', outframe='LSRK', veltype='radio', niter=0, # don't use these for dirty: #usemask='auto-multithresh', #scales=[0,3,9,27,81], deconvolver='multiscale', interactive=False, cell=cellsize, imsize=imsize, weighting='briggs', robust=0.0, gridder='mosaic', restoringbeam='', # do not use restoringbeam='common' # it results in bad edge channels dominating the beam chanchunks=chanchunks) # the threshold needs to be computed if any imaging is to be done # no .image file is produced, only a residual ia.open(lineimagename + ".residual") stats = ia.statistics(robust=True)
if 'maskname' in dirty_impars: maskname = dirty_impars['maskname'][0] del dirty_impars['maskname'] imname = contimagename + "_robust{0}_dirty".format(robust) if not os.path.exists(imname + ".image.tt0"): logprint("Imaging parameters are: {0}".format(dirty_impars), origin='almaimf_cont_selfcal') tclean(vis=selfcal_ms, field=field.encode(), imagename=imname, phasecenter=phasecenter, outframe='LSRK', veltype='radio', usemask='pb', interactive=False, cell=cellsize, imsize=imsize, pbcor=True, antenna=antennae, datacolumn='data', **dirty_impars) ia.open(imname + ".image.tt0") ia.sethistory(origin='almaimf_cont_selfcal', history=[ "{0}: {1}".format(key, val) for key, val in dirty_impars.items() ]) ia.close()