lsst.afw  13.0-109-g62eb66483
warper.py
Go to the documentation of this file.
1 from __future__ import absolute_import, division, print_function
2 from builtins import object
3 #
4 # LSST Data Management System
5 # Copyright 2008, 2009, 2010 LSST Corporation.
6 #
7 # This product includes software developed by the
8 # LSST Project (http://www.lsst.org/).
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the LSST License Statement and
21 # the GNU General Public License along with this program. If not,
22 # see <http://www.lsstcorp.org/LegalNotices/>.
23 #
24 import lsst.pex.config as pexConfig
25 import lsst.afw.geom as afwGeom
26 import lsst.afw.image as afwImage
27 from . import mathLib
28 
29 __all__ = ["Warper", "WarperConfig"]
30 
31 
32 def computeWarpedBBox(destWcs, srcBBox, srcWcs):
33  """Compute the bounding box of a warped image
34 
35  The bounding box includes all warped pixels and it may be a bit oversize.
36 
37  @param destWcs: WCS of warped exposure
38  @param srcBBox: parent bounding box of unwarped image
39  @param srcWcs: WCS of unwarped image
40 
41  @return destBBox: bounding box of warped exposure
42  """
43  srcPosBox = afwGeom.Box2D(srcBBox)
44  destPosBox = afwGeom.Box2D()
45  for inX in (srcPosBox.getMinX(), srcPosBox.getMaxX()):
46  for inY in (srcPosBox.getMinY(), srcPosBox.getMaxY()):
47  destPos = destWcs.skyToPixel(
48  srcWcs.pixelToSky(afwGeom.Point2D(inX, inY)))
49  destPosBox.include(destPos)
50  destBBox = afwGeom.Box2I(destPosBox, afwGeom.Box2I.EXPAND)
51  return destBBox
52 
53 
54 _DefaultInterpLength = 10
55 _DefaultCacheSize = 1000000
56 
57 
58 class WarperConfig(pexConfig.Config):
59  warpingKernelName = pexConfig.ChoiceField(
60  dtype = str,
61  doc = "Warping kernel",
62  default = "lanczos3",
63  allowed = {
64  "bilinear": "bilinear interpolation",
65  "lanczos3": "Lanczos kernel of order 3",
66  "lanczos4": "Lanczos kernel of order 4",
67  "lanczos5": "Lanczos kernel of order 5",
68  }
69  )
70  maskWarpingKernelName = pexConfig.ChoiceField(
71  dtype = str,
72  doc = "Warping kernel for mask (use warpingKernelName if '')",
73  default = "bilinear",
74  allowed = {
75  "": "use the regular warping kernel for the mask plane, as well as the image and variance planes",
76  "bilinear": "bilinear interpolation",
77  "lanczos3": "Lanczos kernel of order 3",
78  "lanczos4": "Lanczos kernel of order 4",
79  "lanczos5": "Lanczos kernel of order 5",
80  }
81  )
82  interpLength = pexConfig.Field(
83  dtype = int,
84  doc = "interpLength argument to lsst.afw.math.warpExposure",
85  default = _DefaultInterpLength,
86  )
87  cacheSize = pexConfig.Field(
88  dtype = int,
89  doc = "cacheSize argument to lsst.afw.math.SeparableKernel.computeCache",
90  default = _DefaultCacheSize,
91  )
92  growFullMask = pexConfig.Field(
93  dtype = int,
94  doc = "mask bits to grow to full width of image/variance kernel,",
95  default = afwImage.Mask.getPlaneBitMask("EDGE"),
96  )
97 
98 
99 class Warper(object):
100  """Warp images
101  """
102  ConfigClass = WarperConfig
103 
104  def __init__(self,
105  warpingKernelName,
106  interpLength = _DefaultInterpLength,
107  cacheSize = _DefaultCacheSize,
108  maskWarpingKernelName = "",
109  growFullMask = afwImage.Mask.getPlaneBitMask("EDGE"),):
110  """Create a Warper
111 
112  Inputs:
113  - warpingKernelName: argument to lsst.afw.math.makeWarpingKernel
114  - interpLength: interpLength argument to lsst.afw.warpExposure
115  - cacheSize: size of computeCache
116  - maskWarpingKernelName: name of mask warping kernel (if "" then use warpingKernelName);
117  an argument to lsst.afw.math.makeWarpingKernel
118  """
119  self._warpingControl = mathLib.WarpingControl(
120  warpingKernelName, maskWarpingKernelName, cacheSize, interpLength, growFullMask)
121 
122  @classmethod
123  def fromConfig(cls, config):
124  """Create a Warper from a config
125 
126  @param config: an instance of Warper.ConfigClass
127  """
128  return cls(
129  warpingKernelName = config.warpingKernelName,
130  maskWarpingKernelName = config.maskWarpingKernelName,
131  interpLength = config.interpLength,
132  cacheSize = config.cacheSize,
133  growFullMask = config.growFullMask,
134  )
135 
136  def getWarpingKernel(self):
137  """Get the warping kernel"""
138  return self._warpingControl.getWarpingKernel()
139 
141  """Get the mask warping kernel"""
143 
144  def warpExposure(self, destWcs, srcExposure, border=0, maxBBox=None, destBBox=None):
145  """Warp an exposure
146 
147  @param destWcs: WCS of warped exposure
148  @param srcExposure: exposure to warp
149  @param border: grow bbox of warped exposure by this amount in all directions (int pixels);
150  if negative then the bbox is shrunk;
151  border is applied before maxBBox;
152  ignored if destBBox is not None
153  @param maxBBox: maximum allowed parent bbox of warped exposure (an afwGeom.Box2I or None);
154  if None then the warped exposure will be just big enough to contain all warped pixels;
155  if provided then the warped exposure may be smaller, and so missing some warped pixels;
156  ignored if destBBox is not None
157  @param destBBox: exact parent bbox of warped exposure (an afwGeom.Box2I or None);
158  if None then border and maxBBox are used to determine the bbox,
159  otherwise border and maxBBox are ignored
160 
161  @return destExposure: warped exposure (of same type as srcExposure)
162 
163  @note: calls mathLib.warpExposure insted of self.warpImage because the former
164  copies attributes such as Calib, and that should be done in one place
165  """
166  destBBox = self._computeDestBBox(
167  destWcs = destWcs,
168  srcImage = srcExposure.getMaskedImage(),
169  srcWcs = srcExposure.getWcs(),
170  border = border,
171  maxBBox = maxBBox,
172  destBBox = destBBox,
173  )
174  destExposure = srcExposure.Factory(destBBox, destWcs)
175  mathLib.warpExposure(destExposure, srcExposure, self._warpingControl)
176  return destExposure
177 
178  def warpImage(self, destWcs, srcImage, srcWcs, border=0, maxBBox=None, destBBox=None):
179  """Warp an image or masked image
180 
181  @param destWcs: WCS of warped image
182  @param srcImage: image or masked image to warp
183  @param srcWcs: WCS of image
184  @param border: grow bbox of warped image by this amount in all directions (int pixels);
185  if negative then the bbox is shrunk;
186  border is applied before maxBBox;
187  ignored if destBBox is not None
188  @param maxBBox: maximum allowed parent bbox of warped image (an afwGeom.Box2I or None);
189  if None then the warped image will be just big enough to contain all warped pixels;
190  if provided then the warped image may be smaller, and so missing some warped pixels;
191  ignored if destBBox is not None
192  @param destBBox: exact parent bbox of warped image (an afwGeom.Box2I or None);
193  if None then border and maxBBox are used to determine the bbox,
194  otherwise border and maxBBox are ignored
195 
196  @return destImage: warped image or masked image (of same type as srcImage)
197  """
198  destBBox = self._computeDestBBox(
199  destWcs = destWcs,
200  srcImage = srcImage,
201  srcWcs = srcWcs,
202  border = border,
203  maxBBox = maxBBox,
204  destBBox = destBBox,
205  )
206  destImage = srcImage.Factory(destBBox)
207  mathLib.warpImage(destImage, destWcs, srcImage,
208  srcWcs, self._warpingControl)
209  return destImage
210 
211  def _computeDestBBox(self, destWcs, srcImage, srcWcs, border, maxBBox, destBBox):
212  """Process destBBox argument for warpImage and warpExposure
213 
214  @param destWcs: WCS of warped image
215  @param srcImage: image or masked image to warp
216  @param srcWcs: WCS of image
217  @param border: grow bbox of warped image by this amount in all directions (int pixels);
218  if negative then the bbox is shrunk;
219  border is applied before maxBBox;
220  ignored if destBBox is not None
221  @param maxBBox: maximum allowed parent bbox of warped image (an afwGeom.Box2I or None);
222  if None then the warped image will be just big enough to contain all warped pixels;
223  if provided then the warped image may be smaller, and so missing some warped pixels;
224  ignored if destBBox is not None
225  @param destBBox: exact parent bbox of warped image (an afwGeom.Box2I or None);
226  if None then border and maxBBox are used to determine the bbox,
227  otherwise border and maxBBox are ignored
228  """
229  if destBBox is None: # warning: == None fails due to Box2I.__eq__
230  destBBox = computeWarpedBBox(
231  destWcs, srcImage.getBBox(afwImage.PARENT), srcWcs)
232  if border:
233  destBBox.grow(border)
234  if maxBBox is not None:
235  destBBox.clip(maxBBox)
236  return destBBox
def _computeDestBBox(self, destWcs, srcImage, srcWcs, border, maxBBox, destBBox)
Definition: warper.py:211
def getMaskWarpingKernel(self)
Definition: warper.py:140
def warpExposure(self, destWcs, srcExposure, border=0, maxBBox=None, destBBox=None)
Definition: warper.py:144
def computeWarpedBBox(destWcs, srcBBox, srcWcs)
Definition: warper.py:32
An integer coordinate rectangle.
Definition: Box.h:55
def __init__(self, warpingKernelName, interpLength=_DefaultInterpLength, cacheSize=_DefaultCacheSize, maskWarpingKernelName="", growFullMask=afwImage.Mask.getPlaneBitMask("EDGE"))
Definition: warper.py:109
def warpImage(self, destWcs, srcImage, srcWcs, border=0, maxBBox=None, destBBox=None)
Definition: warper.py:178
def fromConfig(cls, config)
Definition: warper.py:123
A floating-point coordinate rectangle geometry.
Definition: Box.h:266