| 1 |
Coordinate conversions |
| 2 |
====================== |
| 3 |
|
| 4 |
Converting between pixel indices and spherical coordinates |
| 5 |
---------------------------------------------------------- |
| 6 |
|
| 7 |
As described in :doc:`getting_started`, coordinates in a HEALPix pixellization |
| 8 |
can follow either the 'ring' or 'nested' convention. Let's start by setting up |
| 9 |
an example pixellization:: |
| 10 |
|
| 11 |
>>> from astropy_healpix import HEALPix |
| 12 |
>>> hp = HEALPix(nside=16, order='nested') |
| 13 |
|
| 14 |
The :meth:`~astropy_healpix.HEALPix.healpix_to_lonlat` method can be used |
| 15 |
to convert HEALPix indices to :class:`~astropy.coordinates.Longitude` and |
| 16 |
:class:`~astropy.coordinates.Latitude` objects:: |
| 17 |
|
| 18 |
>>> lon, lat = hp.healpix_to_lonlat([1, 442, 2200]) |
| 19 |
>>> lon # doctest: +FLOAT_CMP |
| 20 |
<Longitude [ 0.83448555, 1.63624617, 0.4712389 ] rad> |
| 21 |
>>> lat # doctest: +FLOAT_CMP |
| 22 |
<Latitude [ 0.08343009, 0.94842784,-0.78529135] rad> |
| 23 |
|
| 24 |
The :class:`~astropy.coordinates.Longitude` and |
| 25 |
:class:`~astropy.coordinates.Latitude` objects are fully-fledged |
| 26 |
:class:`~astropy.units.Quantity` objects and also include shortcuts to get |
| 27 |
the values in various units:: |
| 28 |
|
| 29 |
>>> lon.hourangle # doctest: +FLOAT_CMP |
| 30 |
array([ 3.1875, 6.25 , 1.8 ]) |
| 31 |
>>> lat.degree # doctest: +FLOAT_CMP |
| 32 |
array([ 4.78019185, 54.3409123 , -44.99388015]) |
| 33 |
|
| 34 |
Conversely, given longitudes and latitudes as :class:`~astropy.units.Quantity` |
| 35 |
objects, it is possible to recover HEALPix pixel indices:: |
| 36 |
|
| 37 |
>>> from astropy import units as u |
| 38 |
>>> print(hp.lonlat_to_healpix([1, 3, 4] * u.deg, [5, 6, 9] * u.deg)) |
| 39 |
[1217 1217 1222] |
| 40 |
|
| 41 |
In these examples, what is being converted is the position of the center of each |
| 42 |
pixel. In fact, the :meth:`~astropy_healpix.HEALPix.lonlat_to_healpix` method can also |
| 43 |
take or give the fractional position inside each HEALPix pixel, e.g.:: |
| 44 |
|
| 45 |
>>> index, dx, dy = hp.lonlat_to_healpix([1, 3, 4] * u.deg, [5, 6, 9] * u.deg, |
| 46 |
... return_offsets=True) |
| 47 |
>>> print(index) |
| 48 |
[1217 1217 1222] |
| 49 |
>>> dx # doctest: +FLOAT_CMP |
| 50 |
array([ 0.22364669, 0.78767489, 0.58832469]) |
| 51 |
>>> dy # doctest: +FLOAT_CMP |
| 52 |
array([ 0.86809114, 0.72100823, 0.16610247]) |
| 53 |
|
| 54 |
and the :meth:`~astropy_healpix.HEALPix.healpix_to_lonlat` method can take offset |
| 55 |
positions - for example we can use this to find the position of the corners of |
| 56 |
a given pixel:: |
| 57 |
|
| 58 |
>>> dx = [0., 1., 1., 0.] |
| 59 |
>>> dy = [0., 0., 1., 1.] |
| 60 |
>>> lon, lat = hp.healpix_to_lonlat([133, 133, 133, 133], dx=dx, dy=dy) |
| 61 |
>>> lon # doctest: +FLOAT_CMP |
| 62 |
<Longitude [ 0.53996124, 0.58904862, 0.53996124, 0.49087385] rad> |
| 63 |
>>> lat # doctest: +FLOAT_CMP |
| 64 |
<Latitude [ 0.47611906, 0.52359878, 0.57241857, 0.52359878] rad> |
| 65 |
|
| 66 |
.. _celestial: |
| 67 |
|
| 68 |
Celestial coordinates |
| 69 |
--------------------- |
| 70 |
|
| 71 |
For cases where the HEALPix pixellization is of the celestial sphere, a |
| 72 |
``frame`` argument can be passed to :class:`~astropy_healpix.HEALPix`. This |
| 73 |
argument should specify the celestial frame (using an `astropy.coordinates |
| 74 |
<http://docs.astropy.org/en/stable/coordinates/index.html>`_ frame) in which the |
| 75 |
HEALPix pixellization is defined:: |
| 76 |
|
| 77 |
>>> from astropy_healpix import HEALPix |
| 78 |
>>> from astropy.coordinates import Galactic |
| 79 |
>>> hp = HEALPix(nside=16, order='nested', frame=Galactic()) |
| 80 |
|
| 81 |
Each method defined in :class:`~astropy_healpix.HEALPix` and ending in |
| 82 |
``lonlat`` has an equivalent method ending in ``skycoord`` which can be used if |
| 83 |
the frame is set. For example, to convert from HEALPix indices to celestial |
| 84 |
coordinates, you can use the |
| 85 |
:meth:`~astropy_healpix.HEALPix.healpix_to_skycoord` method:: |
| 86 |
|
| 87 |
>>> hp.healpix_to_skycoord([144, 231]) # doctest: +FLOAT_CMP |
| 88 |
<SkyCoord (Galactic): (l, b) in deg |
| 89 |
[( 33.75 , 32.7971683 ), ( 32.14285714, 69.42254649)]> |
| 90 |
|
| 91 |
and to convert from celestial coordinates to HEALPix indices you can use the |
| 92 |
:meth:`~astropy_healpix.HEALPix.skycoord_to_healpix` method, e.g:: |
| 93 |
|
| 94 |
>>> from astropy.coordinates import SkyCoord |
| 95 |
>>> coord = SkyCoord('00h42m44.3503s +41d16m08.634s') |
| 96 |
>>> hp.skycoord_to_healpix(coord) |
| 97 |
2537 |
| 98 |
|
| 99 |
Converting between ring and nested conventions |
| 100 |
---------------------------------------------- |
| 101 |
|
| 102 |
The :class:`~astropy_healpix.HEALPix` class has methods that can be used to |
| 103 |
convert HEALPix pixel indices between the ring and nested convention. These are |
| 104 |
:meth:`~astropy_healpix.HEALPix.nested_to_ring`:: |
| 105 |
|
| 106 |
>>> print(hp.nested_to_ring([30])) |
| 107 |
[873] |
| 108 |
|
| 109 |
and :meth:`~astropy_healpix.HEALPix.ring_to_nested`:: |
| 110 |
|
| 111 |
>>> print(hp.ring_to_nested([1, 2, 3])) |
| 112 |
[ 511 767 1023] |