| 1 |
Interpolating values from a HEALPix map |
| 2 |
======================================= |
| 3 |
|
| 4 |
Main methods |
| 5 |
------------ |
| 6 |
|
| 7 |
While all the functionality we have seen so far in the remainder of the |
| 8 |
documentation is concerned with the geometry of the HEALPix pixellization, the |
| 9 |
main purpose of HEALPix is to actually tabulate values in each pixel to |
| 10 |
represent a physical quantity over a sphere (e.g. flux over the celestial |
| 11 |
sphere). We will refer to this as a HEALPix map. |
| 12 |
|
| 13 |
These maps are stored using a 1-dimensional vector with as many elements as |
| 14 |
pixels in the HEALPix pixellization, and either in the 'ring' or 'nested' |
| 15 |
order. |
| 16 |
|
| 17 |
If you are interested in finding the value in a HEALPix map at a given |
| 18 |
longitude/latitude on the sphere, there are two main options: |
| 19 |
|
| 20 |
* Convert the longitude/latitude to the HEALPix pixel that the position |
| 21 |
falls inside (e.g. ``index``) using :meth:`~astropy_healpix.HEALPix.lonlat_to_healpix` |
| 22 |
or :meth:`~astropy_healpix.HEALPix.skycoord_to_healpix`, and extract the value |
| 23 |
of the array of map values at that index (e.g. ``values[index]``). This is |
| 24 |
essentially equivalent to a nearest-neighbour interpolation. |
| 25 |
|
| 26 |
* Convert the longitude/latitude to the HEALPix pixel that the position |
| 27 |
falls inside then find the other neighboring pixels and carry out a bilinear |
| 28 |
interpolation. This is trickier to do by hand, and we therefore provide the |
| 29 |
methods :meth:`~astropy_healpix.HEALPix.interpolate_bilinear_lonlat` and |
| 30 |
:meth:`~astropy_healpix.HEALPix.interpolate_bilinear_skycoord` methods to |
| 31 |
faciliate this. If you are not already familiar with how to access HEALPix |
| 32 |
data from FITS files, we have provided a `Full example`_ in the following |
| 33 |
section. |
| 34 |
|
| 35 |
Full example |
| 36 |
------------ |
| 37 |
|
| 38 |
To illustrate this, we use an example map from the `WMAP mission |
| 39 |
<https://lambda.gsfc.nasa.gov/product/map/dr3/maps_band_r9_i_5yr_get.cfm>`__, |
| 40 |
specifically the map **K Band Map for the Full Five Years**. We start off by |
| 41 |
downloading and opening this map with Astropy:: |
| 42 |
|
| 43 |
>>> from astropy.io import fits |
| 44 |
>>> hdulist = fits.open('https://lambda.gsfc.nasa.gov/data/map/dr3/skymaps/5yr//wmap_band_imap_r9_5yr_K_v3.fits') # doctest: +REMOTE_DATA |
| 45 |
>>> hdulist.info() # doctest: +REMOTE_DATA |
| 46 |
Filename: ... |
| 47 |
No. Name Ver Type Cards Dimensions Format |
| 48 |
0 PRIMARY 1 PrimaryHDU 19 () |
| 49 |
1 Archive Map Table 1 BinTableHDU 20 3145728R x 2C [E, E] |
| 50 |
|
| 51 |
Since HEALPix maps are stored in tabular form, the data is contained in HDU 1 |
| 52 |
(primary HDUs cannot contain tabular data). |
| 53 |
|
| 54 |
Let's now take a look at the header:: |
| 55 |
|
| 56 |
>>> hdulist[1].header # doctest: +REMOTE_DATA |
| 57 |
XTENSION= 'BINTABLE' /binary table extension |
| 58 |
BITPIX = 8 /8-bit bytes |
| 59 |
NAXIS = 2 /2-dimensional binary table |
| 60 |
NAXIS1 = 8 /width of table in bytes |
| 61 |
NAXIS2 = 3145728 /number of rows in table |
| 62 |
PCOUNT = 0 /size of special data area |
| 63 |
GCOUNT = 1 /one data group (required keyword) |
| 64 |
TFIELDS = 2 /number of fields in each row |
| 65 |
TTYPE1 = 'TEMPERATURE ' /label for field 1 |
| 66 |
TFORM1 = 'E ' /data format of field: 4-byte REAL |
| 67 |
TUNIT1 = 'mK ' /physical unit of field 1 |
| 68 |
TTYPE2 = 'N_OBS ' /label for field 2 |
| 69 |
TFORM2 = 'E ' /data format of field: 4-byte REAL |
| 70 |
TUNIT2 = 'counts ' /physical unit of field 2 |
| 71 |
EXTNAME = 'Archive Map Table' /name of this binary table extension |
| 72 |
PIXTYPE = 'HEALPIX ' /Pixel algorigthm |
| 73 |
ORDERING= 'NESTED ' /Ordering scheme |
| 74 |
NSIDE = 512 /Resolution parameter |
| 75 |
FIRSTPIX= 0 /First pixel (0 based) |
| 76 |
LASTPIX = 3145727 /Last pixel (0 based) |
| 77 |
|
| 78 |
Of particular interest to us are the ``NSIDE`` and ``ORDERING`` keywords:: |
| 79 |
|
| 80 |
>>> hdulist[1].header['NSIDE'] # doctest: +REMOTE_DATA |
| 81 |
512 |
| 82 |
>>> hdulist[1].header['ORDERING'] # doctest: +REMOTE_DATA |
| 83 |
'NESTED' |
| 84 |
|
| 85 |
The data itself can be accessed using:: |
| 86 |
|
| 87 |
>>> hdulist[1].data['TEMPERATURE'] # doctest: +REMOTE_DATA |
| 88 |
array([ 16.285 , 16.802532, 15.320368, ..., 15.07802 , 15.362292, |
| 89 |
15.232816], dtype=float32) |
| 90 |
|
| 91 |
The last piece of information we need is that the map is in Galactic coordinates, |
| 92 |
which is unfortunately not encoded in the header but can be found `here |
| 93 |
<https://lambda.gsfc.nasa.gov/product/map/dr3/maps_band_iqus_info.cfm>`__. |
| 94 |
|
| 95 |
We can now instantiate a :class:`~astropy_healpix.HEALPix` object:: |
| 96 |
|
| 97 |
>>> from astropy_healpix import HEALPix |
| 98 |
>>> from astropy.coordinates import Galactic |
| 99 |
>>> nside = hdulist[1].header['NSIDE'] # doctest: +REMOTE_DATA |
| 100 |
>>> order = hdulist[1].header['ORDERING'] # doctest: +REMOTE_DATA |
| 101 |
>>> hp = HEALPix(nside=nside, order=order, frame=Galactic()) # doctest: +REMOTE_DATA |
| 102 |
|
| 103 |
and we can now use |
| 104 |
:meth:`~astropy_healpix.HEALPix.interpolate_bilinear_skycoord` to interpolate |
| 105 |
the temperature at a given position on the sky:: |
| 106 |
|
| 107 |
>>> from astropy.coordinates import SkyCoord |
| 108 |
>>> coord = SkyCoord('00h42m44.3503s +41d16m08.634s', frame='icrs') |
| 109 |
>>> temperature = hdulist[1].data['temperature'] # doctest: +REMOTE_DATA |
| 110 |
>>> hp.interpolate_bilinear_skycoord(coord, temperature) # doctest: +FLOAT_CMP +REMOTE_DATA |
| 111 |
0.40692833017361985 |
| 112 |
|
| 113 |
Here is a full example that uses this to make a map of a section of the sky: |
| 114 |
|
| 115 |
.. plot:: |
| 116 |
:include-source: |
| 117 |
|
| 118 |
# Get the data |
| 119 |
from astropy.io import fits |
| 120 |
hdulist = fits.open('https://lambda.gsfc.nasa.gov/data/map/dr3/skymaps/5yr//wmap_band_imap_r9_5yr_K_v3.fits') |
| 121 |
|
| 122 |
# Set up the HEALPix projection |
| 123 |
from astropy_healpix import HEALPix |
| 124 |
from astropy.coordinates import Galactic |
| 125 |
nside = hdulist[1].header['NSIDE'] |
| 126 |
order = hdulist[1].header['ORDERING'] |
| 127 |
hp = HEALPix(nside=nside, order=order, frame=Galactic()) |
| 128 |
|
| 129 |
# Sample a 300x200 grid in RA/Dec |
| 130 |
from astropy import units as u |
| 131 |
ra = np.linspace(-15., 15., 300) * u.deg |
| 132 |
dec = np.linspace(-10., 10., 200) * u.deg |
| 133 |
ra_grid, dec_grid = np.meshgrid(ra, dec) |
| 134 |
|
| 135 |
# Set up Astropy coordinate objects |
| 136 |
from astropy.coordinates import SkyCoord |
| 137 |
coords = SkyCoord(ra_grid.ravel(), dec_grid.ravel(), frame='icrs') |
| 138 |
|
| 139 |
# Interpolate values |
| 140 |
temperature = hdulist[1].data['temperature'] |
| 141 |
tmap = hp.interpolate_bilinear_skycoord(coords, temperature) |
| 142 |
tmap = tmap.reshape((200, 300)) |
| 143 |
|
| 144 |
# Make a plot of the interpolated temperatures |
| 145 |
plt.figure(figsize=(9, 5)) |
| 146 |
im = plt.imshow(tmap, extent=[-1, 1, -10, 10], cmap=plt.cm.RdYlBu, origin='lower', aspect='auto') |
| 147 |
plt.colorbar(im) |
| 148 |
plt.xlabel('Right ascension (ICRS)') |
| 149 |
plt.ylabel('Declination (ICRS)') |
| 150 |
plt.show() |
| 151 |
|
| 152 |
In practice, for the common case of reprojecting a HEALPix map to a regular |
| 153 |
gridded image, you can use the `reproject |
| 154 |
<http://reproject.readthedocs.io/en/stable/>`_ package which provides high-level |
| 155 |
reprojection functions that use **healpix** behind the scenes. |