def bin_array_2d_super_smooth(x, y, nxbin, xlow, xhigh, nybin, ylow, yhigh, n_per_smooth_fwhm=100, completeness=None): nx = len(x) ny = len(y) if nx != ny: print 'Error: len(x) != len(y)' return xstep = float(xhigh-xlow)/nxbin ystep = float(yhigh-ylow)/nybin fwhm_list = (2.0 * N.sqrt(n_per_smooth_fwhm/(N.pi*N.arange(1,101)))) sigma_list = fwhm_list/2.3548 rmax = 4 * sigma_list[-1] size = int(round(rmax)) * 2 + 1 gc = (size-1)/2 g_list = [] for sigma in sigma_list: g = N.asarray(gaussian2d((size, size), 1.0, gc, gc, sigma, sigma)) g_list.append(g) x_bin, y_bin, d_bin = bin_array_2d(x, y, nxbin, xlow, xhigh, nybin, ylow, yhigh, completeness) N.putmask(d_bin, d_bin < 1, 1) d_max = d_bin.max() gindex_array = (100*d_bin / d_max).astype(N.int) N.putmask(gindex_array, gindex_array < 0, 0) N.putmask(gindex_array, gindex_array > 99, 99) gindex_array_sorted = N.sort(N.ravel(gindex_array)) gindex_min = gindex_array_sorted[0] gindex_max = gindex_array_sorted[-1] print 'Smoothing FWHM varies between', print '%3.2f and %3.2f bins'%(fwhm_list[gindex_min], fwhm_list[gindex_max]) d_bin_smooth = N.zeros((nybin+2*gc, nxbin+2*gc), N.float) percent_prev = 0 print '%:', for k in range(nx): percent = int((100.0*k)/nx) if percent/10 != percent_prev/10: print percent, percent_prev = percent sys.stdout.flush() jbin_index = int((x[k] - xlow)/xstep) ibin_index = int((y[k] - ylow)/ystep) if 0 <= jbin_index < nxbin and 0 <= ibin_index < nybin: gindex = gindex_array[ibin_index, jbin_index] d_bin_smooth[ibin_index:ibin_index+2*gc+1, jbin_index:jbin_index+2*gc+1] += g_list[gindex] print d_bin_smooth = N.pi*(fwhm_list[gindex_min]/2.0)**2 * d_bin_smooth[gc:-gc, gc:-gc] return x_bin, y_bin, d_bin_smooth, fwhm_list[gindex_min], fwhm_list[gindex_max]
def bin_array_2d_smooth(x, y, nxbin, xlow, xhigh, nybin, ylow, yhigh, smooth_fwhm=10, completeness=None): nx = len(x) ny = len(y) if nx != ny: print 'Error: len(x) != len(y)' return xstep = float(xhigh-xlow)/nxbin ystep = float(yhigh-ylow)/nybin fwhm = smooth_fwhm sigma = fwhm/2.3548 rmax = 4 * sigma size = int(round(rmax)) * 2 + 1 gc = (size-1)/2 g = N.asarray(gaussian2d((size, size), 1.0, gc, gc, sigma, sigma)) x_bin = N.arange(nxbin) * xstep + xlow + xstep/2.0 y_bin = N.arange(nybin) * ystep + ylow + ystep/2.0 d_bin = N.zeros((nybin+2*gc, nxbin+2*gc), N.float) percent_prev = 0 print '%:', for k in range(nx): percent = int((100.0*k)/nx) if percent/10 != percent_prev/10: print percent, percent_prev = percent sys.stdout.flush() jbin_index = int((x[k] - xlow)/xstep) ibin_index = int((y[k] - ylow)/ystep) if completeness is None: c = 1 else: c = completeness[k] if 0 <= jbin_index < nxbin and 0 <= ibin_index < nybin: d_bin[ibin_index:ibin_index+2*gc+1, jbin_index:jbin_index+2*gc+1] += g/c print d_bin = N.pi*(smooth_fwhm/2.0)**2 * d_bin[gc:-gc, gc:-gc] return x_bin, y_bin, d_bin
import gaussian # SPB's own function # produce a symmetrical 2d gaussian centred in stamp # with sigma = 1 pixel, hence FWHM = 2.355 pixels. d = gaussian.gaussian2d((25,25), 1.0, 12.0, 12.0, 1.0, 1.0) pyfits.writeto('psf.fits', d)