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

# This file is part of afw. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

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

 

__all__ = ["Warper", "WarperConfig"] 

 

import lsst.pex.config as pexConfig 

import lsst.geom 

import lsst.afw.image as afwImage 

from . import mathLib 

 

 

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. 

 

Parameters 

---------- 

destWcs : `lsst.afw.geom.SkyWcs` 

WCS of warped exposure 

srcBBox : `lsst.geom.Box2I` 

parent bounding box of unwarped image 

srcWcs : `lsst.afw.geom.SkyWcs` 

WCS of unwarped image 

 

Returns 

------- 

destBBox: `lsst.geom.Box2I` 

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. 

 

Parameters 

---------- 

warpingKernelName : `str` 

see `WarperConfig.warpingKernelName` 

interpLength : `int`, optional 

``interpLength`` argument to `lsst.afw.math.warpExposure` 

cacheSize : `int`, optional 

size of computeCache 

maskWarpingKernelName : `str`, optional 

name of mask warping kernel (if ``""`` then use ``warpingKernelName``); 

see `WarperConfig.maskWarpingKernelName` 

growFullMask : `int`, optional 

mask bits to grow to full width of image/variance kernel 

""" 

ConfigClass = WarperConfig 

 

def __init__(self, 

warpingKernelName, 

interpLength=_DefaultInterpLength, 

cacheSize=_DefaultCacheSize, 

maskWarpingKernelName="", 

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

self._warpingControl = mathLib.WarpingControl( 

warpingKernelName, maskWarpingKernelName, cacheSize, interpLength, growFullMask) 

 

@classmethod 

def fromConfig(cls, config): 

"""Create a Warper from a config. 

 

Parameters 

---------- 

config : `WarperConfig` 

The config to initialize the Warper with. 

""" 

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. 

 

Parameters 

----------- 

destWcs : `lsst.afw.geom.SkyWcs` 

WCS of warped exposure 

srcExposure 

exposure to warp 

border : `int`, optional 

grow bbox of warped exposure by this amount in all directions 

(in pixels); if negative then the bbox is shrunk; border is applied 

before ``maxBBox``; ignored if ``destBBox`` is not `None` 

maxBBox : `lsst.geom.Box2I`, optional 

maximum allowed parent bbox of warped exposure; 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` 

destBBox : `lsst.geom.Box2I`, optional 

exact parent bbox of warped exposure; if `None` then ``border`` and 

``maxBBox`` are used to determine the bbox, otherwise ``border`` 

and ``maxBBox`` are ignored 

 

Returns 

------- 

destExposure : same type as ``srcExposure`` 

warped exposure 

 

Notes 

----- 

calls `lsst.afw.math.warpExposure` insted of `~Warper.warpImage` because the former 

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

 

The PSF is not warped. To warp the PSF, use `lsst.meas.algorithms.WarpedPsf` 

""" 

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. 

 

Parameters 

---------- 

destWcs : `lsst.afw.geom.SkyWcs` 

WCS of warped image 

srcImage 

image or masked image to warp 

srcWcs : `lsst.afw.geom.SkyWcs` 

WCS of image 

border : `int`, optional 

grow bbox of warped image by this amount in all directions 

(in pixels); if negative then the bbox is shrunk; border is applied 

before ``maxBBox``; ignored if ``destBBox`` is not `None` 

maxBBox : `lsst.geom.Box2I`, optional 

maximum allowed parent bbox of warped image; 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` 

destBBox : `lsst.geom.Box2I`, optional 

exact parent bbox of warped image; if `None` then ``border`` and 

``maxBBox`` are used to determine the bbox, otherwise ``border`` 

and ``maxBBox`` are ignored 

 

Returns 

------- 

destImage : same type as ``srcExposure`` 

warped image or masked image 

""" 

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. 

 

Parameters 

---------- 

destWcs : `lsst.afw.geom.SkyWcs` 

WCS of warped image 

srcImage 

image or masked image to warp 

srcWcs : `lsst.afw.geom.SkyWcs` 

WCS of image 

border : `int`, optional 

grow bbox of warped image by this amount in all directions 

(in pixels); if negative then the bbox is shrunk; border is applied 

before ``maxBBox``; ignored if ``destBBox`` is not `None` 

maxBBox : `lsst.geom.Box2I`, optional 

maximum allowed parent bbox of warped image; 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` 

destBBox : `lsst.geom.Box2I`, optional 

exact parent bbox of warped image; 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