| 1 |
# Licensed under a 3-clause BSD style license - see LICENSE.rst |
| 2 |
""" |
| 3 |
This submodule provides a healpy-compatible interface. |
| 4 |
""" |
| 5 |
|
| 6 |
import numpy as np |
| 7 |
from astropy import units as u |
| 8 |
from astropy.coordinates.representation import CartesianRepresentation, UnitSphericalRepresentation |
| 9 |
|
| 10 |
from .core import (nside_to_pixel_resolution, nside_to_pixel_area, |
| 11 |
nside_to_npix, npix_to_nside, nested_to_ring, ring_to_nested, |
| 12 |
level_to_nside, lonlat_to_healpix, healpix_to_lonlat, |
| 13 |
xyz_to_healpix, healpix_to_xyz, |
| 14 |
boundaries_lonlat, bilinear_interpolation_weights, |
| 15 |
interpolate_bilinear_lonlat, _NUMPY_COPY_IF_NEEDED) |
| 16 |
|
| 17 |
RAD2DEG = 180 / np.pi |
| 18 |
PI_2 = np.pi / 2 |
| 19 |
|
| 20 |
__all__ = ['nside2resol', |
| 21 |
'nside2pixarea', |
| 22 |
'nside2npix', |
| 23 |
'npix2nside', |
| 24 |
'pix2ang', |
| 25 |
'ang2pix', |
| 26 |
'pix2vec', |
| 27 |
'vec2pix', |
| 28 |
'order2nside', |
| 29 |
'nest2ring', |
| 30 |
'ring2nest', |
| 31 |
'boundaries', |
| 32 |
'vec2ang', |
| 33 |
'ang2vec', |
| 34 |
'get_interp_weights', |
| 35 |
'get_interp_val'] |
| 36 |
|
| 37 |
|
| 38 |
def _lonlat_to_healpy(lon, lat, lonlat=False): |
| 39 |
# We use in-place operations below to avoid making temporary arrays - this |
| 40 |
# is safe because the lon/lat arrays returned from healpix_to_lonlat are |
| 41 |
# new and not used elsewhere. |
| 42 |
if lonlat: |
| 43 |
return lon.to(u.deg).value, lat.to(u.deg).value |
| 44 |
else: |
| 45 |
lat, lon = lat.to(u.rad).value, lon.to(u.rad).value |
| 46 |
if np.isscalar(lon): |
| 47 |
return PI_2 - lat, lon |
| 48 |
else: |
| 49 |
lat = np.subtract(PI_2, lat, out=lat) |
| 50 |
return lat, lon |
| 51 |
|
| 52 |
|
| 53 |
def _healpy_to_lonlat(theta, phi, lonlat=False): |
| 54 |
# Unlike in _lonlat_to_healpy, we don't use in-place operations since we |
| 55 |
# don't want to modify theta and phi since the user may be using them |
| 56 |
# elsewhere. |
| 57 |
if lonlat: |
| 58 |
lon = np.asarray(theta) / RAD2DEG |
| 59 |
lat = np.asarray(phi) / RAD2DEG |
| 60 |
else: |
| 61 |
lat = PI_2 - np.asarray(theta) |
| 62 |
lon = np.asarray(phi) |
| 63 |
return u.Quantity(lon, u.rad, copy=_NUMPY_COPY_IF_NEEDED), u.Quantity( |
| 64 |
lat, u.rad, copy=_NUMPY_COPY_IF_NEEDED) |
| 65 |
|
| 66 |
|
| 67 |
def nside2resol(nside, arcmin=False): |
| 68 |
"""Drop-in replacement for healpy `~healpy.pixelfunc.nside2resol`.""" |
| 69 |
resolution = nside_to_pixel_resolution(nside) |
| 70 |
if arcmin: |
| 71 |
return resolution.to(u.arcmin).value |
| 72 |
else: |
| 73 |
return resolution.to(u.rad).value |
| 74 |
|
| 75 |
|
| 76 |
def nside2pixarea(nside, degrees=False): |
| 77 |
"""Drop-in replacement for healpy `~healpy.pixelfunc.nside2pixarea`.""" |
| 78 |
area = nside_to_pixel_area(nside) |
| 79 |
if degrees: |
| 80 |
return area.to(u.deg ** 2).value |
| 81 |
else: |
| 82 |
return area.to(u.sr).value |
| 83 |
|
| 84 |
|
| 85 |
def nside2npix(nside): |
| 86 |
"""Drop-in replacement for healpy `~healpy.pixelfunc.nside2npix`.""" |
| 87 |
return nside_to_npix(nside) |
| 88 |
|
| 89 |
|
| 90 |
def npix2nside(npix): |
| 91 |
"""Drop-in replacement for healpy `~healpy.pixelfunc.npix2nside`.""" |
| 92 |
return npix_to_nside(npix) |
| 93 |
|
| 94 |
|
| 95 |
def order2nside(order): |
| 96 |
"""Drop-in replacement for healpy `~healpy.pixelfunc.order2nside`.""" |
| 97 |
return level_to_nside(order) |
| 98 |
|
| 99 |
|
| 100 |
def pix2ang(nside, ipix, nest=False, lonlat=False): |
| 101 |
"""Drop-in replacement for healpy `~healpy.pixelfunc.pix2ang`.""" |
| 102 |
lon, lat = healpix_to_lonlat(ipix, nside, order='nested' if nest else 'ring') |
| 103 |
return _lonlat_to_healpy(lon, lat, lonlat=lonlat) |
| 104 |
|
| 105 |
|
| 106 |
def ang2pix(nside, theta, phi, nest=False, lonlat=False): |
| 107 |
"""Drop-in replacement for healpy `~healpy.pixelfunc.ang2pix`.""" |
| 108 |
lon, lat = _healpy_to_lonlat(theta, phi, lonlat=lonlat) |
| 109 |
return lonlat_to_healpix(lon, lat, nside, order='nested' if nest else 'ring') |
| 110 |
|
| 111 |
|
| 112 |
def pix2vec(nside, ipix, nest=False): |
| 113 |
"""Drop-in replacement for healpy `~healpy.pixelfunc.pix2vec`.""" |
| 114 |
return healpix_to_xyz(ipix, nside, order='nested' if nest else 'ring') |
| 115 |
|
| 116 |
|
| 117 |
def vec2pix(nside, x, y, z, nest=False): |
| 118 |
"""Drop-in replacement for healpy `~healpy.pixelfunc.vec2pix`.""" |
| 119 |
return xyz_to_healpix(x, y, z, nside, order='nested' if nest else 'ring') |
| 120 |
|
| 121 |
|
| 122 |
def nest2ring(nside, ipix): |
| 123 |
"""Drop-in replacement for healpy `~healpy.pixelfunc.nest2ring`.""" |
| 124 |
ipix = np.atleast_1d(ipix).astype(np.int64, copy=_NUMPY_COPY_IF_NEEDED) |
| 125 |
return nested_to_ring(ipix, nside) |
| 126 |
|
| 127 |
|
| 128 |
def ring2nest(nside, ipix): |
| 129 |
"""Drop-in replacement for healpy `~healpy.pixelfunc.ring2nest`.""" |
| 130 |
ipix = np.atleast_1d(ipix).astype(np.int64, copy=_NUMPY_COPY_IF_NEEDED) |
| 131 |
return ring_to_nested(ipix, nside) |
| 132 |
|
| 133 |
|
| 134 |
def boundaries(nside, pix, step=1, nest=False): |
| 135 |
"""Drop-in replacement for healpy `~healpy.boundaries`.""" |
| 136 |
pix = np.asarray(pix) |
| 137 |
if pix.ndim > 1: |
| 138 |
# For consistency with healpy we only support scalars or 1D arrays |
| 139 |
raise ValueError("Array has to be one dimensional") |
| 140 |
lon, lat = boundaries_lonlat(pix, step, nside, order='nested' if nest else 'ring') |
| 141 |
rep_sph = UnitSphericalRepresentation(lon, lat) |
| 142 |
rep_car = rep_sph.to_cartesian().xyz.value.swapaxes(0, 1) |
| 143 |
if rep_car.shape[0] == 1: |
| 144 |
return rep_car[0] |
| 145 |
else: |
| 146 |
return rep_car |
| 147 |
|
| 148 |
|
| 149 |
def vec2ang(vectors, lonlat=False): |
| 150 |
"""Drop-in replacement for healpy `~healpy.pixelfunc.vec2ang`.""" |
| 151 |
x, y, z = vectors.transpose() |
| 152 |
rep_car = CartesianRepresentation(x, y, z) |
| 153 |
rep_sph = rep_car.represent_as(UnitSphericalRepresentation) |
| 154 |
return _lonlat_to_healpy(rep_sph.lon.ravel(), rep_sph.lat.ravel(), lonlat=lonlat) |
| 155 |
|
| 156 |
|
| 157 |
def ang2vec(theta, phi, lonlat=False): |
| 158 |
"""Drop-in replacement for healpy `~healpy.pixelfunc.ang2vec`.""" |
| 159 |
lon, lat = _healpy_to_lonlat(theta, phi, lonlat=lonlat) |
| 160 |
rep_sph = UnitSphericalRepresentation(lon, lat) |
| 161 |
rep_car = rep_sph.represent_as(CartesianRepresentation) |
| 162 |
return rep_car.xyz.value |
| 163 |
|
| 164 |
|
| 165 |
def get_interp_weights(nside, theta, phi=None, nest=False, lonlat=False): |
| 166 |
""" |
| 167 |
Drop-in replacement for healpy `~healpy.pixelfunc.get_interp_weights`. |
| 168 |
|
| 169 |
Although note that the order of the weights and pixels may differ. |
| 170 |
""" |
| 171 |
# if phi is not given, theta is interpreted as pixel number |
| 172 |
if phi is None: |
| 173 |
theta, phi = pix2ang(nside, ipix=theta, nest=nest) |
| 174 |
|
| 175 |
lon, lat = _healpy_to_lonlat(theta, phi, lonlat=lonlat) |
| 176 |
return bilinear_interpolation_weights(lon, lat, nside, order='nested' if nest else 'ring') |
| 177 |
|
| 178 |
|
| 179 |
def get_interp_val(m, theta, phi, nest=False, lonlat=False): |
| 180 |
""" |
| 181 |
Drop-in replacement for healpy `~healpy.pixelfunc.get_interp_val`. |
| 182 |
""" |
| 183 |
lon, lat = _healpy_to_lonlat(theta, phi, lonlat=lonlat) |
| 184 |
return interpolate_bilinear_lonlat(lon, lat, m, order='nested' if nest else 'ring') |