LSST Applications 27.0.0,g0265f82a02+469cd937ee,g02d81e74bb+21ad69e7e1,g1470d8bcf6+cbe83ee85a,g2079a07aa2+e67c6346a6,g212a7c68fe+04a9158687,g2305ad1205+94392ce272,g295015adf3+81dd352a9d,g2bbee38e9b+469cd937ee,g337abbeb29+469cd937ee,g3939d97d7f+72a9f7b576,g487adcacf7+71499e7cba,g50ff169b8f+5929b3527e,g52b1c1532d+a6fc98d2e7,g591dd9f2cf+df404f777f,g5a732f18d5+be83d3ecdb,g64a986408d+21ad69e7e1,g858d7b2824+21ad69e7e1,g8a8a8dda67+a6fc98d2e7,g99cad8db69+f62e5b0af5,g9ddcbc5298+d4bad12328,ga1e77700b3+9c366c4306,ga8c6da7877+71e4819109,gb0e22166c9+25ba2f69a1,gb6a65358fc+469cd937ee,gbb8dafda3b+69d3c0e320,gc07e1c2157+a98bf949bb,gc120e1dc64+615ec43309,gc28159a63d+469cd937ee,gcf0d15dbbd+72a9f7b576,gdaeeff99f8+a38ce5ea23,ge6526c86ff+3a7c1ac5f1,ge79ae78c31+469cd937ee,gee10cc3b42+a6fc98d2e7,gf1cff7945b+21ad69e7e1,gfbcc870c63+9a11dc8c8f
LSST Data Management Base Package
Loading...
Searching...
No Matches
fitSipDistortion.py
Go to the documentation of this file.
1# This file is part of meas_astrom.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <https://www.gnu.org/licenses/>.
21
22__all__ = ["FitSipDistortionTask", "FitSipDistortionConfig"]
23
24
25import lsst.sphgeom
26import lsst.pipe.base
27import lsst.geom
28import lsst.afw.image
29import lsst.afw.geom
31from lsst.utils.timer import timeMethod
32
33from ._measAstromLib import (OutlierRejectionControl,
34 ScaledPolynomialTransformFitter,
35 SipForwardTransform, SipReverseTransform,
36 makeMatchStatisticsInRadians, makeWcs)
37
38from .setMatchDistance import setMatchDistance
39
40
42 """Config for FitSipDistortionTask"""
44 doc="Order of SIP polynomial",
45 dtype=int,
46 default=4,
47 min=0,
48 )
50 doc="Number of rejection iterations",
51 dtype=int,
52 default=3,
53 min=0,
54 )
56 doc="Number of standard deviations for clipping level",
57 dtype=float,
58 default=3.0,
59 min=0.0,
60 )
62 doc="Minimum number of matches to reject when sigma-clipping",
63 dtype=int,
64 default=0
65 )
67 doc="Maximum number of matches to reject when sigma-clipping",
68 dtype=int,
69 default=1
70 )
71 maxScatterArcsec = lsst.pex.config.RangeField(
72 doc="Maximum median scatter of a WCS fit beyond which the fit fails (arcsec); "
73 "be generous, as this is only intended to catch catastrophic failures",
74 dtype=float,
75 default=10,
76 min=0,
77 )
78 refUncertainty = lsst.pex.config.Field(
79 doc="RMS uncertainty in reference catalog positions, in pixels. Will be added "
80 "in quadrature with measured uncertainties in the fit.",
81 dtype=float,
82 default=0.25,
83 )
85 doc="Number of X grid points used to invert the SIP reverse transform.",
86 dtype=int,
87 default=100,
88 )
90 doc="Number of Y grid points used to invert the SIP reverse transform.",
91 dtype=int,
92 default=100,
93 )
95 doc="When setting the gird region, how much to extend the image "
96 "bounding box (in pixels) before transforming it to intermediate "
97 "world coordinates using the initial WCS.",
98 dtype=float,
99 default=50.0,
100 )
101
102
103class FitSipDistortionTask(lsst.pipe.base.Task):
104 """Fit a TAN-SIP WCS given a list of reference object/source matches.
105 """
106 ConfigClass = FitSipDistortionConfig
107 _DefaultName = "fitWcs"
108
109 def __init__(self, **kwargs):
110 lsst.pipe.base.Task.__init__(self, **kwargs)
111 self.outlierRejectionCtrl = OutlierRejectionControl()
112 self.outlierRejectionCtrl.nClipMin = self.config.nClipMin
113 self.outlierRejectionCtrl.nClipMax = self.config.nClipMax
114 self.outlierRejectionCtrl.nSigma = self.config.rejSigma
115
116 @timeMethod
117 def fitWcs(self, matches, initWcs, bbox=None, refCat=None, sourceCat=None, exposure=None):
118 """Fit a TAN-SIP WCS from a list of reference object/source matches.
119
120 Parameters
121 ----------
122 matches : `list` of `lsst.afw.table.ReferenceMatch`
123 A sequence of reference object/source matches.
124 The following fields are read:
125 - match.first (reference object) coord
126 - match.second (source) centroid
127
128 The following fields are written:
129 - match.first (reference object) centroid
130 - match.second (source) centroid
131 - match.distance (on sky separation, in radians)
132
133 initWcs : `lsst.afw.geom.SkyWcs`
134 An initial WCS whose CD matrix is used as the final CD matrix.
135 bbox : `lsst.geom.Box2I`
136 The region over which the WCS will be valid (PARENT pixel coordinates);
137 if `None` or an empty box then computed from matches
138 refCat : `lsst.afw.table.SimpleCatalog`
139 Reference object catalog, or `None`.
140 If provided then all centroids are updated with the new WCS,
141 otherwise only the centroids for ref objects in matches are updated.
142 Required fields are "centroid_x", "centroid_y", "coord_ra", and "coord_dec".
143 sourceCat : `lsst.afw.table.SourceCatalog`
144 Source catalog, or `None`.
145 If provided then coords are updated with the new WCS;
146 otherwise only the coords for sources in matches are updated.
147 Required input fields are "slot_Centroid_x", "slot_Centroid_y",
148 "slot_Centroid_xErr", "slot_Centroid_yErr", and optionally
149 "slot_Centroid_x_y_Cov". The "coord_ra" and "coord_dec" fields
150 will be updated but are not used as input.
151 exposure : `lsst.afw.image.Exposure`
152 An Exposure or other displayable image on which matches can be
153 overplotted. Ignored (and may be `None`) if display-based debugging
154 is not enabled via lsstDebug.
155
156 Returns
157 -------
158 An lsst.pipe.base.Struct with the following fields:
159 - wcs : `lsst.afw.geom.SkyWcs`
160 The best-fit WCS.
161 - scatterOnSky : `lsst.geom.Angle`
162 The median on-sky separation between reference objects and
163 sources in "matches", as an `lsst.geom.Angle`
164 """
165 import lsstDebug
166 display = lsstDebug.Info(__name__).display
167 displayFrame = lsstDebug.Info(__name__).frame
168 displayPause = lsstDebug.Info(__name__).pause
169
170 if bbox is None:
171 bbox = lsst.geom.Box2D()
172 for match in matches:
173 bbox.include(match.second.getCentroid())
174 bbox = lsst.geom.Box2I(bbox)
175
176 wcs = self.makeInitialWcs(matches, initWcs)
177 cdMatrix = lsst.geom.LinearTransform(wcs.getCdMatrix())
178
179 # Fit the "reverse" mapping from intermediate world coordinates to
180 # pixels, rejecting outliers. Fitting in this direction first makes it
181 # easier to handle the case where we have uncertainty on source
182 # positions but not reference positions. That's the case we have
183 # right now for purely bookeeeping reasons, and it may be the case we
184 # have in the future when we us Gaia as the reference catalog.
185 revFitter = ScaledPolynomialTransformFitter.fromMatches(self.config.order, matches, wcs,
186 self.config.refUncertainty)
187 revFitter.fit()
188 for nIter in range(self.config.numRejIter):
189 revFitter.updateModel()
190 intrinsicScatter = revFitter.updateIntrinsicScatter()
191 clippedSigma, nRejected = revFitter.rejectOutliers(self.outlierRejectionCtrl)
192 self.log.debug(
193 "Iteration %s: intrinsic scatter is %4.3f pixels, "
194 "rejected %d outliers at %3.2f sigma.",
195 nIter+1, intrinsicScatter, nRejected, clippedSigma
196 )
197 if display:
198 displayFrame = self.display(revFitter, exposure=exposure, bbox=bbox,
199 frame=displayFrame, displayPause=displayPause)
200 revFitter.fit()
201 revScaledPoly = revFitter.getTransform()
202 # Convert the generic ScaledPolynomialTransform result to SIP form
203 # with given CRPIX and CD (this is an exact conversion, up to
204 # floating-point round-off error)
205 sipReverse = SipReverseTransform.convert(revScaledPoly, wcs.getPixelOrigin(), cdMatrix)
206
207 # Fit the forward mapping to a grid of points created from the reverse
208 # transform. Because that grid needs to be defined in intermediate
209 # world coordinates, and we don't have a good way to get from pixels to
210 # intermediate world coordinates yet (that's what we're fitting), we'll
211 # first grow the box to make it conservatively large...
212 gridBBoxPix = lsst.geom.Box2D(bbox)
213 gridBBoxPix.grow(self.config.gridBorder)
214 # ...and then we'll transform using just the CRPIX offset and CD matrix
215 # linear transform, which is the TAN-only (no SIP distortion, and
216 # hence approximate) mapping from pixels to intermediate world
217 # coordinates.
218 gridBBoxIwc = lsst.geom.Box2D()
219 for point in gridBBoxPix.getCorners():
220 point -= lsst.geom.Extent2D(wcs.getPixelOrigin())
221 gridBBoxIwc.include(cdMatrix(point))
222 fwdFitter = ScaledPolynomialTransformFitter.fromGrid(self.config.order, gridBBoxIwc,
223 self.config.nGridX, self.config.nGridY,
224 revScaledPoly)
225 fwdFitter.fit()
226 # Convert to SIP forward form.
227 fwdScaledPoly = fwdFitter.getTransform()
228 sipForward = SipForwardTransform.convert(fwdScaledPoly, wcs.getPixelOrigin(), cdMatrix)
229
230 # Make a new WCS from the SIP transform objects and the CRVAL in the
231 # initial WCS.
232 wcs = makeWcs(sipForward, sipReverse, wcs.getSkyOrigin())
233
234 if refCat is not None:
235 self.log.debug("Updating centroids in refCat")
236 lsst.afw.table.updateRefCentroids(wcs, refList=refCat)
237 else:
238 self.log.warning("Updating reference object centroids in match list; refCat is None")
239 lsst.afw.table.updateRefCentroids(wcs, refList=[match.first for match in matches])
240
241 if sourceCat is not None:
242 self.log.debug("Updating coords in sourceCat")
243 lsst.afw.table.updateSourceCoords(wcs, sourceList=sourceCat)
244 else:
245 self.log.warning("Updating source coords in match list; sourceCat is None")
246 lsst.afw.table.updateSourceCoords(wcs, sourceList=[match.second for match in matches])
247
248 self.log.debug("Updating distance in match list")
249 setMatchDistance(matches)
250
251 stats = makeMatchStatisticsInRadians(wcs, matches, lsst.afw.math.MEDIAN)
252 scatterOnSky = stats.getValue()*lsst.geom.radians
253
254 if scatterOnSky.asArcseconds() > self.config.maxScatterArcsec:
255 raise lsst.pipe.base.TaskError(
256 "Fit failed: median scatter on sky = %0.3f arcsec > %0.3f config.maxScatterArcsec" %
257 (scatterOnSky.asArcseconds(), self.config.maxScatterArcsec))
258
259 return lsst.pipe.base.Struct(
260 wcs=wcs,
261 scatterOnSky=scatterOnSky,
262 )
263
264 def display(self, revFitter, exposure=None, bbox=None, frame=0, pause=True):
265 """Display positions and outlier status overlaid on an image.
266
267 This method is called by fitWcs when display debugging is enabled. It
268 always drops into pdb before returning to allow interactive inspection,
269 and hence it should never be called in non-interactive contexts.
270
271 Parameters
272 ----------
273 revFitter : :cpp:class:`lsst::meas::astrom::ScaledPolynomialTransformFitter`
274 Fitter object initialized with `fromMatches` for fitting a "reverse"
275 distortion: the mapping from intermediate world coordinates to
276 pixels.
277 exposure : :cpp:class:`lsst::afw::image::Exposure`
278 An Exposure or other displayable image on which matches can be
279 overplotted.
280 bbox : :cpp:class:`lsst::afw::geom::Box2I`
281 Bounding box of the region on which matches should be plotted.
282 """
283 data = revFitter.getData()
284 disp = lsst.afw.display.getDisplay(frame=frame)
285 if exposure is not None:
286 disp.mtv(exposure)
287 elif bbox is not None:
288 disp.mtv(exposure=lsst.afw.image.ExposureF(bbox))
289 else:
290 raise TypeError("At least one of 'exposure' and 'bbox' must be provided.")
291 data = revFitter.getData()
292 srcKey = lsst.afw.table.Point2DKey(data.schema["src"])
293 srcErrKey = lsst.afw.table.CovarianceMatrix2fKey(data.schema["src"], ["x", "y"])
294 refKey = lsst.afw.table.Point2DKey(data.schema["initial"])
295 modelKey = lsst.afw.table.Point2DKey(data.schema["model"])
296 rejectedKey = data.schema.find("rejected").key
297 with disp.Buffering():
298 for record in data:
299 colors = ((lsst.afw.display.RED, lsst.afw.display.GREEN)
300 if not record.get(rejectedKey) else
301 (lsst.afw.display.MAGENTA, lsst.afw.display.CYAN))
302 rx, ry = record.get(refKey)
303 disp.dot("x", rx, ry, size=10, ctype=colors[0])
304 mx, my = record.get(modelKey)
305 disp.dot("o", mx, my, size=10, ctype=colors[0])
306 disp.line([(rx, ry), (mx, my)], ctype=colors[0])
307 sx, sy = record.get(srcKey)
308 sErr = record.get(srcErrKey)
309 sEllipse = lsst.afw.geom.Quadrupole(sErr[0, 0], sErr[1, 1], sErr[0, 1])
310 disp.dot(sEllipse, sx, sy, ctype=colors[1])
311 if pause or pause is None: # default is to pause
312 print("Dropping into debugger to allow inspection of display. Type 'continue' when done.")
313 import pdb
314 pdb.set_trace()
315 return frame
316 else:
317 return frame + 1 # increment and return the frame for the next iteration.
318
319 def makeInitialWcs(self, matches, wcs):
320 """Generate a guess Wcs from the astrometric matches
321
322 We create a Wcs anchored at the center of the matches, with the scale
323 of the input Wcs. This is necessary because the Wcs may have a very
324 approximation position (as is common with telescoped-generated Wcs).
325 We're using the best of each: positions from the matches, and scale
326 from the input Wcs.
327
328 Parameters
329 ----------
330 matches : list of :cpp:class:`lsst::afw::table::ReferenceMatch`
331 A sequence of reference object/source matches.
332 The following fields are read:
333
334 - match.first (reference object) coord
335 - match.second (source) centroid
336
337 wcs : :cpp:class:`lsst::afw::geom::SkyWcs`
338 An initial WCS whose CD matrix is used as the CD matrix of the
339 result.
340
341 Returns
342 -------
343 newWcs : `lsst.afw.geom.SkyWcs`
344 A new WCS guess.
345 """
346 crpix = lsst.geom.Extent2D(0, 0)
347 crval = lsst.sphgeom.Vector3d(0, 0, 0)
348 for mm in matches:
349 crpix += lsst.geom.Extent2D(mm.second.getCentroid())
350 crval += mm.first.getCoord().getVector()
351 crpix /= len(matches)
352 crval /= len(matches)
353 cd = wcs.getCdMatrix()
354 newWcs = lsst.afw.geom.makeSkyWcs(crpix=lsst.geom.Point2D(crpix),
355 crval=lsst.geom.SpherePoint(crval),
356 cdMatrix=cd)
357 return newWcs
An ellipse core with quadrupole moments as parameters.
Definition Quadrupole.h:47
A floating-point coordinate rectangle geometry.
Definition Box.h:413
An integer coordinate rectangle.
Definition Box.h:55
A 2D linear coordinate transformation.
Point in an unspecified spherical coordinate system.
Definition SpherePoint.h:57
fitWcs(self, matches, initWcs, bbox=None, refCat=None, sourceCat=None, exposure=None)
display(self, revFitter, exposure=None, bbox=None, frame=0, pause=True)
Vector3d is a vector in ℝ³ with components stored in double precision.
Definition Vector3d.h:51
std::shared_ptr< SkyWcs > makeSkyWcs(daf::base::PropertySet &metadata, bool strip=false)
Construct a SkyWcs from FITS keywords.
Definition SkyWcs.cc:521
void updateRefCentroids(geom::SkyWcs const &wcs, ReferenceCollection &refList)
Update centroids in a collection of reference objects.
Definition wcsUtils.cc:73
void updateSourceCoords(geom::SkyWcs const &wcs, SourceCollection &sourceList, bool include_covariance=true)
Update sky coordinates in a collection of source objects.
Definition wcsUtils.cc:125