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