Coverage for tests/test_utils.py: 24%

103 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-03-03 03:15 -0800

1# This file is part of summit_utils. 

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 

22"""Test cases for utils.""" 

23 

24import copy 

25import itertools 

26import unittest 

27 

28import astropy.time 

29import astropy.units as u 

30import lsst.afw.image as afwImage 

31import lsst.geom as geom 

32import lsst.utils.tests 

33import numpy as np 

34from astro_metadata_translator import makeObservationInfo 

35from lsst.obs.base import createInitialSkyWcsFromBoresight 

36from lsst.obs.base.makeRawVisitInfoViaObsInfo import MakeRawVisitInfoViaObsInfo 

37from lsst.obs.lsst import Latiss 

38from lsst.summit.utils.utils import (getExpPositionOffset, 

39 getFieldNameAndTileNumber, 

40 ) 

41from lsst.obs.lsst.translators.latiss import AUXTEL_LOCATION 

42 

43 

44class ExpSkyPositionOffsetTestCase(lsst.utils.tests.TestCase): 

45 """A test case for testing sky position offsets for exposures.""" 

46 

47 def setUp(self): 

48 camera = Latiss.getCamera() 

49 self.assertTrue(len(camera) == 1) 

50 self.detector = camera[0] 

51 

52 self.viMaker = MakeRawVisitInfoViaObsInfo() 

53 self.mi = afwImage.maskedImage.MaskedImageF(0, 0) 

54 self.baseHeader = dict(boresight_airmass=1.5, 

55 temperature=15*u.deg_C, 

56 observation_type="science", 

57 exposure_time=5*u.ks, 

58 detector_num=32, 

59 location=AUXTEL_LOCATION, 

60 ) 

61 

62 def test_getExpPositionOffset(self): 

63 epsilon = 0.0001 

64 ra1s = [0, 45, 90] 

65 ra2s = copy.copy(ra1s) 

66 ra2s.extend([r + epsilon for r in ra1s]) 

67 ra1s = np.deg2rad(ra1s) 

68 ra2s = np.deg2rad(ra2s) 

69 

70 epsilon = 0.0001 

71 dec1s = [0, 45, 90] 

72 dec2s = copy.copy(dec1s) 

73 dec2s.extend([d + epsilon for d in dec1s[:-1]]) # skip last point as >90 not allowed for dec 

74 

75 rotAngle1 = geom.Angle(43.2, geom.degrees) # arbitrary non-zero 

76 rotAngle2 = geom.Angle(56.7, geom.degrees) 

77 

78 t1 = astropy.time.Time("2021-09-15T12:00:00", format="isot", scale="utc") 

79 t2 = astropy.time.Time("2021-09-15T12:01:00", format="isot", scale="utc") 

80 expTime = astropy.time.TimeDelta(20, format='sec') 

81 

82 header1 = copy.copy(self.baseHeader) 

83 header2 = copy.copy(self.baseHeader) 

84 header1['datetime_begin'] = astropy.time.Time(t1, format="isot", scale="utc") 

85 header2['datetime_begin'] = astropy.time.Time(t2, format="isot", scale="utc") 

86 

87 header1['datetime_end'] = astropy.time.Time(t1+expTime, format="isot", scale="utc") 

88 header2['datetime_end'] = astropy.time.Time(t2+expTime, format="isot", scale="utc") 

89 

90 obsInfo1 = makeObservationInfo(**header1) 

91 obsInfo2 = makeObservationInfo(**header2) 

92 

93 vi1 = self.viMaker.observationInfo2visitInfo(obsInfo1) 

94 vi2 = self.viMaker.observationInfo2visitInfo(obsInfo2) 

95 expInfo1 = afwImage.ExposureInfo() 

96 expInfo1.setVisitInfo(vi1) 

97 expInfo2 = afwImage.ExposureInfo() 

98 expInfo2.setVisitInfo(vi2) 

99 

100 for ra1, dec1, ra2, dec2 in itertools.product(ra1s, dec1s, ra2s, dec2s): 

101 pos1 = geom.SpherePoint(ra1, dec1, geom.degrees) 

102 pos2 = geom.SpherePoint(ra2, dec2, geom.degrees) 

103 

104 wcs1 = createInitialSkyWcsFromBoresight(pos1, rotAngle1, self.detector, flipX=True) 

105 wcs2 = createInitialSkyWcsFromBoresight(pos2, rotAngle2, self.detector, flipX=True) 

106 

107 exp1 = afwImage.ExposureF(self.mi, expInfo1) 

108 exp2 = afwImage.ExposureF(self.mi, expInfo2) 

109 

110 exp1.setWcs(wcs1) 

111 exp2.setWcs(wcs2) 

112 

113 result = getExpPositionOffset(exp1, exp2) 

114 

115 deltaRa = ra1 - ra2 

116 deltaDec = dec1 - dec2 

117 

118 self.assertAlmostEqual(result.deltaRa.asDegrees(), deltaRa, 6) 

119 self.assertAlmostEqual(result.deltaDec.asDegrees(), deltaDec, 6) 

120 

121 

122class MiscUtilsTestCase(lsst.utils.tests.TestCase): 

123 

124 def setUp(self) -> None: 

125 return super().setUp() 

126 

127 def test_getFieldNameAndTileNumber(self): 

128 field, num = getFieldNameAndTileNumber('simple') 

129 self.assertEqual(field, 'simple') 

130 self.assertIsNone(num) 

131 

132 field, num = getFieldNameAndTileNumber('_simple') 

133 self.assertEqual(field, '_simple') 

134 self.assertIsNone(num) 

135 

136 field, num = getFieldNameAndTileNumber('simple_321') 

137 self.assertEqual(field, 'simple') 

138 self.assertEqual(num, 321) 

139 

140 field, num = getFieldNameAndTileNumber('_simple_321') 

141 self.assertEqual(field, '_simple') 

142 self.assertEqual(num, 321) 

143 

144 field, num = getFieldNameAndTileNumber('test_321a_123') 

145 self.assertEqual(field, 'test_321a') 

146 self.assertEqual(num, 123) 

147 

148 field, num = getFieldNameAndTileNumber('test_321a_123_') 

149 self.assertEqual(field, 'test_321a_123_') 

150 self.assertIsNone(num) 

151 

152 field, num = getFieldNameAndTileNumber('test_321a_123a') 

153 self.assertEqual(field, 'test_321a_123a') 

154 self.assertIsNone(num) 

155 

156 field, num = getFieldNameAndTileNumber('test_321a:asd_asd-dsa_321') 

157 self.assertEqual(field, 'test_321a:asd_asd-dsa') 

158 self.assertEqual(num, 321) 

159 

160 

161class TestMemory(lsst.utils.tests.MemoryTestCase): 

162 pass 

163 

164 

165def setup_module(module): 

166 lsst.utils.tests.init() 

167 

168 

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

170 lsst.utils.tests.init() 

171 unittest.main()