예제 #1
0
파일: casa.py 프로젝트: SpheMakh/pyxis
def make_image (msname="$MS",column="${im.COLUMN}",imager='$IMAGER',
                dirty=True,restore=False,restore_lsm=True,psf=False,
                dirty_image="${im.DIRTY_IMAGE}",
                model_image="${im.MODEL_IMAGE}",
                restored_image="${im.RESTORED_IMAGE}",
                residual_image="${im.RESIDUAL_IMAGE}",
                psf_image="${im.PSF_IMAGE}",
                algorithm="${im.CLEAN_ALGORITHM}",
                fullrest_image="${im.FULLREST_IMAGE}",
                restoring_options="${im.RESTORING_OPTIONS}",
                channelize=None,lsm="$LSM",**kw0):
    """ run casa 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
     
    do_moresane=False
    if algorithm.lower() in ['moresane','pymoresane']: 
        do_moresane = True
        from im import moresane
    imager,msname,column,lsm,dirty_image,psf_image,restored_image,residual_image,\
model_image,algorithm,fullrest_image,restoring_options = \
interpolate_locals("imager msname column lsm dirty_image psf_image restored_image "
                   "residual_image model_image algorithm fullrest_image restoring_options")

    makedir('$DESTDIR')

    if restore and column != "CORRECTED_DATA":
        abort("Due to imager limitations, restored images can only be made from the CORRECTED_DATA column.")

    # setup imager options
    kw0.update(dict(chanstart=ms.CHANSTART,chanstep=ms.CHANSTEP,nchan=ms.NUMCHANS));
    if 'nchan' not in kw0 or 'start' not in kw0:
        if channelize is None:
            channelize = im.IMAGE_CHANNELIZE;
        if channelize == 0:
            kw0.update(nchan=1,width=ms.NUMCHANS);
        elif channelize > 0:
            kw0.update(nchan=ms.NUMCHANS//channelize,start=ms.CHANSTART,width=channelize);

    kw0.update(vis=msname,imagename=im.BASENAME_IMAGE)
    def make_dirty():
        info("im.casa.make_image: making dirty image $dirty_image")
        kw = kw0.copy()
        kw['niter'] = 0
        _run(dirty=dirty_image,makepsf=psf,psf=psf_image,**kw)
    if dirty:
        make_dirty()

    if do_moresane and restore:
        if np.logical_or(not dirty,not psf): 
            psf = True
            make_dirty()

        opts = restore if isinstance(restore,dict) else {}
        info(" making restored 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,**opts)
    elif restore:
        info(" making restored image $restored_image\
              (model is $model_image, residual is $residual_image)")
        kw = kw0.copy()
        kw['psfmode'] = algorithm if algorithm in 'clark clarkstokes hogbom' else 'clark'
        if isinstance(restore,dict):
            kw.update(restore)
        _run(model=model_image,residual=residual_image,restored=restored_image,makepsf=psf,psf=psf_image,clean=True,**kw)

    if restore:
        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))
예제 #2
0
파일: casa.py 프로젝트: pthouvenin/pyxis
def make_image(msname="$MS",
               column="${im.COLUMN}",
               imager='$IMAGER',
               dirty=True,
               restore=False,
               restore_lsm=True,
               psf=False,
               dirty_image="${im.DIRTY_IMAGE}",
               model_image="${im.MODEL_IMAGE}",
               restored_image="${im.RESTORED_IMAGE}",
               residual_image="${im.RESIDUAL_IMAGE}",
               psf_image="${im.PSF_IMAGE}",
               algorithm="${im.CLEAN_ALGORITHM}",
               fullrest_image="${im.FULLREST_IMAGE}",
               restoring_options="${im.RESTORING_OPTIONS}",
               channelize=None,
               lsm="$LSM",
               **kw0):
    """ run casa imager """

    _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

    do_moresane = False
    if algorithm.lower() in ['moresane', 'pymoresane']:
        do_moresane = True
        from im import moresane
    imager,msname,column,lsm,dirty_image,psf_image,restored_image,residual_image,\
model_image,algorithm,fullrest_image,restoring_options = \
interpolate_locals("imager msname column lsm dirty_image psf_image restored_image "
                   "residual_image model_image algorithm fullrest_image restoring_options")

    makedir('$DESTDIR')

    if restore and column != "CORRECTED_DATA":
        abort(
            "Due to imager limitations, restored images can only be made from the CORRECTED_DATA column."
        )

    # setup imager options
    kw0.update(
        dict(chanstart=ms.CHANSTART, chanstep=ms.CHANSTEP, nchan=ms.NUMCHANS))
    if 'nchan' not in kw0 or 'start' not in kw0:
        if channelize is None:
            channelize = im.IMAGE_CHANNELIZE
        if channelize == 0:
            kw0.update(nchan=1, width=ms.NUMCHANS)
        elif channelize > 0:
            kw0.update(nchan=ms.NUMCHANS // channelize,
                       start=ms.CHANSTART,
                       width=channelize)

    kw0.update(vis=msname, imagename=im.BASENAME_IMAGE)

    def make_dirty():
        info("im.casa.make_image: making dirty image $dirty_image")
        kw = kw0.copy()
        kw['niter'] = 0
        _run(dirty=dirty_image, makepsf=psf, psf=psf_image, **kw)

    if dirty:
        make_dirty()

    if do_moresane and restore:
        if np.logical_or(not dirty, not psf):
            psf = True
            make_dirty()

        opts = restore if isinstance(restore, dict) else {}
        info(" making restored 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,
                        **opts)
    elif restore:
        info(" making restored image $restored_image\
              (model is $model_image, residual is $residual_image)")
        kw = kw0.copy()
        kw['psfmode'] = algorithm if algorithm in 'clark clarkstokes hogbom' else 'clark'
        if isinstance(restore, dict):
            kw.update(restore)
        _run(model=model_image,
             residual=residual_image,
             restored=restored_image,
             makepsf=psf,
             psf=psf_image,
             clean=True,
             **kw)

    if restore:
        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
예제 #3
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
예제 #4
0
파일: wsclean.py 프로젝트: pthouvenin/pyxis
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 list(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 list(kw.keys()):
        if isinstance(kw['channelrange'], str):
            start, end = list(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 list(kw.keys()):
        pol = kw['pol']
    elif 'stokes' in list(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
예제 #5
0
def make_image (msname="$MS",column="${im.COLUMN}",imager='$IMAGER',
                dirty=True,restore=False,restore_lsm=True,psf=False,
                dirty_image="${im.DIRTY_IMAGE}",
                restored_image="${im.RESTORED_IMAGE}",
                residual_image="${im.RESIDUAL_IMAGE}",
                psf_image="${im.PSF_IMAGE}",
                model_image="${im.MODEL_IMAGE}",
                fullrest_image="${im.FULLREST_IMAGE}",
                restoring_options="${im.RESTORING_OPTIONS}",
                algorithm="${im.CLEAN_ALGORITHM}",
                channelize=None,lsm="$LSM",
                double_psf=None,**kw0):
  """Makes image(s) from MS. Set dirty and restore to True or False to make the appropriate images. You can also
  set either to a dict of options to be passed to the imager. If restore=True and restore_lsm is True and 'lsm' is set, 
  it will also make a full-restored image (i.e. will restore the LSM into the image) with tigger-restore. Use this when 
  deconvolving residual images. Note that RESTORING_OPTIONS are passed to tigger-restore.
  
  'channelize', if set, overrides the IMAGE_CHANNELIZE setting. If both are None, the options in the 'imager' module take effect.
  
  'algorithm' is the deconvolution algorithm to use (hogbom, clark, csclean, multiscale, entropy) 
  
  'dirty_image', etc. sets the image names, with defaults determined by the globals DIRTY_IMAGE, etc.
  """;
  
  _imager = im.IMAGER
  im.IMAGER = II(imager)
  # retain lwimager label for dirty maps and psf_maps
  #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

  do_moresane = False
  if algorithm.lower() in ['moresane','pymoresane']:
      from im import moresane
      do_moresane = True

  imager,msname,column,lsm,dirty_image,psf_image,restored_image,residual_image,model_image,algorithm,\
     fullrest_image,restoring_options,double_psf = \
     interpolate_locals("imager msname column lsm dirty_image psf_image restored_image "
                        "residual_image model_image algorithm fullrest_image restoring_options double_psf");
  makedir('$DESTDIR');
  if restore and column != "CORRECTED_DATA":
    abort("Due to imager limitations, restored images can only be made from the CORRECTED_DATA column.");

  # setup imager options
  kw0.update(dict(chanstart=ms.CHANSTART,chanstep=ms.CHANSTEP,nchan=ms.NUMCHANS));
  if 'img_nchan' not in kw0 or 'img_chanstart' not in kw0:
    if channelize is None:
      channelize = im.IMAGE_CHANNELIZE;
    if channelize == 0:
      kw0.update(img_nchan=1,img_chanstart=ms.CHANSTART,img_chanstep=ms.NUMCHANS);
    elif channelize > 0:
      kw0.update(img_nchan=ms.NUMCHANS//channelize,img_chanstart=ms.CHANSTART,img_chanstep=channelize);
  
  kw0.update(ms=msname,data=column);

  def make_dirty(**kw1):
    info("im.lwimager.make_image: making dirty image $dirty_image");
    kw = kw0.copy();
    if type(dirty) is dict:
      kw.update(dirty);
    kw.update(kw1)
    kw['operation'] = 'image';
    _run(image=dirty_image,**kw);

  if dirty: make_dirty()

  def make_psf(**kw1):
    info("im.lwimager.make_image: making PSF image $psf_image");
    kw = kw0.copy();
    if type(psf) is dict:
      kw.update(psf);
    kw['operation'] = 'image';
    kw['data'] = 'psf';
    kw['stokes'] = "I";
    kw.update(kw1)
    _run(image=psf_image,**kw);
  if psf: make_psf()

  if do_moresane and restore:
    # Moresane does better with a double sized PSF
    double_psf = double_psf or im.DOUBLE_PSF
    if double_psf: 
        _npix = int(npix)*2 if 'npix' not in kw0.keys() else (kw0['npix'])*2
        make_psf(npix=_npix)
    elif not psf: 
         make_psf()
    if not dirty: make_dirty()  
    opts = restore if isinstance(restore,dict) else {}
    moresane.deconv(dirty_image,psf_image,model_image=model_image,
                       residual_image=residual_image,restored_image=restored_image,**opts)
  elif restore:
    info("im.lwimager.make_image: making restored image $restored_image");
    info("                   (model is $model_image, residual is $residual_image)");
    kw = kw0.copy();
    if type(restore) is dict:
      kw.update(restore);
    kw.setdefault("operation",algorithm or "clark");
    temp_images = [];
    ## if fixed model was specified as a fits image, convert to CASA image
    if kw.pop('fixed',None):
      kw['fixed'] = 1;
      if not os.path.exists(model_image):
        warn("fixed=1 (use prior model) specified, but $model_image does not exist, ignoring"); 
      elif not os.path.isdir(model_image):
        info("converting prior model $model_image into CASA image");
        modimg = model_image+".img";
        temp_images.append(modimg);
        argo.fits2casa(model_image,modimg);
        model_image = modimg;
    ## if mask was specified as a fits image, convert to CASA image
    mask = kw.get("mask");
    if mask and not isinstance(mask,str):
      kw['mask'] = mask = im.MASK_IMAGE; 
    imgmask = None;
    if mask and os.path.exists(mask) and not os.path.isdir(mask):
      info("converting clean mask $mask into CASA image");
      kw['mask'] = imgmask = mask+".img";
      argo.fits2casa(mask,imgmask);
      temp_images.append(imgmask);
    ## run the imager
    _run(restored=restored_image,model=model_image,residual=residual_image,**kw)
    ## delete CASA temp images if created above
    for img in temp_images:
      rm_fr(img);
  if restore:
    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
예제 #6
0
def make_image (msname="$MS",column="${im.COLUMN}",imager='$IMAGER',
                dirty=True,restore=False,restore_lsm=True,psf=False,
                dirty_image="${im.DIRTY_IMAGE}",
                restored_image="${im.RESTORED_IMAGE}",
                residual_image="${im.RESIDUAL_IMAGE}",
                psf_image="${im.PSF_IMAGE}",
                model_image="${im.MODEL_IMAGE}",
                fullrest_image="${im.FULLREST_IMAGE}",
                restoring_options="${im.RESTORING_OPTIONS}",
                algorithm="${im.CLEAN_ALGORITHM}",
                channelize=None,lsm="$LSM",
                double_psf=None,**kw0):
  """Makes image(s) from MS. Set dirty and restore to True or False to make the appropriate images. You can also
  set either to a dict of options to be passed to the imager. If restore=True and restore_lsm is True and 'lsm' is set, 
  it will also make a full-restored image (i.e. will restore the LSM into the image) with tigger-restore. Use this when 
  deconvolving residual images. Note that RESTORING_OPTIONS are passed to tigger-restore.
  
  'channelize', if set, overrides the IMAGE_CHANNELIZE setting. If both are None, the options in the 'imager' module take effect.
  
  'algorithm' is the deconvolution algorithm to use (hogbom, clark, csclean, multiscale, entropy) 
  
  'dirty_image', etc. sets the image names, with defaults determined by the globals DIRTY_IMAGE, etc.
  """;
  
  _imager = im.IMAGER
  im.IMAGER = II(imager)
  # retain lwimager label for dirty maps and psf_maps
  #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

  do_moresane = False
  if algorithm.lower() in ['moresane','pymoresane']:
      from im import moresane
      do_moresane = True

  imager,msname,column,lsm,dirty_image,psf_image,restored_image,residual_image,model_image,algorithm,\
     fullrest_image,restoring_options,double_psf = \
     interpolate_locals("imager msname column lsm dirty_image psf_image restored_image "
                        "residual_image model_image algorithm fullrest_image restoring_options double_psf");
  makedir('$DESTDIR');
  if restore and column != "CORRECTED_DATA":
    abort("Due to imager limitations, restored images can only be made from the CORRECTED_DATA column.");

  # setup imager options
  kw0.update(dict(chanstart=ms.CHANSTART,chanstep=ms.CHANSTEP,nchan=ms.NUMCHANS));
  if 'img_nchan' not in kw0 or 'img_chanstart' not in kw0:
    if channelize is None:
      channelize = im.IMAGE_CHANNELIZE;
    if channelize == 0:
      kw0.update(img_nchan=1,img_chanstart=ms.CHANSTART,img_chanstep=ms.NUMCHANS);
    elif channelize > 0:
      kw0.update(img_nchan=ms.NUMCHANS//channelize,img_chanstart=ms.CHANSTART,img_chanstep=channelize);
  
  kw0.update(ms=msname,data=column);

  def make_dirty(**kw1):
    info("im.lwimager.make_image: making dirty image $dirty_image");
    kw = kw0.copy();
    if type(dirty) is dict:
      kw.update(dirty);
    kw.update(kw1)
    kw['operation'] = 'image';
    _run(image=dirty_image,**kw);

  if dirty: make_dirty()

  def make_psf(**kw1):
    info("im.lwimager.make_image: making PSF image $psf_image");
    kw = kw0.copy();
    if type(psf) is dict:
      kw.update(psf);
    kw['operation'] = 'image';
    kw['data'] = 'psf';
    kw['stokes'] = "I";
    kw.update(kw1)
    _run(image=psf_image,**kw);
  if psf: make_psf()

  if do_moresane and restore:
    # Moresane does better with a double sized PSF
    double_psf = double_psf or im.DOUBLE_PSF
    if double_psf: 
        _npix = int(npix)*2 if 'npix' not in list(kw0.keys()) else (kw0['npix'])*2
        make_psf(npix=_npix)
    elif not psf: 
         make_psf()
    if not dirty: make_dirty()  
    opts = restore if isinstance(restore,dict) else {}
    moresane.deconv(dirty_image,psf_image,model_image=model_image,
                       residual_image=residual_image,restored_image=restored_image,**opts)
  elif restore:
    info("im.lwimager.make_image: making restored image $restored_image");
    info("                   (model is $model_image, residual is $residual_image)");
    kw = kw0.copy();
    if type(restore) is dict:
      kw.update(restore);
    kw.setdefault("operation",algorithm or "clark");
    temp_images = [];
    ## if fixed model was specified as a fits image, convert to CASA image
    if kw.pop('fixed',None):
      kw['fixed'] = 1;
      if not os.path.exists(model_image):
        warn("fixed=1 (use prior model) specified, but $model_image does not exist, ignoring"); 
      elif not os.path.isdir(model_image):
        info("converting prior model $model_image into CASA image");
        modimg = model_image+".img";
        temp_images.append(modimg);
        argo.fits2casa(model_image,modimg);
        model_image = modimg;
    ## if mask was specified as a fits image, convert to CASA image
    mask = kw.get("mask");
    if mask and not isinstance(mask,str):
      kw['mask'] = mask = im.MASK_IMAGE; 
    imgmask = None;
    if mask and os.path.exists(mask) and not os.path.isdir(mask):
      info("converting clean mask $mask into CASA image");
      kw['mask'] = imgmask = mask+".img";
      argo.fits2casa(mask,imgmask);
      temp_images.append(imgmask);
    ## run the imager
    _run(restored=restored_image,model=model_image,residual=residual_image,**kw)
    ## delete CASA temp images if created above
    for img in temp_images:
      rm_fr(img);
  if restore:
    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