def make_fits_images(run, camcol, field): """gets field files from local cache (or sdss), returns UGRIZ dict of fits images""" print """==================================================\n\n Grabbing image files from the cache. TODO: turn off the tractor printing... """ imgs = {} for band in BANDS: print "reading in band %s" % band imgs[band] = sdss.get_tractor_image_dr9(run, camcol, field, band) fn = asdss.DR9().retrieve('photoField', run, camcol, field) F = aufits.fits_table(fn) # convert to FitsImage's imgfits = {} for iband,band in enumerate(BANDS): print "converting images %s" % band frame = asdss.DR9().readFrame(run, camcol, field, band) calib = np.median(frame.getCalibVec()) gain = F[0].gain[iband] darkvar = F[0].dark_variance[iband] sky = np.median(frame.getSky()) imgfits[band] = celeste.FitsImage(iband, timg=imgs[band], calib=calib, gain=gain, darkvar=darkvar, sky=sky) return imgfits
def make_fits_images(run, camcol, field): """gets field files from local cache (or sdss), returns UGRIZ dict of fits images""" print """==================================================\n\n Grabbing image files from the cache. TODO: turn off the tractor printing... """ imgs = {} for band in BANDS: print "reading in band %s" % band imgs[band] = sdss.get_tractor_image_dr9(run, camcol, field, band) fn = asdss.DR9().retrieve('photoField', run, camcol, field) F = aufits.fits_table(fn) # convert to FitsImage's imgfits = {} for iband,band in enumerate(BANDS): print "converting images %s" % band frame = asdss.DR9().readFrame(run, camcol, field, band) calib = np.median(frame.getCalibVec()) gain = F[0].gain[iband] darkvar = F[0].dark_variance[iband] sky = np.median(frame.getSky()) imgfits[band] = celeste.FitsImage(band, timg=imgs[band], calib=calib, gain=gain, darkvar=darkvar, sky=sky) return imgfits
def make_fits_images(run, camcol, field): """gets field files from local cache (or sdss), returns UGRIZ dict of fits images""" print """==================================================\n\n Grabbing image files from the cache. TODO: turn off the tractor printing... """ # grab photo field objects - cache in 'sdss_data' if possible if not os.path.exists('sdss_data'): os.makedirs('sdss_data') sdssobj = asdss.DR9(basedir='sdss_data') # grab images imgs = {} for band in BANDS: print "reading in band %s" % band imgs[band] = sdss.get_tractor_image_dr9(run, camcol, field, band, sdss=sdssobj) # grab photo field fn = sdssobj.retrieve('photoField', run, camcol, field) F = aufits.fits_table(fn) # convert to FitsImage's imgfits = {} for iband, band in enumerate(BANDS): print "converting images %s" % band frame = sdssobj.readFrame(run, camcol, field, band) calib = np.median(frame.getCalibVec()) gain = F[0].gain[iband] darkvar = F[0].dark_variance[iband] sky = np.median(frame.getSky()) imgfits[band] = FitsImage(band, timg=imgs[band], calib=calib, gain=gain, darkvar=darkvar, sky=sky, frame=frame, fits_table=F) return imgfits
def make_fits_images(run, camcol, field): """gets field files from local cache (or sdss), returns UGRIZ dict of fits images""" print """==================================================\n\n Grabbing image files from the cache. TODO: turn off the tractor printing... """ # grab photo field objects - cache in 'sdss_data' if possible if not os.path.exists('sdss_data'): os.makedirs('sdss_data') sdssobj = asdss.DR9(basedir='sdss_data') # grab images imgs = {} for band in BANDS: print "reading in band %s" % band imgs[band] = sdss.get_tractor_image_dr9(run, camcol, field, band, sdss=sdssobj) # grab photo field fn = sdssobj.retrieve('photoField', run, camcol, field) F = aufits.fits_table(fn) # convert to FitsImage's imgfits = {} for iband,band in enumerate(BANDS): print "converting images %s" % band frame = sdssobj.readFrame(run, camcol, field, band) calib = np.median(frame.getCalibVec()) gain = F[0].gain[iband] darkvar = F[0].dark_variance[iband] sky = np.median(frame.getSky()) imgfits[band] = FitsImage(band, timg=imgs[band], calib=calib, gain=gain, darkvar=darkvar, sky=sky, frame=frame, fits_table=F) return imgfits
def getSDSSdata(rcf,roi,datadir,vb=False): ''' INPUTS rcf A list of number, specifying the SDSS run, camcol and field ids, ra and dec roi Width of region of interest (in arcsec) datadir Name of directory where fits images will be written to (for posterity) vb Verbose operation? OUTPUTS images A list of tractor image objects, one per image centroids List of flux centroids, one per image total_mags List of total magnitudes, one per image bands List of filter names, one per image ''' run,camcol,field,ra,dec = rcf[0],rcf[1],rcf[2],rcf[3],rcf[4] roipix = int(roi/0.396) geometry = (ra, dec, 0.5*roipix) SDSSbands = ['u','g','r','i','z'] bands = [] images = [] centroids = [] total_mags = [] if vb: print "Querying SDSS skyserver for data at ra,dec = ",ra,dec print " in run, camcol, field = ",[run,camcol,field] print "Looking to make cutouts that are ",roi," arcsec (",roipix," pixels) across" # Ideally we would have first grabbed SDSS run, camcol and field id: # rcf = radec_to_sdss_rcf(ra,dec,radius=math.hypot(fieldradius,13./2.),tablefn="dr9fields.fits") # but, we do not have this table. So need an rcf passed in as an argument. for band in SDSSbands: if vb: print " " print "--------"+band+"-band--------" # Get SDSS image: image,info = st.get_tractor_image_dr9(run, camcol, field, band, roiradecsize=geometry) bands.append(band) images.append(image) # Compute flux centroid for this image (dummy for now): centroid = tractor.RaDecPos(ra,dec) centroids.append(centroid) # Compute total magnitude for this image: background = np.median(image.data) diffimage = image.data - background total_flux = np.sum(diffimage) total_mag = image.photocal.countsToMag(total_flux) total_mags.append(total_mag) if vb: print "Got pixel data in the "+band+" band" print "Image size: ",image.data.shape,"pixels" print "Total brighness of image (mags): ",total_mag return images,centroids,np.array(total_mags),np.array(bands)
axarr[0].set_title("Celeste Render") ########################## # plot tractor rendering # ######################### print "\n========== tractor render ===============\n" from tractor import sdss as st from tractor import Tractor from astrometry.sdss import DR9 # get images sdss = DR9(basedir='sdss_data') tim, tinf = st.get_tractor_image_dr9(run, camcol, field, fimg.band, curl=True, roi=(xlim[0], xlim[1] - 1, ylim[0], ylim[1] - 1), sdss=sdss, psf='kl-gm') #patch = ts.getModelPatch(tim) px, py = fimg.equa2pixel(s.params.u) - np.array([xlim[0], ylim[0]]) patch = ts.getUnitFluxModelPatch(tim) #, px, py) mod = patch.getImage() # get corresponding sources from CelestePy.util.misc import plot_util #tractor = Tractor(images = [tim], catalog=[ts]) #mod = tractor.getModelImage(tim, srcs=[ts]) cim = axarr[1].imshow(mod, extent=xlim + ylim) plot_util.add_colorbar_to_axis(axarr[1], cim)
########################### simg, ylim, xlim = s.compute_scatter_on_pixels(fimg) print "\n========== celeste render ===============\n" s.plot(fimg, ax=axarr[0], unit_flux=True) axarr[0].set_title("Celeste Render") ########################## # plot tractor rendering # ######################### print "\n========== tractor render ===============\n" from tractor import sdss as st from tractor import Tractor from astrometry.sdss import DR9 # get images sdss = DR9(basedir='sdss_data') tim,tinf = st.get_tractor_image_dr9(run, camcol, field, fimg.band, curl=True, roi=(xlim[0], xlim[1]-1, ylim[0], ylim[1]-1), sdss=sdss, psf='kl-gm') #patch = ts.getModelPatch(tim) px, py = fimg.equa2pixel(s.params.u) - np.array([xlim[0], ylim[0]]) patch = ts.getUnitFluxModelPatch(tim) #, px, py) mod = patch.getImage() # get corresponding sources from CelestePy.util.misc import plot_util #tractor = Tractor(images = [tim], catalog=[ts]) #mod = tractor.getModelImage(tim, srcs=[ts]) cim = axarr[1].imshow(mod, extent=xlim+ylim) plot_util.add_colorbar_to_axis(axarr[1], cim) axarr[1].set_title("Tractor Render") cim = axarr[2].imshow(mod - simg) plot_util.add_colorbar_to_axis(axarr[2], cim)
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
x0, y0 = roi[0], roi[2] # rd1 is near the position of the SDSS-catalog object # rd2 is four pixels above. rd1 = RaDecPos(225.67954, 11.265948) rd2 = RaDecPos(225.67991, 11.265852) lvl = logging.INFO logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout) bandname = band sdssprefix = '%06i-%s%i-%04i' % (run, bandname, camcol, field) timg, info = st.get_tractor_image_dr9(run, camcol, field, bandname, roi=roi) sources = st.get_tractor_sources_dr9(run, camcol, field, bandname, roi=roi) wcs = timg.getWcs() for source in sources: x, y = wcs.positionToPixel(source.getPosition(), src=source) print(' (%.2f, %.2f):' % (x + x0, y + y0), source) tractor = Tractor([timg], sources) lnlorig = tractor.getLogLikelihood() zr = np.array([-5., +20.]) * info['skysig'] save(sdssprefix + '-orig', tractor, zr)