Coverage for tests/test_repository.py: 30%

136 statements  

« prev     ^ index     » next       coverage.py v7.1.0, created at 2023-02-05 18:55 -0800

1# 

2# LSST Data Management System 

3# 

4# Copyright 2008-2016 AURA/LSST. 

5# 

6# This product includes software developed by the 

7# LSST Project (http://www.lsst.org/). 

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 LSST License Statement and 

20# the GNU General Public License along with this program. If not, 

21# see <https://www.lsstcorp.org/LegalNotices/>. 

22# 

23import os 

24import unittest 

25from glob import glob 

26from shutil import rmtree 

27from subprocess import check_call 

28from tempfile import mkdtemp 

29 

30import lsst.geom as geom 

31import lsst.daf.persistence as dafPersist 

32import lsst.utils 

33from lsst.daf.base import DateTime 

34from lsst.afw.image import RotType 

35from lsst.geom import degrees, radians 

36from lsst.obs.hsc import HscMapper 

37 

38testDataPackage = "testdata_subaru" 

39try: 

40 testDataDirectory = lsst.utils.getPackageDir(testDataPackage) 

41except LookupError: 

42 testDataDirectory = None 

43 

44 

45def createDataRepository(mapperName, inputPath, calibPath): 

46 """ 

47 Construct a data repository for a particular mapper containing a given set 

48 of input data and return its path. 

49 

50 @param[in] mapperName Name of mapper class for use with repository. 

51 @param[in] inputPath Location of data files which will be ingested. 

52 @param[in] calibPath Location of calibs. 

53 """ 

54 repoPath = mkdtemp() 

55 with open(os.path.join(repoPath, "_mapper"), "w") as _mapper: 

56 _mapper.write(mapperName) 

57 ingest_cmd = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "bin", "hscIngestImages.py") 

58 check_call([ingest_cmd, repoPath, "--calib", calibPath] + glob(os.path.join(inputPath, "*.fits.gz"))) 

59 return repoPath 

60 

61 

62class IngestRawTestCase(lsst.utils.tests.TestCase): 

63 """Ensure that we can ingest raw data to a repository.""" 

64 @unittest.skipIf(testDataDirectory is None, "%s is not available" % testDataPackage) 

65 def setUp(self): 

66 rawPath = os.path.join(testDataDirectory, "hsc", "raw") 

67 calibPath = os.path.join(testDataDirectory, "hsc", "calib") 

68 self.repoPath = createDataRepository("lsst.obs.hsc.HscMapper", rawPath, calibPath) 

69 

70 def tearDown(self): 

71 rmtree(self.repoPath) 

72 

73 def testIngest(self): 

74 self.assertTrue(os.path.exists(os.path.join(self.repoPath, "registry.sqlite3"))) 

75 

76 def testMapperName(self): 

77 name = dafPersist.Butler.getMapperClass(root=self.repoPath).packageName 

78 self.assertEqual(name, "obs_subaru") 

79 

80 

81class GetDataTestCase(lsst.utils.tests.TestCase): 

82 """Demonstrate retrieval of data from a repository.""" 

83 @unittest.skipIf(testDataDirectory is None, "%s is not available" % testDataPackage) 

84 def setUp(self): 

85 rawPath = os.path.join(testDataDirectory, "hsc", "raw") 

86 calibPath = os.path.join(testDataDirectory, "hsc", "calib") 

87 self.repoPath = createDataRepository("lsst.obs.hsc.HscMapper", rawPath, calibPath) 

88 self.butler = dafPersist.Butler(root=self.repoPath, calibRoot=calibPath) 

89 

90 # The following properties of the provided data are known a priori. 

91 self.visit = 904024 

92 self.ccdNum = 50 

93 self.filter = 'i' 

94 self.rawSize = (2144, 4241) 

95 self.ccdSize = (2048, 4176) 

96 self.exptime = 30.0 

97 self.darktime = self.exptime # No explicit darktime 

98 self.dateAvg = DateTime(56598.26165414352, DateTime.MJD, DateTime.TAI) 

99 self.boresightRaDec = geom.SpherePoint(320.7499250000, 0.500019444, degrees) 

100 self.boresightAzAlt = geom.SpherePoint(226.68922661, 63.04359233, degrees) 

101 self.boresightAirmass = 1.121626027604189 

102 self.boresightRotAngle = 270.0*degrees 

103 self.rotType = RotType.SKY 

104 self.obs_longitude = -155.476667*degrees 

105 self.obs_latitude = 19.825556*degrees 

106 self.obs_elevation = 4139 

107 self.weath_airTemperature = -0.8 

108 self.weath_airPressure = 62170. 

109 self.weath_humidity = 33.1 

110 # NOTE: if we deal with DM-8053 and get UT1 implemented, ERA will 

111 # change slightly. 

112 self.era = 2.3659570321481826*radians 

113 

114 def tearDown(self): 

115 del self.butler 

116 rmtree(self.repoPath) 

117 

118 def testRaw(self): 

119 """Test retrieval of raw image""" 

120 raw = self.butler.get("raw", visit=self.visit, ccd=self.ccdNum) 

121 ccd = raw.getDetector() 

122 self.assertEqual(raw.getDimensions(), geom.Extent2I(*self.rawSize)) 

123 self.assertEqual(raw.getFilterLabel().bandLabel, self.filter) 

124 self.assertEqual(ccd.getId(), self.ccdNum) 

125 self.assertEqual(ccd.getBBox().getDimensions(), geom.Extent2I(*self.ccdSize)) 

126 

127 visitInfo = raw.getInfo().getVisitInfo() 

128 self.assertAlmostEqual(visitInfo.getDate().get(), self.dateAvg.get()) 

129 self.assertAnglesAlmostEqual(visitInfo.getEra(), self.era) 

130 self.assertSpherePointsAlmostEqual(visitInfo.getBoresightRaDec(), self.boresightRaDec) 

131 self.assertSpherePointsAlmostEqual(visitInfo.getBoresightAzAlt(), self.boresightAzAlt) 

132 self.assertAlmostEqual(visitInfo.getBoresightAirmass(), self.boresightAirmass) 

133 self.assertAnglesAlmostEqual(visitInfo.getBoresightRotAngle(), self.boresightRotAngle) 

134 self.assertEqual(visitInfo.getRotType(), self.rotType) 

135 self.assertEqual(visitInfo.getExposureTime(), self.exptime) 

136 self.assertEqual(visitInfo.getDarkTime(), self.darktime) 

137 observatory = visitInfo.getObservatory() 

138 self.assertAnglesAlmostEqual(observatory.getLongitude(), self.obs_longitude) 

139 self.assertAnglesAlmostEqual(observatory.getLatitude(), self.obs_latitude) 

140 self.assertAlmostEqual(observatory.getElevation(), self.obs_elevation) 

141 weather = visitInfo.getWeather() 

142 self.assertAlmostEqual(weather.getAirTemperature(), self.weath_airTemperature) 

143 self.assertAlmostEqual(weather.getAirPressure(), self.weath_airPressure, places=4) 

144 self.assertAlmostEqual(weather.getHumidity(), self.weath_humidity) 

145 

146 def testPupil(self): 

147 """Test retrieval of pupil (without checking value)""" 

148 raw = self.butler.get("raw", visit=self.visit, ccd=self.ccdNum) 

149 visitInfo = raw.getInfo().getVisitInfo() 

150 camera = self.butler.get("camera", visit=self.visit, ccd=self.ccdNum) 

151 size = 16.4 

152 npix = 1024 

153 

154 pupilFactory = camera.getPupilFactory(visitInfo, size, npix) 

155 self.assertIsInstance(pupilFactory, lsst.obs.hsc.hscPupil.HscPupilFactory) 

156 

157 pupil = pupilFactory.getPupil(geom.Point2D(0.0, 0.0)) 

158 self.assertEqual(pupil.size, size) 

159 self.assertEqual(pupil.scale, size/npix) 

160 self.assertEqual(pupil.illuminated.shape, (npix, npix)) 

161 

162 def testRawMetadata(self): 

163 """Test retrieval of raw image metadata""" 

164 md = self.butler.get("raw_md", visit=self.visit, ccd=self.ccdNum) 

165 self.assertEqual(md.getScalar("DET-ID"), self.ccdNum) 

166 

167 def testBias(self): 

168 """Test retrieval of bias frame""" 

169 bias = self.butler.get("bias", visit=self.visit, ccd=self.ccdNum) 

170 self.assertEqual(bias.getDimensions(), geom.Extent2I(*self.ccdSize)) 

171 self.assertEqual(bias.getDetector().getId(), self.ccdNum) 

172 

173 def testDark(self): 

174 """Test retrieval of dark frame""" 

175 dark = self.butler.get("dark", visit=self.visit, ccd=self.ccdNum) 

176 self.assertEqual(dark.getDimensions(), geom.Extent2I(*self.ccdSize)) 

177 self.assertEqual(dark.getDetector().getId(), self.ccdNum) 

178 self.assertEqual(dark.getInfo().getVisitInfo().getExposureTime(), 1.0) 

179 

180 def testFlat(self): 

181 """Test retrieval of flat""" 

182 flat = self.butler.get("flat", visit=self.visit, ccd=self.ccdNum) 

183 self.assertEqual(flat.getDimensions(), geom.Extent2I(*self.ccdSize)) 

184 self.assertEqual(flat.getDetector().getId(), self.ccdNum) 

185 

186 def testLinearizer(self): 

187 lin1 = self.butler.get('linearizer', ccd=1) 

188 lin2 = self.butler.get('linearizer', ccd=2) 

189 self.assertIsNotNone(lin1) 

190 self.assertIsNotNone(lin2) 

191 self.assertNotEqual(lin1, lin2) 

192 

193 

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

195 def setUp(self): 

196 HscMapper.clearCache() 

197 lsst.utils.tests.MemoryTestCase.setUp(self) 

198 

199 

200def setup_module(module): 

201 lsst.utils.tests.init() 

202 

203 

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

205 lsst.utils.tests.init() 

206 unittest.main()