Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2This file contains coordinate transformation methods and utilities for converting an ra,dec coordinate set to 

3cartesian coordinates and to grid id using a spatial tree. 

4""" 

5from __future__ import division 

6 

7import numpy as np 

8from scipy.spatial import cKDTree as kdTree 

9 

10from lsst.sims.utils.CoordinateTransformations import _xyz_from_ra_dec 

11 

12__all__ = ['_buildTree'] 

13 

14 

15def _buildTree(ra, dec, leafsize=100, scale=None): 

16 """ 

17 Build KD tree on simDataRA/Dec and set radius (via setRad) for matching. 

18 

19 Parameters 

20 ---------- 

21 ra, dec : float (or arrays) 

22 RA and Dec values (in radians). 

23 leafsize : int (100) 

24 The number of Ra/Dec pointings in each leaf node. 

25 scale : float (None) 

26 If set, the values are scaled up, rounded, and converted to integers. Useful for 

27 forcing a set precision and preventing machine precision differences 

28 """ 

29 if np.any(np.abs(ra) > np.pi * 2.0) or np.any(np.abs(dec) > np.pi * 2.0): 

30 raise ValueError('Expecting RA and Dec values to be in radians.') 

31 x, y, z = _xyz_from_ra_dec(ra, dec) 

32 if scale is not None: 

33 x = np.round(x*scale).astype(int) 

34 y = np.round(y*scale).astype(int) 

35 z = np.round(z*scale).astype(int) 

36 data = list(zip(x, y, z)) 

37 if np.size(data) > 0: 

38 try: 

39 tree = kdTree(data, leafsize=leafsize, balanced_tree=False, compact_nodes=False) 

40 except TypeError: 

41 tree = kdTree(data, leafsize=leafsize) 

42 else: 

43 raise ValueError('ra and dec should have length greater than 0.') 

44 

45 return tree