22 from __future__
import absolute_import, division, print_function
24 __all__ = [
"AstrometryConfig",
"AstrometryTask"]
26 from builtins
import range
28 import lsst.pex.config
as pexConfig
29 import lsst.pipe.base
as pipeBase
30 import lsst.afw.geom
as afwGeom
31 from .ref_match
import RefMatchTask, RefMatchConfig
32 from .fitTanSipWcs
import FitTanSipWcsTask
33 from .display
import displayAstrometry
34 from .createMatchMetadata
import createMatchMetadata
38 wcsFitter = pexConfig.ConfigurableField(
39 target=FitTanSipWcsTask,
42 forceKnownWcs = pexConfig.Field(
44 doc=
"If True then load reference objects and match sources but do not fit a WCS; " +
45 " this simply controls whether 'run' calls 'solve' or 'loadAndMatch'",
48 maxIter = pexConfig.RangeField(
49 doc=
"maximum number of iterations of match sources and fit WCS" +
50 "ignored if not fitting a WCS",
55 minMatchDistanceArcSec = pexConfig.RangeField(
56 doc=
"the match distance below which further iteration is pointless (arcsec); "
57 "ignored if not fitting a WCS",
73 """!Match an input source catalog with objects from a reference catalog and solve for the WCS
75 @anchor AstrometryTask_
77 @section meas_astrom_astrometry_Contents Contents
79 - @ref meas_astrom_astrometry_Purpose
80 - @ref meas_astrom_astrometry_Initialize
81 - @ref meas_astrom_astrometry_IO
82 - @ref meas_astrom_astrometry_Config
83 - @ref meas_astrom_astrometry_Example
84 - @ref meas_astrom_astrometry_Debug
86 @section meas_astrom_astrometry_Purpose Description
88 Match input sourceCat with a reference catalog and solve for the Wcs
90 There are three steps, each performed by different subtasks:
91 - Find position reference stars that overlap the exposure
92 - Match sourceCat to position reference stars
93 - Fit a WCS based on the matches
95 @section meas_astrom_astrometry_Initialize Task initialisation
99 @section meas_astrom_astrometry_IO Invoking the Task
103 @copydoc loadAndMatch
105 @section meas_astrom_astrometry_Config Configuration parameters
107 See @ref AstrometryConfig
109 @section meas_astrom_astrometry_Example A complete example of using AstrometryTask
111 See \ref pipe_tasks_photocal_Example.
113 @section meas_astrom_astrometry_Debug Debug variables
115 The @link lsst.pipe.base.cmdLineTask.CmdLineTask command line task@endlink interface supports a
116 flag @c -d to import @b debug.py from your @c PYTHONPATH; see @ref baseDebug for more about
119 The available variables in AstrometryTask are:
121 <DT> @c display (bool)
122 <DD> If True display information at three stages: after finding reference objects,
123 after matching sources to reference objects, and after fitting the WCS; defaults to False
125 <DD> ds9 frame to use to display the reference objects; the next two frames are used
126 to display the match list and the results of the final WCS; defaults to 0
129 To investigate the @ref meas_astrom_astrometry_Debug, put something like
133 debug = lsstDebug.getInfo(name) # N.b. lsstDebug.Info(name) would call us recursively
134 if name == "lsst.meas.astrom.astrometry":
139 lsstDebug.Info = DebugInfo
141 into your debug.py file and run this task with the @c --debug flag.
143 ConfigClass = AstrometryConfig
144 _DefaultName =
"astrometricSolver"
146 def __init__(self, refObjLoader, schema=None, **kwargs):
147 """!Construct an AstrometryTask
149 @param[in] refObjLoader A reference object loader object
150 @param[in] schema ignored; available for compatibility with an older astrometry task
151 @param[in] kwargs additional keyword arguments for pipe_base Task.\_\_init\_\_
153 RefMatchTask.__init__(self, refObjLoader, schema=schema, **kwargs)
155 if schema
is not None:
156 self.
usedKey = schema.addField(
"calib_astrometryUsed", type=
"Flag",
157 doc=
"set if source was used in astrometric calibration")
161 self.makeSubtask(
"wcsFitter")
164 def run(self, sourceCat, exposure):
165 """!Load reference objects, match sources and optionally fit a WCS
167 This is a thin layer around solve or loadAndMatch, depending on config.forceKnownWcs
169 @param[in,out] exposure exposure whose WCS is to be fit
170 The following are read only:
172 - calib (may be absent)
173 - filter (may be unset)
174 - detector (if wcs is pure tangent; may be absent)
175 The following are updated:
176 - wcs (the initial value is used as an initial guess, and is required)
177 @param[in] sourceCat catalog of sources detected on the exposure (an lsst.afw.table.SourceCatalog)
178 @return an lsst.pipe.base.Struct with these fields:
179 - refCat reference object catalog of objects that overlap the exposure (with some margin)
180 (an lsst::afw::table::SimpleCatalog)
181 - matches astrometric matches, a list of lsst.afw.table.ReferenceMatch
182 - scatterOnSky median on-sky separation between reference objects and sources in "matches"
183 (an lsst.afw.geom.Angle), or None if config.forceKnownWcs True
184 - matchMeta metadata needed to unpersist matches (an lsst.daf.base.PropertyList)
186 if self.config.forceKnownWcs:
187 res = self.loadAndMatch(exposure=exposure, sourceCat=sourceCat)
188 res.scatterOnSky =
None
190 res = self.
solve(exposure=exposure, sourceCat=sourceCat)
194 def solve(self, exposure, sourceCat):
195 """!Load reference objects overlapping an exposure, match to sources and fit a WCS
197 @return an lsst.pipe.base.Struct with these fields:
198 - refCat reference object catalog of objects that overlap the exposure (with some margin)
199 (an lsst::afw::table::SimpleCatalog)
200 - matches astrometric matches, a list of lsst.afw.table.ReferenceMatch
201 - scatterOnSky median on-sky separation between reference objects and sources in "matches"
202 (an lsst.afw.geom.Angle)
203 - matchMeta metadata needed to unpersist matches (an lsst.daf.base.PropertyList)
205 @note ignores config.forceKnownWcs
208 debug = lsstDebug.Info(__name__)
211 expMd = self._getExposureMetadata(exposure)
212 exp_bbox = expMd.bbox
213 exp_bbox.grow(afwGeom.Extent2I(self.config.matcher.maxOffsetPix,
214 self.config.matcher.maxOffsetPix))
216 loadRes = self.refObjLoader.loadPixelBox(
219 filterName=expMd.filterName,
223 frame = int(debug.frame)
225 refCat=loadRes.refCat,
230 title=
"Reference catalog",
235 match_tolerance =
None
236 for i
in range(self.config.maxIter):
240 refCat=loadRes.refCat,
242 refFluxField=loadRes.fluxField,
246 match_tolerance=match_tolerance,
248 except Exception
as e:
251 self.log.info(
"Fit WCS iter %d failed; using previous iteration: %s" % (iterNum, e))
257 match_tolerance = tryRes.match_tolerance
258 tryMatchDist = self._computeMatchStatsOnSky(tryRes.matches)
260 "Match and fit WCS iteration %d: found %d matches with scatter = %0.3f +- %0.3f arcsec; "
261 "max match distance = %0.3f arcsec",
262 iterNum, len(tryRes.matches), tryMatchDist.distMean.asArcseconds(),
263 tryMatchDist.distStdDev.asArcseconds(), tryMatchDist.maxMatchDist.asArcseconds())
265 maxMatchDist = tryMatchDist.maxMatchDist
268 if maxMatchDist.asArcseconds() < self.config.minMatchDistanceArcSec:
270 "Max match distance = %0.3f arcsec < %0.3f = config.minMatchDistanceArcSec; "
271 "that's good enough",
272 maxMatchDist.asArcseconds(), self.config.minMatchDistanceArcSec)
274 match_tolerance.maxMatchDist = maxMatchDist
277 "Matched and fit WCS in %d iterations; "
278 "found %d matches with scatter = %0.3f +- %0.3f arcsec" %
279 (iterNum, len(tryRes.matches), tryMatchDist.distMean.asArcseconds(),
280 tryMatchDist.distStdDev.asArcseconds()))
282 for m
in res.matches:
284 m.second.set(self.
usedKey,
True)
285 exposure.setWcs(res.wcs)
287 return pipeBase.Struct(
288 refCat=loadRes.refCat,
290 scatterOnSky=res.scatterOnSky,
295 def _matchAndFitWcs(self, refCat, sourceCat, refFluxField, bbox, wcs, match_tolerance,
297 """!Match sources to reference objects and fit a WCS
299 @param[in] refCat catalog of reference objects
300 @param[in] sourceCat catalog of sources detected on the exposure (an lsst.afw.table.SourceCatalog)
301 @param[in] refFluxField field of refCat to use for flux
302 @param[in] bbox bounding box of exposure (an lsst.afw.geom.Box2I)
303 @param[in] wcs initial guess for WCS of exposure (an lsst.afw.image.Wcs)
304 @param[in] match_tolerance a MatchTolerance object (or None) specifying
305 internal tolerances to the matcher. See the MatchTolerance
306 definition in the respective matcher for the class definition.
307 @param[in] exposure exposure whose WCS is to be fit, or None; used only for the debug display
309 @return an lsst.pipe.base.Struct with these fields:
310 - matches astrometric matches, a list of lsst.afw.table.ReferenceMatch
311 - wcs the fit WCS (an lsst.afw.image.Wcs)
312 - scatterOnSky median on-sky separation between reference objects and sources in "matches"
313 (an lsst.afw.geom.Angle)
316 debug = lsstDebug.Info(__name__)
317 matchRes = self.matcher.matchObjectsToSources(
321 refFluxField=refFluxField,
322 match_tolerance=match_tolerance,
324 self.log.debug(
"Found %s matches", len(matchRes.matches))
326 frame = int(debug.frame)
329 sourceCat=matchRes.usableSourceCat,
330 matches=matchRes.matches,
337 self.log.debug(
"Fitting WCS")
338 fitRes = self.wcsFitter.fitWcs(
339 matches=matchRes.matches,
347 scatterOnSky = fitRes.scatterOnSky
349 frame = int(debug.frame)
352 sourceCat=matchRes.usableSourceCat,
353 matches=matchRes.matches,
357 title=
"Fit TAN-SIP WCS",
360 return pipeBase.Struct(
361 matches=matchRes.matches,
363 scatterOnSky=scatterOnSky,
364 match_tolerance=matchRes.match_tolerance,
def _matchAndFitWcs
Match sources to reference objects and fit a WCS.
def __init__
Construct an AstrometryTask.
def run
Load reference objects, match sources and optionally fit a WCS.
def solve
Load reference objects overlapping an exposure, match to sources and fit a WCS.
Match an input source catalog with objects from a reference catalog and solve for the WCS...