Esempio n. 1
0
    def finish(self, survey, brickname, version_header,
               apradec=None, apertures=None):
        # apradec = (ra,dec): aperture photometry locations
        # apertures: RADII in PIXELS
        if apradec is not None:
            assert(apertures is not None)
            (ra,dec) = apradec
            ok,xx,yy = self.wcs.radec2pixelxy(ra, dec)
            assert(np.all(ok))
            del ok
            apxy = np.vstack((xx - 1., yy - 1.)).T
            ap_iphots = [np.zeros((len(ra), len(apertures)), np.float32)
                         for band in self.bands]
            ap_dphots = [np.zeros((len(ra), len(apertures)), np.float32)
                         for band in self.bands]
            ap_rphots = [np.zeros((len(ra), len(apertures)), np.float32)
                         for band in self.bands]

        coimgs = []
        comods = []
        for iband,band in enumerate(self.bands):
            coimg = self.co_images[band]
            comod = self.co_models[band]
            coiv  = self.co_invvars[band]
            con   = self.co_nobs[band]
            with np.errstate(divide='ignore', invalid='ignore'):
                coimg /= coiv
                comod /= coiv
            coimg[coiv == 0] = 0.
            comod[coiv == 0] = 0.
            coimgs.append(coimg)
            comods.append(comod)

            hdr = copy_header_with_wcs(version_header, self.wcs)
            self.add_to_header(hdr, band)
            self.write_coadds(survey, brickname, hdr, band, coimg, comod, coiv, con)

            if apradec is not None:
                import photutils
                mask = (coiv == 0)
                with np.errstate(divide='ignore'):
                    imsigma = 1.0/np.sqrt(coiv)
                imsigma[mask] = 0.
                for irad,rad in enumerate(apertures):
                    aper = photutils.CircularAperture(apxy, rad)
                    p = photutils.aperture_photometry(coimg, aper, error=imsigma,
                                                      mask=mask)
                    ap_iphots[iband][:,irad] = p.field('aperture_sum')
                    ap_dphots[iband][:,irad] = p.field('aperture_sum_err')
                    p = photutils.aperture_photometry(coimg - comod, aper, mask=mask)
                    ap_rphots[iband][:,irad] = p.field('aperture_sum')

        self.write_color_image(survey, brickname, coimgs, comods)

        if apradec is not None:
            return ap_iphots, ap_dphots, ap_rphots
Esempio n. 2
0
def mask_outlier_pixels(survey,
                        tims,
                        bands,
                        targetwcs,
                        brickname,
                        version_header,
                        mp=None,
                        plots=False,
                        ps=None,
                        make_badcoadds=True,
                        refstars=None):
    from legacypipe.bits import DQ_BITS
    from scipy.ndimage.morphology import binary_dilation

    H, W = targetwcs.shape

    if make_badcoadds:
        badcoadds_pos = []
        badcoadds_neg = []
    else:
        badcoadds_pos = None
        badcoadds_neg = None

    star_veto = np.zeros(targetwcs.shape, np.bool)
    if refstars:
        gaia = refstars[refstars.isgaia]
        # Not moving Gaia stars to epoch of individual images...
        _, bx, by = targetwcs.radec2pixelxy(gaia.ra, gaia.dec)
        bx -= 1.
        by -= 1.
        # Radius to mask around Gaia stars, in arcsec
        radius = 1.0
        pixrad = radius / targetwcs.pixel_scale()
        for x, y in zip(bx, by):
            xlo = int(np.clip(np.floor(x - pixrad), 0, W - 1))
            xhi = int(np.clip(np.ceil(x + pixrad), 0, W - 1))
            ylo = int(np.clip(np.floor(y - pixrad), 0, H - 1))
            yhi = int(np.clip(np.ceil(y + pixrad), 0, H - 1))
            if xlo == xhi or ylo == yhi:
                continue
            r2 = (((np.arange(ylo, yhi + 1) - y)**2)[:, np.newaxis] +
                  ((np.arange(xlo, xhi + 1) - x)**2)[np.newaxis, :])
            star_veto[ylo:yhi + 1, xlo:xhi + 1] |= (r2 < pixrad)

    # if plots:
    #     import pylab as plt
    #     plt.clf()
    #     plt.imshow(star_veto, interpolation='nearest', origin='lower',
    #                vmin=0, vmax=1, cmap='hot')
    #     ax = plt.axis()
    #     plt.plot(bx, by, 'r.')
    #     plt.axis(ax)
    #     plt.title('Star vetos')
    #     ps.savefig()

    with survey.write_output('outliers_mask', brick=brickname) as out:
        # empty Primary HDU
        out.fits.write(None, header=version_header)

        for band in bands:
            btims = [tim for tim in tims if tim.band == band]
            if len(btims) == 0:
                continue
            debug(len(btims), 'images for band', band)

            H, W = targetwcs.shape
            # Build blurred reference image
            sigs = np.array([tim.psf_sigma for tim in btims])
            debug('PSF sigmas:', sigs)
            targetsig = max(sigs) + 0.5
            addsigs = np.sqrt(targetsig**2 - sigs**2)
            debug('Target sigma:', targetsig)
            debug('Blur sigmas:', addsigs)
            coimg = np.zeros((H, W), np.float32)
            cow = np.zeros((H, W), np.float32)
            masks = np.zeros((H, W), np.int16)

            R = mp.map(blur_resample_one,
                       [(tim, sig, targetwcs)
                        for tim, sig in zip(btims, addsigs)])
            for tim, r in zip(btims, R):
                if r is None:
                    continue
                Yo, Xo, iacc, wacc, macc = r
                coimg[Yo, Xo] += iacc
                cow[Yo, Xo] += wacc
                masks[Yo, Xo] |= macc
                del Yo, Xo, iacc, wacc, macc
            del r, R

            #
            veto = np.logical_or(
                star_veto,
                np.logical_or(
                    binary_dilation(masks & DQ_BITS['bleed'], iterations=3),
                    binary_dilation(masks & DQ_BITS['satur'], iterations=10)))
            del masks

            # if plots:
            #     plt.clf()
            #     plt.imshow(veto, interpolation='nearest', origin='lower', cmap='gray')
            #     plt.title('SATUR, BLEED veto (%s band)' % band)
            #     ps.savefig()

            R = mp.map(compare_one, [(tim, sig, targetwcs, coimg, cow, veto,
                                      make_badcoadds, plots, ps)
                                     for tim, sig in zip(btims, addsigs)])
            del coimg, cow, veto

            masks = []
            badcoadd_pos = None
            badcoadd_neg = None
            if make_badcoadds:
                badcoadd_pos = np.zeros((H, W), np.float32)
                badcon_pos = np.zeros((H, W), np.int16)
                badcoadd_neg = np.zeros((H, W), np.float32)
                badcon_neg = np.zeros((H, W), np.int16)

            for r, tim in zip(R, btims):
                if r is None:
                    # none masked
                    mask = np.zeros(tim.shape, np.uint8)
                else:
                    mask, badco = r
                    if make_badcoadds:
                        badhot, badcold = badco
                        yo, xo, bimg = badhot
                        badcoadd_pos[yo, xo] += bimg
                        badcon_pos[yo, xo] += 1
                        yo, xo, bimg = badcold
                        badcoadd_neg[yo, xo] += bimg
                        badcon_neg[yo, xo] += 1
                        del yo, xo, bimg, badhot, badcold
                    del badco

                # Apply the mask!
                maskbits = get_bits_to_mask()
                tim.inverr[(mask & maskbits) > 0] = 0.
                tim.dq[(mask & maskbits) > 0] |= DQ_BITS['outlier']

                # Write output!
                from legacypipe.utils import copy_header_with_wcs
                hdr = copy_header_with_wcs(None, tim.subwcs)
                hdr.add_record(
                    dict(name='IMTYPE',
                         value='outlier_mask',
                         comment='LegacySurvey image type'))
                hdr.add_record(dict(name='CAMERA', value=tim.imobj.camera))
                hdr.add_record(dict(name='EXPNUM', value=tim.imobj.expnum))
                hdr.add_record(dict(name='CCDNAME', value=tim.imobj.ccdname))
                hdr.add_record(dict(name='X0', value=tim.x0))
                hdr.add_record(dict(name='Y0', value=tim.y0))

                # HCOMPRESS;: 943k
                # GZIP_1: 4.4M
                # GZIP: 4.4M
                # RICE: 2.8M
                extname = '%s-%s-%s' % (tim.imobj.camera, tim.imobj.expnum,
                                        tim.imobj.ccdname)
                out.fits.write(mask,
                               header=hdr,
                               extname=extname,
                               compress='HCOMPRESS')

            del r, R

            if make_badcoadds:
                badcoadd_pos /= np.maximum(badcon_pos, 1)
                badcoadd_neg /= np.maximum(badcon_neg, 1)
                badcoadds_pos.append(badcoadd_pos)
                badcoadds_neg.append(badcoadd_neg)

    return badcoadds_pos, badcoadds_neg
Esempio n. 3
0
def write_coadd_images(band,
                       survey, brickname, version_header, tims, targetwcs,
                       co_sky,
                       coadd_headers=None,
                       cowimg=None, cow=None, cowmod=None, cochi2=None,
                       cowblobmod=None,
                       psfdetiv=None, galdetiv=None, congood=None,
                       psfsize=None, **kwargs):

    hdr = copy_header_with_wcs(version_header, targetwcs)
    # Grab headers from input images...
    get_coadd_headers(hdr, tims, band, coadd_headers)
    imgs = [
        ('image',  'image', cowimg),
        ('invvar', 'wtmap', cow   ),
        ]
    if congood is not None:
        imgs.append(('nexp',   'expmap',   congood))
    if psfdetiv is not None:
        imgs.append(('depth', 'psfdepth', psfdetiv))
    if galdetiv is not None:
        imgs.append(('galdepth', 'galdepth', galdetiv))
    if psfsize is not None:
        imgs.append(('psfsize', 'psfsize', psfsize))
    if cowmod is not None:
        imgs.extend([
                ('model', 'model', cowmod),
                ('chi2',  'chi2',  cochi2),
                ])
    if cowblobmod is not None:
        imgs.append(('blobmodel', 'blobmodel', cowblobmod))
    for name,prodtype,img in imgs:
        if img is None:
            debug('Image type', prodtype, 'is None -- skipping')
            continue
        # Make a copy, because each image has different values for
        # these headers...
        hdr2 = fitsio.FITSHDR()
        for r in hdr.records():
            hdr2.add_record(r)
        hdr2.add_record(dict(name='IMTYPE', value=name,
                             comment='LegacySurveys image type'))
        hdr2.add_record(dict(name='PRODTYPE', value=prodtype,
                             comment='NOAO image type'))
        if name in ['image', 'model', 'blobmodel']:
            hdr2.add_record(dict(name='MAGZERO', value=22.5,
                                 comment='Magnitude zeropoint'))
            hdr2.add_record(dict(name='BUNIT', value='nanomaggy',
                                 comment='AB mag = 22.5 - 2.5*log10(nanomaggy)'))
        if name == 'image' and co_sky is not None:
            hdr2.add_record(dict(name='COSKY_%s' % band.upper(), value=co_sky.get(band, 'None'),
                                 comment='Sky level estimated (+subtracted) from coadd'))
        if name in ['invvar', 'depth', 'galdepth']:
            hdr2.add_record(dict(name='BUNIT', value='1/nanomaggy^2',
                                 comment='Ivar of ABmag=22.5-2.5*log10(nmgy)'))
        if name in ['psfsize']:
            hdr2.add_record(dict(name='BUNIT', value='arcsec',
                                 comment='Effective PSF size'))
        with survey.write_output(name, brick=brickname, band=band,
                                 shape=img.shape) as out:
            out.fits.write(img, header=hdr2)