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

# 

# 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/>. 

# 

import lsst.pex.config as pexConfig 

import lsst.geom 

import lsst.afw.image as afwImage 

from . import mathLib 

 

__all__ = ["Warper", "WarperConfig"] 

 

 

def computeWarpedBBox(destWcs, srcBBox, srcWcs): 

"""Compute the bounding box of a warped image 

 

The bounding box includes all warped pixels and it may be a bit oversize. 

 

@param destWcs: WCS of warped exposure, an lsst.afw.geom.SkyWcs 

@param srcBBox: parent bounding box of unwarped image 

@param srcWcs: WCS of unwarped image, an lsst.afw.geom.SkyWcs 

 

@return destBBox: bounding box of warped exposure 

""" 

srcPosBox = lsst.geom.Box2D(srcBBox) 

destPosBox = lsst.geom.Box2D() 

for inX in (srcPosBox.getMinX(), srcPosBox.getMaxX()): 

for inY in (srcPosBox.getMinY(), srcPosBox.getMaxY()): 

destPos = destWcs.skyToPixel(srcWcs.pixelToSky(inX, inY)) 

destPosBox.include(destPos) 

destBBox = lsst.geom.Box2I(destPosBox, lsst.geom.Box2I.EXPAND) 

return destBBox 

 

 

_DefaultInterpLength = 10 

_DefaultCacheSize = 1000000 

 

 

class WarperConfig(pexConfig.Config): 

warpingKernelName = pexConfig.ChoiceField( 

dtype=str, 

doc="Warping kernel", 

default="lanczos3", 

allowed={ 

"bilinear": "bilinear interpolation", 

"lanczos3": "Lanczos kernel of order 3", 

"lanczos4": "Lanczos kernel of order 4", 

"lanczos5": "Lanczos kernel of order 5", 

} 

) 

maskWarpingKernelName = pexConfig.ChoiceField( 

dtype=str, 

doc="Warping kernel for mask (use warpingKernelName if '')", 

default="bilinear", 

allowed={ 

"": "use the regular warping kernel for the mask plane, as well as the image and variance planes", 

"bilinear": "bilinear interpolation", 

"lanczos3": "Lanczos kernel of order 3", 

"lanczos4": "Lanczos kernel of order 4", 

"lanczos5": "Lanczos kernel of order 5", 

} 

) 

interpLength = pexConfig.Field( 

dtype=int, 

doc="interpLength argument to lsst.afw.math.warpExposure", 

default=_DefaultInterpLength, 

) 

cacheSize = pexConfig.Field( 

dtype=int, 

doc="cacheSize argument to lsst.afw.math.SeparableKernel.computeCache", 

default=_DefaultCacheSize, 

) 

growFullMask = pexConfig.Field( 

dtype=int, 

doc="mask bits to grow to full width of image/variance kernel,", 

default=afwImage.Mask.getPlaneBitMask("EDGE"), 

) 

 

 

class Warper: 

"""Warp images 

""" 

ConfigClass = WarperConfig 

 

def __init__(self, 

warpingKernelName, 

interpLength=_DefaultInterpLength, 

cacheSize=_DefaultCacheSize, 

maskWarpingKernelName="", 

growFullMask=afwImage.Mask.getPlaneBitMask("EDGE"),): 

"""Create a Warper 

 

Inputs: 

- warpingKernelName: argument to lsst.afw.math.makeWarpingKernel 

- interpLength: interpLength argument to lsst.afw.warpExposure 

- cacheSize: size of computeCache 

- maskWarpingKernelName: name of mask warping kernel (if "" then use warpingKernelName); 

an argument to lsst.afw.math.makeWarpingKernel 

""" 

self._warpingControl = mathLib.WarpingControl( 

warpingKernelName, maskWarpingKernelName, cacheSize, interpLength, growFullMask) 

 

@classmethod 

def fromConfig(cls, config): 

"""Create a Warper from a config 

 

@param config: an instance of Warper.ConfigClass 

""" 

return cls( 

warpingKernelName=config.warpingKernelName, 

maskWarpingKernelName=config.maskWarpingKernelName, 

interpLength=config.interpLength, 

cacheSize=config.cacheSize, 

growFullMask=config.growFullMask, 

) 

 

def getWarpingKernel(self): 

"""Get the warping kernel""" 

return self._warpingControl.getWarpingKernel() 

 

def getMaskWarpingKernel(self): 

"""Get the mask warping kernel""" 

return self._warpingControl.getMaskWarpingKernel() 

 

def warpExposure(self, destWcs, srcExposure, border=0, maxBBox=None, destBBox=None): 

"""Warp an exposure 

 

@param destWcs: WCS of warped exposure, an lsst.afw.geom.SkyWcs 

@param srcExposure: exposure to warp 

@param border: grow bbox of warped exposure by this amount in all directions (int pixels); 

if negative then the bbox is shrunk; 

border is applied before maxBBox; 

ignored if destBBox is not None 

@param maxBBox: maximum allowed parent bbox of warped exposure (an lsst.geom.Box2I or None); 

if None then the warped exposure will be just big enough to contain all warped pixels; 

if provided then the warped exposure may be smaller, and so missing some warped pixels; 

ignored if destBBox is not None 

@param destBBox: exact parent bbox of warped exposure (an lsst.geom.Box2I or None); 

if None then border and maxBBox are used to determine the bbox, 

otherwise border and maxBBox are ignored 

 

@return destExposure: warped exposure (of same type as srcExposure) 

 

@note: calls mathLib.warpExposure insted of self.warpImage because the former 

copies attributes such as Calib, and that should be done in one place 

""" 

destBBox = self._computeDestBBox( 

destWcs=destWcs, 

srcImage=srcExposure.getMaskedImage(), 

srcWcs=srcExposure.getWcs(), 

border=border, 

maxBBox=maxBBox, 

destBBox=destBBox, 

) 

destExposure = srcExposure.Factory(destBBox, destWcs) 

mathLib.warpExposure(destExposure, srcExposure, self._warpingControl) 

return destExposure 

 

def warpImage(self, destWcs, srcImage, srcWcs, border=0, maxBBox=None, destBBox=None): 

"""Warp an image or masked image 

 

@param destWcs: WCS of warped image, an lsst.afw.geom.SkyWcs 

@param srcImage: image or masked image to warp 

@param srcWcs: WCS of image, an lsst.afw.geom.SkyWcs 

@param border: grow bbox of warped image by this amount in all directions (int pixels); 

if negative then the bbox is shrunk; 

border is applied before maxBBox; 

ignored if destBBox is not None 

@param maxBBox: maximum allowed parent bbox of warped image (an lsst.geom.Box2I or None); 

if None then the warped image will be just big enough to contain all warped pixels; 

if provided then the warped image may be smaller, and so missing some warped pixels; 

ignored if destBBox is not None 

@param destBBox: exact parent bbox of warped image (an lsst.geom.Box2I or None); 

if None then border and maxBBox are used to determine the bbox, 

otherwise border and maxBBox are ignored 

 

@return destImage: warped image or masked image (of same type as srcImage) 

""" 

destBBox = self._computeDestBBox( 

destWcs=destWcs, 

srcImage=srcImage, 

srcWcs=srcWcs, 

border=border, 

maxBBox=maxBBox, 

destBBox=destBBox, 

) 

destImage = srcImage.Factory(destBBox) 

mathLib.warpImage(destImage, destWcs, srcImage, 

srcWcs, self._warpingControl) 

return destImage 

 

def _computeDestBBox(self, destWcs, srcImage, srcWcs, border, maxBBox, destBBox): 

"""Process destBBox argument for warpImage and warpExposure 

 

@param destWcs: WCS of warped image, an lsst.afw.geom.SkyWcs 

@param srcImage: image or masked image to warp 

@param srcWcs: WCS of image, an lsst.afw.geom.SkyWcs 

@param border: grow bbox of warped image by this amount in all directions (int pixels); 

if negative then the bbox is shrunk; 

border is applied before maxBBox; 

ignored if destBBox is not None 

@param maxBBox: maximum allowed parent bbox of warped image (an lsst.geom.Box2I or None); 

if None then the warped image will be just big enough to contain all warped pixels; 

if provided then the warped image may be smaller, and so missing some warped pixels; 

ignored if destBBox is not None 

@param destBBox: exact parent bbox of warped image (an lsst.geom.Box2I or None); 

if None then border and maxBBox are used to determine the bbox, 

otherwise border and maxBBox are ignored 

""" 

if destBBox is None: # warning: == None fails due to Box2I.__eq__ 

destBBox = computeWarpedBBox( 

destWcs, srcImage.getBBox(afwImage.PARENT), srcWcs) 

if border: 

destBBox.grow(border) 

if maxBBox is not None: 

destBBox.clip(maxBBox) 

return destBBox