def _detmap(X): from scipy.ndimage.filters import gaussian_filter from legacypipe.survey import tim_get_resamp (tim, targetwcs, H, W) = X R = tim_get_resamp(tim, targetwcs) if R is None: return None,None,None,None,None ie = tim.getInvvar() assert(tim.psf_sigma > 0) psfnorm = 1./(2. * np.sqrt(np.pi) * tim.psf_sigma) detim = tim.getImage().copy() detim[ie == 0] = 0. # Patch SATURATED pixels with the value saturated pixels would have?? #detim[(tim.dq & tim.dq_bits['satur']) > 0] = tim.satval detim = gaussian_filter(detim, tim.psf_sigma) / psfnorm**2 detsig1 = tim.sig1 / psfnorm subh,subw = tim.shape detiv = np.zeros((subh,subw), np.float32) + (1. / detsig1**2) detiv[ie == 0] = 0. (Yo,Xo,Yi,Xi) = R if tim.dq is None: sat = None else: sat = ((tim.dq[Yi,Xi] & tim.dq_saturation_bits) > 0) return Yo, Xo, detim[Yi,Xi], detiv[Yi,Xi], sat
def _galdetmap(X): from scipy.ndimage.filters import gaussian_filter from legacypipe.survey import tim_get_resamp (tim, galsize, gaussian, targetwcs, H, W) = X R = tim_get_resamp(tim, targetwcs) if R is None: return None,None,None,None ie = tim.getInvvar() assert(tim.psf_sigma > 0) pixscale = tim.subwcs.pixel_scale() if gaussian: # convert galsize (in sigmas in arcsec) to pixels galsigma = galsize / pixscale sigma = np.hypot(tim.psf_sigma, galsigma) gnorm = 1./(2. * np.sqrt(np.pi) * sigma) detim = tim.getImage().copy() detim[ie == 0] = 0. detim = gaussian_filter(detim, sigma) / gnorm**2 else: galsigs = np.sqrt(ExpGalaxy.profile.var[:,0,0]) * galsize / pixscale galamps = ExpGalaxy.profile.amp #print('Galaxy sigma: %.2f, PSF sigma: %.2f' % (galsigma, tim.psf_sigma)) print('Galaxy amps', galamps, 'sigmas', galsigs) sz = 20 xx,yy = np.meshgrid(np.arange(-sz, sz+1), np.arange(-sz, sz+1)) rr = xx**2 + yy**2 normim = 0 detim = 0 img = tim.getImage().copy() img[ie == 0] = 0. for amp,sig in zip(galamps, galsigs): sig = np.hypot(tim.psf_sigma, sig) detim += amp * gaussian_filter(img, sig) normim += amp * 1./(2.*np.pi*sig**2) * np.exp(-0.5 * rr / sig**2) #print('Normimg:', normim.sum()) gnorm = np.sqrt(np.sum(normim**2)) print('Galnorm', gnorm, 'vs psfnorm', 1./(2. * np.sqrt(np.pi) * tim.psf_sigma), 'seeing', tim.psf_fwhm/pixscale) detim /= gnorm**2 detsig1 = tim.sig1 / gnorm subh,subw = tim.shape detiv = np.zeros((subh,subw), np.float32) + (1. / detsig1**2) detiv[ie == 0] = 0. (Yo,Xo,Yi,Xi) = R return Yo, Xo, detim[Yi,Xi], detiv[Yi,Xi]
def _detmap(X): from scipy.ndimage.filters import gaussian_filter from legacypipe.survey import tim_get_resamp (tim, targetwcs, apodize) = X R = tim_get_resamp(tim, targetwcs) if R is None: return None, None, None, None, None assert (tim.psf_sigma > 0) psfnorm = 1. / (2. * np.sqrt(np.pi) * tim.psf_sigma) ie = tim.getInvError() detim = tim.getImage().copy() # Zero out all masked pixels detim[ie == 0] = 0. tim.getSky().addTo(detim, scale=-1.) subh, subw = tim.shape detsig1 = tim.sig1 / psfnorm detiv = np.zeros((subh, subw), np.float32) + (1. / detsig1**2) detiv[ie == 0] = 0. (Yo, Xo, Yi, Xi) = R if tim.dq is None: sat = None else: sat = ((tim.dq[Yi, Xi] & tim.dq_saturation_bits) > 0) # Replace saturated pixels by the brightest (non-masked) pixel in the image if np.any(sat): I, = np.nonzero(sat) debug('Filling', len(I), 'saturated detmap pixels with max') detim[Yi[I], Xi[I]] = np.max(detim) # detection is based on S/N, so plug in values > 0 for iv detiv[Yi[I], Xi[I]] = 1. / detsig1**2 detim = gaussian_filter(detim, tim.psf_sigma) / psfnorm**2 detiv = gaussian_filter(detiv, tim.psf_sigma) if apodize: apodize = int(apodize) ramp = np.arctan(np.linspace(-np.pi, np.pi, apodize + 2)) ramp = (ramp - ramp.min()) / (ramp.max() - ramp.min()) # drop first and last (= 0 and 1) ramp = ramp[1:-1] detiv[:len(ramp), :] *= ramp[:, np.newaxis] detiv[:, :len(ramp)] *= ramp[np.newaxis, :] detiv[-len(ramp):, :] *= ramp[::-1][:, np.newaxis] detiv[:, -len(ramp):] *= ramp[::-1][np.newaxis, :] return Yo, Xo, detim[Yi, Xi], detiv[Yi, Xi], sat
def _detmap(X): from scipy.ndimage.filters import gaussian_filter from legacypipe.survey import tim_get_resamp (tim, targetwcs, H, W, apodize) = X R = tim_get_resamp(tim, targetwcs) if R is None: return None, None, None, None, None assert (tim.psf_sigma > 0) psfnorm = 1. / (2. * np.sqrt(np.pi) * tim.psf_sigma) ie = tim.getInvvar() detim = tim.getImage().copy() tim.getSky().addTo(detim, scale=-1.) detim = gaussian_filter(detim, tim.psf_sigma) / psfnorm**2 detsig1 = tim.sig1 / psfnorm subh, subw = tim.shape detiv = np.zeros((subh, subw), np.float32) + (1. / detsig1**2) detiv[ie == 0] = 0. detiv = gaussian_filter(detiv, tim.psf_sigma) if apodize: apodize = int(apodize) ramp = np.arctan(np.linspace(-np.pi, np.pi, apodize + 2)) ramp = (ramp - ramp.min()) / (ramp.max() - ramp.min()) # drop first and last (= 0 and 1) ramp = ramp[1:-1] detiv[:len(ramp), :] *= ramp[:, np.newaxis] detiv[:, :len(ramp)] *= ramp[np.newaxis, :] detiv[-len(ramp):, :] *= ramp[::-1][:, np.newaxis] detiv[:, -len(ramp):] *= ramp[::-1][np.newaxis, :] (Yo, Xo, Yi, Xi) = R if tim.dq is None: sat = None else: sat = ((tim.dq[Yi, Xi] & tim.dq_saturation_bits) > 0) return Yo, Xo, detim[Yi, Xi], detiv[Yi, Xi], sat
def quick_coadds(tims, bands, targetwcs, images=None, get_cow=False, get_n2=False, fill_holes=True): W = int(targetwcs.get_width()) H = int(targetwcs.get_height()) coimgs = [] cons = [] if get_n2: cons2 = [] if get_cow: # moo cowimgs = [] wimgs = [] for ib, band in enumerate(bands): coimg = np.zeros((H, W), np.float32) coimg2 = np.zeros((H, W), np.float32) con = np.zeros((H, W), np.uint8) con2 = np.zeros((H, W), np.uint8) if get_cow: cowimg = np.zeros((H, W), np.float32) wimg = np.zeros((H, W), np.float32) for itim, tim in enumerate(tims): if tim.band != band: continue R = tim_get_resamp(tim, targetwcs) if R is None: continue (Yo, Xo, Yi, Xi) = R nn = (tim.getInvError()[Yi, Xi] > 0) if images is None: coimg[Yo, Xo] += tim.getImage()[Yi, Xi] * nn coimg2[Yo, Xo] += tim.getImage()[Yi, Xi] else: coimg[Yo, Xo] += images[itim][Yi, Xi] * nn coimg2[Yo, Xo] += images[itim][Yi, Xi] con[Yo, Xo] += nn if get_cow: cowimg[Yo, Xo] += tim.getInvvar()[Yi, Xi] * tim.getImage()[Yi, Xi] wimg[Yo, Xo] += tim.getInvvar()[Yi, Xi] con2[Yo, Xo] += 1 coimg /= np.maximum(con, 1) if fill_holes: coimg[con == 0] = coimg2[con == 0] / np.maximum(1, con2[con == 0]) if get_cow: cowimg /= np.maximum(wimg, 1e-16) cowimg[wimg == 0] = coimg[wimg == 0] cowimgs.append(cowimg) wimgs.append(wimg) coimgs.append(coimg) cons.append(con) if get_n2: cons2.append(con2) rtn = [coimgs, cons] if get_cow: rtn.extend([cowimgs, wimgs]) if get_n2: rtn.append(cons2) return rtn
def quick_coadds(tims, bands, targetwcs, images=None, get_cow=False, get_n2=False, fill_holes=True, get_max=False, get_saturated=False, addnoise=False): W = int(targetwcs.get_width()) H = int(targetwcs.get_height()) coimgs = [] cons = [] if get_n2: cons2 = [] if get_cow: # moo cowimgs = [] wimgs = [] if get_max: maximgs = [] if get_saturated: satur = np.zeros((H,W), np.bool) if addnoise: noise = np.zeros((H,W), np.float32) for band in bands: coimg = np.zeros((H,W), np.float32) coimg2 = np.zeros((H,W), np.float32) con = np.zeros((H,W), np.int16) con2 = np.zeros((H,W), np.int16) if get_cow: cowimg = np.zeros((H,W), np.float32) wimg = np.zeros((H,W), np.float32) if get_max: maximg = np.zeros((H,W), np.float32) for itim,tim in enumerate(tims): if tim.band != band: continue R = tim_get_resamp(tim, targetwcs) if R is None: continue (Yo,Xo,Yi,Xi) = R nn = (tim.getInvError()[Yi,Xi] > 0) if images is None: coimg [Yo,Xo] += tim.getImage()[Yi,Xi] * nn coimg2[Yo,Xo] += tim.getImage()[Yi,Xi] if get_max: maximg[Yo,Xo] = np.maximum(maximg[Yo,Xo], tim.getImage()[Yi,Xi] * nn) else: coimg [Yo,Xo] += images[itim][Yi,Xi] * nn coimg2[Yo,Xo] += images[itim][Yi,Xi] if get_max: maximg[Yo,Xo] = np.maximum(maximg[Yo,Xo], images[itim][Yi,Xi] * nn) if addnoise: noise[:,:] = 0. noise[Yo[nn],Xo[nn]] = 1./(tim.getInvError()[Yi,Xi][nn]) coimg += noise * np.random.normal(size=noise.shape) con [Yo,Xo] += nn if get_cow: cowimg[Yo,Xo] += tim.getInvvar()[Yi,Xi] * tim.getImage()[Yi,Xi] wimg [Yo,Xo] += tim.getInvvar()[Yi,Xi] if get_saturated and tim.dq is not None: satur[Yo,Xo] |= ((tim.dq[Yi,Xi] & tim.dq_saturation_bits) > 0) con2 [Yo,Xo] += 1 coimg /= np.maximum(con,1) if fill_holes: coimg[con == 0] = coimg2[con == 0] / np.maximum(1, con2[con == 0]) if get_cow: cowimg /= np.maximum(wimg, 1e-16) cowimg[wimg == 0] = coimg[wimg == 0] cowimgs.append(cowimg) wimgs.append(wimg) if get_max: maximgs.append(maximg) coimgs.append(coimg) cons.append(con) if get_n2: cons2.append(con2) rtn = [coimgs,cons] if get_cow: rtn.extend([cowimgs, wimgs]) if get_n2: rtn.append(cons2) if get_max: rtn.append(maximgs) if get_saturated: rtn.append(satur) return rtn
def quick_coadds(tims, bands, targetwcs, images=None, get_cow=False, get_n2=False, fill_holes=True): W = targetwcs.get_width() H = targetwcs.get_height() coimgs = [] cons = [] if get_n2: cons2 = [] if get_cow: # moo cowimgs = [] wimgs = [] for ib,band in enumerate(bands): coimg = np.zeros((H,W), np.float32) coimg2 = np.zeros((H,W), np.float32) con = np.zeros((H,W), np.uint8) con2 = np.zeros((H,W), np.uint8) if get_cow: cowimg = np.zeros((H,W), np.float32) wimg = np.zeros((H,W), np.float32) for itim,tim in enumerate(tims): if tim.band != band: continue R = tim_get_resamp(tim, targetwcs) if R is None: continue (Yo,Xo,Yi,Xi) = R nn = (tim.getInvError()[Yi,Xi] > 0) if images is None: coimg [Yo,Xo] += tim.getImage()[Yi,Xi] * nn coimg2[Yo,Xo] += tim.getImage()[Yi,Xi] else: coimg [Yo,Xo] += images[itim][Yi,Xi] * nn coimg2[Yo,Xo] += images[itim][Yi,Xi] con [Yo,Xo] += nn if get_cow: cowimg[Yo,Xo] += tim.getInvvar()[Yi,Xi] * tim.getImage()[Yi,Xi] wimg [Yo,Xo] += tim.getInvvar()[Yi,Xi] con2 [Yo,Xo] += 1 coimg /= np.maximum(con,1) if fill_holes: coimg[con == 0] = coimg2[con == 0] / np.maximum(1, con2[con == 0]) if get_cow: cowimg /= np.maximum(wimg, 1e-16) cowimg[wimg == 0] = coimg[wimg == 0] cowimgs.append(cowimg) wimgs.append(wimg) coimgs.append(coimg) cons.append(con) if get_n2: cons2.append(con2) rtn = [coimgs,cons] if get_cow: rtn.extend([cowimgs, wimgs]) if get_n2: rtn.append(cons2) return rtn