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