Exemplo n.º 1
0
def reg_to_mask(regfile, baseimage):
    """
    This is like makemask(mode='copy', inpimage=baseimage, inpmask=regfile)

    """
    casalog.post("Creating mask from regions {0} into base image {1}".format(
        regfile, baseimage),
                 origin='reg_to_mask')

    assert baseimage.endswith('.image') or baseimage.endswith('.image.tt0')
    cleanbox_mask = baseimage.replace(".image.tt0",
                                      ".image").replace(".image", ".mask")
    cleanbox_mask_image = baseimage

    if not os.path.exists(cleanbox_mask) or not os.path.exists(
            cleanbox_mask_image):

        # create a mask based on region selection (no thresholding here)
        dirtyimagename = baseimage

        exportfits(dirtyimagename, dirtyimagename + ".fits", overwrite=True)
        reg = regions.read_ds9(regfile)
        imghdu = fits.open(dirtyimagename + ".fits")[0]
        assert imghdu.data.shape[-1] != 1
        assert imghdu.data.shape[-2] != 1

        mask = get_mask(reg, imghdu)
        assert mask.shape[0] > 1
        assert mask.shape[1] > 1
        assert mask.size > 1

        imghdu.data = mask.astype('int16')
        imghdu.header['BITPIX'] = 16
        imghdu.writeto(cleanbox_mask + '.fits', clobber=True)
        importfits(fitsimage=cleanbox_mask + '.fits',
                   imagename=cleanbox_mask_image,
                   overwrite=True)
        ia.open(cleanbox_mask_image)
        ia.calcmask(mask="'{0}' > 0.5".format(cleanbox_mask_image),
                    name='cleanbox_mask')

        ia.close()
        makemask(mode='copy',
                 inpimage=cleanbox_mask_image,
                 inpmask=cleanbox_mask_image + ":cleanbox_mask",
                 output=cleanbox_mask,
                 overwrite=True)

    mask = cleanbox_mask

    ia.open(mask)
    stats = ia.statistics()
    ia.close()

    casalog.post("Resulting mask file is {0}".format(mask),
                 origin='reg_to_mask')
    casalog.post("Mask sum = {0} out of {1}, or {2}%".format(
        stats['sum'], stats['npts'], stats['sum'] / stats['npts'] * 100),
                 origin='reg_to_mask')

    return mask
Exemplo n.º 2
0
                    shape=imghdu.data.shape[2:])
imghdu.data = mask.astype('int16')
imghdu.header['BITPIX'] = 16
imghdu.writeto('cleanbox_mask_SgrB2.fits', clobber=True)
cleanbox_mask_image = 'cleanbox_mask_SgrB2.image'
importfits(fitsimage='cleanbox_mask_SgrB2.fits',
           imagename=cleanbox_mask_image,
           overwrite=True)
ia.open(cleanbox_mask_image)
ia.calcmask(mask=cleanbox_mask_image+" > 0.5",
            name='cleanbox_mask')

ia.close()
cleanbox_mask = 'cleanbox_mask.mask'
makemask(mode='copy', inpimage=cleanbox_mask_image,
         inpmask=cleanbox_mask_image+":cleanbox_mask",
         output=cleanbox_mask,
         overwrite=True)

mask = cleanbox_mask_image









imagename = '18A-229_Q_singlefield_selfcal_iter1_wterms'
myclean(vis=cont_vis,
        fields="Sgr B2 N Q,Sgr B2 NM Q,Sgr B2 MS Q".split(","),
        if not os.path.exists(dirtyimage):
            if os.path.exists(dirtyimage + ".fits"):
                importfits(fitsimage=dirtyimage + ".fits",
                           imagename=dirtyimage)
            else:
                raise IOError("Missing dirty image file & dirty FITS image")

        ia.open(dirtyimage)
        ia.calcmask(mask=dirtyimage +
                    " > {0}".format(mask_threshold[field] / 1e3),
                    name='dirty_mask_{0}'.format(field_nospace))

        ia.close()
        makemask(mode='copy',
                 inpimage=dirtyimage,
                 inpmask=dirtyimage + ":dirty_mask_{0}".format(field_nospace),
                 output='dirty_mask_{0}.mask'.format(field_nospace),
                 overwrite=True)
        mask = 'dirty_mask_{0}.mask'.format(field_nospace)
        exportfits(mask, mask + '.fits', dropdeg=True, overwrite=True)

        exportfits(dirtyimage, dirtyimage + ".fits", overwrite=True)
        reg = pyregion.open('cleanbox_regions_SgrB2.reg')
        imghdu = fits.open(dirtyimage + ".fits")[0]
        hdr = wcs.WCS(imghdu).celestial.to_header()
        mask = reg.get_mask(header=hdr,
                            shape=imghdu.data.squeeze().shape)[None,
                                                               None, :, :]
        imghdu.data = mask.astype('int16')
        imghdu.header['BITPIX'] = 16
        imghdu.writeto('cleanbox_mask_{0}.fits'.format(field_nospace),