Exemple #1
0
def deconv(dirty_image,
           psf_image,
           model_image='${im.MODEL_IMAGE}',
           residual_image='${im.RESIDUAL_IMAGE}',
           restored_image='${im.RESTORED_IMAGE}',
           image_prefix='${im.BASENAME_IMAGE}',
           path='$MORESANE_PATH',
           **kw):
    """ Runs PyMORESANE """

    # Check if PyMORESANE is where it is said to be
    model_image, residual_image, restored_image, path = interpolate_locals(
        'model_image residual_image restored_image path')
    found_path = argo.findImager(path, imager_name='PyMORESANE')
    if not found_path:
        abort('could not find PyMORESANE at $path')

    kw['modelname'] = model_image
    kw['residualname'] = residual_image
    kw['restoredname'] = restored_image

    # make dict of imager arguments that have been specified globally or locally
    args = dict([(arg, globals()[arg]) for arg in _moresane_args
                 if arg in globals() and globals()[arg] is not None])
    args.update([(arg, kw[arg]) for arg in _moresane_args if arg in kw])

    x.sh(
        argo.gen_run_cmd(path,
                         args,
                         suf='--',
                         assign='=',
                         pos_args=[dirty_image, psf_image, image_prefix]))
Exemple #2
0
def _run(msname='$MS',clean=False,path='${im.WSCLEAN_PATH}',**kw):
    """ Run WSCLEAN """
    msname,path = interpolate_locals('msname path')
    path = argo.findImager(path,imager_name='WSCLEAN')
    if path is False:
        abort('Could not find WSCLEAN in system path, alises or at $path')

    # make dict of imager arguments that have been specified globally or locally
    args = dict([ (arg,globals()[arg.replace("-","_")]) for arg in _wsclean_args 
                    if arg.replace("-","_") in globals() and globals()[arg.replace("-","_")] is not None ]);
    args.update([ (arg,kw[arg]) for arg in _wsclean_args if arg in kw ])

    # map image size/resolution parameters
    csz = kw.get('cellsize',cellsize)
    np = kw.get('npix',npix)
    args['scale'] = csz if isinstance(csz,(int,float)) else argo.toDeg(csz)
    args['size']= '%d %d'%(np,np)

    # map weight parameters
    wgt = args['weight'];
    if wgt == "robust" or wgt == "briggs":
        args['weight'] = 'briggs %.2f'%(kw.get('robust',robust))

    # map threshold
    if isinstance(args['threshold'],str):
        args['threshold'] = im.argo.toJy(args['threshold'])
    
    # map clean parameter
    if not clean:
        args['niter'] = 0

    ms.FIELD is not None and args.setdefault('field',ms.FIELD)
    x.sh(argo.gen_run_cmd(path,args,suf='-',assign=' ',pos_args=[msname] if not isinstance(msname,(list,tuple)) else msname))
Exemple #3
0
def _run (convert_output_to_fits=True,lwimager_path="$LWIMAGER_PATH",**kw):
  # look up lwimager
  lwimager_path = interpolate_locals("lwimager_path");
  lwimager_path = argo.findImager(lwimager_path)
  # make dict of imager arguments that have been specified globally or locally
  args = dict([ (arg,globals()[arg]) for arg in _lwimager_args if arg in globals() and globals()[arg] is not None ]);
  args.update([ (arg,kw[arg]) for arg in _lwimager_args if arg in kw ]);
  if no_weight_fov:
    args.pop('weight_fov',0);
  # add ifrs, spwid and field arguments
  ms.IFRS is not None and args.setdefault('ifrs',ms.IFRS);
  ms.DDID is not None and args.setdefault('spwid',ms.DDID);
  ms.FIELD is not None and args.setdefault('field',ms.FIELD);
  # have an IFR subset? Parse that too
  msname,ifrs = kw['ms'],args.pop('ifrs',None);
  if ifrs and ifrs.lower() != "all":
    import Meow.IfrSet
    subset = Meow.IfrSet.from_ms(msname).subset(ifrs).taql_string();
    args['select'] = "(%s)&&(%s)"%(args['select'],subset) if 'select' in args else subset;
  # make image names
  fitsfiles = {};
  for arg in _fileargs:
    if arg in args:
      if not args[arg].endswith(".img"):
        fitsfiles[arg] = args[arg];
        args[arg] = args[arg]+".img";
  # run the imager
  x.time("$lwimager_path",**args);
  # convert CASA images to FITS
  if convert_output_to_fits:
    fs = kw.get('flux_rescale') or flux_rescale;
    velo = kw.get('velocity') or velocity;
    for arg in _fileargs:
      if arg in fitsfiles:
        _im = pyrap.images.image(args[arg]);
        if fs and fs != 1:
          _im.putdata(fs*_im.getdata());
        _im.tofits(fitsfiles[arg],overwrite=True,velocity=velo);  
        subprocess.call("rm -fr "+args[arg],shell=True);
Exemple #4
0
def deconv(dirty_image,psf_image,
                 model_image='${im.MODEL_IMAGE}',
                 residual_image='${im.RESIDUAL_IMAGE}',
                 restored_image='${im.RESTORED_IMAGE}',
                 image_prefix='${im.BASENAME_IMAGE}',
                 path='$MORESANE_PATH',**kw):
    """ Runs PyMORESANE """

    # Check if PyMORESANE is where it is said to be
    model_image,residual_image,restored_image,path = interpolate_locals('model_image residual_image restored_image path')
    found_path = argo.findImager(path,imager_name='PyMORESANE') 
    if not found_path:
        abort('could not find PyMORESANE at $path')
   
    kw['modelname'] = model_image
    kw['residualname'] = residual_image
    kw['restoredname'] = restored_image
    
    # make dict of imager arguments that have been specified globally or locally
    args = dict([ (arg,globals()[arg]) for arg in _moresane_args if arg in globals() and globals()[arg] is not None ]);
    args.update([ (arg,kw[arg]) for arg in _moresane_args if arg in kw ])

    x.sh(argo.gen_run_cmd(path,args,suf='--',assign='=',pos_args=[dirty_image,psf_image,image_prefix]))
Exemple #5
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