lsst.meas.astrom  16.0-14-g22e2ff2
approximateWcs.py
Go to the documentation of this file.
1 #
2 # LSST Data Management System
3 # Copyright 2008-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__ = ["approximateWcs"]
24 
25 import numpy as np
26 
27 import lsst.geom
28 import lsst.afw.table as afwTable
29 import lsst.afw.geom as afwGeom
30 from lsst.meas.base import SingleFrameMeasurementTask
31 from lsst.meas.astrom.sip import makeCreateWcsWithSip
32 from lsst.afw.geom.utils import assertWcsAlmostEqualOverBBox
33 
34 
36  """A fake unit test case class that will enable us to call
37  assertWcsAlmostEqualOverBBox from the method approximateWcs"""
38 
39  def fail(self, msgStr):
40  raise UserWarning("WCS fitting failed " + msgStr)
41 
42 
43 def approximateWcs(wcs, bbox, order=3, nx=20, ny=20, iterations=3,
44  skyTolerance=0.001*lsst.geom.arcseconds, pixelTolerance=0.02, useTanWcs=False):
45  """Approximate an existing WCS as a TAN-SIP WCS
46 
47  The fit is performed by evaluating the WCS at a uniform grid of points within a bounding box.
48 
49  @param[in] wcs wcs to approximate
50  @param[in] bbox the region over which the WCS will be fit
51  @param[in] order order of SIP fit
52  @param[in] nx number of grid points along x
53  @param[in] ny number of grid points along y
54  @param[in] iterations number of times to iterate over fitting
55  @param[in] skyTolerance maximum allowed difference in world coordinates between
56  input wcs and approximate wcs (default is 0.001 arcsec)
57  @param[in] pixelTolerance maximum allowed difference in pixel coordinates between
58  input wcs and approximate wcs (default is 0.02 pixels)
59  @param[in] useTanWcs send a TAN version of wcs to the fitter? It is documented to require that,
60  but I don't think the fitter actually cares
61  @return the fit TAN-SIP WCS
62  """
63  if useTanWcs:
64  crpix = wcs.getPixelOrigin()
65  crval = wcs.getSkyOrigin()
66  cdMatrix = wcs.getCdMatrix(crpix)
67  tanWcs = afwGeom.makeSkyWcs(crpix=crpix, crval=crval, cdMatrix=cdMatrix)
68  else:
69  tanWcs = wcs
70 
71  # create a matchList consisting of a grid of points covering the bbox
72  refSchema = afwTable.SimpleTable.makeMinimalSchema()
73  refCoordKey = afwTable.CoordKey(refSchema["coord"])
74  refCat = afwTable.SimpleCatalog(refSchema)
75 
76  sourceSchema = afwTable.SourceTable.makeMinimalSchema()
77  SingleFrameMeasurementTask(schema=sourceSchema) # expand the schema
78  sourceCentroidKey = afwTable.Point2DKey(sourceSchema["slot_Centroid"])
79 
80  sourceCat = afwTable.SourceCatalog(sourceSchema)
81 
82  matchList = []
83 
84  bboxd = lsst.geom.Box2D(bbox)
85  for x in np.linspace(bboxd.getMinX(), bboxd.getMaxX(), nx):
86  for y in np.linspace(bboxd.getMinY(), bboxd.getMaxY(), ny):
87  pixelPos = lsst.geom.Point2D(x, y)
88  skyCoord = wcs.pixelToSky(pixelPos)
89 
90  refObj = refCat.addNew()
91  refObj.set(refCoordKey, skyCoord)
92 
93  source = sourceCat.addNew()
94  source.set(sourceCentroidKey, pixelPos)
95 
96  matchList.append(afwTable.ReferenceMatch(refObj, source, 0.0))
97 
98  # The TAN-SIP fitter is fitting x and y separately, so we have to iterate to make it converge
99  for indx in range(iterations):
100  sipObject = makeCreateWcsWithSip(matchList, tanWcs, order, bbox)
101  tanWcs = sipObject.getNewWcs()
102  fitWcs = sipObject.getNewWcs()
103 
104  mockTest = _MockTestCase()
105  assertWcsAlmostEqualOverBBox(mockTest, wcs, fitWcs, bbox, maxDiffSky=skyTolerance,
106  maxDiffPix=pixelTolerance)
107 
108  return fitWcs
def approximateWcs(wcs, bbox, order=3, nx=20, ny=20, iterations=3, skyTolerance=0.001 *lsst.geom.arcseconds, pixelTolerance=0.02, useTanWcs=False)
CreateWcsWithSip< MatchT > makeCreateWcsWithSip(std::vector< MatchT > const &matches, afw::geom::SkyWcs const &linearWcs, int const order, geom::Box2I const &bbox=geom::Box2I(), int const ngrid=0)
Factory function for CreateWcsWithSip.