Пример #1
0
def main():
    #img = fitsio.read('347736-S5.fits')
    img = fitsio.read('392772-N29.fits')
    print(img.shape)
    img = img.T.copy()
    mm = np.median(img)
    img -= mm

    ps = PlotSequence('sky')
    lo,hi = np.percentile(img, [20,80])
    ima = dict(vmin=lo, vmax=hi, interpolation='nearest', origin='lower',
               cmap='gray')
    plt.clf()
    plt.imshow(img, **ima)
    ps.savefig()

    # PAD
    padimg = np.zeros((2048, 4096))
    padimg[1:-1, 1:-1] = img
    img = padimg
    
    from tractor.splinesky import SplineSky
    from scipy.ndimage.morphology import binary_dilation
    from astrometry.util.util import median_smooth

    # # Estimate per-pixel noise via Blanton's 5-pixel MAD
    slice1 = (slice(0,-5,10),slice(0,-5,10))
    slice2 = (slice(5,None,10),slice(5,None,10))
    mad = np.median(np.abs(img[slice1] - img[slice2]).ravel())
    sig1 = 1.4826 * mad / np.sqrt(2.)
    print('sig1 estimate:', sig1)

    
    mask = np.zeros(img.shape, bool)
    mask[binary_dilation(img > 5*sig1, iterations=5)] = True

    for mm in [None, mask]:

        notmm = None
        if mm is not None:
            notmm = np.logical_not(mm)
        
        sky = SplineSky.BlantonMethod(img, notmm, 512)

        skyfn = 'sky-%s.fits' % (mm is not None and 'mask' or 'nomask')
        sky.write_fits(skyfn)

        from tractor.utils import get_class_from_name
        
        print('Reading sky model from', skyfn)
        hdr = fitsio.read_header(skyfn)
        skyclass = hdr['SKY']
        clazz = get_class_from_name(skyclass)

        if getattr(clazz, 'from_fits'):
            fromfits = getattr(clazz, 'from_fits')
            skyobj = fromfits(skyfn, hdr)
        else:
            fromfits = getattr(clazz, 'fromFitsHeader')
            skyobj = fromfits(hdr, prefix='SKY_')
        sky2 = skyobj
        print('sky2', sky2)
        sky2.write_fits(skyfn.replace('sky', 'sky2'))
        
        mod = np.zeros_like(img)
        sky.addTo(mod)

        plt.clf()
        plt.imshow(mod, **ima)
        plt.title('Blanton method')
        ps.savefig()

        plt.clf()
        plt.imshow(img - mod, **ima)
        plt.title('Blanton method (subtracted)')
        ps.savefig()
    
    
        grid = 512
        #grid = 256
        img = img.astype(np.float32)
        med = np.zeros_like(img)
        median_smooth(img, mm, grid/2, med)

        plt.clf()
        plt.imshow(med, **ima)
        plt.title('dmedsmooth')
        ps.savefig()

        plt.clf()
        plt.imshow(img - med, **ima)
        plt.title('dmedsmooth (subtracted)')
        ps.savefig()

    sys.exit(0)
    
    med2 = np.zeros_like(img)
    mask = np.zeros(img.shape, bool)
    mask[binary_dilation(img > 5*sig1, iterations=5)] = True
    median_smooth(img, mask, grid/2, med2)

    # UN-PAD
    img = img[1:-1, 1:-1]
    med = med[1:-1, 1:-1]
    med2 = med2[1:-1, 1:-1]
    mask = mask[1:-1, 1:-1]

    sub = img - med
    
    plt.clf()
    plt.imshow(sub, **ima)
    ps.savefig()

    plt.clf()
    plt.imshow(img - med2, **ima)
    ps.savefig()

    plt.clf()
    plt.imshow(img * (1-mask), **ima)
    ps.savefig()
    
    plt.clf()
    plt.imshow(med2, **ima)
    ps.savefig()

    
    lo2,hi2 = np.percentile(img, [5,95])

    ha = dict(bins=100, range=(lo2,hi2), log=True,
             histtype='step')
    plt.clf()
    n1,b,p = plt.hist(img.ravel(), color='r', **ha)
    n2,b,p = plt.hist(sub.ravel(), color='b', **ha)
    mx = max(max(n1), max(n2))
    plt.ylim(mx*0.1, mx)
    ps.savefig()

    ha = dict(bins=100, range=(lo2,hi2), histtype='step')
    plt.clf()
    n1,b,p = plt.hist(img.ravel(), color='r', **ha)
    n2,b,p = plt.hist(sub.ravel(), color='b', **ha)

    n3,b,p = plt.hist((img - sub).ravel(), color='m', **ha)

    #mx = max(max(n1), max(n2))
    #plt.ylim(0, mx)
    ps.savefig()
Пример #2
0
def stage_1(expnum=431202,
            extname='S19',
            plotprefix='lsb',
            plots=False,
            brightstars='bright.fits',
            pixscale=0.27,
            **kwa):
    if plots:
        ps = PlotSequence(plotprefix)
    else:
        ps = None

    survey = LegacySurveyData()
    C = survey.find_ccds(expnum=expnum, ccdname=extname)
    print len(C), 'CCDs'
    im = survey.get_image_object(C[0])
    print 'im', im

    #(x0,x1,y0,y1) = opt.zoom
    #zoomslice = (slice(y0,y1), slice(x0,x1))
    zoomslice = None

    #tim = im.get_tractor_image(gaussPsf=True, splinesky=True, slc=zoomslice)
    tim = im.get_tractor_image(hybridPsf=True, splinesky=True, slc=zoomslice)
    print 'Tim', tim

    cats = []
    bricks = bricks_touching_wcs(tim.subwcs, survey=survey)
    bricknames = bricks.brickname
    for b in bricknames:
        fn = survey.find_file('tractor', brick=b)
        if not os.path.exists(fn):
            print 'WARNING: file does not exist:', fn
            continue
        print 'Reading', fn
        cat = fits_table(fn)
        print 'Read', len(cat), 'sources'
        if cat is None or len(cat) == 0:
            continue
        cats.append(cat)
    if len(cats):
        T = merge_tables(cats)
        T._header = cats[0]._header

        # margin
        M = 20
        ok, x, y = tim.subwcs.radec2pixelxy(T.ra, T.dec)
        x -= 1.
        y -= 1.
        T.x = x
        T.y = y
        H, W = tim.shape
        T.cut((x > -M) * (x < (W + M)) * (y > -M) * (y < (H + M)))
        print 'Cut to', len(T), 'within image bounds'

        T.cut(T.brick_primary)
        print 'Cut to', len(T), 'brick_primary'
        T.cut((T.out_of_bounds == False) * (T.left_blob == False))
        print 'Cut to', len(T), 'not out-of-bound or left-blob'
        print 'Brightest z-band:', np.max(T.decam_flux[:, 4])
        print 'Brightest r-band:', np.max(T.decam_flux[:, 2])

        orig_catalog = T.copy()

        # Cut to compact sources
        T.cut(np.maximum(T.shapeexp_r, T.shapedev_r) < 3.)
        print 'Cut to', len(T), 'compact catalog objects'

        cat = read_fits_catalog(T, allbands='ugrizY')

    else:
        cat = []
        orig_catalog = fits_table()

    print len(cat), 'catalog objects'

    if plots:
        plt.clf()
        img = tim.getImage()
        mn, mx = np.percentile(img.ravel(), [25, 99])
        print('Image plot range:', mn, mx)
        tim.ima = dict(interpolation='nearest',
                       origin='lower',
                       vmin=mn,
                       vmax=mx)
        plt.imshow(tim.getImage(), **tim.ima)
        plt.title('Orig data')
        ps.savefig()

    # Mask out bright pixels.
    mask = np.zeros(tim.shape, np.bool)
    bright = fits_table(brightstars)
    print 'Read', len(bright), 'SDSS bright stars'
    ok, bx, by = tim.subwcs.radec2pixelxy(bright.ra, bright.dec)
    bx = np.round(bx).astype(int)
    by = np.round(by).astype(int)

    H, W = mask.shape
    bright.modelmag = 22.5 - 2.5 * np.log10(bright.modelflux)
    mag = bright.modelmag[:, 2]
    radius = (10.**(3.5 - 0.15 * mag) / pixscale).astype(np.int)

    I = np.flatnonzero(ok * (radius > 0) * (bx + radius > 0) *
                       (bx - radius < W) * (by + radius > 0) *
                       (by - radius < H))
    print len(I), 'bright stars are near the image'

    xx, yy = np.meshgrid(np.arange(W), np.arange(H))
    for x, y, r in zip(bx[I], by[I], radius[I]):
        mask[(xx - x)**2 + (yy - y)**2 < r**2] = True

    mask[tim.inverr == 0] = True
    tim.inverr[mask] = 0.
    tim.data[mask] = 0.

    if plots:
        plt.clf()
        plt.imshow(mask,
                   interpolation='nearest',
                   origin='lower',
                   vmin=0,
                   vmax=1,
                   cmap='gray')
        plt.title('Mask')
        ps.savefig()

        plt.clf()
        plt.imshow(tim.getImage(), **tim.ima)
        plt.title('Masked')
        ps.savefig()

    tr = Tractor([tim], cat)
    mod = tr.getModelImage(tim)
    print('Model range:', mod.min(), mod.max())

    # print('Model counts:', [tim.getPhotoCal().brightnessToCounts(src.getBrightness())
    #                         for src in cat])
    # print('Catalog:')
    # for src in cat[:10]:
    #     print('  ', src)

    if False:
        # OLD DEBUGGING
        print 'Model median:', np.median(mod)
        rawimg = fitsio.read(
            'decals-lsb/images/decam/CP20150407/c4d_150410_035040_ooi_z_v1.fits.fz',
            ext=im.hdu)
        print 'Image median:', np.median(rawimg)
        print 'mid sky', tim.midsky
        rawmod = mod * tim.zpscale + tim.midsky
        print 'Model median:', np.median(rawmod)
        fitsio.write('model.fits', rawmod, clobber=True)

    if plots:
        plt.clf()
        plt.imshow(mod, **tim.ima)
        plt.title('Model')
        ps.savefig()

        ax = plt.axis()
        ok, xx, yy = tim.subwcs.radec2pixelxy(
            [src.getPosition().ra for src in cat],
            [src.getPosition().dec for src in cat])
        plt.plot(xx, yy, 'r+')
        plt.axis(ax)
        ps.savefig()

    mod[mask] = 0.

    if plots:
        plt.clf()
        plt.imshow(mod, **tim.ima)
        plt.title('Masked model')
        ps.savefig()

        imchi = dict(interpolation='nearest',
                     origin='lower',
                     vmin=-5,
                     vmax=5,
                     cmap='RdBu')

        # plt.clf()
        # plt.imshow((tim.getImage() - mod) * tim.getInvError(), **imchi)
        # plt.title('Chi')
        # plt.colorbar()
        # ps.savefig()

        plt.clf()
        plt.imshow((tim.getImage() - mod), **tim.ima)
        plt.title('Residuals')
        ps.savefig()

    resid = tim.getImage() - mod

    sky = np.zeros_like(resid)
    median_smooth(resid, mask, 256, sky)

    if plots:
        plt.clf()
        plt.imshow(sky, **tim.ima)
        plt.title('Smoothed residuals (sky)')
        ps.savefig()

    resid -= sky
    # Re-apply mask
    resid[mask] = 0.

    if plots:
        plt.clf()
        plt.imshow(resid, **tim.ima)
        plt.title('Residual - sky')
        ps.savefig()

    return dict(resid=resid,
                sky=sky,
                ps=ps,
                tim=tim,
                tr=tr,
                mod=mod,
                mask=mask,
                orig_catalog=orig_catalog,
                pixscale=pixscale)
Пример #3
0
def main():
    #img = fitsio.read('347736-S5.fits')
    img = fitsio.read('392772-N29.fits')
    print(img.shape)
    img = img.T.copy()
    mm = np.median(img)
    img -= mm
    # PAD
    padimg = np.zeros((2048, 4096))
    padimg[1:-1, 1:-1] = img
    img = padimg
    
    ps = PlotSequence('sky')
    lo,hi = np.percentile(img, [20,80])
    ima = dict(vmin=lo, vmax=hi, interpolation='nearest', origin='lower',
               cmap='gray')
    plt.clf()
    plt.imshow(img, **ima)
    ps.savefig()

    from astrometry.util.util import median_smooth
    
    grid = 512
    #grid = 256
    img = img.astype(np.float32)
    med = np.zeros_like(img)
    median_smooth(img, None, grid/2, med)

    plt.clf()
    plt.imshow(med, **ima)
    ps.savefig()

    # # Estimate per-pixel noise via Blanton's 5-pixel MAD
    slice1 = (slice(0,-5,10),slice(0,-5,10))
    slice2 = (slice(5,None,10),slice(5,None,10))
    mad = np.median(np.abs(img[slice1] - img[slice2]).ravel())
    sig1 = 1.4826 * mad / np.sqrt(2.)
    print('sig1 estimate:', sig1)

    from scipy.ndimage.morphology import binary_dilation
    
    med2 = np.zeros_like(img)
    mask = np.zeros(img.shape, bool)
    mask[binary_dilation(img > 5*sig1, iterations=5)] = True
    median_smooth(img, mask, grid/2, med2)

    # UN-PAD
    img = img[1:-1, 1:-1]
    med = med[1:-1, 1:-1]
    med2 = med2[1:-1, 1:-1]
    mask = mask[1:-1, 1:-1]

    sub = img - med
    
    plt.clf()
    plt.imshow(sub, **ima)
    ps.savefig()

    plt.clf()
    plt.imshow(img - med2, **ima)
    ps.savefig()

    plt.clf()
    plt.imshow(img * (1-mask), **ima)
    ps.savefig()
    
    plt.clf()
    plt.imshow(med2, **ima)
    ps.savefig()

    
    lo2,hi2 = np.percentile(img, [5,95])

    ha = dict(bins=100, range=(lo2,hi2), log=True,
             histtype='step')
    plt.clf()
    n1,b,p = plt.hist(img.ravel(), color='r', **ha)
    n2,b,p = plt.hist(sub.ravel(), color='b', **ha)
    mx = max(max(n1), max(n2))
    plt.ylim(mx*0.1, mx)
    ps.savefig()

    ha = dict(bins=100, range=(lo2,hi2), histtype='step')
    plt.clf()
    n1,b,p = plt.hist(img.ravel(), color='r', **ha)
    n2,b,p = plt.hist(sub.ravel(), color='b', **ha)

    n3,b,p = plt.hist((img - sub).ravel(), color='m', **ha)

    #mx = max(max(n1), max(n2))
    #plt.ylim(0, mx)
    ps.savefig()
Пример #4
0
def main():
    #img = fitsio.read('347736-S5.fits')
    img = fitsio.read('392772-N29.fits')
    print(img.shape)
    img = img.T.copy()
    mm = np.median(img)
    img -= mm
    # PAD
    padimg = np.zeros((2048, 4096))
    padimg[1:-1, 1:-1] = img
    img = padimg

    ps = PlotSequence('sky')
    lo, hi = np.percentile(img, [20, 80])
    ima = dict(vmin=lo,
               vmax=hi,
               interpolation='nearest',
               origin='lower',
               cmap='gray')
    plt.clf()
    plt.imshow(img, **ima)
    ps.savefig()

    from astrometry.util.util import median_smooth

    grid = 512
    #grid = 256
    img = img.astype(np.float32)
    med = np.zeros_like(img)
    median_smooth(img, None, grid / 2, med)

    plt.clf()
    plt.imshow(med, **ima)
    ps.savefig()

    # # Estimate per-pixel noise via Blanton's 5-pixel MAD
    slice1 = (slice(0, -5, 10), slice(0, -5, 10))
    slice2 = (slice(5, None, 10), slice(5, None, 10))
    mad = np.median(np.abs(img[slice1] - img[slice2]).ravel())
    sig1 = 1.4826 * mad / np.sqrt(2.)
    print('sig1 estimate:', sig1)

    from scipy.ndimage.morphology import binary_dilation

    med2 = np.zeros_like(img)
    mask = np.zeros(img.shape, bool)
    mask[binary_dilation(img > 5 * sig1, iterations=5)] = True
    median_smooth(img, mask, grid / 2, med2)

    # UN-PAD
    img = img[1:-1, 1:-1]
    med = med[1:-1, 1:-1]
    med2 = med2[1:-1, 1:-1]
    mask = mask[1:-1, 1:-1]

    sub = img - med

    plt.clf()
    plt.imshow(sub, **ima)
    ps.savefig()

    plt.clf()
    plt.imshow(img - med2, **ima)
    ps.savefig()

    plt.clf()
    plt.imshow(img * (1 - mask), **ima)
    ps.savefig()

    plt.clf()
    plt.imshow(med2, **ima)
    ps.savefig()

    lo2, hi2 = np.percentile(img, [5, 95])

    ha = dict(bins=100, range=(lo2, hi2), log=True, histtype='step')
    plt.clf()
    n1, b, p = plt.hist(img.ravel(), color='r', **ha)
    n2, b, p = plt.hist(sub.ravel(), color='b', **ha)
    mx = max(max(n1), max(n2))
    plt.ylim(mx * 0.1, mx)
    ps.savefig()

    ha = dict(bins=100, range=(lo2, hi2), histtype='step')
    plt.clf()
    n1, b, p = plt.hist(img.ravel(), color='r', **ha)
    n2, b, p = plt.hist(sub.ravel(), color='b', **ha)

    n3, b, p = plt.hist((img - sub).ravel(), color='m', **ha)

    #mx = max(max(n1), max(n2))
    #plt.ylim(0, mx)
    ps.savefig()
Пример #5
0
def main():
    '''
    This function generates the plots in the paper.

    Some files and directories are assumed to exist in the current directory:

    * WISE atlas tiles, from http://unwise.me/data/allsky-atlas.fits
    * unwise-neo1-coadds, from http://unwise.me/data/neo1/
    * unwise-neo1-coadds-half, unwise-neo1-coadds-quarter: directories

    '''
    # First, create the WCS into which we want to render
    # degrees width to render in galactic coords
    # |l| < 60
    # |b| < 30
    width = 120
    # ~2 arcmin per pixel
    W = int(width * 60.) / 2
    H = W/2
    zoom = 360. / width
    wcs = anwcs_create_hammer_aitoff(0., 0., zoom, W, H, 0)

    # Select WISE tiles that overlap.  This atlas table is available
    # from http://unwise.me/data/allsky-atlas.fits

    # Select WISE tiles that overlap.
    T = fits_table('allsky-atlas.fits')
    print(len(T), 'tiles total')
    T.ll,T.bb = radectolb(T.ra, T.dec)
    I = np.flatnonzero(np.logical_or(T.ll < width+1,
                                     T.ll > (360-width-1)) *
                                     (T.bb > -width/2-1) * (T.bb < width/2+1))
    T.cut(I)
    print(len(I), 'tiles in L,B range')

    # Create a coadd for each WISE band
    lbpat = 'unwise-neo1-w%i-lb.fits'
    imgs = []
    for band in [1,2]:
        outfn = lbpat % (band)
        if os.path.exists(outfn):
            print('Exists:', outfn)
            img = fitsio.read(outfn)
            imgs.append(img)
            continue

        coimg  = np.zeros((H,W), np.float32)
        conimg = np.zeros((H,W), np.float32)

        for i,brick in enumerate(T.coadd_id):
            # We downsample by 2, twice, just to make repeat runs a
            # little faster.
            # unWISE
            fn = os.path.join('unwise-neo1-coadds', brick[:3], brick,
                              'unwise-%s-w%i-img-u.fits' % (brick, band))
            qfn = os.path.join('unwise-neo1-coadds-quarter',
                               'unwise-%s-w%i.fits' % (brick, band))
            hfn = os.path.join('unwise-neo1-coadds-half',
                               'unwise-%s-w%i.fits' % (brick, band))

            if not os.path.exists(qfn):
                if not os.path.exists(hfn):
                    print('Reading', fn)
                    halfsize(fn, hfn)
                halfsize(hfn, qfn)
            fn = qfn

            print('Reading', fn)
            img = fitsio.read(fn)
            bwcs = Tan(fn, 0)
            bh,bw = img.shape

            # Coadd each unWISE pixel into the nearest target pixel.
            xx,yy = np.meshgrid(np.arange(bw), np.arange(bh))
            rr,dd = bwcs.pixelxy2radec(xx, yy)
            ll,bb = radectolb(rr.ravel(), dd.ravel())
            ll = ll.reshape(rr.shape)
            bb = bb.reshape(rr.shape)
            ok,ox,oy = wcs.radec2pixelxy(ll, bb)
            ox = np.round(ox - 1).astype(int)
            oy = np.round(oy - 1).astype(int)
            K = (ox >= 0) * (ox < W) * (oy >= 0) * (oy < H) * ok

            #print('ok:', np.unique(ok), 'x', ox.min(), ox.max(), 'y', oy.min(), oy.max())
            assert(np.all(np.isfinite(img)))
            if np.sum(K) == 0:
                # no overlap
                print('No overlap')
                continue
    
            np.add.at( coimg, (oy[K], ox[K]), img[K])
            np.add.at(conimg, (oy[K], ox[K]), 1)

        img = coimg / np.maximum(conimg, 1)

        # Hack -- write and then read FITS WCS header.
        fn = 'wiselb.wcs'
        wcs.writeto(fn)
        hdr = fitsio.read_header(fn)
        hdr['CTYPE1'] = 'GLON-AIT'
        hdr['CTYPE2'] = 'GLAT-AIT'

        fitsio.write(outfn, img, header=hdr, clobber=True)
        fitsio.write(outfn.replace('.fits', '-n.fits'), conimg,
                     header=hdr, clobber=True)
        imgs.append(img)

    w1,w2 = imgs

    # Get/confirm L,B bounds...
    H,W = w1.shape
    print('Image size', W, 'x', H)
    ok,l1,b1 = wcs.pixelxy2radec(1, (H+1)/2.)
    ok,l2,b2 = wcs.pixelxy2radec(W, (H+1)/2.)
    ok,l3,b3 = wcs.pixelxy2radec((W+1)/2., 1)
    ok,l4,b4 = wcs.pixelxy2radec((W+1)/2., H)
    print('L,B', (l1,b1), (l2,b2), (l3,b3), (l4,b4))
    llo,lhi = l2,l1+360
    blo,bhi = b3,b4
    
    # Set plot sizes
    plt.figure(1, figsize=(10,5))
    plt.subplots_adjust(left=0.1, right=0.95, bottom=0.1, top=0.95)

    plt.figure(2, figsize=(5,5))
    plt.subplots_adjust(left=0.11, right=0.96, bottom=0.1, top=0.95)

    suffix = '.pdf'
    
    rgb = wise_rgb(w1, w2)
    xlo,ylo = 0,0
    
    plt.figure(1)
    plt.clf()
    plt.imshow(rgb, origin='lower', interpolation='nearest')
    lbticks(wcs, xlo, ylo, lticks=[60,30,0,330,300], bticks=[-30,-15,0,15,30])
    plt.savefig('xbulge-00' + suffix)

    # Compute the median of each row as a crude way of suppressing the
    # Galactic plane
    medy1 = np.median(w1, axis=1)
    medy2 = np.median(w2, axis=1)

    rgb = wise_rgb(w1 - medy1[:,np.newaxis],
                   w2 - medy2[:,np.newaxis])

    # Zoom in a bit for Galactic plane subtracted version
    lhi,llo,blo,bhi = 40, 320, -20, 20
    okxy = np.array([wcs.radec2pixelxy(l,b) for l,b in [
            (llo, blo), (llo, bhi), (lhi, blo), (lhi, bhi)]])
    xlo = int(np.floor(min(okxy[:,-2])))
    xhi = int(np.ceil (max(okxy[:,-2])))
    ylo = int(np.floor(min(okxy[:,-1])))
    yhi = int(np.ceil (max(okxy[:,-1])))
    
    plt.clf()
    plt.imshow(rgb[ylo:yhi, xlo:xhi, :],origin='lower', interpolation='nearest')
    #lbticks(wcs, xlo, ylo, lticks=[40,20,0,340,320], bticks=[-20,-10,0,10,20])
    lbticks(wcs, xlo, ylo, lticks=[30,15,0,345,330], bticks=[-20,-10,0,10,20])
    plt.savefig('xbulge-01' + suffix)

    # Zoom in on the core
    lhi,llo,blo,bhi = 15, 345, -15, 15
    ok,x1,y1 = wcs.radec2pixelxy(llo, blo)
    ok,x2,y2 = wcs.radec2pixelxy(llo, bhi)
    ok,x3,y3 = wcs.radec2pixelxy(lhi, blo)
    ok,x4,y4 = wcs.radec2pixelxy(lhi, bhi)

    xlo = int(np.floor(min(x1,x2,x3,x4)))
    xhi = int(np.ceil (max(x1,x2,x3,x4)))
    ylo = int(np.floor(min(y1,y2,y3,y4)))
    yhi = int(np.ceil (max(y1,y2,y3,y4)))
    print('xlo,ylo', xlo, ylo)

    w1 = w1[ylo:yhi, xlo:xhi]
    w2 = w2[ylo:yhi, xlo:xhi]

    plt.figure(2)

    # Apply color cut
    w1mag = -2.5*(np.log10(w1) - 9.)
    w2mag = -2.5*(np.log10(w2) - 9.)
    cc = w1mag - w2mag
    goodcolor = np.isfinite(cc)
    mlo,mhi = np.percentile(cc[goodcolor], [5,95])
    print('W1 - W2 color masks:', mlo,mhi)
    mask = goodcolor * (cc > mlo) * (cc < mhi)

    plt.clf()
    rgb = wise_rgb(w1, w2)
    plt.imshow(rgb, origin='lower', interpolation='nearest')
    lbticks(wcs, xlo,ylo)
    plt.title('Data')
    plt.savefig('xbulge-fit-data' + suffix)

    plt.clf()
    rgb = wise_rgb(w1 * mask, w2 * mask)
    plt.imshow(rgb, origin='lower', interpolation='nearest')
    lbticks(wcs, xlo,ylo)
    plt.title('Data (masked)')
    plt.savefig('xbulge-fit-masked' + suffix)
    
    ie = mask.astype(np.float32)

    from tractor import (Image, NCircularGaussianPSF, LinearPhotoCal, Tractor,
                         PixPos, Fluxes)
    from tractor.galaxy import ExpGalaxy, GalaxyShape

    # Create Tractor images
    tim1 = Image(data=w1 * mask, inverr=ie,
                 psf=NCircularGaussianPSF([1.],[1.]),
                 photocal=LinearPhotoCal(1., 'w1'))
    tim2 = Image(data=w2 * mask, inverr=ie,
                 psf=NCircularGaussianPSF([1.],[1.]),
                 photocal=LinearPhotoCal(1., 'w2'))
    H,W = w1.shape
    gal = ExpGalaxy(PixPos(W/2, H/2), Fluxes(w1=w1.sum(), w2=w2.sum()),
                    GalaxyShape(200, 0.4, 90.))
    tractor = Tractor([tim1, tim2],[gal])

    # fitsio.write('data-w1.fits', w1 * mask, clobber=True)
    # fitsio.write('data-w2.fits', w2 * mask, clobber=True)
    # fitsio.write('mask.fits', mask.astype(np.uint8), clobber=True)

    # Optimize galaxy model
    tractor.freezeParam('images')
    for step in range(50):
        dlnp,x,alpha = tractor.optimize()
        print('dlnp', dlnp)
        print('x', x)
        print('alpha', alpha)
        print('Galaxy', gal)
        if dlnp == 0:
            break

    # Get galaxy model images, compute residuals
    mod1 = tractor.getModelImage(0)
    resid1 = w1 - mod1
    mod2 = tractor.getModelImage(1)
    resid2 = w2 - mod2

    rgb = wise_rgb(mod1, mod2)
    plt.clf()
    plt.imshow(rgb, origin='lower', interpolation='nearest')
    lbticks(wcs, xlo,ylo)
    plt.title('Model')
    plt.savefig('xbulge-fit-model' + suffix)

    rgb = resid_rgb(resid1, resid2)
    plt.clf()
    plt.imshow(rgb, origin='lower', interpolation='nearest')
    lbticks(wcs, xlo,ylo)
    plt.title('Residuals')
    plt.savefig('xbulge-fit-resid' + suffix)

    rgb = resid_rgb(resid1*mask, resid2*mask)
    plt.clf()
    plt.imshow(rgb, origin='lower', interpolation='nearest')
    lbticks(wcs, xlo,ylo)
    plt.title('Residuals (masked)')
    plt.savefig('xbulge-fit-residmasked' + suffix)

    # fitsio.write('resid1.fits', resid1, clobber=True)
    # fitsio.write('resid2.fits', resid2, clobber=True)

    # Compute median-smoothed residuals
    fr1 = np.zeros_like(resid1)
    fr2 = np.zeros_like(resid2)
    median_smooth(resid1, np.logical_not(mask), 25, fr1)
    median_smooth(resid2, np.logical_not(mask), 25, fr2)

    rgb = resid_rgb(fr1, fr2)
    plt.clf()
    plt.imshow(rgb, origin='lower', interpolation='nearest')
    lbticks(wcs, xlo,ylo)
    plt.title('Residuals (smoothed)')
    plt.savefig('xbulge-fit-smooth2' + suffix)
Пример #6
0
def main():
    #img = fitsio.read('347736-S5.fits')
    img = fitsio.read('392772-N29.fits')
    print(img.shape)
    img = img.T.copy()
    mm = np.median(img)
    img -= mm

    ps = PlotSequence('sky')
    lo, hi = np.percentile(img, [20, 80])
    ima = dict(vmin=lo,
               vmax=hi,
               interpolation='nearest',
               origin='lower',
               cmap='gray')
    plt.clf()
    plt.imshow(img, **ima)
    ps.savefig()

    # PAD
    padimg = np.zeros((2048, 4096))
    padimg[1:-1, 1:-1] = img
    img = padimg

    from tractor.splinesky import SplineSky
    from scipy.ndimage.morphology import binary_dilation
    from astrometry.util.util import median_smooth

    # # Estimate per-pixel noise via Blanton's 5-pixel MAD
    slice1 = (slice(0, -5, 10), slice(0, -5, 10))
    slice2 = (slice(5, None, 10), slice(5, None, 10))
    mad = np.median(np.abs(img[slice1] - img[slice2]).ravel())
    sig1 = 1.4826 * mad / np.sqrt(2.)
    print('sig1 estimate:', sig1)

    mask = np.zeros(img.shape, bool)
    mask[binary_dilation(img > 5 * sig1, iterations=5)] = True

    for mm in [None, mask]:

        notmm = None
        if mm is not None:
            notmm = np.logical_not(mm)

        sky = SplineSky.BlantonMethod(img, notmm, 512)

        skyfn = 'sky-%s.fits' % (mm is not None and 'mask' or 'nomask')
        sky.write_fits(skyfn)

        from tractor.utils import get_class_from_name

        print('Reading sky model from', skyfn)
        hdr = fitsio.read_header(skyfn)
        skyclass = hdr['SKY']
        clazz = get_class_from_name(skyclass)

        if getattr(clazz, 'from_fits'):
            fromfits = getattr(clazz, 'from_fits')
            skyobj = fromfits(skyfn, hdr)
        else:
            fromfits = getattr(clazz, 'fromFitsHeader')
            skyobj = fromfits(hdr, prefix='SKY_')
        sky2 = skyobj
        print('sky2', sky2)
        sky2.write_fits(skyfn.replace('sky', 'sky2'))

        mod = np.zeros_like(img)
        sky.addTo(mod)

        plt.clf()
        plt.imshow(mod, **ima)
        plt.title('Blanton method')
        ps.savefig()

        plt.clf()
        plt.imshow(img - mod, **ima)
        plt.title('Blanton method (subtracted)')
        ps.savefig()

        grid = 512
        #grid = 256
        img = img.astype(np.float32)
        med = np.zeros_like(img)
        median_smooth(img, mm, grid / 2, med)

        plt.clf()
        plt.imshow(med, **ima)
        plt.title('dmedsmooth')
        ps.savefig()

        plt.clf()
        plt.imshow(img - med, **ima)
        plt.title('dmedsmooth (subtracted)')
        ps.savefig()

    sys.exit(0)

    med2 = np.zeros_like(img)
    mask = np.zeros(img.shape, bool)
    mask[binary_dilation(img > 5 * sig1, iterations=5)] = True
    median_smooth(img, mask, grid / 2, med2)

    # UN-PAD
    img = img[1:-1, 1:-1]
    med = med[1:-1, 1:-1]
    med2 = med2[1:-1, 1:-1]
    mask = mask[1:-1, 1:-1]

    sub = img - med

    plt.clf()
    plt.imshow(sub, **ima)
    ps.savefig()

    plt.clf()
    plt.imshow(img - med2, **ima)
    ps.savefig()

    plt.clf()
    plt.imshow(img * (1 - mask), **ima)
    ps.savefig()

    plt.clf()
    plt.imshow(med2, **ima)
    ps.savefig()

    lo2, hi2 = np.percentile(img, [5, 95])

    ha = dict(bins=100, range=(lo2, hi2), log=True, histtype='step')
    plt.clf()
    n1, b, p = plt.hist(img.ravel(), color='r', **ha)
    n2, b, p = plt.hist(sub.ravel(), color='b', **ha)
    mx = max(max(n1), max(n2))
    plt.ylim(mx * 0.1, mx)
    ps.savefig()

    ha = dict(bins=100, range=(lo2, hi2), histtype='step')
    plt.clf()
    n1, b, p = plt.hist(img.ravel(), color='r', **ha)
    n2, b, p = plt.hist(sub.ravel(), color='b', **ha)

    n3, b, p = plt.hist((img - sub).ravel(), color='m', **ha)

    #mx = max(max(n1), max(n2))
    #plt.ylim(0, mx)
    ps.savefig()
Пример #7
0
fr1 = median_filter(resid1*mask, size=50)
fr2 = median_filter(resid2*mask, size=50)

rgb = resid_rgb(fr1, fr2)
plt.clf()
dimshow(rgb)
plt.title('Residuals (smoothed)')
lbticks(wcs, xlo,ylo)
ps.savefig()
plt.savefig('xbulge-fit-smooth.pdf')

from astrometry.util.util import median_smooth
fr1 = np.zeros_like(resid1)
fr2 = np.zeros_like(resid2)
median_smooth(resid1, np.logical_not(mask), 25, fr1)
median_smooth(resid2, np.logical_not(mask), 25, fr2)
#median_smooth(resid1, np.logical_not(mask), 15, fr1)
#median_smooth(resid2, np.logical_not(mask), 15, fr2)

rgb = resid_rgb(fr1, fr2)
plt.clf()
dimshow(rgb)
plt.title('Residuals (smoothed)')
lbticks(wcs, xlo,ylo)
ps.savefig()
plt.savefig('xbulge-fit-smooth2.pdf')