Beispiel #1
0
def create_source_table_from_fields(RCF, ra, dec, cutToPrimary,
                                    srcband, sdss):
    # gather rows from sources within a specified radius
    TT = []
    for ifield,(run,camcol,field) in enumerate(RCF):

        # Retrieve SDSS catalog sources in the field
        srcs,objs = tsdss.get_tractor_sources_dr9(
                        run, camcol, field,
                        bandname      = srcband,
                        sdss          = sdss,          # cache is in scratch/
                        radecrad      = (ra, dec, radius*np.sqrt(2.)),
                        nanomaggies   = True,
                        cutToPrimary  = cutToPrimary,
                        getsourceobjs = True,
                        useObjcType   = True)

        print 'Got sources:'
        for src in srcs:
            print '  ', src

        # Write out the sources
        T     = aufits.fits_table()
        T.ra  = [src.getPosition().ra  for src in srcs]
        T.dec = [src.getPosition().dec for src in srcs]

        # same objects, same order
        assert(len(objs) == len(srcs))
        assert(np.all(T.ra == objs.ra))

        # r-band
        bandnum     = 2
        T.primary   = ((objs.resolve_status & 256) > 0)
        T.run       = objs.run
        T.camcol    = objs.camcol
        T.field     = objs.field
        T.is_star   = (objs.objc_type == 6)
        T.frac_dev  = objs.fracdev[:,bandnum]
        T.theta_dev = objs.theta_dev[:,bandnum]
        T.theta_exp = objs.theta_exp[:,bandnum]
        T.phi_dev   = objs.phi_dev_deg[:,bandnum]
        T.phi_exp   = objs.phi_exp_deg[:,bandnum]
        T.ab_dev    = objs.ab_dev[:,bandnum]
        T.ab_exp    = objs.ab_exp[:,bandnum]

        for band in bands:
            bi = tsdss.band_index(band)
            T.set('psfflux_%s' % band, objs.psfflux[:,bi])
            T.set('devflux_%s' % band, objs.devflux[:,bi])
            T.set('expflux_%s' % band, objs.expflux[:,bi])
            T.set('cmodelflux_%s' % band, objs.cmodelflux[:,bi])

        TT.append(T)
    T = tsdss.merge_tables(TT)
    return T
Beispiel #2
0
def make_resampled_psf_images(RCF, band, ra, dec, sdss, 
                            targetwcs, W, H, addToHeader, plots=False,
                            max_exposures=1):
    """ Given a list of (Run, Camcol, Field) tuples, returns a list of
    (img, imgvar, and header) info for stamp sized imgs centered at ra, dec
    """
    # populate list of resampled images and their new psf's
    output_imgs = []

    # zip through each frame, cut out the relevatn patch
    for ifield, (run,camcol,field) in enumerate(RCF[:max_exposures]):
        print """=============================
               RCF %d of %d 
               ======================== """%(ifield, len(RCF))
        # get photofield filename from SDSS, cut it down to relevent RCF
        fn = sdss.retrieve('photoField', run, camcol, field)
        F  = aufits.fits_table(fn)
        F.cut((F.run == run) * (F.camcol == camcol) * (F.field == field))
        print len(F), 'fields'
        assert(len(F) == 1)
        F = F[0]

        # actually get the tractor image (check if it's in cache!)
        boundpixradius = int(np.ceil(np.sqrt(2.) * pixradius))
        print 'RA,Dec,size', (ra, dec, boundpixradius)
        tim, tinfo = tsdss.get_tractor_image_dr9(
            run, camcol, field, band, sdss=sdss, nanomaggies=True,
            roiradecsize=(ra, dec, boundpixradius))
        print 'Got tim:', tim
        frame = sdss.readFrame(run, camcol, field, band)
        if tim is None:
            continue

        # find pixel position for input RA, DEC in tractor image (original field)
        x,y = tim.getWcs().positionToPixel(tsdss.RaDecPos(ra, dec))
        x,y = int(x), int(y)

        # Grab calibration information for header
        tim.sdss_calib   = np.median(frame.getCalibVec())
        tim.sdss_sky     = frame.getSkyAt(x,y)
        iband            = tsdss.band_index(band)
        tim.sdss_gain    = F.gain[iband]
        tim.sdss_darkvar = F.dark_variance[iband]

        # get region of interest in the original frame
        roi = tinfo['roi']
        x0,x1,y0,y1 = roi

        # Resample to common grid
        th,tw = tim.shape
        wwcs = tsdss.TractorWCSWrapper(tim.getWcs(), tw, th)
        try:
            Yo,Xo,Yi,Xi,[rim] = aresample.resample_with_wcs(
                targetwcs, wwcs, [tim.getImage()], Lanczos)
        except aresample.OverlapError:
            continue
        img = np.zeros((H,W))
        img[Yo,Xo] = rim
        iv  = np.zeros((H,W))
        iv[Yo,Xo] = tim.getInvvar()[Yi,Xi]

        # Convert old PSF to new stamp-specific PSF
        newpsf = convert_psf_between_imgs(tim, targetwcs)

        # create the image's header
        hdr = construct_new_header(tim, tinfo, targetwcs, newpsf, 
                                    run, camcol, field, band, addToHeader)

        # add to the list of resampled imgs,
        output_imgs.append((img, iv, hdr))

    return output_imgs