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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

""" 

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

cartesian coordinates and to grid id using a spatial tree. 

""" 

from __future__ import division 

 

import numpy as np 

from scipy.spatial import cKDTree as kdTree 

 

from lsst.sims.utils.CoordinateTransformations import _xyz_from_ra_dec 

 

__all__ = ['_buildTree'] 

 

 

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

""" 

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

 

Parameters 

---------- 

ra, dec : float (or arrays) 

RA and Dec values (in radians). 

leafsize : int (100) 

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

scale : float (None) 

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

forcing a set precision and preventing machine precision differences 

""" 

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

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

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

if scale is not None: 

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

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

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

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

if np.size(data) > 0: 

try: 

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

except TypeError: 

tree = kdTree(data, leafsize=leafsize) 

else: 

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

 

return tree