def compare_brick_to_ps1(brickname, ps, name='', basedir=''): decals = Decals() brick = decals.get_brick_by_name(brickname) wcs = wcs_for_brick(brick) magrange = (15,20) ps1 = ps1cat(ccdwcs=wcs) ps1 = ps1.get_stars(magrange=magrange) print 'Got', len(ps1), 'PS1 stars' T = fits_table(os.path.join(basedir, 'tractor', brickname[:3], 'tractor-%s.fits' % brickname)) I,J,d = match_radec(T.ra, T.dec, ps1.ra, ps1.dec, 1./3600.) print 'Matched', len(I), 'stars to PS1' T.cut(I) ps1.cut(J) bands = 'z' ap = 5 allbands = 'ugrizY' mags = np.arange(magrange[0], 1+magrange[1]) for band in bands: iband = allbands.index(band) piband = ps1cat.ps1band[band] T.flux = T.decam_flux[:,iband] T.mag = NanoMaggies.nanomaggiesToMag(T.flux) print 'apflux shape', T.decam_apflux.shape T.apflux = T.decam_apflux[:, iband, ap] T.apmag = NanoMaggies.nanomaggiesToMag(T.apflux) ps1mag = ps1.median[:,piband] plt.clf() for cc,mag,label in [('b', T.mag, 'Mag'), ('r', T.apmag, 'Aper mag')]: plt.plot(ps1mag, mag - ps1mag, '.', color=cc, label=label, alpha=0.6) mm,dd = [],[] for mlo,mhi in zip(mags, mags[1:]): I = np.flatnonzero((ps1mag > mlo) * (ps1mag <= mhi)) mm.append((mlo+mhi)/2.) dd.append(np.median(mag[I] - ps1mag[I])) plt.plot(mm, dd, 'o-', color=cc) plt.xlabel('PS1 %s mag' % band) plt.ylabel('Mag - PS1 (mag)') plt.title('%sPS1 comparison: brick %s' % (name, brickname)) plt.ylim(-0.2, 0.2) mlo,mhi = magrange plt.xlim(mhi, mlo) plt.axhline(0., color='k', alpha=0.1) plt.legend() ps.savefig()
def apphot_ps1stars(ccd, ps, apertures, decals, sky_inner_r=40, sky_outer_r=50): im = decals.get_image_object(ccd) tim = im.get_tractor_image(gaussPsf=True, splinesky=True) img = tim.getImage() wcs = tim.subwcs magrange = (15,21) ps1 = ps1cat(ccdwcs=wcs) ps1 = ps1.get_stars(magrange=magrange) print 'Got', len(ps1), 'PS1 stars' band = ccd.filter piband = ps1cat.ps1band[band] print 'band:', band ps1.cut(ps1.nmag_ok[:,piband] > 0) print 'Keeping', len(ps1), 'stars with nmag_ok' ok,x,y = wcs.radec2pixelxy(ps1.ra, ps1.dec) apxy = np.vstack((x - 1., y - 1.)).T ap = [] aperr = [] nmasked = [] with np.errstate(divide='ignore'): ie = tim.getInvError() imsigma = 1. / ie imsigma[ie == 0] = 0 mask = (imsigma == 0) for rad in apertures: aper = photutils.CircularAperture(apxy, rad) p = photutils.aperture_photometry(img, aper, error=imsigma, mask=mask) aperr.append(p.field('aperture_sum_err')) ap.append(p.field('aperture_sum')) p = photutils.aperture_photometry((ie == 0), aper) nmasked.append(p.field('aperture_sum')) ap = np.vstack(ap).T aperr = np.vstack(aperr).T nmasked = np.vstack(nmasked).T print 'Aperture fluxes:', ap[:5] print 'Aperture flux errors:', aperr[:5] print 'Nmasked:', nmasked[:5] H,W = img.shape sky = [] skysigma = [] skymed = [] skynmasked = [] for xi,yi in zip(x,y): ix = int(np.round(xi)) iy = int(np.round(yi)) skyR = sky_outer_r xlo = max(0, ix-skyR) xhi = min(W, ix+skyR+1) ylo = max(0, iy-skyR) yhi = min(H, iy+skyR+1) xx,yy = np.meshgrid(np.arange(xlo,xhi), np.arange(ylo,yhi)) r2 = (xx - xi)**2 + (yy - yi)**2 inannulus = ((r2 >= sky_inner_r**2) * (r2 < sky_outer_r**2)) unmasked = (ie[ylo:yhi, xlo:xhi] > 0) #sky.append(np.median(img[ylo:yhi, xlo:xhi][inannulus * unmasked])) skypix = img[ylo:yhi, xlo:xhi][inannulus * unmasked] # this is the default value... nsigma = 4. goodpix,lo,hi = sigmaclip(skypix, low=nsigma, high=nsigma) # sigmaclip returns unclipped pixels, lo,hi, where lo,hi are # mean(goodpix) +- nsigma * sigma meansky = np.mean(goodpix) sky.append(meansky) skysigma.append((meansky - lo) / nsigma) skymed.append(np.median(skypix)) skynmasked.append(np.sum(inannulus * np.logical_not(unmasked))) sky = np.array(sky) skysigma = np.array(skysigma) skymed = np.array(skymed) skynmasked = np.array(skynmasked) print 'sky', sky[:5] print 'median sky', skymed[:5] print 'sky sigma', skysigma[:5] psmag = ps1.median[:,piband] ap2 = ap - sky[:,np.newaxis] * (np.pi * apertures**2)[np.newaxis,:] if ps is not None: plt.clf() nstars,naps = ap.shape for iap in range(naps): plt.plot(psmag, ap[:,iap], 'b.') #for iap in range(naps): # plt.plot(psmag, ap2[:,iap], 'r.') plt.yscale('symlog') plt.xlabel('PS1 %s mag' % band) plt.ylabel('DECam Aperture Flux') #plt.plot(psmag, nmasked[:,-1], 'ro') plt.plot(np.vstack((psmag,psmag)), np.vstack((np.zeros_like(psmag),nmasked[:,-1])), 'r-', alpha=0.5) plt.ylim(0, 1e3) ps.savefig() plt.clf() plt.plot(ap.T / np.max(ap, axis=1), '.') plt.ylim(0, 1) ps.savefig() plt.clf() dimshow(tim.getImage(), **tim.ima) ax = plt.axis() plt.plot(x, y, 'o', mec='r', mfc='none', ms=10) plt.axis(ax) ps.savefig() color = ps1_to_decam(ps1.median, band) print 'Color terms:', color T = fits_table() T.apflux = ap.astype(np.float32) T.apfluxerr = aperr.astype(np.float32) T.apnmasked = nmasked.astype(np.int16) # Zero out the errors when pixels are masked T.apfluxerr[T.apnmasked > 0] = 0. #T.apflux2 = ap2.astype(np.float32) T.sky = sky.astype(np.float32) T.skysigma = skysigma.astype(np.float32) T.expnum = np.array([ccd.expnum] * len(T)) T.ccdname = np.array([ccd.ccdname] * len(T)).astype('S3') T.band = np.array([band] * len(T)) T.ps1_objid = ps1.obj_id T.ps1_mag = psmag + color T.ra = ps1.ra T.dec = ps1.dec T.tai = np.array([tim.time.toMjd()] * len(T)).astype(np.float32) T.airmass = np.array([tim.primhdr['AIRMASS']] * len(T)).astype(np.float32) T.x = (x + tim.x0).astype(np.float32) T.y = (y + tim.y0).astype(np.float32) if False: plt.clf() plt.plot(skymed, sky, 'b.') plt.xlabel('sky median') plt.ylabel('sigma-clipped sky') ax = plt.axis() lo,hi = min(ax),max(ax) plt.plot([lo,hi],[lo,hi],'k-', alpha=0.25) plt.axis(ax) ps.savefig() return T, tim.primhdr
def compare_to_ps1(ps, ccds): decals = Decals() allplots = [] for expnum,ccdname in ccds: ccd = decals.find_ccds(expnum=expnum, ccdname=ccdname) assert(len(ccd) == 1) ccd = ccd[0] im = decals.get_image_object(ccd) print 'Reading', im wcs = im.get_wcs() magrange = (15,20) ps1 = ps1cat(ccdwcs=wcs) ps1 = ps1.get_stars(band=im.band, magrange=magrange) print 'Got', len(ps1), 'PS1 stars' # ps1.about() F = fits_table('forced-%i-%s.fits' % (expnum, ccdname)) print 'Read', len(F), 'forced-phot results' F.ra,F.dec = wcs.pixelxy2radec(F.x+1, F.y+1) I,J,d = match_radec(F.ra, F.dec, ps1.ra, ps1.dec, 1./3600.) print 'Matched', len(I), 'stars to PS1' F.cut(I) ps1.cut(J) F.mag = NanoMaggies.nanomaggiesToMag(F.flux) F.apmag = NanoMaggies.nanomaggiesToMag(F.apflux[:,5]) iband = ps1cat.ps1band[im.band] ps1mag = ps1.median[:,iband] mags = np.arange(magrange[0], 1+magrange[1]) psf = im.read_psf_model(0, 0, pixPsf=True) pixscale = 0.262 apertures = apertures_arcsec / pixscale h,w = ccd.height, ccd.width psfimg = psf.getPointSourcePatch(w/2., h/2.).patch ph,pw = psfimg.shape cx,cy = pw/2, ph/2 apphot = [] for rad in apertures: aper = photutils.CircularAperture((cx,cy), rad) p = photutils.aperture_photometry(psfimg, aper) apphot.append(p.field('aperture_sum')) apphot = np.hstack(apphot) print 'aperture photometry:', apphot skyest = apphot[6] - apphot[5] print 'Sky estimate:', skyest skyest /= np.pi * (apertures[6]**2 - apertures[5]**2) print 'Sky estimate per pixel:', skyest fraction = apphot[5] - skyest * np.pi * apertures[5]**2 print 'Fraction of flux:', fraction zp = 2.5 * np.log10(fraction) print 'ZP adjustment:', zp plt.clf() for cc,mag,label in [('b', F.mag, 'Forced mag'), ('r', F.apmag, 'Aper mag')]: plt.plot(ps1mag, mag - ps1mag, '.', color=cc, label=label, alpha=0.6) mm,dd = [],[] for mlo,mhi in zip(mags, mags[1:]): I = np.flatnonzero((ps1mag > mlo) * (ps1mag <= mhi)) mm.append((mlo+mhi)/2.) dd.append(np.median(mag[I] - ps1mag[I])) plt.plot(mm, dd, 'o-', color=cc) mm = np.array(mm) dd = np.array(dd) plt.plot(mm, dd - zp, 'o--', lw=3, alpha=0.5, color=cc) allplots.append((mm, dd, zp, cc, label)) plt.xlabel('PS1 %s mag' % im.band) plt.ylabel('Mag - PS1 (mag)') plt.title('PS1 - Single-epoch mag: %i-%s' % (expnum, ccdname)) plt.ylim(-0.2, 0.2) mlo,mhi = magrange plt.xlim(mhi, mlo) plt.axhline(0., color='k', alpha=0.1) plt.legend() ps.savefig() plt.clf() # for mm,dd,zp,cc,label in allplots: # plt.plot(mm, dd, 'o-', color=cc, label=label) # plt.plot(mm, dd - zp, 'o--', lw=3, alpha=0.5, color=cc) for sp,add in [(1,False),(2,True)]: plt.subplot(2,1,sp) for mm,dd,zp,cc,label in allplots: if add: plt.plot(mm, dd - zp, 'o--', lw=3, alpha=0.5, color=cc) else: plt.plot(mm, dd, 'o-', color=cc, label=label) plt.ylabel('Mag - PS1 (mag)') plt.ylim(-0.2, 0.05) mlo,mhi = magrange plt.xlim(mhi, mlo) plt.axhline(0., color='k', alpha=0.1) plt.axhline(-0.05, color='k', alpha=0.1) plt.axhline(-0.1, color='k', alpha=0.1) plt.xlabel('PS1 %s mag' % im.band) plt.suptitle('PS1 - Single-epoch mags') #plt.legend() ps.savefig() plt.clf() for mm,dd,zp,cc,label in allplots: plt.plot(mm, dd, 'o-', color=cc, label=label) plt.plot(mm, dd - zp, 'o--', lw=3, alpha=0.5, color=cc) plt.ylabel('Mag - PS1 (mag)') plt.ylim(-0.2, 0.05) mlo,mhi = magrange plt.xlim(mhi, mlo) plt.axhline(0., color='k', alpha=0.1) plt.xlabel('PS1 %s mag' % im.band) plt.suptitle('PS1 - Single-epoch mags') ps.savefig()
def apphot_ps1stars(ccd, ps, apertures, decals, sky_inner_r=40, sky_outer_r=50): im = decals.get_image_object(ccd) tim = im.get_tractor_image(gaussPsf=True, splinesky=True) img = tim.getImage() wcs = tim.subwcs magrange = (15, 21) ps1 = ps1cat(ccdwcs=wcs) ps1 = ps1.get_stars(magrange=magrange) print 'Got', len(ps1), 'PS1 stars' band = ccd.filter piband = ps1cat.ps1band[band] print 'band:', band ps1.cut(ps1.nmag_ok[:, piband] > 0) print 'Keeping', len(ps1), 'stars with nmag_ok' ok, x, y = wcs.radec2pixelxy(ps1.ra, ps1.dec) apxy = np.vstack((x - 1., y - 1.)).T ap = [] aperr = [] nmasked = [] with np.errstate(divide='ignore'): ie = tim.getInvError() imsigma = 1. / ie imsigma[ie == 0] = 0 mask = (imsigma == 0) for rad in apertures: aper = photutils.CircularAperture(apxy, rad) p = photutils.aperture_photometry(img, aper, error=imsigma, mask=mask) aperr.append(p.field('aperture_sum_err')) ap.append(p.field('aperture_sum')) p = photutils.aperture_photometry((ie == 0), aper) nmasked.append(p.field('aperture_sum')) ap = np.vstack(ap).T aperr = np.vstack(aperr).T nmasked = np.vstack(nmasked).T print 'Aperture fluxes:', ap[:5] print 'Aperture flux errors:', aperr[:5] print 'Nmasked:', nmasked[:5] H, W = img.shape sky = [] skysigma = [] skymed = [] skynmasked = [] for xi, yi in zip(x, y): ix = int(np.round(xi)) iy = int(np.round(yi)) skyR = sky_outer_r xlo = max(0, ix - skyR) xhi = min(W, ix + skyR + 1) ylo = max(0, iy - skyR) yhi = min(H, iy + skyR + 1) xx, yy = np.meshgrid(np.arange(xlo, xhi), np.arange(ylo, yhi)) r2 = (xx - xi)**2 + (yy - yi)**2 inannulus = ((r2 >= sky_inner_r**2) * (r2 < sky_outer_r**2)) unmasked = (ie[ylo:yhi, xlo:xhi] > 0) #sky.append(np.median(img[ylo:yhi, xlo:xhi][inannulus * unmasked])) skypix = img[ylo:yhi, xlo:xhi][inannulus * unmasked] # this is the default value... nsigma = 4. goodpix, lo, hi = sigmaclip(skypix, low=nsigma, high=nsigma) # sigmaclip returns unclipped pixels, lo,hi, where lo,hi are # mean(goodpix) +- nsigma * sigma meansky = np.mean(goodpix) sky.append(meansky) skysigma.append((meansky - lo) / nsigma) skymed.append(np.median(skypix)) skynmasked.append(np.sum(inannulus * np.logical_not(unmasked))) sky = np.array(sky) skysigma = np.array(skysigma) skymed = np.array(skymed) skynmasked = np.array(skynmasked) print 'sky', sky[:5] print 'median sky', skymed[:5] print 'sky sigma', skysigma[:5] psmag = ps1.median[:, piband] ap2 = ap - sky[:, np.newaxis] * (np.pi * apertures**2)[np.newaxis, :] if ps is not None: plt.clf() nstars, naps = ap.shape for iap in range(naps): plt.plot(psmag, ap[:, iap], 'b.') #for iap in range(naps): # plt.plot(psmag, ap2[:,iap], 'r.') plt.yscale('symlog') plt.xlabel('PS1 %s mag' % band) plt.ylabel('DECam Aperture Flux') #plt.plot(psmag, nmasked[:,-1], 'ro') plt.plot(np.vstack((psmag, psmag)), np.vstack((np.zeros_like(psmag), nmasked[:, -1])), 'r-', alpha=0.5) plt.ylim(0, 1e3) ps.savefig() plt.clf() plt.plot(ap.T / np.max(ap, axis=1), '.') plt.ylim(0, 1) ps.savefig() plt.clf() dimshow(tim.getImage(), **tim.ima) ax = plt.axis() plt.plot(x, y, 'o', mec='r', mfc='none', ms=10) plt.axis(ax) ps.savefig() color = ps1_to_decam(ps1.median, band) print 'Color terms:', color T = fits_table() T.apflux = ap.astype(np.float32) T.apfluxerr = aperr.astype(np.float32) T.apnmasked = nmasked.astype(np.int16) # Zero out the errors when pixels are masked T.apfluxerr[T.apnmasked > 0] = 0. #T.apflux2 = ap2.astype(np.float32) T.sky = sky.astype(np.float32) T.skysigma = skysigma.astype(np.float32) T.expnum = np.array([ccd.expnum] * len(T)) T.ccdname = np.array([ccd.ccdname] * len(T)).astype('S3') T.band = np.array([band] * len(T)) T.ps1_objid = ps1.obj_id T.ps1_mag = psmag + color T.ra = ps1.ra T.dec = ps1.dec T.tai = np.array([tim.time.toMjd()] * len(T)).astype(np.float32) T.airmass = np.array([tim.primhdr['AIRMASS']] * len(T)).astype(np.float32) T.x = (x + tim.x0).astype(np.float32) T.y = (y + tim.y0).astype(np.float32) if False: plt.clf() plt.plot(skymed, sky, 'b.') plt.xlabel('sky median') plt.ylabel('sigma-clipped sky') ax = plt.axis() lo, hi = min(ax), max(ax) plt.plot([lo, hi], [lo, hi], 'k-', alpha=0.25) plt.axis(ax) ps.savefig() return T, tim.primhdr