Coverage for python/lsst/faro/utils/coord_util.py: 27%

24 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-08-22 11:23 +0000

1# This file is part of faro. 

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 

22import numpy as np 

23 

24import lsst.geom as geom 

25 

26__all__ = ( 

27 "averageRaFromCat", 

28 "averageDecFromCat", 

29 "averageRaDecFromCat", 

30 "averageRaDec", 

31 "sphDist", 

32) 

33 

34 

35def averageRaFromCat(cat): 

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

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

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

39 The actual computation involves both RA and Dec. 

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

41 but that's neither enforced nor required. 

42 Parameters 

43 ---------- 

44 cat : collection 

45 Object for which ``cat["coord_ra"]`` and ``cat["coord_dec"]`` both 

46 return radians. 

47 Returns 

48 ------- 

49 ra_mean : `float` 

50 Mean RA in radians. 

51 """ 

52 meanRa, _ = averageRaDecFromCat(cat) 

53 return meanRa 

54 

55 

56def averageDecFromCat(cat): 

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

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

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

60 The actual computation involves both RA and Dec. 

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

62 but that's neither enforced nor required. 

63 Parameters 

64 ---------- 

65 cat : collection 

66 Object for which ``cat["coord_ra"]`` and ``cat["coord_dec"]`` both 

67 return radians. 

68 Returns 

69 ------- 

70 dec_mean : `float` 

71 Mean Dec in radians. 

72 """ 

73 _, meanDec = averageRaDecFromCat(cat) 

74 return meanDec 

75 

76 

77def averageRaDecFromCat(cat): 

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

79 Convenience wrapper around averageRaDec 

80 Parameters 

81 ---------- 

82 cat : collection 

83 Object for which ``cat["coord_ra"]`` and ``cat["coord_dec"]`` both 

84 return radians. 

85 Returns 

86 ------- 

87 ra_mean : `float` 

88 Mean RA in radians. 

89 dec_mean : `float` 

90 Mean Dec in radians. 

91 """ 

92 return averageRaDec(cat["coord_ra"], cat["coord_dec"]) 

93 

94 

95def averageRaDec(ra, dec): 

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

97 Parameters 

98 ---------- 

99 ra : `list` [`float`] 

100 RA in [radians] 

101 dec : `list` [`float`] 

102 Dec in [radians] 

103 Returns 

104 ------- 

105 float, float 

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

107 """ 

108 assert len(ra) == len(dec) 

109 

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

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

112 coords = [ 

113 geom.SpherePoint(ar, ad, geom.radians) for (ar, ad) in zip(angleRa, angleDec) 

114 ] 

115 

116 meanRa, meanDec = geom.averageSpherePoint(coords) 

117 

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

119 

120 

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

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

123 Parameters 

124 ---------- 

125 ra_mean : `float` 

126 Mean RA in radians. 

127 dec_mean : `float` 

128 Mean Dec in radians. 

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

130 Array of RA in radians. 

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

132 Array of Dec in radians. 

133 Notes 

134 ----- 

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

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

137 differences that we're looking at here. 

138 """ 

139 # Haversine 

140 dra = ra - ra_mean 

141 ddec = dec - dec_mean 

142 a = np.square(np.sin(ddec / 2)) + np.cos(dec_mean) * np.cos(dec) * np.square( 

143 np.sin(dra / 2) 

144 ) 

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

146 

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

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

149 

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

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

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

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

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

155 

156 return dist