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

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

# 

# LSST Data Management System 

# Copyright 2008, 2009, 2010 LSST Corporation. 

# 

# This product includes software developed by the 

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

# 

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

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

# see <http://www.lsstcorp.org/LegalNotices/>. 

# 

 

 

# test1079 

# @brief Test that the wcs of sub-images are written and read from disk correctly 

# $Id$ 

# @author Fergal Mullally 

 

import os.path 

import unittest 

import numbers 

 

import lsst.utils 

import lsst.geom 

import lsst.afw.image as afwImage 

from lsst.afw.fits import readMetadata 

import lsst.utils.tests 

import lsst.pex.exceptions 

import lsst.afw.display.ds9 as ds9 

 

try: 

type(verbose) 

except NameError: 

verbose = 0 

 

 

class SavingSubImagesTest(unittest.TestCase): 

""" 

Tests for changes made for ticket #1079. In the LSST wcs transformations are done in terms 

of pixel position, which is measured from the lower left hand corner of the parent image from 

which this sub-image is drawn. However, when saving a sub-image to disk, the fits standards 

has no concept of parent- and sub- images, and specifies that the wcs is measured relative to 

the pixel index (i.e the lower left hand corner of the sub-image). This test makes sure 

we're saving and reading wcs headers from sub-images correctly. 

""" 

 

def setUp(self): 

path = lsst.utils.getPackageDir("afw") 

self.parentFile = os.path.join(path, "tests", "data", "parent.fits") 

 

self.parent = afwImage.ExposureF(self.parentFile) 

self.llcParent = self.parent.getMaskedImage().getXY0() 

self.oParent = self.parent.getWcs().getPixelOrigin() 

 

# A list of pixel positions to test 

self.testPositions = [] 

self.testPositions.append(lsst.geom.Point2D(128, 128)) 

self.testPositions.append(lsst.geom.Point2D(0, 0)) 

self.testPositions.append(lsst.geom.Point2D(20, 30)) 

self.testPositions.append(lsst.geom.Point2D(60, 50)) 

self.testPositions.append(lsst.geom.Point2D(80, 80)) 

self.testPositions.append(lsst.geom.Point2D(255, 255)) 

 

self.parent.getMaskedImage().set(0) 

for p in self.testPositions: 

self.parent.getMaskedImage().set( 

int(p[0]), int(p[1]), (10 + p[0],)) 

 

def tearDown(self): 

del self.parent 

del self.oParent 

del self.testPositions 

 

def testInvarianceOfCrpix1(self): 

"""Test that crpix is the same for parent and sub-image. Also tests that llc of sub-image 

saved correctly""" 

 

llc = lsst.geom.Point2I(20, 30) 

bbox = lsst.geom.Box2I(llc, lsst.geom.Extent2I(60, 50)) 

subImg = afwImage.ExposureF(self.parent, bbox, afwImage.LOCAL) 

 

subImgLlc = subImg.getMaskedImage().getXY0() 

oSubImage = subImg.getWcs().getPixelOrigin() 

 

# Useful for debugging 

if False: 

print(self.parent.getMaskedImage().getXY0()) 

print(subImg.getMaskedImage().getXY0()) 

print(self.oParent, oSubImage) 

 

for i in range(2): 

self.assertEqual(llc[i], subImgLlc[i], 

"Corner of sub-image not correct") 

self.assertAlmostEqual( 

self.oParent[i], oSubImage[i], 6, "Crpix of sub-image not correct") 

 

def testInvarianceOfCrpix2(self): 

"""For sub-images loaded from disk, test that crpix is the same for parent and sub-image. 

Also tests that llc of sub-image saved correctly""" 

 

# Load sub-image directly off of disk 

llc = lsst.geom.Point2I(20, 30) 

bbox = lsst.geom.Box2I(llc, lsst.geom.Extent2I(60, 50)) 

subImg = afwImage.ExposureF(self.parentFile, bbox, afwImage.LOCAL) 

oSubImage = subImg.getWcs().getPixelOrigin() 

subImgLlc = subImg.getMaskedImage().getXY0() 

 

# Useful for debugging 

if False: 

print(self.parent.getMaskedImage().getXY0()) 

print(subImg.getMaskedImage().getXY0()) 

print(self.oParent, oSubImage) 

 

for i in range(2): 

self.assertEqual(llc[i], subImgLlc[i], 

"Corner of sub-image not correct") 

self.assertAlmostEqual( 

self.oParent[i], oSubImage[i], 6, "Crpix of sub-image not correct") 

 

def testInvarianceOfPixelToSky(self): 

 

for deep in (True, False): 

llc = lsst.geom.Point2I(20, 30) 

bbox = lsst.geom.Box2I(llc, lsst.geom.Extent2I(60, 50)) 

subImg = afwImage.ExposureF( 

self.parent, bbox, afwImage.LOCAL, deep) 

 

xy0 = subImg.getMaskedImage().getXY0() 

 

if False: 

ds9.mtv(self.parent, frame=0) 

ds9.mtv(subImg, frame=1) 

 

for p in self.testPositions: 

subP = p - lsst.geom.Extent2D(llc[0], llc[1]) # pixel in subImg 

 

147 ↛ 150line 147 didn't jump to line 150 if \ 

subP[0] < 0 or subP[0] >= bbox.getWidth() or \ 

subP[1] < 0 or subP[1] >= bbox.getHeight(): 

continue 

 

adParent = self.parent.getWcs().pixelToSky(p) 

adSub = subImg.getWcs().pixelToSky( 

subP + lsst.geom.Extent2D(xy0[0], xy0[1])) 

# 

# Check that we're talking about the same pixel 

# 

self.assertEqual(self.parent.getMaskedImage().get(int(p[0]), int(p[1])), 

subImg.getMaskedImage().get(int(subP[0]), int(subP[1]))) 

 

self.assertEqual(adParent[0], adSub[0], 

"RAs are equal; deep = %s" % deep) 

self.assertEqual(adParent[1], adSub[1], 

"DECs are equal; deep = %s" % deep) 

 

def testSubSubImage(self): 

"""Check that a sub-image of a sub-image is equivalent to a sub image, i.e 

that the parent is an invarient""" 

 

llc1 = lsst.geom.Point2I(20, 30) 

bbox = lsst.geom.Box2I(llc1, lsst.geom.Extent2I(60, 50)) 

subImg = afwImage.ExposureF(self.parentFile, bbox, afwImage.LOCAL) 

 

llc2 = lsst.geom.Point2I(22, 23) 

 

# This subsub image should fail. Although it's big enough to fit in the parent image 

# it's too small for the sub-image 

bbox = lsst.geom.Box2I(llc2, lsst.geom.Extent2I(100, 110)) 

self.assertRaises(lsst.pex.exceptions.Exception, 

afwImage.ExposureF, subImg, bbox, afwImage.LOCAL) 

 

bbox = lsst.geom.Box2I(llc2, lsst.geom.Extent2I(10, 11)) 

subSubImg = afwImage.ExposureF(subImg, bbox, afwImage.LOCAL) 

 

sub0 = subImg.getMaskedImage().getXY0() 

subsub0 = subSubImg.getMaskedImage().getXY0() 

 

if False: 

print(sub0) 

print(subsub0) 

 

for i in range(2): 

self.assertEqual(llc1[i], sub0[i], "XY0 don't match (1)") 

self.assertEqual(llc1[i] + llc2[i], 

subsub0[i], "XY0 don't match (2)") 

 

subCrpix = subImg.getWcs().getPixelOrigin() 

subsubCrpix = subSubImg.getWcs().getPixelOrigin() 

 

for i in range(2): 

self.assertAlmostEqual( 

subCrpix[i], subsubCrpix[i], 6, "crpix don't match") 

 

def testRoundTrip(self): 

"""Test that saving and retrieving an image doesn't alter the metadata""" 

llc = lsst.geom.Point2I(20, 30) 

bbox = lsst.geom.Box2I(llc, lsst.geom.Extent2I(60, 50)) 

for deep in (False, True): 

subImg = afwImage.ExposureF( 

self.parent, bbox, afwImage.LOCAL, deep) 

 

with lsst.utils.tests.getTempFilePath("_%s.fits" % (deep,)) as outFile: 

subImg.writeFits(outFile) 

newImg = afwImage.ExposureF(outFile) 

 

subXY0 = subImg.getMaskedImage().getXY0() 

newXY0 = newImg.getMaskedImage().getXY0() 

 

self.parent.getWcs().getPixelOrigin() 

subCrpix = subImg.getWcs().getPixelOrigin() 

newCrpix = newImg.getWcs().getPixelOrigin() 

 

for i in range(2): 

self.assertEqual( 

subXY0[i], newXY0[i], "Origin has changed; deep = %s" % deep) 

self.assertAlmostEqual( 

subCrpix[i], newCrpix[i], 6, "crpix has changed; deep = %s" % deep) 

 

def testFitsHeader(self): 

"""Test that XY0 and crpix are written to the header as expected""" 

 

# getPixelOrigin() returns origin in lsst coordinates, so need to add 1 to 

# compare to values stored in fits headers 

parentCrpix = self.parent.getWcs().getPixelOrigin() 

 

# Make a sub-image 

x0, y0 = 20, 30 

llc = lsst.geom.Point2I(x0, y0) 

bbox = lsst.geom.Box2I(llc, lsst.geom.Extent2I(60, 50)) 

deep = False 

subImg = afwImage.ExposureF(self.parent, bbox, afwImage.LOCAL, deep) 

 

with lsst.utils.tests.getTempFilePath(".fits") as outFile: 

subImg.writeFits(outFile) 

hdr = readMetadata(outFile) 

 

def checkLtvHeader(hdr, name, value): 

# Per DM-4133, LTVn headers are required to be floating point 

self.assertTrue(hdr.exists(name), name + 

" not saved to FITS header") 

self.assertIsInstance( 

hdr.get(name), numbers.Real, name + " is not numeric") 

self.assertNotIsInstance( 

hdr.get(name), numbers.Integral, name + " is an int") 

self.assertEqual(hdr.get(name), value, 

name + " has wrong value") 

 

checkLtvHeader(hdr, "LTV1", -1*x0) 

checkLtvHeader(hdr, "LTV2", -1*y0) 

 

self.assertTrue(hdr.exists("CRPIX1"), 

"CRPIX1 not saved to fits header") 

self.assertTrue(hdr.exists("CRPIX2"), 

"CRPIX2 not saved to fits header") 

 

fitsCrpix = [hdr.get("CRPIX1"), hdr.get("CRPIX2")] 

self.assertAlmostEqual( 

fitsCrpix[0] - hdr.get("LTV1"), parentCrpix[0]+1, 6, "CRPIX1 saved wrong") 

self.assertAlmostEqual( 

fitsCrpix[1] - hdr.get("LTV2"), parentCrpix[1]+1, 6, "CRPIX2 saved wrong") 

 

 

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

pass 

 

 

def setup_module(module): 

lsst.utils.tests.init() 

 

 

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

lsst.utils.tests.init() 

unittest.main()