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