Пример #1
0
    def combine_pol(pol,image_prefix=None,mfs=False):
        dirtys = eval_list(['$image_prefix-%s-dirty.fits'%d for d in pol])
        if dirty:
            argo.combine_fits(dirtys,outname=dirty_image,ctype='STOKES',keep_old=keep_component_images)
        else:
            for item in dirtys:
                rm_fr(item)

        if restore:
            model = eval_list(['$image_prefix-%s-model.fits'%d for d in pol])
            argo.combine_fits(model,outname=model_image,ctype='STOKES',keep_old=keep_component_images)

            residual = eval_list(['$image_prefix-%s-residual.fits'%d for d in pol])
            argo.combine_fits(residual,outname=residual_image,ctype='STOKES',keep_old=keep_component_images)

            restored = eval_list(['$image_prefix-%s-image.fits'%d for d in pol])
            argo.combine_fits(restored,outname=restored_image,ctype='STOKES',keep_old=keep_component_images)

            if mfs:
                model_mfs = eval_list(['$image_prefix-MFS-%s-model.fits'%d for d in pol])
                argo.combine_fits(model_mfs,
                       outname=model_image.replace('.model.fits','-MFS.model.fits'),
                       ctype='STOKES',keep_old=keep_component_images)

                residual_mfs = eval_list(['$image_prefix-MFS-%s-residual.fits'%d for d in pol])
                argo.combine_fits(residual_mfs,
                       outname=residual_image.replace('.residual.fits','-MFS.residual.fits'),
                       ctype='STOKES',keep_old=keep_component_images)

                restored_mfs = eval_list(['$image_prefix-MFS-%s-image.fits'%d for d in pol])
                argo.combine_fits(restored_mfs,
                       outname=restored_image.replace('.restored.fits','-MFS.restored.fits'),
                       ctype='STOKES',keep_old=False)
        else:
            for fits in ['$image_prefix-%s-image.fits'%d for d in pol]:
                rm_fr(fits)
Пример #2
0
def make_image(msname='$MS',image_prefix='${im.BASENAME_IMAGE}',column='${im.COLUMN}',
               mslist=None,         # if given, overrieds msname
                path='${WSCLEAN_PATH}',
                imager='$IMAGER',
                restore=False,
                dirty=True,
                psf=False,
                restore_lsm=False,
                lsm='$LSM',
                algorithm='${im.CLEAN_ALGORITHM}',
                channelize=None,
                psf_image='${im.PSF_IMAGE}',
                dirty_image='${im.DIRTY_IMAGE}',
                model_image='${im.MODEL_IMAGE}',
                residual_image='${im.RESIDUAL_IMAGE}',
                restored_image='${im.RESTORED_IMAGE}',
                fullrest_image='${im.FULLREST_IMAGE}',
                restoring_options='${im.RESTORING_OPTIONS}',
                keep_component_images=False,
                **kw):
    """ run WSCLEAN """

    makedir('$DESTDIR')
    _imager = im.IMAGER
    im.IMAGER = II(imager)
    #Add algorithm label if required
    if im.DECONV_LABEL and restore:
        if isinstance(im.DECONV_LABEL,bool):
            if im.DECONV_LABEL:
                im.DECONV_LABEL = algorithm
    elif im.DECONV_LABEL is False:
        im.DECONV_LABEL = None

    path,msname,image_prefix,column,dirty_image,model_image,residual_image,restored_image,psf_image,channelize,\
      fullrest_image,restoring_options = \
      interpolate_locals('path msname image_prefix column dirty_image model_image residual_image '
                         'restored_image psf_image channelize fullrest_image restoring_options')

    # Check if WSCLEAN is where it is said to be
    path = argo.findImager(path,imager_name='WSCLEAN')
    
    # wsclean requires a WEIGHT_SPECTRUM column in the MS
    if wsclean_version()[0]<1.6:
        argo.addcol(msname,colname='WEIGHT_SPECTRUM',valuetype='float',init_with=1) 
    
    if 'datacolumn' not in kw.keys():
        kw['datacolumn'] = column
     
    # Cater for moresane
    do_moresane = False
    if restore and algorithm.lower() in ['moresane','pymoresane']:
        kw['niter'] = 0
        kw['makepsf'] = True
        psf = True
        dirty = True
        if isinstance(restore,dict):
            kw0 = restore.copy()
        else: 
            kw0 = {}
        restore = False
        do_moresane = True
        from im import moresane
    else:
        if isinstance(restore,dict):
            kw.update(restore)
            restore = True
        elif not isinstance(restore,bool):
            restore = False

    kw['name'] = image_prefix    

    # Check channel selection options in kw
    if 'channelrange' in kw.keys():
        if isinstance(kw['channelrange'],str):
            start,end = map(int,kw['channelrange'].split())
        else:
            start,end = kw['channelrange']
    else:
        start,end = ms.CHANSTART,ms.CHANSTART+ms.NUMCHANS;
        # if multiple MSs are specified, adjust channel range
        if mslist:
            end = ms.TOTAL_CHANNELS*(len(mslist)-1) + end

    kw['channelrange'] = "%d %d"%(start,end);

    nr = 1 
    if not channelize:
        channelize = im.IMAGE_CHANNELIZE
    if channelize:
        nr = (end-start)//channelize
        kw['channelsout'] = nr
    if nr ==1:
        channelize=False
   
    if dirty: info("im.wsclean.make_image: making dirty image $dirty_image")
    if restore: info(" making restored image $restored_image\
                    (model is $model_image, residual is $residual_image)")
    
    if psf and not restore:
        kw['makepsf'] = True

    if 'pol' in kw.keys():
        pol = kw['pol']
    elif 'stokes' in kw.keys():
        pol = kw['pol'] = kw.pop('stokes')
    else:
        pol = stokes
        kw['pol'] = pol

    if ',' in pol:
        pol = pol.split(',')
    kw['clean'] = restore

    # also accepts list of MSs
    _run(mslist or msname,**kw)

    # delete gridding image unless user wants it
    if not KEEP_GRIDDING_IMAGE:
        rm_fr('$image_prefix-gridding.fits')

    # delete first-residual images
    first_residual_images = glob.glob(II("$image_prefix-*-first-residual.fits"))
    if first_residual_images:
        rm_fr(" ".join(first_residual_images))

    #TODO(sphe): always keep wsclean MFS images?
    # Combine images if needed
#    mfs = mode if 'mode' not in kw.keys() else kw['mode']
#    mfs = mode=='mfs'
#    abort(mfs,mode)

    def eval_list(vals):
        l = []
        for val in vals:
            l.append(II(val))
        return l

    def combine_pol(pol,image_prefix=None,mfs=False):
        dirtys = eval_list(['$image_prefix-%s-dirty.fits'%d for d in pol])
        if dirty:
            argo.combine_fits(dirtys,outname=dirty_image,ctype='STOKES',keep_old=keep_component_images)
        else:
            for item in dirtys:
                rm_fr(item)

        if restore:
            model = eval_list(['$image_prefix-%s-model.fits'%d for d in pol])
            argo.combine_fits(model,outname=model_image,ctype='STOKES',keep_old=keep_component_images)

            residual = eval_list(['$image_prefix-%s-residual.fits'%d for d in pol])
            argo.combine_fits(residual,outname=residual_image,ctype='STOKES',keep_old=keep_component_images)

            restored = eval_list(['$image_prefix-%s-image.fits'%d for d in pol])
            argo.combine_fits(restored,outname=restored_image,ctype='STOKES',keep_old=keep_component_images)

            if mfs:
                model_mfs = eval_list(['$image_prefix-MFS-%s-model.fits'%d for d in pol])
                argo.combine_fits(model_mfs,
                       outname=model_image.replace('.model.fits','-MFS.model.fits'),
                       ctype='STOKES',keep_old=keep_component_images)

                residual_mfs = eval_list(['$image_prefix-MFS-%s-residual.fits'%d for d in pol])
                argo.combine_fits(residual_mfs,
                       outname=residual_image.replace('.residual.fits','-MFS.residual.fits'),
                       ctype='STOKES',keep_old=keep_component_images)

                restored_mfs = eval_list(['$image_prefix-MFS-%s-image.fits'%d for d in pol])
                argo.combine_fits(restored_mfs,
                       outname=restored_image.replace('.restored.fits','-MFS.restored.fits'),
                       ctype='STOKES',keep_old=False)
        else:
            for fits in ['$image_prefix-%s-image.fits'%d for d in pol]:
                rm_fr(fits)

    if not channelize:
        if psf:
            x.mv('${image_prefix}-psf.fits $psf_image')
        elif restore:
            rm_fr('${image_prefix}-psf.fits')
        if len(pol)>1:
            combine_pol(pol,image_prefix)
        else:
            if dirty:
                x.mv('${image_prefix}-dirty.fits $dirty_image')
            else: 
                rm_fr('${image_prefix}-dirty.fits')

            if restore:
                 x.mv('${image_prefix}-model.fits $model_image')
                 x.mv('${image_prefix}-residual.fits $residual_image')
                 x.mv('${image_prefix}-image.fits $restored_image')
                # x.mv('${image_prefix}-psf.fits $psf_image')
            else:
                rm_fr('${image_prefix}-image.fits')
    else:
        # Combine component images from wsclean
        labels = ['%04d'%d for d in range(nr)]
        psfs = eval_list(['$image_prefix-%s-psf.fits'%d for d in labels])
        if psf: 
            argo.combine_fits(psfs,outname=II('$image_prefix.psf.fits'),ctype='FREQ',keep_old=keep_component_images)
        elif restore:
            for fits in psfs:
                rm_fr(fits)

        for i in pol:
            if len(pol) == 1:
               i = ''

            if i : 
                i = '-%s'%i
         
            dirtys = eval_list(['$image_prefix-%s$i-dirty.fits'%d for d in labels])
            if dirty:
                argo.combine_fits(dirtys,
                       outname=II('$image_prefix$i-dirty.fits') if i else dirty_image,
                       ctype='FREQ',keep_old=keep_component_images)
                if not restore:
                    xo.sh('rm -fr ${image_prefix}*image*.fits')
            else: 
                for fits in dirtys:
                    rm_fr(fits)

            if restore:
                model = eval_list(['$image_prefix-%s$i-model.fits'%d for d in labels])
                argo.combine_fits(model,outname=II('$image_prefix$i-model.fits') if i else model_image,
                       ctype='FREQ',keep_old=keep_component_images)

                residual = eval_list(['$image_prefix-%s$i-residual.fits'%d for d in labels])
                argo.combine_fits(residual,outname=II('$image_prefix$i-residual.fits') if i else residual_image,
                       ctype='FREQ',keep_old=keep_component_images)

                restored = eval_list(['$image_prefix-%s$i-image.fits'%d for d in labels])
                argo.combine_fits(restored,outname=II('$image_prefix$i-image.fits') if i else restored_image ,
                       ctype='FREQ',keep_old=keep_component_images)
                if len(pol)==1:
                    for old,new in zip([image_prefix+'-MFS-%s.fits'%img for img in 'model residual image'.split()],
                                       [model_image.replace('.model.fits','-MFS.model.fits'),
                                        residual_image.replace('.residual.fits','-MFS.residual.fits'),
                                        restored_image.replace('.restored.fits','-MFS.restored.fits')]):
                        #TODO(sphe) Should we always keep wsclean MFS images?
                        #if mfs:
                        x.mv('$old $new')
                        #else: 
                        #    rm_fr(old)

        if len(pol)>1:
            combine_pol(pol,image_prefix,mfs=True)

    if do_moresane:
        info(" im.moresane.deconv: making estored image $restored_image \
              model is $model_image, residual is $residual_image)")
        moresane.deconv(dirty_image,psf_image,model_image=model_image,
                           residual_image=residual_image,
                           restored_image=restored_image,**kw0)

    if restore or do_moresane:
        if lsm and restore_lsm:
            info("Restoring LSM into FULLREST_IMAGE=$fullrest_image");
            opts = restore_lsm if isinstance(restore_lsm,dict) else {};
            tigger_restore(restoring_options,"-f",
                           restored_image,lsm,
                           fullrest_image,
                           kwopt_to_command_line(**opts));
    
    im.IMAGER = _imager