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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

# This file is part of obs_lsst. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

 

import os 

import sys 

import unittest 

 

import lsst.utils.tests 

from lsst.utils import getPackageDir 

from lsst.daf.persistence import Butler 

from lsst.afw.cameraGeom import Camera, Detector 

from lsst.afw.image import ImageFitsReader 

import lsst.obs.base.yamlCamera as yamlCamera 

 

from lsst.obs.lsst.assembly import fixAmpGeometry 

from lsst.obs.lsst.utils import readRawFile 

 

PACKAGE_DIR = getPackageDir("obs_lsst") 

TESTDIR = os.path.dirname(__file__) 

LATISS_DATA_ROOT = os.path.join(PACKAGE_DIR, "data", "input", "latiss") 

BAD_OVERSCAN_GEN2_DATA_ID = {'dayObs': '2018-09-20', 'seqNum': 65, 'detector': 0} 

BAD_OVERSCAN_FILENAME = "raw/2018-09-20/3018092000065-det000.fits" 

LOCAL_DATA_ROOT = os.path.join(TESTDIR, "data") 

 

 

class RawAssemblyTestCase(lsst.utils.tests.TestCase): 

 

def setUp(self): 

# A snapshot of LATISS that has incorrect overscan regions for this 

# data ID 

self.cameraBroken = Camera.readFits(os.path.join(LOCAL_DATA_ROOT, "camera-bad-overscan.fits")) 

# A snapshot of the Detector for this file after we've read it in with 

# code that fixes the overscans. 

self.detectorFixed = Detector.readFits(os.path.join(LOCAL_DATA_ROOT, "detector-fixed-assembled.fits")) 

self.assertEqual(self.cameraBroken[0].getName(), self.detectorFixed.getName()) 

 

def assertAmpRawBBoxesEqual(self, amp1, amp2): 

"""Check that Raw bounding boxes match between amps. 

 

Parameters 

---------- 

amp1 : `~lsst.afw.cameraGeom.Amplifier` 

First amplifier. 

amp2 : `~lsst.afw.cameraGeom.Amplifier` 

Second amplifier. 

""" 

self.assertEqual(amp1.getRawBBox(), amp2.getRawBBox()) 

self.assertEqual(amp1.getRawHorizontalOverscanBBox(), amp2.getRawHorizontalOverscanBBox()) 

self.assertEqual(amp1.getRawVerticalOverscanBBox(), amp2.getRawVerticalOverscanBBox()) 

 

def assertAmpRawBBoxesFlippablyEqual(self, amp1, amp2): 

"""Check that amp1 can be self-consistently transformed to match amp2. 

 

This method compares amplifier bounding boxes by confirming 

that they represent the same segment of the detector image. 

If the offsets or amplifier flips differ between the 

amplifiers, this method will pass even if the raw bounding 

boxes returned by the amplifier accessors are not equal. 

 

Parameters 

---------- 

amp1 : `~lsst.afw.cameraGeom.Amplifier` 

Amplifier to transform. 

amp2 : `~lsst.afw.cameraGeom.Amplifier` 

Reference amplifier. 

 

""" 

xFlip = amp1.getRawFlipX() ^ amp2.getRawFlipX() 

yFlip = amp1.getRawFlipY() ^ amp2.getRawFlipY() 

XYOffset = amp1.getRawXYOffset() - amp2.getRawXYOffset() 

 

testRawBox = amp1.getRawBBox() 

testHOSBox = amp1.getRawHorizontalOverscanBBox() 

testVOSBox = amp1.getRawVerticalOverscanBBox() 

 

if xFlip: 

size = amp1.getRawBBox().getWidth() 

testRawBox.flipLR(size) 

testHOSBox.flipLR(size) 

testVOSBox.flipLR(size) 

if yFlip: 

size = amp1.getRawBBox().getHeight() 

testRawBox.flipTB(size) 

testHOSBox.flipTB(size) 

testVOSBox.flipTB(size) 

 

testRawBox.shift(XYOffset) 

testHOSBox.shift(XYOffset) 

testVOSBox.shift(XYOffset) 

 

self.assertEqual(testRawBox, amp2.getRawBBox()) 

self.assertEqual(testHOSBox, amp2.getRawHorizontalOverscanBBox()) 

self.assertEqual(testVOSBox, amp2.getRawVerticalOverscanBBox()) 

 

def testGen2GetBadOverscan(self): 

"""Test that we can use the Gen2 Butler to read a file with overscan 

regions that disagree with cameraGeom, and that the detector attached 

to it has its overscan regions corrected. 

 

This is essentially just a regression test, and an incomplete one at 

that: the fixed Detector snapshot that we're comparing to was generated 

by the same code we're calling here. And because the LATISS 

associated by the Butler we use in this test may in the future be 

corrected to have the right overscan regions, we may end up just 

testing a simpler case than we intended. We'll use a snapshot of 

the incorrect Camera in other tests to get coverage of that case. 

""" 

butler = Butler(LATISS_DATA_ROOT) 

raw = butler.get("raw", dataId=BAD_OVERSCAN_GEN2_DATA_ID) 

for amp1, amp2 in zip(self.detectorFixed, raw.getDetector()): 

with self.subTest(amp=amp1.getName()): 

self.assertEqual(amp1.getName(), amp2.getName()) 

self.assertAmpRawBBoxesEqual(amp1, amp2) 

 

def testFixBadOverscans(self): 

"""Test the low-level code for repairing cameraGeom overscan regions 

that disagree with raw files. 

""" 

testFile = os.path.join(LATISS_DATA_ROOT, BAD_OVERSCAN_FILENAME) 

 

for i, (ampBad, ampGood) in enumerate(zip(self.cameraBroken[0], self.detectorFixed)): 

with self.subTest(amp=ampBad.getName()): 

self.assertEqual(ampBad.getName(), ampGood.getName()) 

hdu = i + 1 

reader = ImageFitsReader(testFile, hdu=hdu) 

metadata = reader.readMetadata() 

image = reader.read() 

self.assertEqual(ampGood.getRawBBox().getDimensions(), image.getBBox().getDimensions()) 

self.assertNotEqual(ampBad.getRawBBox().getDimensions(), image.getBBox().getDimensions()) 

newAmp, modified = fixAmpGeometry(ampBad, image.getBBox(), metadata) 

self.assertTrue(modified) 

self.assertNotEqual(newAmp.getRawBBox().getDimensions(), ampBad.getRawBBox().getDimensions()) 

self.assertAmpRawBBoxesFlippablyEqual(newAmp, ampGood) 

 

 

class ReadRawFileTestCase(lsst.utils.tests.TestCase): 

def testReadRawLatissFile(self): 

fileName = os.path.join(LATISS_DATA_ROOT, BAD_OVERSCAN_FILENAME) 

policy = os.path.join(PACKAGE_DIR, "policy", "latiss.yaml") 

camera = yamlCamera.makeCamera(policy) 

exposure = readRawFile(fileName, camera[0], dataId={"file": fileName}) 

self.assertIsInstance(exposure, lsst.afw.image.Exposure) 

md = exposure.getMetadata() 

self.assertIn("INSTRUME", md) 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

setup_module(sys.modules[__name__]) 

unittest.main()