def sample_lonlat(self, n): """ Sample 2D distribution of points in lon, lat """ # From http://en.wikipedia.org/wiki/Ellipse#General_parametric_form # However, Martin et al. (2009) use PA theta "from North to East" # Definition of phi (position angle) is offset by pi/4 # Definition of t (eccentric anamoly) remains the same (x,y-frame usual) # In the end, everything is trouble because we use glon, glat... radius = self.sample_radius(n) a = radius b = self.jacobian * radius t = 2. * np.pi * numpy.random.rand(n) cost, sint = np.cos(t), np.sin(t) phi = np.pi / 2. - np.deg2rad(self.theta) cosphi, sinphi = np.cos(phi), np.sin(phi) x = a * cost * cosphi - b * sint * sinphi y = a * cost * sinphi + b * sint * cosphi if self.projector is None: logger.debug("Creating AITOFF projector for sampling") projector = Projector(self.lon, self.lat, 'ait') else: projector = self.projector lon, lat = projector.imageToSphere(x, y) return lon, lat
def findObjects(pixels, values, nside, zvalues, rev, good): """ Characterize labelled candidates in a multi-dimensional HEALPix map. Parameters: values : (Sparse) HEALPix array of data values nside : HEALPix dimensionality pixels : Pixel values associated to (sparse) HEALPix array zvalues : Values of the z-dimension (usually distance modulus) rev : Reverse indices for pixels in each "island" good : Array containg labels for each "island" Returns: objs : numpy.recarray of object characteristics """ ngood = len(good) objs = numpy.recarray((ngood, ), dtype=[ ('LABEL', 'i4'), ('NPIX', 'i4'), ('VAL_MAX', 'f4'), ('IDX_MAX', 'i4'), ('ZIDX_MAX', 'i4'), ('PIX_MAX', 'i4'), ('X_MAX', 'f4'), ('Y_MAX', 'f4'), ('Z_MAX', 'f4'), ('X_CENT', 'f4'), ('Y_CENT', 'f4'), ('Z_CENT', 'f4'), ('X_BARY', 'f4'), ('Y_BARY', 'f4'), ('Z_BARY', 'f4'), ('CUT', 'i2'), ]) objs['CUT'][:] = 0 shape = values.shape ncol = shape[1] for i in range(0, ngood): logger.debug("i=%i", i) # This code could use some cleanup... indices = rev[rev[good[i]]:rev[good[i] + 1]] npix = len(indices) idx = indices // ncol # This is the spatial index zidx = indices % ncol # This is the distance index pix = pixels[idx] # This is the healpix pixel xval, yval = pix2ang(nside, pix) zval = zvalues[zidx] objs[i]['LABEL'] = good[i] objs[i]['NPIX'] = npix logger.debug("LABEL=%i" % objs[i]['LABEL']) logger.debug("NPIX=%i" % objs[i]['NPIX']) island = values[idx, zidx] idxmax = island.argmax() xval_max, yval_max, zval_max = xval[idxmax], yval[idxmax], zval[ idxmax] objs[i]['VAL_MAX'] = island[idxmax] objs[i]['IDX_MAX'] = idx[idxmax] objs[i]['ZIDX_MAX'] = zidx[idxmax] objs[i]['PIX_MAX'] = pix[idxmax] objs[i]['X_MAX'] = xval_max objs[i]['Y_MAX'] = yval_max objs[i]['Z_MAX'] = zval_max proj = Projector(xval_max, yval_max) xpix, ypix = proj.sphereToImage(xval, yval) # Projected centroid x_cent, y_cent, zval_cent = numpy.average([xpix, ypix, zval], axis=1) xval_cent, yval_cent = proj.imageToSphere(x_cent, y_cent) objs[i]['X_CENT'] = xval_cent objs[i]['Y_CENT'] = yval_cent objs[i]['Z_CENT'] = zval_cent # Projected barycenter weights = [island, island, island] x_bary, y_bary, zval_bary = numpy.average([xpix, ypix, zval], weights=weights, axis=1) xval_bary, yval_bary = proj.imageToSphere(x_bary, y_bary) objs[i]['X_BARY'] = xval_bary objs[i]['Y_BARY'] = yval_bary objs[i]['Z_BARY'] = zval_bary return objs
def projector(self): if self.proj is None or self.proj.lower() == 'none': return None else: return Projector(self.lon, self.lat, self.proj)
def findObjects(pixels, values, nside, zvalues, rev, good): """ Characterize labelled candidates in a multi-dimensional HEALPix map. Parameters: values : (Sparse) HEALPix array of data values nside : HEALPix dimensionality pixels : Pixel values associated to (sparse) HEALPix array zvalues : Values of the z-dimension (usually distance modulus) rev : Reverse indices for pixels in each "island" good : Array containg labels for each "island" Returns: objs : numpy.recarray of object characteristics """ ngood = len(good) objs = numpy.recarray((ngood,), dtype=[('LABEL','i4'), ('NPIX','i4'), ('VAL_MAX','f4'), ('IDX_MAX','i4'), ('ZIDX_MAX','i4'), ('PIX_MAX','i4'), ('X_MAX','f4'), ('Y_MAX','f4'), ('Z_MAX','f4'), ('X_CENT','f4'), ('Y_CENT','f4'), ('Z_CENT','f4'), ('X_BARY','f4'), ('Y_BARY','f4'), ('Z_BARY','f4'), ('CUT','i2'),]) objs['CUT'][:] = 0 shape = values.shape ncol = shape[1] for i in range(0,ngood): logger.debug("i=%i",i) # This code could use some cleanup... indices=rev[rev[good[i]]:rev[good[i]+1]] npix = len(indices) idx = indices // ncol # This is the spatial index zidx = indices % ncol # This is the distance index pix = pixels[idx] # This is the healpix pixel xval,yval = pix2ang(nside, pix) zval = zvalues[zidx] objs[i]['LABEL'] = good[i] objs[i]['NPIX'] = npix logger.debug("LABEL=%i"%objs[i]['LABEL']) logger.debug("NPIX=%i"%objs[i]['NPIX']) island = values[idx,zidx] idxmax = island.argmax() xval_max,yval_max,zval_max = xval[idxmax],yval[idxmax],zval[idxmax] objs[i]['VAL_MAX'] = island[idxmax] objs[i]['IDX_MAX'] = idx[idxmax] objs[i]['ZIDX_MAX'] = zidx[idxmax] objs[i]['PIX_MAX'] = pix[idxmax] objs[i]['X_MAX'] = xval_max objs[i]['Y_MAX'] = yval_max objs[i]['Z_MAX'] = zval_max proj = Projector(xval_max,yval_max) xpix,ypix = proj.sphereToImage(xval,yval) # Projected centroid x_cent,y_cent,zval_cent = numpy.average([xpix,ypix,zval],axis=1) xval_cent, yval_cent = proj.imageToSphere(x_cent,y_cent) objs[i]['X_CENT'] = xval_cent objs[i]['Y_CENT'] = yval_cent objs[i]['Z_CENT'] = zval_cent # Projected barycenter weights=[island,island,island] x_bary,y_bary,zval_bary = numpy.average([xpix,ypix,zval],weights=weights,axis=1) xval_bary,yval_bary = proj.imageToSphere(x_bary, y_bary) objs[i]['X_BARY'] = xval_bary objs[i]['Y_BARY'] = yval_bary objs[i]['Z_BARY'] = zval_bary return objs
def projector(self): """Projector used to transform to local sky coordinates.""" if self.proj is None or self.proj.lower() == 'none': return None else: return Projector(self.lon, self.lat, self.proj)