lsst.afw  14.0-49-g535656444+1
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, an lsst.afw.geom.SkyWcs
38  @param srcBBox: parent bounding box of unwarped image
39  @param srcWcs: WCS of unwarped image, an lsst.afw.geom.SkyWcs
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(srcWcs.pixelToSky(inX, inY))
48  destPosBox.include(destPos)
49  destBBox = afwGeom.Box2I(destPosBox, afwGeom.Box2I.EXPAND)
50  return destBBox
51 
52 
53 _DefaultInterpLength = 10
54 _DefaultCacheSize = 1000000
55 
56 
57 class WarperConfig(pexConfig.Config):
58  warpingKernelName = pexConfig.ChoiceField(
59  dtype = str,
60  doc = "Warping kernel",
61  default = "lanczos3",
62  allowed = {
63  "bilinear": "bilinear interpolation",
64  "lanczos3": "Lanczos kernel of order 3",
65  "lanczos4": "Lanczos kernel of order 4",
66  "lanczos5": "Lanczos kernel of order 5",
67  }
68  )
69  maskWarpingKernelName = pexConfig.ChoiceField(
70  dtype = str,
71  doc = "Warping kernel for mask (use warpingKernelName if '')",
72  default = "bilinear",
73  allowed = {
74  "": "use the regular warping kernel for the mask plane, as well as the image and variance planes",
75  "bilinear": "bilinear interpolation",
76  "lanczos3": "Lanczos kernel of order 3",
77  "lanczos4": "Lanczos kernel of order 4",
78  "lanczos5": "Lanczos kernel of order 5",
79  }
80  )
81  interpLength = pexConfig.Field(
82  dtype = int,
83  doc = "interpLength argument to lsst.afw.math.warpExposure",
84  default = _DefaultInterpLength,
85  )
86  cacheSize = pexConfig.Field(
87  dtype = int,
88  doc = "cacheSize argument to lsst.afw.math.SeparableKernel.computeCache",
89  default = _DefaultCacheSize,
90  )
91  growFullMask = pexConfig.Field(
92  dtype = int,
93  doc = "mask bits to grow to full width of image/variance kernel,",
94  default = afwImage.Mask.getPlaneBitMask("EDGE"),
95  )
96 
97 
98 class Warper(object):
99  """Warp images
100  """
101  ConfigClass = WarperConfig
102 
103  def __init__(self,
104  warpingKernelName,
105  interpLength = _DefaultInterpLength,
106  cacheSize = _DefaultCacheSize,
107  maskWarpingKernelName = "",
108  growFullMask = afwImage.Mask.getPlaneBitMask("EDGE"),):
109  """Create a Warper
110 
111  Inputs:
112  - warpingKernelName: argument to lsst.afw.math.makeWarpingKernel
113  - interpLength: interpLength argument to lsst.afw.warpExposure
114  - cacheSize: size of computeCache
115  - maskWarpingKernelName: name of mask warping kernel (if "" then use warpingKernelName);
116  an argument to lsst.afw.math.makeWarpingKernel
117  """
118  self._warpingControl = mathLib.WarpingControl(
119  warpingKernelName, maskWarpingKernelName, cacheSize, interpLength, growFullMask)
120 
121  @classmethod
122  def fromConfig(cls, config):
123  """Create a Warper from a config
124 
125  @param config: an instance of Warper.ConfigClass
126  """
127  return cls(
128  warpingKernelName = config.warpingKernelName,
129  maskWarpingKernelName = config.maskWarpingKernelName,
130  interpLength = config.interpLength,
131  cacheSize = config.cacheSize,
132  growFullMask = config.growFullMask,
133  )
134 
135  def getWarpingKernel(self):
136  """Get the warping kernel"""
137  return self._warpingControl.getWarpingKernel()
138 
140  """Get the mask warping kernel"""
142 
143  def warpExposure(self, destWcs, srcExposure, border=0, maxBBox=None, destBBox=None):
144  """Warp an exposure
145 
146  @param destWcs: WCS of warped exposure, an lsst.afw.geom.SkyWcs
147  @param srcExposure: exposure to warp
148  @param border: grow bbox of warped exposure by this amount in all directions (int pixels);
149  if negative then the bbox is shrunk;
150  border is applied before maxBBox;
151  ignored if destBBox is not None
152  @param maxBBox: maximum allowed parent bbox of warped exposure (an afwGeom.Box2I or None);
153  if None then the warped exposure will be just big enough to contain all warped pixels;
154  if provided then the warped exposure may be smaller, and so missing some warped pixels;
155  ignored if destBBox is not None
156  @param destBBox: exact parent bbox of warped exposure (an afwGeom.Box2I or None);
157  if None then border and maxBBox are used to determine the bbox,
158  otherwise border and maxBBox are ignored
159 
160  @return destExposure: warped exposure (of same type as srcExposure)
161 
162  @note: calls mathLib.warpExposure insted of self.warpImage because the former
163  copies attributes such as Calib, and that should be done in one place
164  """
165  destBBox = self._computeDestBBox(
166  destWcs = destWcs,
167  srcImage = srcExposure.getMaskedImage(),
168  srcWcs = srcExposure.getWcs(),
169  border = border,
170  maxBBox = maxBBox,
171  destBBox = destBBox,
172  )
173  destExposure = srcExposure.Factory(destBBox, destWcs)
174  mathLib.warpExposure(destExposure, srcExposure, self._warpingControl)
175  return destExposure
176 
177  def warpImage(self, destWcs, srcImage, srcWcs, border=0, maxBBox=None, destBBox=None):
178  """Warp an image or masked image
179 
180  @param destWcs: WCS of warped image, an lsst.afw.geom.SkyWcs
181  @param srcImage: image or masked image to warp
182  @param srcWcs: WCS of image, an lsst.afw.geom.SkyWcs
183  @param border: grow bbox of warped image by this amount in all directions (int pixels);
184  if negative then the bbox is shrunk;
185  border is applied before maxBBox;
186  ignored if destBBox is not None
187  @param maxBBox: maximum allowed parent bbox of warped image (an afwGeom.Box2I or None);
188  if None then the warped image will be just big enough to contain all warped pixels;
189  if provided then the warped image may be smaller, and so missing some warped pixels;
190  ignored if destBBox is not None
191  @param destBBox: exact parent bbox of warped image (an afwGeom.Box2I or None);
192  if None then border and maxBBox are used to determine the bbox,
193  otherwise border and maxBBox are ignored
194 
195  @return destImage: warped image or masked image (of same type as srcImage)
196  """
197  destBBox = self._computeDestBBox(
198  destWcs = destWcs,
199  srcImage = srcImage,
200  srcWcs = srcWcs,
201  border = border,
202  maxBBox = maxBBox,
203  destBBox = destBBox,
204  )
205  destImage = srcImage.Factory(destBBox)
206  mathLib.warpImage(destImage, destWcs, srcImage,
207  srcWcs, self._warpingControl)
208  return destImage
209 
210  def _computeDestBBox(self, destWcs, srcImage, srcWcs, border, maxBBox, destBBox):
211  """Process destBBox argument for warpImage and warpExposure
212 
213  @param destWcs: WCS of warped image, an lsst.afw.geom.SkyWcs
214  @param srcImage: image or masked image to warp
215  @param srcWcs: WCS of image, an lsst.afw.geom.SkyWcs
216  @param border: grow bbox of warped image by this amount in all directions (int pixels);
217  if negative then the bbox is shrunk;
218  border is applied before maxBBox;
219  ignored if destBBox is not None
220  @param maxBBox: maximum allowed parent bbox of warped image (an afwGeom.Box2I or None);
221  if None then the warped image will be just big enough to contain all warped pixels;
222  if provided then the warped image may be smaller, and so missing some warped pixels;
223  ignored if destBBox is not None
224  @param destBBox: exact parent bbox of warped image (an afwGeom.Box2I or None);
225  if None then border and maxBBox are used to determine the bbox,
226  otherwise border and maxBBox are ignored
227  """
228  if destBBox is None: # warning: == None fails due to Box2I.__eq__
229  destBBox = computeWarpedBBox(
230  destWcs, srcImage.getBBox(afwImage.PARENT), srcWcs)
231  if border:
232  destBBox.grow(border)
233  if maxBBox is not None:
234  destBBox.clip(maxBBox)
235  return destBBox
def _computeDestBBox(self, destWcs, srcImage, srcWcs, border, maxBBox, destBBox)
Definition: warper.py:210
def getMaskWarpingKernel(self)
Definition: warper.py:139
def warpExposure(self, destWcs, srcExposure, border=0, maxBBox=None, destBBox=None)
Definition: warper.py:143
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:108
def warpImage(self, destWcs, srcImage, srcWcs, border=0, maxBBox=None, destBBox=None)
Definition: warper.py:177
def fromConfig(cls, config)
Definition: warper.py:122
A floating-point coordinate rectangle geometry.
Definition: Box.h:266