Coverage for python/lsst/faro/utils/coord_util.py: 27%
24 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-26 08:52 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-26 08:52 +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/>.
22import numpy as np
24import lsst.geom as geom
26__all__ = (
27 "averageRaFromCat",
28 "averageDecFromCat",
29 "averageRaDecFromCat",
30 "averageRaDec",
31 "sphDist",
32)
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 with .get method for 'coord_ra', 'coord_dec' that returns radians.
46 Returns
47 -------
48 ra_mean : `float`
49 Mean RA in radians.
50 """
51 meanRa, meanDec = averageRaDecFromCat(cat)
52 return meanRa
55def averageDecFromCat(cat):
56 """Compute the average declination from a catalog of measurements.
57 This function is used as an aggregate function to extract just declination
58 from a lsst.afw.table.MultiMatch final match catalog.
59 The actual computation involves both RA and Dec.
60 The intent is to use this for a set of measurements of the same source
61 but that's neither enforced nor required.
62 Parameters
63 ----------
64 cat : collection
65 Object with .get method for 'coord_ra', 'coord_dec' that returns radians.
66 Returns
67 -------
68 dec_mean : `float`
69 Mean Dec in radians.
70 """
71 meanRa, meanDec = averageRaDecFromCat(cat)
72 return meanDec
75def averageRaDecFromCat(cat):
76 """Calculate the average right ascension and declination from a catalog.
77 Convenience wrapper around averageRaDec
78 Parameters
79 ----------
80 cat : collection
81 Object with .get method for 'coord_ra', 'coord_dec' that returns radians.
82 Returns
83 -------
84 ra_mean : `float`
85 Mean RA in radians.
86 dec_mean : `float`
87 Mean Dec in radians.
88 """
89 return averageRaDec(cat.get("coord_ra"), cat.get("coord_dec"))
92def averageRaDec(ra, dec):
93 """Calculate average RA, Dec from input lists using spherical geometry.
94 Parameters
95 ----------
96 ra : `list` [`float`]
97 RA in [radians]
98 dec : `list` [`float`]
99 Dec in [radians]
100 Returns
101 -------
102 float, float
103 meanRa, meanDec -- Tuple of average RA, Dec [radians]
104 """
105 assert len(ra) == len(dec)
107 angleRa = [geom.Angle(r, geom.radians) for r in ra]
108 angleDec = [geom.Angle(d, geom.radians) for d in dec]
109 coords = [
110 geom.SpherePoint(ar, ad, geom.radians) for (ar, ad) in zip(angleRa, angleDec)
111 ]
113 meanRa, meanDec = geom.averageSpherePoint(coords)
115 return meanRa.asRadians(), meanDec.asRadians()
118def sphDist(ra_mean, dec_mean, ra, dec):
119 """Calculate distance on the surface of a unit sphere.
120 Parameters
121 ----------
122 ra_mean : `float`
123 Mean RA in radians.
124 dec_mean : `float`
125 Mean Dec in radians.
126 ra : `numpy.array` [`float`]
127 Array of RA in radians.
128 dec : `numpy.array` [`float`]
129 Array of Dec in radians.
130 Notes
131 -----
132 Uses the Haversine formula to preserve accuracy at small angles.
133 Law of cosines approach doesn't work well for the typically very small
134 differences that we're looking at here.
135 """
136 # Haversine
137 dra = ra - ra_mean
138 ddec = dec - dec_mean
139 a = np.square(np.sin(ddec / 2)) + np.cos(dec_mean) * np.cos(dec) * np.square(
140 np.sin(dra / 2)
141 )
142 dist = 2 * np.arcsin(np.sqrt(a))
144 # This is what the law of cosines would look like
145 # dist = np.arccos(np.sin(dec1)*np.sin(dec2) + np.cos(dec1)*np.cos(dec2)*np.cos(ra1 - ra2))
147 # This will also work, but must run separately for each element
148 # whereas the numpy version will run on either scalars or arrays:
149 # sp1 = geom.SpherePoint(ra1, dec1, geom.radians)
150 # sp2 = geom.SpherePoint(ra2, dec2, geom.radians)
151 # return sp1.separation(sp2).asRadians()
153 return dist