Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# This file is part of obs_decam. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://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 <http://www.gnu.org/licenses/>. 

21 

22"""Unit tests for gen3 DECam raw formatter, which has to determine which 

23HDU a given detector is in in a multi-extension FITS file. 

24""" 

25 

26import astro_metadata_translator 

27import unittest 

28import os 

29 

30import lsst.utils.tests 

31import lsst.obs.decam 

32import lsst.daf.butler 

33import lsst.afw.image 

34 

35testDataPackage = "testdata_decam" 

36try: 

37 testDataDirectory = lsst.utils.getPackageDir(testDataPackage) 

38except LookupError: 

39 testDataDirectory = None 

40 

41 

42@unittest.skipIf(testDataDirectory is None, "testdata_decam must be set up") 

43class DarkEnergyCameraRawFormatterTestCase(lsst.utils.tests.TestCase): 

44 def setUp(self): 

45 path = 'rawData/2013-09-01/z/decam0229388.fits.fz' 

46 self.filename = os.path.join(testDataDirectory, path) 

47 location = lsst.daf.butler.Location(testDataDirectory, path) 

48 self.fileDescriptor = lsst.daf.butler.FileDescriptor(location, None) 

49 

50 def check_readMetadata(self, dataId, expected): 

51 formatter = lsst.obs.decam.DarkEnergyCameraRawFormatter(self.fileDescriptor, dataId) 

52 metadata = formatter.readMetadata() 

53 self.assertEqual(metadata, expected) 

54 

55 def test_readMetadata(self): 

56 dataId = {'detector': 25} 

57 # detector 25 is in HDU 1 

58 expected = lsst.afw.image.readMetadata(self.filename, 1) 

59 self.assertEqual(expected['CCDNUM'], 25) # sanity check 

60 self.check_readMetadata(dataId, expected) 

61 

62 dataId = {'detector': 1} 

63 # detector 1 is in HDU 2 

64 expected = lsst.afw.image.readMetadata(self.filename, 2) 

65 astro_metadata_translator.fix_header(expected) 

66 self.assertEqual(expected['CCDNUM'], 1) # sanity check 

67 self.check_readMetadata(dataId, expected) 

68 

69 def test_readMetadata_raises(self): 

70 dataId = {'detector': 70} 

71 formatter = lsst.obs.decam.DarkEnergyCameraRawFormatter(self.fileDescriptor, dataId) 

72 with self.assertRaisesRegex(ValueError, "detectorId=70"): 

73 formatter.readMetadata() 

74 

75 def check_readImage(self, dataId, expected): 

76 formatter = lsst.obs.decam.DarkEnergyCameraRawFormatter(self.fileDescriptor, dataId) 

77 image = formatter.readImage() 

78 self.assertImagesEqual(image, expected) 

79 

80 def test_readImage(self): 

81 dataId = {'detector': 25} 

82 # detector 25 is in HDU 1 

83 expected = lsst.afw.image.ImageI(self.filename, 1) 

84 self.check_readImage(dataId, expected) 

85 

86 dataId = {'detector': 1} 

87 # detector 1 is in HDU 2 

88 expected = lsst.afw.image.ImageI(self.filename, 2) 

89 self.check_readImage(dataId, expected) 

90 

91 def test_readMetadata_full_file(self): 

92 """Test reading a file with all HDUs, and with all HDUs in a shuffled 

93 order. 

94 """ 

95 

96 full_file = 'rawData/raw/c4d_150227_012718_ori-stripped.fits.fz' 

97 full_location = lsst.daf.butler.Location(testDataDirectory, full_file) 

98 full_fileDescriptor = lsst.daf.butler.FileDescriptor(full_location, None) 

99 

100 shuffled_file = 'rawData/raw/c4d_150227_012718_ori-stripped-shuffled.fits.fz' 

101 shuffled_location = lsst.daf.butler.Location(testDataDirectory, shuffled_file) 

102 shuffled_fileDescriptor = lsst.daf.butler.FileDescriptor(shuffled_location, None) 

103 

104 for detector in range(1, 63): 

105 formatter = lsst.obs.decam.DarkEnergyCameraRawFormatter(full_fileDescriptor, detector) 

106 full_index, full_metadata = formatter._determineHDU(detector) 

107 formatter = lsst.obs.decam.DarkEnergyCameraRawFormatter(shuffled_fileDescriptor, detector) 

108 shuffled_index, shuffled_metadata = formatter._determineHDU(detector) 

109 

110 # The shuffled file should have different indices, 

111 self.assertNotEqual(full_index, shuffled_index) 

112 # but the metadata should be the same in both files. 

113 self.assertEqual(shuffled_metadata, full_metadata) 

114 

115 

116@unittest.skipIf(testDataDirectory is None, "testdata_decam must be set up") 

117class DarkEnergyCameraCPCalibFormatterTestCase(lsst.utils.tests.TestCase): 

118 """DECam Community Pipeline calibrations have one detector per HDU. 

119 """ 

120 def setUp(self): 

121 path = 'hits2015-zeroed/c4d_150218_191721_zci_v1.fits.fz' 

122 self.biasFile = os.path.join(testDataDirectory, path) 

123 location = lsst.daf.butler.Location(testDataDirectory, path) 

124 self.biasDescriptor = lsst.daf.butler.FileDescriptor(location, None) 

125 

126 path = 'hits2015-zeroed/c4d_150218_200522_fci_g_v1.fits.fz' 

127 self.flatFile = os.path.join(testDataDirectory, path) 

128 location = lsst.daf.butler.Location(testDataDirectory, path) 

129 self.flatDescriptor = lsst.daf.butler.FileDescriptor(location, None) 

130 

131 def check_readMetadata(self, dataId, expected, fileDescriptor): 

132 formatter = lsst.obs.decam.DarkEnergyCameraCPCalibFormatter(fileDescriptor, dataId) 

133 metadata = formatter.readMetadata() 

134 self.assertEqual(metadata['CCDNUM'], dataId['detector'], dataId) 

135 self.assertEqual(metadata, expected, msg=dataId) 

136 

137 def test_readMetadata(self): 

138 for i in range(1, 63): 

139 dataId = {'detector': i} 

140 expected = lsst.afw.image.readMetadata(self.biasFile, i) 

141 self.check_readMetadata(dataId, expected, self.biasDescriptor) 

142 

143 expected = lsst.afw.image.readMetadata(self.flatFile, i) 

144 self.check_readMetadata(dataId, expected, self.flatDescriptor) 

145 

146 

147def setup_module(module): 

148 lsst.utils.tests.init() 

149 

150 

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

152 lsst.utils.tests.init() 

153 unittest.main()