def set_model(self, root, typ, apply_region=True): """ apply_region: Isolate the clean components of a model fits file to those under self.region typ = init / best / regrid / ... """ if apply_region: region_file = self.get_region() for model_file in glob.glob(root+'*[0-9]-model.fits'): lib_img.blank_image_reg(model_file, region_file, model_file, inverse=True, blankval=0.) self.model[typ] = root
log='$nameMS_taql-c' + str(c) + '.log', commandType='general') ### DONE with w.if_todo('lowres_img_c%02i' % c): # Making beam mask logger.info('Preparing mask for low-res clean...') lib_util.run_wsclean(s, 'wscleanLRmask.log', MSs.getStrWsclean(), name='img/tmp', size=imgsizepix, scale='30arcsec') os.system('mv img/tmp-image.fits img/wide-lr-mask.fits') lib_img.blank_image_reg('img/wide-lr-mask.fits', beamReg, blankval=0.) lib_img.blank_image_reg('img/wide-lr-mask.fits', beamReg, blankval=1., inverse=True) # reclean low-resolution logger.info('Cleaning low-res...') imagename_lr = 'img/wide-lr' lib_util.run_wsclean(s, 'wscleanLR.log', MSs.getStrWsclean(), name=imagename_lr, do_predict=False, parallel_gridding=4,
'point': 'cross', 'font': '"helvetica 16 normal roman"' }) sh.comment = 'color=red text="%s"' % (name + '.reg') regions = pyregion.ShapeList([sh]) lib_util.check_rm(peel_region_file) regions.write(peel_region_file) # copy and blank models logger.info('Peel - Cleanup model images...') os.system('cp ' + imagenameM + '*model.fits peel-' + name) imagename_peel = 'peel-' + name + '/' + imagenameM.split( '/')[-1] for model_file in glob.glob(imagename_peel + '*model.fits'): lib_img.blank_image_reg(model_file, peel_region_file, blankval=0., inverse=True) # predict the source to peel logger.info('Peel - Predict init...') s.add('wsclean -predict -name ' + imagename_peel + ' -j ' + str(s.max_processors) + ' -channels-out 2 \ -reorder -parallel-reordering 4 ' + MSs.getStrWsclean(), log='wsclean-pre.log', commandType='wsclean', processors='max') s.run(check=True) # add the source to peel back logger.info( 'Peel - add model: CORRECTED_DATA = CORRECTED_DATA + MODEL_DATA...'
def clean(p, MSs, res='normal', size=[1, 1], empty=False, imagereg=None): """ p = patch name mss = list of mss to clean size = in deg of the image """ # set pixscale and imsize pixscale = MSs.resolution if res == 'normal': pixscale = float('%.1f' % (pixscale / 2.5)) elif res == 'high': pixscale = float('%.1f' % (pixscale / 3.5)) elif res == 'low': pass # no change imsize = [ int(size[0] * 1.5 / (pixscale / 3600.)), int(size[1] * 1.5 / (pixscale / 3600.)) ] # add 50% imsize[0] += imsize[0] % 2 imsize[1] += imsize[1] % 2 if imsize[0] < 256: imsize[0] = 256 if imsize[1] < 256: imsize[1] = 256 logger.debug('Image size: ' + str(imsize) + ' - Pixel scale: ' + str(pixscale)) if res == 'normal': weight = 'briggs -0.3' maxuv_l = None elif res == 'high': weight = 'briggs -0.6' maxuv_l = None elif res == 'low': weight = 'briggs 0' maxuv_l = 3500 else: logger.error('Wrong "res": %s.' % str(res)) sys.exit() if empty: logger.info('Cleaning empty (' + str(p) + ')...') imagename = 'img/empty-' + str(p) lib_util.run_wsclean(s, 'wscleanE-' + str(p) + '.log', MSs.getStrWsclean(), name=imagename, data_column='SUBTRACTED_DATA', size=imsize, scale=str(pixscale) + 'arcsec', weight=weight, niter=0, no_update_model_required='', minuv_l=30, mgain=0, baseline_averaging='') else: # clean 1 logger.info('Cleaning (' + str(p) + ')...') imagename = 'img/extract-' + str(p) lib_util.run_wsclean(s, 'wscleanA-' + str(p) + '.log', MSs.getStrWsclean(), name=imagename, size=imsize, scale=str(pixscale) + 'arcsec', weight=weight, niter=10000, no_update_model_required='', minuv_l=30, maxuv_l=maxuv_l, mgain=0.85, baseline_averaging='', parallel_deconvolution=512, auto_threshold=5, join_channels='', fit_spectral_pol=3, channels_out=ch_out, deconvolution_channels=3) # make mask im = lib_img.Image(imagename + '-MFS-image.fits', userReg=userReg) try: im.makeMask(threshpix=10, rmsbox=(70, 5)) except: logger.warning('Fail to create mask for %s.' % imagename + '-MFS-image.fits') return if imagereg is not None: lib_img.blank_image_reg(im.maskname, imagereg, inverse=True, blankval=0.) # clean 2 # TODO: add deconvolution_channels when bug fixed logger.info('Cleaning w/ mask (' + str(p) + ')...') imagenameM = 'img/extractM-' + str(p) lib_util.run_wsclean( s, 'wscleanB-' + str(p) + '.log', MSs.getStrWsclean(), name=imagenameM, do_predict=True, size=imsize, scale=str(pixscale) + 'arcsec', weight=weight, niter=100000, no_update_model_required='', minuv_l=30, maxuv_l=maxuv_l, reuse_psf=imagename, reuse_dirty=imagename, mgain=0.85, multiscale='', baseline_averaging='', parallel_deconvolution=512, auto_threshold=0.7, auto_mask=1.5, fits_mask=im.maskname, join_channels='', fit_spectral_pol=3, channels_out=ch_out) # , deconvolution_channels=3) os.system('cat logs/wscleanB-' + str(p) + '.log | grep "background noise"')
fit_spectral_pol=3, channels_out=ch_out) # , deconvolution_channels=3) os.system('cat logs/wscleanB-' + str(p) + '.log | grep "background noise"') with w.if_todo('predict_rest'): # DDF predict+corrupt in MODEL_DATA of everything BUT the calibrator indico = wideDD_image.root + '.DicoModel' outdico = indico + '-' + target_reg_file.split('.')[ 0] # use prefix of target reg inmask = sorted(glob.glob(wideDD_image.root + '*_mask-ddcal.fits'))[-1] outmask = outdico + '.mask' lib_img.blank_image_reg(inmask, target_reg_file, outfile=outmask, inverse=False, blankval=0.) s.add('MaskDicoModel.py --MaskName=%s --InDicoModel=%s --OutDicoModel=%s' % (outmask, indico, outdico), log='MaskDicoModel.log', commandType='python', processors='max') s.run(check=True) ddf_parms = { 'Data_MS': MSs.getStrDDF(), 'Data_ColName': 'CORRECTED_DATA', 'Data_Sort': 1, 'Output_Mode': 'Predict', 'Predict_InitDicoModel': outdico,
else: # this dd-cal should not be in the data anymore but probably the source finder got some strong residuals if d.peel_off: logger.info('This sources has been peeled, skip.') continue # DDF predict+corrupt in MODEL_DATA of everything BUT the calibrator indico = full_image.root + '.DicoModel' outdico = indico + '-' + d.name inmask = sorted(glob.glob(full_image.root + '.mask*.fits'))[-1] outmask = outdico + '.mask' lib_img.blank_image_reg(inmask, d.get_region(), outfile=outmask, inverse=False, blankval=0.) s.add( 'MaskDicoModel.py --MaskName=%s --InDicoModel=%s --OutDicoModel=%s' % (outmask, indico, outdico), log='MaskDicoModel-' + logstring + '.log', commandType='python', processors='max') s.run(check=True) ddf_parms = { 'Data_MS': MSs.getStrDDF(), 'Data_ColName': 'CORRECTED_DATA',
baseline_averaging='', parallel_deconvolution=512, local_rms='', auto_threshold=0.5, auto_mask=1., fits_mask=im.maskname, \ join_channels='', fit_spectral_pol=3, channels_out=9, deconvolution_channels=3) lib_util.run_wsclean(s, 'wscleanBlow-'+str(p)+'.log', MSs.getStrWsclean(), name=imagename+'-low', size=imsize, scale=str(pixscale)+'arcsec', \ weight=weight, niter=100000, no_update_model_required='', minuv_l=30, maxuv_l=maxuv_l, taper_gaussian='30asec', mgain=0.85, \ multiscale='', multiscale_scale_bias=0.75, multiscale_scales='0,10,20,40', \ baseline_averaging='', parallel_deconvolution=512, local_rms='', auto_threshold=0.5, auto_mask=1., fits_mask=im.maskname, \ join_channels='', fit_spectral_pol=3, channels_out=9, deconvolution_channels=3) os.system('cat logs/wscleanB-' + str(p) + '.log | grep "background noise"') # Load facet mask and set target region to 0 mask_voro = 'ddcal/masks/facets%02i.fits' % lastcycle os.system('cp %s facet/facets.fits' % mask_voro) lib_img.blank_image_reg('facet/facets.fits', target_reg, blankval=0) # mosaic the skymodel, Isl_patch_000 will be the target of interest lsm = lsmtool.load(mosaic_image.skymodel_cut) lsm.group('facet', facet='facet/facets.fits', root='Isl_patch') lsm.setPatchPositions(method='mid') # center of the facets directions = set(lsm.getColValues('patch')) coord = [c.deg for c in lsm.getPatchPositions('Isl_patch_0')['Isl_patch_0']] logger.info("Facet centre: " + str(coord)) # calculate region size (TODO: maybe better using regionfile?) ramin = np.min( lsm.getColValues('RA')[lsm.getColValues('Patch') == 'Isl_patch_0']) ramax = np.max( lsm.getColValues('RA')[lsm.getColValues('Patch') == 'Isl_patch_0']) decmin = np.min(
else: # these dd-cal are not in the data anymore if d.peel_off: logger.info('This sources has been peeled, skip.') continue # DDF predict+corrupt in MODEL_DATA of everything BUT the calibrator indico = full_image.root + '.DicoModel' outdico = indico + '-' + d.name inmask = sorted(glob.glob(full_image.root + '.mask*.fits'))[-1] outmask = outdico + '.mask' lib_img.blank_image_reg(inmask, d.get_region(), outfile=outmask, inverse=False, blankval=0.) s.add('MaskDicoModel.py --MaskName=%s --InDicoModel=%s --OutDicoModel=%s' % (outmask, indico, outdico), \ log='MaskDicoModel-'+logstring+'.log', commandType='python', processors='max') s.run(check=True) ddf_parms = { 'Data_MS': MSs.getStrDDF(), 'Data_ColName': 'CORRECTED_DATA', 'Data_Sort': 1, 'Output_Mode': 'Predict',
# if next is a "cont" then I need the do_predict logger.info('Cleaning shallow (cycle: ' + str(c) + ')...') lib_util.run_wsclean(s, 'wsclean-c%02i.log' % c, MSs.getStrWsclean(), do_predict=True, name=imagename, \ parallel_gridding=4, baseline_averaging='', size=5000, scale='2.5arcsec', \ niter=1000, no_update_model_required='', minuv_l=30, mgain=0.4, nmiter=0, \ auto_threshold=5, local_rms='', local_rms_method='rms-with-min', \ join_channels='', fit_spectral_pol=2, channels_out=2, **kwargs1 ) # check if hand-made mask is available im = lib_img.Image(imagename + '-MFS-image.fits') im.makeMask(threshisl=5, rmsbox=(50, 5), atrous_do=True) maskfits = imagename + '-mask.fits' region = '%s/regions/%s.reg' % (parset_dir, target) if os.path.exists(region): lib_img.blank_image_reg(maskfits, beamReg, blankval=0.) lib_img.blank_image_reg(maskfits, region, blankval=1.) logger.info('Cleaning full (cycle: ' + str(c) + ')...') lib_util.run_wsclean(s, 'wsclean-c%02i.log' % c, MSs.getStrWsclean(), do_predict=True, cont=True, name=imagename, \ parallel_gridding=4, size=5000, scale='2.5arcsec', \ niter=1000000, no_update_model_required='', minuv_l=30, mgain=0.4, nmiter=0, \ auto_threshold=0.5, auto_mask=2., local_rms='', local_rms_method='rms-with-min', fits_mask=maskfits, \ multiscale='', multiscale_scale_bias=0.8, \ join_channels='', fit_spectral_pol=2, channels_out=2, **kwargs2 ) os.system('cat logs/wsclean-c%02i.log | grep "background noise"' % c) w.done('image-c%02i' % c) ### DONE # Set CORRECTED_DATA = CORRECTED_DATA - MODEL_DATA