lsst.afw  13.0-73-gb2a4e38
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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.MaskU.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.MaskU.getPlaneBitMask("EDGE"),
110  ):
111  """Create a Warper
112 
113  Inputs:
114  - warpingKernelName: argument to lsst.afw.math.makeWarpingKernel
115  - interpLength: interpLength argument to lsst.afw.warpExposure
116  - cacheSize: size of computeCache
117  - maskWarpingKernelName: name of mask warping kernel (if "" then use warpingKernelName);
118  an argument to lsst.afw.math.makeWarpingKernel
119  """
120  self._warpingControl = mathLib.WarpingControl(
121  warpingKernelName, maskWarpingKernelName, cacheSize, interpLength, growFullMask)
122 
123  @classmethod
124  def fromConfig(cls, config):
125  """Create a Warper from a config
126 
127  @param config: an instance of Warper.ConfigClass
128  """
129  return cls(
130  warpingKernelName = config.warpingKernelName,
131  maskWarpingKernelName = config.maskWarpingKernelName,
132  interpLength = config.interpLength,
133  cacheSize = config.cacheSize,
134  growFullMask = config.growFullMask,
135  )
136 
137  def getWarpingKernel(self):
138  """Get the warping kernel"""
139  return self._warpingControl.getWarpingKernel()
140 
142  """Get the mask warping kernel"""
143  return self._warpingControl.getMaskWarpingKernel()
144 
145  def warpExposure(self, destWcs, srcExposure, border=0, maxBBox=None, destBBox=None):
146  """Warp an exposure
147 
148  @param destWcs: WCS of warped exposure
149  @param srcExposure: exposure to warp
150  @param border: grow bbox of warped exposure by this amount in all directions (int pixels);
151  if negative then the bbox is shrunk;
152  border is applied before maxBBox;
153  ignored if destBBox is not None
154  @param maxBBox: maximum allowed parent bbox of warped exposure (an afwGeom.Box2I or None);
155  if None then the warped exposure will be just big enough to contain all warped pixels;
156  if provided then the warped exposure may be smaller, and so missing some warped pixels;
157  ignored if destBBox is not None
158  @param destBBox: exact parent bbox of warped exposure (an afwGeom.Box2I or None);
159  if None then border and maxBBox are used to determine the bbox,
160  otherwise border and maxBBox are ignored
161 
162  @return destExposure: warped exposure (of same type as srcExposure)
163 
164  @note: calls mathLib.warpExposure insted of self.warpImage because the former
165  copies attributes such as Calib, and that should be done in one place
166  """
167  destBBox = self._computeDestBBox(
168  destWcs = destWcs,
169  srcImage = srcExposure.getMaskedImage(),
170  srcWcs = srcExposure.getWcs(),
171  border = border,
172  maxBBox = maxBBox,
173  destBBox = destBBox,
174  )
175  destExposure = srcExposure.Factory(destBBox, destWcs)
176  mathLib.warpExposure(destExposure, srcExposure, self._warpingControl)
177  return destExposure
178 
179  def warpImage(self, destWcs, srcImage, srcWcs, border=0, maxBBox=None, destBBox=None):
180  """Warp an image or masked image
181 
182  @param destWcs: WCS of warped image
183  @param srcImage: image or masked image to warp
184  @param srcWcs: WCS of image
185  @param border: grow bbox of warped image by this amount in all directions (int pixels);
186  if negative then the bbox is shrunk;
187  border is applied before maxBBox;
188  ignored if destBBox is not None
189  @param maxBBox: maximum allowed parent bbox of warped image (an afwGeom.Box2I or None);
190  if None then the warped image will be just big enough to contain all warped pixels;
191  if provided then the warped image may be smaller, and so missing some warped pixels;
192  ignored if destBBox is not None
193  @param destBBox: exact parent bbox of warped image (an afwGeom.Box2I or None);
194  if None then border and maxBBox are used to determine the bbox,
195  otherwise border and maxBBox are ignored
196 
197  @return destImage: warped image or masked image (of same type as srcImage)
198  """
199  destBBox = self._computeDestBBox(
200  destWcs = destWcs,
201  srcImage = srcImage,
202  srcWcs = srcWcs,
203  border = border,
204  maxBBox = maxBBox,
205  destBBox = destBBox,
206  )
207  destImage = srcImage.Factory(destBBox)
208  mathLib.warpImage(destImage, destWcs, srcImage,
209  srcWcs, self._warpingControl)
210  return destImage
211 
212  def _computeDestBBox(self, destWcs, srcImage, srcWcs, border, maxBBox, destBBox):
213  """Process destBBox argument for warpImage and warpExposure
214 
215  @param destWcs: WCS of warped image
216  @param srcImage: image or masked image to warp
217  @param srcWcs: WCS of image
218  @param border: grow bbox of warped image by this amount in all directions (int pixels);
219  if negative then the bbox is shrunk;
220  border is applied before maxBBox;
221  ignored if destBBox is not None
222  @param maxBBox: maximum allowed parent bbox of warped image (an afwGeom.Box2I or None);
223  if None then the warped image will be just big enough to contain all warped pixels;
224  if provided then the warped image may be smaller, and so missing some warped pixels;
225  ignored if destBBox is not None
226  @param destBBox: exact parent bbox of warped image (an afwGeom.Box2I or None);
227  if None then border and maxBBox are used to determine the bbox,
228  otherwise border and maxBBox are ignored
229  """
230  if destBBox is None: # warning: == None fails due to Box2I.__eq__
231  destBBox = computeWarpedBBox(
232  destWcs, srcImage.getBBox(afwImage.PARENT), srcWcs)
233  if border:
234  destBBox.grow(border)
235  if maxBBox is not None:
236  destBBox.clip(maxBBox)
237  return destBBox
An integer coordinate rectangle.
Definition: Box.h:55
A floating-point coordinate rectangle geometry.
Definition: Box.h:266