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

1import numpy as np 

2 

3import lsst.geom as geom 

4 

5__all__ = ("averageRaFromCat", "averageDecFromCat", "averageRaDecFromCat", "averageRaDec", "sphDist") 

6 

7 

8def averageRaFromCat(cat): 

9 """Compute the average right ascension from a catalog of measurements. 

10 This function is used as an aggregate function to extract just RA 

11 from a lsst.afw.table.MultiMatch final match catalog. 

12 The actual computation involves both RA and Dec. 

13 The intent is to use this for a set of measurements of the same source 

14 but that's neither enforced nor required. 

15 Parameters 

16 ---------- 

17 cat : collection 

18 Object with .get method for 'coord_ra', 'coord_dec' that returns radians. 

19 Returns 

20 ------- 

21 ra_mean : `float` 

22 Mean RA in radians. 

23 """ 

24 meanRa, meanDec = averageRaDecFromCat(cat) 

25 return meanRa 

26 

27 

28def averageDecFromCat(cat): 

29 """Compute the average declination from a catalog of measurements. 

30 This function is used as an aggregate function to extract just declination 

31 from a lsst.afw.table.MultiMatch final match catalog. 

32 The actual computation involves both RA and Dec. 

33 The intent is to use this for a set of measurements of the same source 

34 but that's neither enforced nor required. 

35 Parameters 

36 ---------- 

37 cat : collection 

38 Object with .get method for 'coord_ra', 'coord_dec' that returns radians. 

39 Returns 

40 ------- 

41 dec_mean : `float` 

42 Mean Dec in radians. 

43 """ 

44 meanRa, meanDec = averageRaDecFromCat(cat) 

45 return meanDec 

46 

47 

48def averageRaDecFromCat(cat): 

49 """Calculate the average right ascension and declination from a catalog. 

50 Convenience wrapper around averageRaDec 

51 Parameters 

52 ---------- 

53 cat : collection 

54 Object with .get method for 'coord_ra', 'coord_dec' that returns radians. 

55 Returns 

56 ------- 

57 ra_mean : `float` 

58 Mean RA in radians. 

59 dec_mean : `float` 

60 Mean Dec in radians. 

61 """ 

62 return averageRaDec(cat.get('coord_ra'), cat.get('coord_dec')) 

63 

64 

65def averageRaDec(ra, dec): 

66 """Calculate average RA, Dec from input lists using spherical geometry. 

67 Parameters 

68 ---------- 

69 ra : `list` [`float`] 

70 RA in [radians] 

71 dec : `list` [`float`] 

72 Dec in [radians] 

73 Returns 

74 ------- 

75 float, float 

76 meanRa, meanDec -- Tuple of average RA, Dec [radians] 

77 """ 

78 assert(len(ra) == len(dec)) 

79 

80 angleRa = [geom.Angle(r, geom.radians) for r in ra] 

81 angleDec = [geom.Angle(d, geom.radians) for d in dec] 

82 coords = [geom.SpherePoint(ar, ad, geom.radians) for (ar, ad) in zip(angleRa, angleDec)] 

83 

84 meanRa, meanDec = geom.averageSpherePoint(coords) 

85 

86 return meanRa.asRadians(), meanDec.asRadians() 

87 

88 

89def sphDist(ra_mean, dec_mean, ra, dec): 

90 """Calculate distance on the surface of a unit sphere. 

91 Parameters 

92 ---------- 

93 ra_mean : `float` 

94 Mean RA in radians. 

95 dec_mean : `float` 

96 Mean Dec in radians. 

97 ra : `numpy.array` [`float`] 

98 Array of RA in radians. 

99 dec : `numpy.array` [`float`] 

100 Array of Dec in radians. 

101 Notes 

102 ----- 

103 Uses the Haversine formula to preserve accuracy at small angles. 

104 Law of cosines approach doesn't work well for the typically very small 

105 differences that we're looking at here. 

106 """ 

107 # Haversine 

108 dra = ra - ra_mean 

109 ddec = dec - dec_mean 

110 a = np.square(np.sin(ddec/2)) + \ 

111 np.cos(dec_mean)*np.cos(dec)*np.square(np.sin(dra/2)) 

112 dist = 2 * np.arcsin(np.sqrt(a)) 

113 

114 # This is what the law of cosines would look like 

115 # dist = np.arccos(np.sin(dec1)*np.sin(dec2) + np.cos(dec1)*np.cos(dec2)*np.cos(ra1 - ra2)) 

116 

117 # This will also work, but must run separately for each element 

118 # whereas the numpy version will run on either scalars or arrays: 

119 # sp1 = geom.SpherePoint(ra1, dec1, geom.radians) 

120 # sp2 = geom.SpherePoint(ra2, dec2, geom.radians) 

121 # return sp1.separation(sp2).asRadians() 

122 

123 return dist