def fftFromLiteMap(liteMap,applySlepianTaper = False,nresForSlepian=3.0): """ @brief Creates an fft2D object out of a liteMap @param liteMap The map whose fft is being taken @param applySlepianTaper If True applies the lowest order taper (to minimize edge-leakage) @param nresForSlepian If above is True, specifies the resolution of the taeper to use. """ ft = fft2D() ft.Nx = liteMap.Nx ft.Ny = liteMap.Ny flTrace.issue("flipper.fftTools",1, "Taking FFT of map with (Ny,Nx)= (%f,%f)"%(ft.Ny,ft.Nx)) ft.pixScaleX = liteMap.pixScaleX ft.pixScaleY = liteMap.pixScaleY lx = 2*np.pi * fftfreq( ft.Nx, d = ft.pixScaleX ) ly = 2*np.pi * fftfreq( ft.Ny, d = ft.pixScaleY ) ix = np.mod(np.arange(ft.Nx*ft.Ny),ft.Nx) iy = np.arange(ft.Nx*ft.Ny)/ft.Nx modLMap = np.zeros([ft.Ny,ft.Nx]) modLMap[iy,ix] = np.sqrt(lx[ix]**2 + ly[iy]**2) ft.modLMap = modLMap ft.lx = lx ft.ly = ly ft.ix = ix ft.iy = iy ft.thetaMap = np.zeros([ft.Ny,ft.Nx]) ft.thetaMap[iy[:],ix[:]] = np.arctan2(ly[iy[:]],lx[ix[:]]) ft.thetaMap *=180./np.pi map = liteMap.data.copy() #map = map0.copy() #map[:,:] =map0[::-1,:] taper = map.copy()*0.0 + 1.0 if (applySlepianTaper) : try: path_to_taper = os.path.join(__FLIPPER_DIR__, "tapers", 'taper_Ny%d_Nx%d_Nres%3.1f'%(ft.Ny,ft.Nx,nresForSlepian)) f = open(path_to_taper) taper = pickle.load(f) f.close() except: taper = slepianTaper00(ft.Nx,ft.Ny,nresForSlepian) path_to_taper = os.path.join(__FLIPPER_DIR__, "tapers", 'taper_Ny%d_Nx%d_Nres%3.1f'%(ft.Ny,ft.Nx,nresForSlepian)) f = open(path_to_taper, mode="w") pickle.dump(taper,f) f.close() ft.kMap = fftfast.fft(map*taper,axes=[-2,-1]) del map, modLMap, lx, ly return ft
def apply(self,ltMap): discDiffMap = discDifference(self.radius,ltMap,\ discKern1=self.discKern1,\ discKern3=self.discKern3) flTrace.issue("flipper.prewhitener",5,"Applying disc differencing with R=%f arcmin"%self.radius) discDiffMap.data[:] += self.addBackFraction*ltMap.data[:] if self.smoothingFWHM>0.: flTrace.issue("flipper.prewhitener",5,"Smoothing with FWHM: %f arcmin"%self.smoothingFWHM) discDiffMap = discDiffMap.convolveWithGaussian(self.smoothingFWHM) return discDiffMap
def testIsotropyInAnnuli(self,binningFile,cutByMask=False): """ @brief tests the isotropy in each annulus specfied by the binningFIle (see _testIsotropyInAnnulus) @param binningFile An ascii file with columns binLower, binUpper, binCenter (nBins on the first line) @param cutByMask If set to True, multiply the annulus by the k-space mask before adding pixels (see createKspaceMask). @return binLowerBound,binUpperBound,BinCenter,BinnedValue,BinWeight """ binLower,binUpper,binCenter = readBinningFile(binningFile) nBins = binLower.size flTrace.issue("fftTools",0, "nBins= %d"%nBins) binMean = np.zeros(nBins) binWeight = np.zeros(nBins) for i in xrange(nBins): (binMean[i],binWeight[i]) = self._testIsotropyInAnnulus(binLower[i],binUpper[i],cutByMask=cutByMask) return binLower,binUpper,binCenter,binMean,binWeight
def liteMapFromDataAndWCS(data,wcs): """ @brief Given a np array: data and a astLib.astWCS instance: wcs creates a liteMap """ ltmap = liteMap() ltmap.data = data.copy() [ltmap.Ny,ltmap.Nx] = ltmap.data.shape ltmap.wcs = wcs.copy() ltmap.header = ltmap.wcs.header #[ltmap.x0,ltmap.x1,ltmap.y0,ltmap.y1] = wcs.getImageMinMaxWCSCoords() ltmap.x0,ltmap.y0 = wcs.pix2wcs(0,0) ltmap.x1,ltmap.y1 = wcs.pix2wcs(ltmap.Nx-1,ltmap.Ny-1) if ltmap.x0 > ltmap.x1: ltmap.pixScaleX = np.abs(ltmap.x1-ltmap.x0)/ltmap.Nx*np.pi/180.\ *np.cos(np.pi/180.*0.5*(ltmap.y0+ltmap.y1)) else: ltmap.pixScaleX = np.abs((360.-ltmap.x1)+ltmap.x0)/ltmap.Nx*np.pi/180.\ *np.cos(np.pi/180.*0.5*(ltmap.y0+ltmap.y1)) ltmap.pixScaleY = np.abs(ltmap.y1-ltmap.y0)/ltmap.Ny*np.pi/180. #print 0.5*(ltmap.y0+ltmap.y1) ltmap.area = ltmap.Nx*ltmap.Ny*ltmap.pixScaleX*ltmap.pixScaleY*(180./np.pi)**2 #print np.cos(np.pi/180.*0.5*(ltmap.y0+ltmap.y1)) flTrace.issue('flipper.liteMap',1,'Reading file %s'%file) flTrace.issue("flipper.liteMap",1, "Map dimensions (Ny,Nx) %d %d"%\ (ltmap.Ny,ltmap.Nx)) flTrace.issue("flipper.liteMap",1, "pixel scales Y, X (degrees) %f %f"%\ (ltmap.pixScaleY*180./np.pi,ltmap.pixScaleX*180./np.pi)) return ltmap
def liteMapFromFits(file,extension=0): """ @brief Reads in a FITS file and creates a liteMap object out of it. @param extension specify the FITS HDU where the map image is stored """ ltmap = liteMap() hdulist = pyfits.open(file) header = hdulist[extension].header flTrace.issue('flipper.liteMap',3,"Map header \n %s"%header) ltmap.data = hdulist[extension].data.copy() [ltmap.Ny,ltmap.Nx] = ltmap.data.shape wcs = astLib.astWCS.WCS(file,extensionName = extension) ltmap.wcs = wcs.copy() ltmap.header = ltmap.wcs.header ltmap.x0,ltmap.y0 = wcs.pix2wcs(0,0) ltmap.x1,ltmap.y1 = wcs.pix2wcs(ltmap.Nx-1,ltmap.Ny-1) #[ltmap.x0,ltmap.x1,ltmap.y0,ltmap.y1] = wcs.getImageMinMaxWCSCoords() if ltmap.x0 > ltmap.x1: ltmap.pixScaleX = np.abs(ltmap.x1-ltmap.x0)/ltmap.Nx*np.pi/180.\ *np.cos(np.pi/180.*0.5*(ltmap.y0+ltmap.y1)) else: ltmap.pixScaleX = np.abs((360.-ltmap.x1)+ltmap.x0)/ltmap.Nx*np.pi/180.\ *np.cos(np.pi/180.*0.5*(ltmap.y0+ltmap.y1)) ltmap.pixScaleY = np.abs(ltmap.y1-ltmap.y0)/ltmap.Ny*np.pi/180. #print 0.5*(ltmap.y0+ltmap.y1) ltmap.area = ltmap.Nx*ltmap.Ny*ltmap.pixScaleX*ltmap.pixScaleY*(180./np.pi)**2 #print np.cos(np.pi/180.*0.5*(ltmap.y0+ltmap.y1)) flTrace.issue('flipper.liteMap',1,'Reading file %s'%file) flTrace.issue("flipper.liteMap",1, "Map dimensions (Ny,Nx) %d %d"%\ (ltmap.Ny,ltmap.Nx)) flTrace.issue("flipper.liteMap",1, "pixel scales Y, X (degrees) %f %f"%\ (ltmap.pixScaleY*180./np.pi,ltmap.pixScaleX*180./np.pi)) return ltmap
def liteMapFromDataAndWCS(data, wcs): """ @brief Given a numpy array: data and a astLib.astWCS instance: wcs creates a liteMap """ ltmap = liteMap() ltmap.data = data.copy() [ltmap.Ny, ltmap.Nx] = ltmap.data.shape ltmap.wcs = wcs.copy() ltmap.header = ltmap.wcs.header #[ltmap.x0,ltmap.x1,ltmap.y0,ltmap.y1] = wcs.getImageMinMaxWCSCoords() ltmap.x0, ltmap.y0 = wcs.pix2wcs(0, 0) ltmap.x1, ltmap.y1 = wcs.pix2wcs(ltmap.Nx - 1, ltmap.Ny - 1) if ltmap.x0 > ltmap.x1: ltmap.pixScaleX = numpy.abs(ltmap.x1-ltmap.x0)/ltmap.Nx*numpy.pi/180.\ *numpy.cos(numpy.pi/180.*0.5*(ltmap.y0+ltmap.y1)) else: ltmap.pixScaleX = numpy.abs((360.-ltmap.x1)+ltmap.x0)/ltmap.Nx*numpy.pi/180.\ *numpy.cos(numpy.pi/180.*0.5*(ltmap.y0+ltmap.y1)) ltmap.pixScaleY = numpy.abs(ltmap.y1 - ltmap.y0) / ltmap.Ny * numpy.pi / 180. #print 0.5*(ltmap.y0+ltmap.y1) ltmap.area = ltmap.Nx * ltmap.Ny * ltmap.pixScaleX * ltmap.pixScaleY * ( 180. / numpy.pi)**2 #print numpy.cos(numpy.pi/180.*0.5*(ltmap.y0+ltmap.y1)) flTrace.issue('flipper.liteMap', 1, 'Reading file %s' % file) flTrace.issue("flipper.liteMap",1, "Map dimensions (Ny,Nx) %d %d"%\ (ltmap.Ny,ltmap.Nx)) flTrace.issue("flipper.liteMap",1, "pixel scales Y, X (degrees) %f %f"%\ (ltmap.pixScaleY*180./numpy.pi,ltmap.pixScaleX*180./numpy.pi)) return ltmap
def fftFromLiteMap(liteMap, applySlepianTaper=False, nresForSlepian=3.0, fftType=None): """ @brief Creates an fft2D object out of a liteMap @param liteMap The map whose fft is being taken @param applySlepianTaper If True applies the lowest order taper (to minimize edge-leakage) @param nresForSlepian If above is True, specifies the resolution of the taeper to use. """ ft = fft2D() ft.Nx = liteMap.Nx ft.Ny = liteMap.Ny flTrace.issue("flipper.fftTools", 1, "Taking FFT of map with (Ny,Nx)= (%f,%f)" % (ft.Ny, ft.Nx)) ft.pixScaleX = liteMap.pixScaleX ft.pixScaleY = liteMap.pixScaleY lx = 2 * numpy.pi * fftfreq(ft.Nx, d=ft.pixScaleX) ly = 2 * numpy.pi * fftfreq(ft.Ny, d=ft.pixScaleY) ix = numpy.mod(numpy.arange(ft.Nx * ft.Ny), ft.Nx) iy = numpy.arange(ft.Nx * ft.Ny) / ft.Nx modLMap = numpy.zeros([ft.Ny, ft.Nx]) modLMap[iy, ix] = numpy.sqrt(lx[ix]**2 + ly[iy]**2) ft.modLMap = modLMap ft.lx = lx ft.ly = ly ft.ix = ix ft.iy = iy ft.thetaMap = numpy.zeros([ft.Ny, ft.Nx]) ft.thetaMap[iy[:], ix[:]] = numpy.arctan2(ly[iy[:]], lx[ix[:]]) ft.thetaMap *= 180. / numpy.pi map = liteMap.data.copy() #map = map0.copy() #map[:,:] =map0[::-1,:] taper = map.copy() * 0.0 + 1.0 if (applySlepianTaper): try: f = open(taperDir + os.path.sep + 'taper_Ny%d_Nx%d_Nres%3.1f' % (ft.Ny, ft.Nx, nresForSlepian)) taper = pickle.load(f) f.close() except: taper = slepianTaper00(ft.Nx, ft.Ny, nresForSlepian) f = open(taperDir + os.path.sep + 'taper_Ny%d_Nx%d_Nres%3.1f' % (ft.Ny, ft.Nx, nresForSlepian), mode="w") pickle.dump(taper, f) f.close() if fftType == 'fftw3': ft.kMap = fft.fft(map * taper, axes=[-2, -1]) else: ft.kMap = fft2(map * taper) del map, modLMap, lx, ly return ft
def addLiteMapsWithSpectralWeighting( liteMap1, liteMap2, kMask1Params = None, kMask2Params = None, signalMap = None ): """ @brief add two maps, weighting in Fourier space Maps must be the same size. @param kMask1Params mask params for liteMap1 (see fftTools.power2D.createKspaceMask) @param kMask2Params mask params for liteMap2 (see fftTools.power2D.createKspaceMask) @param signalMap liteMap with best estimate of signal to use when estimating noise weighting @return new map """ #Get fourier weights flTrace.issue("liteMap", 0, "Computing Weights") #np1 = fftTools.noisePowerFromLiteMaps(liteMap1, liteMap2, applySlepianTaper = False) #np2 = fftTools.noisePowerFromLiteMaps(liteMap2, liteMap1, applySlepianTaper = False) data1 = copy.copy(liteMap1.data) data2 = copy.copy(liteMap2.data) if signalMap != None: liteMap1.data[:] = (liteMap1.data - signalMap.data)[:] liteMap2.data[:] = (liteMap2.data - signalMap.data)[:] np1 = fftTools.powerFromLiteMap(liteMap1)#, applySlepianTaper = True) np2 = fftTools.powerFromLiteMap(liteMap2)#), applySlepianTaper = True) print "testing", liteMap1.data == data1 liteMap1.data[:] = data1[:] liteMap2.data[:] = data2[:] n1 = np1.powerMap n2 = np2.powerMap # n1[np.where( n1<n1.max()*.002)] = n1.max()*.001 # n2[np.where( n2<n2.max()*.002)] = n2.max()*.001 w1 = 1/n1 w2 = 1/n2 m1 = np.median(w1) m2 = np.median(w2) w1[np.where(abs(w1)>4*m1)]=4*m1 w2[np.where(abs(w2)>4*m2)]=4*m2 #w1[:] = 1. #w2[:] = 1. #pylab.hist(w1.ravel()) #pylab.savefig("hist1.png") #pylab.clf() yrange = [4,5000] np1.powerMap = w1 #np1.plot(pngFile="w1.png", log=True, zoomUptoL=8000, yrange = yrange) np2.powerMap = w2 #np2.plot(pngFile="w2.png", log=True, zoomUptoL=8000, yrange = yrange) if kMask1Params != None: np1.createKspaceMask(**kMask1Params) w1 *= np1.kMask if kMask2Params != None: np2.createKspaceMask(**kMask2Params) w2 *= np2.kMask pylab.clf() invW = 1.0/(w1+w2) invW[np.where(np.isnan(invW))] = 0. invW[np.where(np.isinf(invW))] = 0. flTrace.issue("liteMap", 3, "NaNs in inverse weight: %s" % str(np.where(np.isnan(invW)))) flTrace.issue("liteMap", 3, "Infs in inverse weight: %s" % str(np.where(np.isinf(invW)))) flTrace.issue("liteMap", 2, "Adding Maps") f1 = fftTools.fftFromLiteMap( liteMap1, applySlepianTaper = False ) f2 = fftTools.fftFromLiteMap( liteMap2, applySlepianTaper = False ) kTot = (f1.kMap*w1 + f2.kMap*w2)*invW flTrace.issue("liteMap", 3, "NaNs in filtered transform: %s" % str(np.where(np.isnan(kTot)))) f1.kMap = kTot finalMap = liteMap1.copy() finalMap.data[:] = 0. finalMap.data = f1.mapFromFFT() return finalMap
def loadDataFromHealpixMap(self, hpm, interpolate = False, hpCoords = "J2000"): """ @brief copy data from a Healpix map (from healpy), return a lite map @param hpm healpy map @param interpolate use interpolation when copying @param hpCoords coordinates of hpm (e.g., "J2000"(RA, Dec) or "GALACTIC") Assumes that liteMap is in J2000 RA Dec. The Healpix map must contain the liteMap. """ inds = np.indices([self.Nx, self.Ny]) x = inds[0].ravel() y = inds[1].ravel() skyLinear = np.array(self.pixToSky(x,y)) ph = skyLinear[:,0] th = skyLinear[:,1] thOut = [] phOut = [] if hpCoords != "J2000": for i in xrange(len(th)): crd = astCoords.convertCoords("J2000", hpCoords, ph[i], th[i], 0.) phOut.append(crd[0]) thOut.append(crd[1]) thOut = np.array(thOut) phOut = np.array(phOut) else: thOut = th phOut = ph flTrace.issue("flipper.liteMap", 3, "theta (min, max): %f, %f" % (th.min(), th.max())) flTrace.issue("flipper.liteMap", 3, "phi (min, max): %f, %f" % (ph.min(), ph.max())) flTrace.issue("flipper.liteMap", 3, "phiOut (min, max): (%f, %f) " % ( phOut.min(), phOut.max() )) flTrace.issue("flipper.liteMap", 3, "thetaOut (min, max): (%f, %f) " % ( thOut.min(), thOut.max() )) phOut *= np.pi/180 thOut = 90. - thOut #polar angle is 0 at north pole thOut *= np.pi/180 flTrace.issue("flipper.liteMap", 3, "phiOut rad (min, max): (%f, %f) " % ( phOut.min(), phOut.max() )) flTrace.issue("flipper.liteMap", 3, "thetaOut rad (min, max): (%f, %f) " % ( thOut.min(), thOut.max() )) if interpolate: self.data[y,x] = healpy.get_interp_val(hpm, thOut, phOut) else: ind = healpy.ang2pix( healpy.get_nside(hpm), thOut, phOut ) flTrace.issue("flipper.liteMap", 3, "healpix indices (min,max): %d, %d" % (ind.min(), ind.max())) self.data[:] = 0. self.data[[y,x]]=hpm[ind]