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