Coverage for tests/test_star.py: 20%
87 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-16 13:18 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-16 13:18 +0000
1# This file is part of jointcal.
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 warnings
24import numpy as np
25import astropy.units as u
26import astropy.time
27from astropy.coordinates import SkyCoord
29import unittest
30import lsst.utils.tests
32import lsst.jointcal
35class TestProperMotion(lsst.utils.tests.TestCase):
36 """Tests of applying proper motion correction to stars."""
37 def setUp(self):
38 # These tests use times that are outside what ERFA considers "reasonable";
39 # ignore any warnings it emits about times and distances.
40 warnings.filterwarnings("ignore", module="erfa")
41 self.ra = 300 * u.degree
42 self.dec = 80 * u.degree
43 self.pm_ra = 1000 * u.mas / u.yr # ra*cos(dec)
44 self.pm_dec = 2000 * u.mas / u.yr
45 flux = 10 # need something for the constructor, but value is irrelevant here
46 # 100 year baseline
47 self.baseline_epoch = astropy.time.Time("2000-01-01 00:00:05", scale="tai")
48 self.observed_epoch = astropy.time.Time("2100-01-01 00:00:05", scale="tai")
49 self.dt = (self.observed_epoch.tai - self.baseline_epoch.tai).to(astropy.units.yr)
50 self.coord = SkyCoord(self.ra, self.dec, frame="icrs",
51 pm_ra_cosdec=self.pm_ra, pm_dec=self.pm_dec,
52 obstime=self.observed_epoch)
53 self.star = lsst.jointcal.BaseStar(self.ra.value, self.dec.value,
54 flux, flux*0.001)
55 self.refStar = lsst.jointcal.RefStar(self.ra.value, self.dec.value,
56 flux, flux*0.001)
57 self.star.vx = self.ra.to_value(u.degree) * 0.01
58 self.star.vy = self.dec.to_value(u.degree) * 0.01
59 self.properMotion = lsst.jointcal.ProperMotion(self.pm_ra.to_value(u.radian/u.yr),
60 self.pm_dec.to_value(u.radian/u.yr),
61 self.pm_ra.to_value(u.radian/u.yr)*0.01,
62 self.pm_dec.to_value(u.radian/u.yr)*0.01)
64 # Test points on the whole sphere, all with the same large proper motion value.
65 np.random.seed(100)
66 n = 100
67 ras = np.random.random(n)*360 * u.degree
68 # uniformly distributed in declination
69 decs = np.arccos(np.random.random(n)*2 - 1) * u.degree
70 pm_ra = np.ones(n) * 300 * u.mas / u.yr
71 pm_dec = np.ones(n) * 400 * u.mas / u.yr
72 self.coords = SkyCoord(ras, decs, frame="icrs", pm_ra_cosdec=pm_ra, pm_dec=pm_dec,
73 obstime=self.observed_epoch)
75 def test_apply_no_proper_motion(self):
76 """If refCat as no ProperMotion set, applyProperMotion() should change
77 nothing.
78 """
79 result = self.refStar.applyProperMotion(self.star, self.dt.value)
80 self.assertEqual(result.x, self.star.x)
81 self.assertEqual(result.y, self.star.y)
82 # TODO? astropy SkyCoord does not include coordinate errors, or error propogation.
83 # How do I test it?
84 # self.assertEqual(result.vx, self.star.vx)
85 # self.assertEqual(result.vy, self.star.vy)
87 def test_apply_one(self):
88 """Test apply on a single coordinate (useful for debugging).
89 """
90 expect = self.coord.apply_space_motion(dt=self.dt)
92 self.refStar.setProperMotion(self.properMotion)
93 result = self.refStar.applyProperMotion(self.star, self.dt.value)
94 # original star should not be changed:
95 self.assertEqual(self.ra.to_value(u.degree), self.star.x)
96 self.assertEqual(self.dec.to_value(u.degree), self.star.y)
97 self.assertEqual(self.ra.to_value(u.degree)*0.01, self.star.vx)
98 self.assertEqual(self.dec.to_value(u.degree)*0.01, self.star.vy)
100 # 5e-8 deg == 180 microarcsec over a 100 year baseline.
101 # The precision here is driven by differences in astropy coord.apply_space_motion()
102 # for different architectures; the computation of refStar.applyProperMotion does
103 # not depend on architecture to full double precision.
104 self.assertFloatsAlmostEqual(result.x, expect.ra.to_value(u.degree), rtol=5e-8)
105 self.assertFloatsAlmostEqual(result.y, expect.dec.to_value(u.degree), rtol=5e-8)
106 # TODO? astropy SkyCoord does not include coordinate errors, or error propogation.
107 # How do I test it?
108 # self.assertEqual(result.vx, expect.vx)
109 # self.assertEqual(result.vy, expect.vy)
111 def test_apply_many(self):
112 """Test apply over a range of points on the sphere.
113 """
114 expect = self.coords.apply_space_motion(dt=self.dt)
116 ras = np.zeros(len(expect))
117 decs = np.zeros(len(expect))
118 for i, x in enumerate(self.coords):
119 self.coords
120 star = lsst.jointcal.BaseStar(x.ra.value, x.dec.value, 100, 100*0.001)
121 refStar = lsst.jointcal.RefStar(x.ra.value, x.dec.value, 100, 100*0.001)
122 star.vx = x.ra.to_value(u.degree) * 0.01
123 star.vy = x.dec.to_value(u.degree) * 0.01
124 properMotion = lsst.jointcal.ProperMotion(x.pm_ra_cosdec.to_value(u.radian/u.yr),
125 x.pm_dec.to_value(u.radian/u.yr),
126 x.pm_ra_cosdec.to_value(u.radian/u.yr)*0.01,
127 x.pm_dec.to_value(u.radian/u.yr)*0.01)
128 refStar.setProperMotion(properMotion)
129 result = refStar.applyProperMotion(star, self.dt.value)
130 ras[i] = result.x
131 decs[i] = result.y
132 self.assertFloatsAlmostEqual(ras, expect.ra.to_value(u.degree), rtol=3e-7)
133 self.assertFloatsAlmostEqual(decs, expect.dec.to_value(u.degree), rtol=6e-7)
135 def test_apply_many_reverse(self):
136 """Test apply for a negative time shift over a range of points on the
137 sphere."""
138 expect = self.coords.apply_space_motion(dt=-self.dt)
140 ras = np.zeros(len(expect))
141 decs = np.zeros(len(expect))
142 for i, x in enumerate(self.coords):
143 self.coords
144 star = lsst.jointcal.BaseStar(x.ra.value, x.dec.value, 100, 100*0.001)
145 refStar = lsst.jointcal.RefStar(x.ra.value, x.dec.value, 100, 100*0.001)
146 star.vx = x.ra.to_value(u.degree) * 0.01
147 star.vy = x.dec.to_value(u.degree) * 0.01
148 properMotion = lsst.jointcal.ProperMotion(x.pm_ra_cosdec.to_value(u.radian/u.yr),
149 x.pm_dec.to_value(u.radian/u.yr),
150 x.pm_ra_cosdec.to_value(u.radian/u.yr)*0.01,
151 x.pm_dec.to_value(u.radian/u.yr)*0.01)
152 refStar.setProperMotion(properMotion)
153 result = refStar.applyProperMotion(star, -self.dt.value)
154 ras[i] = result.x
155 decs[i] = result.y
156 self.assertFloatsAlmostEqual(ras, expect.ra.to_value(u.degree), rtol=3e-7)
157 self.assertFloatsAlmostEqual(decs, expect.dec.to_value(u.degree), rtol=6e-7)
160class MemoryTester(lsst.utils.tests.MemoryTestCase):
161 pass
164def setup_module(module):
165 lsst.utils.tests.init()
168if __name__ == "__main__": 168 ↛ 169line 168 didn't jump to line 169, because the condition on line 168 was never true
169 lsst.utils.tests.init()
170 unittest.main()