lsst.pipe.tasks gf10b05e212+10497e73ae
associationUtils.py
Go to the documentation of this file.
1# This file is part of pipe_tasks.
2
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21#
22
23"""Utilities for interfacing with hpgeom. Originally implemented in
24http://github.com/LSSTDESC/dia_pipe and then translated to hpgeom.
25"""
26
27import hpgeom as hpg
28import numpy as np
29
30
31def toIndex(nside, ra, dec):
32 """Return healpix index given ra, dec in degrees
33
34 Parameters
35 ----------
36 nside : `int`
37 Power of 2 nside healpix resolution.
38 ra : `float`
39 RA in degrees.
40 dec : `float`
41 Declination in degrees
42
43 Returns
44 -------
45 index : `int`
46 Unique healpix pixel ID containing point RA, DEC at resolution nside.
47 """
48 return hpg.angle_to_pixel(nside, ra, dec, nest=False)
49
50
51def toRaDec(nside, index):
52 """Convert from healpix index to ra,dec in degrees
53
54 Parameters
55 ----------
56 nside : `int`
57 Resolution of healpixel "grid".
58 index : `int`
59 Index of the healpix pixel we want to find the location of.
60
61 Returns
62 -------
63 pos : `numpy.ndarray`, (2,)
64 RA and DEC of healpix pixel location in degrees.
65 """
66 ra, dec = hpg.pixel_to_angle(nside, index, nest=False)
67 return np.dstack((ra, dec))[0]
68
69
70def eq2xyz(ra, dec):
71 """Convert from equatorial ra,dec in degrees to x,y,z on unit sphere.
72
73 Parameters
74 ----------
75 ra : `float`
76 RA in degrees.
77 dec : `float`
78 Declination in degrees
79
80 Returns
81 -------
82 xyz : `numpy.ndarray`, (3,)
83 Float xyz positions on the unit sphere.
84 """
85 phi = np.deg2rad(ra)
86 theta = np.pi/2 - np.deg2rad(dec)
87 sintheta = np.sin(theta)
88 x = sintheta*np.cos(phi)
89 y = sintheta*np.sin(phi)
90 z = np.cos(theta)
91 return np.array([x, y, z])
92
93
94def eq2xyzVec(ra, dec):
95 """Convert equatorial ra,dec in degrees to x,y,z on the unit sphere
96 parameters
97
98 Vectorized version of ``eq2xyz``
99
100 Parameters
101 ----------
102 ra : array_like, (N,)
103 Array of RA in degrees.
104 dec : array_like, (N,)
105 Declination in degrees
106
107 Returns
108 -------
109 vec : `numpy.ndarray`, (N,3)
110 Array of unitsphere 3-vectors.
111 """
112 ra = np.array(ra, dtype='f8', ndmin=1, copy=False)
113 dec = np.array(dec, dtype='f8', ndmin=1, copy=False)
114 if ra.size != dec.size:
115 raise ValueError("ra,dec not same size: %s,%s" % (ra.size, dec.size))
116
117 vec = eq2xyz(ra, dec)
118
119 return vec
120
121
122def convert_spherical(ra, dec):
123 """Convert from ra,dec to spherical coordinates.
124
125 Used in query_disc.
126
127 Parameters
128 ----------
129 ra : `float`
130 RA in radians.
131 dec : `float`
132 Declination in radians
133 """
134 return np.dstack([np.cos(dec*np.pi/180)*np.cos(ra*np.pi/180),
135 np.cos(dec*np.pi/180)*np.sin(ra*np.pi/180),
136 np.sin(dec*np.pi/180)])[0]
137
138
140 """Convert from and a array ra,dec to spherical coordinates.
141
142 Used in query_disc
143
144 Parameters
145 ----------
146 array : `numpy.ndarray`, (N, 2)
147 (N, 2) Array of RA, DEC values.
148
149 Returns
150 -------
151 vecs : `numpy.ndarray`, (N, 3)
152 Vectors on the unit sphere
153 """
154 ra = array[:, 0]
155 dec = array[:, 1]
156 return convert_spherical(ra, dec)
157
158
159def query_disc(nside, ra, dec, max_rad, min_rad=0):
160 """Get the list of healpix indices within max_rad, min_rad given in radians
161 around ra,dec given in degrees
162
163 Parameters
164 ----------
165 nside : `int`
166 Resolution of the healpixels to search/return.
167 ra : `float`
168 RA in degrees.
169 dec : `float`
170 Declination in degrees
171 max_rad : `float`
172 Max distance in radians to search nearby healpixels.
173 min_rad : `float`, optional
174 Minimum distance in radians to search healpixels. Default = 0.
175 """
176 ra = np.atleast_1d(ra)
177 dec = np.atleast_1d(dec)
178
179 max_rad_deg = np.rad2deg(max_rad)
180
181 pixels = np.unique(
182 [hpg.query_circle(nside, a, b, max_rad_deg, nest=False)
183 for (a, b) in zip(ra, dec)])
184
185 if min_rad > 0 and len(pixels) > 0:
186 vec0 = convert_spherical(ra, dec)
187 min_rad2 = min_rad**2
188 vecs = convert_spherical_array(toRaDec(nside, pixels))
189 dsq = np.sum((vecs - vec0)**2, axis=1)
190 match = dsq > min_rad2
191 pixels = pixels[match]
192
193 return pixels
def query_disc(nside, ra, dec, max_rad, min_rad=0)