23__all__ = [
"AstrometryConfig",
"AstrometryTask"]
26from astropy
import units
31from lsst.utils.timer
import timeMethod
32from .ref_match
import RefMatchTask, RefMatchConfig
33from .fitTanSipWcs
import FitTanSipWcsTask
34from .display
import displayAstrometry
38 """Config for AstrometryTask.
40 wcsFitter = pexConfig.ConfigurableField(
41 target=FitTanSipWcsTask,
44 forceKnownWcs = pexConfig.Field(
46 doc=
"If True then load reference objects and match sources but do not fit a WCS; "
47 "this simply controls whether 'run' calls 'solve' or 'loadAndMatch'",
50 maxIter = pexConfig.RangeField(
51 doc=
"maximum number of iterations of match sources and fit WCS"
52 "ignored if not fitting a WCS",
57 minMatchDistanceArcSec = pexConfig.RangeField(
58 doc=
"the match distance below which further iteration is pointless (arcsec); "
59 "ignored if not fitting a WCS",
64 maxMeanDistanceArcsec = pexConfig.RangeField(
65 doc=
"Maximum mean on-sky distance (in arcsec) between matched source and rerference "
66 "objects post-fit. A mean distance greater than this threshold raises a TaskError "
67 "and the WCS fit is considered a failure. The default is set to the maximum tolerated "
68 "by the external global calibration (e.g. jointcal) step for conceivable recovery. "
69 "Appropriate value will be dataset and workflow dependent.",
74 doMagnitudeOutlierRejection = pexConfig.Field(
76 doc=(
"If True then a rough zeropoint will be computed from matched sources "
77 "and outliers will be rejected in the iterations."),
80 magnitudeOutlierRejectionNSigma = pexConfig.Field(
82 doc=(
"Number of sigma (measured from the distribution) in magnitude "
83 "for a potential reference/source match to be rejected during "
101 """Match an input source catalog with objects from a reference catalog and
104 This task
is broken into two main subasks: matching
and WCS fitting which
105 are very interactive. The matching here can be considered
in part a first
106 pass WCS fitter due to the fitter
's sensitivity to outliers.
110 refObjLoader : `lsst.meas.algorithms.ReferenceLoader`
111 A reference object loader object; gen3 pipeline tasks will pass `
None`
112 and call `setRefObjLoader`
in `runQuantum`.
114 Used to set
"calib_astrometry_used" flag
in output source catalog.
116 Additional keyword arguments
for pipe_base
117 `lsst.pipe.base.Task.__init__`.
119 ConfigClass = AstrometryConfig
120 _DefaultName = "astrometricSolver"
122 def __init__(self, refObjLoader=None, schema=None, **kwargs):
123 RefMatchTask.__init__(self, refObjLoader=refObjLoader, **kwargs)
125 if schema
is not None:
126 self.
usedKey = schema.addField(
"calib_astrometry_used", type=
"Flag",
127 doc=
"set if source was used in astrometric calibration")
131 self.makeSubtask(
"wcsFitter")
134 def run(self, sourceCat, exposure):
135 """Load reference objects, match sources and optionally fit a WCS.
137 This is a thin layer around solve
or loadAndMatch, depending on
138 config.forceKnownWcs.
143 exposure whose WCS
is to be fit
144 The following are read only:
147 - filter (may be unset)
148 - detector (
if wcs
is pure tangent; may be absent)
150 The following are updated:
152 - wcs (the initial value
is used
as an initial guess,
and is
156 catalog of sources detected on the exposure
160 result : `lsst.pipe.base.Struct`
163 - ``refCat`` : reference object catalog of objects that overlap the
165 - ``matches`` : astrometric matches
167 - ``scatterOnSky`` : median on-sky separation between reference
168 objects
and sources
in "matches"
169 (`lsst.afw.geom.Angle`)
or `
None`
if config.forceKnownWcs
True
170 - ``matchMeta`` : metadata needed to unpersist matches
174 raise RuntimeError(
"Running matcher task with no refObjLoader set in __init__ or setRefObjLoader")
175 if self.config.forceKnownWcs:
176 res = self.
loadAndMatch(exposure=exposure, sourceCat=sourceCat)
177 res.scatterOnSky =
None
179 res = self.
solve(exposure=exposure, sourceCat=sourceCat)
183 def solve(self, exposure, sourceCat):
184 """Load reference objects overlapping an exposure, match to sources and
189 result : `lsst.pipe.base.Struct`
190 Result struct with components:
192 - ``refCat`` : reference object catalog of objects that overlap the
193 exposure (
with some margin) (`lsst::afw::table::SimpleCatalog`).
194 - ``matches`` : astrometric matches
196 - ``scatterOnSky`` : median on-sky separation between reference
198 - ``matchMeta`` : metadata needed to unpersist matches
204 If the measured mean on-sky distance between the matched source
and
205 reference objects
is greater than
206 ``self.config.maxMeanDistanceArcsec``.
210 ignores config.forceKnownWcs
213 raise RuntimeError(
"Running matcher task with no refObjLoader set in __init__ or setRefObjLoader")
219 sourceSelection = self.sourceSelector.run(sourceCat)
221 self.log.info(
"Purged %d sources, leaving %d good sources",
222 len(sourceCat) - len(sourceSelection.sourceCat),
223 len(sourceSelection.sourceCat))
228 filterName=expMd.filterName,
232 refSelection = self.referenceSelector.run(loadRes.refCat)
237 filterName=expMd.filterName,
242 frame = int(debug.frame)
244 refCat=refSelection.sourceCat,
245 sourceCat=sourceSelection.sourceCat,
249 title=
"Reference catalog",
254 match_tolerance =
None
256 for i
in range(self.config.maxIter):
261 refCat=refSelection.sourceCat,
263 goodSourceCat=sourceSelection.sourceCat,
264 refFluxField=loadRes.fluxField,
268 match_tolerance=match_tolerance,
270 except Exception
as e:
274 self.log.info(
"Fit WCS iter %d failed; using previous iteration: %s", iterNum, e)
278 self.log.info(
"Fit WCS iter %d failed: %s" % (iterNum, e))
282 match_tolerance = tryRes.match_tolerance
285 "Match and fit WCS iteration %d: found %d matches with on-sky distance mean and "
286 "scatter = %0.3f +- %0.3f arcsec; max match distance = %0.3f arcsec",
287 iterNum, len(tryRes.matches), tryMatchDist.distMean.asArcseconds(),
288 tryMatchDist.distStdDev.asArcseconds(), tryMatchDist.maxMatchDist.asArcseconds())
290 maxMatchDist = tryMatchDist.maxMatchDist
293 if maxMatchDist.asArcseconds() < self.config.minMatchDistanceArcSec:
295 "Max match distance = %0.3f arcsec < %0.3f = config.minMatchDistanceArcSec; "
296 "that's good enough",
297 maxMatchDist.asArcseconds(), self.config.minMatchDistanceArcSec)
299 match_tolerance.maxMatchDist = maxMatchDist
302 self.log.info(
"Matched and fit WCS in %d iterations; "
303 "found %d matches with mean and scatter = %0.3f +- %0.3f arcsec" %
304 (iterNum, len(tryRes.matches), tryMatchDist.distMean.asArcseconds(),
305 tryMatchDist.distStdDev.asArcseconds()))
306 if tryMatchDist.distMean.asArcseconds() > self.config.maxMeanDistanceArcsec:
307 self.log.info(
"Assigning as a fit failure: mean on-sky distance = %0.3f arcsec > %0.3f "
308 "(maxMeanDistanceArcsec)" % (tryMatchDist.distMean.asArcseconds(),
309 self.config.maxMeanDistanceArcsec))
313 self.log.warning(
"WCS fit failed. Setting exposure's WCS to None and coord_ra & coord_dec "
314 "cols in sourceCat to nan.")
315 sourceCat[
"coord_ra"] = np.nan
316 sourceCat[
"coord_dec"] = np.nan
317 exposure.setWcs(
None)
321 for m
in res.matches:
323 m.second.set(self.
usedKey,
True)
324 exposure.setWcs(res.wcs)
325 matches = res.matches
326 scatterOnSky = res.scatterOnSky
332 md = exposure.getMetadata()
333 md[
'SFM_ASTROM_OFFSET_MEAN'] = tryMatchDist.distMean.asArcseconds()
334 md[
'SFM_ASTROM_OFFSET_STD'] = tryMatchDist.distStdDev.asArcseconds()
336 return pipeBase.Struct(
337 refCat=refSelection.sourceCat,
339 scatterOnSky=scatterOnSky,
344 def _matchAndFitWcs(self, refCat, sourceCat, goodSourceCat, refFluxField, bbox, wcs, match_tolerance,
346 """Match sources to reference objects and fit a WCS.
351 catalog of reference objects
353 catalog of sources detected on the exposure
355 catalog of down-selected good sources detected on the exposure
357 field of refCat to use
for flux
359 bounding box of exposure
361 initial guess
for WCS of exposure
363 a MatchTolerance object (
or None) specifying
364 internal tolerances to the matcher. See the MatchTolerance
365 definition
in the respective matcher
for the
class definition.
367 exposure whose WCS
is to be fit,
or None; used only
for the debug
372 result : `lsst.pipe.base.Struct`
373 Result struct
with components:
375 - ``matches``: astrometric matches
378 - ``scatterOnSky`` : median on-sky separation between reference
379 objects
and sources
in "matches" (`lsst.afw.geom.Angle`).
384 sourceFluxField =
"slot_%sFlux_instFlux" % (self.config.sourceFluxType)
386 matchRes = self.matcher.matchObjectsToSources(
388 sourceCat=goodSourceCat,
390 sourceFluxField=sourceFluxField,
391 refFluxField=refFluxField,
392 match_tolerance=match_tolerance,
394 self.log.debug(
"Found %s matches", len(matchRes.matches))
396 frame = int(debug.frame)
399 sourceCat=matchRes.usableSourceCat,
400 matches=matchRes.matches,
407 if self.config.doMagnitudeOutlierRejection:
410 matches = matchRes.matches
412 self.log.debug(
"Fitting WCS")
413 fitRes = self.wcsFitter.fitWcs(
422 scatterOnSky = fitRes.scatterOnSky
424 frame = int(debug.frame)
427 sourceCat=matchRes.usableSourceCat,
432 title=
"Fit TAN-SIP WCS",
435 return pipeBase.Struct(
438 scatterOnSky=scatterOnSky,
439 match_tolerance=matchRes.match_tolerance,
443 """Remove magnitude outliers, computing a simple zeropoint.
447 sourceFluxField : `str`
448 Field in source catalog
for instrumental fluxes.
450 Field
in reference catalog
for fluxes (nJy).
452 List of source/reference matches input
457 List of source/reference matches
with magnitude
460 nMatch = len(matchesIn)
461 sourceMag = np.zeros(nMatch)
462 refMag = np.zeros(nMatch)
463 for i, match
in enumerate(matchesIn):
464 sourceMag[i] = -2.5*np.log10(match[1][sourceFluxField])
465 refMag[i] = (match[0][refFluxField]*units.nJy).to_value(units.ABmag)
467 deltaMag = refMag - sourceMag
469 goodDelta, = np.where(np.isfinite(deltaMag))
470 zp = np.median(deltaMag[goodDelta])
474 zpSigma = np.clip(scipy.stats.median_abs_deviation(deltaMag[goodDelta], scale=
'normal'),
478 self.log.info(
"Rough zeropoint from astrometry matches is %.4f +/- %.4f.",
481 goodStars = goodDelta[(np.abs(deltaMag[goodDelta] - zp)
482 <= self.config.magnitudeOutlierRejectionNSigma*zpSigma)]
484 nOutlier = nMatch - goodStars.size
485 self.log.info(
"Removed %d magnitude outliers out of %d total astrometry matches.",
489 for matchInd
in goodStars:
490 matchesOut.append(matchesIn[matchInd])
A 2-dimensional celestial WCS that transform pixels to ICRS RA/Dec, using the LSST standard for pixel...
A class to contain the data, WCS, and other information needed to describe an image of the sky.
Defines the fields and offsets for a table.
Custom catalog class for record/table subclasses that are guaranteed to have an ID,...
Class for storing ordered metadata with comments.
A class representing an angle.
An integer coordinate rectangle.
_matchAndFitWcs(self, refCat, sourceCat, goodSourceCat, refFluxField, bbox, wcs, match_tolerance, exposure=None)
__init__(self, refObjLoader=None, schema=None, **kwargs)
solve(self, exposure, sourceCat)
_removeMagnitudeOutliers(self, sourceFluxField, refFluxField, matchesIn)
_computeMatchStatsOnSky(self, matchList)
_getExposureMetadata(self, exposure)
loadAndMatch(self, exposure, sourceCat)
Lightweight representation of a geometric match between two records.