def from_cone(ra, dec, error, dateobs): localization_name = "%.5f_%.5f_%.5f" % (ra, dec, error) center = SkyCoord(ra * u.deg, dec * u.deg) radius = error * u.deg # Determine resolution such that there are at least # 16 pixels across the error radius. hpx = HEALPix(pixel_resolution_to_nside(radius / 16, round='up'), 'nested', frame=ICRS()) # Find all pixels in the 4-sigma error circle. ipix = hpx.cone_search_skycoord(center, 4 * radius) # Convert to multi-resolution pixel indices and sort. uniq = moc.nest2uniq(nside_to_level(hpx.nside), ipix) i = np.argsort(uniq) ipix = ipix[i] uniq = uniq[i] # Evaluate Gaussian. distance = hpx.healpix_to_skycoord(ipix).separation(center) probdensity = np.exp(-0.5 * np.square(distance / radius).to_value( u.dimensionless_unscaled)) probdensity /= probdensity.sum() * hpx.pixel_area.to_value(u.steradian) models.db.session.merge( models.Localization( localization_name=localization_name, dateobs=dateobs, uniq=uniq.tolist(), probdensity=probdensity.tolist())) models.db.session.commit() return localization_name
def main(args=None): args = parser().parse_args(args) import logging import warnings import astropy_healpix as ah from astropy.io import fits from ..io import read_sky_map, write_sky_map from ..bayestar import rasterize log = logging.getLogger() if args.nside is None: order = None else: order = ah.nside_to_level(args.nside) log.info('reading FITS file %s', args.input.name) hdus = fits.open(args.input) ordering = hdus[1].header['ORDERING'] expected_ordering = 'NUNIQ' if ordering != expected_ordering: msg = 'Expected the FITS file {} to have ordering {}, but it is {}' warnings.warn(msg.format(args.input.name, expected_ordering, ordering)) log.debug('converting original FITS file to Astropy table') table = read_sky_map(hdus, moc=True) log.debug('flattening HEALPix tree') table = rasterize(table, order=order) log.info('writing FITS file %s', args.output.name) write_sky_map(args.output.name, table, nest=True) log.debug('done')
def _reconstruct_nested_breadthfirst(m, extra): m = np.asarray(m) max_npix = len(m) max_nside = ah.npix_to_nside(max_npix) max_order = ah.nside_to_level(max_nside) seen = np.zeros(max_npix, dtype=bool) for order in range(max_order + 1): nside = ah.level_to_nside(order) npix = ah.nside_to_npix(nside) skip = max_npix // npix if skip > 1: b = m.reshape(-1, skip) a = b[:, 0].reshape(-1, 1) b = b[:, 1:] aseen = seen.reshape(-1, skip) eq = ((a == b) | ((a != a) & (b != b))).all(1) & (~aseen).all(1) else: eq = ~seen for ipix in np.flatnonzero(eq): ipix0 = ipix * skip ipix1 = (ipix + 1) * skip seen[ipix0:ipix1] = True if extra: yield _HEALPixTreeVisitExtra(nside, max_nside, ipix, ipix0, ipix1, m[ipix0]) else: yield _HEALPixTreeVisit(nside, ipix)
def from_cone(ra, dec, error): localization_name = "%.5f_%.5f_%.5f" % (ra, dec, error) center = SkyCoord(ra * u.deg, dec * u.deg) radius = error * u.deg # Determine resolution such that there are at least # 16 pixels across the error radius. hpx = HEALPix(pixel_resolution_to_nside(radius / 16, round='up'), 'nested', frame=ICRS()) # Find all pixels in the 4-sigma error circle. ipix = hpx.cone_search_skycoord(center, 4 * radius) # Convert to multi-resolution pixel indices and sort. uniq = ligo.skymap.moc.nest2uniq(nside_to_level(hpx.nside), ipix.astype(np.int64)) i = np.argsort(uniq) ipix = ipix[i] uniq = uniq[i] # Evaluate Gaussian. distance = hpx.healpix_to_skycoord(ipix).separation(center) probdensity = np.exp( -0.5 * np.square(distance / radius).to_value(u.dimensionless_unscaled)) probdensity /= probdensity.sum() * hpx.pixel_area.to_value(u.steradian) skymap = table.Table( [np.asarray(uniq), np.asarray(probdensity)], names=['UNIQ', 'PROBDENSITY']) return skymap, uniq
def adaptive_healpix_histogram(theta, phi, max_samples_per_pixel, nside=-1, max_nside=-1, nest=False): """Adaptively histogram the posterior samples represented by the (theta, phi) points using a recursively subdivided HEALPix tree. Nodes are subdivided until each leaf contains no more than max_samples_per_pixel samples. Finally, the tree is flattened to a fixed-resolution HEALPix image with a resolution appropriate for the depth of the tree. If nside is specified, the result is resampled to another desired HEALPix resolution. """ # Calculate pixel index of every sample, at the maximum 64-bit resolution. # # At this resolution, each pixel is only 0.2 mas across; we'll use the # 64-bit pixel indices as a proxy for the true sample coordinates so that # we don't have to do any trigonometry (aside from the initial hp.ang2pix # call). ipix = hp.ang2pix(HEALPIX_MACHINE_NSIDE, theta, phi, nest=True) # Build tree structure. if nside == -1 and max_nside == -1: max_order = HEALPIX_MACHINE_ORDER elif nside == -1: max_order = ah.nside_to_level(max_nside) elif max_nside == -1: max_order = ah.nside_to_level(nside) else: max_order = ah.nside_to_level(min(nside, max_nside)) tree = HEALPixTree(ipix, max_samples_per_pixel, max_order) # Compute a flattened bitmap representation of the tree. p = tree.flat_bitmap # If requested, resample the tree to the output resolution. if nside != -1: p = hp.ud_grade(p, nside, order_in='NESTED', order_out='NESTED') # Normalize. p /= np.sum(p) if not nest: p = hp.reorder(p, n2r=True) # Done! return p