23 __all__ = [
"AstrometryConfig",
"AstrometryTask"]
26 from astropy
import units
31 from .ref_match
import RefMatchTask, RefMatchConfig
32 from .fitTanSipWcs
import FitTanSipWcsTask
33 from .display
import displayAstrometry
37 """Config for AstrometryTask.
39 wcsFitter = pexConfig.ConfigurableField(
40 target=FitTanSipWcsTask,
43 forceKnownWcs = pexConfig.Field(
45 doc=
"If True then load reference objects and match sources but do not fit a WCS; "
46 "this simply controls whether 'run' calls 'solve' or 'loadAndMatch'",
49 maxIter = pexConfig.RangeField(
50 doc=
"maximum number of iterations of match sources and fit WCS"
51 "ignored if not fitting a WCS",
56 minMatchDistanceArcSec = pexConfig.RangeField(
57 doc=
"the match distance below which further iteration is pointless (arcsec); "
58 "ignored if not fitting a WCS",
63 maxMeanDistanceArcsec = pexConfig.RangeField(
64 doc=
"Maximum mean on-sky distance (in arcsec) between matched source and rerference "
65 "objects post-fit. A mean distance greater than this threshold raises a TaskError "
66 "and the WCS fit is considered a failure. The default is set to the maximum tolerated "
67 "by the external global calibration (e.g. jointcal) step for conceivable recovery. "
68 "Appropriate value will be dataset and workflow dependent.",
73 doMagnitudeOutlierRejection = pexConfig.Field(
75 doc=(
"If True then a rough zeropoint will be computed from matched sources "
76 "and outliers will be rejected in the iterations."),
79 magnitudeOutlierRejectionNSigma = pexConfig.Field(
81 doc=(
"Number of sigma (measured from the distribution) in magnitude "
82 "for a potential reference/source match to be rejected during "
100 """Match an input source catalog with objects from a reference catalog and
103 This task is broken into two main subasks: matching and WCS fitting which
104 are very interactive. The matching here can be considered in part a first
105 pass WCS fitter due to the fitter's sensitivity to outliers.
109 refObjLoader : `lsst.meas.algorithms.ReferenceLoader`
110 A reference object loader object
111 schema : `lsst.afw.table.Schema`
112 Used to set "calib_astrometry_used" flag in output source catalog.
114 additional keyword arguments for pipe_base
115 `lsst.pipe.base.Task.__init__`
117 ConfigClass = AstrometryConfig
118 _DefaultName =
"astrometricSolver"
120 def __init__(self, refObjLoader, schema=None, **kwargs):
121 RefMatchTask.__init__(self, refObjLoader, **kwargs)
123 if schema
is not None:
124 self.
usedKeyusedKey = schema.addField(
"calib_astrometry_used", type=
"Flag",
125 doc=
"set if source was used in astrometric calibration")
129 self.makeSubtask(
"wcsFitter")
132 def run(self, sourceCat, exposure):
133 """Load reference objects, match sources and optionally fit a WCS.
135 This is a thin layer around solve or loadAndMatch, depending on
136 config.forceKnownWcs.
140 exposure : `lsst.afw.image.Exposure`
141 exposure whose WCS is to be fit
142 The following are read only:
145 - photoCalib (may be absent)
146 - filter (may be unset)
147 - detector (if wcs is pure tangent; may be absent)
149 The following are updated:
151 - wcs (the initial value is used as an initial guess, and is
154 sourceCat : `lsst.afw.table.SourceCatalog`
155 catalog of sources detected on the exposure
159 result : `lsst.pipe.base.Struct`
162 - ``refCat`` : reference object catalog of objects that overlap the
163 exposure (with some margin) (`lsst.afw.table.SimpleCatalog`).
164 - ``matches`` : astrometric matches
165 (`list` of `lsst.afw.table.ReferenceMatch`).
166 - ``scatterOnSky`` : median on-sky separation between reference
167 objects and sources in "matches"
168 (`lsst.afw.geom.Angle`) or `None` if config.forceKnownWcs True
169 - ``matchMeta`` : metadata needed to unpersist matches
170 (`lsst.daf.base.PropertyList`)
173 raise RuntimeError(
"Running matcher task with no refObjLoader set in __init__ or setRefObjLoader")
174 if self.config.forceKnownWcs:
175 res = self.
loadAndMatchloadAndMatch(exposure=exposure, sourceCat=sourceCat)
176 res.scatterOnSky =
None
178 res = self.
solvesolve(exposure=exposure, sourceCat=sourceCat)
182 def solve(self, exposure, sourceCat):
183 """Load reference objects overlapping an exposure, match to sources and
188 result : `lsst.pipe.base.Struct`
189 Result struct with components:
191 - ``refCat`` : reference object catalog of objects that overlap the
192 exposure (with some margin) (`lsst::afw::table::SimpleCatalog`).
193 - ``matches`` : astrometric matches
194 (`list` of `lsst.afw.table.ReferenceMatch`).
195 - ``scatterOnSky`` : median on-sky separation between reference
196 objects and sources in "matches" (`lsst.geom.Angle`)
197 - ``matchMeta`` : metadata needed to unpersist matches
198 (`lsst.daf.base.PropertyList`)
203 If the measured mean on-sky distance between the matched source and
204 reference objects is greater than
205 ``self.config.maxMeanDistanceArcsec``.
209 ignores config.forceKnownWcs
212 raise RuntimeError(
"Running matcher task with no refObjLoader set in __init__ or setRefObjLoader")
218 sourceSelection = self.sourceSelector.
run(sourceCat)
220 self.log.info(
"Purged %d sources, leaving %d good sources" %
221 (len(sourceCat) - len(sourceSelection.sourceCat),
222 len(sourceSelection.sourceCat)))
227 filterName=expMd.filterName,
228 photoCalib=expMd.photoCalib,
232 refSelection = self.referenceSelector.
run(loadRes.refCat)
234 matchMeta = self.
refObjLoaderrefObjLoader.getMetadataBox(
237 filterName=expMd.filterName,
238 photoCalib=expMd.photoCalib,
243 frame = int(debug.frame)
245 refCat=refSelection.sourceCat,
246 sourceCat=sourceSelection.sourceCat,
250 title=
"Reference catalog",
255 match_tolerance =
None
256 for i
in range(self.config.maxIter):
260 refCat=refSelection.sourceCat,
262 goodSourceCat=sourceSelection.sourceCat,
263 refFluxField=loadRes.fluxField,
267 match_tolerance=match_tolerance,
269 except Exception
as e:
272 self.log.info(
"Fit WCS iter %d failed; using previous iteration: %s" % (iterNum, e))
278 match_tolerance = tryRes.match_tolerance
281 "Match and fit WCS iteration %d: found %d matches with on-sky distance mean "
282 "= %0.3f +- %0.3f arcsec; max match distance = %0.3f arcsec",
283 iterNum, len(tryRes.matches), tryMatchDist.distMean.asArcseconds(),
284 tryMatchDist.distStdDev.asArcseconds(), tryMatchDist.maxMatchDist.asArcseconds())
286 maxMatchDist = tryMatchDist.maxMatchDist
289 if maxMatchDist.asArcseconds() < self.config.minMatchDistanceArcSec:
291 "Max match distance = %0.3f arcsec < %0.3f = config.minMatchDistanceArcSec; "
292 "that's good enough",
293 maxMatchDist.asArcseconds(), self.config.minMatchDistanceArcSec)
295 match_tolerance.maxMatchDist = maxMatchDist
298 "Matched and fit WCS in %d iterations; "
299 "found %d matches with on-sky distance mean and scatter = %0.3f +- %0.3f arcsec",
300 iterNum, len(tryRes.matches), tryMatchDist.distMean.asArcseconds(),
301 tryMatchDist.distStdDev.asArcseconds())
302 if tryMatchDist.distMean.asArcseconds() > self.config.maxMeanDistanceArcsec:
303 raise pipeBase.TaskError(
304 "Fatal astrometry failure detected: mean on-sky distance = %0.3f arcsec > %0.3f "
305 "(maxMeanDistanceArcsec)" %
306 (tryMatchDist.distMean.asArcseconds(), self.config.maxMeanDistanceArcsec))
307 for m
in res.matches:
309 m.second.set(self.
usedKeyusedKey,
True)
310 exposure.setWcs(res.wcs)
313 md = exposure.getMetadata()
314 md[
'SFM_ASTROM_OFFSET_MEAN'] = tryMatchDist.distMean.asArcseconds()
315 md[
'SFM_ASTROM_OFFSET_STD'] = tryMatchDist.distStdDev.asArcseconds()
317 return pipeBase.Struct(
318 refCat=refSelection.sourceCat,
320 scatterOnSky=res.scatterOnSky,
325 def _matchAndFitWcs(self, refCat, sourceCat, goodSourceCat, refFluxField, bbox, wcs, match_tolerance,
327 """Match sources to reference objects and fit a WCS.
331 refCat : `lsst.afw.table.SimpleCatalog`
332 catalog of reference objects
333 sourceCat : `lsst.afw.table.SourceCatalog`
334 catalog of sources detected on the exposure
335 goodSourceCat : `lsst.afw.table.SourceCatalog`
336 catalog of down-selected good sources detected on the exposure
338 field of refCat to use for flux
339 bbox : `lsst.geom.Box2I`
340 bounding box of exposure
341 wcs : `lsst.afw.geom.SkyWcs`
342 initial guess for WCS of exposure
343 match_tolerance : `lsst.meas.astrom.MatchTolerance`
344 a MatchTolerance object (or None) specifying
345 internal tolerances to the matcher. See the MatchTolerance
346 definition in the respective matcher for the class definition.
347 exposure : `lsst.afw.image.Exposure`
348 exposure whose WCS is to be fit, or None; used only for the debug
353 result : `lsst.pipe.base.Struct`
354 Result struct with components:
356 - ``matches``: astrometric matches
357 (`list` of `lsst.afw.table.ReferenceMatch`).
358 - ``wcs``: the fit WCS (lsst.afw.geom.SkyWcs).
359 - ``scatterOnSky`` : median on-sky separation between reference
360 objects and sources in "matches" (`lsst.afw.geom.Angle`).
365 sourceFluxField =
"slot_%sFlux_instFlux" % (self.config.sourceFluxType)
367 matchRes = self.matcher.matchObjectsToSources(
369 sourceCat=goodSourceCat,
371 sourceFluxField=sourceFluxField,
372 refFluxField=refFluxField,
373 match_tolerance=match_tolerance,
375 self.log.debug(
"Found %s matches", len(matchRes.matches))
377 frame = int(debug.frame)
380 sourceCat=matchRes.usableSourceCat,
381 matches=matchRes.matches,
388 if self.config.doMagnitudeOutlierRejection:
391 matches = matchRes.matches
393 self.log.debug(
"Fitting WCS")
394 fitRes = self.wcsFitter.fitWcs(
403 scatterOnSky = fitRes.scatterOnSky
405 frame = int(debug.frame)
408 sourceCat=matchRes.usableSourceCat,
413 title=
"Fit TAN-SIP WCS",
416 return pipeBase.Struct(
419 scatterOnSky=scatterOnSky,
420 match_tolerance=matchRes.match_tolerance,
423 def _removeMagnitudeOutliers(self, sourceFluxField, refFluxField, matchesIn):
424 """Remove magnitude outliers, computing a simple zeropoint.
428 sourceFluxField : `str`
429 Field in source catalog for instrumental fluxes.
431 Field in reference catalog for fluxes (nJy).
432 matchesIn : `list` [`lsst.afw.table.ReferenceMatch`]
433 List of source/reference matches input
437 matchesOut : `list` [`lsst.afw.table.ReferenceMatch`]
438 List of source/reference matches with magnitude
441 nMatch = len(matchesIn)
442 sourceMag = np.zeros(nMatch)
443 refMag = np.zeros(nMatch)
444 for i, match
in enumerate(matchesIn):
445 sourceMag[i] = -2.5*np.log10(match[1][sourceFluxField])
446 refMag[i] = (match[0][refFluxField]*units.nJy).to_value(units.ABmag)
448 deltaMag = refMag - sourceMag
450 goodDelta, = np.where(np.isfinite(deltaMag))
451 zp = np.median(deltaMag[goodDelta])
455 zpSigma = np.clip(scipy.stats.median_abs_deviation(deltaMag[goodDelta], scale=
'normal'),
459 self.log.info(
"Rough zeropoint from astrometry matches is %.4f +/- %.4f.",
462 goodStars = goodDelta[(np.abs(deltaMag[goodDelta] - zp)
463 <= self.config.magnitudeOutlierRejectionNSigma*zpSigma)]
465 nOutlier = nMatch - goodStars.size
466 self.log.info(
"Removed %d magnitude outliers out of %d total astrometry matches.",
470 for matchInd
in goodStars:
471 matchesOut.append(matchesIn[matchInd])
def solve(self, exposure, sourceCat)
def _removeMagnitudeOutliers(self, sourceFluxField, refFluxField, matchesIn)
def _matchAndFitWcs(self, refCat, sourceCat, goodSourceCat, refFluxField, bbox, wcs, match_tolerance, exposure=None)
def run(self, sourceCat, exposure)
def __init__(self, refObjLoader, schema=None, **kwargs)
def _computeMatchStatsOnSky(self, matchList)
def loadAndMatch(self, exposure, sourceCat)
def _getExposureMetadata(self, exposure)
def displayAstrometry(refCat=None, sourceCat=None, distortedCentroidKey=None, bbox=None, exposure=None, matches=None, frame=1, title="", pause=True)