22 from __future__
import absolute_import, division, print_function
24 __all__ = [
"AstrometryConfig",
"AstrometryTask"]
26 from builtins
import range
29 import lsst.pipe.base
as pipeBase
30 from .ref_match
import RefMatchTask, RefMatchConfig
31 from .fitTanSipWcs
import FitTanSipWcsTask
32 from .display
import displayAstrometry
36 wcsFitter = pexConfig.ConfigurableField(
37 target=FitTanSipWcsTask,
40 forceKnownWcs = pexConfig.Field(
42 doc=
"If True then load reference objects and match sources but do not fit a WCS; " +
43 " this simply controls whether 'run' calls 'solve' or 'loadAndMatch'",
46 maxIter = pexConfig.RangeField(
47 doc=
"maximum number of iterations of match sources and fit WCS" +
48 "ignored if not fitting a WCS",
53 minMatchDistanceArcSec = pexConfig.RangeField(
54 doc=
"the match distance below which further iteration is pointless (arcsec); " 55 "ignored if not fitting a WCS",
71 """!Match an input source catalog with objects from a reference catalog and solve for the WCS 73 @anchor AstrometryTask_ 75 @section meas_astrom_astrometry_Contents Contents 77 - @ref meas_astrom_astrometry_Purpose 78 - @ref meas_astrom_astrometry_Initialize 79 - @ref meas_astrom_astrometry_IO 80 - @ref meas_astrom_astrometry_Config 81 - @ref meas_astrom_astrometry_Example 82 - @ref meas_astrom_astrometry_Debug 84 @section meas_astrom_astrometry_Purpose Description 86 Match input sourceCat with a reference catalog and solve for the Wcs 88 There are three steps, each performed by different subtasks: 89 - Find position reference stars that overlap the exposure 90 - Match sourceCat to position reference stars 91 - Fit a WCS based on the matches 93 @section meas_astrom_astrometry_Initialize Task initialisation 97 @section meas_astrom_astrometry_IO Invoking the Task 101 @copydoc loadAndMatch 103 @section meas_astrom_astrometry_Config Configuration parameters 105 See @ref AstrometryConfig 107 @section meas_astrom_astrometry_Example A complete example of using AstrometryTask 109 See \ref pipe_tasks_photocal_Example. 111 @section meas_astrom_astrometry_Debug Debug variables 113 The @link lsst.pipe.base.cmdLineTask.CmdLineTask command line task@endlink interface supports a 114 flag @c -d to import @b debug.py from your @c PYTHONPATH; see @ref baseDebug for more about 117 The available variables in AstrometryTask are: 119 <DT> @c display (bool) 120 <DD> If True display information at three stages: after finding reference objects, 121 after matching sources to reference objects, and after fitting the WCS; defaults to False 123 <DD> ds9 frame to use to display the reference objects; the next two frames are used 124 to display the match list and the results of the final WCS; defaults to 0 127 To investigate the @ref meas_astrom_astrometry_Debug, put something like 131 debug = lsstDebug.getInfo(name) # N.b. lsstDebug.Info(name) would call us recursively 132 if name == "lsst.meas.astrom.astrometry": 137 lsstDebug.Info = DebugInfo 139 into your debug.py file and run this task with the @c --debug flag. 141 ConfigClass = AstrometryConfig
142 _DefaultName =
"astrometricSolver" 144 def __init__(self, refObjLoader, schema=None, **kwargs):
145 """!Construct an AstrometryTask 147 @param[in] refObjLoader A reference object loader object 148 @param[in] schema ignored; available for compatibility with an older astrometry task 149 @param[in] kwargs additional keyword arguments for pipe_base Task.\_\_init\_\_ 151 RefMatchTask.__init__(self, refObjLoader, schema=schema, **kwargs)
153 if schema
is not None:
154 self.
usedKey = schema.addField(
"calib_astrometryUsed", type=
"Flag",
155 doc=
"set if source was used in astrometric calibration")
159 self.makeSubtask(
"wcsFitter")
162 def run(self, sourceCat, exposure):
163 """!Load reference objects, match sources and optionally fit a WCS 165 This is a thin layer around solve or loadAndMatch, depending on config.forceKnownWcs 167 @param[in,out] exposure exposure whose WCS is to be fit 168 The following are read only: 170 - calib (may be absent) 171 - filter (may be unset) 172 - detector (if wcs is pure tangent; may be absent) 173 The following are updated: 174 - wcs (the initial value is used as an initial guess, and is required) 175 @param[in] sourceCat catalog of sources detected on the exposure (an lsst.afw.table.SourceCatalog) 176 @return an lsst.pipe.base.Struct with these fields: 177 - refCat reference object catalog of objects that overlap the exposure (with some margin) 178 (an lsst::afw::table::SimpleCatalog) 179 - matches astrometric matches, a list of lsst.afw.table.ReferenceMatch 180 - scatterOnSky median on-sky separation between reference objects and sources in "matches" 181 (an lsst.afw.geom.Angle), or None if config.forceKnownWcs True 182 - matchMeta metadata needed to unpersist matches (an lsst.daf.base.PropertyList) 184 if self.config.forceKnownWcs:
185 res = self.
loadAndMatch(exposure=exposure, sourceCat=sourceCat)
186 res.scatterOnSky =
None 188 res = self.
solve(exposure=exposure, sourceCat=sourceCat)
192 def solve(self, exposure, sourceCat):
193 """!Load reference objects overlapping an exposure, match to sources and fit a WCS 195 @return an lsst.pipe.base.Struct with these fields: 196 - refCat reference object catalog of objects that overlap the exposure (with some margin) 197 (an lsst::afw::table::SimpleCatalog) 198 - matches astrometric matches, a list of lsst.afw.table.ReferenceMatch 199 - scatterOnSky median on-sky separation between reference objects and sources in "matches" 200 (an lsst.afw.geom.Angle) 201 - matchMeta metadata needed to unpersist matches (an lsst.daf.base.PropertyList) 203 @note ignores config.forceKnownWcs 213 filterName=expMd.filterName,
219 filterName=expMd.filterName,
224 frame = int(debug.frame)
226 refCat=loadRes.refCat,
231 title=
"Reference catalog",
236 match_tolerance =
None 237 for i
in range(self.config.maxIter):
241 refCat=loadRes.refCat,
243 refFluxField=loadRes.fluxField,
247 match_tolerance=match_tolerance,
249 except Exception
as e:
252 self.log.info(
"Fit WCS iter %d failed; using previous iteration: %s" % (iterNum, e))
258 match_tolerance = tryRes.match_tolerance
261 "Match and fit WCS iteration %d: found %d matches with scatter = %0.3f +- %0.3f arcsec; " 262 "max match distance = %0.3f arcsec",
263 iterNum, len(tryRes.matches), tryMatchDist.distMean.asArcseconds(),
264 tryMatchDist.distStdDev.asArcseconds(), tryMatchDist.maxMatchDist.asArcseconds())
266 maxMatchDist = tryMatchDist.maxMatchDist
269 if maxMatchDist.asArcseconds() < self.config.minMatchDistanceArcSec:
271 "Max match distance = %0.3f arcsec < %0.3f = config.minMatchDistanceArcSec; " 272 "that's good enough",
273 maxMatchDist.asArcseconds(), self.config.minMatchDistanceArcSec)
275 match_tolerance.maxMatchDist = maxMatchDist
278 "Matched and fit WCS in %d iterations; " 279 "found %d matches with scatter = %0.3f +- %0.3f arcsec" %
280 (iterNum, len(tryRes.matches), tryMatchDist.distMean.asArcseconds(),
281 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.geom.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.geom.Wcs) 312 - scatterOnSky median on-sky separation between reference objects and sources in "matches" 313 (an lsst.afw.geom.Angle) 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(self, refCat, sourceCat, refFluxField, bbox, wcs, match_tolerance, exposure=None)
Match sources to reference objects and fit a WCS.
def solve(self, exposure, sourceCat)
Load reference objects overlapping an exposure, match to sources and fit a WCS.
def _computeMatchStatsOnSky(self, matchList)
def _getExposureMetadata(self, exposure)
Extract metadata from an exposure.
def run(self, sourceCat, exposure)
Load reference objects, match sources and optionally fit a WCS.
Match an input source catalog with objects from a reference catalog.
def __init__(self, refObjLoader, schema=None, kwargs)
Construct an AstrometryTask.
def displayAstrometry(refCat=None, sourceCat=None, distortedCentroidKey=None, bbox=None, exposure=None, matches=None, frame=1, title="", pause=True)
def loadAndMatch(self, exposure, sourceCat)
Load reference objects overlapping an exposure and match to sources detected on that exposure...
Match an input source catalog with objects from a reference catalog and solve for the WCS...