Coverage for tests/test_star.py: 24%

70 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-02-02 08:00 -0800

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/>. 

21 

22import warnings 

23 

24import numpy as np 

25import astropy.units as u 

26import astropy.time 

27from astropy.coordinates import SkyCoord 

28 

29import unittest 

30import lsst.utils.tests 

31 

32import lsst.jointcal.star 

33 

34 

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.star.BaseStar(self.ra.value, self.dec.value, 

54 flux, flux*0.001) 

55 self.refStar = lsst.jointcal.star.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.star.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) 

63 

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) 

74 

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) 

86 

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) 

91 

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) 

99 

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) 

110 

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) 

115 

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.star.BaseStar(x.ra.value, x.dec.value, 

121 100, 100*0.001) 

122 refStar = lsst.jointcal.star.RefStar(x.ra.value, x.dec.value, 

123 100, 100*0.001) 

124 star.vx = x.ra.to_value(u.degree) * 0.01 

125 star.vy = x.dec.to_value(u.degree) * 0.01 

126 properMotion = lsst.jointcal.star.ProperMotion(x.pm_ra_cosdec.to_value(u.radian/u.yr), 

127 x.pm_dec.to_value(u.radian/u.yr), 

128 x.pm_ra_cosdec.to_value(u.radian/u.yr)*0.01, 

129 x.pm_dec.to_value(u.radian/u.yr)*0.01) 

130 refStar.setProperMotion(properMotion) 

131 result = refStar.applyProperMotion(star, self.dt.value) 

132 ras[i] = result.x 

133 decs[i] = result.y 

134 self.assertFloatsAlmostEqual(ras, expect.ra.to_value(u.degree), rtol=1e-7) 

135 self.assertFloatsAlmostEqual(decs, expect.dec.to_value(u.degree), rtol=6e-7) 

136 

137 

138class MemoryTester(lsst.utils.tests.MemoryTestCase): 

139 pass 

140 

141 

142def setup_module(module): 

143 lsst.utils.tests.init() 

144 

145 

146if __name__ == "__main__": 146 ↛ 147line 146 didn't jump to line 147, because the condition on line 146 was never true

147 lsst.utils.tests.init() 

148 unittest.main()