lsst.pipe.tasks  21.0.0-100-g362f0c00+4647f1f7f0
associationUtils.py
Go to the documentation of this file.
1 import healpy as hp
2 import numpy as np
3 
4 """Utilities for interfacing with healpy. Originally implemented in
5 http://github.com/LSSTDESC/dia_pipe
6 """
7 
8 # Will update docs and the like in DM-30673
9 
10 
11 def toIndex(nside, ra, dec):
12  """Return healpix index given ra,dec in degrees"""
13  return hp.pixelfunc.ang2pix(nside, np.radians(-dec+90.), np.radians(ra))
14 
15 
16 def toRaDec(nside, index):
17  """Convert from healpix index to ra,dec in degrees"""
18  vec = hp.pix2ang(nside, index)
19  dec = np.rad2deg(-vec[0])+90
20  ra = np.rad2deg(vec[1])
21  return np.dstack((ra, dec))[0]
22 
23 
24 def eq2xyz(ra, dec):
25  """Convert from equatorial ra,dec in degrees to x,y,z on unit sphere"""
26  phi = np.deg2rad(ra)
27  theta = np.pi/2 - np.deg2rad(dec)
28  sintheta = np.sin(theta)
29  x = sintheta * np.cos(phi)
30  y = sintheta * np.sin(phi)
31  z = np.cos(theta)
32  return np.array([x, y, z])
33 
34 
35 def eq2vec(ra, dec):
36  """Convert equatorial ra,dec in degrees to x,y,z on the unit sphere parameters"""
37  ra = np.array(ra, dtype='f8', ndmin=1, copy=False)
38  dec = np.array(dec, dtype='f8', ndmin=1, copy=False)
39  if ra.size != dec.size:
40  raise ValueError("ra,dec not same size: %s,%s" % (ra.size, dec.size))
41 
42  vec = eq2xyz(ra, dec)
43 
44  return vec
45 
46 
47 def convert_spherical(ra, dec):
48  """Convert from ra,dec to spherical"""
49 
50  return np.dstack([np.cos(dec*np.pi/180.)*np.cos(ra*np.pi/180.),
51  np.cos(dec*np.pi/180.)*np.sin(ra*np.pi/180.),
52  np.sin(dec*np.pi/180.)])[0]
53 
54 
56  """Convert from ra,dec to spherical from array"""
57  ra = array[:, 0]
58  dec = array[:, 1]
59  return convert_spherical(ra, dec)
60 
61 
62 def query_disc(nside, ra, dec, max_rad, min_rad=0):
63  """
64  Get the list of healpix indices within max_rad,min_rad given in radians
65  around ra,dec given in degrees
66  """
67  if np.isscalar(ra):
68  ra = np.array([ra])
69  dec = np.array([dec])
70 
71  pixels = np.unique([hp.query_disc(nside, eq2vec(a, b), max_rad) for (a, b) in zip(ra, dec)])
72 
73  if min_rad > 0 and len(pixels) > 0:
74  vec0 = convert_spherical(ra, dec)
75  min_rad2 = min_rad**2
76  vecs = convert_spherical_array(toRaDec(nside, pixels))
77  dsq = np.sum((vecs-vec0)**2, axis=1)
78  match = dsq > min_rad2
79  pixels = pixels[match]
80 
81  return pixels
def query_disc(nside, ra, dec, max_rad, min_rad=0)