22 from __future__
import absolute_import, division, print_function
24 __all__ = [
"approximateWcs"]
26 from builtins
import range
27 from builtins
import object
31 import lsst.afw.table
as afwTable
32 import lsst.afw.geom
as afwGeom
33 from lsst.meas.base
import SingleFrameMeasurementTask
35 from lsst.afw.image.basicUtils
import assertWcsAlmostEqualOverBBox
39 """A fake unit test case class that will enable us to call
40 assertWcsAlmostEqualOverBBox from the method approximateWcs"""
43 raise UserWarning(
"WCS fitting failed " + msgStr)
47 skyTolerance=0.001*afwGeom.arcseconds, pixelTolerance=0.02, useTanWcs=
False):
48 """Approximate an existing WCS as a TAN-SIP WCS
50 The fit is performed by evaluating the WCS at a uniform grid of points within a bounding box.
52 @param[in] wcs wcs to approximate
53 @param[in] bbox the region over which the WCS will be fit
54 @param[in] order order of SIP fit
55 @param[in] nx number of grid points along x
56 @param[in] ny number of grid points along y
57 @param[in] iterations number of times to iterate over fitting
58 @param[in] skyTolerance maximum allowed difference in world coordinates between
59 input wcs and approximate wcs (default is 0.001 arcsec)
60 @param[in] pixelTolerance maximum allowed difference in pixel coordinates between
61 input wcs and approximate wcs (default is 0.02 pixels)
62 @param[in] useTanWcs send a TAN version of wcs to the fitter? It is documented to require that,
63 but I don't think the fitter actually cares
64 @return the fit TAN-SIP WCS
67 crCoord = wcs.getSkyOrigin()
68 crPix = wcs.getPixelOrigin()
69 cdMat = wcs.getCDMatrix()
70 tanWcs = afwImage.makeWcs(crCoord, crPix, cdMat[0, 0], cdMat[0, 1], cdMat[1, 0], cdMat[1, 1])
75 refSchema = afwTable.SimpleTable.makeMinimalSchema()
76 refCoordKey = afwTable.CoordKey(refSchema[
"coord"])
77 refCat = afwTable.SimpleCatalog(refSchema)
79 sourceSchema = afwTable.SourceTable.makeMinimalSchema()
80 SingleFrameMeasurementTask(schema=sourceSchema)
81 sourceCentroidKey = afwTable.Point2DKey(sourceSchema[
"slot_Centroid"])
83 sourceCat = afwTable.SourceCatalog(sourceSchema)
87 bboxd = afwGeom.Box2D(bbox)
88 for x
in np.linspace(bboxd.getMinX(), bboxd.getMaxX(), nx):
89 for y
in np.linspace(bboxd.getMinY(), bboxd.getMaxY(), ny):
90 pixelPos = afwGeom.Point2D(x, y)
91 skyCoord = wcs.pixelToSky(pixelPos)
93 refObj = refCat.addNew()
94 refObj.set(refCoordKey, skyCoord)
96 source = sourceCat.addNew()
97 source.set(sourceCentroidKey, pixelPos)
99 matchList.append(afwTable.ReferenceMatch(refObj, source, 0.0))
102 for indx
in range(iterations):
104 tanWcs = sipObject.getNewWcs()
105 fitWcs = sipObject.getNewWcs()
108 assertWcsAlmostEqualOverBBox(mockTest, wcs, fitWcs, bbox, maxDiffSky=skyTolerance,
109 maxDiffPix=pixelTolerance)
CreateWcsWithSip< MatchT > makeCreateWcsWithSip(std::vector< MatchT > const &matches, afw::image::Wcs const &linearWcs, int const order, afw::geom::Box2I const &bbox=afw::geom::Box2I(), int const ngrid=0)
Factory function for CreateWcsWithSip.