lsst.afw  14.0-42-g99f9a3040+1
wcsUtilsContinued.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2017 LSST Corporation.
4 #
5 # This product includes software developed by the
6 # LSST Project (http://www.lsst.org/).
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the LSST License Statement and
19 # the GNU General Public License along with this program. If not,
20 # see <http://www.lsstcorp.org/LegalNotices/>.
21 #
22 from __future__ import absolute_import, division, print_function
23 
24 __all__ = ["getSipMatrixFromMetadata", "makeDistortedTanWcs", "computePixelToDistortedPixel"]
25 
26 from ..coordinates import Point2D
27 from ..transformFactory import linearizeTransform, makeTransform
28 from ..skyWcs import makeModifiedWcs
29 from .wcsUtils import _getSipMatrixFromMetadata
30 
31 
32 def getSipMatrixFromMetadata(metadata, name):
33  """Extract a SIP matrix from FITS TAN-SIP WCS metadata
34 
35  Omitted coefficients are set to 0 and all coefficients may be omitted.
36 
37  Parameters
38  ----------
39  metadata : lsst.daf.base.PropertySet
40  FITS metadata
41  name : str
42  Name of TAN-SIP matrix; one of "A", "B", "Ap", "Bp"
43 
44  Returns
45  -------
46  numpy.array
47  The SIP matrix
48 
49  Raises
50  ------
51  TypeError
52  If the order keyword <name>_ORDER (e.g. AP_ORDER) is not found,
53  the value of the order keyword cannot be read as an integer,
54  the value of the order keyword is negative,
55  or if a matrix parameter (e.g. AP_5_0) cannot be read as a float.
56  """
57  arr = _getSipMatrixFromMetadata(metadata, name)
58  if arr.shape == (): # order=0
59  arr.shape = (1, 1)
60  return arr
61 
62 
63 def makeDistortedTanWcs(tanWcs, pixelToFocalPlane, focalPlaneToFieldAngle):
64  """
65  Compute a WCS that includes a model of optical distortion.
66 
67  This is useful in the common case that the initial WCS entirely ignores
68  the effect of optical distortion.
69 
70  Parameters
71  ----------
72  tanWcs : lsst.afw.geom.SkyWcs
73  A pure TAN WCS, such as is usually provided in raw data.
74  This should have no existing compensation for optical distortion
75  (though it may include an ACTUAL_PIXELS frame to model pixel-level
76  distortions).
77  pixelToFocalPlane : lsst.afw.geom.TransformPoint2ToPoint2
78  Transform parent pixel coordinates to focal plane coordinates.
79  This models the location of the CCD on the focal plane
80  and is almost always an affine transformation.
81  This can be obtained from the detector of an exposure.
82  focalPlaneToFieldAngle : lsst.afw.geom.TransformPoint2ToPoint2
83  Transform focal plane coordinates to field angle coordinates.
84  This is a model for optical distortion, and is often a radial
85  polynomial. This can be obtained from the camera geometry.
86 
87 
88  Returns
89  -------
90  lsst.afw.geom.SkyWcs
91  A copy of `tanWcs` that includes the effect of optical distortion.
92 
93  Raises
94  ------
95  RuntimeError
96  If the current frame of `wcs` is not a SkyFrame;
97  LookupError
98  If 2-dimensional Frames with Domain "PIXELS" and "IWC"
99  are not all found.
100  """
101  # The math is as follows:
102  #
103  # Our input TAN WCS is:
104  # tanWcs = PIXELS frame -> pixelToIwc -> IWC frame -> iwcToSky -> SkyFrame
105  # See lsst.afw.geom.SkyWcs for a description of these frames.
106  # tanWcs may also contain an ACTUAL_PIXELS frame before the PIXELS frame;
107  # if so it will be preserved, but it is irrelevant to the computation
108  # and so not discussed further.
109  #
110  # Our desired WCS must still contain the PIXELS and IWC frames.
111  # The distortion will be inserted just after the PIXELS frame,
112  # So the new WCS will be as follows:
113  # wcs = PIXELS frame -> pixelToDistortedPixel -> pixelToIwc -> IWC frame -> iwcToSky -> SkyFrame
114  #
115  # We compute pixelToDistortedPixel as follows...
116  #
117  # We will omit the frames from now on, for simplicity. Thus:
118  # tanWcs = pixelToIwc -> iwcToSksy
119  # and:
120  # wcs = pixelToDistortedPixel -> pixelToIwc -> iwcToSky
121  #
122  # We also know pixelToFocalPlane and focalPlaneToFieldAngle,
123  # and can use them as follows:
124  #
125  # The tan WCS can be expressed as:
126  # tanWcs = pixelToFocalPlane -> focalPlaneToTanFieldAngle -> fieldAngleToIwc -> iwcToSky
127  # where:
128  # - focalPlaneToTanFieldAngle is the linear approximation to
129  # focalPlaneToFieldAngle at the center of the focal plane
130  #
131  # The desired WCS can be expressed as:
132  # wcs = pixelToFocalPlane -> focalPlaneToFieldAngle -> fieldAngleToIwc -> iwcToSky
133  #
134  # By equating the two expressions for tanWcs, we get:
135  # pixelToIwc = pixelToFocalPlane -> focalPlaneToTanFieldAngle -> fieldAngleToIwc
136  # fieldAngleToIwc = tanFieldAngleToFocalPlane -> focalPlaneToPixel -> pixelToIwc
137  #
138  # By equating the two expressions for desired wcs we get:
139  # pixelToDistortedPixel -> pixelToIwc = pixelToFocalPlane -> focalPlaneToFieldAngle -> fieldAngleToIwc
140  #
141  # Substitute our expression for fieldAngleToIwc from tanWcs into the
142  # previous equation, we get:
143  # pixelToDistortedPixel -> pixelToIwc
144  # = pixelToFocalPlane -> focalPlaneToFieldAngle -> tanFieldAngleToFocalPlane -> focalPlaneToPixel
145  # -> pixelToIwc
146  #
147  # Thus:
148  # pixelToDistortedPixel
149  # = pixelToFocalPlane -> focalPlaneToFieldAngle -> tanFieldAngleToFocalPlane -> focalPlaneToPixel
150 
151  pixelToDistortedPixel = computePixelToDistortedPixel(pixelToFocalPlane, focalPlaneToFieldAngle)
152  return makeModifiedWcs(pixelTransform=pixelToDistortedPixel, wcs=tanWcs, modifyActualPixels=False)
153 
154 
155 def computePixelToDistortedPixel(pixelToFocalPlane, focalPlaneToFieldAngle):
156  """
157  Compute the transform pixelToDistortedPixel, which applies optical
158  distortion specified by focalPlaneToFieldAngle
159 
160  The resulting transform is designed to be used to convert a pure TAN WCS
161  to a WCS that includes a model for optical distortion. In detail,
162  the initial WCS will contain these frames and transforms:
163  PIXELS frame -> pixelToIwc -> IWC frame -> gridToIwc -> SkyFrame
164  To produce the WCS with distortion, replace pixelToIwc with:
165  pixelToDistortedPixel -> pixelToIwc
166 
167  Parameters
168  ----------
169  pixelToFocalPlane : lsst.afw.geom.TransformPoint2ToPoint2
170  Transform parent pixel coordinates to focal plane coordinates
171  focalPlaneToFieldAngle : lsst.afw.geom.TransformPoint2ToPoint2
172  Transform focal plane coordinates to field angle coordinates
173 
174  Returns
175  -------
176  pixelToDistortedPixel : lsst.afw.geom.TransformPoint2ToPoint2
177  A transform that applies the effect of the optical distortion model.
178  """
179  # return pixelToFocalPlane -> focalPlaneToFieldAngle -> tanFieldAngleToocalPlane -> focalPlaneToPixel
180  focalPlaneToTanFieldAngle = makeTransform(linearizeTransform(focalPlaneToFieldAngle, Point2D(0, 0)))
181  return pixelToFocalPlane.then(focalPlaneToFieldAngle) \
182  .then(focalPlaneToTanFieldAngle.getInverse()) \
183  .then(pixelToFocalPlane.getInverse())
AffineTransform linearizeTransform(TransformPoint2ToPoint2 const &original, Point2D const &inPoint)
Approximate a Transform by its local linearization.
Point< double, 2 > Point2D
Definition: Point.h:300
std::shared_ptr< TransformPoint2ToPoint2 > makeTransform(AffineTransform const &affine)
Wrap an AffineTransform as a Transform.
def makeDistortedTanWcs(tanWcs, pixelToFocalPlane, focalPlaneToFieldAngle)
std::shared_ptr< SkyWcs > makeModifiedWcs(TransformPoint2ToPoint2 const &pixelTransform, SkyWcs const &wcs, bool modifyActualPixels)
Create a new SkyWcs whose pixels are transformed by pixelTransform, as described below.
def computePixelToDistortedPixel(pixelToFocalPlane, focalPlaneToFieldAngle)